fixed whitespace
[moodle.git] / lib / setuplib.php
CommitLineData
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 */
10class object {};
11
251387d0 12/**
13 * Base Moodle Exception class
14 */
15class 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 */
50class 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 */
65function 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
11e7b506 87/**
88 * Initialises $FULLME and friends.
89 * @return void
90 */
91function initialise_fullme() {
92 global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT;
93
84b88cfd 94 if (substr($CFG->wwwroot, -1) == '/') {
95 print_error('wwwrootslash', 'error');
96 }
97
11e7b506 98 $url = parse_url($CFG->wwwroot);
37ccf1fe 99 if (!isset($url['path'])) {
100 $url['path'] = '';
101 }
102 $url['path'] .= '/';
11e7b506 103
104 if (CLI_SCRIPT) {
105 // urls do not make much sense in CLI scripts
106 $backtrace = debug_backtrace();
107 $topfile = array_pop($backtrace);
108 $topfile = realpath($topfile['file']);
109 $dirroot = realpath($CFG->dirroot);
110
111 if (strpos($topfile, $dirroot) !== 0) {
112 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null;
113 } else {
114 $relme = substr($topfile, strlen($dirroot));
115 $relme = str_replace('\\', '/', $relme); // Win fix
116 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = $relme;
117 }
118
119 return;
120 }
121
122 $rurl = array();
123 $hostport = explode(':', $_SERVER['HTTP_HOST']);
124 $rurl['host'] = reset($hostport);
125 $rurl['port'] = $_SERVER['SERVER_PORT'];
126 $rurl['path'] = $_SERVER['SCRIPT_NAME']; // script path without slash arguments
127
128 if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
129 //Apache server
130 $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https';
131 $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
132
133 } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
134 //lighttpd
135 $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https';
136 $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
137
138 } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) {
139 //IIS
140 $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https';
141 $rurl['fullpath'] = $_SERVER['SCRIPT_NAME'];
142
143 // NOTE: ignore PATH_INFO because it is incorrectly encoded using 8bit filesystem legacy encoding in IIS
144 // since 2.0 we rely on iis rewrite extenssion like Helicon ISAPI_rewrite
21517960 145 // example rule: RewriteRule ^([^\?]+?\.php)(\/.+)$ $1\?file=$2 [QSA]
11e7b506 146
147 if ($_SERVER['QUERY_STRING'] != '') {
991ec2ee 148 $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING'];
11e7b506 149 }
150 $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility
151
152 } else {
4d6d912a 153 print_error('unsupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']);
11e7b506 154 }
155
156 if (strpos($rurl['path'], $url['path']) === 0) {
37ccf1fe 157 $SCRIPT = substr($rurl['path'], strlen($url['path'])-1);
11e7b506 158 } else {
159 // probably some weird external script
160 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null;
161 return;
162 }
163
164 // $CFG->sslproxy specifies if external SSL apliance is used (server using http, ext box translating everything to https)
165 if (empty($CFG->sslproxy)) {
166 if ($rurl['scheme'] == 'http' and $url['scheme'] == 'https') {
167 print_error('sslonlyaccess', 'error');
168 }
169 }
170
171 // $CFG->reverseproxy specifies if reverse proxy server used - used in advanced load balancing setups only!
172 // this is not supposed to solve lan/wan access problems!!!!!
173 if (empty($CFG->reverseproxy)) {
174 if (($rurl['host'] != $url['host']) or (!empty($url['port']) and $rurl['port'] != $url['port'])) {
175 print_error('wwwrootmismatch', 'error', '', $CFG->wwwroot);
176 }
f1e715ff 177 } else {
178 if ($rurl['host'] == $url['host']) {
179 // hopefully this will stop all those "clever" admins trying to set up moodle with two different addresses in intranet and Internet
180 print_error('reverseproxyabused', 'error');
181 }
11e7b506 182 }
183
184 $FULLME = $rurl['scheme'].'://'.$url['host'];
185 if (!empty($url['port'])) {
186 $FULLME .= ':'.$url['port'];
187 }
188 $FULLSCRIPT = $FULLME.$rurl['path'];
189 $FULLME = $FULLME.$rurl['fullpath'];
190 $ME = $rurl['fullpath'];
191
192}
193
d3f9f1f8 194/**
195 * Initializes our performance info early.
196 *
197 * Pairs up with get_performance_info() which is actually
251387d0 198 * in moodlelib.php. This function is here so that we can
199 * call it before all the libs are pulled in.
d3f9f1f8 200 *
201 * @uses $PERF
202 */
203function init_performance_info() {
204
6fc4ad72 205 global $PERF, $CFG, $USER;
251387d0 206
ab130a0b 207 $PERF = new object();
d3f9f1f8 208 $PERF->logwrites = 0;
209 if (function_exists('microtime')) {
210 $PERF->starttime = microtime();
211 }
212 if (function_exists('memory_get_usage')) {
213 $PERF->startmemory = memory_get_usage();
214 }
215 if (function_exists('posix_times')) {
251387d0 216 $PERF->startposixtimes = posix_times();
d3f9f1f8 217 }
b65567f4 218 if (function_exists('apd_set_pprof_trace')) {
219 // APD profiling
6fc4ad72 220 if ($USER->id > 0 && $CFG->perfdebug >= 15) {
251387d0 221 $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id;
6fc4ad72 222 mkdir($tempdir);
223 apd_set_pprof_trace($tempdir);
224 $PERF->profiling = true;
225 }
b65567f4 226 }
d3f9f1f8 227}
228
76f3815b 229/**
230 * Function to raise the memory limit to a new value.
231 * Will respect the memory limit if it is higher, thus allowing
232 * settings in php.ini, apache conf or command line switches
233 * to override it
234 *
235 * The memory limit should be expressed with a string (eg:'64M')
236 *
237 * @param string $newlimit the new memory limit
238 * @return bool
239 */
11e7b506 240function raise_memory_limit($newlimit) {
76f3815b 241
242 if (empty($newlimit)) {
243 return false;
244 }
245
246 $cur = @ini_get('memory_limit');
247 if (empty($cur)) {
248 // if php is compiled without --enable-memory-limits
249 // apparently memory_limit is set to ''
250 $cur=0;
251 } else {
252 if ($cur == -1){
253 return true; // unlimited mem!
254 }
255 $cur = get_real_size($cur);
256 }
257
258 $new = get_real_size($newlimit);
259 if ($new > $cur) {
260 ini_set('memory_limit', $newlimit);
261 return true;
262 }
263 return false;
264}
265
266/**
267 * Converts numbers like 10M into bytes.
268 *
269 * @param mixed $size The size to be converted
270 * @return mixed
271 */
272function get_real_size($size=0) {
273 if (!$size) {
274 return 0;
275 }
11e7b506 276 $scan = array();
76f3815b 277 $scan['MB'] = 1048576;
278 $scan['Mb'] = 1048576;
279 $scan['M'] = 1048576;
280 $scan['m'] = 1048576;
281 $scan['KB'] = 1024;
282 $scan['Kb'] = 1024;
283 $scan['K'] = 1024;
284 $scan['k'] = 1024;
285
286 while (list($key) = each($scan)) {
287 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
288 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
289 break;
290 }
291 }
292 return $size;
293}
294
d3f9f1f8 295/**
296 * Create a directory.
297 *
298 * @uses $CFG
299 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
300 * param bool $shownotices If true then notification messages will be printed out on error.
301 * @return string|false Returns full path to directory if successful, false if not
302 */
303function make_upload_directory($directory, $shownotices=true) {
304
305 global $CFG;
306
307 $currdir = $CFG->dataroot;
308
309 umask(0000);
310
311 if (!file_exists($currdir)) {
312 if (! mkdir($currdir, $CFG->directorypermissions)) {
313 if ($shownotices) {
251387d0 314 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
d3f9f1f8 315 $currdir .' with web server write access</div>'."<br />\n";
316 }
317 return false;
318 }
d100b8a0 319 }
320
321 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
322 if (!file_exists($currdir.'/.htaccess')) {
d3f9f1f8 323 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 324 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 325 @fclose($handle);
326 }
327 }
328
329 $dirarray = explode('/', $directory);
330
331 foreach ($dirarray as $dir) {
332 $currdir = $currdir .'/'. $dir;
333 if (! file_exists($currdir)) {
334 if (! mkdir($currdir, $CFG->directorypermissions)) {
335 if ($shownotices) {
251387d0 336 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
d3f9f1f8 337 $currdir .')</div>'."<br />\n";
338 }
339 return false;
340 }
341 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
342 }
343 }
344
345 return $currdir;
346}
347
419e1d93 348function init_memcached() {
349 global $CFG, $MCACHE;
350
f917d0ea 351 include_once($CFG->libdir . '/memcached.class.php');
352 $MCACHE = new memcached;
353 if ($MCACHE->status()) {
354 return true;
251387d0 355 }
f917d0ea 356 unset($MCACHE);
251387d0 357 return false;
419e1d93 358}
359
2142d492 360function init_eaccelerator() {
361 global $CFG, $MCACHE;
362
363 include_once($CFG->libdir . '/eaccelerator.class.php');
364 $MCACHE = new eaccelerator;
f917d0ea 365 if ($MCACHE->status()) {
2142d492 366 return true;
251387d0 367 }
2142d492 368 unset($MCACHE);
369 return false;
370}
371
372
373
d3f9f1f8 374?>