<?php

////////////////////////////////////////////////////////////////////////////////
//
// File:	function.cssMinifier.php
// Project:	CSS Minifier plugin for CMS Made Simple
// Version:	1.0
// Licence:	Free software under the GNU General Public License
// Web:		http://dev.cmsmadesimple.org/projects/cssMinifier
// Created:	20180904, v1.0, Magal Hezi @author Steffen Becker
//
////////////////////////////////////////////////////////////////////////////////
	
function smarty_cms_function_cssMinifier($params, &$smarty) {
	
	$data_field = isset($params['css']) ? $params['css'] : '';
		$cssFiles = explode(",", $data_field);
		$cssContents = '';
		$config = cmsms()->GetConfig();
	
		foreach($cssFiles as $file) {
			$filename=basename($file);
		    $cssContents = file_get_contents($config['root_path'].'/tmp/cache/'.$filename);
			$minFilePath = $config['root_path'].'/tmp/cache/';
			$minFileName = 'minified_'.$filename;
			$minFileFullPath = $minFilePath.$minFileName;
	
			if (!file_exists($minFileFullPath)) {
				file_put_contents($minFileFullPath, minifyCss($cssContents));
			}

			echo '<link rel="stylesheet" type="text/css" href="'.$config['root_url'].'/tmp/cache/'.$minFileName.'" />';
		}
		

	}

function minifyCss($css) {
		  // some of the following functions to minimize the css-output are directly taken
		  // from the awesome CSS JS Booster: https://github.com/Schepp/CSS-JS-Booster
		  // all credits to Christian Schaefer: http://twitter.com/derSchepp

		  // remove comments
		  $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);

		  // backup values within single or double quotes
		  preg_match_all('/(\'[^\']*?\'|"[^"]*?")/ims', $css, $hit, PREG_PATTERN_ORDER);
		  for ($i=0; $i < count($hit[1]); $i++) {
			$css = str_replace($hit[1][$i], '##########' . $i . '##########', $css);
		  }

		  // remove traling semicolon of selector's last property
		  $css = preg_replace('/;[\s\r\n\t]*?}[\s\r\n\t]*/ims', "}\r\n", $css);
		  // remove any whitespace between semicolon and property-name
		  $css = preg_replace('/;[\s\r\n\t]*?([\r\n]?[^\s\r\n\t])/ims', ';$1', $css);
		  // remove any whitespace surrounding property-colon
		  $css = preg_replace('/[\s\r\n\t]*:[\s\r\n\t]*?([^\s\r\n\t])/ims', ':$1', $css);
		  // remove any whitespace surrounding selector-comma
		  $css = preg_replace('/[\s\r\n\t]*,[\s\r\n\t]*?([^\s\r\n\t])/ims', ',$1', $css);
		  // remove any whitespace surrounding opening parenthesis
		  $css = preg_replace('/[\s\r\n\t]*{[\s\r\n\t]*?([^\s\r\n\t])/ims', '{$1', $css);
		  // remove any whitespace between numbers and units
		  $css = preg_replace('/([\d\.]+)[\s\r\n\t]+(px|em|pt|%)/ims', '$1$2', $css);
		  // shorten zero-values
		  $css = preg_replace('/([^\d\.]0)(px|em|pt|%)/ims', '$1', $css);
		  // constrain multiple whitespaces
		  $css = preg_replace('/\p{Zs}+/ims',' ', $css);
		  // remove newlines
		  $css = str_replace(array("\r\n", "\r", "\n"), '', $css);

		  // Restore backupped values within single or double quotes
		  for ($i=0; $i < count($hit[1]); $i++) {
			$css = str_replace('##########' . $i . '##########', $hit[1][$i], $css);
		  }

		  return $css;
}

function smarty_cms_help_function_cssMinifier() {
	?>
	<h3>What does this do?</h3>
	<p>This CMSMS cssMinifier plugin lets a user convert the default {cms_stylesheet} into a minified css file.</p>
<p>This function takes a css-string and compresses it, removing unneccessary whitespace, colons, removing unneccessary px/em declarations etc.</p>
<h3>How do I use it?</h3>
	<p>Upload the plugin file into your assets\plugins folder first.<br>Insert the tag <code>{cssMinifier}</code> into the &quot;head&quot; section of your <strong style="text-decoration:underline;">template</strong> (case sensitive) this way:</p>
	<p>{capture assign=stylesheets}{cms_stylesheet nolinks=true}{/capture}<br>
{cssMinifier css=$stylesheets}</p>
	<h3>What parameters does it take?	</h3>
	<p>This plugin needs to convert the default {cms_stylesheet} into a string before processing it. Use {capture} as above.</p>
</p>
	<?php
}

function smarty_cms_about_function_cssMinifier() {
	?>
<p>Author: <a href="http://www.facebook.com/MagalFreelanceWebDeveloper">Magal Hezi</a></p>

<p>Version: 1.0</p>
	<p>Update: Check for updates for this plugin at the <a href="http://dev.cmsmadesimple.org/projects/cssminifier" target="_blank">CMS Made Simple Forge page</a></p>
	<p>Feature Requests: If you want to add others social bookmarking services or functionality, please let me know by opening a new feature request in the dedicated <strong>Feature Requests tab</strong> of the <a href="http://dev.cmsmadesimple.org/projects/cssminifier" target="_blank">CMS Made Simple Forge page</a> for this plugin</p>
	<p>Bugs: If you want to submit a new bug, please let me know by opening a new bug in the dedicated <strong>Bug Tracker tab</strong> of the <a href="http://dev.cmsmadesimple.org/projects/cssminifier" target="_blank">CMS Made Simple Forge page</a> for this plugin</p>
	<p>Licence: Free software under the GNU General Public License</p>
	<p>Change History:</p>
	<ul>
<li>Version 1.0</li>
		<ul>
			<li>First release</li>
		</ul>
	</ul>
	<?php
}
?>
