d3f9f1f8 |
1 | <?php // $Id$ |
2 | // These functions are required very early in the Moodle |
3 | // setup process, before any of the main libraries are |
4 | // loaded. |
5 | |
6 | |
7 | /** |
8 | * Initializes our performance info early. |
9 | * |
10 | * Pairs up with get_performance_info() which is actually |
11 | * in moodlelib.php. This function is here so that we can |
12 | * call it before all the libs are pulled in. |
13 | * |
14 | * @uses $PERF |
15 | */ |
16 | function init_performance_info() { |
17 | |
18 | global $PERF; |
19 | |
20 | $PERF = new Object; |
21 | $PERF->dbqueries = 0; |
22 | $PERF->logwrites = 0; |
23 | if (function_exists('microtime')) { |
24 | $PERF->starttime = microtime(); |
25 | } |
26 | if (function_exists('memory_get_usage')) { |
27 | $PERF->startmemory = memory_get_usage(); |
28 | } |
29 | if (function_exists('posix_times')) { |
30 | $PERF->startposixtimes = posix_times(); |
31 | } |
32 | } |
33 | |
76f3815b |
34 | /** |
35 | * Function to raise the memory limit to a new value. |
36 | * Will respect the memory limit if it is higher, thus allowing |
37 | * settings in php.ini, apache conf or command line switches |
38 | * to override it |
39 | * |
40 | * The memory limit should be expressed with a string (eg:'64M') |
41 | * |
42 | * @param string $newlimit the new memory limit |
43 | * @return bool |
44 | */ |
45 | function raise_memory_limit ($newlimit) { |
46 | |
47 | if (empty($newlimit)) { |
48 | return false; |
49 | } |
50 | |
51 | $cur = @ini_get('memory_limit'); |
52 | if (empty($cur)) { |
53 | // if php is compiled without --enable-memory-limits |
54 | // apparently memory_limit is set to '' |
55 | $cur=0; |
56 | } else { |
57 | if ($cur == -1){ |
58 | return true; // unlimited mem! |
59 | } |
60 | $cur = get_real_size($cur); |
61 | } |
62 | |
63 | $new = get_real_size($newlimit); |
64 | if ($new > $cur) { |
65 | ini_set('memory_limit', $newlimit); |
66 | return true; |
67 | } |
68 | return false; |
69 | } |
70 | |
71 | /** |
72 | * Converts numbers like 10M into bytes. |
73 | * |
74 | * @param mixed $size The size to be converted |
75 | * @return mixed |
76 | */ |
77 | function get_real_size($size=0) { |
78 | if (!$size) { |
79 | return 0; |
80 | } |
81 | $scan['MB'] = 1048576; |
82 | $scan['Mb'] = 1048576; |
83 | $scan['M'] = 1048576; |
84 | $scan['m'] = 1048576; |
85 | $scan['KB'] = 1024; |
86 | $scan['Kb'] = 1024; |
87 | $scan['K'] = 1024; |
88 | $scan['k'] = 1024; |
89 | |
90 | while (list($key) = each($scan)) { |
91 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
92 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
93 | break; |
94 | } |
95 | } |
96 | return $size; |
97 | } |
98 | |
d3f9f1f8 |
99 | /** |
100 | * Create a directory. |
101 | * |
102 | * @uses $CFG |
103 | * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 |
104 | * param bool $shownotices If true then notification messages will be printed out on error. |
105 | * @return string|false Returns full path to directory if successful, false if not |
106 | */ |
107 | function make_upload_directory($directory, $shownotices=true) { |
108 | |
109 | global $CFG; |
110 | |
111 | $currdir = $CFG->dataroot; |
112 | |
113 | umask(0000); |
114 | |
115 | if (!file_exists($currdir)) { |
116 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
117 | if ($shownotices) { |
118 | echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '. |
119 | $currdir .' with web server write access</div>'."<br />\n"; |
120 | } |
121 | return false; |
122 | } |
d100b8a0 |
123 | } |
124 | |
125 | // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open |
126 | if (!file_exists($currdir.'/.htaccess')) { |
d3f9f1f8 |
127 | if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety |
d389b128 |
128 | @fwrite($handle, "deny from all\r\nAllowOverride None\r\n"); |
d3f9f1f8 |
129 | @fclose($handle); |
130 | } |
131 | } |
132 | |
133 | $dirarray = explode('/', $directory); |
134 | |
135 | foreach ($dirarray as $dir) { |
136 | $currdir = $currdir .'/'. $dir; |
137 | if (! file_exists($currdir)) { |
138 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
139 | if ($shownotices) { |
140 | echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('. |
141 | $currdir .')</div>'."<br />\n"; |
142 | } |
143 | return false; |
144 | } |
145 | //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it |
146 | } |
147 | } |
148 | |
149 | return $currdir; |
150 | } |
151 | |
7104d80d |
152 | /** |
153 | * This function will introspect inside DB to detect it it's a UTF-8 DB or no |
154 | * Used from setup.php to set correctly "set names" when the installation |
155 | * process is performed without the initial and beautiful installer |
156 | */ |
157 | function setup_is_unicodedb() { |
158 | |
7ddf8aa5 |
159 | global $CFG, $db, $INSTALL; |
7104d80d |
160 | |
161 | $unicodedb = false; |
7ddf8aa5 |
162 | |
163 | // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set. |
164 | // we need to get dbtype from the right variable |
165 | if (!empty($INSTALL['dbtype'])) { |
166 | $dbtype = $INSTALL['dbtype']; |
167 | } else { |
168 | $dbtype = $CFG->dbtype; |
169 | } |
7104d80d |
170 | |
7ddf8aa5 |
171 | switch ($dbtype) { |
7104d80d |
172 | case 'mysql': |
7104d80d |
173 | $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'"); |
174 | if ($rs && $rs->RecordCount() > 0) { |
175 | $records = $rs->GetAssoc(true); |
176 | $encoding = $records['character_set_database']['Value']; |
177 | if (strtoupper($encoding) == 'UTF8') { |
178 | $unicodedb = true; |
179 | } |
180 | } |
181 | break; |
182 | case 'postgres7': |
183 | /// Get PostgreSQL server_encoding value |
184 | $rs = $db->Execute("SHOW server_encoding"); |
185 | if ($rs && $rs->RecordCount() > 0) { |
186 | $encoding = $rs->fields['server_encoding']; |
2544a80d |
187 | if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') { |
7104d80d |
188 | $unicodedb = true; |
189 | } |
190 | } |
191 | break; |
bf052299 |
192 | case 'mssql': |
b2ad0336 |
193 | case 'mssql_n': |
97ab73aa |
194 | case 'odbc_mssql': |
bf052299 |
195 | /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32) |
196 | $unicodedb = true; |
010f2a39 |
197 | break; |
0700134d |
198 | case 'oci8po': |
199 | /// Get Oracle DB character set value |
200 | $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'"); |
201 | if ($rs && $rs->RecordCount() > 0) { |
202 | $encoding = $rs->fields['value']; |
203 | if (strtoupper($encoding) == 'AL32UTF8') { |
204 | $unicodedb = true; |
205 | } |
206 | } |
207 | break; |
7104d80d |
208 | } |
209 | return $unicodedb; |
210 | } |
211 | |
419e1d93 |
212 | function init_memcached() { |
213 | global $CFG, $MCACHE; |
214 | |
215 | if (!function_exists('memcache_connect')) { |
216 | debugging("Memcached is set to true but the memcached extension is not installed"); |
217 | return false; |
218 | } |
219 | |
220 | $hosts = split(',', $CFG->memcachedhosts); |
221 | $MCACHE = new Memcache; |
9c967c33 |
222 | if (count($hosts) === 1 && !empty($CFG->memcachedpconn)) { |
419e1d93 |
223 | // the faster pconnect is only available |
224 | // for single-server setups |
9c967c33 |
225 | // NOTE: PHP-PECL client is buggy and pconnect() |
226 | // will segfault if the server is unavailable |
419e1d93 |
227 | $MCACHE->pconnect($hosts[0]); |
228 | } else { |
229 | // multi-host setup will share key space |
230 | foreach ($hosts as $host) { |
231 | $host = trim($host); |
232 | $MCACHE->addServer($host); |
233 | } |
234 | } |
235 | |
236 | return true; |
237 | } |
238 | |
2142d492 |
239 | function init_eaccelerator() { |
240 | global $CFG, $MCACHE; |
241 | |
242 | include_once($CFG->libdir . '/eaccelerator.class.php'); |
243 | $MCACHE = new eaccelerator; |
244 | if ($MCACHE->status) { |
245 | return true; |
246 | } |
247 | unset($MCACHE); |
248 | return false; |
249 | } |
250 | |
251 | |
252 | |
d3f9f1f8 |
253 | ?> |