Advertisement
englishextra

minify/

Sep 28th, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * shimansky.biz, mobile-english.ru
  4.  *
  5.  * Static web site core scripts
  6.  * @package shimansky.biz
  7.  * @author Serguei Shimansky <englishextra@yandex.ru>
  8.  * @copyright Serguei Shimansky 11:28 23.07.2011
  9.  * @access public
  10.  * @version 0.1
  11.  * @link https://bitbucket.org/englishextra/shimansky.biz
  12.  * @link https://bitbucket.org/englishextra/mobile-english.ru
  13.  * @link https://github.com/englishextra/shimansky.biz.git
  14.  * @link https://github.com/englishextra/mobile-english.ru.git
  15.  */
  16.  
  17. $relpa = ($relpa0 = preg_replace("/[\/]+/", "/", $_SERVER['DOCUMENT_ROOT'] . '/')) ? $relpa0 : '';
  18.  
  19. ob_start();
  20.  
  21. /**
  22.  * put these lines in .htaccess send unpacked gzip to browser
  23.  * php_flag zlib.output_compression On
  24.  * php_value zlib.output_compression_level 5
  25.  */
  26. function lib_local_print_gzipped_scripts($type, $expires_offset, $p) {
  27.  
  28.     $contenttype = '';
  29.     if ($type == 'js') {$contenttype = 'application/x-javascript';}
  30.     elseif ($type == 'css') {$contenttype = 'text/css';}
  31.  
  32.     header("Cache-Control: public, max-age=" . $expires_offset);
  33.     header("Expires: " . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . " GMT");
  34.     header("Content-Type: " . $contenttype . "; charset=UTF-8");
  35.  
  36.     if ( !ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
  37.         header('Vary: Accept-Encoding');
  38.         if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate')) {
  39.             header('Content-Encoding: deflate');
  40.             $p = gzdeflate( $p, 3 );
  41.         } elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode')) {
  42.             header('Content-Encoding: gzip');
  43.             $p = gzencode( $p, 3 );
  44.         }
  45.     }
  46.     echo $p;
  47. }
  48.  
  49. function lib_local_write_file ($data, $w, $type) {
  50.     if (!$fo = fopen($data, $type)) {
  51.         die('Cannot open file: ' . $data);
  52.     }
  53.     if (!is_writable($data)) {
  54.         die('Cannot write file: ' . $data);
  55.     }
  56.     flock($fo, LOCK_EX);
  57.     fputs($fo, $w);
  58.     fflush($fo);
  59.     flock($fo, LOCK_UN);
  60.     fclose($fo);
  61. }
  62.  
  63. function lib_local_fix_filename ($s) {
  64.     $s = str_replace(array(' ','\\','\/','_','-','.',',','!','(',')','[',']'), '', strtolower($s));
  65.     return $s;
  66. }
  67.  
  68. function lib_local_packCSS($s) {
  69.  
  70.     //$s = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $s);
  71.  
  72.     $s = str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", "", trim(@strval($s))));
  73.  
  74.     $a = array("/[\ ]+/s" => " ",
  75.                 "/\; \}/s" => "}",
  76.                 "/\;\}/s" => "}",
  77.                 "/\}\ /s" => "}",
  78.                 "/\: /s" => ":",
  79.                 "/\ \{/s" => "{",
  80.                 "/\{\ /s" => "{",
  81.                 "/\; /s" => ";",
  82.                 "/\,\ /s" => ",",
  83.                 "/\/\*(.*?)\*\//s" => "");
  84.  
  85.     foreach($a as $k => $v) {
  86.         $s = preg_replace($k, $v, $s);
  87.     }
  88.  
  89.     return $s;
  90. }
  91.  
  92. error_reporting(0);
  93.  
  94. /**
  95.  * get query string
  96.  */
  97. $type = isset($_GET['type']) ? $_GET['type'] : '';
  98. $load = isset($_GET['load']) ? $_GET['load'] : '';
  99. $static = isset($_GET['static']) ? $_GET['static'] : '';
  100. $method = isset($_GET['method']) ? $_GET['method'] : 'minify';
  101.  
  102. /**
  103.  * die if not enough parameters
  104.  */
  105. if (!$type && (!$load || !$static)) {die('/*empty query string*/');}
  106.  
  107. $hash_filepath = $relpa . 'temp/cache/' . $type . '_' . md5($load);
  108.  
  109. /**
  110.  * is mobile device? print empty js and handheld css
  111.  */
  112. require_once $relpa .'lib/lib_mobile_device_detect.inc';
  113.  
  114. $is_handheld = (mobile_device_detect()) ? 1 : '';
  115.  
  116. /**
  117.  * is search engine bot? print empty js and handheld css
  118.  */
  119. require_once $relpa .'lib/lib_browser.inc';
  120.  
  121. if (!isset($browser) || empty($browser)) {$browser = new Browser();}
  122.  
  123. $ua = ($ua0 = $browser->getBrowser()) ? $ua0 : '';
  124.  
  125. //$is_handheld = ($browser->isMobile()) ? 1 : ''; //not reliable with browser.php library
  126.  
  127. /**
  128.  * is search bot?
  129.  */
  130. $is_robot = ($browser->isRobot()) ? 1 : '';
  131.  
  132. /**
  133.  * if mobile device or search engine bot, print empty js and handheld css
  134.  */
  135. if (!empty($is_handheld) || !empty($is_robot) || !$ua) {
  136.  
  137.     $handheld_css_filepath = $relpa . 'css/handheld.css';
  138.  
  139.     $handheld_css = (file_exists($handheld_css_filepath)) ? file_get_contents($handheld_css_filepath) : '';
  140.  
  141.     $p = '';
  142.  
  143.     switch ($type) {
  144.  
  145.         case 'js':
  146.             $p = '';
  147.             break;
  148.  
  149.         case 'css':
  150.             $p = lib_local_packCSS($handheld_css);
  151.             break;
  152.     }
  153.  
  154.     lib_local_write_file($hash_filepath, $p, "w+");
  155.     lib_local_print_gzipped_scripts($type, 31536000, $p);
  156.     exit;
  157. }
  158.  
  159. /**
  160.  * read from cache when we are sure this is not mobile
  161.  */
  162. if (!empty($ua)) {$hash_filepath = $relpa . 'temp/cache/'
  163.     . lib_local_fix_filename ($ua)
  164.     . '_' . $type . '_' . md5($load);}
  165.  
  166. $hash_filepath_exits = ( file_exists($hash_filepath) ) ? 1 : '';
  167.  
  168. if ($hash_filepath_exits) {
  169.  
  170.     lib_local_print_gzipped_scripts($type, 31536000, '/* from cache file ' . str_replace($relpa . 'temp/cache/', '', $hash_filepath) . ' */' . "\n\n" . file_get_contents($hash_filepath));
  171.     exit;
  172. }
  173.  
  174. /**
  175.  * initialise output sting
  176.  */
  177. $p = '';
  178.  
  179. /**
  180.  * print static content first, in .htacces there mast be a rewrite rule:
  181.  * RewriteEngine On
  182.  * RewriteBase /
  183.  * RewriteRule ^(css|js)/([0-9a-z]+)\.(css|js)?$ $1/?static=$1/$2.$3&type=$3 [L]
  184.  */
  185. if (!empty($static) && !empty($type)) {
  186.  
  187.     $p = file_get_contents($relpa . $static);
  188.  
  189. /**
  190.  * print packed or minified
  191.  */
  192. } elseif (!empty($load) && !empty($type)) {
  193.  
  194.     require_once $relpa . 'lib/lib_packscripts.inc';
  195.  
  196.     $load = explode(',', preg_replace( '/[^a-z0-9\_\,\.\/]+/i', '', $load));
  197.  
  198.     // obfuscate content
  199.     if ($type == 'css') {
  200.  
  201.         switch ($method) {
  202.  
  203.             case 'pack':
  204.                 foreach( $load as $handle ) {
  205.                     $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n\n" . Minify_CSS_Compressor::process(file_get_contents($relpa . $type .'/' . $handle . '.' . $type), null) . "\n";
  206.                 }
  207.                 break;
  208.  
  209.             case 'minify':
  210.                 foreach( $load as $handle ) {
  211.                     $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n\n" . CssMin::minify(file_get_contents($relpa . $type .'/' . $handle . '.' . $type)) . "\n";
  212.                 }
  213.                 break;
  214.         }
  215.  
  216.     } elseif ($type == 'js') {
  217.  
  218.         switch ($method) {
  219.  
  220.             case 'pack':
  221.                 foreach( $load as $handle ) {
  222.                     $packer = new JavaScriptPacker(file_get_contents($relpa . $type .'/' . $handle . '.' . $type) . "\n", 'Normal', true, false);
  223.                     $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n" . $packer->pack();
  224.                 }
  225.                 break;
  226.  
  227.             case 'minify':
  228.                 foreach( $load as $handle ) {
  229.                     $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n" . JSMin::minify(file_get_contents($relpa . $type .'/' . $handle . '.' . $type)) . "\n";
  230.                 }
  231.                 break;
  232.         }
  233.     }
  234. }
  235.  
  236. /**
  237.  * cache and print output
  238.  */
  239. if (!empty($p)) {
  240.  
  241.     if (!$hash_filepath_exits
  242.         && !preg_match("/localhost/i", $_SERVER['HTTP_HOST'])
  243.     ) {
  244.         lib_local_write_file($hash_filepath, $p, "w+");
  245.     }
  246.  
  247.     lib_local_print_gzipped_scripts($type, 31536000, $p);
  248. }
  249.  
  250. ob_end_flush();
  251.  
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement