public function index()
 {
     // Get rid of old deleted/spam comments once in a while
     db::build()->delete("comments")->where("state", "IN", array("deleted", "spam"))->where("updated", "<", db::expr("UNIX_TIMESTAMP() - 86400 * 7"))->execute();
     // Redirect to the appropriate queue
     url::redirect("admin/manage_comments/queue/unpublished");
 }
Example #2
0
 public function index()
 {
     // Get rid of old deleted/spam comments once in a while
     db::build()->delete("comments")->where("state", "IN", array("deleted", "spam"))->where("updated", "<", db::expr("UNIX_TIMESTAMP() - 86400 * 7"))->execute();
     $view = new Admin_View("admin.html");
     $view->content = new View("admin_manage_comments.html");
     $view->content->menu = $this->_menu($this->_counts());
     print $view;
 }
 public function install_creates_root_item_test()
 {
     $max_right_ptr = ORM::factory("item")->select(db::expr("MAX(`right_ptr`) AS `right_ptr`"))->find()->right_ptr;
     $root = ORM::factory('item')->find(1);
     $this->assert_equal("Gallery", $root->title);
     $this->assert_equal(1, $root->left_ptr);
     $this->assert_equal($max_right_ptr, $root->right_ptr);
     $this->assert_equal(0, $root->parent_id);
     $this->assert_equal(1, $root->level);
 }
Example #4
0
 /**
  * Show a list of all available, running and finished tasks.
  */
 public function index()
 {
     $query = db::build()->update("tasks")->set("state", "stalled")->where("done", "=", 0)->where("state", "<>", "stalled")->where(db::expr("UNIX_TIMESTAMP(NOW()) - `updated` > 15"))->execute();
     $stalled_count = $query->count();
     if ($stalled_count) {
         log::warning("tasks", t2("One task is stalled", "%count tasks are stalled", $stalled_count), t('<a href="%url">view</a>', array("url" => html::mark_clean(url::site("admin/maintenance")))));
     }
     $view = new Admin_View("admin.html");
     $view->page_title = t("Maintenance tasks");
     $view->content = new View("admin_maintenance.html");
     $view->content->task_definitions = task::get_definitions();
     $view->content->running_tasks = ORM::factory("task")->where("done", "=", 0)->order_by("updated", "DESC")->find_all();
     $view->content->finished_tasks = ORM::factory("task")->where("done", "=", 1)->order_by("updated", "DESC")->find_all();
     print $view;
     // Do some maintenance while we're in here
     db::build()->delete("caches")->where("expiration", "<>", 0)->where("expiration", "<=", time())->execute();
 }
Example #5
0
 /**
  * Increment the value of a variable for this module
  *
  * Note: Frequently updating counters is very inefficient because it invalidates the cache value
  * which has to be rebuilt every time we make a change.
  *
  * @todo Get rid of this and find an alternate approach for all callers (currently only Akismet)
  *
  * @deprecated
  * @param string $module_name
  * @param string $name
  * @param string $increment (optional, default is 1)
  */
 static function incr_var($module_name, $name, $increment = 1)
 {
     db::build()->update("vars")->set("value", db::expr("`value` + {$increment}"))->where("module_name", "=", $module_name)->where("name", "=", $name)->execute();
     Cache::instance()->delete("var_cache");
     self::$var_cache = null;
 }
Example #6
0
 static function upgrade($version)
 {
     $db = Database::instance();
     if ($version == 1) {
         module::set_var("gallery", "date_format", "Y-M-d");
         module::set_var("gallery", "date_time_format", "Y-M-d H:i:s");
         module::set_var("gallery", "time_format", "H:i:s");
         module::set_version("gallery", $version = 2);
     }
     if ($version == 2) {
         module::set_var("gallery", "show_credits", 1);
         module::set_version("gallery", $version = 3);
     }
     if ($version == 3) {
         $db->query("CREATE TABLE {caches} (\n                 `id` varchar(255) NOT NULL,\n                 `tags` varchar(255),\n                 `expiration` int(9) NOT NULL,\n                 `cache` text,\n                 PRIMARY KEY (`id`),\n                 KEY (`tags`))\n                 DEFAULT CHARSET=utf8;");
         module::set_version("gallery", $version = 4);
     }
     if ($version == 4) {
         Cache::instance()->delete_all();
         $db->query("ALTER TABLE {caches} MODIFY COLUMN `cache` LONGBLOB");
         module::set_version("gallery", $version = 5);
     }
     if ($version == 5) {
         Cache::instance()->delete_all();
         $db->query("ALTER TABLE {caches} DROP COLUMN `id`");
         $db->query("ALTER TABLE {caches} ADD COLUMN `key` varchar(255) NOT NULL");
         $db->query("ALTER TABLE {caches} ADD COLUMN `id` int(9) NOT NULL auto_increment PRIMARY KEY");
         module::set_version("gallery", $version = 6);
     }
     if ($version == 6) {
         module::clear_var("gallery", "version");
         module::set_version("gallery", $version = 7);
     }
     if ($version == 7) {
         $groups = identity::groups();
         $permissions = ORM::factory("permission")->find_all();
         foreach ($groups as $group) {
             foreach ($permissions as $permission) {
                 // Update access intents
                 $db->query("ALTER TABLE {access_intents} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) DEFAULT NULL");
                 // Update access cache
                 if ($permission->name === "view") {
                     $db->query("ALTER TABLE {items} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) DEFAULT FALSE");
                 } else {
                     $db->query("ALTER TABLE {access_caches} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) NOT NULL DEFAULT FALSE");
                 }
             }
         }
         module::set_version("gallery", $version = 8);
     }
     if ($version == 8) {
         $db->query("ALTER TABLE {items} CHANGE COLUMN `left`  `left_ptr`  INT(9) NOT NULL;");
         $db->query("ALTER TABLE {items} CHANGE COLUMN `right` `right_ptr` INT(9) NOT NULL;");
         module::set_version("gallery", $version = 9);
     }
     if ($version == 9) {
         $db->query("ALTER TABLE {items} ADD KEY `weight` (`weight` DESC);");
         module::set_version("gallery", $version = 10);
     }
     if ($version == 10) {
         module::set_var("gallery", "image_sharpen", 15);
         module::set_version("gallery", $version = 11);
     }
     if ($version == 11) {
         $db->query("ALTER TABLE {items} ADD COLUMN `relative_url_cache` varchar(255) DEFAULT NULL");
         $db->query("ALTER TABLE {items} ADD COLUMN `slug` varchar(255) DEFAULT NULL");
         // This is imperfect since some of the slugs may contain invalid characters, but it'll do
         // for now because we don't want a lengthy operation here.
         $db->query("UPDATE {items} SET `slug` = `name`");
         // Flush all path caches because we're going to start urlencoding them.
         $db->query("UPDATE {items} SET `relative_url_cache` = NULL, `relative_path_cache` = NULL");
         module::set_version("gallery", $version = 12);
     }
     if ($version == 12) {
         if (module::get_var("gallery", "active_site_theme") == "default") {
             module::set_var("gallery", "active_site_theme", "wind");
         }
         if (module::get_var("gallery", "active_admin_theme") == "admin_default") {
             module::set_var("gallery", "active_admin_theme", "admin_wind");
         }
         module::set_version("gallery", $version = 13);
     }
     if ($version == 13) {
         // Add rules for generating our thumbnails and resizes
         Database::instance()->query("UPDATE {graphics_rules} SET `operation` = CONCAT('gallery_graphics::', `operation`);");
         module::set_version("gallery", $version = 14);
     }
     if ($version == 14) {
         $sidebar_blocks = block_manager::get_active("site_sidebar");
         if (empty($sidebar_blocks)) {
             $available_blocks = block_manager::get_available_site_blocks();
             foreach (array_keys(block_manager::get_available_site_blocks()) as $id) {
                 $sidebar_blocks[] = explode(":", $id);
             }
             block_manager::set_active("site_sidebar", $sidebar_blocks);
         }
         module::set_version("gallery", $version = 15);
     }
     if ($version == 15) {
         module::set_var("gallery", "identity_provider", "user");
         module::set_version("gallery", $version = 16);
     }
     // Convert block keys to an md5 hash of the module and block name
     if ($version == 16) {
         foreach (array("dashboard_sidebar", "dashboard_center", "site_sidebar") as $location) {
             $blocks = block_manager::get_active($location);
             $new_blocks = array();
             foreach ($blocks as $block) {
                 $new_blocks[md5("{$block[0]}:{$block[1]}")] = $block;
             }
             block_manager::set_active($location, $new_blocks);
         }
         module::set_version("gallery", $version = 17);
     }
     // We didn't like md5 hashes so convert block keys back to random keys to allow duplicates.
     if ($version == 17) {
         foreach (array("dashboard_sidebar", "dashboard_center", "site_sidebar") as $location) {
             $blocks = block_manager::get_active($location);
             $new_blocks = array();
             foreach ($blocks as $block) {
                 $new_blocks[random::int()] = $block;
             }
             block_manager::set_active($location, $new_blocks);
         }
         module::set_version("gallery", $version = 18);
     }
     // Rename blocks_site.sidebar to blocks_site_sidebar
     if ($version == 18) {
         $blocks = block_manager::get_active("site.sidebar");
         block_manager::set_active("site_sidebar", $blocks);
         module::clear_var("gallery", "blocks_site.sidebar");
         module::set_version("gallery", $version = 19);
     }
     // Set a default for the number of simultaneous uploads
     // Version 20 was reverted in 57adefc5baa7a2b0dfcd3e736e80c2fa86d3bfa2, so skip it.
     if ($version == 19 || $version == 20) {
         module::set_var("gallery", "simultaneous_upload_limit", 5);
         module::set_version("gallery", $version = 21);
     }
     // Update the graphics rules table so that the maximum height for resizes is 640 not 480.
     // Fixes ticket #671
     if ($version == 21) {
         $resize_rule = ORM::factory("graphics_rule")->where("id", "=", "2")->find();
         // make sure it hasn't been changed already
         $args = unserialize($resize_rule->args);
         if ($args["height"] == 480 && $args["width"] == 640) {
             $args["height"] = 640;
             $resize_rule->args = serialize($args);
             $resize_rule->save();
         }
         module::set_version("gallery", $version = 22);
     }
     // Update slug values to be legal.  We should have done this in the 11->12 upgrader, but I was
     // lazy.  Mea culpa!
     if ($version == 22) {
         foreach (db::build()->from("items")->select("id", "slug")->where(db::expr("`slug` REGEXP '[^_A-Za-z0-9-]'"), "=", 1)->execute() as $row) {
             $new_slug = item::convert_filename_to_slug($row->slug);
             if (empty($new_slug)) {
                 $new_slug = random::int();
             }
             db::build()->update("items")->set("slug", $new_slug)->set("relative_url_cache", null)->where("id", "=", $row->id)->execute();
         }
         module::set_version("gallery", $version = 23);
     }
     if ($version == 23) {
         $db->query("CREATE TABLE {failed_logins} (\n                  `id` int(9) NOT NULL auto_increment,\n                  `count` int(9) NOT NULL,\n                  `name` varchar(255) NOT NULL,\n                  `time` int(9) NOT NULL,\n                  PRIMARY KEY (`id`))\n                  DEFAULT CHARSET=utf8;");
         module::set_version("gallery", $version = 24);
     }
     if ($version == 24) {
         foreach (array("logs", "tmp", "uploads") as $dir) {
             self::_protect_directory(VARPATH . $dir);
         }
         module::set_version("gallery", $version = 25);
     }
     if ($version == 25) {
         db::build()->update("items")->set("title", db::expr("`name`"))->and_open()->where("title", "IS", null)->or_where("title", "=", "")->close()->execute();
         module::set_version("gallery", $version = 26);
     }
     if ($version == 26) {
         if (in_array("failed_logins", Database::instance()->list_tables())) {
             $db->query("RENAME TABLE {failed_logins} TO {failed_auths}");
         }
         module::set_version("gallery", $version = 27);
     }
     if ($version == 27) {
         // Set the admin area timeout to 90 minutes
         module::set_var("gallery", "admin_area_timeout", 90 * 60);
         module::set_version("gallery", $version = 28);
     }
     if ($version == 28) {
         module::set_var("gallery", "credits", "Powered by <a href=\"%url\">%gallery_version</a>");
         module::set_version("gallery", $version = 29);
     }
     if ($version == 29) {
         $db->query("ALTER TABLE {caches} ADD KEY (`key`);");
         module::set_version("gallery", $version = 30);
     }
     if ($version == 30) {
         module::set_var("gallery", "maintenance_mode", 0);
         module::set_version("gallery", $version = 31);
     }
     if ($version == 31) {
         $db->query("ALTER TABLE {modules} ADD COLUMN `weight` int(9) DEFAULT NULL");
         $db->query("ALTER TABLE {modules} ADD KEY (`weight`)");
         db::update("modules")->set("weight", db::expr("`id`"))->execute();
         module::set_version("gallery", $version = 32);
     }
     if ($version == 32) {
         $db->query("ALTER TABLE {items} ADD KEY (`left_ptr`)");
         module::set_version("gallery", $version = 33);
     }
     if ($version == 33) {
         $db->query("ALTER TABLE {access_caches} ADD KEY (`item_id`)");
         module::set_version("gallery", $version = 34);
     }
     if ($version == 34) {
         module::set_var("gallery", "visible_title_length", 15);
         module::set_version("gallery", $version = 35);
     }
     if ($version == 35) {
         module::set_var("gallery", "favicon_url", "lib/images/favicon.ico");
         module::set_version("gallery", $version = 36);
     }
     if ($version == 36) {
         module::set_var("gallery", "email_from", "*****@*****.**");
         module::set_var("gallery", "email_reply_to", "*****@*****.**");
         module::set_var("gallery", "email_line_length", 70);
         module::set_var("gallery", "email_header_separator", serialize("\n"));
         module::set_version("gallery", $version = 37);
     }
     // Changed our minds and decided that the initial value should be empty
     // But don't just reset it blindly, only do it if the value is version 37 default
     if ($version == 37) {
         $email = module::get_var("gallery", "email_from", "");
         if ($email == "*****@*****.**") {
             module::set_var("gallery", "email_from", "");
         }
         $email = module::get_var("gallery", "email_reply_to", "");
         if ($email == "*****@*****.**") {
             module::set_var("gallery", "email_reply_to", "");
         }
         module::set_version("gallery", $version = 38);
     }
     if ($version == 38) {
         module::set_var("gallery", "show_user_profiles_to", "registered_users");
         module::set_version("gallery", $version = 39);
     }
     if ($version == 39) {
         module::set_var("gallery", "extra_binary_paths", "/usr/local/bin:/opt/local/bin:/opt/bin");
         module::set_version("gallery", $version = 40);
     }
     if ($version == 40) {
         module::clear_var("gallery", "_cache");
         module::set_version("gallery", $version = 41);
     }
     if ($version == 41) {
         $db->query("TRUNCATE TABLE {caches}");
         $db->query("ALTER TABLE {caches} DROP INDEX `key`, ADD UNIQUE `key` (`key`)");
         module::set_version("gallery", $version = 42);
     }
     if ($version == 42) {
         $db->query("ALTER TABLE {items} CHANGE `description` `description` text DEFAULT NULL");
         module::set_version("gallery", $version = 43);
     }
     if ($version == 43) {
         $db->query("ALTER TABLE {items} CHANGE `rand_key` `rand_key` DECIMAL(11, 10)");
         module::set_version("gallery", $version = 44);
     }
     if ($version == 44) {
         $db->query("ALTER TABLE {messages} CHANGE `value` `value` text default NULL");
         module::set_version("gallery", $version = 45);
     }
     if ($version == 45) {
         // Splice the upgrade_checker block into the admin dashboard at the top
         // of the page, but under the welcome block if it's in the first position.
         $blocks = block_manager::get_active("dashboard_center");
         $index = count($blocks) && current($blocks) == array("gallery", "welcome") ? 1 : 0;
         array_splice($blocks, $index, 0, array(random::int() => array("gallery", "upgrade_checker")));
         block_manager::set_active("dashboard_center", $blocks);
         module::set_var("gallery", "upgrade_checker_auto_enabled", true);
         module::set_version("gallery", $version = 46);
     }
     if ($version == 46) {
         module::set_var("gallery", "apple_touch_icon_url", "lib/images/apple-touch-icon.png");
         module::set_version("gallery", $version = 47);
     }
     if ($version == 47 || $version == 48) {
         // Add configuration variable to set timezone.  Defaults to the currently
         // used timezone (from PHP configuration).  Note that in v48 we were
         // setting this value incorrectly, so we're going to stomp this value for v49.
         module::set_var("gallery", "timezone", null);
         module::set_version("gallery", $version = 49);
     }
     if ($version == 49) {
         // In v49 we changed the Item_Model validation code to disallow files with two dots in them,
         // but we didn't rename any files which fail to validate, so as soon as you do anything to
         // change those files (eg. as a side effect of getting the url or file path) it fails to
         // validate.  Fix those here.  This might be slow, but if it times out it can just pick up
         // where it left off.
         foreach (db::build()->from("items")->select("id")->where("type", "<>", "album")->where(db::expr("`name` REGEXP '\\\\..*\\\\.'"), "=", 1)->order_by("id", "asc")->execute() as $row) {
             set_time_limit(30);
             $item = ORM::factory("item", $row->id);
             $item->name = legal_file::smash_extensions($item->name);
             $item->save();
         }
         module::set_version("gallery", $version = 50);
     }
     if ($version == 50) {
         // In v51, we added a lock_timeout variable so that administrators could edit the time out
         // from 1 second to a higher variable if their system runs concurrent parallel uploads for
         // instance.
         module::set_var("gallery", "lock_timeout", 1);
         module::set_version("gallery", $version = 51);
     }
     if ($version == 51) {
         // In v52, we added functions to the legal_file helper that map photo and movie file
         // extensions to their mime types (and allow extension of the list by other modules).  During
         // this process, we correctly mapped m4v files to video/x-m4v, correcting a previous error
         // where they were mapped to video/mp4.  This corrects the existing items.
         db::build()->update("items")->set("mime_type", "video/x-m4v")->where("name", "REGEXP", "\\.m4v\$")->execute();
         module::set_version("gallery", $version = 52);
     }
     if ($version == 52) {
         // In v53, we added the ability to change the default time used when extracting frames from
         // movies.  Previously we hard-coded this at 3 seconds, so we use that as the default.
         module::set_var("gallery", "movie_extract_frame_time", 3);
         module::set_version("gallery", $version = 53);
     }
     if ($version == 53) {
         // In v54, we changed how we check for name and slug conflicts in Item_Model.  Previously,
         // we checked the whole filename.  As a result, "foo.jpg" and "foo.png" were not considered
         // conflicting if their slugs were different (a rare case in practice since server_add and
         // uploader would give them both the same slug "foo").  Now, we check the filename without its
         // extension.  This upgrade stanza fixes any conflicts where they were previously allowed.
         // This might be slow, but if it times out it can just pick up where it left off.
         // Find and loop through each conflict (e.g. "foo.jpg", "foo.png", and "foo.flv" are one
         // conflict; "bar.jpg", "bar.png", and "bar.flv" are another)
         foreach (db::build()->select_distinct(array("parent_base_name" => db::expr("CONCAT(`parent_id`, ':', LOWER(SUBSTR(`name`, 1, LOCATE('.', `name`) - 1)))")))->select(array("C" => "COUNT(\"*\")"))->from("items")->where("type", "<>", "album")->having("C", ">", 1)->group_by("parent_base_name")->execute() as $conflict) {
             list($parent_id, $base_name) = explode(":", $conflict->parent_base_name, 2);
             $base_name_escaped = Database::escape_for_like($base_name);
             // Loop through the items for each conflict
             foreach (db::build()->from("items")->select("id")->where("type", "<>", "album")->where("parent_id", "=", $parent_id)->where("name", "LIKE", "{$base_name_escaped}.%")->limit(1000000)->offset(1)->execute() as $row) {
                 set_time_limit(30);
                 $item = ORM::factory("item", $row->id);
                 $item->name = $item->name;
                 // this will force Item_Model to check for conflicts on save
                 $item->save();
             }
         }
         module::set_version("gallery", $version = 54);
     }
     if ($version == 54) {
         $db->query("ALTER TABLE {items} ADD KEY `relative_path_cache` (`relative_path_cache`)");
         module::set_version("gallery", $version = 55);
     }
     if ($version == 55) {
         // In v56, we added the ability to change the default behavior regarding movie uploads.  It
         // can be set to "always", "never", or "autodetect" to match the previous behavior where they
         // are allowed only if FFmpeg is found.
         module::set_var("gallery", "movie_allow_uploads", "autodetect");
         module::set_version("gallery", $version = 56);
     }
     if ($version == 56) {
         // Cleanup possible instances where resize_dirty of albums or movies was set to 0.  This is
         // unlikely to have occurred, and doesn't currently matter much since albums and movies don't
         // have resize images anyway.  However, it may be useful to be consistent here going forward.
         db::build()->update("items")->set("resize_dirty", 1)->where("type", "<>", "photo")->execute();
         module::set_version("gallery", $version = 57);
     }
     if ($version == 57) {
         // In v58 we changed the Item_Model validation code to disallow files or directories with
         // backslashes in them, and we need to fix any existing items that have them.  This is
         // pretty unlikely, as having backslashes would have probably already caused other issues for
         // users, but we should check anyway.  This might be slow, but if it times out it can just
         // pick up where it left off.
         foreach (db::build()->from("items")->select("id")->where(db::expr("`name` REGEXP '\\\\\\\\'"), "=", 1)->order_by("id", "asc")->execute() as $row) {
             set_time_limit(30);
             $item = ORM::factory("item", $row->id);
             $item->name = str_replace("\\", "_", $item->name);
             $item->save();
         }
         module::set_version("gallery", $version = 58);
     }
 }
Example #7
0
 static function upgrade($version)
 {
     $db = Database::instance();
     if ($version == 1) {
         module::set_var("gallery", "date_format", "Y-M-d");
         module::set_var("gallery", "date_time_format", "Y-M-d H:i:s");
         module::set_var("gallery", "time_format", "H:i:s");
         module::set_version("gallery", $version = 2);
     }
     if ($version == 2) {
         module::set_var("gallery", "show_credits", 1);
         module::set_version("gallery", $version = 3);
     }
     if ($version == 3) {
         $db->query("CREATE TABLE {caches} (\n                 `id` varchar(255) NOT NULL,\n                 `tags` varchar(255),\n                 `expiration` int(9) NOT NULL,\n                 `cache` text,\n                 PRIMARY KEY (`id`),\n                 KEY (`tags`))\n                 DEFAULT CHARSET=utf8;");
         module::set_version("gallery", $version = 4);
     }
     if ($version == 4) {
         Cache::instance()->delete_all();
         $db->query("ALTER TABLE {caches} MODIFY COLUMN `cache` LONGBLOB");
         module::set_version("gallery", $version = 5);
     }
     if ($version == 5) {
         Cache::instance()->delete_all();
         $db->query("ALTER TABLE {caches} DROP COLUMN `id`");
         $db->query("ALTER TABLE {caches} ADD COLUMN `key` varchar(255) NOT NULL");
         $db->query("ALTER TABLE {caches} ADD COLUMN `id` int(9) NOT NULL auto_increment PRIMARY KEY");
         module::set_version("gallery", $version = 6);
     }
     if ($version == 6) {
         module::clear_var("gallery", "version");
         module::set_version("gallery", $version = 7);
     }
     if ($version == 7) {
         $groups = identity::groups();
         $permissions = ORM::factory("permission")->find_all();
         foreach ($groups as $group) {
             foreach ($permissions as $permission) {
                 // Update access intents
                 $db->query("ALTER TABLE {access_intents} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) DEFAULT NULL");
                 // Update access cache
                 if ($permission->name === "view") {
                     $db->query("ALTER TABLE {items} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) DEFAULT FALSE");
                 } else {
                     $db->query("ALTER TABLE {access_caches} MODIFY COLUMN {$permission->name}_{$group->id} BINARY(1) NOT NULL DEFAULT FALSE");
                 }
             }
         }
         module::set_version("gallery", $version = 8);
     }
     if ($version == 8) {
         $db->query("ALTER TABLE {items} CHANGE COLUMN `left`  `left_ptr`  INT(9) NOT NULL;");
         $db->query("ALTER TABLE {items} CHANGE COLUMN `right` `right_ptr` INT(9) NOT NULL;");
         module::set_version("gallery", $version = 9);
     }
     if ($version == 9) {
         $db->query("ALTER TABLE {items} ADD KEY `weight` (`weight` DESC);");
         module::set_version("gallery", $version = 10);
     }
     if ($version == 10) {
         module::set_var("gallery", "image_sharpen", 15);
         module::set_version("gallery", $version = 11);
     }
     if ($version == 11) {
         $db->query("ALTER TABLE {items} ADD COLUMN `relative_url_cache` varchar(255) DEFAULT NULL");
         $db->query("ALTER TABLE {items} ADD COLUMN `slug` varchar(255) DEFAULT NULL");
         // This is imperfect since some of the slugs may contain invalid characters, but it'll do
         // for now because we don't want a lengthy operation here.
         $db->query("UPDATE {items} SET `slug` = `name`");
         // Flush all path caches becuase we're going to start urlencoding them.
         $db->query("UPDATE {items} SET `relative_url_cache` = NULL, `relative_path_cache` = NULL");
         module::set_version("gallery", $version = 12);
     }
     if ($version == 12) {
         if (module::get_var("gallery", "active_site_theme") == "default") {
             module::set_var("gallery", "active_site_theme", "wind");
         }
         if (module::get_var("gallery", "active_admin_theme") == "admin_default") {
             module::set_var("gallery", "active_admin_theme", "admin_wind");
         }
         module::set_version("gallery", $version = 13);
     }
     if ($version == 13) {
         // Add rules for generating our thumbnails and resizes
         Database::instance()->query("UPDATE {graphics_rules} SET `operation` = CONCAT('gallery_graphics::', `operation`);");
         module::set_version("gallery", $version = 14);
     }
     if ($version == 14) {
         $sidebar_blocks = block_manager::get_active("site_sidebar");
         if (empty($sidebar_blocks)) {
             $available_blocks = block_manager::get_available_site_blocks();
             foreach (array_keys(block_manager::get_available_site_blocks()) as $id) {
                 $sidebar_blocks[] = explode(":", $id);
             }
             block_manager::set_active("site_sidebar", $sidebar_blocks);
         }
         module::set_version("gallery", $version = 15);
     }
     if ($version == 15) {
         module::set_var("gallery", "identity_provider", "user");
         module::set_version("gallery", $version = 16);
     }
     // Convert block keys to an md5 hash of the module and block name
     if ($version == 16) {
         foreach (array("dashboard_sidebar", "dashboard_center", "site_sidebar") as $location) {
             $blocks = block_manager::get_active($location);
             $new_blocks = array();
             foreach ($blocks as $block) {
                 $new_blocks[md5("{$block[0]}:{$block[1]}")] = $block;
             }
             block_manager::set_active($location, $new_blocks);
         }
         module::set_version("gallery", $version = 17);
     }
     // We didn't like md5 hashes so convert block keys back to random keys to allow duplicates.
     if ($version == 17) {
         foreach (array("dashboard_sidebar", "dashboard_center", "site_sidebar") as $location) {
             $blocks = block_manager::get_active($location);
             $new_blocks = array();
             foreach ($blocks as $block) {
                 $new_blocks[random::int()] = $block;
             }
             block_manager::set_active($location, $new_blocks);
         }
         module::set_version("gallery", $version = 18);
     }
     // Rename blocks_site.sidebar to blocks_site_sidebar
     if ($version == 18) {
         $blocks = block_manager::get_active("site.sidebar");
         block_manager::set_active("site_sidebar", $blocks);
         module::clear_var("gallery", "blocks_site.sidebar");
         module::set_version("gallery", $version = 19);
     }
     // Set a default for the number of simultaneous uploads
     // Version 20 was reverted in 57adefc5baa7a2b0dfcd3e736e80c2fa86d3bfa2, so skip it.
     if ($version == 19 || $version == 20) {
         module::set_var("gallery", "simultaneous_upload_limit", 5);
         module::set_version("gallery", $version = 21);
     }
     // Update the graphics rules table so that the maximum height for resizes is 640 not 480.
     // Fixes ticket #671
     if ($version == 21) {
         $resize_rule = ORM::factory("graphics_rule")->where("id", "=", "2")->find();
         // make sure it hasn't been changed already
         $args = unserialize($resize_rule->args);
         if ($args["height"] == 480 && $args["width"] == 640) {
             $args["height"] = 640;
             $resize_rule->args = serialize($args);
             $resize_rule->save();
         }
         module::set_version("gallery", $version = 22);
     }
     // Update slug values to be legal.  We should have done this in the 11->12 upgrader, but I was
     // lazy.  Mea culpa!
     if ($version == 22) {
         foreach (db::build()->from("items")->select("id", "slug")->where(db::expr("`slug` REGEXP '[^_A-Za-z0-9-]'"), "=", 1)->execute() as $row) {
             $new_slug = item::convert_filename_to_slug($row->slug);
             if (empty($new_slug)) {
                 $new_slug = random::int();
             }
             db::build()->update("items")->set("slug", $new_slug)->set("relative_url_cache", null)->where("id", "=", $row->id)->execute();
         }
         module::set_version("gallery", $version = 23);
     }
     if ($version == 23) {
         $db->query("CREATE TABLE {failed_logins} (\n                  `id` int(9) NOT NULL auto_increment,\n                  `count` int(9) NOT NULL,\n                  `name` varchar(255) NOT NULL,\n                  `time` int(9) NOT NULL,\n                  PRIMARY KEY (`id`))\n                  DEFAULT CHARSET=utf8;");
         module::set_version("gallery", $version = 24);
     }
     if ($version == 24) {
         foreach (array("logs", "tmp", "uploads") as $dir) {
             self::_protect_directory(VARPATH . $dir);
         }
         module::set_version("gallery", $version = 25);
     }
     if ($version == 25) {
         db::build()->update("items")->set("title", db::expr("`name`"))->and_open()->where("title", "IS", null)->or_where("title", "=", "")->close()->execute();
         module::set_version("gallery", $version = 26);
     }
     if ($version == 26) {
         if (in_array("failed_logins", Database::instance()->list_tables())) {
             $db->query("RENAME TABLE {failed_logins} TO {failed_auths}");
         }
         module::set_version("gallery", $version = 27);
     }
     if ($version == 27) {
         // Set the admin area timeout to 90 minutes
         module::set_var("gallery", "admin_area_timeout", 90 * 60);
         module::set_version("gallery", $version = 28);
     }
     if ($version == 28) {
         module::set_var("gallery", "credits", "Powered by <a href=\"%url\">%gallery_version</a>");
         module::set_version("gallery", $version = 29);
     }
     if ($version == 29) {
         $db->query("ALTER TABLE {caches} ADD KEY (`key`);");
         module::set_version("gallery", $version = 30);
     }
     if ($version == 30) {
         module::set_var("gallery", "maintenance_mode", 0);
         module::set_version("gallery", $version = 31);
     }
     if ($version == 31) {
         $db->query("ALTER TABLE {modules} ADD COLUMN `weight` int(9) DEFAULT NULL");
         $db->query("ALTER TABLE {modules} ADD KEY (`weight`)");
         db::update("modules")->set("weight", db::expr("`id`"))->execute();
         module::set_version("gallery", $version = 32);
     }
     if ($version == 32) {
         $db->query("ALTER TABLE {items} ADD KEY (`left_ptr`)");
         module::set_version("gallery", $version = 33);
     }
     if ($version == 33) {
         $db->query("ALTER TABLE {access_caches} ADD KEY (`item_id`)");
         module::set_version("gallery", $version = 34);
     }
     if ($version == 34) {
         module::set_var("gallery", "visible_title_length", 15);
         module::set_version("gallery", $version = 35);
     }
     if ($version == 35) {
         module::set_var("gallery", "favicon_url", "lib/images/favicon.ico");
         module::set_version("gallery", $version = 36);
     }
     if ($version == 36) {
         module::set_var("gallery", "email_from", "*****@*****.**");
         module::set_var("gallery", "email_reply_to", "*****@*****.**");
         module::set_var("gallery", "email_line_length", 70);
         module::set_var("gallery", "email_header_separator", serialize("\n"));
         module::set_version("gallery", $version = 37);
     }
     // Changed our minds and decided that the initial value should be empty
     // But don't just reset it blindly, only do it if the value is version 37 default
     if ($version == 37) {
         $email = module::get_var("gallery", "email_from", "");
         if ($email == "*****@*****.**") {
             module::set_var("gallery", "email_from", "");
         }
         $email = module::get_var("gallery", "email_reply_to", "");
         if ($email == "*****@*****.**") {
             module::set_var("gallery", "email_reply_to", "");
         }
         module::set_version("gallery", $version = 38);
     }
     if ($version == 38) {
         module::set_var("gallery", "show_user_profiles_to", "registered_users");
         module::set_version("gallery", $version = 39);
     }
     if ($version == 39) {
         module::set_var("gallery", "extra_binary_paths", "/usr/local/bin:/opt/local/bin:/opt/bin");
         module::set_version("gallery", $version = 40);
     }
     if ($version == 40) {
         module::clear_var("gallery", "_cache");
         module::set_version("gallery", $version = 41);
     }
     if ($version == 41) {
         $db->query("TRUNCATE TABLE {caches}");
         $db->query("ALTER TABLE {caches} DROP INDEX `key`, ADD UNIQUE `key` (`key`)");
         module::set_version("gallery", $version = 42);
     }
     if ($version == 42) {
         $db->query("ALTER TABLE {items} CHANGE `description` `description` text DEFAULT NULL");
         module::set_version("gallery", $version = 43);
     }
     if ($version == 43) {
         $db->query("ALTER TABLE {items} CHANGE `rand_key` `rand_key` DECIMAL(11, 10)");
         module::set_version("gallery", $version = 44);
     }
     if ($version == 44) {
         $db->query("ALTER TABLE {messages} CHANGE `value` `value` text default NULL");
         module::set_version("gallery", $version = 45);
     }
     if ($version == 45) {
         // Splice the upgrade_checker block into the admin dashboard at the top
         // of the page, but under the welcome block if it's in the first position.
         $blocks = block_manager::get_active("dashboard_center");
         $index = count($blocks) && current($blocks) == array("gallery", "welcome") ? 1 : 0;
         array_splice($blocks, $index, 0, array(random::int() => array("gallery", "upgrade_checker")));
         block_manager::set_active("dashboard_center", $blocks);
         module::set_var("gallery", "upgrade_checker_auto_enabled", true);
         module::set_version("gallery", $version = 46);
     }
 }
Example #8
0
 private function _clean_expired()
 {
     db::build()->delete("digibug_proxies")->where("request_date", "<=", db::expr("(CURDATE() - INTERVAL 90 DAY)"))->limit(20)->execute();
 }
Example #9
0
 function rearrange()
 {
     access::verify_csrf();
     $input = Input::instance();
     $target = ORM::factory("item", $input->post("target_id"));
     if (!$target->loaded()) {
         json::reply(null);
         return;
     }
     $album = $target->parent();
     access::required("edit", $album);
     if ($album->sort_column != "weight") {
         // Force all the weights into the current order before changing the order to manual
         $weight = 0;
         foreach ($album->children() as $child) {
             $child->weight = ++$weight;
             $child->save();
         }
         $album->sort_column = "weight";
         $album->sort_order = "ASC";
         $album->save();
     }
     $source_ids = explode(",", $input->post("source_ids"));
     $base_weight = $target->weight;
     if ($input->post("relative") == "after") {
         $base_weight++;
     }
     if ($source_ids) {
         // Make a hole the right size
         db::build()->update("items")->set("weight", db::expr("`weight` + " . count($source_ids)))->where("parent_id", "=", $album->id)->where("weight", ">=", $base_weight)->execute();
         // Move all the source items to the right spots.
         for ($i = 0; $i < count($source_ids); $i++) {
             $source = ORM::factory("item", $source_ids[$i]);
             if ($source->parent_id = $album->id) {
                 $source->weight = $base_weight + $i;
                 $source->save();
             }
         }
     }
     json::reply(null);
 }
Example #10
0
 /**
  * Move this item to the specified target.
  *
  * @chainable
  * @param   Item_Model $target Target node
  * @return  ORM_MTPP
  */
 protected function move_to($target)
 {
     if ($this->contains($target)) {
         throw new Exception("@todo INVALID_TARGET can't move item inside itself");
     }
     $this->lock();
     $this->reload();
     // Assume that the prior lock holder may have changed this entry
     $target->reload();
     $number_to_move = (int) (($this->right_ptr - $this->left_ptr) / 2 + 1);
     $size_of_hole = $number_to_move * 2;
     $original_left_ptr = $this->left_ptr;
     $original_right_ptr = $this->right_ptr;
     $target_right_ptr = $target->right_ptr;
     $level_delta = $target->level + 1 - $this->level;
     try {
         if ($level_delta) {
             // Update the levels for the to-be-moved items
             db::build()->update($this->table_name)->set("level", db::expr("`level` + {$level_delta}"))->where("left_ptr", ">=", $original_left_ptr)->where("right_ptr", "<=", $original_right_ptr)->execute();
         }
         // Make a hole in the target for the move
         db::build()->update($this->table_name)->set("left_ptr", db::expr("`left_ptr` + {$size_of_hole}"))->where("left_ptr", ">=", $target_right_ptr)->execute();
         db::build()->update($this->table_name)->set("right_ptr", db::expr("`right_ptr` + {$size_of_hole}"))->where("right_ptr", ">=", $target_right_ptr)->execute();
         // Change the parent.
         db::build()->update($this->table_name)->set("parent_id", $target->id)->where("id", "=", $this->id)->execute();
         // If the source is to the right of the target then we just adjusted its left_ptr and
         // right_ptr above.
         $left_ptr = $original_left_ptr;
         $right_ptr = $original_right_ptr;
         if ($original_left_ptr > $target_right_ptr) {
             $left_ptr += $size_of_hole;
             $right_ptr += $size_of_hole;
         }
         $new_offset = $target->right_ptr - $left_ptr;
         db::build()->update($this->table_name)->set("left_ptr", db::expr("`left_ptr` + {$new_offset}"))->set("right_ptr", db::expr("`right_ptr` + {$new_offset}"))->where("left_ptr", ">=", $left_ptr)->where("right_ptr", "<=", $right_ptr)->execute();
         // Close the hole in the source's parent after the move
         db::build()->update($this->table_name)->set("left_ptr", db::expr("`left_ptr` - {$size_of_hole}"))->where("left_ptr", ">", $right_ptr)->execute();
         db::build()->update($this->table_name)->set("right_ptr", db::expr("`right_ptr` - {$size_of_hole}"))->where("right_ptr", ">", $right_ptr)->execute();
     } catch (Exception $e) {
         $this->unlock();
         throw $e;
     }
     $this->unlock();
     // Lets reload to get the changes.
     $this->reload();
     $target->reload();
     return $this;
 }
Example #11
0
 public function find_dirty()
 {
     $gravity = module::get_var('emboss', 'gravity');
     $transparency = module::get_var('emboss', 'transparency');
     $q = db::build()->select()->from('emboss_mappings')->or_where('cur_overlay_id', '!=', db::expr('best_overlay_id'))->or_where('cur_gravity', '!=', $gravity)->or_where('cur_transparency', '!=', $transparency)->execute();
     return $q;
 }
Example #12
0
 /**
  * Delete all tags associated with an item
  */
 static function clear_all($item)
 {
     db::build()->update("tags")->set("count", db::expr("`count` - 1"))->where("count", ">", 0)->where("id", "IN", db::build()->select("tag_id")->from("items_tags")->where("item_id", "=", $item->id))->execute();
     db::build()->delete("items_tags")->where("item_id", "=", $item->id)->execute();
 }
Example #13
0
 static function send_pending_notifications()
 {
     foreach (db::build()->select(db::expr("DISTINCT `email`"))->from("pending_notifications")->execute() as $row) {
         $email = $row->email;
         $result = ORM::factory("pending_notification")->where("email", "=", $email)->find_all();
         if ($result->count() == 1) {
             $pending = $result->current();
             Sendmail::factory()->to($email)->subject($pending->subject)->header("Mime-Version", "1.0")->header("Content-Type", "text/html; charset=UTF-8")->message($pending->body)->send();
             $pending->delete();
         } else {
             $text = "";
             $locale = null;
             foreach ($result as $pending) {
                 $text .= $pending->text;
                 $locale = $pending->locale;
                 $pending->delete();
             }
             Sendmail::factory()->to($email)->subject(t("New activity for %site_name", array("site_name" => item::root()->title, "locale" => $locale)))->header("Mime-Version", "1.0")->header("Content-Type", "text/html; charset=UTF-8")->message($text)->send();
         }
     }
 }
Example #14
0
 static function find_dupe_names()
 {
     return db::build()->select_distinct(array("parent_name" => db::expr("CONCAT(`parent_id`, ':', LOWER(`name`))")))->select("id")->select(array("C" => "COUNT(\"*\")"))->from("items")->where("type", "<>", "album")->having("C", ">", 1)->group_by("parent_name")->execute();
 }
Example #15
0
 static function find_dupe_base_names()
 {
     // looking for photos or movies, not albums
     return db::build()->select_distinct(array("parent_base_name" => db::expr("CONCAT(`parent_id`, ':', LOWER(SUBSTR(`name`, 1, LOCATE('.', `name`) - 1)))")))->select("id")->select(array("C" => "COUNT(\"*\")"))->from("items")->where("type", "<>", "album")->having("C", ">", 1)->group_by("parent_base_name")->execute();
 }