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