SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/MediaMushController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Helpers\AdminAuth;
  19.  
  20.  
  21. /**
  22. * - No description yet -
  23. **/
  24. class MediaMushController extends Controller
  25. {
  26.  
  27. /**
  28.   * Does all of the mushing of media
  29.   **/
  30. public function mush($skin = 'default')
  31. {
  32. Kohana::closeBuffers();
  33.  
  34. AdminAuth::checkLogin();
  35. header('Content-type: text/plain');
  36.  
  37. $timestamp = time();
  38.  
  39. // CSS
  40. echo "Mushing CSS\n";
  41. flush();
  42. $css = file_get_contents(DOCROOT . "skin/{$skin}/cache/css_files.txt");
  43. $css = explode("\n", $css);
  44. $css = $this->mushFiles($css);
  45. $res = @file_put_contents(DOCROOT . "skin/{$skin}/cache/site_{$timestamp}.css", trim($css));
  46. if (! $res) {
  47. echo " Unable to save file\n";
  48. }
  49.  
  50. // JS
  51. echo "\nMushing JavaScript\n";
  52. flush();
  53. $js = file_get_contents(DOCROOT . "skin/{$skin}/cache/js_files.txt");
  54. $js = explode("\n", $js);
  55. $js = $this->mushFiles($js);
  56. $res = @file_put_contents(DOCROOT . "skin/{$skin}/cache/site_{$timestamp}.js", trim($js));
  57. if (! $res) {
  58. echo " Unable to save file\n";
  59. }
  60.  
  61. // PHP
  62. echo "\nGenerating mush reference script\n";
  63. flush();
  64. $php = "<link href=\"SKIN/cache/site_{$timestamp}.css\" rel=\"stylesheet\" type=\"text/css\">\n";
  65. $php .= "<script src=\"SKIN/cache/site_{$timestamp}.js\" type=\"text/javascript\"></script>";
  66. $res = @file_put_contents(DOCROOT . "skin/{$skin}/cache/mush.php", $php);
  67. if (! $res) {
  68. echo " Unable to save file\n";
  69. }
  70. }
  71.  
  72.  
  73. /**
  74.   * Glues a bunch of files together
  75.   **/
  76. private function mushFiles(array $files)
  77. {
  78. $out = '';
  79.  
  80. foreach ($files as $filename) {
  81. $filename = trim($filename);
  82. if ($filename == '') continue;
  83.  
  84. $content = file_get_contents(DOCROOT . $filename);
  85.  
  86. if (preg_match('/\.css$/', $filename)) {
  87. $content = $this->preprocessCss($content);
  88.  
  89. } else if (preg_match('/\.min\.js$/', $filename)) {
  90. $content = $this->preprocessJs($content);
  91.  
  92. } else if (preg_match('/\.js$/', $filename)) {
  93. $content = $this->preprocessJsMinify($content);
  94. }
  95.  
  96. $out .= "\n/* {$filename} */\n";
  97. $out .= $content;
  98.  
  99. echo " {$filename}\n";
  100. flush();
  101. }
  102.  
  103. return $out;
  104. }
  105.  
  106.  
  107. /**
  108.   * CSS minify
  109.   **/
  110. private function preprocessCss($content)
  111. {
  112. $content = preg_replace('!/\*(.*?)\*/!sm', '', $content); // nuke multi-line comment
  113. $content = preg_replace('!\s+!', ' ', $content); // nuke multiple spaces
  114. $content = trim($content);
  115.  
  116. return $content;
  117. }
  118.  
  119.  
  120. /**
  121.   * JS minify (scripts not yet minified)
  122.   **/
  123. private function preprocessJsMinify($content)
  124. {
  125. if (! IN_PRODUCTION) return $content;
  126.  
  127. $ch = curl_init('http://closure-compiler.appspot.com/compile');
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. curl_setopt($ch, CURLOPT_POST, 1);
  130. curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=WHITESPACE_ONLY&js_code=' . urlencode($content));
  131. $content = curl_exec($ch);
  132. curl_close($ch);
  133.  
  134. return $content;
  135. }
  136.  
  137.  
  138. /**
  139.   * JS minify (minor mods)
  140.   **/
  141. private function preprocessJs($content)
  142. {
  143. $content = preg_replace('!\n\n!', "\n", $content);
  144. $content = trim($content);
  145.  
  146. return $content;
  147. }
  148.  
  149.  
  150. }
  151.  
  152.  
  153.