PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/leaf/modules/css_compressor.php

https://github.com/ivansoriasolis/Vidali
PHP | 117 lines | 72 code | 43 blank | 2 comment | 20 complexity | d0833d64e6cfacc4ce1f82ccff02978b MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. chdir('../../');
  3. include('leaf/run.php');
  4. function reducir_color( $matches ){
  5. $matches[0] = strtolower( $matches[0] );
  6. if( (isset( $matches[0][2] ) && ($matches[0][1] == $matches[0][2])) && (isset( $matches[0][4] ) && ($matches[0][3] == $matches[0][4])) && (isset( $matches[0][6] ) && ($matches[0][5] == $matches[0][6])) )
  7. return '#'.$matches[0][1].$matches[0][3].$matches[0][5];
  8. else
  9. return $matches[0];
  10. }
  11. $imagenes_localizadas = array();
  12. function localizar_imagenes( $matches ){
  13. $archivo = $matches[1].'.'.$matches[2];
  14. if( is_file( $archivo ))
  15. return 'url('.$archivo.');';
  16. else{
  17. global $imagenes_localizadas;
  18. if( empty( $imagenes_localizadas ))
  19. lw('finder')->regfinder( $imagenes_localizadas , '/\.(jpg|bmp|png|jpeg|gif|JPG|BMP|PNG|JPEG|GIF)$/' , 'template/' , RECURSIVE );
  20. return 'url(../../'.lw('finder')->getPath( basename( $archivo ) , $imagenes_localizadas , 1 ).');';
  21. }
  22. }
  23. function compress( $buffer ){
  24. $buffer = preg_replace( '/(\r\n|\r|\n|\t|\s{2,})+/' , '' , $buffer );
  25. if( lw::gi('config')->get('css','autolocate') == true ){
  26. $buffer_aux = '';
  27. preg_match_all( '/\/\*INI_LW\*\/\s\/\*CSS (.*?.css)\\*\/\s?(.*?)\/\*FIN_LW\*\//' , $buffer , $coincidencias );
  28. for( $i = 0 ; $i < count( $coincidencias[1] ) ; $i++ )
  29. $buffer_aux .= preg_replace_callback( '/url\s?\(\'?\"?(.*?)\.(jpg|bmp|png|jpeg|gif|JPG|BMP|PNG|JPEG|GIF)\'?\"?\)/' , "localizar_imagenes" , $coincidencias[2][$i] );
  30. }
  31. if( is_array( $coincidencias ) ){
  32. $buffer_aux = preg_replace_callback( '/\#([a-hA-H0-9]+)/' , "reducir_color" , $buffer_aux );
  33. $buffer = $buffer_aux;
  34. }else{
  35. $buffer = preg_replace_callback( '/\#([a-hA-H0-9]+)/' , "reducir_color" , $buffer );
  36. }
  37. $buffer = preg_replace( '/\/\*(.*?)\*\//' , '' , $buffer );
  38. $buffer = preg_replace('/;\s{0,}\}/','}',$buffer);
  39. $buffer = preg_replace('/\s{1,}\{/','{',$buffer);
  40. $buffer = preg_replace('/\);\s{1,}/',');',$buffer);
  41. $buffer = preg_replace('/\:(\s{0,})?/',':',$buffer);
  42. $buffer = preg_replace('/\{(\s{0,1})?/','{',$buffer);
  43. $buffer = preg_replace('/\;(\s{0,1})?/',';',$buffer);
  44. $buffer = preg_replace('/url\(\'(.*?)\'\)\s?([a-zA-Z-]+)\s?/' , "url($1)$2" , $buffer );
  45. $buffer = preg_replace('/\,\s{0,1}\./',',.',$buffer);
  46. $buffer = preg_replace('/font-weight:(\s{0,})?bold/','font-weight:700',$buffer);
  47. $buffer = preg_replace('/font-weight:(\s{0,})?normal/','font-weight:400',$buffer);
  48. $buffer = preg_replace('/\:0px/', ':0' , $buffer );
  49. $buffer = preg_replace('/(px 0px)/', 'px 0' , $buffer );
  50. $buffer = preg_replace('/rgba\((\s?)+([0-9]+)(\s?)+\,(\s?)+([0-9]+)(\s?)+\,(\s?)+([0-9]+)(\s?)+\,(\s?)+([0-9]+)\.([0-9])+(\s?)+\)/',"rgba($2,$5,$8,$11.$12)",$buffer);
  51. $buffer = preg_replace('/\?/','',utf8_decode($buffer));
  52. return $buffer;
  53. }
  54. $revalidar = 60*60;
  55. $expire = 'expires: '.gmdate( 'D, d M Y H:i:s' , time()+$revalidar ) . ' GMT';
  56. header('Content-type: text/css');
  57. //header('cache-control: must-revalidate');
  58. //header( $expire );
  59. ob_start('compress');
  60. $dir = lw::gi('config')->get('css','dir');
  61. $files = array_unique( lw('finder')->regfinder( $buscar , '/\.css$/' , $dir , RECURSIVE ));
  62. $lista = lw('config')->get('css','list');
  63. if( lw('config')->get('css','autolocate') == true ) $lista[] .= '.dont_remove';
  64. for( $x = 0 ; $x < count( $lista ) ; $x++ ){
  65. $archivo_css = $lista[$x];
  66. $path = preg_replace( '/\/\//' , '/' , lw('finder')->getPath( $archivo_css.'.css' , $buscar , 1 ));
  67. if( $path != 'null' ){
  68. echo '/*INI_LW*/ /*CSS '.$path.'*/';
  69. include( $path );
  70. echo '/*FIN_LW*/';
  71. }
  72. }
  73. ob_end_flush();
  74. ?>