diff options
Diffstat (limited to 'modules/gallery/libraries')
-rw-r--r-- | modules/gallery/libraries/Admin_View.php | 18 | ||||
-rw-r--r-- | modules/gallery/libraries/Gallery_View.php | 160 | ||||
-rw-r--r-- | modules/gallery/libraries/HtmlPurifier.php | 38 | ||||
-rw-r--r-- | modules/gallery/libraries/Menu.php | 9 | ||||
-rw-r--r-- | modules/gallery/libraries/Theme_View.php | 58 | ||||
-rw-r--r-- | modules/gallery/libraries/drivers/Cache/Database.php | 181 |
6 files changed, 430 insertions, 34 deletions
diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 7a7396eb..47770a90 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); @@ -109,6 +98,11 @@ class Admin_View_Core extends View { } } + if ($function == "admin_head") { + array_unshift($blocks, $this->combine_files($this->css, "css")); + array_unshift($blocks, $this->combine_files($this->scripts, "javascript")); + } + 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..133066d7 --- /dev/null +++ b/modules/gallery/libraries/Gallery_View.php @@ -0,0 +1,160 @@ +<?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(); + protected $css = 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); + } + + /** + * Add a css file to the combined css list. + * @param $file the relative path to a script from the gallery3 directory + */ + public function css($file, $theme_relative=false) { + $this->css[$file] = 1; + } + + /** + * Add a css file to the combined css list. + * @param $file the relative path to a script from the base of the active theme + * @param + */ + public function theme_css($file) { + $file = "themes/{$this->theme_name}/$file"; + $this->css[$file] = 1; + } + + /** + * Combine a series of files into a single one and cache it in the database. + */ + protected function combine_files($files, $type) { + $links = array(); + + // Include the url in the cache key so that if the Gallery moves, we don't use old cached + // entries. + $key = array(url::abs_file("")); + + foreach (array_keys($files) as $file) { + $path = DOCROOT . $file; + if (file_exists($path)) { + $stats = stat($path); + $links[$file] = $path; + // 7 == size, 9 == mtime, see http://php.net/stat + $key[] = "$file $stats[7] $stats[9]"; + } else { + Kohana::log("error", "missing file ($type): $file"); + } + } + + $key = md5(join(" ", $key)); + $cache = Cache::instance(); + $contents = $cache->get($key); + + if (empty($contents)) { + $contents = ""; + foreach ($links as $file => $link) { + if ($type == "css") { + $contents .= "/* $file */\n" . $this->process_css($link) . "\n"; + } else { + $contents .= "/* $file */\n" . file_get_contents($link) . "\n"; + } + } + + $cache->set($key, $contents, array($type), 30 * 84600); + if (function_exists("gzencode")) { + $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array($type, "gzip"), 30 * 84600); + } + } + + if ($type == "css") { + return "<!-- LOOKING FOR YOUR CSS? It's all been combined into the link below -->\n" . + html::stylesheet("combined/css/$key", "screen,print,projection", true); + } else { + return "<!-- LOOKING FOR YOUR JAVASCRIPT? It's all been combined into the link below -->\n" . + html::script("combined/javascript/$key", true); + } + } + + /** + * Convert relative references inside a CSS file to absolute ones so that when it's served from + * a new location as part of a combined bundle the references are still correct. + * @param string the path to the css file + */ + private function process_css($css_file) { + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + $docroot_length = strlen(DOCROOT); + + $css = file_get_contents($css_file); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($css_file) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $search[] = $match[0]; + $replace[] = "url('" . url::abs_file($relative) . "')"; + } else { + Kohana::log("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'"); + } + } + $replace = str_replace(DIRECTORY_SEPARATOR, "/", $replace); + $css = str_replace($search, $replace, $css); + } + $imports = preg_match_all("#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#", + $css, $matches, PREG_SET_ORDER); + + if ($imports) { + $search = $replace = array(); + foreach ($matches as $match) { + $search[] = $match[0]; + $replace[] = $this->process_css(dirname($css_file) . "/$match[1]"); + } + $css = str_replace($search, $replace, $css); + } + + return $css; + } +}
\ No newline at end of file diff --git a/modules/gallery/libraries/HtmlPurifier.php b/modules/gallery/libraries/HtmlPurifier.php new file mode 100644 index 00000000..daa5896e --- /dev/null +++ b/modules/gallery/libraries/HtmlPurifier.php @@ -0,0 +1,38 @@ +<?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 HtmlPurifier_Core { + private static $_instance; + + static function instance($config=null) { + require_once(dirname(__file__) . "/HTMLPurifier/HTMLPurifier.auto.php"); + if (self::$_instance == NULL) { + $config = isset($config) ? $config : Kohana::config('purifier'); + $purifier_config = HTMLPurifier_Config::createDefault(); + foreach ($config as $category => $key_value) { + foreach ($key_value as $key => $value) { + $purifier_config->set("$category.$key", $value); + } + } + self::$_instance = new HtmlPurifier($purifier_config); + } + + return self::$_instance; + } +} diff --git a/modules/gallery/libraries/Menu.php b/modules/gallery/libraries/Menu.php index 6d0881ce..a39b59a5 100644 --- a/modules/gallery/libraries/Menu.php +++ b/modules/gallery/libraries/Menu.php @@ -136,7 +136,9 @@ class Menu_Core extends Menu_Element { return new Menu_Element_Dialog($type); case "root": - return new Menu("root"); + $menu = new Menu("root"); + $menu->css_class("gMenu"); + return $menu; case "submenu": return new Menu("submenu"); @@ -156,6 +158,7 @@ class Menu_Core extends Menu_Element { } } } + return $this; } public function __construct($type) { @@ -206,8 +209,8 @@ class Menu_Core extends Menu_Element { } public function __toString() { - $html = $this->is_root ? "<ul class=\"gMenu\">" : - "<li><a href=#>$this->label</a><ul class=\"gMenu\">"; + $html = $this->is_root ? "<ul class=\"$this->css_class\">" : + "<li title=\"$this->label\"><a href=\"#\">$this->label</a><ul>"; $html .= implode("\n", $this->elements); $html .= $this->is_root ? "</ul>" : "</ul></li>"; return $html; diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 7b2ca840..fa45ec89 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") { @@ -110,31 +99,35 @@ class Theme_View_Core extends View { } public function album_menu() { - $this->_menu("album"); + print $this->_menu("album"); } public function tag_menu() { - $this->_menu("tag"); + print $this->_menu("tag"); } public function photo_menu() { - $this->_menu("photo"); + print $this->_menu("photo"); } - private function _menu($type) { + public function thumb_menu($item) { + print $this->_menu("thumb", $item)->css_class("gThumbMenu"); + } + + 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)); } } - print $menu; + return $menu->compact(); } public function pager() { @@ -192,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( @@ -200,6 +214,12 @@ class Theme_View_Core extends View { array_merge(array($this), $args)); } } + + if ($function == "head") { + array_unshift($blocks, $this->combine_files($this->css, "css")); + array_unshift($blocks, $this->combine_files($this->scripts, "javascript")); + } + 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..43f4e38a --- /dev/null +++ b/modules/gallery/libraries/drivers/Cache/Database.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. + */ +/* + * 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" => serialize($data)), array("key" => $id)); + } else { + $status = $this->db->insert( + "caches", + array("key" => $id, "tags" => $tags, "expiration" => $lifetime, "cache" => serialize($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->key] = unserialize($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 = unserialize($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 |