MDL-15107 feedback dml conversion
[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;
20
21 /**
22 * Constructor
23 * @param string $errorcode The name of the string from error.php to print
24 * @param string $module name of module
25 * @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.
26 * @param object $a Extra words and phrases that might be required in the error string
27 */
28 function __construct($errorcode, $module='', $link='', $a=NULL) {
29 if (empty($module) || $module == 'moodle' || $module == 'core') {
30 $module = 'error';
31 }
32
33 $this->errorcode = $errorcode;
34 $this->module = $module;
35 $this->link = $link;
36 $this->a = $a;
37
38 $message = get_string($errorcode, $module, $a);
39
40 parent::__construct($message, 0);
41 }
42}
43
44/**
45 * Default exception handler, uncought exceptions are equivalent to using print_error()
46 */
47function default_exception_handler($ex) {
48 $backtrace = $ex->getTrace();
49 $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex));
50 array_unshift($backtrace, $place);
51
52 if ($ex instanceof moodle_exception) {
53 _print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace);
54 } else {
55 _print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace);
56 }
57}
6a525ce2 58
d3f9f1f8 59/**
60 * Initializes our performance info early.
61 *
62 * Pairs up with get_performance_info() which is actually
251387d0 63 * in moodlelib.php. This function is here so that we can
64 * call it before all the libs are pulled in.
d3f9f1f8 65 *
66 * @uses $PERF
67 */
68function init_performance_info() {
69
6fc4ad72 70 global $PERF, $CFG, $USER;
251387d0 71
ab130a0b 72 $PERF = new object();
d3f9f1f8 73 $PERF->logwrites = 0;
74 if (function_exists('microtime')) {
75 $PERF->starttime = microtime();
76 }
77 if (function_exists('memory_get_usage')) {
78 $PERF->startmemory = memory_get_usage();
79 }
80 if (function_exists('posix_times')) {
251387d0 81 $PERF->startposixtimes = posix_times();
d3f9f1f8 82 }
b65567f4 83 if (function_exists('apd_set_pprof_trace')) {
84 // APD profiling
6fc4ad72 85 if ($USER->id > 0 && $CFG->perfdebug >= 15) {
251387d0 86 $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id;
6fc4ad72 87 mkdir($tempdir);
88 apd_set_pprof_trace($tempdir);
89 $PERF->profiling = true;
90 }
b65567f4 91 }
d3f9f1f8 92}
93
76f3815b 94/**
95 * Function to raise the memory limit to a new value.
96 * Will respect the memory limit if it is higher, thus allowing
97 * settings in php.ini, apache conf or command line switches
98 * to override it
99 *
100 * The memory limit should be expressed with a string (eg:'64M')
101 *
102 * @param string $newlimit the new memory limit
103 * @return bool
104 */
105function raise_memory_limit ($newlimit) {
106
107 if (empty($newlimit)) {
108 return false;
109 }
110
111 $cur = @ini_get('memory_limit');
112 if (empty($cur)) {
113 // if php is compiled without --enable-memory-limits
114 // apparently memory_limit is set to ''
115 $cur=0;
116 } else {
117 if ($cur == -1){
118 return true; // unlimited mem!
119 }
120 $cur = get_real_size($cur);
121 }
122
123 $new = get_real_size($newlimit);
124 if ($new > $cur) {
125 ini_set('memory_limit', $newlimit);
126 return true;
127 }
128 return false;
129}
130
131/**
132 * Converts numbers like 10M into bytes.
133 *
134 * @param mixed $size The size to be converted
135 * @return mixed
136 */
137function get_real_size($size=0) {
138 if (!$size) {
139 return 0;
140 }
141 $scan['MB'] = 1048576;
142 $scan['Mb'] = 1048576;
143 $scan['M'] = 1048576;
144 $scan['m'] = 1048576;
145 $scan['KB'] = 1024;
146 $scan['Kb'] = 1024;
147 $scan['K'] = 1024;
148 $scan['k'] = 1024;
149
150 while (list($key) = each($scan)) {
151 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
152 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
153 break;
154 }
155 }
156 return $size;
157}
158
d3f9f1f8 159/**
160 * Create a directory.
161 *
162 * @uses $CFG
163 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
164 * param bool $shownotices If true then notification messages will be printed out on error.
165 * @return string|false Returns full path to directory if successful, false if not
166 */
167function make_upload_directory($directory, $shownotices=true) {
168
169 global $CFG;
170
171 $currdir = $CFG->dataroot;
172
173 umask(0000);
174
175 if (!file_exists($currdir)) {
176 if (! mkdir($currdir, $CFG->directorypermissions)) {
177 if ($shownotices) {
251387d0 178 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
d3f9f1f8 179 $currdir .' with web server write access</div>'."<br />\n";
180 }
181 return false;
182 }
d100b8a0 183 }
184
185 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
186 if (!file_exists($currdir.'/.htaccess')) {
d3f9f1f8 187 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 188 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 189 @fclose($handle);
190 }
191 }
192
193 $dirarray = explode('/', $directory);
194
195 foreach ($dirarray as $dir) {
196 $currdir = $currdir .'/'. $dir;
197 if (! file_exists($currdir)) {
198 if (! mkdir($currdir, $CFG->directorypermissions)) {
199 if ($shownotices) {
251387d0 200 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
d3f9f1f8 201 $currdir .')</div>'."<br />\n";
202 }
203 return false;
204 }
205 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
206 }
207 }
208
209 return $currdir;
210}
211
419e1d93 212function init_memcached() {
213 global $CFG, $MCACHE;
214
f917d0ea 215 include_once($CFG->libdir . '/memcached.class.php');
216 $MCACHE = new memcached;
217 if ($MCACHE->status()) {
218 return true;
251387d0 219 }
f917d0ea 220 unset($MCACHE);
251387d0 221 return false;
419e1d93 222}
223
2142d492 224function init_eaccelerator() {
225 global $CFG, $MCACHE;
226
227 include_once($CFG->libdir . '/eaccelerator.class.php');
228 $MCACHE = new eaccelerator;
f917d0ea 229 if ($MCACHE->status()) {
2142d492 230 return true;
251387d0 231 }
2142d492 232 unset($MCACHE);
233 return false;
234}
235
236
237
d3f9f1f8 238?>