From 73de6eedd9100bf88eed4c8d638f118485869cd3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 10 Jun 2009 01:23:18 -0700 Subject: Rename package -> packager (it's a noun which has verbs on it). Force a HTTP_HOST so that url::base() works. --- modules/gallery/controllers/package.php | 169 ------------------------------ modules/gallery/controllers/packager.php | 171 +++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 169 deletions(-) delete mode 100644 modules/gallery/controllers/package.php create mode 100644 modules/gallery/controllers/packager.php (limited to 'modules/gallery/controllers') diff --git a/modules/gallery/controllers/package.php b/modules/gallery/controllers/package.php deleted file mode 100644 index f5146fc8..00000000 --- a/modules/gallery/controllers/package.php +++ /dev/null @@ -1,169 +0,0 @@ -_reset(); // empty and reinstall the standard modules - $this->_dump_database(); // Dump the database - $this->_dump_var(); // Dump the var directory - } catch (Exception $e) { - print $e->getTraceAsString(); - return; - } - - print "Successfully wrote install.sql and init_var.php\n"; - } - - private function _reset() { - $db = Database::instance(); - - // Drop all tables - foreach ($db->list_tables() as $table) { - $db->query("DROP TABLE IF EXISTS `$table`"); - } - - // Clean out data - dir::unlink(VARPATH . "uploads"); - dir::unlink(VARPATH . "albums"); - dir::unlink(VARPATH . "resizes"); - dir::unlink(VARPATH . "thumbs"); - dir::unlink(VARPATH . "modules"); - dir::unlink(VARPATH . "tmp"); - - $db->clear_cache(); - module::$modules = array(); - module::$active = array(); - - // Use a known random seed so that subsequent packaging runs will reuse the same random - // numbers, keeping our install.sql file more stable. - srand(0); - - gallery_installer::install(true); - module::load_modules(); - - foreach (array("user", "comment", "organize", "info", "rss", - "search", "slideshow", "tag") as $module_name) { - module::install($module_name); - module::activate($module_name); - } - } - - private function _dump_database() { - // We now have a clean install with just the packages that we want. Make sure that the - // database is clean too. - $i = 1; - foreach (array("blocks_dashboard_sidebar", "blocks_dashboard_center") as $key) { - $blocks = array(); - foreach (unserialize(module::get_var("gallery", $key)) as $rnd => $value) { - $blocks[++$i] = $value; - } - module::set_var("gallery", $key, serialize($blocks)); - } - - $db = Database::instance(); - $db->query("TRUNCATE {sessions}"); - $db->query("TRUNCATE {logs}"); - $db->query("DELETE FROM {vars} WHERE `module_name` = 'core' AND `name` = '_cache'"); - $db->update("users", array("password" => ""), array("id" => 1)); - $db->update("users", array("password" => ""), array("id" => 2)); - - $dbconfig = Kohana::config('database.default'); - $conn = $dbconfig["connection"]; - $pass = $conn["pass"] ? "-p{$conn['pass']}" : ""; - $sql_file = DOCROOT . "installer/install.sql"; - if (!is_writable($sql_file)) { - print "$sql_file is not writeable"; - return; - } - $command = "mysqldump --compact --add-drop-table -h{$conn['host']} " . - "-u{$conn['user']} $pass {$conn['database']} > $sql_file"; - exec($command, $output, $status); - if ($status) { - print "
";
-      print "$command\n";
-      print "Failed to dump database\n";
-      print implode("\n", $output);
-      return;
-    }
-
-    // Post-process the sql file
-    $buf = "";
-    $root = ORM::factory("item", 1);
-    $root_created_timestamp = $root->created;
-    $root_updated_timestamp = $root->updated;
-    foreach (file($sql_file) as $line) {
-      // Prefix tables
-      $line = preg_replace(
-        "/(CREATE TABLE|IF EXISTS|INSERT INTO) `{$dbconfig['table_prefix']}(\w+)`/", "\\1 {\\2}",
-        $line);
-
-      // Normalize dates
-      $line = preg_replace("/,$root_created_timestamp,/", ",UNIX_TIMESTAMP(),", $line);
-      $line = preg_replace("/,$root_updated_timestamp,/", ",UNIX_TIMESTAMP(),", $line);
-      $buf .= $line;
-    }
-    $fd = fopen($sql_file, "wb");
-    fwrite($fd, $buf);
-    fclose($fd);
-  }
-
-  private function _dump_var() {
-    $objects = new RecursiveIteratorIterator(
-      new RecursiveDirectoryIterator(VARPATH),
-      RecursiveIteratorIterator::SELF_FIRST);
-
-    $var_file = DOCROOT . "installer/init_var.php";
-    if (!is_writable($var_file)) {
-      print "$var_file is not writeable";
-      return;
-    }
-
-    $paths = array();
-    foreach($objects as $name => $file){
-      if ($file->getBasename() == "database.php") {
-        continue;
-      } else if (basename($file->getPath()) == "logs") {
-        continue;
-      }
-
-      if ($file->isDir()) {
-        $paths[] = "VARPATH . \"" . substr($name, strlen(VARPATH)) . "\"";
-      } else {
-        // @todo: serialize non-directories
-        print "Unknown file: $name";
-        return;
-      }
-    }
-    // Sort the paths so that the var file is stable
-    sort($paths);
-
-    $fd = fopen($var_file, "w");
-    fwrite($fd, "\n");
-    fwrite($fd, "_reset();                // empty and reinstall the standard modules
+      $this->_dump_database();        // Dump the database
+      $this->_dump_var();             // Dump the var directory
+    } catch (Exception $e) {
+      print $e->getTraceAsString();
+      return;
+    }
+
+    print "Successfully wrote install.sql and init_var.php\n";
+  }
+
+  private function _reset() {
+    $db = Database::instance();
+
+    // Drop all tables
+    foreach ($db->list_tables() as $table) {
+      $db->query("DROP TABLE IF EXISTS `$table`");
+    }
+
+    // Clean out data
+    dir::unlink(VARPATH . "uploads");
+    dir::unlink(VARPATH . "albums");
+    dir::unlink(VARPATH . "resizes");
+    dir::unlink(VARPATH . "thumbs");
+    dir::unlink(VARPATH . "modules");
+    dir::unlink(VARPATH . "tmp");
+
+    $db->clear_cache();
+    module::$modules = array();
+    module::$active = array();
+
+    // Use a known random seed so that subsequent packaging runs will reuse the same random
+    // numbers, keeping our install.sql file more stable.
+    srand(0);
+
+    gallery_installer::install(true);
+    module::load_modules();
+
+    foreach (array("user", "comment", "organize", "info", "rss",
+                   "search", "slideshow", "tag") as $module_name) {
+      module::install($module_name);
+      module::activate($module_name);
+    }
+  }
+
+  private function _dump_database() {
+    // We now have a clean install with just the packages that we want.  Make sure that the
+    // database is clean too.
+    $i = 1;
+    foreach (array("blocks_dashboard_sidebar", "blocks_dashboard_center") as $key) {
+      $blocks = array();
+      foreach (unserialize(module::get_var("gallery", $key)) as $rnd => $value) {
+        $blocks[++$i] = $value;
+      }
+      module::set_var("gallery", $key, serialize($blocks));
+    }
+
+    $db = Database::instance();
+    $db->query("TRUNCATE {sessions}");
+    $db->query("TRUNCATE {logs}");
+    $db->query("DELETE FROM {vars} WHERE `module_name` = 'core' AND `name` = '_cache'");
+    $db->update("users", array("password" => ""), array("id" => 1));
+    $db->update("users", array("password" => ""), array("id" => 2));
+
+    $dbconfig = Kohana::config('database.default');
+    $conn = $dbconfig["connection"];
+    $pass = $conn["pass"] ? "-p{$conn['pass']}" : "";
+    $sql_file = DOCROOT . "installer/install.sql";
+    if (!is_writable($sql_file)) {
+      print "$sql_file is not writeable";
+      return;
+    }
+    $command = "mysqldump --compact --add-drop-table -h{$conn['host']} " .
+      "-u{$conn['user']} $pass {$conn['database']} > $sql_file";
+    exec($command, $output, $status);
+    if ($status) {
+      print "
";
+      print "$command\n";
+      print "Failed to dump database\n";
+      print implode("\n", $output);
+      return;
+    }
+
+    // Post-process the sql file
+    $buf = "";
+    $root = ORM::factory("item", 1);
+    $root_created_timestamp = $root->created;
+    $root_updated_timestamp = $root->updated;
+    foreach (file($sql_file) as $line) {
+      // Prefix tables
+      $line = preg_replace(
+        "/(CREATE TABLE|IF EXISTS|INSERT INTO) `{$dbconfig['table_prefix']}(\w+)`/", "\\1 {\\2}",
+        $line);
+
+      // Normalize dates
+      $line = preg_replace("/,$root_created_timestamp,/", ",UNIX_TIMESTAMP(),", $line);
+      $line = preg_replace("/,$root_updated_timestamp,/", ",UNIX_TIMESTAMP(),", $line);
+      $buf .= $line;
+    }
+    $fd = fopen($sql_file, "wb");
+    fwrite($fd, $buf);
+    fclose($fd);
+  }
+
+  private function _dump_var() {
+    $objects = new RecursiveIteratorIterator(
+      new RecursiveDirectoryIterator(VARPATH),
+      RecursiveIteratorIterator::SELF_FIRST);
+
+    $var_file = DOCROOT . "installer/init_var.php";
+    if (!is_writable($var_file)) {
+      print "$var_file is not writeable";
+      return;
+    }
+
+    $paths = array();
+    foreach($objects as $name => $file){
+      if ($file->getBasename() == "database.php") {
+        continue;
+      } else if (basename($file->getPath()) == "logs") {
+        continue;
+      }
+
+      if ($file->isDir()) {
+        $paths[] = "VARPATH . \"" . substr($name, strlen(VARPATH)) . "\"";
+      } else {
+        // @todo: serialize non-directories
+        print "Unknown file: $name";
+        return;
+      }
+    }
+    // Sort the paths so that the var file is stable
+    sort($paths);
+
+    $fd = fopen($var_file, "w");
+    fwrite($fd, "\n");
+    fwrite($fd, "
Date: Wed, 10 Jun 2009 01:23:44 -0700
Subject: Add CLI sapi support

---
 modules/gallery/controllers/upgrader.php | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

(limited to 'modules/gallery/controllers')

diff --git a/modules/gallery/controllers/upgrader.php b/modules/gallery/controllers/upgrader.php
index 0d5bb4f6..0833e253 100644
--- a/modules/gallery/controllers/upgrader.php
+++ b/modules/gallery/controllers/upgrader.php
@@ -32,7 +32,11 @@ class Upgrader_Controller extends Controller {
 
   public function upgrade() {
     // Todo: give the admin a chance to log in here
-    if (!user::active()->admin) {
+    if (php_sapi_name() == "cli") {
+      // @todo this may screw up some module installers, but we don't have a better answer at
+      // this time.
+      $_SERVER["HTTP_HOST"] = "example.com";
+    } else if (!user::active()->admin) {
       access::forbidden();
     }
 
@@ -51,6 +55,10 @@ class Upgrader_Controller extends Controller {
       }
     }
 
-    url::redirect("upgrader?done=1");
+    if (php_sapi_name() == "cli") {
+      print "Upgrade complete\n";
+    } else {
+      url::redirect("upgrader?done=1");
+    }
   }
 }
-- 
cgit v1.2.3


From 0d5826ab9ff919a9a191e654cb3a3032d31494f4 Mon Sep 17 00:00:00 2001
From: Bharat Mediratta 
Date: Wed, 10 Jun 2009 20:37:38 -0700
Subject: Fix log links.  html::anchor() implicitly calls url::site(), calling
 it a second time mangles the urls.

Fixes ticket #360.
---
 modules/gallery/controllers/admin_maintenance.php | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

(limited to 'modules/gallery/controllers')

diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php
index c169de75..7c5934a3 100644
--- a/modules/gallery/controllers/admin_maintenance.php
+++ b/modules/gallery/controllers/admin_maintenance.php
@@ -61,7 +61,7 @@ class Admin_Maintenance_Controller extends Admin_Controller {
 
     log::info("tasks", t("Task %task_name started (task id %task_id)",
                          array("task_name" => $task->name, "task_id" => $task->id)),
-              html::anchor(url::site("admin/maintenance"), t("maintenance")));
+              html::anchor("admin/maintenance", t("maintenance")));
     print $view;
   }
 
@@ -81,7 +81,7 @@ class Admin_Maintenance_Controller extends Admin_Controller {
 
     log::info("tasks", t("Task %task_name resumed (task id %task_id)",
                          array("task_name" => $task->name, "task_id" => $task->id)),
-              html::anchor(url::site("admin/maintenance"), t("maintenance")));
+              html::anchor("admin/maintenance", t("maintenance")));
     print $view;
   }
 
@@ -152,14 +152,14 @@ class Admin_Maintenance_Controller extends Admin_Controller {
       case "success":
         log::success("tasks", t("Task %task_name completed (task id %task_id)",
                                 array("task_name" => $task->name, "task_id" => $task->id)),
-                     html::anchor(url::site("admin/maintenance"), t("maintenance")));
+                     html::anchor("admin/maintenance", t("maintenance")));
         message::success(t("Task completed successfully"));
         break;
 
       case "error":
         log::error("tasks", t("Task %task_name failed (task id %task_id)",
                               array("task_name" => $task->name, "task_id" => $task->id)),
-                   html::anchor(url::site("admin/maintenance"), t("maintenance")));
+                   html::anchor("admin/maintenance", t("maintenance")));
         message::success(t("Task failed"));
         break;
       }
-- 
cgit v1.2.3


From 4118ca4f1db76447da8846fc4835fea702687d21 Mon Sep 17 00:00:00 2001
From: Bharat Mediratta 
Date: Wed, 10 Jun 2009 21:05:24 -0700
Subject: Provide a way for non-admins to authenticate and use the upgrader,
 without using our regular code paths.

---
 modules/gallery/controllers/upgrader.php | 19 ++++++++++++++-----
 modules/gallery/views/upgrader.html.php  | 16 ++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

(limited to 'modules/gallery/controllers')

diff --git a/modules/gallery/controllers/upgrader.php b/modules/gallery/controllers/upgrader.php
index 0833e253..5eb96fdd 100644
--- a/modules/gallery/controllers/upgrader.php
+++ b/modules/gallery/controllers/upgrader.php
@@ -19,24 +19,33 @@
  */
 class Upgrader_Controller extends Controller {
   public function index() {
-    // Todo: give the admin a chance to log in here
-    if (!user::active()->admin) {
-      access::forbidden();
+    $session = Session::instance();
+
+    // Make sure we have an upgrade token
+    if (!($upgrade_token = $session->get("upgrade_token", null))) {
+      $session->set("upgrade_token", $upgrade_token = md5(rand()));
+    }
+
+    // If the upgrade token exists, then bless this session
+    if (file_exists(TMPPATH . $upgrade_token)) {
+      $session->set("can_upgrade", true);
+      @unlink(TMPPATH . $upgrade_token);
     }
 
     $view = new View("upgrader.html");
+    $view->can_upgrade = user::active()->admin || $session->get("can_upgrade");
+    $view->upgrade_token = $upgrade_token;
     $view->available = module::available();
     $view->done = Input::instance()->get("done");
     print $view;
   }
 
   public function upgrade() {
-    // Todo: give the admin a chance to log in here
     if (php_sapi_name() == "cli") {
       // @todo this may screw up some module installers, but we don't have a better answer at
       // this time.
       $_SERVER["HTTP_HOST"] = "example.com";
-    } else if (!user::active()->admin) {
+    } else if (!user::active()->admin && !Session::instance()->get("can_upgrade", false)) {
       access::forbidden();
     }
 
diff --git a/modules/gallery/views/upgrader.html.php b/modules/gallery/views/upgrader.html.php
index 6b9a0110..fa21e196 100644
--- a/modules/gallery/views/upgrader.html.php
+++ b/modules/gallery/views/upgrader.html.php
@@ -9,6 +9,9 @@
       font-family: Trebuchet MS;
       font-size: 1.1em;
     }
+    h1 {
+      font-size: 1.4em;
+    }
     div#outer {
       width: 650px;
       background: white;
@@ -93,11 +96,17 @@
     .gray_on_done {
       opacity: ;
     }
+    pre {
+      display: inline;
+      margin: 0px;
+      padding: 0px;
+    }
   
   
     
" />
+
@@ -158,6 +167,13 @@

+ +

+

+ gallery3/var/tmp directory.", array("name" => "
$upgrade_token")) ?> +

+ "> +