Applied patch #411
[elgg.git] / lib / dbsetup.php
bloba06e938795ec862ff1c60ec26a959bac86c1247c
1 <?php
2 global $db, $METATABLES;
4 /// Check if the main tables have been installed yet or not.
5 if (!$METATABLES) { // No tables yet at all.
6 $maintables = false;
7 } else {
8 $maintables = false;
9 $datalists = false;
10 foreach ($METATABLES as $table) {
11 if (preg_match("/^{$CFG->prefix}users$/", $table)) {
12 $maintables = true;
14 if (preg_match("/^{$CFG->prefix}datalists$/", $table)) {
15 $datalists = true;
20 $strdatabasesuccess = "Yay!"; // well, if people leave never-defined variables about the place...
22 $newinstall = false;
24 if (!$maintables) {
25 if (file_exists($CFG->dirroot . "lib/db/$CFG->dbtype.sql")) {
26 $db->debug = true;
28 //version check
29 $continue = true;
30 $infoarr = $db->ServerInfo();
31 if (!empty($infoarr['version'])) {
32 switch($CFG->dbtype) {
33 case "mysql":
34 if (!preg_match('/^(4\.1|[5-9]\.|[0-9][0-9]+)/', $infoarr['version'])) {
35 error('Error: Your MySQL version is too old: ' . $infoarr['version'] . '. Elgg requires MySQL 4.1 or newer. 5.0 or newer is recommended.');
36 $continue = false;
38 break;
42 if ($continue) {
43 if (modify_database($CFG->dirroot . "lib/db/$CFG->dbtype.sql")) {
44 include_once($CFG->dirroot . "version.php");
45 set_config('version', $version);
46 $db->debug = false;
47 notify($strdatabasesuccess, "green");
48 if (!isset($CFG->newsinitialpassword) || empty($CFG->newsinitialpassword)) {
49 notify("WARNING: the initial password for the news account is 'password'. This account has administrator privileges, and you should log in and change the password as soon as installation is complete.");
50 } else {
51 //$newspassword = $db->qstr(md5($CFG->newsinitialpassword));
52 //execute_sql("update ".$CFG->prefix."users set password = $newspassword where username = 'news'");
53 set_field('users', 'password', md5($CFG->newsinitialpassword), 'username', 'news');
55 //execute_sql("update ".$CFG->prefix."users set email = ". $db->qstr($CFG->sysadminemail) ." where username = 'news'");
56 set_field('users', 'email', $CFG->sysadminemail, 'username', 'news');
57 } else {
58 $db->debug = false;
59 error("Error: Main databases NOT set up successfully");
62 } else {
63 error("Error: Your database ($CFG->dbtype) is not yet fully supported by Elgg. See the lib/db directory.");
65 print_continue("index.php");
66 die;
69 if (user_flag_get("admin",$_SESSION['userid'])) {
71 if (empty($CFG->version)) {
72 $CFG->version = 1;
75 if (empty($CFG->release)) {
76 $CFG->release = "";
79 if (!$datalists) {
80 $CFG->version = -1;
83 /// Upgrades
84 include_once($CFG->dirroot . "version.php"); # defines $version
85 include_once($CFG->dirroot . "lib/db/$CFG->dbtype.php"); # defines upgrades
87 if ($CFG->version) {
88 if ($version > $CFG->version) { // upgrade
90 $a->oldversion = "$CFG->release ($CFG->version)";
91 $a->newversion = "$release ($version)";
93 if (empty($_GET['confirmupgrade'])) {
94 notice_yesno(__gettext('Need to upgrade database'), $CFG->wwwroot . '?confirmupgrade=yes', '');
95 exit;
97 } else {
98 $db->debug=true;
99 if (main_upgrade($CFG->version)) {
100 $db->debug=false;
101 if (set_config("version", $version)) {
102 notify($strdatabasesuccess, "green");
103 print_continue("index.php");
104 exit;
105 } else {
106 notify("Upgrade failed! (Could not update version in config table)");
108 } else {
109 $db->debug=false;
110 notify("Upgrade failed! See /version.php");
113 } else if ($version < $CFG->version) {
114 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
117 } else {
118 if (set_config("version", $version)) {
119 print_header("Elgg $release ($version)");
120 print_continue("index.php");
121 die;
122 } else {
123 $db->debug=true;
124 if (main_upgrade(0)) {
125 print_continue("index.php");
126 } else {
127 error("A problem occurred inserting current version into databases");
129 $db->debug=false;