diff options
-rw-r--r-- | modules/comment/helpers/comment_theme.php | 4 | ||||
-rw-r--r-- | modules/gallery/controllers/javascript.php | 69 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_theme.php | 34 | ||||
-rw-r--r-- | modules/gallery/libraries/Theme_View.php | 70 | ||||
-rw-r--r-- | themes/admin_default/views/admin.html.php | 10 | ||||
-rw-r--r-- | themes/default/views/movie.html.php | 1 | ||||
-rw-r--r-- | themes/default/views/page.html.php | 12 | ||||
-rw-r--r-- | themes/default/views/photo.html.php | 1 |
8 files changed, 169 insertions, 32 deletions
diff --git a/modules/comment/helpers/comment_theme.php b/modules/comment/helpers/comment_theme.php index d9f1acf4..89b2f57c 100644 --- a/modules/comment/helpers/comment_theme.php +++ b/modules/comment/helpers/comment_theme.php @@ -19,8 +19,8 @@ */ class comment_theme_Core { static function head($theme) { - $url = url::file("modules/comment/js/comment.js"); - return "<script src=\"$url\" type=\"text/javascript\"></script>\n"; + $theme->script("modules/comment/js/comment.js"); + return ""; } static function photo_bottom($theme) { diff --git a/modules/gallery/controllers/javascript.php b/modules/gallery/controllers/javascript.php new file mode 100644 index 00000000..bc231e0a --- /dev/null +++ b/modules/gallery/controllers/javascript.php @@ -0,0 +1,69 @@ +<?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 Javascript_Controller extends Controller { + public function combined($key) { + if (preg_match('/[^0-9a-f]/', $key)) { + // The key can't contain non-hex, so just terminate early + Kohana::show_404(); + } + + // We don't need to save the session for this request + Session::abort_save(); + + // Dump out the javascript file + $cache = Cache::instance(); + + $modified = $cache->get("{$key}_modified"); + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && !empty($modified)) { + header('HTTP/1.0 304 Not Modified'); + return; + } + + $content = ""; + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { + $content = $cache->get("{$key}_gz"); + } + + if (empty($content)) { + $content = $cache->get($key); + } + + if (empty($content)) { + Kohana::show_404(); + } + + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { + header("Content-Encoding: gzip"); + header("Cache-Control: private, x-gzip-ok=\"public\""); + } + + header("Content-Type: text/javascript; charset=UTF-8"); + + header("Expires: " . gmdate(21474383647)); + header("Last-Modified: " . $modified); + + Kohana::close_buffers(false); + + $handle = fopen("php://output", "wb"); + fwrite($handle, $content); + fclose($handle); + } +} + diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index a96c8f5b..b6b24b27 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -21,6 +21,22 @@ class gallery_theme_Core { static function head($theme) { $session = Session::instance(); $buf = ""; + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/gallery.form.js"); + $theme->script("lib/superfish/js/superfish.js"); + if ($theme->page_type == 'photo') { + $theme->script("lib/jquery.scrollTo.js"); + $theme->script("lib/jquery.localscroll.js"); + $theme->script("lib/gallery.show_full_size.js"); + } + if ($theme->page_type == 'movie') { + $theme->script("lib/flowplayer.js"); + } + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/debug.css") . "\" />"; @@ -29,7 +45,7 @@ class gallery_theme_Core { && access::can("edit", $theme->item())) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/quick.css") . "\" />"; - $buf .= html::script("modules/gallery/js/quick.js"); + $theme->script("modules/gallery/js/quick.js"); } if (module::is_active("rss")) { @@ -43,8 +59,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/l10n_client.css") . "\" />"; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; @@ -79,6 +95,14 @@ class gallery_theme_Core { static function admin_head($theme) { $session = Session::instance(); $buf = ""; + $theme->script("lib/jquery.js"); + $theme->script("lib/jquery.form.js"); + $theme->script("lib/jquery-ui.js"); + $theme->script("lib/gallery.common.js"); + $theme->script("lib/gallery.dialog.js"); + $theme->script("lib/superfish/js/superfish.js"); + $theme->script($theme->url("js/jquery.dropshadow.js", false, true)); + $theme->script($theme->url("js/ui.init.js", false, true)); if ($session->get("debug")) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/debug.css") . "\" />"; @@ -87,8 +111,8 @@ class gallery_theme_Core { if ($session->get("l10n_mode", false)) { $buf .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . url::file("modules/gallery/css/l10n_client.css") . "\" />"; - $buf .= html::script("lib/jquery.cookie.js"); - $buf .= html::script("modules/gallery/js/l10n_client.js"); + $theme->script("lib/jquery.cookie.js"); + $theme->script("modules/gallery/js/l10n_client.js"); } return $buf; diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 4612a74b..f55cb55a 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -19,6 +19,7 @@ */ class Theme_View_Core extends View { private $theme_name = null; + private $scripts = array(); /** * Attempts to load a view and pre-load view data. @@ -68,9 +69,9 @@ class Theme_View_Core extends View { return module::get_var("gallery", "thumb_size", 200) / 200; } - public function url($path, $absolute_url=false) { + public function url($path, $absolute_url=false, $no_root=false) { $arg = "themes/{$this->theme_name}/$path"; - return $absolute_url ? url::abs_file($arg) : url::file($arg); + return $absolute_url ? url::abs_file($arg) : $no_root ? $arg : url::file($arg); } public function item() { @@ -167,6 +168,45 @@ class Theme_View_Core extends View { return message::get(); } + public function script($file) { + $this->scripts[$file] = 1; + } + + private 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("warn", "Javascript file missing: " . $file); + } + } + + $key = md5($key); + $contents = Cache::instance()->get($key); + if (empty($contents)) { + $contents = ''; + foreach ($links as $link) { + $contents .= file_get_contents($link); + } + Cache::instance()->set($key, $contents, array("javascript"), 84600); + if (function_exists("gzencode")) { + Cache::instance()->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("javascript", "gzip"), 84600); + } + Cache::instance()->set("{$key}_modified", time(), array("javascript", "modified"), 84600); + } + + // Handcraft the script link because html::script will add a .js extenstion + return "<script type=\"text/javascript\" src=\"" . url::site("javascript/combined/$key") . + "\"></script>"; + } + /** * Handle all theme functions that insert module content. */ @@ -196,7 +236,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( @@ -204,6 +265,11 @@ class Theme_View_Core extends View { array_merge(array($this), $args)); } } + + if ($function == "head" || $function == "admin_head") { + array_unshift($blocks, $this->_combine_script()); + } + if (Session::instance()->get("debug")) { if ($function != "head") { array_unshift( diff --git a/themes/admin_default/views/admin.html.php b/themes/admin_default/views/admin.html.php index b7cfaa40..dfbafe31 100644 --- a/themes/admin_default/views/admin.html.php +++ b/themes/admin_default/views/admin.html.php @@ -20,15 +20,7 @@ <link rel="stylesheet" type="text/css" href="<?= $theme->url("css/fix-ie.css") ?>" media="screen,print,projection" /> <![endif]--> - <script src="<?= url::file("lib/jquery.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery-ui.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.common.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.dialog.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/superfish/js/superfish.js") ?>" type="text/javascript"></script> - <script src="<?= $theme->url("js/jquery.dropshadow.js") ?>" type="text/javascript"></script> - <script src="<?= $theme->url("js/ui.init.js") ?>" type="text/javascript"></script> - <?= $theme->admin_head() ?> + <?= $theme->admin_head() ?> </head> <body <?= $theme->body_attributes() ?>> diff --git a/themes/default/views/movie.html.php b/themes/default/views/movie.html.php index e8559697..134e3571 100644 --- a/themes/default/views/movie.html.php +++ b/themes/default/views/movie.html.php @@ -1,5 +1,4 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> -<script src="<?= url::file("lib/flowplayer.js") ?>" type="text/javascript"></script> <div id="gItem"> <?= $theme->photo_top() ?> diff --git a/themes/default/views/page.html.php b/themes/default/views/page.html.php index f6286f95..c74d4097 100644 --- a/themes/default/views/page.html.php +++ b/themes/default/views/page.html.php @@ -49,18 +49,6 @@ </style> <? endif ?> <? endif ?> - <script src="<?= url::file("lib/jquery.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery-ui.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.common.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.dialog.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/gallery.form.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/superfish/js/superfish.js") ?>" type="text/javascript"></script> - <? if ($theme->page_type == 'photo'): ?> - <script src="<?= url::file("lib/jquery.scrollTo.js") ?>" type="text/javascript"></script> - <script src="<?= url::file("lib/jquery.localscroll.js") ?>" type="text/javascript"></script> - <? endif ?> - <script src="<?= $theme->url("js/ui.init.js") ?>" type="text/javascript"></script> <?= $theme->head() ?> </head> diff --git a/themes/default/views/photo.html.php b/themes/default/views/photo.html.php index 1c3b81a7..4765a4e3 100644 --- a/themes/default/views/photo.html.php +++ b/themes/default/views/photo.html.php @@ -2,7 +2,6 @@ <? if (access::can("view_full", $theme->item())): ?> <!-- Use javascript to show the full size as an overlay on the current page --> -<script src="<?= url::file("lib/gallery.show_full_size.js") ?>" type="text/javascript"></script> <script> $(document).ready(function() { $(".gFullSizeLink").click(function() { |