Exemple #1
0
function run()
{
    global $website;
    global $layout;
    // force no cache on this page. Thanks to: http://james.cridland.net/code/caching.html
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    switch (@$_REQUEST['act']) {
        case 'manual_update':
            $ok = update::install_from_repository(intval($_POST['update_manual_file']));
            if ($ok) {
                $layout->navigate_notification(t(293, "Application successfully updated"), false);
            } else {
                $files = glob(NAVIGATE_PATH . '/updates/update-*.log.txt');
                $log_location = array_pop($files);
                $log_location = str_replace(NAVIGATE_PATH, NAVIGATE_URL, $log_location);
                $layout->navigate_notification(t(294, "Error updating.") . "<br /><a href='" . $log_location . "' target='_blank'>" . t(366, "Log") . "</a>", true, true);
            }
            $out = update_list();
            break;
        case 'install_next_update':
            // install next update
            $updates = update::updates_available();
            $update_summary = base64_decode($updates[0]['text']);
            $ok = update::install_from_navigatecms($updates);
            if ($ok) {
                $layout->navigate_notification(t(293, "Application successfully updated"), false);
                $layout->add_content('
                    <div style=" display: none; " id="navigate_installed_update_summary">' . $update_summary . '</div>
                ');
                $layout->add_script('
                    $("#navigate_installed_update_summary").dialog({
                        modal: true,
                        title: "Navigate v' . $updates[0]['Version'] . ' r' . $updates[0]['Revision'] . '",
                        width: 650,
                        height: 400
                    });
                ');
            } else {
                $files = glob(NAVIGATE_PATH . '/updates/update-*.log.txt');
                $log_location = array_pop($files);
                $log_location = str_replace(NAVIGATE_PATH, NAVIGATE_URL, $log_location);
                $layout->navigate_notification(t(294, "Error updating.") . "<br /><a href='" . $log_location . "' target='_blank'>" . t(366, "Log") . "</a>", true, true);
            }
            $out = update_list();
            break;
        case 'cache_clean':
            update::cache_clean();
            // don't break
        // don't break
        case 0:
        default:
            $out = update_list();
    }
    return $out;
}
 public static function install_from_file($version, $revision, $ufile, $ulog)
 {
     global $DB;
     // remove old update files (will be there if a previous update fails)
     file_put_contents($ulog, "remove old update folder\n", FILE_APPEND);
     core_remove_folder(NAVIGATE_PATH . '/updates/update');
     // decompress
     file_put_contents($ulog, "create new folder\n", FILE_APPEND);
     mkdir(NAVIGATE_PATH . '/updates/update');
     $zip = new ZipArchive();
     file_put_contents($ulog, "open zip file\n", FILE_APPEND);
     if ($zip->open($ufile) === TRUE) {
         file_put_contents($ulog, "extract zip file\n", FILE_APPEND);
         $zip->extractTo(NAVIGATE_PATH . '/updates/update');
         $zip->close();
     } else {
         file_put_contents($ulog, "zip extraction failed\n", FILE_APPEND);
         @unlink($ufile);
         core_remove_folder(NAVIGATE_PATH . '/updates/update');
         return false;
     }
     // chmod files (may fail, but not fatal error)
     file_put_contents($ulog, "chmod update (may fail in Windows)... ", FILE_APPEND);
     $chmod_status = core_chmodr(NAVIGATE_PATH . '/updates/update', 0755);
     file_put_contents($ulog, $chmod_status . "\n", FILE_APPEND);
     // do file changes
     file_put_contents($ulog, "parse file changes\n", FILE_APPEND);
     $hgchanges = file_get_contents(NAVIGATE_PATH . '/updates/update/changes.txt');
     $hgchanges = explode("\n", $hgchanges);
     foreach ($hgchanges as $change) {
         file_put_contents($ulog, $change . "\n", FILE_APPEND);
         $change = trim($change);
         if (empty($change)) {
             continue;
         }
         $change = explode(" ", $change, 2);
         // new, removed and modified files
         // M = modified
         // A = added
         // R = removed
         // C = clean
         // ! = missing (deleted by non-hg command, but still tracked)
         // ? = not tracked
         // I = ignored
         //   = origin of the previous file listed as A (added)
         $file = str_replace('\\', '/', $change[1]);
         //if(substr($file, 0, strlen('plugins/'))=='plugins/') continue;
         if (substr($file, 0, strlen('setup/')) == 'setup/') {
             continue;
         }
         switch ($change[0]) {
             case 'A':
                 // added a new file
             // added a new file
             case 'M':
                 // modified file
                 if (!file_exists(NAVIGATE_PATH . '/updates/update/' . $file)) {
                     file_put_contents($ulog, "file doesn't exist!\n", FILE_APPEND);
                     return false;
                 }
                 @mkdir(dirname(NAVIGATE_PATH . '/' . $file), 0777, true);
                 if (!@copy(NAVIGATE_PATH . '/updates/update/' . $file, NAVIGATE_PATH . '/' . $file)) {
                     file_put_contents($ulog, "cannot copy file!\n", FILE_APPEND);
                     return false;
                 }
                 break;
             case 'R':
                 // remove file
                 @unlink(NAVIGATE_PATH . '/' . $file);
                 break;
             default:
                 // all other cases
                 // IGNORE the change, as we are now only getting the modified files
         }
     }
     // process SQL updates
     file_put_contents($ulog, "process sql update\n", FILE_APPEND);
     if (file_exists(NAVIGATE_PATH . '/updates/update/update.sql')) {
         $sql = file_get_contents(NAVIGATE_PATH . '/updates/update/update.sql');
         // execute SQL in a transaction
         // http://php.net/manual/en/pdo.transactions.php
         try {
             // can't do it in one step => SQLSTATE[HY000]: General error: 2014
             $sql = explode("\n\n", $sql);
             //file_put_contents($ulog, "begin transaction\n", FILE_APPEND);
             //$DB->beginTransaction();
             foreach ($sql as $sqlline) {
                 $sqlline = trim($sqlline);
                 if (empty($sqlline)) {
                     continue;
                 }
                 file_put_contents($ulog, "execute sql:\n" . $sqlline . "\n", FILE_APPEND);
                 if (!$DB->execute($sqlline)) {
                     file_put_contents($ulog, "execute failed: " . $DB->get_last_error() . "\n", FILE_APPEND);
                     //throw new Exception($DB->get_last_error());
                 }
                 // force commit changes (slower but safer... no --> SQLSTATE[HY000]: General error: 2014)
                 $DB->disconnect();
                 $DB->connect();
             }
             //file_put_contents($ulog, "commit transaction\n", FILE_APPEND);
             //$DB->commit();
         } catch (Exception $e) {
             file_put_contents($ulog, "transaction error: \n" . $e->getMessage() . "\n", FILE_APPEND);
             //$DB->rollBack();
             return false;
         }
     } else {
         file_put_contents($ulog, "no SQL found\n", FILE_APPEND);
     }
     // add the update row to know which navigate revision is currently installed
     file_put_contents($ulog, "insert new version row on updates\n", FILE_APPEND);
     $urow = new update();
     $urow->id = 0;
     $urow->version = $version;
     $urow->revision = $revision;
     $urow->date_updated = time();
     $urow->status = 'ok';
     $urow->changelog = '';
     try {
         $ok = $urow->insert();
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
     if ($error) {
         file_put_contents($ulog, "execute insert failed:\n" . $DB->get_last_error() . "\n", FILE_APPEND);
     }
     if (file_exists(NAVIGATE_PATH . '/updates/update/update-post.php')) {
         include_once NAVIGATE_PATH . '/updates/update/update-post.php';
     }
     file_put_contents($ulog, "update finished!\n", FILE_APPEND);
     $urow->changelog = file_get_contents($ulog);
     $urow->save();
     @unlink($ufile);
     update::cache_clean();
     return true;
 }