Dan's fix from MDL-7263
[moodle.git] / lib / setuplib.php
bloba92fe4ff9e65d7a07a603ec28f878ff2e3beb7ea
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 * Create a directory.
37 * @uses $CFG
38 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
39 * param bool $shownotices If true then notification messages will be printed out on error.
40 * @return string|false Returns full path to directory if successful, false if not
42 function make_upload_directory($directory, $shownotices=true) {
44 global $CFG;
46 $currdir = $CFG->dataroot;
48 umask(0000);
50 if (!file_exists($currdir)) {
51 if (! mkdir($currdir, $CFG->directorypermissions)) {
52 if ($shownotices) {
53 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
54 $currdir .' with web server write access</div>'."<br />\n";
56 return false;
60 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
61 if (!file_exists($currdir.'/.htaccess')) {
62 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
63 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
64 @fclose($handle);
68 $dirarray = explode('/', $directory);
70 foreach ($dirarray as $dir) {
71 $currdir = $currdir .'/'. $dir;
72 if (! file_exists($currdir)) {
73 if (! mkdir($currdir, $CFG->directorypermissions)) {
74 if ($shownotices) {
75 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
76 $currdir .')</div>'."<br />\n";
78 return false;
80 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
84 return $currdir;
87 /**
88 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
89 * Used from setup.php to set correctly "set names" when the installation
90 * process is performed without the initial and beautiful installer
92 function setup_is_unicodedb() {
94 global $CFG, $db;
96 $unicodedb = false;
98 switch ($CFG->dbtype) {
99 case 'mysql':
100 /// Get MySQL character_set_database value
101 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
102 if ($rs && $rs->RecordCount() > 0) {
103 $records = $rs->GetAssoc(true);
104 $encoding = $records['character_set_database']['Value'];
105 if (strtoupper($encoding) == 'UTF8') {
106 $unicodedb = true;
109 break;
110 case 'postgres7':
111 /// Get PostgreSQL server_encoding value
112 $rs = $db->Execute("SHOW server_encoding");
113 if ($rs && $rs->RecordCount() > 0) {
114 $encoding = $rs->fields['server_encoding'];
115 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
116 $unicodedb = true;
119 break;
120 case 'mssql':
121 case 'mssql_n':
122 case 'odbc_mssql':
123 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
124 $unicodedb = true;
125 break;
126 case 'oci8po':
127 /// Get Oracle DB character set value
128 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
129 if ($rs && $rs->RecordCount() > 0) {
130 $encoding = $rs->fields['value'];
131 if (strtoupper($encoding) == 'AL32UTF8') {
132 $unicodedb = true;
135 break;
137 return $unicodedb;