Bumping version for 1.8.9 release. Thinking we might have to go to x.x.10 for the...
[moodle.git] / lib / setuplib.php
blob1e5fe2d3b086dfd2dd0990e9ed541ab4f41d6eeb
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.
7 /**
8 * Initializes our performance info early.
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.
14 * @uses $PERF
16 function init_performance_info() {
18 global $PERF;
20 $PERF = new Object;
21 $PERF->dbqueries = 0;
22 $PERF->logwrites = 0;
23 if (function_exists('microtime')) {
24 $PERF->starttime = microtime();
26 if (function_exists('memory_get_usage')) {
27 $PERF->startmemory = memory_get_usage();
29 if (function_exists('posix_times')) {
30 $PERF->startposixtimes = posix_times();
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
40 * The memory limit should be expressed with a string (eg:'64M')
42 * @param string $newlimit the new memory limit
43 * @return bool
45 function raise_memory_limit ($newlimit) {
47 if (empty($newlimit)) {
48 return false;
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!
60 $cur = get_real_size($cur);
63 $new = get_real_size($newlimit);
64 if ($new > $cur) {
65 ini_set('memory_limit', $newlimit);
66 return true;
68 return false;
71 /**
72 * Converts numbers like 10M into bytes.
74 * @param mixed $size The size to be converted
75 * @return mixed
77 function get_real_size($size=0) {
78 if (!$size) {
79 return 0;
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;
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;
96 return $size;
99 /**
100 * Create a directory.
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
107 function make_upload_directory($directory, $shownotices=true) {
109 global $CFG;
111 $currdir = $CFG->dataroot;
113 umask(0000);
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";
121 return false;
125 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
126 if (!file_exists($currdir.'/.htaccess')) {
127 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
128 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
129 @fclose($handle);
133 $dirarray = explode('/', $directory);
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";
143 return false;
145 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
149 return $currdir;
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
157 function setup_is_unicodedb() {
159 global $CFG, $db, $INSTALL;
161 $unicodedb = false;
163 // Calculate $CFG->dbfamily
164 $dbfamily = set_dbfamily();
166 switch ($dbfamily) {
167 case 'mysql':
168 $rs = $db->Execute("SHOW LOCAL VARIABLES LIKE 'character_set_database'");
169 if ($rs && $rs->RecordCount() > 0) {
170 $records = $rs->GetAssoc(true);
171 $encoding = $records['character_set_database']['Value'];
172 if (strtoupper($encoding) == 'UTF8') {
173 $unicodedb = true;
176 break;
177 case 'postgres':
178 /// Get PostgreSQL server_encoding value
179 $rs = $db->Execute("SHOW server_encoding");
180 if ($rs && $rs->RecordCount() > 0) {
181 $encoding = $rs->fields['server_encoding'];
182 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
183 $unicodedb = true;
186 break;
187 case 'mssql':
188 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
189 $unicodedb = true;
190 break;
191 case 'oracle':
192 /// Get Oracle DB character set value
193 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
194 if ($rs && $rs->RecordCount() > 0) {
195 $encoding = $rs->fields['value'];
196 if (strtoupper($encoding) == 'AL32UTF8') {
197 $unicodedb = true;
200 break;
202 return $unicodedb;
206 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype
207 * It's called by configure_dbconnection() and at install time. Shouldn't be used
208 * in other places. Code should rely on dbfamily to perform conditional execution
209 * instead of using dbtype directly. This allows quicker adoption of different
210 * drivers going against the same DB backend.
212 * This function must contain the init code needed for each dbtype supported.
214 * return string dbfamily value (mysql, postgres, oracle, mssql)
216 function set_dbfamily() {
218 global $CFG, $INSTALL;
220 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set.
221 // we need to get dbtype from the right variable
222 if (!empty($INSTALL['dbtype'])) {
223 $dbtype = $INSTALL['dbtype'];
224 } else {
225 $dbtype = $CFG->dbtype;
228 switch ($dbtype) {
229 case 'mysql':
230 case 'mysqli':
231 $CFG->dbfamily='mysql';
232 break;
233 case 'postgres7':
234 $CFG->dbfamily='postgres';
235 break;
236 case 'mssql':
237 case 'mssql_n':
238 case 'odbc_mssql':
239 $CFG->dbfamily='mssql';
240 break;
241 case 'oci8po':
242 $CFG->dbfamily='oracle';
243 break;
246 return $CFG->dbfamily;
249 function init_memcached() {
250 global $CFG, $MCACHE;
252 include_once($CFG->libdir . '/memcached.class.php');
253 $MCACHE = new memcached;
254 if ($MCACHE->status()) {
255 return true;
257 unset($MCACHE);
258 return false;
261 function init_eaccelerator() {
262 global $CFG, $MCACHE;
264 include_once($CFG->libdir . '/eaccelerator.class.php');
265 $MCACHE = new eaccelerator;
266 if ($MCACHE->status()) {
267 return true;
269 unset($MCACHE);
270 return false;