/** * Css helper functions * * @filesource * @copyright Copyright (c) 2008-2014, Kamil "Brego" Dzieliński * @license http://opensource.org/licenses/mit-license.php The MIT License * @author Kamil "Brego" Dzieliński <*****@*****.**> * @link http://prank.brego.dk/ Prank's project page * @link http://github.com/brego/prank/ Prank's Git repository * @package Prank * @subpackage Helpers * @since Prank 0.10 * @version Prank 0.75 */ function css() { clearstatcache(); $args = func_get_args(); $files = []; foreach ($args as $index => $filename) { if (substr($filename, -4, 4) != '.css') { $args[$index] = $filename . '.css'; } } foreach ($args as $filename) { $files[] = $filename; $files[] = filemtime(WEBROOT . 'css' . DS . $filename); } $hash = md5(implode($files)); if (is_file(WEBROOT . 'tmp/' . $hash . '.php')) { $link = $link = '<link rel="stylesheet" href="' . url('tmp/' . $hash . '.php') . '" type="text/css" />' . "\n"; } else { $compressed = null; foreach ($args as $filename) { $file = file_get_contents(WEBROOT . 'css' . DS . $filename); $compressed .= compress_css($file); } $compressed_filename = 'tmp/' . md5(implode('', $files)) . '.php'; $output = "<?php header('Content-Type: text/css'); ob_start('ob_gzhandler'); ?>\n"; $output .= "/*\nThis file is a compressed version of this site's CSS code.\n"; $output .= "For uncompressed version, refer to the following files:\n"; $output .= implode("\n", $args) . "\nIn the css/ directory of this site."; $output .= "\n*/\n"; $output .= $compressed; $output .= "\n<?php ob_end_flush(); ?>"; file_put_contents($compressed_filename, $output); $link = '<link rel="stylesheet" href="' . url($compressed_filename) . '" type="text/css" />' . "\n"; } return $link; }
function combine_files($sourcedir, $targetfile, $compression_mode) { $content = ''; foreach (glob($sourcedir . "/*") as $file) { $content .= "\n\n" . file_get_contents($file); } if ($compression_mode == 'minimize_css') { include 'css_compressor.php'; list($content, $stats) = compress_css($content); // $debugMsg = $stats['original_size'].' → '.$stats['final_size'].' ( – '.$stats['reduction'].')'; } file_put_contents($targetfile, $content); }
fpassthru($fp); fclose($fp); exit; } } } // Get contents of the files $contents = ''; reset($elements); while (list(, $element) = each($elements)) { $path = realpath($base . '/' . $element); $contents .= "\n\n" . file_get_contents($path); } // Compress the objects for a smaller file to send... if ($type == 'css') { $contents = compress_css($contents); } // Send Content-Type header("Content-Type: text/" . $type); if (isset($encoding) && $encoding != 'none') { // Send compressed contents $contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE); header("Content-Encoding: " . $encoding); header('Content-Length: ' . strlen($contents)); echo $contents; } else { // Send regular contents header('Content-Length: ' . strlen($contents)); echo $contents; } // Store cache
function output_file($file_item, $current_path, $type) { global $ignore_files, $template_exts, $total_reduce_size; if ($current_path !== '') { foreach ((array) $file_item['file'] as $file_name => $file_info) { if (false === strpos($current_path, 'views') && $file_name[0] === '_' || $file_name[0] == '.') { continue; } if (in_array($file_name, $ignore_files)) { continue; } $file_path = $current_path . '/' . $file_name; $result = true; $file_skiped = false; $ext = get_ext($file_name); if ('all' != $type && $ext != $type) { continue; } if ($ext == 'css') { if (strpos($file_name, 'min.') === 0) { $result = compress_css($file_path); } else { $file_skiped = true; } } elseif ($ext == 'js') { if (strpos($file_name, 'min.') === 0) { $result = compress_js($file_path); } else { $file_skiped = true; } } elseif ($ext == 'png') { compress_png($file_path); } elseif (in_array($ext, $template_exts)) { compress_template($file_path); } else { copy(ROOT_PATH . $file_path, OUTPUT_DIR . $file_path); } $file_size = 0; if ($file_skiped === false) { if (file_exists(OUTPUT_DIR . $file_path)) { $file_size = filesize(OUTPUT_DIR . $file_path); $total_reduce_size += $file_info['infor']['file_size'] - $file_size; if ($file_size <= 0) { $file_size = 'b style="color:#ff0000">' . $file_size . '</b>'; } else { $file_size = human_file_size($file_size); } } else { $result = false; } } if (false === $result) { echo '<p style="color:#ff0000;font-weight:bold">' . $file_path . ' Error</p>'; } else { if ($file_skiped === false) { echo $file_path . ' <b style="color:#1EBC16">OK</b> <span style="color:#888">(' . human_file_size($file_info['infor']['file_size']) . ' → ' . $file_size . ')</span><br />'; } else { echo '<span style="color:#aaa">' . $file_path . ' <b>Skiped</b></span><br />'; } } } } foreach ((array) $file_item['dir'] as $dir_name => $dir) { if ($dir_name[0] === '_' || $dir_name[0] == '.') { continue; } if (in_array($dir_name, $ignore_files)) { continue; } $dir_path = $current_path . '/' . $dir_name; echo '<h2>' . $dir_path . '</h2>'; make_dir($dir_path, OUTPUT_DIR); output_file($dir, $dir_path, $type); } }
function compressCSS($dir, $compress = 0, $sort = 0) { global $lezaz; $pattern = "*.css"; $vars_dir_path = $lezaz->lezaz_path($dir) . '/'; $vars_dir_link = $lezaz->lezaz_path($dir, 1) . '/'; $glop = glob($vars_dir_path . $pattern); if ($sort) { arsort($glop); } else { asort($glop); } foreach ($glop as $filename) { $fn = basename($filename); if (strpos('XXX' . $filename, 'Xall.css.')) { continue; } if ($compress == 1 || $compress == 2) { $Xfile .= hash_file('md5', $filename); } else { $return .= " <link rel=\"stylesheet\" type=\"text/css\" href=\"{$vars_dir_link}{$fn}\"/>\n"; } } if (file_exists($vars_dir_path . 'all.css.' . md5($Xfile) . $compress . '.css')) { return " <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $vars_dir_link . 'all.css.' . md5($Xfile) . $compress . '.css' . "\"/>\n"; } if ($return) { return $return; } array_map('unlink', glob($vars_dir_path . "all.css.*.css")); // delete all mini files created by lezaz foreach (glob($vars_dir_path . $pattern) as $filename) { $fn = basename($filename); if ($compress == '1') { $cssall .= "\n\n\n/*{$fn}*/\n========================\n" . file_get_contents($filename); } else { if ($compress == '2') { $cssau = compress_css($filename); $cssall .= "\n\n\n/*{$fn}*/\n========================\n" . $cssau; } } } $lezaz->file->write($vars_dir_path . 'all.css.' . md5($Xfile) . $compress . '.css', get_import($cssall)); return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $vars_dir_link . 'all.css.' . md5($Xfile) . $compress . '.css' . "\"/> \n"; // <link rel=\"stylesheet\" type=\"text/css\" href=\"' . $vars_dir_link . '$it\"/>";')); asort($files); arsort($files); }