public function index() { // Set up a new admin page for the quotas module. $view = new Admin_View("admin.html"); $view->page_title = t("Users and groups"); $view->page_type = "collection"; $view->page_subtype = "admin_users_quotas"; $view->content = new View("admin_quotas.html"); $page_size = module::get_var("user", "page_size", 10); $page = Input::instance()->get("page", "1"); $builder = db::build(); $user_count = $builder->from("users")->count_records(); $view->page = $page; $view->page_size = $page_size; $view->children_count = $user_count; $view->max_pages = ceil($view->children_count / $view->page_size); $view->content->pager = new Pagination(); $view->content->pager->initialize(array("query_string" => "page", "total_items" => $user_count, "items_per_page" => $page_size, "style" => "classic")); if ($page < 1) { url::redirect(url::merge(array("page" => 1))); } else { if ($page > $view->content->pager->total_pages) { url::redirect(url::merge(array("page" => $view->content->pager->total_pages))); } } $view->content->users = ORM::factory("user")->order_by("users.name", "ASC")->find_all($page_size, $view->content->pager->sql_offset); $view->content->groups = ORM::factory("group")->order_by("name", "ASC")->find_all(); $view->content->quota_options = $this->_get_quota_settings_form(); print $view; }
public function index() { $view = new Admin_View("admin.html"); $view->page_title = t("Users and groups"); $view->page_type = "collection"; $view->page_subtype = "admin_users"; $view->content = new View("admin_users.html"); // @todo: add this as a config option $page_size = module::get_var("user", "page_size", 10); $page = Input::instance()->get("page", "1"); $builder = db::build(); $user_count = $builder->from("users")->count_records(); // Pagination info $view->page = $page; $view->page_size = $page_size; $view->children_count = $user_count; $view->max_pages = ceil($view->children_count / $view->page_size); $view->content->pager = new Pagination(); $view->content->pager->initialize(array("query_string" => "page", "total_items" => $user_count, "items_per_page" => $page_size, "style" => "classic")); // Make sure that the page references a valid offset if ($page < 1) { url::redirect(url::merge(array("page" => 1))); } else { if ($page > $view->content->pager->total_pages) { url::redirect(url::merge(array("page" => $view->content->pager->total_pages))); } } // Join our users against the items table so that we can get a count of their items // in the same query. $view->content->users = ORM::factory("user")->order_by("users.name", "ASC")->find_all($page_size, $view->content->pager->sql_offset); $view->content->groups = ORM::factory("group")->order_by("name", "ASC")->find_all(); print $view; }
static function identity_provider_changed($old_provider, $new_provider) { $admin = identity::admin_user(); db::build()->update("tasks")->set("owner_id", $admin->id)->execute(); db::build()->update("items")->set("owner_id", $admin->id)->execute(); db::build()->update("logs")->set("user_id", $admin->id)->execute(); }
static function stats() { $remaining = db::build()->from("items")->join("search_records", "items.id", "search_records.item_id", "left")->and_open()->where("search_records.item_id", "IS", null)->or_where("search_records.dirty", "=", 1)->close()->count_records(); $total = ORM::factory("item")->count_all(); $percent = round(100 * ($total - $remaining) / $total); return array($remaining, $total, $percent); }
static function available_tasks() { // Delete extra exif_records db::build()->delete("exif_records")->where("item_id", "NOT IN", db::build()->select("id")->from("items")->where("type", "=", "photo"))->execute(); list($remaining, $total, $percent) = exif::stats(); return array(Task_Definition::factory()->callback("exif_task::update_index")->name(t("Extract Exif data"))->description($remaining ? t2("1 photo needs to be scanned", "%count (%percent%) of your photos need to be scanned", $remaining, array("percent" => 100 - $percent)) : t("Exif data is up-to-date"))->severity($remaining ? log::WARNING : log::SUCCESS)); }
static function fix_internet_addresses($task) { $start = microtime(true); $total = $task->get("total"); if (empty($total)) { $task->set("total", $total = db::build()->count_records("items")); $task->set("last_id", 0); $task->set("completed", 0); } $last_id = $task->get("last_id"); $completed = $task->get("completed"); foreach (ORM::factory("item")->where("id", ">", $last_id)->find_all(100) as $item) { $item->slug = item::convert_filename_to_slug($item->slug); $item->save(); $last_id = $item->id; $completed++; if ($completed == $total || microtime(true) - $start > 1.5) { break; } } $task->set("completed", $completed); $task->set("last_id", $last_id); if ($total == $completed) { $task->done = true; $task->state = "success"; $task->percent_complete = 100; db::build()->update("items")->set("relative_path_cache", null)->set("relative_url_cache", null)->execute(); } else { $task->percent_complete = round(100 * $completed / $total); } $task->status = t2("One row updated", "%count / %total rows updated", $completed, array("total" => $total)); }
private function get_uas_and_ips() { $uas = array(); $ips = array(); $sample_size = 0; $d = new Session_Database_Driver(); foreach (db::build()->select("session_id")->from("sessions")->execute() as $r) { $data = $this->unserialize_session($d->read($r->session_id)); $ua = $data["user_agent"]; $ip = $data["ip_address"]; if (!isset($uas[$ua])) { $uas[$ua] = 0; } if (!isset($ips[$ip])) { $ips[$ip] = 0; } $uas[$ua]++; $ips[$ip]++; // Limit the sample size once we've found N user agents if (++$sample_size == 5000) { break; } } arsort($uas); arsort($ips); // Top N only array_splice($uas, 15); array_splice($ips, 15); return array($uas, $ips, $sample_size); }
function rearrange($target_id, $before_or_after) { access::verify_csrf(); $target = ORM::factory("item", $target_id); $album = $target->parent(); access::required("view", $album); access::required("edit", $album); $source_ids = Input::instance()->post("source_ids", array()); if ($album->sort_column != "weight") { $i = 0; foreach ($album->children() as $child) { // Do this directly in the database to avoid sending notifications db::build()->update("items")->set("weight", ++$i)->where("id", "=", $child->id)->execute(); } $album->sort_column = "weight"; $album->sort_order = "ASC"; $album->save(); $target->reload(); } // Find the insertion point $target_weight = $target->weight; if ($before_or_after == "after") { $target_weight++; } // Make a hole $count = count($source_ids); db::build()->update("items")->set("weight", new Database_Expression("`weight` + {$count}"))->where("weight", ">=", $target_weight)->where("parent_id", "=", $album->id)->execute(); // Insert source items into the hole foreach ($source_ids as $source_id) { db::build()->update("items")->set("weight", $target_weight++)->where("id", "=", $source_id)->execute(); } module::event("album_rearrange", $album); print json_encode(array("grid" => self::_get_micro_thumb_grid($album, 0)->__toString(), "sort_column" => $album->sort_column, "sort_order" => $album->sort_order)); }
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("dashboard_sidebar", "dashboard_center", "site_sidebar") as $key) { $blocks = array(); foreach (unserialize(module::get_var("gallery", "blocks_{$key}")) as $rnd => $value) { $blocks[++$i] = $value; } module::set_var("gallery", "blocks_{$key}", serialize($blocks)); } Database::instance()->query("TRUNCATE {caches}"); Database::instance()->query("TRUNCATE {sessions}"); Database::instance()->query("TRUNCATE {logs}"); db::build()->update("users")->set(array("password" => ""))->where("id", "in", array(1, 2))->execute(); $dbconfig = Kohana::config('database.default'); $conn = $dbconfig["connection"]; $sql_file = DOCROOT . "installer/install.sql"; if (!is_writable($sql_file)) { print "{$sql_file} is not writeable"; return; } $command = sprintf("mysqldump --compact --skip-extended-insert --add-drop-table %s %s %s %s > {$sql_file}", escapeshellarg("-h{$conn['host']}"), escapeshellarg("-u{$conn['user']}"), $conn['pass'] ? escapeshellarg("-p{$conn['pass']}") : "", escapeshellarg($conn['database'])); exec($command, $output, $status); if ($status) { print "<pre>"; 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; $table_name = ""; foreach (file($sql_file) as $line) { // Prefix tables $line = preg_replace("/(CREATE TABLE|IF EXISTS|INSERT INTO) `{$dbconfig['table_prefix']}(\\w+)`/", "\\1 {\\2}", $line); if (preg_match("/CREATE TABLE {(\\w+)}/", $line, $matches)) { $table_name = $matches[1]; } // Normalize dates $line = preg_replace("/,{$root_created_timestamp},/", ",UNIX_TIMESTAMP(),", $line); $line = preg_replace("/,{$root_updated_timestamp},/", ",UNIX_TIMESTAMP(),", $line); // Remove ENGINE= specifications execpt for search records, it always needs to be MyISAM if ($table_name != "search_records") { $line = preg_replace("/ENGINE=\\S+ /", "", $line); } // Null out ids in the vars table since it's an auto_increment table and this will result in // more stable values so we'll have less churn in install.sql. $line = preg_replace("/^INSERT INTO {vars} VALUES \\(\\d+/", "INSERT INTO {vars} VALUES (NULL", $line); $buf .= $line; } $fd = fopen($sql_file, "wb"); fwrite($fd, $buf); fclose($fd); }
public function update() { //Get the ordered list of modules $modulerawlist = explode("&", trim($_POST['modulelist'], "&")); //Make sure that gallery and user modules are first in the list $current_weight = 2; $identity_provider = module::get_var("gallery", "identity_provider"); foreach ($modulerawlist as $row) { $currentry = explode("=", $row); $currentry = explode(":", $currentry[1]); if ($currentry[0] == "gallery") { $modulelist[0] = $row; } elseif ($currentry[0] == $identity_provider) { $modulelist[1] = $row; } else { $modulelist[$current_weight] = $row; $current_weight++; } } ksort($modulelist); //Write the correct weight values $current_weight = 0; foreach ($modulelist as $row) { $current_weight++; $currentry = explode("=", $row); $currentry = explode(":", $currentry[1]); db::build()->update("modules")->set("weight", $current_weight)->where("id", "=", $currentry[1])->execute(); } message::success(t("Your settings have been saved.")); url::redirect("admin/moduleorder"); print $this->_get_view(); }
private static function _get_modules() { $modules = array(); foreach (db::build()->select("*")->from("modules")->order_by("weight")->execute() as $row) { $modules["{$row->name}:{$row->id}"] = $row->name; } return $modules; }
static function item_deleted($item) { // If an album is deleted, remove any associated passwords. $existingPasswords = ORM::factory("items_albumpassword")->where("album_id", "=", $item->id)->find_all(); if (count($existingPasswords) > 0) { db::build()->delete("items_albumpassword")->where("album_id", "=", $item->id)->execute(); } }
static function available_tasks() { // Automatically delete extra exif_coordinates whenever the maintance screen is loaded. db::build()->delete("exif_coordinates")->where("item_id", "NOT IN", db::build()->select("id")->from("items"))->execute(); // Display an option on the maintance screen for scanning existing photos // for GPS data (in case photos were uploaded before the module was active). return array(Task_Definition::factory()->callback("exif_gps_task::update_gps_index")->name(t("Extract Exif GPS data"))->description(t("Scan all photos for missing GPS data"))->severity(log::SUCCESS)); }
private function _remove_tag($tag, $delete) { $name = $tag->name; db::build()->delete("items_tags")->where("tag_id", "=", $tag->id)->execute(); if ($delete) { $tag->delete(); } }
static function user_before_delete($user) { try { db::build()->delete("subscriptions")->where("user_id", "=", $user->id)->execute(); } catch (Exception $e) { Kohana_Log::add("error", "@todo notification_event::user_before_delete() failed"); Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString()); } }
public function cache_get_test() { $id = random::hash(); db::build()->insert("caches")->columns("key", "tags", "expiration", "cache")->values($id, "<tag1>, <tag2>", 84600 + time(), serialize("some test data"))->execute(); $data = $this->_driver->get(array($id)); $this->assert_equal("some test data", $data, "cached data should match"); $data = $this->_driver->get(array("")); $this->assert_equal(null, $data, "cached data should not be found"); }
static function identity_provider_changed($old_provider, $new_provider) { $admin = identity::admin_user(); db::build()->update("tasks")->set("owner_id", $admin->id)->execute(); db::build()->update("items")->set("owner_id", $admin->id)->execute(); db::build()->update("logs")->set("user_id", $admin->id)->execute(); module::set_var("gallery", "email_from", $admin->email); module::set_var("gallery", "email_reply_to", $admin->email); }
public function delete_gps($tag_id) { // Delete the GSP data associated with a tag. // Delete the record. db::build()->delete("tags_gpses")->where("tag_id", "=", $tag_id)->execute(); // Redirect back to the main screen and display a "success" message. message::success(t("Your Settings Have Been Saved.")); url::redirect("admin/tagsmap"); }
/** * Garbage collect * * @static */ public static function gc() { static $collected = false; // Remove users idle for over 15 minutes if (!$collected) { $collected = true; db::build()->delete('online_users', array(array('last_activity', '<', time() - 60 * 15)))->execute(); } }
static function item_deleted($item) { if ($item->is_movie()) { transcode::log("Deleting transcoded files for item " . $item->id); if (is_dir(VARPATH . "modules/transcode/flv/" . $item->id)) { self::rrmdir(VARPATH . "modules/transcode/flv/" . $item->id); } db::build()->delete("transcode_resolutions")->where("item_id", "=", $item->id)->execute(); } }
static function upgrade($version) { if ($version == 1) { module::set_var("user", "mininum_password_length", 5); module::set_version("user", $version = 2); } if ($version == 2) { db::build()->update("users")->set("email", "*****@*****.**")->where("guest", "=", 0)->and_open()->where("email", "IS", null)->or_where("email", "=", "")->close()->execute(); module::set_version("user", $version = 3); } }
private function _counts() { $counts->unpublished = 0; $counts->published = 0; $counts->spam = 0; $counts->deleted = 0; foreach (db::build()->select("state")->select(array("c" => 'COUNT("*")'))->from("comments")->group_by("state")->execute() as $row) { $counts->{$row->state} = $row->c; } return $counts; }
static function item_deleted($item) { // Check for and delete existing Faces and Notes. $existingFaces = ORM::factory("items_face")->where("item_id", "=", $item->id)->find_all(); if (count($existingFaces) > 0) { db::build()->delete("items_faces")->where("item_id", "=", $item->id)->execute(); } $existingNotes = ORM::factory("items_note")->where("item_id", "=", $item->id)->find_all(); if (count($existingNotes) > 0) { db::build()->delete("items_notes")->where("item_id", "=", $item->id)->execute(); } }
static function item_deleted($item) { if (!$item->is_photo()) { return; } $name = $item->name; $id = $item->id; $path = $item->file_path(); $orig = str_replace(VARPATH . 'albums/', VARPATH . 'originals/', $path); @unlink($orig); db::build()->from('emboss_mappings')->where('image_id', '=', $id)->delete()->execute(); log::info('emboss', "item_deleted: {$name}"); }
public function updating_view_count_only_doesnt_change_updated_date_test() { $item = test::random_photo(); $item->reload(); $this->assert_equal(0, $item->view_count); // Force the updated date to something well known db::build()->update("items")->set("updated", 0)->where("id", "=", $item->id)->execute(); $item->reload(); $item->view_count++; $item->save(); $this->assert_same(1, $item->view_count); $this->assert_true(empty($item->updated)); }
static function sidebar_bottom($theme) { $item = $theme->item(); if ($item && $item->is_photo()) { $record = db::build()->select("key_count")->from("exif_records")->where("item_id", "=", $item->id)->execute()->current(); if ($record && $record->key_count) { $view = new View("exif_sidebar.html"); $view->item = $item; return $view; } } return null; }
static function item_edit_form_completed($item, $form) { // Update the database with any changes to the item_links field. $record = ORM::factory("item_link")->where("item_id", "=", $item->id)->find(); if ($form->edit_item->item_links_url->value != "") { if (!$record->loaded()) { $record->item_id = $item->id; } $record->url = $form->edit_item->item_links_url->value; $record->save(); } else { db::build()->delete("item_links")->where("item_id", "=", $item->id)->execute(); } }
/** * Overload ORM::delete() to trigger an item_related_update event for all items that are * related to this tag. */ public function delete() { $related_item_ids = array(); foreach (db::build()->select("item_id")->from("items_tags")->where("tag_id", "=", $this->id)->execute() as $row) { $related_item_ids[$row->item_id] = 1; } $result = parent::delete(); if ($related_item_ids) { foreach (ORM::factory("item")->where("id", "IN", array_keys($related_item_ids))->find_all() as $item) { module::event("item_related_update", $item); } } return $result; }
public function doClear($id) { $photo = ORM::factory("item", $id); $rateid = "rate" . $id; $ratable = db::build()->select("id")->from("ratables")->where("ratableKey", "=", $rateid)->execute()->current(); if (db::build()->select("id")->from("ratings")->where("ratable_id", "=", $ratable->id)->execute()->count() < 1) { message::warning(t("No votes have been registered for this item: Nothing cleared!")); json::reply(array("result" => "success", "location" => $photo->url())); return; } $ratings = db::build()->delete("ratings")->where("ratable_id", "=", $ratable->id)->execute(); message::success(t("All ratings and votes for this item have been cleared!")); json::reply(array("result" => "success", "location" => $photo->url())); }
/** * 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(new Database_Expression("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_schedule.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(); $view->content->schedule_definitions = scheduler::get_definitions(); print $view; }