summaryrefslogtreecommitdiff
path: root/modules/gallery/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/controllers')
-rw-r--r--modules/gallery/controllers/admin.php52
-rw-r--r--modules/gallery/controllers/admin_advanced_settings.php53
-rw-r--r--modules/gallery/controllers/admin_dashboard.php93
-rw-r--r--modules/gallery/controllers/admin_graphics.php63
-rw-r--r--modules/gallery/controllers/admin_languages.php136
-rw-r--r--modules/gallery/controllers/admin_maintenance.php181
-rw-r--r--modules/gallery/controllers/admin_modules.php65
-rw-r--r--modules/gallery/controllers/admin_theme_details.php67
-rw-r--r--modules/gallery/controllers/admin_themes.php79
-rw-r--r--modules/gallery/controllers/after_install.php30
-rw-r--r--modules/gallery/controllers/albums.php229
-rw-r--r--modules/gallery/controllers/file_proxy.php120
-rw-r--r--modules/gallery/controllers/items.php30
-rw-r--r--modules/gallery/controllers/l10n_client.php128
-rw-r--r--modules/gallery/controllers/maintenance.php24
-rw-r--r--modules/gallery/controllers/move.php64
-rw-r--r--modules/gallery/controllers/movies.php114
-rw-r--r--modules/gallery/controllers/permissions.php80
-rw-r--r--modules/gallery/controllers/photos.php116
-rw-r--r--modules/gallery/controllers/quick.php122
-rw-r--r--modules/gallery/controllers/rest.php183
-rw-r--r--modules/gallery/controllers/scaffold.php437
-rw-r--r--modules/gallery/controllers/simple_uploader.php86
23 files changed, 2552 insertions, 0 deletions
diff --git a/modules/gallery/controllers/admin.php b/modules/gallery/controllers/admin.php
new file mode 100644
index 00000000..af0f387a
--- /dev/null
+++ b/modules/gallery/controllers/admin.php
@@ -0,0 +1,52 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Controller extends Controller {
+ private $theme;
+
+ public function __construct($theme=null) {
+ if (!(user::active()->admin)) {
+ throw new Exception("@todo UNAUTHORIZED", 401);
+ }
+ parent::__construct();
+ }
+
+ public function __call($controller_name, $args) {
+ if (request::method() == "post") {
+ access::verify_csrf();
+ }
+
+ if ($controller_name == "index") {
+ $controller_name = "dashboard";
+ }
+ $controller_name = "Admin_{$controller_name}_Controller";
+ if ($args) {
+ $method = array_shift($args);
+ } else {
+ $method = "index";
+ }
+
+ if (!method_exists($controller_name, $method)) {
+ return kohana::show_404();
+ }
+
+ call_user_func_array(array(new $controller_name, $method), $args);
+ }
+}
+
diff --git a/modules/gallery/controllers/admin_advanced_settings.php b/modules/gallery/controllers/admin_advanced_settings.php
new file mode 100644
index 00000000..79bc1183
--- /dev/null
+++ b/modules/gallery/controllers/admin_advanced_settings.php
@@ -0,0 +1,53 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Advanced_Settings_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_advanced_settings.html");
+ $view->content->vars = ORM::factory("var")
+ ->orderby("module_name", "name")
+ ->find_all();
+ print $view;
+ }
+
+ public function edit($module_name, $var_name) {
+ $value = module::get_var($module_name, $var_name);
+ $form = new Forge("admin/advanced_settings/save/$module_name/$var_name", "", "post");
+ $group = $form->group("edit_var")->label(
+ t("Edit %var (%module_name)",
+ array("module_name" => $module_name, "var" => $var_name)));
+ $group->input("module_name")->label(t("Module"))->value($module_name)->disabled(1);
+ $group->input("var_name")->label(t("Setting"))->value($var_name)->disabled(1);
+ $group->textarea("value")->label(t("Value"))->value($value);
+ $group->submit("")->value(t("Save"));
+ print $form;
+ }
+
+ public function save($module_name, $var_name) {
+ access::verify_csrf();
+
+ module::set_var($module_name, $var_name, Input::instance()->post("value"));
+ message::success(
+ t("Saved value for %var (%module_name)",
+ array("var" => $var_name, "module_name" => $module_name)));
+
+ print json_encode(array("result" => "success"));
+ }
+}
diff --git a/modules/gallery/controllers/admin_dashboard.php b/modules/gallery/controllers/admin_dashboard.php
new file mode 100644
index 00000000..a1090a6d
--- /dev/null
+++ b/modules/gallery/controllers/admin_dashboard.php
@@ -0,0 +1,93 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Dashboard_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_dashboard.html");
+ $view->content->blocks = block_manager::get_html("dashboard_center");
+ $view->sidebar = "<div id=\"gAdminDashboardSidebar\">" .
+ block_manager::get_html("dashboard_sidebar") .
+ "</div>";
+ print $view;
+ }
+
+ public function add_block() {
+ $form = gallery_block::get_add_block_form();
+ if ($form->validate()) {
+ list ($module_name, $id) = explode(":", $form->add_block->id->value);
+ $available = block_manager::get_available();
+
+ if ($form->add_block->center->value) {
+ block_manager::add("dashboard_center", $module_name, $id);
+ message::success(
+ t("Added <b>%title</b> block to the dashboard center",
+ array("title" => $available["$module_name:$id"])));
+ } else {
+ block_manager::add("dashboard_sidebar", $module_name, $id);
+ message::success(
+ t("Added <b>%title</b> to the dashboard sidebar",
+ array("title" => $available["$module_name:$id"])));
+ }
+ }
+ url::redirect("admin/dashboard");
+ }
+
+ public function remove_block($id) {
+ access::verify_csrf();
+ $blocks_center = block_manager::get_active("dashboard_center");
+ $blocks_sidebar = block_manager::get_active("dashboard_sidebar");
+
+ if (array_key_exists($id, $blocks_sidebar)) {
+ $deleted = $blocks_sidebar[$id];
+ block_manager::remove("dashboard_sidebar", $id);
+ } else if (array_key_exists($id, $blocks_center)) {
+ $deleted = $blocks_center[$id];
+ block_manager::remove("dashboard_center", $id);
+ }
+
+ if (!empty($deleted)) {
+ $available = block_manager::get_available();
+ $title = $available[join(":", $deleted)];
+ message::success(t("Removed <b>%title</b> block", array("title" => $title)));
+ }
+
+ url::redirect("admin");
+ }
+
+ public function reorder() {
+ access::verify_csrf();
+ $active_set = array();
+ foreach (array("dashboard_sidebar", "dashboard_center") as $location) {
+ foreach (block_manager::get_active($location) as $id => $info) {
+ $active_set[$id] = $info;
+ }
+ }
+
+ foreach (array("dashboard_sidebar", "dashboard_center") as $location) {
+ $new_blocks = array();
+ foreach ($this->input->get($location, array()) as $id) {
+ $new_blocks[$id] = $active_set[$id];
+ }
+ block_manager::set_active($location, $new_blocks);
+ }
+
+ $this->_force_block_adder();
+ }
+}
diff --git a/modules/gallery/controllers/admin_graphics.php b/modules/gallery/controllers/admin_graphics.php
new file mode 100644
index 00000000..7e8ef47c
--- /dev/null
+++ b/modules/gallery/controllers/admin_graphics.php
@@ -0,0 +1,63 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Graphics_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_graphics.html");
+ $view->content->available = "";
+
+ $tk = new ArrayObject(graphics::detect_toolkits(), ArrayObject::ARRAY_AS_PROPS);
+ $active = module::get_var("gallery", "graphics_toolkit", "none");
+ foreach (array("gd", "imagemagick", "graphicsmagick", "none") as $id) {
+ if ($id == $active) {
+ $view->content->active = new View("admin_graphics_$id.html");
+ $view->content->active->tk = $tk;
+ $view->content->active->is_active = true;
+ } else if ($id != "none") {
+ $v = new View("admin_graphics_$id.html");
+ $v->tk = $tk;
+ $v->is_active = false;
+ $view->content->available .= $v;
+ }
+ }
+
+ print $view;
+ }
+
+ public function choose($toolkit) {
+ access::verify_csrf();
+ if ($toolkit != module::get_var("gallery", "graphics_toolkit")) {
+ module::set_var("gallery", "graphics_toolkit", $toolkit);
+
+ $toolkit_info = graphics::detect_toolkits();
+ if ($toolkit == "graphicsmagick" || $toolkit == "imagemagick") {
+ module::set_var("gallery", "graphics_toolkit_path", $toolkit_info[$toolkit]);
+ }
+
+ site_status::clear("missing_graphics_toolkit");
+ message::success(t("Updated Graphics Toolkit"));
+ log::success("graphics", t("Changed graphics toolkit to: %toolkit",
+ array("toolkit" => $toolkit)));
+ }
+
+ url::redirect("admin/graphics");
+ }
+}
+
diff --git a/modules/gallery/controllers/admin_languages.php b/modules/gallery/controllers/admin_languages.php
new file mode 100644
index 00000000..1dea733c
--- /dev/null
+++ b/modules/gallery/controllers/admin_languages.php
@@ -0,0 +1,136 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Languages_Controller extends Admin_Controller {
+ public function index($share_translations_form=null) {
+ $v = new Admin_View("admin.html");
+ $v->content = new View("admin_languages.html");
+ $v->content->settings_form = $this->_languages_form();
+ if (empty($share_translations_form)) {
+ $share_translations_form = $this->_share_translations_form();
+ }
+ $v->content->share_translations_form = $share_translations_form;
+ $this->_outgoing_translations_count();
+ print $v;
+ }
+
+ public function save() {
+ $form = $this->_languages_form();
+ if ($form->validate()) {
+ module::set_var("gallery", "default_locale", $form->choose_language->locale->value);
+ locale::update_installed($form->choose_language->installed_locales->value);
+ message::success(t("Settings saved"));
+ }
+ url::redirect("admin/languages");
+ }
+
+ public function share() {
+ $form = $this->_share_translations_form();
+ if (!$form->validate()) {
+ // Show the page with form errors
+ return $this->index($form);
+ }
+
+ if ($form->sharing->share) {
+ l10n_client::submit_translations();
+ message::success(t("Translations submitted"));
+ } else {
+ return $this->_save_api_key($form);
+ }
+ url::redirect("admin/languages");
+ }
+
+ private function _save_api_key($form) {
+ $new_key = $form->sharing->api_key->value;
+ if ($new_key && !l10n_client::validate_api_key($new_key)) {
+ $form->sharing->api_key->add_error("invalid", 1);
+ $valid = false;
+ } else {
+ $valid = true;
+ }
+
+ if ($valid) {
+ $old_key = l10n_client::api_key();
+ l10n_client::api_key($new_key);
+ if ($old_key && !$new_key) {
+ message::success(t("Your API key has been cleared."));
+ } else if ($old_key && $new_key && $old_key != $new_key) {
+ message::success(t("Your API key has been changed."));
+ } else if (!$old_key && $new_key) {
+ message::success(t("Your API key has been saved."));
+ }
+
+ log::success(t("gallery"), t("l10n_client API key changed."));
+ url::redirect("admin/languages");
+ } else {
+ // Show the page with form errors
+ $this->index($form);
+ }
+ }
+
+ private function _languages_form() {
+ $all_locales = locale::available();
+ $installed_locales = locale::installed();
+ $form = new Forge("admin/languages/save", "", "post", array("id" => "gLanguageSettingsForm"));
+ $group = $form->group("choose_language")
+ ->label(t("Language settings"));
+ $group->dropdown("locale")
+ ->options($installed_locales)
+ ->selected(module::get_var("gallery", "default_locale"))
+ ->label(t("Default language"))
+ ->rules('required');
+
+ $installation_options = array();
+ foreach ($all_locales as $code => $display_name) {
+ $installation_options[$code] = array($display_name, isset($installed_locales->$code));
+ }
+ $group->checklist("installed_locales")
+ ->label(t("Installed Languages"))
+ ->options($installation_options)
+ ->rules("required");
+ $group->submit("save")->value(t("Save settings"));
+ return $form;
+ }
+
+ private function _outgoing_translations_count() {
+ return ORM::factory("outgoing_translation")->count_all();
+ }
+
+ private function _share_translations_form() {
+ $form = new Forge("admin/languages/share", "", "post", array("id" => "gShareTranslationsForm"));
+ $group = $form->group("sharing")
+ ->label(t("Sharing you own translations with the Gallery community is easy. Please do!"));
+ $api_key = l10n_client::api_key();
+ $server_link = l10n_client::server_api_key_url();
+ $group->input("api_key")
+ ->label(empty($api_key)
+ ? t("This is a unique key that will allow you to send translations to the remote server. To get your API key go to %server-link.",
+ array("server-link" => html::anchor($server_link)))
+ : t("API Key"))
+ ->value($api_key)
+ ->error_messages("invalid", t("The API key you provided is invalid."));
+ $group->submit("save")->value(t("Save settings"));
+ if ($api_key && $this->_outgoing_translations_count()) {
+ // TODO: UI improvement: hide API key / save button when API key is set.
+ $group->submit("share")->value(t("Submit translations"));
+ }
+ return $form;
+ }
+}
+
diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php
new file mode 100644
index 00000000..c169de75
--- /dev/null
+++ b/modules/gallery/controllers/admin_maintenance.php
@@ -0,0 +1,181 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Maintenance_Controller extends Admin_Controller {
+ /**
+ * Show a list of all available, running and finished tasks.
+ */
+ public function index() {
+ $query = Database::instance()->query(
+ "UPDATE {tasks} SET `state` = 'stalled' " .
+ "WHERE done = 0 " .
+ "AND state <> 'stalled' " .
+ "AND unix_timestamp(now()) - updated > 15");
+ $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" => url::site("admin/maintenance"))));
+ }
+
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_maintenance.html");
+ $view->content->task_definitions = task::get_definitions();
+ $view->content->running_tasks = ORM::factory("task")
+ ->where("done", 0)->orderby("updated", "DESC")->find_all();
+ $view->content->finished_tasks = ORM::factory("task")
+ ->where("done", 1)->orderby("updated", "DESC")->find_all();
+ print $view;
+ }
+
+ /**
+ * Start a new task
+ * @param string $task_callback
+ */
+ public function start($task_callback) {
+ access::verify_csrf();
+
+ $tasks = task::get_definitions();
+ $task = task::create($tasks[$task_callback], array());
+ $view = new View("admin_maintenance_task.html");
+ $view->task = $task;
+
+ 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")));
+ print $view;
+ }
+
+ /**
+ * Resume a stalled task
+ * @param string $task_id
+ */
+ public function resume($task_id) {
+ access::verify_csrf();
+
+ $task = ORM::factory("task", $task_id);
+ if (!$task->loaded) {
+ throw new Exception("@todo MISSING_TASK");
+ }
+ $view = new View("admin_maintenance_task.html");
+ $view->task = $task;
+
+ 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")));
+ print $view;
+ }
+
+ /**
+ * Cancel a task.
+ * @param string $task_id
+ */
+ public function cancel($task_id) {
+ access::verify_csrf();
+
+ task::cancel($task_id);
+
+ message::success(t("Task cancelled"));
+ url::redirect("admin/maintenance");
+ }
+
+ public function cancel_running_tasks() {
+ access::verify_csrf();
+ Database::instance()->update(
+ "tasks",
+ array("done" => 1, "state" => "cancelled"),
+ array("done" => 0));
+ message::success(t("All running tasks cancelled"));
+ url::redirect("admin/maintenance");
+ }
+
+ /**
+ * Remove a task.
+ * @param string $task_id
+ */
+ public function remove($task_id) {
+ access::verify_csrf();
+
+ task::remove($task_id);
+
+ message::success(t("Task removed"));
+ url::redirect("admin/maintenance");
+ }
+
+ public function remove_finished_tasks() {
+ access::verify_csrf();
+ Database::instance()->delete("tasks", array("done" => 1));
+ message::success(t("All finished tasks removed"));
+ url::redirect("admin/maintenance");
+ }
+
+ /**
+ * Run a task. This will trigger the task to do a small amount of work, then it will report
+ * back with status on the task.
+ * @param string $task_id
+ */
+ public function run($task_id) {
+ access::verify_csrf();
+
+ try {
+ $task = task::run($task_id);
+ } catch (Exception $e) {
+ Kohana::log(
+ "error",
+ sprintf(
+ "%s in %s at line %s:\n%s", $e->getMessage(), $e->getFile(),
+ $e->getLine(), $e->getTraceAsString()));
+ throw $e;
+ }
+
+ if ($task->done) {
+ switch ($task->state) {
+ 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")));
+ 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")));
+ message::success(t("Task failed"));
+ break;
+ }
+ print json_encode(array("result" => "success",
+ "task" => array(
+ "percent_complete" => $task->percent_complete,
+ "status" => $task->status,
+ "done" => $task->done),
+ "location" => url::site("admin/maintenance")));
+
+ } else {
+ print json_encode(array("result" => "in_progress",
+ "task" => array(
+ "percent_complete" => $task->percent_complete,
+ "status" => $task->status,
+ "done" => $task->done)));
+ }
+ }
+}
diff --git a/modules/gallery/controllers/admin_modules.php b/modules/gallery/controllers/admin_modules.php
new file mode 100644
index 00000000..f7dd909d
--- /dev/null
+++ b/modules/gallery/controllers/admin_modules.php
@@ -0,0 +1,65 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Modules_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_modules.html");
+ $view->content->available = module::available();
+ print $view;
+ }
+
+ public function save() {
+ access::verify_csrf();
+
+ $changes->activate = array();
+ $changes->deactivate = array();
+ $activated_names = array();
+ $deactivated_names = array();
+ foreach (module::available() as $module_name => $info) {
+ if ($info->locked) {
+ continue;
+ }
+
+ $desired = $this->input->post($module_name) == 1;
+ if ($info->active && !$desired && module::is_active($module_name)) {
+ $changes->deactivate[] = $module_name;
+ $deactivated_names[] = $info->name;
+ module::deactivate($module_name);
+ } else if (!$info->active && $desired && !module::is_active($module_name)) {
+ $changes->activate[] = $module_name;
+ $activated_names[] = $info->name;
+ module::install($module_name);
+ module::activate($module_name);
+ }
+ }
+
+ module::event("module_change", $changes);
+
+ // @todo this type of collation is questionable from a i18n perspective
+ if ($activated_names) {
+ message::success(t("Activated: %names", array("names" => join(", ", $activated_names))));
+ }
+ if ($deactivated_names) {
+ message::success(t("Deactivated: %names", array("names" => join(", ", $deactivated_names))));
+ }
+ url::redirect("admin/modules");
+ }
+}
+
diff --git a/modules/gallery/controllers/admin_theme_details.php b/modules/gallery/controllers/admin_theme_details.php
new file mode 100644
index 00000000..fec1311b
--- /dev/null
+++ b/modules/gallery/controllers/admin_theme_details.php
@@ -0,0 +1,67 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Theme_Details_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_theme_details.html");
+ $view->content->form = theme::get_edit_form_admin();
+ print $view;
+ }
+
+ public function save() {
+ $form = theme::get_edit_form_admin();
+ if ($form->validate()) {
+ module::set_var("gallery", "page_size", $form->edit_theme->page_size->value);
+
+ $thumb_size = $form->edit_theme->thumb_size->value;
+ $thumb_dirty = false;
+ if (module::get_var("gallery", "thumb_size") != $thumb_size) {
+ graphics::remove_rule("gallery", "thumb", "resize");
+ graphics::add_rule(
+ "gallery", "thumb", "resize",
+ array("width" => $thumb_size, "height" => $thumb_size, "master" => Image::AUTO),
+ 100);
+ module::set_var("gallery", "thumb_size", $thumb_size);
+ }
+
+ $resize_size = $form->edit_theme->resize_size->value;
+ $resize_dirty = false;
+ if (module::get_var("gallery", "resize_size") != $resize_size) {
+ graphics::remove_rule("gallery", "resize", "resize");
+ graphics::add_rule(
+ "gallery", "resize", "resize",
+ array("width" => $resize_size, "height" => $resize_size, "master" => Image::AUTO),
+ 100);
+ module::set_var("gallery", "resize_size", $resize_size);
+ }
+
+ module::set_var("gallery", "header_text", $form->edit_theme->header_text->value);
+ module::set_var("gallery", "footer_text", $form->edit_theme->footer_text->value);
+
+ message::success(t("Updated theme details"));
+ url::redirect("admin/theme_details");
+ } else {
+ $view = new Admin_View("admin.html");
+ $view->content = $form;
+ print $view;
+ }
+ }
+}
+
diff --git a/modules/gallery/controllers/admin_themes.php b/modules/gallery/controllers/admin_themes.php
new file mode 100644
index 00000000..aef6c2d1
--- /dev/null
+++ b/modules/gallery/controllers/admin_themes.php
@@ -0,0 +1,79 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Themes_Controller extends Admin_Controller {
+ public function index() {
+ $view = new Admin_View("admin.html");
+ $view->content = new View("admin_themes.html");
+ $view->content->admin = module::get_var("gallery", "active_admin_theme");
+ $view->content->site = module::get_var("gallery", "active_site_theme");
+ $view->content->themes = $this->_get_themes();
+ print $view;
+ }
+
+ private function _get_themes() {
+ $themes = array();
+ foreach (scandir(THEMEPATH) as $theme_name) {
+ if ($theme_name[0] == ".") {
+ continue;
+ }
+
+ $file = THEMEPATH . "$theme_name/theme.info";
+ $theme_info = new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
+ $themes[$theme_name] = $theme_info;
+ }
+ return $themes;
+ }
+
+ public function preview($type, $theme_name) {
+ $view = new View("admin_themes_preview.html");
+ $theme_name = preg_replace("/[^\w]/", "", $theme_name);
+ $view->info = new ArrayObject(
+ parse_ini_file(THEMEPATH . "$theme_name/theme.info"), ArrayObject::ARRAY_AS_PROPS);
+ $view->theme_name = $theme_name;
+ $view->type = $type;
+ if ($type == "admin") {
+ $view->url = url::site("admin?theme=$theme_name");
+ } else {
+ $view->url = url::site("albums/1?theme=$theme_name");
+ }
+ print $view;
+ }
+
+ public function choose($type, $theme_name) {
+ access::verify_csrf();
+
+ $theme_name = preg_replace("/[^\w]/", "", $theme_name);
+ $info = new ArrayObject(
+ parse_ini_file(THEMEPATH . "$theme_name/theme.info"), ArrayObject::ARRAY_AS_PROPS);
+
+ if ($type == "admin" && $info->admin) {
+ module::set_var("gallery", "active_admin_theme", $theme_name);
+ message::success(t("Successfully changed your admin theme to <b>%theme_name</b>",
+ array("theme_name" => $info->name)));
+ } else if ($type == "site" && $info->site) {
+ module::set_var("gallery", "active_site_theme", $theme_name);
+ message::success(t("Successfully changed your Gallery theme to <b>%theme_name</b>",
+ array("theme_name" => $info->name)));
+ }
+
+ url::redirect("admin/themes");
+ }
+}
+
diff --git a/modules/gallery/controllers/after_install.php b/modules/gallery/controllers/after_install.php
new file mode 100644
index 00000000..f066afe4
--- /dev/null
+++ b/modules/gallery/controllers/after_install.php
@@ -0,0 +1,30 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class After_Install_Controller extends Controller {
+ public function index() {
+ if (!user::active()->admin) {
+ url::redirect("albums/1");
+ }
+
+ $v = new View("after_install.html");
+ $v->user = user::active();
+ print $v;
+ }
+}
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php
new file mode 100644
index 00000000..03a64f43
--- /dev/null
+++ b/modules/gallery/controllers/albums.php
@@ -0,0 +1,229 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Albums_Controller extends Items_Controller {
+
+ /**
+ * @see REST_Controller::_show($resource)
+ */
+ public function _show($album) {
+ if (!access::can("view", $album)) {
+ if ($album->id != 1) {
+ access::forbidden();
+ } else {
+ print new Theme_View("login_page.html", "album");
+ return;
+ }
+ }
+
+ $page_size = module::get_var("gallery", "page_size", 9);
+ $show = $this->input->get("show");
+
+ if ($show) {
+ $index = $album->get_position($show);
+ $page = ceil($index / $page_size);
+ if ($page == 1) {
+ url::redirect("albums/$album->id");
+ } else {
+ url::redirect("albums/$album->id?page=$page");
+ }
+ }
+
+ $page = $this->input->get("page", "1");
+ $children_count = $album->viewable()->children_count();
+ $offset = ($page - 1) * $page_size;
+ $max_pages = max(ceil($children_count / $page_size), 1);
+
+ // Make sure that the page references a valid offset
+ if ($page < 1) {
+ url::redirect("albums/$album->id");
+ } else if ($page > $max_pages) {
+ url::redirect("albums/$album->id?page=$max_pages");
+ }
+
+ $template = new Theme_View("page.html", "album");
+ $template->set_global("page_size", $page_size);
+ $template->set_global("item", $album);
+ $template->set_global("children", $album->viewable()->children($page_size, $offset));
+ $template->set_global("children_count", $children_count);
+ $template->set_global("parents", $album->parents());
+ $template->content = new View("album.html");
+
+ // We can't use math in ORM or the query builder, so do this by hand. It's important
+ // that we do this with math, otherwise concurrent accesses will damage accuracy.
+ Database::instance()->query(
+ "UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $album->id");
+
+ print $template;
+ }
+
+ /**
+ * @see REST_Controller::_create($resource)
+ */
+ public function _create($album) {
+ access::required("add", $album);
+
+ switch ($this->input->post("type")) {
+ case "album":
+ return $this->_create_album($album);
+
+ case "photo":
+ return $this->_create_photo($album);
+
+ default:
+ access::forbidden();
+ }
+ }
+
+ private function _create_album($album) {
+ access::required("add", $album);
+
+ $form = album::get_add_form($album);
+ if ($form->validate()) {
+ $new_album = album::create(
+ $album,
+ $this->input->post("name"),
+ $this->input->post("title", $this->input->post("name")),
+ $this->input->post("description"),
+ user::active()->id);
+
+ log::success("content", "Created an album",
+ html::anchor("albums/$new_album->id", "view album"));
+ message::success(t("Created album %album_title", array("album_title" => $new_album->title)));
+
+ print json_encode(
+ array("result" => "success",
+ "location" => url::site("albums/$new_album->id"),
+ "resource" => url::site("albums/$new_album->id")));
+ } else {
+ print json_encode(
+ array("result" => "error",
+ "form" => $form->__toString() . html::script("gallery/js/albums_form_add.js")));
+ }
+ }
+
+ private function _create_photo($album) {
+ access::required("add", $album);
+
+ // If we set the content type as JSON, it triggers saving the result as
+ // a document in the browser (well, in Chrome at least).
+ // @todo figure out why and fix this.
+ $form = photo::get_add_form($album);
+ if ($form->validate()) {
+ $photo = photo::create(
+ $album,
+ $this->input->post("file"),
+ $_FILES["file"]["name"],
+ $this->input->post("title", $this->input->post("name")),
+ $this->input->post("description"),
+ user::active()->id);
+
+ log::success("content", "Added a photo", html::anchor("photos/$photo->id", "view photo"));
+ message::success(t("Added photo %photo_title", array("photo_title" => $photo->title)));
+
+ print json_encode(
+ array("result" => "success",
+ "resource" => url::site("photos/$photo->id"),
+ "location" => url::site("photos/$photo->id")));
+ } else {
+ print json_encode(
+ array("result" => "error",
+ "form" => $form->__toString()));
+ }
+ }
+
+ /**
+ * @see REST_Controller::_update($resource)
+ */
+ public function _update($album) {
+ access::required("edit", $album);
+
+ $form = album::get_edit_form($album);
+ if ($valid = $form->validate()) {
+ // Make sure that there's not a conflict
+ if (Database::instance()
+ ->from("items")
+ ->where("parent_id", $album->parent_id)
+ ->where("id <>", $album->id)
+ ->where("name", $form->edit_album->dirname->value)
+ ->count_records()) {
+ $form->edit_album->dirname->add_error("conflict", 1);
+ $valid = false;
+ }
+ }
+
+ // @todo
+ // @todo we need to make sure that filename / dirname components can't contain a /
+ // @todo
+
+ if ($valid) {
+ $orig = clone $album;
+ $album->title = $form->edit_album->title->value;
+ $album->description = $form->edit_album->description->value;
+ $album->sort_column = $form->edit_album->sort_order->column->value;
+ $album->sort_order = $form->edit_album->sort_order->direction->value;
+ $album->rename($form->edit_album->dirname->value);
+ $album->save();
+
+ module::event("item_updated", $orig, $album);
+
+ log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
+ message::success(t("Saved album %album_title", array("album_title" => $album->title)));
+
+ print json_encode(
+ array("result" => "success",
+ "location" => url::site("albums/$album->id")));
+ } else {
+ print json_encode(
+ array("result" => "error",
+ "form" => $form->__toString()));
+ }
+ }
+
+ /**
+ * @see REST_Controller::_form_add($parameters)
+ */
+ public function _form_add($album_id) {
+ $album = ORM::factory("item", $album_id);
+ access::required("add", $album);
+
+ switch ($this->input->get("type")) {
+ case "album":
+ print album::get_add_form($album) .
+ html::script("gallery/js/albums_form_add.js");
+ break;
+
+ case "photo":
+ print photo::get_add_form($album);
+ break;
+
+ default:
+ kohana::show_404();
+ }
+ }
+
+ /**
+ * @see REST_Controller::_form_add($parameters)
+ */
+ public function _form_edit($album) {
+ access::required("edit", $album);
+
+ print album::get_edit_form($album);
+ }
+}
diff --git a/modules/gallery/controllers/file_proxy.php b/modules/gallery/controllers/file_proxy.php
new file mode 100644
index 00000000..f3c5f109
--- /dev/null
+++ b/modules/gallery/controllers/file_proxy.php
@@ -0,0 +1,120 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+/**
+ * Proxy access to files in var/albums and var/resizes, making sure that the session user has
+ * access to view these files.
+ *
+ * Security Philosophy: we do not use the information provided to find if the file exists on
+ * disk. We use this information only to locate the correct item in the database and then we
+ * *only* use information from the database to find and proxy the correct file. This way all user
+ * input is sanitized against the database before we perform any file I/O.
+ */
+class File_Proxy_Controller extends Controller {
+ public function __call($function, $args) {
+ // request_uri: http://example.com/gallery3/var/trunk/albums/foo/bar.jpg
+ $request_uri = $this->input->server("REQUEST_URI");
+ $request_uri = preg_replace("/\?.*/", "", $request_uri);
+
+ // var_uri: http://example.com/gallery3/var/
+ $var_uri = url::file("var/");
+
+ // Make sure that the request is for a file inside var
+ $offset = strpos($request_uri, $var_uri);
+ if ($offset === false) {
+ kohana::show_404();
+ }
+
+ $file = substr($request_uri, strlen($var_uri));
+
+ // Make sure that we don't leave the var dir
+ if (strpos($file, "..") !== false) {
+ kohana::show_404();
+ }
+
+ // We only handle var/resizes and var/albums
+ $paths = explode("/", $file);
+ $type = $paths[0];
+ if ($type != "resizes" && $type != "albums" && $type != "thumbs") {
+ kohana::show_404();
+ }
+
+ // If the last element is .album.jpg, pop that off since it's not a real item
+ if ($paths[count($paths)-1] == ".album.jpg") {
+ array_pop($paths);
+ }
+ if ($paths[count($paths)-1] == "") {
+ array_pop($paths);
+ }
+
+ // Find all items that match the level and name, then iterate over those to find a match.
+ // In most cases we'll get it in one. Note that for the level calculation, we just count the
+ // size of $paths. $paths includes the type ("thumbs", etc) but it doesn't include the root,
+ // so it's a wash.
+ $count = count($paths);
+ $compare_file = VARPATH . $file;
+ $item = null;
+ foreach (ORM::factory("item")
+ ->where("name", $paths[$count - 1])
+ ->where("level", $count)
+ ->find_all() as $match) {
+ if ($type == "albums") {
+ $match_file = $match->file_path();
+ } else if ($type == "resizes") {
+ $match_file = $match->resize_path();
+ } else {
+ $match_file = $match->thumb_path();
+ }
+ if ($match_file == $compare_file) {
+ $item = $match;
+ break;
+ }
+ }
+
+ if (!$item) {
+ kohana::show_404();
+ }
+
+ // Make sure we have access to the item
+ if (!access::can("view", $item)) {
+ kohana::show_404();
+ }
+
+ // Make sure we have view_full access to the original
+ if ($type == "albums" && !access::can("view_full", $item)) {
+ kohana::show_404();
+ }
+
+ // Don't try to load a directory
+ if ($type == "albums" && $item->is_album()) {
+ kohana::show_404();
+ }
+
+ if (!file_exists($match_file)) {
+ kohana::show_404();
+ }
+
+ // Dump out the image
+ header("Content-Type: $item->mime_type");
+ Kohana::close_buffers(false);
+ $fd = fopen($match_file, "rb");
+ fpassthru($fd);
+ fclose($fd);
+ }
+}
diff --git a/modules/gallery/controllers/items.php b/modules/gallery/controllers/items.php
new file mode 100644
index 00000000..13891726
--- /dev/null
+++ b/modules/gallery/controllers/items.php
@@ -0,0 +1,30 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Items_Controller extends REST_Controller {
+ protected $resource_type = "item";
+
+ public function _show($item) {
+ // Redirect to the more specific resource type, since it will render
+ // differently. We could also just delegate here, but it feels more appropriate
+ // to have a single canonical resource mapping.
+ access::required("view", $item);
+ return url::redirect($item->url(array(), true));
+ }
+}
diff --git a/modules/gallery/controllers/l10n_client.php b/modules/gallery/controllers/l10n_client.php
new file mode 100644
index 00000000..17520051
--- /dev/null
+++ b/modules/gallery/controllers/l10n_client.php
@@ -0,0 +1,128 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class L10n_Client_Controller extends Controller {
+ public function save() {
+ access::verify_csrf();
+ user::active()->admin or access::forbidden();
+
+ $input = Input::instance();
+ $message = $input->post("l10n-message-source");
+ $translation = $input->post("l10n-edit-target");
+ $key = I18n::get_message_key($message);
+ $locale = I18n::instance()->locale();
+
+ $entry = ORM::factory("outgoing_translation")
+ ->where(array("key" => $key,
+ "locale" => $locale))
+ ->find();
+
+ if (!$entry->loaded) {
+ $entry->key = $key;
+ $entry->locale = $locale;
+ $entry->message = serialize($message);
+ $entry->base_revision = null;
+ }
+
+ $entry->translation = serialize($translation);
+
+ $entry_from_incoming = ORM::factory("incoming_translation")
+ ->where(array("key" => $key,
+ "locale" => $locale))
+ ->find();
+
+ if (!$entry_from_incoming->loaded) {
+ $entry->base_revision = $entry_from_incoming->revision;
+ }
+
+ $entry->save();
+
+ print json_encode(new stdClass());
+ }
+
+ public function toggle_l10n_mode() {
+ access::verify_csrf();
+
+ $session = Session::instance();
+ $session->set("l10n_mode",
+ !$session->get("l10n_mode", false));
+
+ url::redirect("albums/1");
+ }
+
+ private static function _l10n_client_form() {
+ $form = new Forge("l10n_client/save", "", "post", array("id" => "gL10nClientSaveForm"));
+ $group = $form->group("l10n_message");
+ $group->hidden("l10n-message-source")->value("");
+ $group->textarea("l10n-edit-target");
+ $group->submit("l10n-edit-save")->value(t("Save translation"));
+ // TODO(andy_st): Avoiding multiple submit buttons for now (hassle with jQuery form plugin).
+ // $group->submit("l10n-edit-copy")->value(t("Copy source"));
+ // $group->submit("l10n-edit-clear")->value(t("Clear"));
+
+ return $form;
+ }
+
+ private static function _l10n_client_search_form() {
+ $form = new Forge("l10n_client/search", "", "post", array("id" => "gL10nSearchForm"));
+ $group = $form->group("l10n_search");
+ $group->input("l10n-search")->id("gL10nSearch");
+ $group->submit("l10n-search-filter-clear")->value(t("X"));
+
+ return $form;
+ }
+
+ public static function l10n_form() {
+ $calls = I18n::instance()->call_log();
+
+ if ($calls) {
+ $string_list = array();
+ foreach ($calls as $call) {
+ list ($message, $options) = $call;
+ // Note: Don't interpolate placeholders for the actual translation input field.
+ // TODO: Use $options to generate a preview.
+ if (is_array($message)) {
+ // TODO: Handle plural forms.
+ // Translate each message. If it has a plural form, get
+ // the current locale's plural rules and all plural translations.
+ continue;
+ }
+ $source = $message;
+ $translation = '';
+ $options_for_raw_translation = array();
+ if (isset($options['count'])) {
+ $options_for_raw_translation['count'] = $options['count'];
+ }
+ if (I18n::instance()->has_translation($message, $options_for_raw_translation)) {
+ $translation = I18n::instance()->translate($message, $options_for_raw_translation);
+ }
+ $string_list[] = array('source' => $source,
+ 'translation' => $translation);
+ }
+
+ $v = new View('l10n_client.html');
+ $v->string_list = $string_list;
+ $v->l10n_form = self::_l10n_client_form();
+ $v->l10n_search_form = self::_l10n_client_search_form();
+ return $v;
+ }
+
+ return '';
+ }
+}
diff --git a/modules/gallery/controllers/maintenance.php b/modules/gallery/controllers/maintenance.php
new file mode 100644
index 00000000..b5f39bed
--- /dev/null
+++ b/modules/gallery/controllers/maintenance.php
@@ -0,0 +1,24 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Maintenance_Controller extends Controller {
+ function index() {
+ print new View("maintenance.html");
+ }
+} \ No newline at end of file
diff --git a/modules/gallery/controllers/move.php b/modules/gallery/controllers/move.php
new file mode 100644
index 00000000..130c247f
--- /dev/null
+++ b/modules/gallery/controllers/move.php
@@ -0,0 +1,64 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Move_Controller extends Controller {
+ public function browse($source_id) {
+ $source = ORM::factory("item", $source_id);
+ access::required("edit", $source);
+
+ $view = new View("move_browse.html");
+ $view->source = $source;
+ $view->tree = $this->_get_tree_html($source, ORM::factory("item", 1));
+ print $view;
+ }
+
+ public function save($source_id) {
+ access::verify_csrf();
+ $source = ORM::factory("item", $source_id);
+ $target = ORM::factory("item", $this->input->post("target_id"));
+
+ item::move($source, $target);
+
+ print json_encode(
+ array("result" => "success",
+ "location" => url::site("albums/{$target->id}")));
+ }
+
+ public function show_sub_tree($source_id, $target_id) {
+ $source = ORM::factory("item", $source_id);
+ $target = ORM::factory("item", $target_id);
+ access::required("edit", $source);
+ access::required("view", $target);
+
+ print $this->_get_tree_html($source, $target);
+ }
+
+ private function _get_tree_html($source, $target) {
+ $view = new View("move_tree.html");
+ $view->source = $source;
+ $view->parent = $target;
+ $view->children = ORM::factory("item")
+ ->viewable()
+ ->where("type", "album")
+ ->where("parent_id", $target->id)
+ ->find_all();
+ return $view;
+ }
+
+}
diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php
new file mode 100644
index 00000000..55bbb0e5
--- /dev/null
+++ b/modules/gallery/controllers/movies.php
@@ -0,0 +1,114 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Movies_Controller extends Items_Controller {
+
+ /**
+ * @see REST_Controller::_show($resource)
+ */
+ public function _show($photo) {
+ access::required("view", $photo);
+
+ // We sort by id ascending so for now, find sibling info by doing id based queries.
+ $next_item = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id >", $photo->id)
+ ->orderby("id", "ASC")
+ ->find();
+ $previous_item = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <", $photo->id)
+ ->orderby("id", "DESC")
+ ->find();
+ $position = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <=", $photo->id)
+ ->count_all();
+
+ $template = new Theme_View("page.html", "photo");
+ $template->set_global("item", $photo);
+ $template->set_global("children", array());
+ $template->set_global("children_count", $photo->children_count());
+ $template->set_global("parents", $photo->parents());
+ $template->set_global("next_item", $next_item->loaded ? $next_item : null);
+ $template->set_global("previous_item", $previous_item->loaded ? $previous_item : null);
+ $template->set_global("sibling_count", $photo->parent()->children_count());
+ $template->set_global("position", $position);
+
+ $template->content = new View("movie.html");
+
+ $photo->view_count++;
+ $photo->save();
+
+ print $template;
+ }
+
+ /**
+ * @see REST_Controller::_update($resource)
+ */
+ public function _update($photo) {
+ access::required("edit", $photo);
+
+ $form = photo::get_edit_form($photo);
+ if ($valid = $form->validate()) {
+ // Make sure that there's not a conflict
+ if (Database::instance()
+ ->from("items")
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <>", $photo->id)
+ ->where("name", $form->edit_photo->filename->value)
+ ->count_records()) {
+ $form->edit_photo->filename->add_error("conflict", 1);
+ $valid = false;
+ }
+ }
+
+ if ($valid) {
+ $orig = clone $photo;
+ $photo->title = $form->edit_photo->title->value;
+ $photo->description = $form->edit_photo->description->value;
+ $photo->rename($form->edit_photo->filename->value);
+ $photo->save();
+
+ module::event("item_updated", $orig, $photo);
+
+ log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
+ message::success(t("Saved photo %photo_title", array("photo_title" => $photo->title)));
+
+ print json_encode(
+ array("result" => "success",
+ "location" => url::site("photos/$photo->id")));
+ } else {
+ print json_encode(
+ array("result" => "error",
+ "form" => $form->__toString()));
+ }
+ }
+
+ /**
+ * @see REST_Controller::_form_edit($resource)
+ */
+ public function _form_edit($photo) {
+ access::required("edit", $photo);
+ print photo::get_edit_form($photo);
+ }
+}
diff --git a/modules/gallery/controllers/permissions.php b/modules/gallery/controllers/permissions.php
new file mode 100644
index 00000000..b0cee303
--- /dev/null
+++ b/modules/gallery/controllers/permissions.php
@@ -0,0 +1,80 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Permissions_Controller extends Controller {
+ function browse($id) {
+ $item = ORM::factory("item", $id);
+ access::required("edit", $item);
+
+ if (!$item->is_album()) {
+ access::forbidden();
+ }
+
+ $view = new View("permissions_browse.html");
+ $view->htaccess_works = access::htaccess_works();
+ $view->item = $item;
+ $view->parents = $item->parents();
+ $view->form = $this->_get_form($item);
+
+ print $view;
+ }
+
+ function form($id) {
+ $item = ORM::factory("item", $id);
+ access::required("edit", $item);
+
+ if (!$item->is_album()) {
+ access::forbidden();
+ }
+
+ print $this->_get_form($item);
+ }
+
+ function change($command, $group_id, $perm_id, $item_id) {
+ access::verify_csrf();
+ $group = ORM::factory("group", $group_id);
+ $perm = ORM::factory("permission", $perm_id);
+ $item = ORM::factory("item", $item_id);
+ access::required("edit", $item);
+
+ if ($group->loaded && $perm->loaded && $item->loaded) {
+ switch($command) {
+ case "allow":
+ access::allow($group, $perm->name, $item);
+ break;
+
+ case "deny":
+ access::deny($group, $perm->name, $item);
+ break;
+
+ case "reset":
+ access::reset($group, $perm->name, $item);
+ break;
+ }
+ }
+ }
+
+ function _get_form($item) {
+ $view = new View("permissions_form.html");
+ $view->item = $item;
+ $view->groups = ORM::factory("group")->find_all();
+ $view->permissions = ORM::factory("permission")->find_all();
+ return $view;
+ }
+}
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php
new file mode 100644
index 00000000..5d4040cf
--- /dev/null
+++ b/modules/gallery/controllers/photos.php
@@ -0,0 +1,116 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Photos_Controller extends Items_Controller {
+
+ /**
+ * @see REST_Controller::_show($resource)
+ */
+ public function _show($photo) {
+ access::required("view", $photo);
+
+ // We sort by id ascending so for now, find sibling info by doing id based queries.
+ $next_item = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id >", $photo->id)
+ ->orderby("id", "ASC")
+ ->find();
+ $previous_item = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <", $photo->id)
+ ->orderby("id", "DESC")
+ ->find();
+ $position = ORM::factory("item")
+ ->viewable()
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <=", $photo->id)
+ ->count_all();
+
+ $template = new Theme_View("page.html", "photo");
+ $template->set_global("item", $photo);
+ $template->set_global("children", array());
+ $template->set_global("children_count", $photo->children_count());
+ $template->set_global("parents", $photo->parents());
+ $template->set_global("next_item", $next_item->loaded ? $next_item : null);
+ $template->set_global("previous_item", $previous_item->loaded ? $previous_item : null);
+ $template->set_global("sibling_count", $photo->parent()->children_count());
+ $template->set_global("position", $position);
+
+ $template->content = new View("photo.html");
+
+ $photo->view_count++;
+ $photo->save();
+
+ print $template;
+ }
+
+ /**
+ * @see REST_Controller::_update($resource)
+ */
+ public function _update($photo) {
+ access::required("edit", $photo);
+
+ $form = photo::get_edit_form($photo);
+ if ($valid = $form->validate()) {
+ if ($form->edit_photo->filename->value != $photo->name) {
+ // Make sure that there's not a conflict
+ if (Database::instance()
+ ->from("items")
+ ->where("parent_id", $photo->parent_id)
+ ->where("id <>", $photo->id)
+ ->where("name", $form->edit_photo->filename->value)
+ ->count_records()) {
+ $form->edit_photo->filename->add_error("conflict", 1);
+ $valid = false;
+ }
+ }
+ }
+
+ if ($valid) {
+ $orig = clone $photo;
+ $photo->title = $form->edit_photo->title->value;
+ $photo->description = $form->edit_photo->description->value;
+ $photo->rename($form->edit_photo->filename->value);
+ $photo->save();
+
+ module::event("item_updated", $orig, $photo);
+
+ log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
+ message::success(t("Saved photo %photo_title", array("photo_title" => $photo->title)));
+
+ print json_encode(
+ array("result" => "success",
+ "location" => url::site("photos/$photo->id")));
+ } else {
+ print json_encode(
+ array("result" => "error",
+ "form" => $form->__toString()));
+ }
+ }
+
+ /**
+ * @see REST_Controller::_form_edit($resource)
+ */
+ public function _form_edit($photo) {
+ access::required("edit", $photo);
+ print photo::get_edit_form($photo);
+ }
+}
diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php
new file mode 100644
index 00000000..643dce30
--- /dev/null
+++ b/modules/gallery/controllers/quick.php
@@ -0,0 +1,122 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Quick_Controller extends Controller {
+ public function pane($id) {
+ $item = ORM::factory("item", $id);
+ if (!$item->loaded) {
+ return "";
+ }
+
+ $view = new View("quick_pane.html");
+ $view->item = $item;
+ $view->page_type = Input::instance()->get("page_type");
+ print $view;
+ }
+
+ public function rotate($id, $dir) {
+ access::verify_csrf();
+ $item = ORM::factory("item", $id);
+ if (!$item->loaded) {
+ return "";
+ }
+
+ $degrees = 0;
+ switch($dir) {
+ case "ccw":
+ $degrees = -90;
+ break;
+
+ case "cw":
+ $degrees = 90;
+ break;
+ }
+
+ if ($degrees) {
+ graphics::rotate($item->file_path(), $item->file_path(), array("degrees" => $degrees));
+
+ list($item->width, $item->height) = getimagesize($item->file_path());
+ $item->resize_dirty= 1;
+ $item->thumb_dirty= 1;
+ $item->save();
+
+ graphics::generate($item);
+
+ $parent = $item->parent();
+ if ($parent->album_cover_item_id == $item->id) {
+ copy($item->thumb_path(), $parent->thumb_path());
+ $parent->thumb_width = $item->thumb_width;
+ $parent->thumb_height = $item->thumb_height;
+ $parent->save();
+ }
+ }
+
+ if (Input::instance()->get("page_type") == "album") {
+ print json_encode(
+ array("src" => $item->thumb_url() . "?rnd=" . rand(),
+ "width" => $item->thumb_width,
+ "height" => $item->thumb_height));
+ } else {
+ print json_encode(
+ array("src" => $item->resize_url() . "?rnd=" . rand(),
+ "width" => $item->resize_width,
+ "height" => $item->resize_height));
+ }
+ }
+
+ public function make_album_cover($id) {
+ access::verify_csrf();
+ item::make_album_cover(ORM::factory("item", $id));
+
+ print json_encode(array("result" => "success"));
+ }
+
+ public function delete($id) {
+ access::verify_csrf();
+ $item = ORM::factory("item", $id);
+ access::required("edit", $item);
+
+ if ($item->is_album()) {
+ $msg = t("Deleted album <b>%title</b>", array("title" => $item->title));
+ } else {
+ $msg = t("Deleted photo <b>%title</b>", array("title" => $item->title));
+ }
+
+ $item->delete();
+ message::success($msg);
+
+ if (Input::instance()->get("page_type") == "album") {
+ print json_encode(array("result" => "success", "reload" => 1));
+ } else {
+ print json_encode(array("result" => "success",
+ "location" => url::site("albums/$parent->id")));
+ }
+ }
+
+ public function form_edit($id) {
+ $item = ORM::factory("item", $id);
+ access::required("edit", $item);
+ if ($item->is_album()) {
+ $form = album::get_edit_form($item);
+ } else {
+ $form = photo::get_edit_form($item);
+ }
+ print $form;
+ }
+}
diff --git a/modules/gallery/controllers/rest.php b/modules/gallery/controllers/rest.php
new file mode 100644
index 00000000..11a6bbac
--- /dev/null
+++ b/modules/gallery/controllers/rest.php
@@ -0,0 +1,183 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+/**
+ * This abstract controller makes it easy to create a RESTful controller. To use it, create a
+ * subclass which defines the resource type and implements get/post/put/delete methods, like this:
+ *
+ * class Comment_Controller extends REST_Controller {
+ * protected $resource_type = "comment"; // this tells REST which model to use
+ *
+ * public function _index() {
+ * // Handle GET request to /controller
+ * }
+ *
+ * public function _show(ORM $comment) {
+ * // Handle GET request to /comments/{comment_id}
+ * }
+ *
+ * public function _update(ORM $comment) {
+ * // Handle PUT request to /comments/{comment_id}
+ * }
+ *
+ * public function _create(ORM $comment) {
+ * // Handle POST request to /comments
+ * }
+ *
+ * public function _delete(ORM $comment) {
+ * // Handle DELETE request to /comments/{comments_id}
+ * }
+ *
+ * public function _form_add($parameters) {
+ * // Handle GET request to /form/add/comments
+ * // Show a form for creating a new comment
+ * }
+ *
+ * public function _form_edit(ORM $comment) {
+ * // Handle GET request to /form/edit/comments
+ * // Show a form for editing an existing comment
+ * }
+ *
+ * A request to http://example.com/gallery3/comments/3 will result in a call to
+ * REST_Controller::__call(3) which will load up the comment associated with id 3. If there's
+ * no such comment, it returns a 404. Otherwise, it will then delegate to
+ * Comment_Controller::get() with the ORM instance as an argument.
+ */
+class REST_Controller extends Controller {
+ protected $resource_type = null;
+
+ public function __construct() {
+ if ($this->resource_type == null) {
+ throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE");
+ }
+ parent::__construct();
+ }
+
+ /**
+ * Handle dispatching for all REST controllers.
+ */
+ public function __call($function, $args) {
+ // If no parameter was provided after the controller name (eg "/albums") then $function will
+ // be set to "index". Otherwise, $function is the first parameter, and $args are all
+ // subsequent parameters.
+ $request_method = rest::request_method();
+ if ($function == "index" && $request_method == "get") {
+ return $this->_index();
+ }
+
+ $resource = ORM::factory($this->resource_type, (int)$function);
+ if (!$resource->loaded && $request_method != "post") {
+ return Kohana::show_404();
+ }
+
+ if ($request_method != "get") {
+ access::verify_csrf();
+ }
+
+ switch ($request_method) {
+ case "get":
+ return $this->_show($resource);
+
+ case "put":
+ return $this->_update($resource);
+
+ case "delete":
+ return $this->_delete($resource);
+
+ case "post":
+ return $this->_create($resource);
+ }
+ }
+
+ /* We're editing an existing item, load it from the database. */
+ public function form_edit($resource_id) {
+ if ($this->resource_type == null) {
+ throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE");
+ }
+
+ // @todo this needs security checks
+ $resource = ORM::factory($this->resource_type, $resource_id);
+ if (!$resource->loaded) {
+ return Kohana::show_404();
+ }
+
+ return $this->_form_edit($resource);
+ }
+
+ /* We're adding a new item, pass along any additional parameters. */
+ public function form_add($parameters) {
+ return $this->_form_add($parameters);
+ }
+
+ /**
+ * Perform a GET request on the controller root
+ * (e.g. http://www.example.com/gallery3/comments)
+ */
+ public function _index() {
+ throw new Exception("@todo _create NOT IMPLEMENTED");
+ }
+
+ /**
+ * Perform a POST request on this resource
+ * @param ORM $resource the instance of this resource type
+ */
+ public function _create($resource) {
+ throw new Exception("@todo _create NOT IMPLEMENTED");
+ }
+
+ /**
+ * Perform a GET request on this resource
+ * @param ORM $resource the instance of this resource type
+ */
+ public function _show($resource) {
+ throw new Exception("@todo _show NOT IMPLEMENTED");
+ }
+
+ /**
+ * Perform a PUT request on this resource
+ * @param ORM $resource the instance of this resource type
+ */
+ public function _update($resource) {
+ throw new Exception("@todo _update NOT IMPLEMENTED");
+ }
+
+ /**
+ * Perform a DELETE request on this resource
+ * @param ORM $resource the instance of this resource type
+ */
+ public function _delete($resource) {
+ throw new Exception("@todo _delete NOT IMPLEMENTED");
+ }
+
+ /**
+ * Present a form for adding a new resource
+ * @param string part of the URI after the controller name
+ */
+ public function _form_add($parameter) {
+ throw new Exception("@todo _form_add NOT IMPLEMENTED");
+ }
+
+ /**
+ * Present a form for editing an existing resource
+ * @param ORM $resource the resource container for instances of this resource type
+ */
+ public function _form_edit($resource) {
+ throw new Exception("@todo _form_edit NOT IMPLEMENTED");
+ }
+}
diff --git a/modules/gallery/controllers/scaffold.php b/modules/gallery/controllers/scaffold.php
new file mode 100644
index 00000000..5b8f9aa9
--- /dev/null
+++ b/modules/gallery/controllers/scaffold.php
@@ -0,0 +1,437 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Scaffold_Controller extends Template_Controller {
+ public $template = "scaffold.html";
+
+ function index() {
+ $session = Session::instance();
+
+ set_error_handler(array("Scaffold_Controller", "_error_handler"));
+ try {
+ $this->template->album_count = ORM::factory("item")->where("type", "album")->count_all();
+ $this->template->photo_count = ORM::factory("item")->where("type", "photo")->count_all();
+ $this->template->album_tree = $this->_load_album_tree();
+ $this->template->add_photo_html = $this->_get_add_photo_html();
+ } catch (Exception $e) {
+ $this->template->album_count = 0;
+ $this->template->photo_count = 0;
+ $this->template->deepest_photo = null;
+ $this->template->album_tree = array();
+ $this->template->add_photo_html = "";
+ }
+
+ $this->_load_comment_info();
+ $this->_load_tag_info();
+
+ restore_error_handler();
+
+ if (!empty($session) && $session->get("profiler", false)) {
+ $profiler = new Profiler();
+ $profiler->render();
+ }
+ }
+
+
+ function add_photos() {
+ $path = trim($this->input->post("path"));
+ $parent_id = (int)$this->input->post("parent_id");
+ $parent = ORM::factory("item", $parent_id);
+ if (!$parent->loaded) {
+ throw new Exception("@todo BAD_ALBUM");
+ }
+
+ batch::start();
+ cookie::set("add_photos_path", $path);
+ $photo_count = 0;
+ foreach (glob("$path/*.[Jj][Pp][Gg]") as $file) {
+ set_time_limit(30);
+ photo::create($parent, $file, basename($file), basename($file));
+ $photo_count++;
+ }
+ batch::stop();
+
+ if ($photo_count > 0) {
+ log::success("content", "(scaffold) Added $photo_count photos",
+ html::anchor("albums/$parent_id", "View album"));
+ }
+
+ url::redirect("scaffold");
+ }
+
+ function add_albums_and_photos($count, $desired_type=null) {
+ srand(time());
+ $parents = ORM::factory("item")->where("type", "album")->find_all()->as_array();
+ $owner_id = user::active()->id;
+
+ $test_images = glob(MODPATH . "gallery/tests/images/*.[Jj][Pp][Gg]");
+
+ batch::start();
+ $album_count = $photo_count = 0;
+ for ($i = 0; $i < $count; $i++) {
+ set_time_limit(30);
+
+ $parent = $parents[array_rand($parents)];
+ $parent->reload();
+ $type = $desired_type;
+ if (!$type) {
+ $type = rand(0, 10) ? "photo" : "album";
+ }
+ if ($type == "album") {
+ $thumb_size = module::get_var("gallery", "thumb_size");
+ $parents[] = album::create(
+ $parent, "rnd_" . rand(), "Rnd $i", "random album $i", $owner_id)
+ ->save();
+ $album_count++;
+ } else {
+ $photo_index = rand(0, count($test_images) - 1);
+ photo::create($parent, $test_images[$photo_index], basename($test_images[$photo_index]),
+ "rnd_" . rand(), "sample thumb", $owner_id);
+ $photo_count++;
+ }
+ }
+ batch::stop();
+
+ if ($photo_count > 0) {
+ log::success("content", "(scaffold) Added $photo_count photos");
+ }
+
+ if ($album_count > 0) {
+ log::success("content", "(scaffold) Added $album_count albums");
+ }
+ url::redirect("scaffold");
+ }
+
+ function random_phrase($count) {
+ static $words;
+ if (empty($words)) {
+ $sample_text = "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium
+ laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi
+ architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas
+ sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione
+ voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit,
+ amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut
+ labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis
+ nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi
+ consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam
+ nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla
+ pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis
+ praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi
+ sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt
+ mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et
+ expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque
+ nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas
+ assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis
+ debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et
+ molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut
+ reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores
+ repellat.";
+ $words = preg_split('/\s+/', $sample_text);
+ }
+
+ $chosen = array();
+ for ($i = 0; $i < $count; $i++) {
+ $chosen[] = $words[array_rand($words)];
+ }
+
+ return implode(' ', $chosen);
+ }
+
+ function add_comments($count) {
+ srand(time());
+ $photos = ORM::factory("item")->where("type", "photo")->find_all()->as_array();
+ $users = ORM::factory("user")->find_all()->as_array();
+
+ if (empty($photos)) {
+ url::redirect("scaffold");
+ }
+
+ if (module::is_active("akismet")) {
+ akismet::$test_mode = 1;
+ }
+ for ($i = 0; $i < $count; $i++) {
+ $photo = $photos[array_rand($photos)];
+ $author = $users[array_rand($users)];
+ $guest_name = ucfirst($this->random_phrase(rand(1, 3)));
+ $guest_email = sprintf("%s@%s.com", $this->random_phrase(1), $this->random_phrase(1));
+ $guest_url = sprintf("http://www.%s.com", $this->random_phrase(1));
+ comment::create($photo, $author, $this->random_phrase(rand(8, 500)),
+ $guest_name, $guest_email, $guest_url);
+ }
+
+ url::redirect("scaffold");
+ }
+
+ function add_tags($count) {
+ $items = ORM::factory("item")->find_all()->as_array();
+
+ if (!empty($items)) {
+ $tags = $this->_generateTags($count);
+
+ while ($count-- > 0) {
+ $tag_name = $tags[array_rand($tags)];
+ $item = $items[array_rand($items)];
+
+ tag::add($item, $tag_name);
+ }
+ }
+
+ url::redirect("scaffold");
+ }
+
+ private function _generateTags($number){
+ // Words from lorem2.com
+ $words = explode(
+ " ",
+ "Lorem ipsum dolor sit amet consectetuer adipiscing elit Donec odio Quisque volutpat " .
+ "mattis eros Nullam malesuada erat ut turpis Suspendisse urna nibh viverra non " .
+ "semper suscipit posuere a pede Donec nec justo eget felis facilisis " .
+ "fermentum Aliquam porttitor mauris sit amet orci Aenean dignissim pellentesque " .
+ "felis Morbi in sem quis dui placerat ornare Pellentesque odio nisi euismod in " .
+ "pharetra a ultricies in diam Sed arcu Cras consequat Praesent dapibus neque " .
+ "id cursus faucibus tortor neque egestas augue eu vulputate magna eros eu " .
+ "erat Aliquam erat volutpat Nam dui mi tincidunt quis accumsan porttitor " .
+ "facilisis luctus metus Phasellus ultrices nulla quis nibh Quisque a " .
+ "lectus Donec consectetuer ligula vulputate sem tristique cursus Nam nulla quam " .
+ "gravida non commodo a sodales sit amet nisi Pellentesque fermentum " .
+ "dolor Aliquam quam lectus facilisis auctor ultrices ut elementum vulputate " .
+ "nunc Sed adipiscing ornare risus Morbi est est blandit sit amet sagittis vel " .
+ "euismod vel velit Pellentesque egestas sem Suspendisse commodo ullamcorper " .
+ "magna");
+
+ while ($number--) {
+ $results[] = $words[array_rand($words, 1)];
+ }
+ return $results;
+ }
+
+ function _error_handler($x) {
+ }
+
+ private function _load_comment_info() {
+ if (class_exists("Comment_Model")) {
+ $this->template->comment_count = ORM::factory("comment")->count_all();
+ } else {
+ $this->template->comment_count = 0;
+ }
+ }
+
+ private function _load_tag_info() {
+ if (class_exists("Tag_Model")) {
+ $this->template->tag_count = ORM::factory("tag")->count_all();
+ $this->template->most_tagged = Database::instance()
+ ->select("item_id AS id", "COUNT(tag_id) AS count")
+ ->from("items_tags")
+ ->groupby("item_id")
+ ->orderby("count", "DESC")
+ ->limit(1)
+ ->get()
+ ->current();
+ } else {
+ $this->template->tag_count = 0;
+ $this->template->most_tagged = 0;
+ }
+ }
+
+ function install($module_name, $redirect=true) {
+ $to_install = array();
+ if ($module_name == "*") {
+ foreach (module::available() as $module_name => $info) {
+ if (empty($info->installed)) {
+ $to_install[] = $module_name;
+ }
+ }
+ } else {
+ $to_install[] = $module_name;
+ }
+
+ foreach ($to_install as $module_name) {
+ if ($module_name != "gallery") {
+ require_once(DOCROOT . "modules/${module_name}/helpers/${module_name}_installer.php");
+ }
+ module::install($module_name);
+ }
+
+ if ($redirect) {
+ url::redirect("scaffold");
+ }
+ }
+
+
+ public function package() {
+ $this->auto_render = false;
+ $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);
+
+ try {
+ 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);
+ }
+ } catch (Exception $e) {
+ Kohana::log("error", $e->getTraceAsString());
+ print $e->getTrace();
+ throw $e;
+ }
+
+ url::redirect("scaffold/dump_database");
+ }
+
+ public function dump_database() {
+ $this->auto_render = false;
+
+ // We now have a clean install with just the packages that we want. Make sure that the
+ // database is clean too.
+ $db = Database::instance();
+ $db->query("TRUNCATE {sessions}");
+ $db->query("TRUNCATE {logs}");
+ $db->query("DELETE FROM {vars} WHERE `module_name` = 'gallery' 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 "<pre>";
+ print "$command\n";
+ print "Failed to dump database\n";
+ print implode("\n", $output);
+ return;
+ }
+
+ // Post-process the sql file
+ $buf = "";
+ $root_timestamp = ORM::factory("item", 1)->created;
+ 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_timestamp,/", ",UNIX_TIMESTAMP(),", $line);
+ $buf .= $line;
+ }
+ $fd = fopen($sql_file, "wb");
+ fwrite($fd, $buf);
+ fclose($fd);
+
+ url::redirect("scaffold/dump_var");
+ }
+
+ public function dump_var() {
+ $this->auto_render = false;
+
+ $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, "<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n");
+ fwrite($fd, "<?php\n");
+ foreach ($paths as $path) {
+ fwrite($fd, "!file_exists($path) && mkdir($path);\n");
+ }
+ fclose($fd);
+ url::redirect("scaffold");
+ }
+
+ private function _load_album_tree() {
+ $tree = array();
+ foreach (ORM::factory("item")->where("type", "album")->find_all() as $album) {
+ if ($album->parent_id) {
+ $tree[$album->parent_id]->children[] = $album->id;
+ }
+ $tree[$album->id]->album = $album;
+ $tree[$album->id]->children = array();
+ }
+
+ return $tree;
+ }
+
+ public function form($arg1, $arg2) {
+ if ($arg1 == "add" && $arg2 == "photos") {
+ print $this->_get_add_photo_html();
+ }
+ $this->auto_render = false;
+ }
+
+ public function _get_add_photo_html($parent_id=1) {
+ $parent = ORM::factory("item", $parent_id);
+ return photo::get_add_form($parent);
+ }
+}
diff --git a/modules/gallery/controllers/simple_uploader.php b/modules/gallery/controllers/simple_uploader.php
new file mode 100644
index 00000000..bdf9582f
--- /dev/null
+++ b/modules/gallery/controllers/simple_uploader.php
@@ -0,0 +1,86 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Simple_Uploader_Controller extends Controller {
+ public function app($id) {
+ $item = ORM::factory("item", $id);
+ access::required("edit", $item);
+
+ $v = new View("simple_uploader.html");
+ $v->item = $item;
+ print $v;
+ }
+
+ public function start() {
+ batch::start();
+ }
+
+ public function add_photo($id) {
+ $album = ORM::factory("item", $id);
+ access::required("add", $album);
+ access::verify_csrf();
+
+ $file_validation = new Validation($_FILES);
+ $file_validation->add_rules("Filedata", "upload::valid", "upload::type[gif,jpg,png,flv,mp4]");
+ if ($file_validation->validate()) {
+
+ // SimpleUploader.swf does not yet call /start directly, so simulate it here for now.
+ if (!batch::in_progress()) {
+ batch::start();
+ }
+
+ $temp_filename = upload::save("Filedata");
+ try {
+ $name = substr(basename($temp_filename), 10); // Skip unique identifier Kohana adds
+ $title = $this->convert_filename_to_title($name);
+ $path_info = pathinfo($temp_filename);
+ if (array_key_exists("extension", $path_info) &&
+ in_array(strtolower($path_info["extension"]), array("flv", "mp4"))) {
+ $movie = movie::create($album, $temp_filename, $name, $title);
+ log::success("content", t("Added a movie"),
+ html::anchor("movies/$movie->id", t("view movie")));
+ } else {
+ $photo = photo::create($album, $temp_filename, $name, $title);
+ log::success("content", t("Added a photo"),
+ html::anchor("photos/$photo->id", t("view photo")));
+ }
+ } catch (Exception $e) {
+ unlink($temp_filename);
+ throw $e;
+ }
+ unlink($temp_filename);
+ }
+ print "File Received";
+ }
+
+ /**
+ * We should move this into a helper somewhere.. but where is appropriate?
+ */
+ private function convert_filename_to_title($filename) {
+ $title = strtr($filename, "_", " ");
+ $title = preg_replace("/\..*?$/", "", $title);
+ $title = preg_replace("/ +/", " ", $title);
+ return $title;
+ }
+
+ public function finish() {
+ batch::stop();
+ print json_encode(array("result" => "success"));
+ }
+}