1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21/**
22 * Pack many Javascript files together and download them all at once.
23 *
24 * @package GalleryCore
25 * @subpackage UserInterface
26 * @author Bharat Mediratta <bharat@menalto.com>
27 * @version $Revision: 17785 $
28 */
29class CombinedJavascriptView extends GalleryView {
30    /**
31     * @see GalleryView::isImmediate
32     */
33    function isImmediate() {
34	return true;
35    }
36
37    /**
38     * @see GalleryView::renderImmediate
39     */
40    function renderImmediate() {
41	global $gallery;
42	$platform =& $gallery->getPlatform();
43	$phpVm = $gallery->getPhpVm();
44
45	$ifModifiedSince = GalleryUtilities::getServerVar('HTTP_IF_MODIFIED_SINCE');
46	if (isset($ifModifiedSince)
47	        || ($phpVm->function_exists('getallheaders')
48		    && ($headers = $phpVm->getAllHeaders())
49		    && (isset($headers['If-Modified-Since'])
50			|| isset($headers['If-modified-since'])))) {
51	    $phpVm->header('HTTP/1.0 304 Not Modified');
52	    return null;
53	}
54
55	$key = GalleryUtilities::getRequestVariables('key');
56	if (preg_match('/[^0-9a-f]/', $key)) {
57	    /* The key can't contain non-hex, so just terminate early */
58	    return null;
59	}
60
61 	if (strpos(GalleryUtilities::getServerVar('HTTP_ACCEPT_ENCODING'), 'gzip') !== false) {
62	    $cacheData =& GalleryDataCache::getFromDisk(
63		array('type' => 'module',
64		      'itemId' => 'CombinedJavascript_gzip_' . $key,
65		      'id' => '_all'));
66	    if (isset($cacheData)) {
67		/**
68		 * Try to prevent Apache's mod_deflate from gzipping the output a 2nd time
69		 * since broken versions of mod_deflate sometimes get the byte count wrong.
70		 */
71		if ($phpVm->function_exists('apache_setenv')
72		        && !@$gallery->getConfig('apacheSetenvBroken')) {
73		    @apache_setenv('no-gzip', '1');
74		}
75
76		$phpVm->header('Content-Encoding: gzip');
77		/*
78		 * Cache control is public for proxies that handle gzip properly.
79		 * Private for the ones that don't.
80		 * http://www.mnot.net/blog/2005/05/12/google_cache
81		 */
82		$phpVm->header('Cache-Control: private, x-gzip-ok="public"');
83	    }
84 	}
85
86	if (!isset($cacheData)) {
87	    $cacheData =& GalleryDataCache::getFromDisk(
88		array('type' => 'module',
89		      'itemId' => 'CombinedJavascript_' . $key,
90		      'id' => '_all'));
91
92	    if (isset($cacheData)) {
93		$phpVm->header('Cache-Control: public');
94	    }
95	}
96
97	if (isset($cacheData)) {
98	    $phpVm->header('Content-type: text/javascript; charset=UTF-8');
99	    $phpVm->header('Last-Modified: ' . $gallery->getHttpDate(1));
100	    $phpVm->header('Expires: ' . $gallery->getHttpDate(2147483647));
101
102	    print $cacheData;
103	} else {
104	    return GalleryCoreApi::addEventLogEntry(
105		'core.CombinedJavascript', 'Unable to read combined cache file', $key);
106	}
107	return null;
108    }
109
110    /**
111     * @see GalleryView::autoCacheControl
112     */
113    function autoCacheControl() {
114	return false;
115    }
116
117    /**
118     * @see GalleryView::isAllowedInEmbedOnly
119     */
120    function isAllowedInEmbedOnly() {
121	return true;
122    }
123}
124?>