public function create_db_user()
 {
     $user = Ini::$db_users_name_prefix . $this->id;
     $password = User::generate_password();
     $db_name = Ini::$db_users_db_name_prefix . $this->id;
     $sql = sprintf("CREATE USER '%s'@'localhost' IDENTIFIED BY '%s';", $user, $password);
     mysql_query($sql);
     $this->db_login = $user;
     $this->db_password = $password;
     $this->db_name = $db_name;
     parent::mysql_save();
     $sql = sprintf("CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", $db_name);
     mysql_query($sql);
     $sql = sprintf("GRANT ALL PRIVILEGES ON `%s`.* TO '%s'@'localhost'", $db_name, $user);
     mysql_query($sql);
     Ini::create_db_structure();
 }
Esempio n. 2
0
 public static function update_db($simulate = false, $only_recalculate_hash = false, $only_create_db = false)
 {
     require '../Ini.php';
     $ini = new Ini();
     if ($only_create_db) {
         return Ini::create_db_structure();
     }
     if ($only_recalculate_hash) {
         foreach (User::get_all_db() as $db) {
             mysql_select_db($db);
             OModule::calculate_all_xml_hashes();
         }
         return json_encode(array("result" => 0));
     }
     $versions_to_update = array();
     $previous_version = Setting::get_setting("version");
     if ($previous_version == null) {
         $previous_version = Ini::$version;
     }
     $recalculate_hash = false;
     if (Setup::does_patch_apply("4.0.0.beta2", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta2");
         } else {
             //RDoc.HTML should be larger
             $sql = "ALTER TABLE `" . Ini::$db_master_name . "`.`RDoc` CHANGE `HTML` `HTML` mediumtext NOT NULL;";
             if (!mysql_query($sql)) {
                 return json_encode(array("result" => 1, "param" => $sql));
             }
             //names should be unique
             $tables = array("Test", "Template", "Table", "QTIAssessmentItem");
             foreach (User::get_all_db() as $db) {
                 foreach ($tables as $table) {
                     $sql = sprintf("SELECT `name` , COUNT(  `name` ) AS `c`\r\n                            FROM  `%s`.`%s` \r\n                            GROUP BY `name` \r\n                            HAVING `c`>1", $db, $table);
                     $z = mysql_query($sql);
                     if (!$z) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                     while ($r = mysql_fetch_array($z)) {
                         $sql = sprintf("UPDATE `%s`.`%s` SET `name`=CONCAT(`name`,'_',`id`) WHERE `name`='%s'", $db, $table, mysql_real_escape_string($r[0]));
                         if (!mysql_query($sql)) {
                             return json_encode(array("result" => 1, "param" => $sql));
                         }
                     }
                     //change object name column to unique
                     $sql = sprintf("ALTER TABLE `%s`.`%s` CHANGE  `name`  `name` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $db, $table);
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                     $sql = sprintf("ALTER TABLE  `%s`.`%s` ADD UNIQUE ( `name` )", $db, $table);
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 //add TestSession new columns
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_show'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_show` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_hide'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_hide` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_show_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_show_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='effect_hide_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `effect_hide_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_HTML'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_HTML` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_head'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_head` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_show'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_show` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_hide'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_hide` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_show_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_show_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_effect_hide_options'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_effect_hide_options` TEXT NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_Template_id'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_Template_id` bigint(20) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_UserWorkspace_id'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `loader_UserWorkspace_id` bigint(20) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='loader_Template_id'", $db, Test::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) > 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` DROP COLUMN `loader_Template_id`", $db, Test::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta2");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta2"));
         }
     }
     if (Setup::does_patch_apply("4.0.0.beta4", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta4");
         } else {
             foreach (User::get_all_db() as $db) {
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='open'", $db, Test::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) > 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` CHANGE `open` `type` TINYINT(1) NOT NULL", $db, Test::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta4");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta4"));
         }
     }
     if (Setup::does_patch_apply("4.0.0.beta9", $previous_version)) {
         if ($simulate) {
             array_push($versions_to_update, "4.0.0.beta9");
         } else {
             foreach (User::get_all_db() as $db) {
                 $sql = sprintf("SHOW COLUMNS FROM `%s`.`%s` WHERE `Field`='resume_declined'", $db, TestSession::get_mysql_table());
                 $z = mysql_query($sql);
                 if (mysql_num_rows($z) == 0) {
                     $sql = sprintf("ALTER TABLE `%s`.`%s` ADD  `resume_declined` tinyint(1) NOT NULL", $db, TestSession::get_mysql_table());
                     if (!mysql_query($sql)) {
                         return json_encode(array("result" => 1, "param" => $sql));
                     }
                 }
             }
             Setting::set_setting("version", "4.0.0.beta9");
             return json_encode(array("result" => 0, "param" => "4.0.0.beta9"));
         }
     }
     if ($simulate) {
         return json_encode(array("versions" => $versions_to_update, "recalculate_hash" => $recalculate_hash, "create_db" => Ini::create_db_structure(true)));
     }
     return json_encode(array("result" => 2));
 }