Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * shimansky.biz, mobile-english.ru
- *
- * Static web site core scripts
- * @package shimansky.biz
- * @author Serguei Shimansky <englishextra@yandex.ru>
- * @copyright Serguei Shimansky 11:28 23.07.2011
- * @access public
- * @version 0.1
- * @link https://bitbucket.org/englishextra/shimansky.biz
- * @link https://bitbucket.org/englishextra/mobile-english.ru
- * @link https://github.com/englishextra/shimansky.biz.git
- * @link https://github.com/englishextra/mobile-english.ru.git
- */
- $relpa = ($relpa0 = preg_replace("/[\/]+/", "/", $_SERVER['DOCUMENT_ROOT'] . '/')) ? $relpa0 : '';
- ob_start();
- /**
- * put these lines in .htaccess send unpacked gzip to browser
- * php_flag zlib.output_compression On
- * php_value zlib.output_compression_level 5
- */
- function lib_local_print_gzipped_scripts($type, $expires_offset, $p) {
- $contenttype = '';
- if ($type == 'js') {$contenttype = 'application/x-javascript';}
- elseif ($type == 'css') {$contenttype = 'text/css';}
- header("Cache-Control: public, max-age=" . $expires_offset);
- header("Expires: " . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . " GMT");
- header("Content-Type: " . $contenttype . "; charset=UTF-8");
- if ( !ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
- header('Vary: Accept-Encoding');
- if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate')) {
- header('Content-Encoding: deflate');
- $p = gzdeflate( $p, 3 );
- } elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode')) {
- header('Content-Encoding: gzip');
- $p = gzencode( $p, 3 );
- }
- }
- echo $p;
- }
- function lib_local_write_file ($data, $w, $type) {
- if (!$fo = fopen($data, $type)) {
- die('Cannot open file: ' . $data);
- }
- if (!is_writable($data)) {
- die('Cannot write file: ' . $data);
- }
- flock($fo, LOCK_EX);
- fputs($fo, $w);
- fflush($fo);
- flock($fo, LOCK_UN);
- fclose($fo);
- }
- function lib_local_fix_filename ($s) {
- $s = str_replace(array(' ','\\','\/','_','-','.',',','!','(',')','[',']'), '', strtolower($s));
- return $s;
- }
- function lib_local_packCSS($s) {
- //$s = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $s);
- $s = str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", "", trim(@strval($s))));
- $a = array("/[\ ]+/s" => " ",
- "/\; \}/s" => "}",
- "/\;\}/s" => "}",
- "/\}\ /s" => "}",
- "/\: /s" => ":",
- "/\ \{/s" => "{",
- "/\{\ /s" => "{",
- "/\; /s" => ";",
- "/\,\ /s" => ",",
- "/\/\*(.*?)\*\//s" => "");
- foreach($a as $k => $v) {
- $s = preg_replace($k, $v, $s);
- }
- return $s;
- }
- error_reporting(0);
- /**
- * get query string
- */
- $type = isset($_GET['type']) ? $_GET['type'] : '';
- $load = isset($_GET['load']) ? $_GET['load'] : '';
- $static = isset($_GET['static']) ? $_GET['static'] : '';
- $method = isset($_GET['method']) ? $_GET['method'] : 'minify';
- /**
- * die if not enough parameters
- */
- if (!$type && (!$load || !$static)) {die('/*empty query string*/');}
- $hash_filepath = $relpa . 'temp/cache/' . $type . '_' . md5($load);
- /**
- * is mobile device? print empty js and handheld css
- */
- require_once $relpa .'lib/lib_mobile_device_detect.inc';
- $is_handheld = (mobile_device_detect()) ? 1 : '';
- /**
- * is search engine bot? print empty js and handheld css
- */
- require_once $relpa .'lib/lib_browser.inc';
- if (!isset($browser) || empty($browser)) {$browser = new Browser();}
- $ua = ($ua0 = $browser->getBrowser()) ? $ua0 : '';
- //$is_handheld = ($browser->isMobile()) ? 1 : ''; //not reliable with browser.php library
- /**
- * is search bot?
- */
- $is_robot = ($browser->isRobot()) ? 1 : '';
- /**
- * if mobile device or search engine bot, print empty js and handheld css
- */
- if (!empty($is_handheld) || !empty($is_robot) || !$ua) {
- $handheld_css_filepath = $relpa . 'css/handheld.css';
- $handheld_css = (file_exists($handheld_css_filepath)) ? file_get_contents($handheld_css_filepath) : '';
- $p = '';
- switch ($type) {
- case 'js':
- $p = '';
- break;
- case 'css':
- $p = lib_local_packCSS($handheld_css);
- break;
- }
- lib_local_write_file($hash_filepath, $p, "w+");
- lib_local_print_gzipped_scripts($type, 31536000, $p);
- exit;
- }
- /**
- * read from cache when we are sure this is not mobile
- */
- if (!empty($ua)) {$hash_filepath = $relpa . 'temp/cache/'
- . lib_local_fix_filename ($ua)
- . '_' . $type . '_' . md5($load);}
- $hash_filepath_exits = ( file_exists($hash_filepath) ) ? 1 : '';
- if ($hash_filepath_exits) {
- lib_local_print_gzipped_scripts($type, 31536000, '/* from cache file ' . str_replace($relpa . 'temp/cache/', '', $hash_filepath) . ' */' . "\n\n" . file_get_contents($hash_filepath));
- exit;
- }
- /**
- * initialise output sting
- */
- $p = '';
- /**
- * print static content first, in .htacces there mast be a rewrite rule:
- * RewriteEngine On
- * RewriteBase /
- * RewriteRule ^(css|js)/([0-9a-z]+)\.(css|js)?$ $1/?static=$1/$2.$3&type=$3 [L]
- */
- if (!empty($static) && !empty($type)) {
- $p = file_get_contents($relpa . $static);
- /**
- * print packed or minified
- */
- } elseif (!empty($load) && !empty($type)) {
- require_once $relpa . 'lib/lib_packscripts.inc';
- $load = explode(',', preg_replace( '/[^a-z0-9\_\,\.\/]+/i', '', $load));
- // obfuscate content
- if ($type == 'css') {
- switch ($method) {
- case 'pack':
- foreach( $load as $handle ) {
- $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n\n" . Minify_CSS_Compressor::process(file_get_contents($relpa . $type .'/' . $handle . '.' . $type), null) . "\n";
- }
- break;
- case 'minify':
- foreach( $load as $handle ) {
- $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n\n" . CssMin::minify(file_get_contents($relpa . $type .'/' . $handle . '.' . $type)) . "\n";
- }
- break;
- }
- } elseif ($type == 'js') {
- switch ($method) {
- case 'pack':
- foreach( $load as $handle ) {
- $packer = new JavaScriptPacker(file_get_contents($relpa . $type .'/' . $handle . '.' . $type) . "\n", 'Normal', true, false);
- $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n" . $packer->pack();
- }
- break;
- case 'minify':
- foreach( $load as $handle ) {
- $p .= "\n" . '/*'. $type .'/' . $handle . '.' . $type .'*/'. "\n" . JSMin::minify(file_get_contents($relpa . $type .'/' . $handle . '.' . $type)) . "\n";
- }
- break;
- }
- }
- }
- /**
- * cache and print output
- */
- if (!empty($p)) {
- if (!$hash_filepath_exits
- && !preg_match("/localhost/i", $_SERVER['HTTP_HOST'])
- ) {
- lib_local_write_file($hash_filepath, $p, "w+");
- }
- lib_local_print_gzipped_scripts($type, 31536000, $p);
- }
- ob_end_flush();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement