commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / kcfinder / core / class / minifier.php
1 <?php
2
3 /** This file is part of KCFinder project
4 *
5 * @desc Minify JS & CSS
6 * @package KCFinder
7 * @version 3.12
8 * @author Pavel Tzonkov <sunhater@sunhater.com>
9 * @copyright 2010-2014 KCFinder Project
10 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
11 * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12 * @link http://kcfinder.sunhater.com
13 */
14
15 namespace kcfinder;
16
17 class minifier {
18
19 protected $config;
20 protected $type = "js";
21 protected $minCmd = "";
22 protected $mime = array(
23 'js' => "text/javascript",
24 'css' => "text/css"
25 );
26
27 public function __construct($type=null) {
28 require "conf/config.php";
29 $this->config = $_CONFIG;
30 $type = strtolower($type);
31 if (isset($this->mime[$type]))
32 $this->type = $type;
33 if (isset($_CONFIG["_{$this->type}MinCmd"]))
34 $this->minCmd = $_CONFIG["_{$this->type}MinCmd"];
35 }
36
37 public function minify($cacheFile=null, $dir=null) {
38 if ($dir === null)
39 $dir = dirname($_SERVER['SCRIPT_FILENAME']);
40
41 // MODIFICATION TIME FILES
42 $mtFiles = array(
43 __FILE__,
44 $_SERVER['SCRIPT_FILENAME'],
45 "conf/config.php"
46 );
47
48 // GET SOURCE CODE FILES
49 $files = dir::content($dir, array(
50 'types' => "file",
51 'pattern' => '/^.*\.' . $this->type . '$/'
52 ));
53
54 // GET NEWEST MODIFICATION TIME
55 $mtime = 0;
56 foreach (array_merge($mtFiles, $files) as $file) {
57 $fmtime = filemtime($file);
58 if ($fmtime > $mtime)
59 $mtime = $fmtime;
60 }
61
62 $header = "Content-Type: {$this->mime[$this->type]}";
63
64 // GET SOURCE CODE FROM CLIENT HTTP CACHE IF EXISTS
65 httpCache::checkMTime($mtime, $header);
66
67 // OUTPUT SOURCE CODE
68 header($header);
69
70 // GET SOURCE CODE FROM SERVER-SIDE CACHE
71 if (($cacheFile !== null) &&
72 file_exists($cacheFile) &&
73 (
74 (filemtime($cacheFile) >= $mtime) ||
75 !is_writable($cacheFile) // if cache file cannot be modified
76 ) // the script will output it always
77 ) { // with its distribution content
78 readfile($cacheFile);
79 die;
80 }
81
82 // MINIFY AND JOIN SOURCE CODE
83 $source = "";
84 foreach ($files as $file) {
85
86 if (strlen($this->minCmd) && (substr($file, 4, 1) != "_")) {
87 $cmd = str_replace("{file}", $file, $this->minCmd);
88 $source .= `$cmd`;
89
90 } else
91 $source .= file_get_contents($file);
92 }
93
94 // UPDATE SERVER-SIDE CACHE
95 if (($cacheFile !== null) &&
96 (
97 is_writable($cacheFile) ||
98 (
99 !file_exists($cacheFile) &&
100 dir::isWritable(dirname($cacheFile))
101 )
102 )
103 ) {
104 file_put_contents($cacheFile, $source);
105 touch($cacheFile, $mtime);
106 }
107
108 // OUTPUT SOURCE CODE
109 echo $source;
110
111 }
112 }
113
114 ?>