merged fixing a warning
[moodle.git] / lib / setuplib.php
CommitLineData
d3f9f1f8 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.
5
6
7/**
8 * Initializes our performance info early.
9 *
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.
13 *
14 * @uses $PERF
15 */
16function init_performance_info() {
17
18 global $PERF;
b65567f4 19
d3f9f1f8 20 $PERF = new Object;
21 $PERF->dbqueries = 0;
22 $PERF->logwrites = 0;
23 if (function_exists('microtime')) {
24 $PERF->starttime = microtime();
25 }
26 if (function_exists('memory_get_usage')) {
27 $PERF->startmemory = memory_get_usage();
28 }
29 if (function_exists('posix_times')) {
30 $PERF->startposixtimes = posix_times();
31 }
b65567f4 32 if (function_exists('apd_set_pprof_trace')) {
33 // APD profiling
34 apd_set_pprof_trace();
35 $PERF->process = 44444;
36 }
d3f9f1f8 37}
38
76f3815b 39/**
40 * Function to raise the memory limit to a new value.
41 * Will respect the memory limit if it is higher, thus allowing
42 * settings in php.ini, apache conf or command line switches
43 * to override it
44 *
45 * The memory limit should be expressed with a string (eg:'64M')
46 *
47 * @param string $newlimit the new memory limit
48 * @return bool
49 */
50function raise_memory_limit ($newlimit) {
51
52 if (empty($newlimit)) {
53 return false;
54 }
55
56 $cur = @ini_get('memory_limit');
57 if (empty($cur)) {
58 // if php is compiled without --enable-memory-limits
59 // apparently memory_limit is set to ''
60 $cur=0;
61 } else {
62 if ($cur == -1){
63 return true; // unlimited mem!
64 }
65 $cur = get_real_size($cur);
66 }
67
68 $new = get_real_size($newlimit);
69 if ($new > $cur) {
70 ini_set('memory_limit', $newlimit);
71 return true;
72 }
73 return false;
74}
75
76/**
77 * Converts numbers like 10M into bytes.
78 *
79 * @param mixed $size The size to be converted
80 * @return mixed
81 */
82function get_real_size($size=0) {
83 if (!$size) {
84 return 0;
85 }
86 $scan['MB'] = 1048576;
87 $scan['Mb'] = 1048576;
88 $scan['M'] = 1048576;
89 $scan['m'] = 1048576;
90 $scan['KB'] = 1024;
91 $scan['Kb'] = 1024;
92 $scan['K'] = 1024;
93 $scan['k'] = 1024;
94
95 while (list($key) = each($scan)) {
96 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
97 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
98 break;
99 }
100 }
101 return $size;
102}
103
d3f9f1f8 104/**
105 * Create a directory.
106 *
107 * @uses $CFG
108 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
109 * param bool $shownotices If true then notification messages will be printed out on error.
110 * @return string|false Returns full path to directory if successful, false if not
111 */
112function make_upload_directory($directory, $shownotices=true) {
113
114 global $CFG;
115
116 $currdir = $CFG->dataroot;
117
118 umask(0000);
119
120 if (!file_exists($currdir)) {
121 if (! mkdir($currdir, $CFG->directorypermissions)) {
122 if ($shownotices) {
123 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
124 $currdir .' with web server write access</div>'."<br />\n";
125 }
126 return false;
127 }
d100b8a0 128 }
129
130 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
131 if (!file_exists($currdir.'/.htaccess')) {
d3f9f1f8 132 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 133 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 134 @fclose($handle);
135 }
136 }
137
138 $dirarray = explode('/', $directory);
139
140 foreach ($dirarray as $dir) {
141 $currdir = $currdir .'/'. $dir;
142 if (! file_exists($currdir)) {
143 if (! mkdir($currdir, $CFG->directorypermissions)) {
144 if ($shownotices) {
145 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
146 $currdir .')</div>'."<br />\n";
147 }
148 return false;
149 }
150 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
151 }
152 }
153
154 return $currdir;
155}
156
7104d80d 157/**
158 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
159 * Used from setup.php to set correctly "set names" when the installation
160 * process is performed without the initial and beautiful installer
161 */
162function setup_is_unicodedb() {
163
7ddf8aa5 164 global $CFG, $db, $INSTALL;
7104d80d 165
166 $unicodedb = false;
7ddf8aa5 167
bb48a537 168 // Calculate $CFG->dbfamily
169 $dbfamily = set_dbfamily();
7104d80d 170
bb48a537 171 switch ($dbfamily) {
7104d80d 172 case 'mysql':
7104d80d 173 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
174 if ($rs && $rs->RecordCount() > 0) {
175 $records = $rs->GetAssoc(true);
176 $encoding = $records['character_set_database']['Value'];
177 if (strtoupper($encoding) == 'UTF8') {
178 $unicodedb = true;
179 }
180 }
181 break;
bb48a537 182 case 'postgres':
7104d80d 183 /// Get PostgreSQL server_encoding value
184 $rs = $db->Execute("SHOW server_encoding");
185 if ($rs && $rs->RecordCount() > 0) {
186 $encoding = $rs->fields['server_encoding'];
2544a80d 187 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
7104d80d 188 $unicodedb = true;
189 }
190 }
191 break;
bf052299 192 case 'mssql':
193 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
194 $unicodedb = true;
010f2a39 195 break;
bb48a537 196 case 'oracle':
0700134d 197 /// Get Oracle DB character set value
198 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
199 if ($rs && $rs->RecordCount() > 0) {
200 $encoding = $rs->fields['value'];
201 if (strtoupper($encoding) == 'AL32UTF8') {
202 $unicodedb = true;
203 }
204 }
205 break;
7104d80d 206 }
207 return $unicodedb;
208}
209
1d5c4004 210/**
211 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype
212 * It's called by configure_dbconnection() and at install time. Shouldn't be used
213 * in other places. Code should rely on dbfamily to perform conditional execution
214 * instead of using dbtype directly. This allows quicker adoption of different
215 * drivers going against the same DB backend.
216 *
217 * This function must contain the init code needed for each dbtype supported.
218 *
219 * return string dbfamily value (mysql, postgres, oracle, mssql)
220 */
221function set_dbfamily() {
222
223 global $CFG, $INSTALL;
224
225 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set.
226 // we need to get dbtype from the right variable
227 if (!empty($INSTALL['dbtype'])) {
228 $dbtype = $INSTALL['dbtype'];
229 } else {
230 $dbtype = $CFG->dbtype;
231 }
232
233 switch ($dbtype) {
234 case 'mysql':
235 case 'mysqli':
236 $CFG->dbfamily='mysql';
237 break;
238 case 'postgres7':
239 $CFG->dbfamily='postgres';
240 break;
241 case 'mssql':
242 case 'mssql_n':
243 case 'odbc_mssql':
244 $CFG->dbfamily='mssql';
245 break;
246 case 'oci8po':
247 $CFG->dbfamily='oracle';
248 break;
249 }
250
251 return $CFG->dbfamily;
252}
253
419e1d93 254function init_memcached() {
255 global $CFG, $MCACHE;
256
f917d0ea 257 include_once($CFG->libdir . '/memcached.class.php');
258 $MCACHE = new memcached;
259 if ($MCACHE->status()) {
260 return true;
261 }
262 unset($MCACHE);
263 return false;
419e1d93 264}
265
2142d492 266function init_eaccelerator() {
267 global $CFG, $MCACHE;
268
269 include_once($CFG->libdir . '/eaccelerator.class.php');
270 $MCACHE = new eaccelerator;
f917d0ea 271 if ($MCACHE->status()) {
2142d492 272 return true;
273 }
274 unset($MCACHE);
275 return false;
276}
277
278
279
d3f9f1f8 280?>