00001 <?php
00002
00009 class TIDEngine{
00010
00026 public $domain = 'http://localhost/TIDengine/';
00027
00035 public $root_cache = 'cache/';
00036
00042 public $pages_cache = 'pages/';
00043
00049 public $css_cache = 'css/';
00050
00056 public $js_cache = 'javascript/';
00057
00063 public $javascript_source = 'javascript/native/source/';
00064
00070 public $javascript_minified = 'javascript/native/minified/';
00071
00077 public $frameworks_source = 'javascript/frameworks/';
00078
00084 public $css_source = 'css/';
00085
00108 public $cache_naming = 'md5';
00109
00116 public $cache_name_pre = '(';
00117
00124 public $cache_name_post = ')-';
00125
00132 public $cache_extension = '.tpl.php';
00133
00141 public $server_caching = true;
00142
00151 public $client_caching = true;
00152
00158 public $gzip = true;
00159
00169 public $compression_level = 4;
00170
00194 public $cache_lifetime = array('page'=>'1.month', 'css'=>'6.month', 'javascript'=>'6.month', 'frameworks'=>'6.month');
00195
00219 public $xhtml_code_operations = 'indent';
00220
00226 public $xhtml_code_indentation = ' ';
00227
00249 public $optimize_css = true;
00250
00256 public $optimize_css_gz = true;
00257
00263 public $compact_css = true;
00264
00271 public $css_file_name = 'master';
00272
00279 public $css_ext = '.css';
00280
00303 public $optimize_javascript = true;
00304
00312 public $combine_all_javascript = true;
00313
00321 public $compact_javascript = true;
00322
00328 public $optimize_javascript_gz = true;
00329
00352 public $packer = 'jsmin';
00353
00360 public $js_ext = '.js';
00361
00367 public $javascript_combined = 'combined';
00368
00374 public $javascript_filename = 'javascript';
00375
00382 public $framework_filename = 'framework';
00383
00391 public $frameworks_type = 'minified';
00392
00410 private $shortcodes = array();
00411
00417 private $data_changes_check;
00418
00424 private $modification_time;
00425
00431 private $template_id;
00432
00441 private $configuration_file = '';
00442
00448 private $meta_spec = array('Content-Language', 'Expires', 'Pragma', 'Cache-Control', 'imagetoolbar');
00449
00455 private $debug_meta = false;
00456
00457
00458
00459
00460
00461
00462
00463
00476 public function __construct() {
00477
00478 if($this->configuration_file !==''){
00479
00480 include ($this->configuration_file);
00481
00482 }
00483
00484 $this->caculate_lifetime();
00485
00486 $this->check_browser_set_extension();
00487
00488 $this->include_packer();
00489
00490
00491 if($this->gzip == true){
00492
00493 $this->cache_extension = $this->cache_extension . '.gz';
00494
00495 }
00496
00497 }
00498
00509 public function check_cache($_data, $template_path, $template_id, $elements, $caching) {
00510
00511
00512 if($caching !== ''){
00513
00514 $this->server_caching = $caching;
00515
00516 }
00517
00518
00519 $template_file = $template_path . $template_id . '.tpl';
00520
00521
00522
00523
00524
00525
00526 $this->data_changes_check = md5(serialize($_data)).md5(serialize($this));
00527
00528
00529 if($this->cache_naming == 'md5'){
00530
00531 $this->template_id = md5($template_id);
00532
00533 }else if($this->cache_naming == 'sha1'){
00534
00535 $this->template_id = sha1($template_id);
00536
00537 }
00538
00539
00540
00541 $this->page_cache_path = $cache_file['page']['cache'][0] = $this->root_cache . $this->pages_cache . $this->cache_name_pre . $this->data_changes_check . $this->cache_name_post . $this->template_id . $this->cache_extension;
00542
00543
00544
00545
00546 if($this->client_caching){
00547
00548
00549
00550 if(file_exists($this->page_cache_path)){
00551
00552 $template_time = filemtime($this->page_cache_path);
00553
00554 }else{
00555
00556 $template_time = time();
00557 }
00558
00559
00560 $headers = $this->getRequestHeaders();
00561
00562
00563 if (isset($headers['If-Modified-Since'])){
00564
00565 $get_mod = strtotime($headers['If-Modified-Since']);
00566
00567
00568 if($get_mod < $template_time+10 && $get_mod > $template_time-10){
00569
00570
00571
00572 header('Last-Modified: '.gmdate('D, d M Y H:i:s', $template_time).' GMT', true, 304);
00573 exit;
00574 }
00575 }
00576
00577 }
00578
00579
00580
00581
00582
00583
00584 if(isset($_data['css'])){
00585
00586
00587 $css_files_num = count($_data['css']);
00588
00589
00590 for ($i = 0; $i < $css_files_num; $i++) {
00591
00592
00593 $cache_file['css']['cache'][] = $this->domain . $this->root_cache . $this->css_cache . $_data['css'][$i] . $this->css_ext;
00594 $cache_file['css']['source'][] = $this->domain . $this->css_source . $_data['css'][$i] .'.css';
00595
00596 }
00597
00598
00599 if($this->compact_css){
00600 $cache_file['css']['cache'] = '';
00601 $cache_file['css']['cache'][0]= $this->root_cache . $this->css_cache . $this->css_file_name . $this->css_ext;
00602
00603 }
00604 unset($_data['css']);
00605 }
00606
00607
00608 if(isset($_data['javascript'])){
00609
00610
00611 $js_files_num = count($_data['javascript']);
00612
00613 for ($i = 0; $i < $js_files_num; $i++) {
00614
00615
00616 $cache_file['javascript']['cache'][] = $this->domain . $this->root_cache . $this->js_cache . $_data['javascript'][$i] . $this->js_ext;
00617
00618
00619
00620 if($this->frameworks_type == 'minified'){
00621
00622 if(!file_exists( $this->domain . $this->javascript_minified . $_data['javascript'][$i] . '.js')){
00623
00624 $this->create_minified($_data['javascript'][$i]);
00625 }
00626
00627 $cache_file['javascript']['source'][] = $this->domain . $this->javascript_minified . $_data['javascript'][$i] . '.js';
00628
00629
00630 }else{
00631
00632 $cache_file['javascript']['source'][] = $this->domain . $this->javascript_minified . $_data['javascript'][$i] . '.js';
00633
00634 }
00635
00636 }
00637
00638
00639 if($this->compact_javascript){
00640
00641 $cache_file['javascript']['cache'] = '';
00642 $cache_file['javascript']['cache'][0] = $this->root_cache . $this->js_cache . $this->javascript_filename . $this->js_ext;
00643
00644 }
00645
00646 unset($_data['javascript']);
00647 }
00648
00649
00650 if(isset($_data['frameworks'])){
00651
00652
00653 $frameworks_files_num = count($_data['frameworks']);
00654
00655
00656 $i = 0;
00657
00658
00659 while ($i < $frameworks_files_num) {
00660
00661
00662
00663
00664 if(is_array($_data['frameworks'][$i])){
00665
00666 $j = 0;
00667 $fw_num = count($_data['frameworks'][$i]);
00668
00669 while ($j < $fw_num) {
00670
00671 $cache_file['frameworks']['source'][] = $this->domain . $this->frameworks_source . $this->frameworks_type . '/'. $_data['frameworks'][$i][0] . '/' . $_data['frameworks'][$i][$j] . '.js';
00672 $cache_file['frameworks']['cache'][] = $this->domain . $this->root_cache . $this->js_cache . $_data['frameworks'][$i][$j] . $this->js_ext;
00673
00674 $j++;
00675
00676 }
00677
00678 }else{
00679
00680 $cache_file['frameworks']['source'][] = $this->domain . $this->frameworks_source . $this->frameworks_type . '/'. $_data['frameworks'][$i] . '/' . $_data['frameworks'][$i] . '.js';
00681 $cache_file['frameworks']['cache'][] = $this->root_cache . $this->js_cache . $_data['frameworks'][$i] . $this->js_ext;
00682
00683 }
00684
00685 $i++;
00686
00687 }
00688
00689
00690 if($this->compact_javascript){
00691
00692 $cache_file['frameworks']['cache'] = '';
00693 $cache_file['frameworks']['cache'][0] = $this->root_cache . $this->js_cache . $this->framework_filename . $this->js_ext;
00694
00695 }
00696
00697 unset($_data['frameworks']);
00698 }
00699
00700
00701 if($this->combine_all_javascript){
00702
00703
00704
00705 $this->compact_javascript = true;
00706
00707 $cache_file['javascript']['source'] = array_merge($cache_file['frameworks']['source'], $cache_file['javascript']['source'] );
00708
00709 $cache_file['javascript']['cache'][0] = $this->root_cache . $this->js_cache . $this->javascript_combined . $this->js_ext;
00710 unset($cache_file['frameworks']);
00711
00712
00713 }
00714
00715
00716 $ex_count = 0;
00717
00718
00719 foreach($cache_file as $type => $paths){
00720
00721
00722 $number_cache = count($paths['cache']);
00723
00724 for ($i = 0; $i < $number_cache; $i++) {
00725
00726
00727
00728 if(!file_exists($paths['cache'][$i])){
00729
00730
00731 $this->modification_time[$type] = time();
00732
00733
00734 $ex_count++;
00735
00736
00737 $cache_file[$type]['exist'][$i] = false;
00738
00739
00740 }else{
00741
00742
00743
00744 if($this->cache_lifetime[$type] !== 'permanent'){
00745
00746
00747 $this->modification_time[$type] = filemtime($paths['cache'][$i]);
00748
00749
00750 if(($this->modification_time[$type] + $this->cache_lifetime[$type]) < time()){
00751
00752
00753 $ex_count++;
00754
00755
00756 $cache_file[$type]['exist'][$i] = false;
00757
00758 }else{
00759
00760
00761
00762 $cache_file[$type]['exist'][$i] = true;
00763
00764
00765 }
00766 }
00767 }
00768 }
00769 }
00770
00771
00772 $cache_file = array_merge($_data, $cache_file);
00773
00774
00775
00776 if($this->server_caching == true){
00777
00778
00779 if($ex_count == 0){
00780
00781 $this->template_id = false;
00782 $this->display($this->page_cache_path);
00783
00784
00785 }else{
00786
00787 $this->shortcodes($cache_file);
00788 $this->display($template_file, true, $elements);
00789
00790 }
00791
00792
00793 }else{
00794
00795 $this->shortcodes($cache_file);
00796 $this->display($template_file, false, $elements);
00797 }
00798
00799
00800 }
00801
00807 public function create_minified($file){
00808
00809
00810 $optimized = $this->js_packers(file_get_contents($this->javascript_source . $file . '.js'));
00811 file_put_contents($this->javascript_minified . $file . '.js', $optimized);
00812 }
00813
00821 public function display($file_path, $cache=false, $elements=false) {
00822
00823
00824 $output_data = '';
00825
00826
00827 if($this->template_id == false){
00828
00829 $output_data .= file_get_contents($file_path);
00830
00831 }else{
00832
00833
00834 if($elements !== false){
00835
00836
00837 foreach($elements as $file=>$path){
00838
00839
00840 if($file == 'body' && empty($path)){
00841
00842 $output_data .= file_get_contents($file_path);
00843
00844
00845 }else{
00846
00847 $output_data .= file_get_contents($path . '/' . $file.'.tpl');
00848
00849 }
00850
00851 }
00852
00853
00854 }else{
00855
00856 $output_data .= file_get_contents($file_path);
00857
00858 }
00859
00860
00861 $output_data = preg_replace(array_keys($this->shortcodes), array_values($this->shortcodes), $output_data);
00862
00863
00864 $output_data = preg_replace('/{.*}/', '', $output_data);
00865
00866
00867 if($this->xhtml_code_operations == 'compact'){
00868
00869 $output_data = $this->clean_file($output_data);
00870
00871
00872 }else if($this->xhtml_code_operations == 'indent'){
00873
00874 $output_data = $this->clean_html_code($output_data);
00875
00876 }
00877
00878 }
00879
00880
00881 if(!ob_start("ob_gzhandler")){
00882 ob_start();
00883 }
00884
00885 if($this->gzip == true){
00886
00887 header("Content-Encoding: gzip");
00888 header("Last-Modified: ".gmdate("D, d M Y H:i:s \G\M\T", $this->modification_time['page']), true, 200);
00889 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $this->cache_lifetime['page']));
00890 header("Cache-Control: public");
00891 header("Pragma: public");
00892 header("Expect:");
00893 header("Content-Description: Steam Inline");
00894 header("Connection: Keep-Alive");
00895 header("Content-Disposition: inline;");
00896 header('ETag: "'. $this->data_changes_check .'"');
00897
00898 }else{
00899
00900 header("Last-Modified: ".gmdate("D, d M Y H:i:s \G\M\T", $this->modification_time['page']), true, 200);
00901 header("Content-Encoding: x-gzip");
00902 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $this->cache_lifetime['page']));
00903 header("Cache-Control: public");
00904 header("Pragma: public");
00905 header("Expect:");
00906 header("Connection: Keep-Alive");
00907 header('ETag: "'. $this->data_changes_check .'"');
00908
00909 }
00910
00911
00912 echo $output_data;
00913
00914
00915 if($this->server_caching == true && $this->template_id !== false){
00916
00917 file_put_contents($this->page_cache_path , $output_data);
00918
00919 }
00920
00921
00922
00923 }
00924
00930 public function shortcodes($data) {
00931
00932
00933 $data = array_change_key_case($data, CASE_UPPER);
00934
00935
00936 foreach($data as $shortcode=>$content){
00937
00938
00939 switch($shortcode){
00940
00941 case 'CSS':
00942 $this->css_data($content);
00943 break;
00944
00945
00946 case 'JAVASCRIPT':
00947 $this->javascript_data($content, 'JAVASCRIPT');
00948 break;
00949
00950
00951 case 'FRAMEWORKS':
00952 $this->javascript_data($content, 'FRAMEWORKS');
00953 break;
00954
00955
00956 case 'META':
00957 $this->meta_data($content);
00958 break;
00959
00960
00961 default:
00962
00963
00964 if(!is_array($content)){
00965
00966 $this->shortcodes["'{{$shortcode}}'"] = $content;
00967
00968
00969 }else{
00970
00971
00972 foreach($content as $child_shortcode=>$child_content){
00973
00974
00975 if(!is_array($child_content)){
00976
00977 $this->shortcodes["'{{$shortcode}.{$child_shortcode}}'"] = $child_content;
00978
00979
00980 }else{
00981
00982 foreach($child_content as $c_shortcode=>$c_content){
00983
00984 $this->shortcodes["'{{$shortcode}.{$child_shortcode}.{$c_shortcode}}'"] = $c_content;
00985
00986 }
00987 }
00988 }
00989 }
00990
00991 }
00992 }
00993
00994 }
00995
01002 public function css_data($data) {
01003
01004
01005 $css = '';
01006
01007
01008 $css_files = count($data['cache']);
01009
01010
01011 foreach ($data['cache'] as $key => $value) {
01012
01013
01014 if($data['exist'][$key]){
01015
01016
01017 $css .= '<link href="'. $value.'" rel="stylesheet" type="text/css" />'."\n";
01018
01019
01020 unset($data['source'][$key]);
01021 unset($data['cache'][$key]);
01022
01023
01024 $css_files--;
01025
01026 }
01027 }
01028
01029
01030 unset($data['exist']);
01031 $data['source'] = array_values($data['source']);
01032 $data['cache'] = array_values($data['cache']);
01033
01034
01035 if($css_files > 0){
01036
01037 $css .= $this->optimize_css($data);
01038
01039 }
01040
01041
01042 $this->shortcodes["'{CSS}'"] = $css;
01043
01044
01045 }
01046
01052 public function optimize_css($files) {
01053
01054
01055 $files_num = count($files['source']);
01056
01057
01058 $css = '';
01059
01060
01061 if($this->compact_css == true){
01062
01063 $combined_file = '';
01064
01065
01066
01068 for ($i = 0; $i < $files_num; $i++) {
01069
01070 $combined_file .= file_get_contents($files['source'][$i]);
01071
01072 }
01073
01074
01075 if($this->optimize_css == true){
01076
01077 $combined_file = $this->clean_file($combined_file);
01078
01079 }
01080
01081
01082 if($this->optimize_css_gz == true){
01083
01084 $combined_file = $this->gzip_data($combined_file);
01085 }
01086
01087
01088 file_put_contents($files['cache'][0], $combined_file);
01089
01090
01091 $css .= '<link href="'. $files['cache'][0].'" rel="stylesheet" type="text/css" />'."\n";
01092
01093 }else{
01094
01095
01096 $cache_path = array();
01097
01098
01099 for ($i = 0; $i < $files_num; $i++) {
01100
01101
01102 $combined_file = file_get_contents($files['source'][$i]);
01103
01104
01105 if($this->optimize_css == true){
01106
01107 $combined_file = $this->clean_file($combined_file);
01108
01109 }
01110
01111
01112 if($this->optimize_css_gz == true){
01113
01114 $combined_file = $this->gzip_data($combined_file);
01115
01116 }
01117
01118
01119 file_put_contents($files['cache'][$i], $combined_file);
01120
01121
01122 $css .= '<link href="'. $files['cache'][$i].'" rel="stylesheet" type="text/css" />'."\n";
01123 }
01124
01125 }
01126
01127 return $css;
01128 }
01129
01135 public function javascript_data($data, $type) {
01136
01137
01138 $js = '';
01139
01140
01141 $js_files = count($data['cache']);
01142
01143
01144 foreach ($data['cache'] as $key => $value) {
01145
01146
01147 if($data['exist'][$key]){
01148
01149
01150 $js .= '<script type="text/javascript" src="' . $value . '"></script>'."\n";
01151
01152 unset($data['source'][$key]);
01153 unset($data['cache'][$key]);
01154
01155
01156 $js_files--;
01157
01158 }
01159 }
01160
01161 unset($data['exist']);
01162 $data['source'] = array_values($data['source']);
01163 $data['cache'] = array_values($data['cache']);
01164
01165
01166 if($js_files > 0){
01167
01168 $js .= $this->optimize_javascript($data, $type);
01169
01170 }
01171
01172
01173 $this->shortcodes["'{{$type}}'"] = $js;
01174 }
01175
01176
01177
01185 public function optimize_javascript($files, $type) {
01186
01187
01188 $files_num = count($files['source']);
01189
01190
01191 $js = '';
01192
01193
01194 if($this->compact_javascript == true){
01195
01196 $combined_file = '';
01197
01198
01199
01200 for ($i = 0; $i < $files_num; $i++) {
01201
01202 $combined_file .= file_get_contents($files['source'][$i]);
01203
01204 }
01205
01206
01207 if($this->optimize_javascript == true && $this->packer !== 'native' && $type !== 'FRAMEWORKS' && $this->frameworks_type !== 'minified'){
01208
01209 $combined_file = $this->js_packers($combined_file);
01210
01211 }
01212
01213
01214 if($this->optimize_javascript_gz == true){
01215
01216 $combined_file = $this->gzip_data($combined_file);
01217 }
01218
01219 file_put_contents($files['cache'][0], $combined_file);
01220 $js .= '<script type="text/javascript" src="'. $files['cache'][0].'"></script>'."\n";
01221
01222
01223 }else{
01224
01225 $cache_path = array();
01226
01227
01228 for ($i = 0; $i < $files_num; $i++) {
01229
01230
01231 $combined_file = file_get_contents($files['source'][$i]);
01232
01233
01234 if($this->optimize_javascript == true && $this->packer !== 'native'){
01235
01236 $combined_file = $this->js_packers($combined_file);
01237
01238 }
01239
01240
01241 if($this->optimize_javascript_gz == true){
01242
01243 $combined_file = $this->gzip_data($combined_file);
01244 }
01245
01246 file_put_contents($files['cache'][$i], $combined_file);
01247 $js .= '<script type="text/javascript" src="'. $files['cache'][$i].'"></script>'."\n";
01248 }
01249
01250 }
01251 return $js;
01252 }
01253
01260 public function js_packers($file_content){
01261
01262
01263 set_time_limit(2500);
01264
01265 switch($this->packer){
01266 case 'packer':
01267
01268 $optimized_file = $this->Packer($file_content);
01269
01270 break;
01271 case 'jsmin':
01272
01273 $optimized_file = $this->JSmin($file_content);
01274
01275 break;
01276 case 'jshrink':
01277
01278 $optimized_file = $this->JShrink($file_content);
01279
01280 break;
01281
01282 case 'jsminplus':
01283
01284 $optimized_file = $this->JSminplus($file_content);
01285
01286 break;
01287
01288
01289
01290
01291
01292
01293
01294 }
01295
01296
01297 return $optimized_file;
01298
01299 }
01305 public function include_debugger(){
01306 if($this->debug_meta){
01307
01308 require ('TIDEngine/core/TIDEbugger.php');
01309 $this->TIDEbugger = new TIDEbugger;
01310 }
01311
01312 }
01313
01319 public function include_packer(){
01320 switch($this->packer){
01321 case 'packer':
01322
01323 require ('TIDEngine/vendor/JavaScriptPacker/JavaScriptPacker.php');
01324
01325 break;
01326 case 'jsmin':
01327
01328 require ('TIDEngine/vendor/JSMin/JSMin.php');
01329
01330 break;
01331 case 'jshrink':
01332
01333 require ('TIDEngine/vendor/JShrink/JShrink.php');
01334
01335 break;
01336
01337 case 'jsminplus':
01338
01339 require ('TIDEngine/vendor/JSMinPlus/JSMinPlus.php');
01340
01341 break;
01342 }
01343
01344 }
01345
01352 public function Packer($js_file){
01353
01354 $packer = new JavaScriptPacker($js_file, 'Normal', true, false);
01355 return $packer->pack();
01356
01357 }
01358
01364 public function JSmin($js_file){
01365
01366
01367 return JSMin::minify($js_file);
01368
01369
01370 }
01371
01378 public function JShrink($js_file){
01379
01380
01381 return JShrink::minify($js_file);
01382
01383 }
01384
01391 public function JSminplus($js_file){
01392
01393
01394 return JSMinPlus::minify($js_file);
01395
01396 }
01397
01403 public function gzip_data($data) {
01404
01405
01406 if(extension_loaded('zlib')) {
01407
01408
01409 $gzipped_file = gzencode($data, $this->compression_level);
01410
01411 }
01412
01413 return $gzipped_file;
01414
01415 }
01416
01424 public function save_data($data, $path, $modifiers=false) {
01425
01426 if(!$modifiers){
01427
01428 file_put_contents($path, $data);
01429
01430 }else{
01431
01432 file_put_contents($path, $data, FILE_APPEND | LOCK_EX);
01433
01434 }
01435 }
01436
01442 public function meta_data($data) {
01443
01444 $meta_tags = '';
01445
01446
01447 foreach ($data as $element=>$meta){
01448
01449
01450 if($element == 'title'){
01451
01452 $this->shortcodes["'{PAGE_TITLE}'"] = $meta;
01453
01454
01455 }else if ( in_array($element, $this->meta_spec)){
01456
01457 $meta_tags .= '<meta http-equiv="'.$element.'" content="'.$meta.'" />';
01458
01459
01460 }else{
01461
01462 $meta_tags .= '<meta name="'.$element.'" content="'.$meta.'" />';
01463
01464 }
01465
01466 }
01467
01468
01469 $this->shortcodes["'{META}'"] = $meta_tags;
01470
01471
01473
01474
01475
01476
01477
01478
01479 }
01480
01486 public function fix_newlines_for_clean_html($fixthistext){
01487
01488
01489 $fixthistext_array = explode("\n", $fixthistext);
01490
01491
01492 foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue){
01493
01494 if (!preg_match("/^(\s)*$/", $unfixedtextvalue)){
01495
01496 $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
01497 $fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
01498
01499 }
01500 }
01501
01502 return implode("\n", $fixedtext_array);
01503
01504 }
01505
01512 public function clean_html_code($uncleanhtml){
01513
01514
01515 $indent = $this->xhtml_code_indentation;
01516
01517
01518 $fixed_uncleanhtml = $this->fix_newlines_for_clean_html($uncleanhtml);
01519
01520 $uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
01521
01522
01523 $indentlevel = 0;
01524
01525 foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml){
01526
01527
01528 $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
01529 $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
01530
01531 $replaceindent = "";
01532
01533
01534 for ($o = 0; $o < $indentlevel; $o++){
01535
01536 $replaceindent .= $this->xhtml_code_indentation;
01537 }
01538
01539
01540 if (preg_match("/<(.+)\/>/", $currentuncleanhtml)){
01541
01542 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
01543
01544
01545 }else if (preg_match("/<!(.*)>/", $currentuncleanhtml)){
01546
01547 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
01548
01549
01550 }else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml)){
01551
01552 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
01553
01554
01555 }else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml)){
01556
01557 $indentlevel--;
01558 $replaceindent = "";
01559
01560 for ($o = 0; $o < $indentlevel; $o++){
01561 $replaceindent .= $this->xhtml_code_indentation;
01562 }
01563
01564
01565 if($currentuncleanhtml == '</textarea>' || $currentuncleanhtml == '</script>'){
01566
01567 $cleanhtml_array[$uncleanhtml_key] = $cleanhtml_array[($uncleanhtml_key - 1)] . $currentuncleanhtml;
01568 unset($cleanhtml_array[($uncleanhtml_key - 1)]);
01569
01570 }else{
01571
01572 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
01573
01574 }
01575
01576
01577 }else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml)){
01578
01579 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
01580
01581 $indentlevel++;
01582 $replaceindent = "";
01583
01584 for ($o = 0; $o < $indentlevel; $o++){
01585 $replaceindent .= $this->xhtml_code_indentation;
01586 }
01587
01588 }else{
01589
01590
01591 $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
01592 }
01593
01594
01595 return implode("\n", $cleanhtml_array);
01596 }
01597
01603 public function clear_cache($template_id=false){
01604
01605
01606
01607 if($template_id == false){
01608
01609 array_map( "unlink", glob( $this->domain . $this->root_cache . $this->pages_cache . '*' . $this->cache_extension ) );
01610
01611
01612 }else{
01613
01614
01615 if($this->cache_naming == 'md5'){
01616
01617 $template_id = md5($template_id);
01618
01619 }else if($this->cache_naming == 'sha1'){
01620
01621 $template_id = sha1($template_id);
01622
01623 }
01624
01625
01626 array_map( "unlink", glob( $this->domain . $this->root_cache . $this->pages_cache . '*-' . $template_id . $this->cache_extension ) );
01627
01628 }
01629
01630 }
01631
01633 public function clean_file($content) {
01634
01635 $content = preg_replace('/^\s+|\n|\r|\s+$/m', '', $content);
01636 $content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content);
01637 $content = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $content);
01638 $content = preg_replace(array('(( )+{)','({( )+)'), '{', $content);
01639 $content = preg_replace(array('(( )+})','(}( )+)','(;( )*})'), '}', $content);
01640 $content = preg_replace(array('(;( )+)','(( )+;)'), ';', $content);
01641 $content = preg_replace(array('(:( )+)','(( )+:)'), ':', $content);
01642 $content = preg_replace('/(\s|)\,(\s|)/', ',', $content);
01643
01644 return $content;
01645
01646 }
01647
01653 public function browser_info($agent=null) {
01654
01655
01656 $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape','konqueror', 'gecko', 'chrome');
01657
01658
01659
01660
01661 $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
01662 $pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
01663
01664
01665 if (!preg_match_all($pattern, $agent, $matches)) return array();
01666
01667
01668
01669
01670
01671
01672 $i = count($matches['browser'])-1;
01673
01674 if($i == 2){
01675
01676 $i=1;
01677
01678 }
01679
01680 return array($matches['browser'][$i], $matches['version'][$i]);
01681
01682 }
01683
01685 public function getRequestHeaders() {
01686
01687
01688 if (function_exists("apache_request_headers")) {
01689
01690
01691 if($headers = apache_request_headers()) {
01692
01693
01694 return $headers;
01695
01696 }
01697 }
01698
01699
01700 $headers = array();
01701
01702
01703 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
01704
01705 $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
01706
01707 }
01708
01709 return $headers;
01710
01711 }
01712
01717 public function caculate_lifetime(){
01718
01719 $d_time = $this->cache_lifetime;
01720
01721
01722 unset($this->cache_lifetime);
01723
01724
01725 $lifetime = array('page', 'css', 'javascript', 'frameworks');
01726
01727
01728 $time_def = array('second'=>'1',
01729 'minute'=>'60',
01730 'hour'=>'3600',
01731 'day'=>'86400',
01732 'month'=>'2592000',
01733 'year'=>'946080000');
01734
01735
01736 if(!is_array($d_time)){
01737
01738
01739 if(preg_match('/([\d]+)\.([\w]+)/', $d_time, $def_time)){
01740
01741 $calc_lifetime = ($def_time[1]*$time_def[$def_time[2]]);
01742
01743
01744 }else{
01745
01746 $calc_lifetime = $d_time;
01747
01748 }
01749
01750
01751 foreach($lifetime as $key => $value){
01752
01753 $this->cache_lifetime[$value] = $calc_lifetime;
01754
01755
01756 }
01757
01758
01759 }else{
01760
01761 foreach($d_time as $type =>$life){
01762
01763 preg_match('/([\d]+)\.([\w]+)/', $life, $def_time);
01764
01765 $this->cache_lifetime[$type] = ($def_time[1]*$time_def[$def_time[2]]) ;
01766
01767 }
01768
01769 }
01770 }
01771
01795 public function check_browser_set_extension(){
01796
01797
01798 $browser = $this->browser_info();
01799 $this->browser = $browser[0];
01800
01801
01802 if($this->optimize_javascript_gz == true ){
01803
01804
01805 if($this->browser == 'safari'){
01806
01807 $this->js_ext = $this->js_ext . '.gz.js';
01808
01809
01810 }else{
01811
01812 $this->js_ext = $this->js_ext . '.gz';
01813
01814 }
01815 }
01816
01817
01818 if( $this->optimize_css_gz == true){
01819
01820
01821 if($this->browser == 'safari'){
01822
01823 $this->css_ext = $this->css_ext . '.gz.css';
01824
01825
01826 }else{
01827
01828 $this->css_ext = $this->css_ext . '.gz';
01829
01830 }
01831 }
01832 }
01833
01834 }