forked from Seanom/Tinifier-Concrete5-Optimiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny.php
123 lines (120 loc) · 5.58 KB
/
tiny.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
Tinifier
---------
@file tiny.php
@date 2011-05-27 22:20:36 -0400 (Wed, 1 June 2011)
@author Jack Lightbody <jack.lightbody@gmail.com>
Copyright (c) 2011 Jack Lightbody <12345j.co.cc>
@license Mit Open Source
@github https://github.com/12345j/Tinifier-Concrete5-Optimiser
@version 1.3.8
*/
defined( 'C5_EXECUTE' ) or die( "Access Denied." );
class TinyHelper {
public function tinify( $content ){
$file=loader::helper('file');
$jsFileMerge = DIRNAME_JAVASCRIPT."/merge.js";
$cssFileMerge = DIRNAME_CSS."/merge.css";
if(file_exists($cssFileMerge)){
unlink($cssFileMerge);
}
if(file_exists($jsFileMerge)){
unlink($jsFileMerge);
}
$jsCombine=array();
$cssCombine=array();
$unknownCss=array();
$unknownJs=array();
// Get all the javascript links to files and put their content in the merge js file
if ( preg_match_all( '#<\s*script\s*(type="text/javascript"\s*)?src=.+<\s*/script\s*>#smUi',$content,$jsLinks )) {
foreach ( $jsLinks[0] as $jsLink ) {
if(preg_match('/<script type="text\/javascript" src="(.*)"><\/script>/', $jsLink )){
$jsItem= preg_replace('/<script type="text\/javascript" src="(.*)"><\/script>/', '$1', $jsLink);// get whats in href attr
array_push($jsCombine, $jsItem);
$content=str_replace($jsLink, '', $content);
}elseif(preg_match('/<script.*class="nocombine".*<\/script>/', $jsLink )){
}else{
array_push($unknownJs, $jsLink);
$content=str_replace($jsLink, '', $content);
}
}
foreach ($jsCombine as $js){
$external = 'http://';
$externalFile = strpos($js, $external);
if($externalFile === false){
$jsFile=BASE_URL.$js;
}else{
$jsFile=$js;
}
$jsFileContents=$file->getContents($jsFile);
/*Compressing the js takes way too long so we just insert the uncompressed stuff. TODO: Speed it up- if its a not new version then don't compress it again. Do this with css too */
//Loader::library( '3rdparty/jsmin' );
//$jsCompress=JSMin::minify( $jsFileContents );
file_put_contents($jsFileMerge, $jsFileContents, FILE_APPEND);
}
}
$content = str_ireplace( '</body>','<script type="text/javascript" src="'.ASSETS_URL_WEB.'/js/merge.js"></body>', $content ); // add the script link to the footer
// get all the inline javascript and add it to the footer (we need this below the merge)
if(preg_match_all( '#<\s*script\s*(type="text/javascript"\s*)?>(.+)<\s*/script\s*>#smUi',$content,$inlineJavascript )){
foreach ($inlineJavascript[0] as $inlineItem ) {
$content=str_replace($inlineItem, '', $content);
$content=str_replace('</body>', $inlineItem.'</body>', $content);
}
}
// get all the css links and add to merge
if ( preg_match_all( '#<\s*link\s*rel="?stylesheet"?.+>#smUi',$content,$cssLinks )) {
foreach ($cssLinks[0] as $cssLink ) {
if(preg_match('/<link rel="stylesheet" type="text\/css" href="(.*)" \/>/', $cssLink )){
$cssItem= preg_replace('/<link rel="stylesheet" type="text\/css" href="(.*)" \/>/', '$1', $cssLink);// get whats in href attr
array_push($cssCombine, $cssItem);
}else{
array_push($unknownCss, $cssLink);
}
$content=str_replace($cssLink, '', $content);
}
foreach($cssCombine as $css){
$cssFile=BASE_URL.$css;
$cssFileContents=$file->getContents($cssFile);
//$cssFileContent=preg_replace("#\url((.*)\)#is", '('.$css.'$1'.')', $cssFileContents);
$cssCompress=cssCompress($cssFileContents);
file_put_contents($cssFileMerge, $cssCompress, FILE_APPEND);
}
}
// get all the inline css and add to merge
if ( preg_match( '#<\s*style.*>.+<\s*/style\s*\/?>#smUi',$content,$inlineCss )>0 ) {
foreach ( $inlineCss as $Inlinecssitem ) {
$Inlinecssitem1=preg_replace('#<\s*style.*>#smUi', "", $Inlinecssitem);
$Inlinecssitem1=preg_replace('#<\s*/style\s*\/?>#smUi', "", $Inlinecssitem1);
$cssCompress=cssCompress($Inlinecssitem1);
file_put_contents($cssFileMerge, $cssCompress, FILE_APPEND);
$content=str_replace($Inlinecssitem, '', $content);
}
}
foreach($unknownJs as $jsU){
$content=str_ireplace('</body>', $jsU.'</body>', $content); // add the js link to the end
}
foreach($unknownCss as $cssU){
$content=str_ireplace( '</head>',$cssU.'</head>', $content ); // add the stylesheet link to the head
}
$content = str_ireplace( '</head>','<link rel="stylesheet" type="text/css" href="'.ASSETS_URL_WEB.'/css/merge.css" /><!--Compressed by Tinifier v1.3.8--></head>', $content ); // add the stylesheet link to the head
$content = preg_replace('/(?:(?<=\>)|(?<=\/\)))(\s+)(?=\<\/?)/','',$content);//remove html whitespace
return $content;
}}
function cssCompress($string) {
/* remove comments */
$string = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $string);
/* remove tabs, spaces, new lines, etc. */
$string = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $string);
/* remove unnecessary spaces */
$string = str_replace('{ ', '{', $string);
$string = str_replace(' }', '}', $string);
$string = str_replace('; ', ';', $string);
$string = str_replace(', ', ',', $string);
$string = str_replace(' {', '{', $string);
$string = str_replace('} ', '}', $string);
$string = str_replace(': ', ':', $string);
$string = str_replace(' ,', ',', $string);
$string = str_replace(' ;', ';', $string);
return $string;
}