PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/min/builder/index.php

https://github.com/acidvertigo/minify
PHP | 232 lines | 199 code | 23 blank | 10 comment | 19 complexity | baf816fa493589eeeee2bdb6f1e6a8e0 MD5 | raw file
  1. <?php
  2. if (phpversion() < 5) {
  3. exit('Minify requires PHP5 or greater.');
  4. }
  5. // check for auto-encoding
  6. $encodeOutput = (function_exists('gzdeflate')
  7. && !ini_get('zlib.output_compression'));
  8. // recommend $min_symlinks setting for Apache UserDir
  9. $symlinkOption = '';
  10. if (0 === strpos($_SERVER["SERVER_SOFTWARE"], 'Apache/')
  11. && preg_match('@^/\\~(\\w+)/@', $_SERVER['REQUEST_URI'], $m)
  12. ) {
  13. $userDir = DIRECTORY_SEPARATOR . $m[1] . DIRECTORY_SEPARATOR;
  14. if (false !== strpos(__FILE__, $userDir)) {
  15. $sm = array();
  16. $sm["//~{$m[1]}"] = dirname(dirname(__FILE__));
  17. $array = str_replace('array (', 'array(', var_export($sm, 1));
  18. $symlinkOption = "\$min_symlinks = $array;";
  19. }
  20. }
  21. require dirname(__FILE__) . '/../config.php';
  22. if (! $min_enableBuilder) {
  23. header('Location: /');
  24. exit();
  25. }
  26. $setIncludeSuccess = set_include_path(dirname(__FILE__) . '/../lib' . PATH_SEPARATOR . get_include_path());
  27. // we do it this way because we want the builder to work after the user corrects
  28. // include_path. (set_include_path returning FALSE is OK).
  29. try {
  30. require_once 'Minify/Cache/File.php';
  31. } catch (Exception $e) {
  32. if (! $setIncludeSuccess) {
  33. echo "Minify: set_include_path() failed. You may need to set your include_path "
  34. ."outside of PHP code, e.g., in php.ini.";
  35. } else {
  36. echo $e->getMessage();
  37. }
  38. exit();
  39. }
  40. require 'Minify.php';
  41. $cachePathCode = '';
  42. if (! isset($min_cachePath) && ! function_exists('sys_get_temp_dir')) {
  43. $detectedTmp = Minify_Cache_File::tmp();
  44. $cachePathCode = "\$min_cachePath = " . var_export($detectedTmp, 1) . ';';
  45. }
  46. ob_start();
  47. ?>
  48. <!DOCTYPE html>
  49. <title>Minify URI Builder</title>
  50. <meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
  51. <link href="css/bootstrap.min.css" rel="stylesheet">
  52. <body>
  53. <?php if ($symlinkOption): ?>
  54. <div class=topNote><strong>Note:</strong> It looks like you're running Minify in a user
  55. directory. You may need the following option in /min/config.php to have URIs
  56. correctly rewritten in CSS output:
  57. <br><textarea id=symlinkOpt rows=3 cols=80 readonly><?php echo htmlspecialchars($symlinkOption); ?></textarea>
  58. </div>
  59. <?php endif; ?>
  60. <p class="alert-message error" id=jsDidntLoad><strong>Uh Oh.</strong> Minify was unable to
  61. serve Javascript for this app. To troubleshoot this,
  62. <a href="http://code.google.com/p/minify/wiki/Debugging">enable FirePHP debugging</a>
  63. and request the <a id=builderScriptSrc href=#>Minify URL</a> directly. Hopefully the
  64. FirePHP console will report the cause of the error.
  65. </p>
  66. <?php if ($cachePathCode): ?>
  67. <p class=topNote><strong>Note:</strong> <code><?php echo
  68. htmlspecialchars($detectedTmp); ?></code> was discovered as a usable temp directory.<br>To
  69. slightly improve performance you can hardcode this in /min/config.php:
  70. <code><?php echo htmlspecialchars($cachePathCode); ?></code></p>
  71. <?php endIf; ?>
  72. <div class="container">
  73. <p id=minRewriteFailed class="hide"><strong>Note:</strong> Your webserver does not seem to
  74. support mod_rewrite (used in /min/.htaccess). Your Minify URIs will contain "?", which
  75. <a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/"
  76. >may reduce the benefit of proxy cache servers</a>.</p>
  77. <h1>Minify URI Builder</h1>
  78. <noscript><p class="topNote">Javascript and a browser supported by jQuery 1.2.6 is required
  79. for this application.</p></noscript>
  80. <div id=app class=hide>
  81. <p>Create a list of Javascript or CSS files (or 1 is fine) you'd like to combine
  82. and click [Update].</p>
  83. <ol id=sources><li></li></ol>
  84. <div>
  85. <p id="add" style="float:left"><button class="btn primary">Add file +</button>&nbsp;</p>
  86. <p><button id=update class="btn success">Update</button></p>
  87. </div>
  88. <div id=bmUris></div>
  89. <div id=results class=hide>
  90. <h2>Minify URI</h2>
  91. <p>Place this URI in your HTML to serve the files above combined, minified, compressed and
  92. with cache headers.</p>
  93. <table id=uriTable>
  94. <tr><th><span class="label warning">URI</span></th><td><a id=uriA class=ext>/min</a> <small>(opens in new window)</small></td></tr>
  95. <tr><th><span class="label warning">HTML</span></th><td><input id="uriHtml" class="span14 uneditable-input" type="text" placeholder=".span14"></td></tr>
  96. </table>
  97. <h2>How to serve these files as a group</h2>
  98. <p>For the best performance you can serve these files as a pre-defined group with a URI
  99. like: <code><span class=minRoot>/min/?</span>g=keyName</code></p>
  100. <p>To do this, add a line like this to /min/groupsConfig.php:</p>
  101. <pre><code>return array(
  102. <span style="color:#666">... your existing groups here ...</span>
  103. <input id=groupConfig class="span14 uneditable-input" type="text" placeholder=".span14">
  104. );</code></pre>
  105. <p><em>Make sure to replace <code>keyName</code> with a unique key for this group.</em></p>
  106. </div>
  107. <div id=getBm>
  108. <h3>Find URIs on a Page</h3>
  109. <p>You can use the bookmarklet below to fetch all CSS &amp; Javascript URIs from a page
  110. on your site. When you active it, this page will open in a new window with a list of
  111. available URIs to add.</p>
  112. <p><a id=bm>Create Minify URIs</a> <small>(right-click, add to bookmarks)</small></p>
  113. </div>
  114. <h3>Combining CSS files that contain <code>@import</code></h3>
  115. <p>If your CSS files contain <code>@import</code> declarations, Minify will not
  116. remove them. Therefore, you will want to remove those that point to files already
  117. in your list, and move any others to the top of the first file in your list
  118. (imports below any styles will be ignored by browsers as invalid).</p>
  119. <p>If you desire, you can use Minify URIs in imports and they will not be touched
  120. by Minify. E.g. <code>@import "<span class=minRoot>/min/?</span>g=css2";</code></p>
  121. <h3>Debug Mode</h3>
  122. <p>When /min/config.php has <code>$min_allowDebugFlag = <strong>true</strong>;</code>
  123. you can get debug output by appending <code>&amp;debug</code> to a Minify URL, or
  124. by sending the cookie <code>minDebug=&lt;match&gt;</code>, where <code>&lt;match&gt;</code>
  125. should be a string in the Minify URIs you'd like to debug. This bookmarklet will allow you to
  126. set this cookie.</p>
  127. <p><a id=bm2>Minify Debug</a> <small>(right-click, add to bookmarks)</small></p>
  128. </div><!-- #app -->
  129. <hr>
  130. <p>Need help? Check the <a href="http://code.google.com/p/minify/w/list?can=3">wiki</a>,
  131. or post to the <a class=ext href="http://groups.google.com/group/minify">discussion
  132. list</a>.</p>
  133. <p><small>Powered by Minify <?php echo Minify::VERSION; ?></small></p>
  134. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
  135. <script>window.jQuery || document.write('<script src="jquery-1.6.3.min.js"><\/script>')</script>
  136. <script>
  137. (function () {
  138. // workaround required to test when /min isn't child of web root
  139. var src = "../?f=" + location.pathname.replace(/\/[^\/]*$/, '/_index.js').substr(1);
  140. // load script immediately
  141. document.write('<\script src="' + src + '"><\/script>');
  142. $(function () {
  143. $('#builderScriptSrc')[0].href = src;
  144. // give Minify a few seconds to serve _index.js before showing scary red warning
  145. setTimeout(function () {
  146. if (! window.MUB) {
  147. // Minify didn't load
  148. $('#jsDidntLoad').css({display:'block'});
  149. }
  150. }, 3000);
  151. // detection of double output encoding
  152. var msg = '<\p class="alert-message error"><\strong>Warning:<\/strong> ';
  153. var url = 'ocCheck.php?' + (new Date()).getTime();
  154. $.get(url, function (ocStatus) {
  155. $.get(url + '&hello=1', function (ocHello) {
  156. if (ocHello != 'World!') {
  157. msg += 'It appears output is being automatically compressed, interfering '
  158. + ' with Minify\'s own compression. ';
  159. if (ocStatus == '1')
  160. msg += 'The option "zlib.output_compression" is enabled in your PHP configuration. '
  161. + 'Minify set this to "0", but it had no effect. This option must be disabled '
  162. + 'in php.ini or .htaccess.';
  163. else
  164. msg += 'The option "zlib.output_compression" is disabled in your PHP configuration '
  165. + 'so this behavior is likely due to a server option.';
  166. $(document.body).prepend(msg + '<\/p>');
  167. } else
  168. if (ocStatus == '1')
  169. $(document.body).prepend('<\p class=topNote><\strong>Note:</\strong> The option '
  170. + '"zlib.output_compression" is enabled in your PHP configuration, but has been '
  171. + 'successfully disabled via ini_set(). If you experience mangled output you '
  172. + 'may want to consider disabling this option in your PHP configuration.<\/p>'
  173. );
  174. });
  175. });
  176. });
  177. })();
  178. </script>
  179. </div>
  180. </body>
  181. <?php
  182. $content = ob_get_clean();
  183. // setup Minify
  184. Minify::setCache(
  185. isset($min_cachePath) ? $min_cachePath : ''
  186. ,$min_cacheFileLocking
  187. );
  188. Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
  189. Minify::serve('Page', array(
  190. 'content' => $content
  191. ,'id' => __FILE__
  192. ,'lastModifiedTime' => max(
  193. // regenerate cache if any of these change
  194. filemtime(__FILE__)
  195. ,filemtime(dirname(__FILE__) . '/../config.php')
  196. ,filemtime(dirname(__FILE__) . '/../lib/Minify.php')
  197. )
  198. ,'minifyAll' => true
  199. ,'encodeOutput' => $encodeOutput
  200. ));