251387d0 |
1 | <?php // $Id$ |
2 | // These functions are required very early in the Moodle |
3 | // setup process, before any of the main libraries are |
d3f9f1f8 |
4 | // loaded. |
5 | |
6 | |
6a525ce2 |
7 | /** |
8 | * Simple class |
9 | */ |
10 | class object {}; |
11 | |
251387d0 |
12 | /** |
13 | * Base Moodle Exception class |
14 | */ |
15 | class moodle_exception extends Exception { |
16 | public $errorcode; |
17 | public $module; |
18 | public $a; |
19 | public $link; |
eee5d9bb |
20 | public $debuginfo; |
251387d0 |
21 | |
22 | /** |
23 | * Constructor |
24 | * @param string $errorcode The name of the string from error.php to print |
25 | * @param string $module name of module |
26 | * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. |
27 | * @param object $a Extra words and phrases that might be required in the error string |
eee5d9bb |
28 | * @param string $debuginfo optional debugging information |
251387d0 |
29 | */ |
5ca18631 |
30 | function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) { |
31 | if (empty($module) || $module == 'moodle' || $module == 'core') { |
251387d0 |
32 | $module = 'error'; |
33 | } |
34 | |
5ca18631 |
35 | $this->errorcode = $errorcode; |
36 | $this->module = $module; |
37 | $this->link = $link; |
38 | $this->a = $a; |
39 | $this->debuginfo = $debuginfo; |
251387d0 |
40 | |
5ca18631 |
41 | $message = get_string($errorcode, $module, $a); |
251387d0 |
42 | |
43 | parent::__construct($message, 0); |
44 | } |
45 | } |
46 | |
655bbf51 |
47 | /** |
9214025e |
48 | * Exception indicating programming error, must be fixed by a programer. |
655bbf51 |
49 | */ |
50 | class coding_exception extends moodle_exception { |
51 | |
52 | /** |
53 | * Constructor |
54 | * @param string $hint short description of problem |
55 | * @param string $debuginfo detailed information how to fix problem |
56 | */ |
57 | function __construct($hint, $debuginfo=null) { |
58 | parent::__construct('codingerror', 'debug', '', $hint, $debuginfo); |
59 | } |
60 | } |
61 | |
251387d0 |
62 | /** |
63 | * Default exception handler, uncought exceptions are equivalent to using print_error() |
64 | */ |
65 | function default_exception_handler($ex) { |
1fe1d104 |
66 | global $CFG; |
67 | |
251387d0 |
68 | $backtrace = $ex->getTrace(); |
69 | $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex)); |
70 | array_unshift($backtrace, $place); |
71 | |
72 | if ($ex instanceof moodle_exception) { |
1fe1d104 |
73 | if (!isset($CFG->theme) or !isset($CFG->stylesheets)) { |
ce152606 |
74 | _print_early_error($ex->errorcode, $ex->module, $ex->a, $backtrace, $ex->debuginfo); |
1fe1d104 |
75 | } else { |
76 | _print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace, $ex->debuginfo); |
77 | } |
251387d0 |
78 | } else { |
1fe1d104 |
79 | if (!isset($CFG->theme) or !isset($CFG->stylesheets)) { |
795a08ad |
80 | _print_early_error('generalexceptionmessage', 'error', $ex->getMessage(), $backtrace); |
1fe1d104 |
81 | } else { |
82 | _print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace); |
83 | } |
251387d0 |
84 | } |
85 | } |
6a525ce2 |
86 | |
fbf2c91e |
87 | /** |
88 | * This function verifies the sanity of PHP configuration |
89 | * and stops execution if anything critical found. |
90 | */ |
91 | function setup_validate_php_configuration() { |
92 | // this must be very fast - no slow checks here!!! |
93 | |
94 | if (ini_get_bool('register_globals')) { |
95 | print_error('globalswarning', 'admin'); |
96 | } |
97 | if (ini_get_bool('session.auto_start')) { |
98 | print_error('sessionautostartwarning', 'admin'); |
99 | } |
100 | if (ini_get_bool('magic_quotes_runtime')) { |
101 | print_error('fatalmagicquotesruntime', 'admin'); |
102 | } |
103 | } |
104 | |
11e7b506 |
105 | /** |
106 | * Initialises $FULLME and friends. |
107 | * @return void |
108 | */ |
109 | function initialise_fullme() { |
110 | global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT; |
111 | |
84b88cfd |
112 | if (substr($CFG->wwwroot, -1) == '/') { |
113 | print_error('wwwrootslash', 'error'); |
114 | } |
115 | |
11e7b506 |
116 | $url = parse_url($CFG->wwwroot); |
37ccf1fe |
117 | if (!isset($url['path'])) { |
118 | $url['path'] = ''; |
119 | } |
120 | $url['path'] .= '/'; |
11e7b506 |
121 | |
122 | if (CLI_SCRIPT) { |
123 | // urls do not make much sense in CLI scripts |
124 | $backtrace = debug_backtrace(); |
125 | $topfile = array_pop($backtrace); |
126 | $topfile = realpath($topfile['file']); |
127 | $dirroot = realpath($CFG->dirroot); |
128 | |
129 | if (strpos($topfile, $dirroot) !== 0) { |
130 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; |
131 | } else { |
132 | $relme = substr($topfile, strlen($dirroot)); |
133 | $relme = str_replace('\\', '/', $relme); // Win fix |
134 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = $relme; |
135 | } |
136 | |
137 | return; |
138 | } |
139 | |
140 | $rurl = array(); |
141 | $hostport = explode(':', $_SERVER['HTTP_HOST']); |
142 | $rurl['host'] = reset($hostport); |
143 | $rurl['port'] = $_SERVER['SERVER_PORT']; |
144 | $rurl['path'] = $_SERVER['SCRIPT_NAME']; // script path without slash arguments |
145 | |
146 | if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) { |
147 | //Apache server |
148 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
149 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded |
150 | |
151 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) { |
152 | //lighttpd |
153 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
154 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded |
155 | |
156 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) { |
157 | //IIS |
158 | $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https'; |
159 | $rurl['fullpath'] = $_SERVER['SCRIPT_NAME']; |
160 | |
161 | // NOTE: ignore PATH_INFO because it is incorrectly encoded using 8bit filesystem legacy encoding in IIS |
162 | // since 2.0 we rely on iis rewrite extenssion like Helicon ISAPI_rewrite |
21517960 |
163 | // example rule: RewriteRule ^([^\?]+?\.php)(\/.+)$ $1\?file=$2 [QSA] |
11e7b506 |
164 | |
165 | if ($_SERVER['QUERY_STRING'] != '') { |
991ec2ee |
166 | $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING']; |
11e7b506 |
167 | } |
168 | $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility |
169 | |
170 | } else { |
4d6d912a |
171 | print_error('unsupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']); |
11e7b506 |
172 | } |
173 | |
174 | if (strpos($rurl['path'], $url['path']) === 0) { |
37ccf1fe |
175 | $SCRIPT = substr($rurl['path'], strlen($url['path'])-1); |
11e7b506 |
176 | } else { |
177 | // probably some weird external script |
178 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; |
179 | return; |
180 | } |
181 | |
182 | // $CFG->sslproxy specifies if external SSL apliance is used (server using http, ext box translating everything to https) |
183 | if (empty($CFG->sslproxy)) { |
184 | if ($rurl['scheme'] == 'http' and $url['scheme'] == 'https') { |
185 | print_error('sslonlyaccess', 'error'); |
186 | } |
187 | } |
188 | |
189 | // $CFG->reverseproxy specifies if reverse proxy server used - used in advanced load balancing setups only! |
190 | // this is not supposed to solve lan/wan access problems!!!!! |
191 | if (empty($CFG->reverseproxy)) { |
192 | if (($rurl['host'] != $url['host']) or (!empty($url['port']) and $rurl['port'] != $url['port'])) { |
193 | print_error('wwwrootmismatch', 'error', '', $CFG->wwwroot); |
194 | } |
f1e715ff |
195 | } else { |
196 | if ($rurl['host'] == $url['host']) { |
197 | // hopefully this will stop all those "clever" admins trying to set up moodle with two different addresses in intranet and Internet |
198 | print_error('reverseproxyabused', 'error'); |
199 | } |
11e7b506 |
200 | } |
201 | |
202 | $FULLME = $rurl['scheme'].'://'.$url['host']; |
203 | if (!empty($url['port'])) { |
204 | $FULLME .= ':'.$url['port']; |
205 | } |
206 | $FULLSCRIPT = $FULLME.$rurl['path']; |
207 | $FULLME = $FULLME.$rurl['fullpath']; |
208 | $ME = $rurl['fullpath']; |
209 | |
210 | } |
211 | |
d3f9f1f8 |
212 | /** |
213 | * Initializes our performance info early. |
214 | * |
215 | * Pairs up with get_performance_info() which is actually |
251387d0 |
216 | * in moodlelib.php. This function is here so that we can |
217 | * call it before all the libs are pulled in. |
d3f9f1f8 |
218 | * |
219 | * @uses $PERF |
220 | */ |
221 | function init_performance_info() { |
222 | |
6fc4ad72 |
223 | global $PERF, $CFG, $USER; |
251387d0 |
224 | |
ab130a0b |
225 | $PERF = new object(); |
d3f9f1f8 |
226 | $PERF->logwrites = 0; |
227 | if (function_exists('microtime')) { |
228 | $PERF->starttime = microtime(); |
229 | } |
230 | if (function_exists('memory_get_usage')) { |
231 | $PERF->startmemory = memory_get_usage(); |
232 | } |
233 | if (function_exists('posix_times')) { |
251387d0 |
234 | $PERF->startposixtimes = posix_times(); |
d3f9f1f8 |
235 | } |
b65567f4 |
236 | if (function_exists('apd_set_pprof_trace')) { |
237 | // APD profiling |
6fc4ad72 |
238 | if ($USER->id > 0 && $CFG->perfdebug >= 15) { |
251387d0 |
239 | $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id; |
6fc4ad72 |
240 | mkdir($tempdir); |
241 | apd_set_pprof_trace($tempdir); |
242 | $PERF->profiling = true; |
243 | } |
b65567f4 |
244 | } |
d3f9f1f8 |
245 | } |
246 | |
76f3815b |
247 | /** |
248 | * Function to raise the memory limit to a new value. |
249 | * Will respect the memory limit if it is higher, thus allowing |
250 | * settings in php.ini, apache conf or command line switches |
251 | * to override it |
252 | * |
253 | * The memory limit should be expressed with a string (eg:'64M') |
254 | * |
255 | * @param string $newlimit the new memory limit |
256 | * @return bool |
257 | */ |
11e7b506 |
258 | function raise_memory_limit($newlimit) { |
76f3815b |
259 | |
260 | if (empty($newlimit)) { |
261 | return false; |
262 | } |
263 | |
264 | $cur = @ini_get('memory_limit'); |
265 | if (empty($cur)) { |
266 | // if php is compiled without --enable-memory-limits |
267 | // apparently memory_limit is set to '' |
268 | $cur=0; |
269 | } else { |
270 | if ($cur == -1){ |
271 | return true; // unlimited mem! |
272 | } |
273 | $cur = get_real_size($cur); |
274 | } |
275 | |
276 | $new = get_real_size($newlimit); |
277 | if ($new > $cur) { |
278 | ini_set('memory_limit', $newlimit); |
7022dd39 |
279 | return true; |
280 | } |
281 | return false; |
282 | } |
283 | |
284 | /** |
285 | * Function to reduce the memory limit to a new value. |
286 | * Will respect the memory limit if it is lower, thus allowing |
287 | * settings in php.ini, apache conf or command line switches |
288 | * to override it |
289 | * |
290 | * The memory limit should be expressed with a string (eg:'64M') |
291 | * |
292 | * @param string $newlimit the new memory limit |
293 | * @return bool |
294 | */ |
295 | function reduce_memory_limit ($newlimit) { |
296 | if (empty($newlimit)) { |
297 | return false; |
298 | } |
299 | $cur = @ini_get('memory_limit'); |
300 | if (empty($cur)) { |
301 | // if php is compiled without --enable-memory-limits |
302 | // apparently memory_limit is set to '' |
303 | $cur=0; |
304 | } else { |
305 | if ($cur == -1){ |
306 | return true; // unlimited mem! |
307 | } |
308 | $cur = get_real_size($cur); |
309 | } |
310 | |
311 | $new = get_real_size($newlimit); |
312 | // -1 is smaller, but it means unlimited |
313 | if ($new < $cur && $new != -1) { |
314 | ini_set('memory_limit', $newlimit); |
76f3815b |
315 | return true; |
316 | } |
317 | return false; |
318 | } |
319 | |
320 | /** |
321 | * Converts numbers like 10M into bytes. |
322 | * |
323 | * @param mixed $size The size to be converted |
324 | * @return mixed |
325 | */ |
326 | function get_real_size($size=0) { |
327 | if (!$size) { |
328 | return 0; |
329 | } |
11e7b506 |
330 | $scan = array(); |
76f3815b |
331 | $scan['MB'] = 1048576; |
332 | $scan['Mb'] = 1048576; |
333 | $scan['M'] = 1048576; |
334 | $scan['m'] = 1048576; |
335 | $scan['KB'] = 1024; |
336 | $scan['Kb'] = 1024; |
337 | $scan['K'] = 1024; |
338 | $scan['k'] = 1024; |
339 | |
340 | while (list($key) = each($scan)) { |
341 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
342 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
343 | break; |
344 | } |
345 | } |
346 | return $size; |
347 | } |
348 | |
d3f9f1f8 |
349 | /** |
350 | * Create a directory. |
351 | * |
352 | * @uses $CFG |
353 | * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 |
354 | * param bool $shownotices If true then notification messages will be printed out on error. |
355 | * @return string|false Returns full path to directory if successful, false if not |
356 | */ |
357 | function make_upload_directory($directory, $shownotices=true) { |
358 | |
359 | global $CFG; |
360 | |
361 | $currdir = $CFG->dataroot; |
362 | |
363 | umask(0000); |
364 | |
365 | if (!file_exists($currdir)) { |
b10a14a3 |
366 | if (!mkdir($currdir, $CFG->directorypermissions) or !is_writable($currdir)) { |
d3f9f1f8 |
367 | if ($shownotices) { |
251387d0 |
368 | echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '. |
d3f9f1f8 |
369 | $currdir .' with web server write access</div>'."<br />\n"; |
370 | } |
371 | return false; |
372 | } |
d100b8a0 |
373 | } |
374 | |
375 | // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open |
376 | if (!file_exists($currdir.'/.htaccess')) { |
d3f9f1f8 |
377 | if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety |
d389b128 |
378 | @fwrite($handle, "deny from all\r\nAllowOverride None\r\n"); |
d3f9f1f8 |
379 | @fclose($handle); |
380 | } |
381 | } |
382 | |
383 | $dirarray = explode('/', $directory); |
384 | |
385 | foreach ($dirarray as $dir) { |
386 | $currdir = $currdir .'/'. $dir; |
387 | if (! file_exists($currdir)) { |
388 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
389 | if ($shownotices) { |
251387d0 |
390 | echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('. |
d3f9f1f8 |
391 | $currdir .')</div>'."<br />\n"; |
392 | } |
393 | return false; |
394 | } |
395 | //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it |
396 | } |
397 | } |
398 | |
399 | return $currdir; |
400 | } |
401 | |
419e1d93 |
402 | function init_memcached() { |
403 | global $CFG, $MCACHE; |
404 | |
f917d0ea |
405 | include_once($CFG->libdir . '/memcached.class.php'); |
406 | $MCACHE = new memcached; |
407 | if ($MCACHE->status()) { |
408 | return true; |
251387d0 |
409 | } |
f917d0ea |
410 | unset($MCACHE); |
251387d0 |
411 | return false; |
419e1d93 |
412 | } |
413 | |
2142d492 |
414 | function init_eaccelerator() { |
415 | global $CFG, $MCACHE; |
416 | |
417 | include_once($CFG->libdir . '/eaccelerator.class.php'); |
418 | $MCACHE = new eaccelerator; |
f917d0ea |
419 | if ($MCACHE->status()) { |
2142d492 |
420 | return true; |
251387d0 |
421 | } |
2142d492 |
422 | unset($MCACHE); |
423 | return false; |
424 | } |
425 | |
426 | |
427 | |
d3f9f1f8 |
428 | ?> |