summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/libraries')
-rw-r--r--modules/gallery/libraries/Admin_View.php18
-rw-r--r--modules/gallery/libraries/Gallery_View.php89
-rw-r--r--modules/gallery/libraries/Menu.php31
-rw-r--r--modules/gallery/libraries/Theme_View.php50
-rw-r--r--modules/gallery/libraries/drivers/Cache/Database.php179
5 files changed, 333 insertions, 34 deletions
diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php
index 1f976871..01496c0d 100644
--- a/modules/gallery/libraries/Admin_View.php
+++ b/modules/gallery/libraries/Admin_View.php
@@ -17,9 +17,7 @@
* 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_View_Core extends View {
- private $theme_name = null;
-
+class Admin_View_Core extends Gallery_View {
/**
* Attempts to load a view and pre-load view data.
*
@@ -46,15 +44,6 @@ class Admin_View_Core extends View {
$this->set_global("user", user::active());
}
- public function url($path, $absolute_url=false) {
- $arg = "themes/{$this->theme_name}/$path";
- return $absolute_url ? url::abs_file($arg) : url::file($arg);
- }
-
- public function display($page_name, $view_class="View") {
- return new $view_class($page_name);
- }
-
public function admin_menu() {
$menu = Menu::factory("root");
gallery_menu::admin($menu, $this);
@@ -69,6 +58,7 @@ class Admin_View_Core extends View {
}
}
+ $menu->compact();
print $menu;
}
@@ -108,6 +98,10 @@ class Admin_View_Core extends View {
}
}
+ if ($function == "admin_head") {
+ array_unshift($blocks, $this->combine_script());
+ }
+
if (Session::instance()->get("debug")) {
if ($function != "admin_head") {
array_unshift(
diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php
new file mode 100644
index 00000000..659b24dc
--- /dev/null
+++ b/modules/gallery/libraries/Gallery_View.php
@@ -0,0 +1,89 @@
+<?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 Gallery_View_Core extends View {
+ protected $theme_name = null;
+ protected $scripts = array();
+
+ /**
+ * Add a script to the combined scripts list.
+ * @param $file the relative path to a script from the gallery3 directory
+ */
+ public function script($file) {
+ $this->scripts[$file] = 1;
+ }
+
+ /**
+ * Add a script to the combined scripts list.
+ * @param $file the relative path to a script from the base of the active theme
+ * @param
+ */
+ public function theme_script($file) {
+ $file = "themes/{$this->theme_name}/$file";
+ $this->scripts[$file] = 1;
+ }
+
+ /**
+ * Provide a url to a resource within the current theme. This allows us to refer to theme
+ * resources without naming the theme itself which makes themes easier to copy.
+ */
+ public function theme_url($path, $absolute_url=false) {
+ $arg = "themes/{$this->theme_name}/$path";
+ return $absolute_url ? url::abs_file($arg) : url::file($arg);
+ }
+
+ /**
+ * Combine a series of Javascript files into a single one and cache it in the database, then
+ * return a single <script> element to refer to it.
+ */
+ protected function combine_script() {
+ $links = array();
+ $key = "";
+ foreach (array_keys($this->scripts) as $file) {
+ $path = DOCROOT . $file;
+ if (file_exists($path)) {
+ $stats = stat($path);
+ $links[] = $path;
+ // 7 == size, 9 == mtime, see http://php.net/stat
+ $key = "{$key}$file $stats[7] $stats[9],";
+ } else {
+ Kohana::log("alert", "Javascript file missing: " . $file);
+ }
+ }
+
+ $key = md5($key);
+ $cache = Cache::instance();
+ $contents = $cache->get($key);
+ if (empty($contents)) {
+ $contents = "";
+ foreach ($links as $link) {
+ $contents .= file_get_contents($link);
+ }
+ $cache->set($key, $contents, array("javascript"), 30 * 84600);
+ if (function_exists("gzencode")) {
+ $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
+ array("javascript", "gzip"), 30 * 84600);
+ }
+ }
+
+ // Handcraft the script link because html::script will add a .js extenstion
+ return "<script type=\"text/javascript\" src=\"" . url::site("combined/javascript/$key") .
+ "\"></script>";
+ }
+} \ No newline at end of file
diff --git a/modules/gallery/libraries/Menu.php b/modules/gallery/libraries/Menu.php
index 83bd1616..6d0881ce 100644
--- a/modules/gallery/libraries/Menu.php
+++ b/modules/gallery/libraries/Menu.php
@@ -23,6 +23,11 @@ class Menu_Element {
public $css_id;
public $css_class;
public $id;
+ public $type;
+
+ public function __construct($type) {
+ $this->type = $type;
+ }
/**
* Set the id
@@ -125,26 +130,38 @@ class Menu_Core extends Menu_Element {
public static function factory($type) {
switch($type) {
case "link":
- return new Menu_Element_Link();
+ return new Menu_Element_Link($type);
case "dialog":
- return new Menu_Element_Dialog();
+ return new Menu_Element_Dialog($type);
case "root":
- $menu = new Menu();
- $menu->is_root = true;
- return $menu;
+ return new Menu("root");
case "submenu":
- return new Menu();
+ return new Menu("submenu");
default:
throw Exception("@todo UNKNOWN_MENU_TYPE");
}
}
- public function __construct() {
+ public function compact() {
+ foreach ($this->elements as $target_id => $element) {
+ if ($element->type == "submenu") {
+ if (empty($element->elements)) {
+ $this->remove($target_id);
+ } else {
+ $element->compact();
+ }
+ }
+ }
+ }
+
+ public function __construct($type) {
+ parent::__construct($type);
$this->elements = array();
+ $this->is_root = $type == "root";
}
/**
diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php
index 31c2faa7..8e320f44 100644
--- a/modules/gallery/libraries/Theme_View.php
+++ b/modules/gallery/libraries/Theme_View.php
@@ -17,9 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
-class Theme_View_Core extends View {
- private $theme_name = null;
-
+class Theme_View_Core extends Gallery_View {
/**
* Attempts to load a view and pre-load view data.
*
@@ -68,11 +66,6 @@ class Theme_View_Core extends View {
return module::get_var("gallery", "thumb_size", 200) / 200;
}
- public function url($path, $absolute_url=false) {
- $arg = "themes/{$this->theme_name}/$path";
- return $absolute_url ? url::abs_file($arg) : url::file($arg);
- }
-
public function item() {
return $this->item;
}
@@ -85,10 +78,6 @@ class Theme_View_Core extends View {
return $this->page_type;
}
- public function display($page_name, $view_class="View") {
- return new $view_class($page_name);
- }
-
public function site_menu() {
$menu = Menu::factory("root");
if ($this->page_type != "login") {
@@ -105,6 +94,7 @@ class Theme_View_Core extends View {
}
}
+ $menu->compact();
print $menu;
}
@@ -120,16 +110,20 @@ class Theme_View_Core extends View {
$this->_menu("photo");
}
- private function _menu($type) {
+ public function thumb_menu($item) {
+ $this->_menu("thumb", $item);
+ }
+
+ private function _menu($type, $item=null) {
$menu = Menu::factory("root");
- call_user_func_array(array("gallery_menu", $type), array(&$menu, $this));
+ call_user_func_array(array("gallery_menu", $type), array(&$menu, $this, $item));
foreach (module::active() as $module) {
if ($module->name == "gallery") {
continue;
}
$class = "{$module->name}_menu";
if (method_exists($class, $type)) {
- call_user_func_array(array($class, $type), array(&$menu, $this));
+ call_user_func_array(array($class, $type), array(&$menu, $this, $item));
}
}
@@ -191,7 +185,28 @@ class Theme_View_Core extends View {
case "thumb_info":
case "thumb_top":
$blocks = array();
+ if (method_exists("gallery_theme", $function)) {
+ switch (count($args)) {
+ case 0:
+ $blocks[] = gallery_theme::$function($this);
+ break;
+ case 1:
+ $blocks[] = gallery_theme::$function($this, $args[0]);
+ break;
+ case 2:
+ $blocks[] = gallery_theme::$function($this, $args[0], $args[1]);
+ break;
+ default:
+ $blocks[] = call_user_func_array(
+ array("gallery_theme", $function),
+ array_merge(array($this), $args));
+ }
+
+ }
foreach (module::active() as $module) {
+ if ($module->name == "gallery") {
+ continue;
+ }
$helper_class = "{$module->name}_theme";
if (method_exists($helper_class, $function)) {
$blocks[] = call_user_func_array(
@@ -199,6 +214,11 @@ class Theme_View_Core extends View {
array_merge(array($this), $args));
}
}
+
+ if ($function == "head") {
+ array_unshift($blocks, $this->combine_script());
+ }
+
if (Session::instance()->get("debug")) {
if ($function != "head") {
array_unshift(
diff --git a/modules/gallery/libraries/drivers/Cache/Database.php b/modules/gallery/libraries/drivers/Cache/Database.php
new file mode 100644
index 00000000..f3a1eb02
--- /dev/null
+++ b/modules/gallery/libraries/drivers/Cache/Database.php
@@ -0,0 +1,179 @@
+<?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.
+ */
+/*
+ * Based on the Cache_Sqlite_Driver developed by the Kohana Team
+ */
+class Cache_Database_Driver implements Cache_Driver {
+ // Kohana database instance
+ protected $db;
+
+ /**
+ * Tests that the storage location is a directory and is writable.
+ */
+ public function __construct() {
+ // Open up an instance of the database
+ $this->db = Database::instance();
+
+ if (!$this->db->table_exists("caches")) {
+ throw new Exception("@todo Cache table is not defined");
+ }
+ }
+
+ /**
+ * Checks if a cache id is already set.
+ *
+ * @param string cache id
+ * @return boolean
+ */
+ public function exists($id) {
+ $count = $this->db->count_records("caches", array("key" => $id, "expiration >=" => time()));
+ return $count > 0;
+ }
+
+ /**
+ * Sets a cache item to the given data, tags, and lifetime.
+ *
+ * @param string cache id to set
+ * @param string data in the cache
+ * @param array cache tags
+ * @param integer lifetime
+ * @return bool
+ */
+ public function set($id, $data, array $tags = NULL, $lifetime) {
+ if (!empty($tags)) {
+ // Escape the tags, adding brackets so the tag can be explicitly matched
+ $tags = "<" . implode(">,<", $tags) . ">";
+ }
+
+ // Cache Database driver expects unix timestamp
+ if ($lifetime !== 0) {
+ $lifetime += time();
+ }
+
+ if ($this->exists($id)) {
+ $status = $this->db->update("caches",
+ array("tags" => $tags, "expiration" => $lifetime, "cache" => $data), array("key" => $id));
+ } else {
+ $status = $this->db->insert("caches",
+ array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => $data));
+ }
+
+ return count($status) > 0;
+ }
+
+ /**
+ * Finds an array of ids for a given tag.
+ *
+ * @param string tag name
+ * @return array of ids that match the tag
+ */
+ public function find($tag) {
+ $db_result = $this->db->from("caches")
+ ->like("tags", "<$tag>")
+ ->get()
+ ->result(true);
+
+ // An array will always be returned
+ $result = array();
+
+ if ($db_result->count() > 0) {
+ // Disable notices for unserializing
+ $ER = error_reporting(~E_NOTICE);
+
+ foreach ($db_result as $row) {
+ // Add each cache to the array
+ $result[$row->id] = $row->cache;
+ }
+
+ // Turn notices back on
+ error_reporting($ER);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Fetches a cache item. This will delete the item if it is expired or if
+ * the hash does not match the stored hash.
+ *
+ * @param string cache id
+ * @return mixed|NULL
+ */
+ public function get($id) {
+ $data = null;
+ $result = $this->db->getwhere("caches", array("key" => $id));
+
+ if (count($result) > 0) {
+ $cache = $result->current();
+ // Make sure the expiration is valid and that the hash matches
+ if ($cache->expiration != 0 && $cache->expiration <= time()) {
+ // Cache is not valid, delete it now
+ $this->delete($cache->id);
+ } else {
+ // Disable notices for unserializing
+ $ER = error_reporting(~E_NOTICE);
+
+ // Return the valid cache data
+ $data = $cache->cache;
+
+ // Turn notices back on
+ error_reporting($ER);
+ }
+ }
+
+ return $data;
+ }
+
+ /**
+ * Deletes a cache item by id or tag
+ *
+ * @param string cache id or tag, or true for "all items"
+ * @param bool delete a tag
+ * @return bool
+ */
+ public function delete($id, $tag = false) {
+ $this->db->from("caches");
+ if ($id === true) {
+ $this->db->where(1);
+ // Delete all caches
+ } else if ($tag === true) {
+ $this->db->like("tags", "<$id>");
+ } else {
+ $this->db->where("key", $id);
+ }
+
+ $status = $this->db->delete();
+
+ return count($status) > 0;
+ }
+
+ /**
+ * Deletes all cache files that are older than the current time.
+ */
+ public function delete_expired() {
+ // Delete all expired caches
+ $status = $this->db->from("caches")
+ ->where(array("expiration !=" => 0, "expiration <=" => time()))
+ ->delete();
+
+ return count($status) > 0;
+ }
+
+} // End Cache Database Driver \ No newline at end of file