https-bitbucket.org-bityvip /libraries/gantry/core/gantrygzipper.class.php

Language PHP Lines 253
MD5 Hash 253ea0681ac2df931c55355a941aada1 Estimated Cost $4,724 (why?)
Repository https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
 * @package   gantry
 * @subpackage core
 * @version   3.2.22 August 3, 2012
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 *
 * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
 *
 */
defined('GANTRY_VERSION') or die();

gantry_import('core.gantrysingleton');

/**
 * @package   gantry
 * @subpackage core
 */
class GantryGZipper {
    function process() {

    }

    function processCSSFiles() {
        global $gantry;

        $cache_time = $gantry->get("gzipper-time");
        $expires_time = $gantry->get("gzipper-expirestime", 1440);
        $strip_css = $gantry->get("gzipper-stripwhitespace", 1);

        $grouped_priories = array();
        $output = array();

        foreach ($gantry->_styles as $priorities) {
            foreach ($priorities as $links) {
                $css_links[$links->path] = $links->url;
            }
        }

//		$css_links = $gantry->_styles;

        ksort($gantry->_styles);

        foreach ($gantry->_styles as $style_priority => $styles) {
            $order_keeper = 0;
            $bump_ok = false;
            foreach ($styles as $style_entry) {
                if ($style_entry->type == 'url') {
                    $directory = 'REMOTE_URL';
                    $filename = $style_entry->url;
                    $bump_ok = true;
                }
                else {
                    $directory = dirname($style_entry->path);
                    $filename = basename($style_entry->path);
                }
                $grouped_priories[$style_priority][$order_keeper][$directory][$filename] = $style_entry;
                if ($bump_ok) {
                    $order_keeper++;
                    $bump_ok = false;
                }
            }
        }
        foreach ($grouped_priories as $priority => $order_kept_entries) {
            foreach ($order_kept_entries as $ordered_files) {
                foreach ($ordered_files as $dir => $files) {
                    // Process full urls
                    if ($dir == 'REMOTE_URL') {
                        foreach ($files as $file => $link) {
                            $gantry->document->addStyleSheet($link->url);
                        }
                        continue;
                    }
                    // Process 
                    else {

                        if (!is_writable($dir)) {
                            foreach ($files as $css_file) {
                                $gantry->document->addStyleSheet($css_file->url);
                            }
                            continue;
                        }
                        $md5sum = "";
                        $path = "";

                        jimport('joomla.filesystem.file');

                        //first trip through to build filename
                        foreach ($files as $file => $details) {
                            $md5sum .= md5($details->url);
                            $detailspath = $dir . DS . $file;
                            if (JFile::exists($detailspath)) {
                                $path = dirname($details->url);
                            }
                        }

                        $cache_filename = "css-" . md5($md5sum) . ".php";
                        $cache_fullpath = $dir . DS . $cache_filename;

                        //see if file is stale
                        if (JFile::exists($cache_fullpath)) {
                            $diff = (time() - filectime($cache_fullpath));
                        } else {
                            $diff = $cache_time + 1;
                        }

                        if ($diff > $cache_time) {
                            $outfile = GantryGZipper::_getOutHeader("css", $expires_time);
                            foreach ($files as $file => $details) {
                                $detailspath = $dir . DS . $file;

                                if (JFile::exists($detailspath)) {
                                    $css_content = JFile::read($detailspath);
                                    if ($strip_css) {
                                        $css_content = GantryGZipper::_stripCSSWhiteSpace($css_content);
                                    }
                                    $outfile .= "\n\n/*** " . $file . " ***/\n\n" . $css_content;
                                }
                            }
                            JFile::write($cache_fullpath, $outfile);
                        }

                        $cache_file_name = $path . "/" . $cache_filename;
                        $gantry->document->addStyleSheet($cache_file_name);
                    }
                }
            }
        }
    }

    function processJsFiles() {
        global $gantry;

        $path = $gantry->basePath;
        $cache_time = $gantry->get("gzipper-time");
        $expires_time = $gantry->get("gzipper-expirestime", 1440);


        $ordered_files = array();
        $output = array();
        $md5sum = "";

        $script_tags = $gantry->_scripts;

        foreach ($script_tags as $filepath => $file) {
            $md5sum .= md5($filepath);
            $ordered_files[] = array(dirname($filepath), basename($filepath), $file);
        }

        if (!is_writable(JPATH_CACHE)) {
            foreach ($this->_scripts as $js_file) {
                $gantry->document->addScript($js_file);
            }
            return;
        }

        if (count($ordered_files) > 0) {
            $cache_filename = "js-" . md5($md5sum) . ".php";
            $cache_fullpath = JPATH_CACHE . DS . $cache_filename;


            //see if file is stale
            if (JFile::exists($cache_fullpath)) {
                $diff = (time() - filectime($cache_fullpath));
            } else {
                $diff = $cache_time + 1;
            }

            if ($diff > $cache_time) {
                $outfile = GantryGZipper::_getOutHeader("js", $expires_time);
                foreach ($ordered_files as $files) {
                    $dir = $files[0];
                    $filename = $files[1];
                    $details = $files[2];

                    $detailspath = $dir . DS . $filename;
                    if (JFile::exists($detailspath)) {
                        $jsfile = JFile::read($detailspath);
                        // fix for stupid joolma code
                        if (strpos($filename, 'joomla.javascript.js')!==false or strpos($filename, 'mambojavascript.js')!==false) {
                            $jsfile = str_replace("// <?php !!", "// ", $jsfile);
                        }
                        $jsfile = self::cleanEndLines($jsfile);
                        $outfile .= "\n\n/*** " . $filename . " ***/\n\n" . $jsfile;

                    }
                }
                JFile::write($cache_fullpath, $outfile);
            }

            $cache_file_name = $path . "/cache/" . $cache_filename;
            $cache_url_name = $gantry->baseUrl . "cache/" . $cache_filename;
            $gantry->document->addScript($cache_url_name);
        }
    }

    function cleanEndLines($data){
        $file_lines = explode("\n",$data);
        while (($line = array_pop($file_lines)) != null){
            $clean_line = rtrim($line);
            if (strlen($clean_line) > 0){
                $end_char = substr($line, strlen($clean_line), 1);
                array_push($file_lines,$line);
                if ($end_char != ';'){
                    array_push($file_lines, ";");
                }
                break;
            }
        }
        return implode($file_lines,"\n");
    }



    function _getOutHeader($type = "css", $expires_time = 1440) {
        if ($type == "css") {
            $header = '<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$expires_time = ' . $expires_time . ';
$offset = 60 * $expires_time ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
                ?>';
        } else {
            $header = '<?php
ob_start ("ob_gzhandler");
header("Content-type: application/x-javascript; charset: UTF-8");
header("Cache-Control: must-revalidate");
$expires_time = ' . $expires_time . ';
$offset = 60 * $expires_time ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
                ?>';
        }
        return $header;
    }

    function _stripCSSWhiteSpace($css_content) {
        // remove comments
        $css_content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css_content);
        // remove tabs, spaces, newlines, etc.
        $css_content = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $css_content);
        return $css_content;
    }
}
Back to Top