Exemplo n.º 1
0
 public function RCall($code, $include_ini_code = false, $close = false, $debug_syntax = false)
 {
     $command = "";
     if (!$debug_syntax) {
         if ($include_ini_code) {
             $command = $this->get_ini_RCode();
         } else {
             $command .= $this->get_next_ini_RCode();
         }
     }
     $command .= $code;
     if (!$debug_syntax) {
         $command .= $this->get_post_RCode();
     }
     $output = array();
     $return = -999;
     $command_obj = json_encode(array("session_id" => $this->id, "code" => $command, "close" => 0));
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- checking for server");
     }
     if (!TestServer::is_running()) {
         TestServer::start_process();
     }
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- server found, trying to send");
     }
     $response = TestServer::send($command_obj);
     $result = json_decode(trim($response));
     if (TestServer::$debug) {
         TestServer::log_debug("TestSession->RCall --- sent and recieved response");
     }
     $output = explode("\n", $result->output);
     $return = $result->return;
     $thisSession = null;
     $status = TestSession::TEST_SESSION_STATUS_ERROR;
     $removed = false;
     $release = 0;
     $html = "";
     $head = "";
     $Template_id = 0;
     $debug = 0;
     $hash = "";
     $time_limit = 0;
     $Test_id = 0;
     $finished = 0;
     $loader_HTML = "";
     $loader_head = "";
     $loader_effect_show = "none";
     $loader_effect_hide = "none";
     $loader_effect_show_options = "";
     $loader_effect_hide_options = "";
     $effect_show = "none";
     $effect_hide = "none";
     $effect_show_options = "";
     $effect_hide_options = "";
     if (!$debug_syntax) {
         $thisSession = TestSession::from_mysql_id($this->id);
         if ($thisSession != null) {
             $status = $thisSession->status;
             $release = $thisSession->release;
             $html = $thisSession->HTML;
             $Template_id = $thisSession->Template_id;
             $debug = $thisSession->debug;
             $hash = $thisSession->hash;
             $time_limit = $thisSession->time_limit;
             $Test_id = $thisSession->Test_id;
             $loader_HTML = $thisSession->loader_HTML;
             $loader_head = $thisSession->loader_head;
             $loader_effect_hide = $thisSession->loader_effect_hide;
             $loader_effect_hide_options = $thisSession->loader_effect_hide_options;
             $loader_effect_show = $thisSession->loader_effect_show;
             $loader_effect_show_options = $thisSession->loader_effect_show_options;
             $effect_hide = $thisSession->effect_hide;
             $effect_hide_options = $thisSession->effect_hide_options;
             $effect_show = $thisSession->effect_show;
             $effect_show_options = $thisSession->effect_show_options;
             if ($return != 0) {
                 $status = TestSession::TEST_SESSION_STATUS_ERROR;
             }
             if ($status == TestSession::TEST_SESSION_STATUS_WORKING && $release == 1 || $close) {
                 $status = TestSession::TEST_SESSION_STATUS_COMPLETED;
             }
             $thisSession->status = $status;
             $thisSession->mysql_save();
             switch ($status) {
                 case TestSession::TEST_SESSION_STATUS_COMPLETED:
                     if ($debug) {
                         TestSession::unregister($thisSession->id);
                         $removed = true;
                     } else {
                         $thisSession->serialize();
                     }
                     break;
                 case TestSession::TEST_SESSION_STATUS_ERROR:
                 case TestSession::TEST_SESSION_STATUS_TAMPERED:
                     if ($debug) {
                         TestSession::unregister($thisSession->id);
                         $removed = true;
                     } else {
                         $thisSession->close();
                     }
                     break;
                 case TestSession::TEST_SESSION_STATUS_TEMPLATE:
                     if ($debug) {
                         $html = Template::strip_html($html);
                         if ($release) {
                             TestSession::unregister($thisSession->id);
                         }
                     } else {
                         $head = Template::from_mysql_id($Template_id)->head;
                         if ($release) {
                             $thisSession->serialize();
                         }
                     }
                     break;
             }
         } else {
             $removed = true;
         }
     }
     $test = Test::from_mysql_id($this->Test_id);
     $debug_data = false;
     $logged_user = User::get_logged_user();
     if ($logged_user != null) {
         $debug_data = $logged_user->is_object_readable($test);
     }
     if ($release == 1 || $status == TestSession::TEST_SESSION_STATUS_COMPLETED || $status == TestSession::TEST_SESSION_STATUS_ERROR || $status == TestSession::TEST_SESSION_STATUS_TAMPERED) {
         $finished = 1;
     }
     if (!$debug_syntax) {
         $response = array("data" => array("HEAD" => $head, "HASH" => $hash, "TIME_LIMIT" => $time_limit, "HTML" => $html, "TEST_ID" => $Test_id, "TEST_SESSION_ID" => $this->id, "STATUS" => $status, "TEMPLATE_ID" => $Template_id, "FINISHED" => $finished, "LOADER_HTML" => $loader_HTML, "LOADER_HEAD" => $loader_head, "LOADER_EFFECT_SHOW" => $loader_effect_show, "LOADER_EFFECT_SHOW_OPTIONS" => $loader_effect_show_options, "LOADER_EFFECT_HIDE" => $loader_effect_hide, "LOADER_EFFECT_HIDE_OPTIONS" => $loader_effect_hide_options, "EFFECT_SHOW" => $effect_show, "EFFECT_HIDE" => $effect_hide, "EFFECT_SHOW_OPTIONS" => $effect_show_options, "EFFECT_HIDE_OPTIONS" => $effect_hide_options));
     }
     if ($debug_data) {
         for ($i = 0; $i < count($output); $i++) {
             if (strpos($output[$i], "CONCERTO_DB_PASSWORD <-") !== false) {
                 $output[$i] = "[hidden]";
             }
             $output[$i] = htmlspecialchars($output[$i], ENT_QUOTES);
         }
         $command_lines = explode("\n", $command);
         for ($i = 0; $i < count($command_lines); $i++) {
             if (strpos($command_lines[$i], "CONCERTO_DB_PASSWORD <-") !== false) {
                 $command_lines[$i] = "[hidden]";
             }
         }
         $command = implode("\n", $command_lines);
         $command = htmlspecialchars($command, ENT_QUOTES);
         if (!is_array($response)) {
             $response = array();
         }
         $response["debug"] = array("code" => $command, "return" => $return, "output" => $output);
     }
     if (Ini::$timer_tamper_prevention && !$debug_syntax && !$removed) {
         $sql = sprintf("UPDATE `%s` SET `time_tamper_prevention`=%d WHERE `id`=%d", TestSession::get_mysql_table(), time(), $this->id);
         mysql_query($sql);
     }
     return $response;
 }
Exemplo n.º 2
0
 public function RCall($values = null, $code = null, $resume_from_last_template = false)
 {
     if (Ini::$log_server_events) {
         TestServer::log_debug("TestSession->RCall --- R call initiated #" . session_id());
     }
     //resume from last template
     if ($resume_from_last_template) {
         $response = array("data" => array("HEAD" => $this->head, "HASH" => $this->hash, "TIME_LIMIT" => $this->time_limit, "HTML" => $this->HTML, "TEST_ID" => $this->Test_id, "TEST_SESSION_ID" => $this->id, "STATUS" => TestSession::TEST_SESSION_STATUS_TEMPLATE, "TEMPLATE_ID" => $this->Template_id, "LOADER_TEMPLATE_ID" => $this->loader_Template_id, "FINISHED" => 0, "EFFECT_SHOW" => $this->effect_show, "EFFECT_HIDE" => $this->effect_hide, "EFFECT_SHOW_OPTIONS" => $this->effect_show_options, "EFFECT_HIDE_OPTIONS" => $this->effect_hide_options, "LOADER_HTML" => $this->loader_HTML, "LOADER_HEAD" => $this->loader_head, "LOADER_EFFECT_SHOW" => $this->loader_effect_show, "LOADER_EFFECT_SHOW_OPTIONS" => $this->loader_effect_show_options, "LOADER_EFFECT_HIDE" => $this->loader_effect_hide, "LOADER_EFFECT_HIDE_OPTIONS" => $this->loader_effect_hide_options, "LOADER_HTML" => $this->loader_HTML, "LOADER_HEAD" => $this->loader_head, "LOADER_EFFECT_SHOW" => $this->loader_effect_show, "LOADER_EFFECT_SHOW_OPTIONS" => $this->loader_effect_show_options, "LOADER_EFFECT_HIDE" => $this->loader_effect_hide, "LOADER_EFFECT_HIDE_OPTIONS" => $this->loader_effect_hide_options));
         return $response;
     }
     //R server connection
     $command_obj = json_encode(array("workspace_id" => $this->UserWorkspace_id, "session_id" => $this->id, "hash" => $this->hash, "values" => $values, "code" => $code, "type" => 0, "IP" => $_SERVER["REMOTE_ADDR"]));
     if (Ini::$log_server_events) {
         TestServer::log_debug("TestSession->RCall --- checking for server");
     }
     if (TestServer::get_server_status() == TestServer::SERVER_STATUS_STOPPED) {
         TestServer::start_process();
         TestServer::wait_until_started();
     } else {
         if (TestServer::get_server_status() == TestServer::SERVER_STATUS_STARTING) {
             TestServer::wait_until_started();
         }
     }
     if (Ini::$log_server_events) {
         TestServer::log_debug("TestSession->RCall --- server found, trying to send");
     }
     $response = TestServer::send($command_obj);
     $result = json_decode(trim($response));
     if (Ini::$log_server_events) {
         TestServer::log_debug("TestSession->RCall --- sent and recieved response");
     }
     $status = TestSession::TEST_SESSION_STATUS_ERROR;
     $removed = false;
     $release = 0;
     $html = "";
     $head = "";
     $Template_id = 0;
     $debug = 0;
     $hash = "";
     $time_limit = 0;
     $Test_id = 0;
     $finished = 0;
     $loader_Template_id = 0;
     $loader_HTML = "";
     $loader_head = "";
     $loader_effect_show = "none";
     $loader_effect_hide = "none";
     $loader_effect_show_options = "";
     $loader_effect_hide_options = "";
     $effect_show = "none";
     $effect_hide = "none";
     $effect_show_options = "";
     $effect_hide_options = "";
     $state = "[]";
     $output = "";
     $thisSession = TestSession::from_mysql_id($this->id);
     $return = $result->return;
     if ($thisSession != null) {
         $output = $thisSession->output;
         $error_output = $thisSession->error_output;
         $state = $thisSession->state;
         $status = $thisSession->status;
         $release = $thisSession->release;
         $html = $thisSession->HTML;
         $head = $thisSession->head;
         $Template_id = $thisSession->Template_id;
         $debug = $thisSession->debug;
         $hash = $thisSession->hash;
         $time_limit = $thisSession->time_limit;
         $Test_id = $thisSession->Test_id;
         $loader_Template_id = $thisSession->loader_Template_id;
         $loader_HTML = $thisSession->loader_HTML;
         $loader_head = $thisSession->loader_head;
         $loader_effect_hide = $thisSession->loader_effect_hide;
         $loader_effect_hide_options = $thisSession->loader_effect_hide_options;
         $loader_effect_show = $thisSession->loader_effect_show;
         $loader_effect_show_options = $thisSession->loader_effect_show_options;
         $effect_hide = $thisSession->effect_hide;
         $effect_hide_options = $thisSession->effect_hide_options;
         $effect_show = $thisSession->effect_show;
         $effect_show_options = $thisSession->effect_show_options;
         if ($return != 0) {
             $status = TestSession::TEST_SESSION_STATUS_ERROR;
         }
         if ($status == TestSession::TEST_SESSION_STATUS_WORKING && $release == 1) {
             $status = TestSession::TEST_SESSION_STATUS_COMPLETED;
         }
         $thisSession->status = $status;
         $thisSession->mysql_save();
         switch ($status) {
             case TestSession::TEST_SESSION_STATUS_COMPLETED:
                 if ($debug) {
                     TestSession::unregister($thisSession->UserWorkspace_id . "-" . $thisSession->id, $thisSession->UserWorkspace_id);
                     $removed = true;
                 } else {
                     $thisSession->close();
                 }
                 break;
             case TestSession::TEST_SESSION_STATUS_ERROR:
                 if ($debug) {
                     TestSession::unregister($thisSession->UserWorkspace_id . "-" . $thisSession->id, $thisSession->UserWorkspace_id);
                     $removed = true;
                 } else {
                     $thisSession->close();
                     if (Ini::$log_r_errors) {
                         $thisSession->log_error(nl2br(htmlspecialchars($error_output, ENT_QUOTES)));
                     }
                 }
                 break;
             case TestSession::TEST_SESSION_STATUS_TAMPERED:
                 if ($debug) {
                     TestSession::unregister($thisSession->UserWorkspace_id . "-" . $thisSession->id, $thisSession->UserWorkspace_id);
                     $removed = true;
                 } else {
                     $thisSession->close();
                 }
                 break;
             case TestSession::TEST_SESSION_STATUS_TEMPLATE:
                 if ($release) {
                     if ($debug) {
                         TestSession::unregister($thisSession->UserWorkspace_id . "-" . $thisSession->id, $thisSession->UserWorkspace_id);
                         $removed = true;
                     } else {
                         $thisSession->close();
                     }
                 }
                 break;
         }
     } else {
         $removed = true;
     }
     $debug_data = false;
     $logged_user = User::get_logged_user();
     if ($logged_user != null) {
         $debug_data = true;
     }
     if ($release == 1 || $status == TestSession::TEST_SESSION_STATUS_COMPLETED || $status == TestSession::TEST_SESSION_STATUS_ERROR || $status == TestSession::TEST_SESSION_STATUS_TAMPERED) {
         $finished = 1;
     }
     $response = array("data" => array("HEAD" => $head, "HASH" => $hash, "TIME_LIMIT" => $time_limit, "HTML" => $html, "TEST_ID" => $Test_id, "TEST_SESSION_ID" => $this->id, "STATUS" => $status, "TEMPLATE_ID" => $Template_id, "FINISHED" => $finished, "LOADER_TEMPLATE_ID" => $loader_Template_id, "LOADER_HTML" => $loader_HTML, "LOADER_HEAD" => $loader_head, "LOADER_EFFECT_SHOW" => $loader_effect_show, "LOADER_EFFECT_SHOW_OPTIONS" => $loader_effect_show_options, "LOADER_EFFECT_HIDE" => $loader_effect_hide, "LOADER_EFFECT_HIDE_OPTIONS" => $loader_effect_hide_options, "EFFECT_SHOW" => $effect_show, "EFFECT_HIDE" => $effect_hide, "EFFECT_SHOW_OPTIONS" => $effect_show_options, "EFFECT_HIDE_OPTIONS" => $effect_hide_options));
     if ($debug_data) {
         if (!is_array($response)) {
             $response = array();
         }
         if ($output != null) {
             if (strpos(trim($output), ">") !== 0 && strpos(trim($output), "[") !== 0) {
                 $output = "> " . trim($output);
             }
         }
         if ($debug) {
             $state = json_decode($state, true);
             if ($state != null) {
                 foreach ($state as $k => $v) {
                     $state[$k] = htmlspecialchars($v, ENT_QUOTES);
                 }
                 $state = json_encode($state);
             } else {
                 $state = "[]";
             }
             $response["debug"] = array("return" => $return, "output" => nl2br(htmlspecialchars($output, ENT_QUOTES)), "error_output" => nl2br(htmlspecialchars($error_output, ENT_QUOTES)), "state" => nl2br($state));
         }
     }
     if (Ini::$timer_tamper_prevention && !$removed) {
         $sql = sprintf("UPDATE `%s` SET `time_tamper_prevention`=%d WHERE `id`=%d", TestSession::get_mysql_table(), time(), $this->id);
         mysql_query($sql);
     }
     return $response;
 }
Exemplo n.º 3
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));
 }