| 1 |
<?php |
| 2 |
|
| 3 |
require '../vendor/autoload.php'; |
| 4 |
|
| 5 |
use tubalmartin\CssMin\Minifier as CSSmin; |
| 6 |
|
| 7 |
mb_internal_encoding('UTF-8'); |
| 8 |
|
| 9 |
/** |
| 10 |
* Navigates through an array and removes slashes from the values. |
| 11 |
* |
| 12 |
* If an array is passed, the array_map() function causes a callback to pass the |
| 13 |
* value back to the function. The slashes from this value will removed. |
| 14 |
* |
| 15 |
* @param array|string $value The array or string to be stripped. |
| 16 |
* @return array|string Stripped array (or string in the callback). |
| 17 |
*/ |
| 18 |
function stripslashes_deep($value) |
| 19 |
{ |
| 20 |
if (is_array($value)) { |
| 21 |
$value = array_map('stripslashes_deep', $value); |
| 22 |
} elseif (is_object($value)) { |
| 23 |
$vars = get_object_vars($value); |
| 24 |
foreach ($vars as $key => $data) { |
| 25 |
$value->{$key} = stripslashes_deep($data); |
| 26 |
} |
| 27 |
} else { |
| 28 |
$value = stripslashes($value); |
| 29 |
} |
| 30 |
|
| 31 |
return $value; |
| 32 |
} |
| 33 |
|
| 34 |
// Disable magic quotes at runtime. |
| 35 |
if (function_exists('ini_set')) { |
| 36 |
ini_set('magic_quotes_sybase', 0); |
| 37 |
ini_set('get_magic_quotes_runtime', 0); |
| 38 |
} |
| 39 |
|
| 40 |
// If get_magic_quotes_gpc is active, strip slashes |
| 41 |
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) { |
| 42 |
$_POST = stripslashes_deep($_POST); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
if (!empty($_POST)) : |
| 47 |
// Form options |
| 48 |
parse_str($_POST['options']); |
| 49 |
|
| 50 |
$linebreak_pos = trim($linebreak_pos) !== '' ? $linebreak_pos : false; |
| 51 |
$raise_php = isset($raise_php) ? true : false; |
| 52 |
|
| 53 |
// Create a new CSSmin object and try to raise PHP settings |
| 54 |
$compressor = new CSSmin($raise_php); |
| 55 |
|
| 56 |
if ($linebreak_pos !== false) { |
| 57 |
$compressor->setLineBreakPosition($linebreak_pos); |
| 58 |
} |
| 59 |
|
| 60 |
if (isset($keep_sourcemap)) { |
| 61 |
$compressor->keepSourceMapComment(); |
| 62 |
} |
| 63 |
|
| 64 |
if (isset($remove_important_comments)) { |
| 65 |
$compressor->removeImportantComments(); |
| 66 |
} |
| 67 |
|
| 68 |
if ($raise_php) { |
| 69 |
$compressor->setMemoryLimit($memory_limit); |
| 70 |
$compressor->setMaxExecutionTime($max_execution_time); |
| 71 |
$compressor->setPcreBacktrackLimit(1000 * $pcre_backtrack_limit); |
| 72 |
$compressor->setPcreRecursionLimit(1000 * $pcre_recursion_limit); |
| 73 |
} |
| 74 |
|
| 75 |
// Compress the CSS code and store data |
| 76 |
$output = array(); |
| 77 |
$output['css'] = $compressor->run($_POST['css']); |
| 78 |
$output['originalSize'] = mb_strlen($_POST['css'], '8bit'); |
| 79 |
$output['compressedSize'] = mb_strlen($output['css'], '8bit'); |
| 80 |
$output['bytesSaved'] = $output['originalSize'] - $output['compressedSize']; |
| 81 |
$output['compressionRatio'] = round(($output['bytesSaved'] * 100) / |
| 82 |
($output['originalSize'] === 0 ? 1 : $output['originalSize']), 2); |
| 83 |
|
| 84 |
// Output data |
| 85 |
echo json_encode($output); |
| 86 |
else : |
| 87 |
?> |
| 88 |
<!DOCTYPE HTML> |
| 89 |
<html lang="en-US"> |
| 90 |
<head> |
| 91 |
<meta charset="UTF-8"> |
| 92 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 93 |
<title>YUI CSS compressor - PHP</title> |
| 94 |
<link rel="stylesheet" type="text/css" href="third-party/bootstrap/css/bootstrap.min.css"> |
| 95 |
<link rel="stylesheet" type="text/css" href="styles.css"> |
| 96 |
<link rel="stylesheet/less" type="text/css" href="styles.less"> |
| 97 |
</head> |
| 98 |
<body> |
| 99 |
<div class="navbar"> |
| 100 |
<div class="navbar-inner"> |
| 101 |
<div class="container-fluid"> |
| 102 |
<a class="brand" href="https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port">YUI CSS compressor PHP port</a> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
</div> |
| 106 |
<div class="container-fluid"> |
| 107 |
<div class="row-fluid"> |
| 108 |
<div id="body" class="span9"> |
| 109 |
<!--Body content--> |
| 110 |
<div id="less-error-message" class="less-error-message"></div> |
| 111 |
<div class="well"> |
| 112 |
<div id="input-container"> |
| 113 |
<label for="input-css">Paste a block of CSS to compress in the area below:</label> |
| 114 |
<textarea id="input-css" class="input-block-level" rows="10"></textarea> |
| 115 |
</div> |
| 116 |
<div id="output-container" class="hide"> |
| 117 |
<label for="output-css">Here's your compressed CSS code:</label> |
| 118 |
<span class="help-block">Original size: <span id="original-size"></span> bytes | Compressed size: <span id="compressed-size"></span> bytes | Bytes saved: <span id="bytes-saved"></span> | Compression ratio: <span id="compression-ratio"></span>%</span> |
| 119 |
<textarea id="output-css" class="input-block-level" rows="10"></textarea> |
| 120 |
</div> |
| 121 |
</div> |
| 122 |
</div> |
| 123 |
<div id="sidebar" class="span3"> |
| 124 |
<form id="options-form"> |
| 125 |
<p class="submit"> |
| 126 |
<button type="submit" id="compress-btn" class="btn btn-primary btn-large" data-loading-text="Compressing...">Compress!</button> |
| 127 |
</p> |
| 128 |
<fieldset> |
| 129 |
<legend>LESS</legend> |
| 130 |
<p class="control-group"> |
| 131 |
<label class="checkbox"> |
| 132 |
<input type="checkbox" id="enable-less" value="1"> Enable compiler <span class="version">v1.7.5</span> |
| 133 |
</label> |
| 134 |
</p> |
| 135 |
</fieldset> |
| 136 |
<fieldset> |
| 137 |
<legend>Compressor options</legend> |
| 138 |
<div class="control-group"> |
| 139 |
<label>Linebreak after <i>n</i> columns</label> |
| 140 |
<input type="text" name="linebreak_pos" class="span1"> |
| 141 |
</div> |
| 142 |
<div class="control-group"> |
| 143 |
<label class="checkbox"> |
| 144 |
<input type="checkbox" name="keep_sourcemap" value="1"> Keep CSS Sourcemap comment |
| 145 |
</label> |
| 146 |
</div> |
| 147 |
<div class="control-group"> |
| 148 |
<label class="checkbox"> |
| 149 |
<input type="checkbox" name="remove_important_comments" value="1"> Remove important comments |
| 150 |
</label> |
| 151 |
</div> |
| 152 |
</fieldset> |
| 153 |
<fieldset> |
| 154 |
<legend>PHP configuration options</legend> |
| 155 |
<div class="control-group"> |
| 156 |
<label class="checkbox"> |
| 157 |
<input type="checkbox" name="raise_php" value="1" checked="checked"> Raise PHP configuration options |
| 158 |
</label> |
| 159 |
<label>Memory limit</label> |
| 160 |
<select name="memory_limit" class="span2"> |
| 161 |
<option value="32M">32M</option> |
| 162 |
<option value="64M">64M</option> |
| 163 |
<option value="128M" selected="selected">128M</option> |
| 164 |
<option value="256M">256M</option> |
| 165 |
<option value="512M">512M</option> |
| 166 |
<option value="1G">1G</option> |
| 167 |
<option value="-1">No limit</option> |
| 168 |
</select> |
| 169 |
<label>Max execution time</label> |
| 170 |
<select name="max_execution_time" class="span2"> |
| 171 |
<option value="30">30 secs</option> |
| 172 |
<option value="60" selected="selected">1 min</option> |
| 173 |
<option value="120">2 mins</option> |
| 174 |
<option value="300">5 mins</option> |
| 175 |
</select> |
| 176 |
<label>PCRE backtrack limit</label> |
| 177 |
<select name="pcre_backtrack_limit" class="span2"> |
| 178 |
<option value="100">100.000</option> |
| 179 |
<option value="1000" selected="selected">1.000.000</option> |
| 180 |
<option value="2000">2.000.000</option> |
| 181 |
<option value="5000">5.000.000</option> |
| 182 |
</select> |
| 183 |
<label>PCRE recursion limit</label> |
| 184 |
<select name="pcre_recursion_limit" class="span2"> |
| 185 |
<option value="100">100.000</option> |
| 186 |
<option value="250">250.000</option> |
| 187 |
<option value="500" selected="selected">500.000</option> |
| 188 |
<option value="1000">1.000.000</option> |
| 189 |
</select> |
| 190 |
</div> |
| 191 |
</fieldset> |
| 192 |
</form> |
| 193 |
</div> |
| 194 |
</div> |
| 195 |
</div> |
| 196 |
<script type="text/javascript"> |
| 197 |
less = { |
| 198 |
env: 'development' |
| 199 |
}; |
| 200 |
</script> |
| 201 |
<script type="text/javascript" src="third-party/less-1.7.5.min.js"></script> |
| 202 |
<script type="text/javascript" src="third-party/jquery-1.12.4.min.js"></script> |
| 203 |
<script type="text/javascript" src="third-party/bootstrap/js/bootstrap.min.js"></script> |
| 204 |
<script type="text/javascript" src="scripts.js"></script> |
| 205 |
</body> |
| 206 |
</html> |
| 207 |
<?php |
| 208 |
endif; |
| 209 |
?> |
| 210 |
|