<?php
/*
 * JS and CSS Minifier 를 이용한
 * stylesheet 파일 및 javascript 파일 압축 후 1 시간 단위로 파일 갱신시켜 
 * 새로 출력 시켜주는 플러그인 입니다. 
 * 
 * 개발자 : 익명닉네임 ( sir.co.kr )
 * 개발자 홈페이지 ( boan.pw )
 * 
 - 아래 부터  JS and CSS Minifier 의 저작권 표기 입니다.
 * 
 * JS and CSS Minifier 
 * version: 1.0 (2013-08-26)
 *
 * This document is licensed as free software under the terms of the
 * MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * Toni Almeida wrote this plugin, which proclaims:
 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK."
 * 
 * This plugin uses online webservices from javascript-minifier.com and cssminifier.com
 * This services are property of Andy Chilton, http://chilts.org/
 *
 * Copyrighted 2013 by Toni Almeida, promatik.
 */
	
function minifyJS($arr){
	minify($arr, 'http://javascript-minifier.com/raw');
}

function minifyCSS($arr){
	minify($arr, 'http://cssminifier.com/raw');
}
	
function minify($arr, $url) {
	foreach ($arr as $key => $value) {
		$handler = fopen($value, 'w');
		fwrite($handler, getMinified($url, file_get_contents($key)));
		fclose($handler);
	}
}
	
function getMinified($url, $content) {
	$postdata = array('http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( array('input' => $content) ) ) );
	return file_get_contents($url, false, stream_context_create($postdata));
}

function stylesheet_min_create($dir_file, $cache_time=1)
{
    if (!file_exists($dir_file)) {

        $dir_min_file = substr(G5_PATH.$dir_file,0 ,-3);


        $cache_fwrite = false;
        $cache_file = $dir_min_file.'min.css';

        if(!file_exists($cache_file)) {
            $cache_fwrite = true;
        }

        if($cache_fwrite) {
            $css_path = G5_PATH.$dir_file;
            $min_css_path = substr(G5_PATH.$dir_file,0 ,-3).'min.css';

            $css = array(
                $css_path => $min_css_path
            );
	
            minifyCSS($css);
        }

        $cache_path = substr(G5_PATH.$dir_file,0 ,-3).'min.css';
        $cache = filemtime($cache_path);

        if($cache && $cache < (G5_SERVER_TIME - 3600 * $cache_time)) {
            @unlink($cache_file);
            $cache_fwrite = true;
        }


    }
    if (file_exists($cache_file)) {

        $dir_min_file = substr($dir_file,0 ,-3);
        $cache_file = $dir_min_file.'min.css';

        $cache_path = substr(G5_PATH.$dir_file,0 ,-3).'min.css';
        $cache = filemtime($cache_path);

        echo '<link rel="stylesheet" href="'.G5_URL.$cache_file.'?cache='.$cache.'">'.PHP_EOL;
    }
}

function javascript_min_create($dir_file, $cache_time=1)
{
    if (!file_exists($dir_file)) {

        $dir_min_file = substr(G5_PATH.$dir_file,0 ,-2);

        $cache_fwrite = false;
        $cache_file = $dir_min_file.'min.js';

        if(!file_exists($cache_file)) {
            $cache_fwrite = true;
        }

        if($cache_fwrite) {
            $js_path = G5_PATH.$dir_file;
            $min_js_path = substr(G5_PATH.$dir_file,0 ,-2).'min.js';

            $js = array(
                $js_path => $min_js_path
            );
	
            minifyJS($js);
        }

        $cache_path = substr(G5_PATH.$dir_file,0 ,-2).'min.js';
        $cache = filemtime($cache_path);

        if($cache && $cache < (G5_SERVER_TIME - 3600 * $cache_time)) {
            @unlink($cache_file);
            $cache_fwrite = true;
        }


    }
    if (file_exists($cache_file)) {

        $dir_min_file = substr($dir_file,0 ,-2);
        $cache_file = $dir_min_file.'min.js';

        $cache_path = substr(G5_PATH.$dir_file,0 ,-2).'min.js';
        $cache = filemtime($cache_path);

        echo '<script src="'.G5_URL.$cache_file.'?cache='.$cache.'"></script>'.PHP_EOL;
    }
}
?>