<?php
/*
 * JS and CSS Minifier 를 이용한
 * stylesheet 파일 및 javascript 파일 압축 후 바로바로 파일 갱신시켜 
 * 새로 출력 시켜주는 시스템 입니다.
 * 
 * 개발자 : 익명닉네임 ( sir.co.kr )
 * 개발자 홈페이지 ( theplug.co.kr )
 * 
 * JS and CSS Minifier for Gnuboard5
 * version: 1.2 (2016-02-23)
 * 
 - 아래 부터  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, 'https://javascript-minifier.com/raw');
}

function minifyCSS($arr){
	minify($arr, 'https://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, $order=0, $cache_time=1)
{
    if (!file_exists($dir_file)) {

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

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

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

        if($cache_data && $cache_data < $cache) {
            @unlink($cache_file);
            $cache_fwrite = true;
        }

        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_fwrite = false;
        }

    }

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

    $cache_path = G5_PATH.$dir_file;
    $cache = filemtime($cache_path);

    add_stylesheet('<link rel="stylesheet" href="'.G5_URL.$cache_file.'?'.date('YmdHis', $cache).'">', $order);
}

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

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

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

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

        if($cache_data && $cache_data < $cache) {
            @unlink($cache_file);
            $cache_fwrite = true;
        }

        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_fwrite = false;
        }

    }

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

    $cache_path = G5_PATH.$dir_file;
    $cache = filemtime($cache_path);

    add_javascript('<script src="'.G5_URL.$cache_file.'?'.date('YmdHis', $cache).'"></script>', $order);
}
?>