Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding original minify library to project sources

  • Loading branch information...
commit beb507fb2e7597d99999affe8483fb208bcef13d 1 parent 195fa56
@Ocramius authored
Showing with 17,405 additions and 0 deletions.
  1. +4 −0 min/.htaccess
  2. +75 −0 min/HISTORY.txt
  3. +26 −0 min/LICENSE.txt
  4. +132 −0 min/README.txt
  5. +53 −0 min/README_1.txt
  6. +35 −0 min/UPGRADING.txt
  7. +242 −0 min/builder/_index.js
  8. +36 −0 min/builder/bm.js
  9. +182 −0 min/builder/index.php
  10. +36 −0 min/builder/ocCheck.php
  11. +1 −0  min/builder/rewriteTest.js
  12. +156 −0 min/config.php
  13. +34 −0 min/groupsConfig.php
  14. +66 −0 min/index.php
  15. +1,370 −0 min/lib/FirePHP.php
  16. +348 −0 min/lib/HTTP/ConditionalGet.php
  17. +326 −0 min/lib/HTTP/Encoder.php
  18. +314 −0 min/lib/JSMin.php
  19. +1,872 −0 min/lib/JSMinPlus.php
  20. +532 −0 min/lib/Minify.php
  21. +103 −0 min/lib/Minify/Build.php
  22. +83 −0 min/lib/Minify/CSS.php
  23. +250 −0 min/lib/Minify/CSS/Compressor.php
  24. +270 −0 min/lib/Minify/CSS/UriRewriter.php
  25. +130 −0 min/lib/Minify/Cache/APC.php
  26. +125 −0 min/lib/Minify/Cache/File.php
  27. +137 −0 min/lib/Minify/Cache/Memcache.php
  28. +90 −0 min/lib/Minify/CommentPreserver.php
  29. +202 −0 min/lib/Minify/Controller/Base.php
  30. +78 −0 min/lib/Minify/Controller/Files.php
  31. +94 −0 min/lib/Minify/Controller/Groups.php
  32. +132 −0 min/lib/Minify/Controller/MinApp.php
  33. +82 −0 min/lib/Minify/Controller/Page.php
  34. +118 −0 min/lib/Minify/Controller/Version1.php
  35. +245 −0 min/lib/Minify/HTML.php
  36. +157 −0 min/lib/Minify/ImportProcessor.php
  37. +131 −0 min/lib/Minify/Lines.php
  38. +45 −0 min/lib/Minify/Logger.php
  39. +37 −0 min/lib/Minify/Packer.php
  40. +187 −0 min/lib/Minify/Source.php
  41. +139 −0 min/lib/Minify/YUICompressor.php
  42. +199 −0 min/lib/Solar/Dir.php
  43. +90 −0 min/utils.php
  44. +44 −0 min_unit_tests/HTTP_ConditionalGet/2.php
  45. +40 −0 min_unit_tests/HTTP_ConditionalGet/3.php
  46. +49 −0 min_unit_tests/HTTP_ConditionalGet/4.php
  47. +27 −0 min_unit_tests/HTTP_ConditionalGet/5.php
  48. +64 −0 min_unit_tests/HTTP_ConditionalGet/_include.php
  49. +36 −0 min_unit_tests/HTTP_ConditionalGet/index.php
  50. BIN  min_unit_tests/HTTP_Encoder/green.png
  51. +60 −0 min_unit_tests/HTTP_Encoder/index.php
  52. +50 −0 min_unit_tests/_inc.php
  53. +9 −0 min_unit_tests/_test_files/css/comments.css
  54. +3 −0  min_unit_tests/_test_files/css/comments.min.css
  55. +66 −0 min_unit_tests/_test_files/css/hacks.css
  56. +4 −0 min_unit_tests/_test_files/css/hacks.min.css
  57. +890 −0 min_unit_tests/_test_files/css/issue62.css
  58. +25 −0 min_unit_tests/_test_files/css/issue62.min.css
  59. +12 −0 min_unit_tests/_test_files/css/paths_prepend.css
  60. +1 −0  min_unit_tests/_test_files/css/paths_prepend.min.css
  61. +14 −0 min_unit_tests/_test_files/css/paths_rewrite.css
  62. +1 −0  min_unit_tests/_test_files/css/paths_rewrite.min.css
  63. +1 −0  min_unit_tests/_test_files/css/readme.txt
  64. +42 −0 min_unit_tests/_test_files/css/selectors.css
  65. +37 −0 min_unit_tests/_test_files/css/selectors.min.css
  66. +31 −0 min_unit_tests/_test_files/css/styles.css
  67. +3 −0  min_unit_tests/_test_files/css/styles.min.css
  68. +434 −0 min_unit_tests/_test_files/css/subsilver.css
  69. +18 −0 min_unit_tests/_test_files/css/subsilver.min.css
  70. +10 −0 min_unit_tests/_test_files/css/unusual_strings.css
  71. +2 −0  min_unit_tests/_test_files/css/unusual_strings.min.css
  72. +658 −0 min_unit_tests/_test_files/css/vladmirated.css
  73. +79 −0 min_unit_tests/_test_files/css/vladmirated.min.css
  74. +14 −0 min_unit_tests/_test_files/css_uriRewriter/exp.css
  75. +14 −0 min_unit_tests/_test_files/css_uriRewriter/in.css
  76. +96 −0 min_unit_tests/_test_files/html/before.html
  77. +36 −0 min_unit_tests/_test_files/html/before.min.html
  78. +95 −0 min_unit_tests/_test_files/html/before2.html
  79. +36 −0 min_unit_tests/_test_files/html/before2.min.html
  80. +3 −0  min_unit_tests/_test_files/importProcessor/1/adjacent.css
  81. +4 −0 min_unit_tests/_test_files/importProcessor/1/tv.css
  82. +4 −0 min_unit_tests/_test_files/importProcessor/adjacent.css
  83. +4 −0 min_unit_tests/_test_files/importProcessor/input.css
  84. +48 −0 min_unit_tests/_test_files/importProcessor/output.css
  85. +57 −0 min_unit_tests/_test_files/js/before.js
  86. +22 −0 min_unit_tests/_test_files/js/before.min.js
  87. +22 −0 min_unit_tests/_test_files/js/before.min_plus.js
  88. +14 −0 min_unit_tests/_test_files/js/condcomm.js
  89. +13 −0 min_unit_tests/_test_files/js/condcomm.min_plus.js
  90. +4 −0 min_unit_tests/_test_files/js/issue74.js
  91. +1 −0  min_unit_tests/_test_files/js/issue74.min.js
  92. +1 −0  min_unit_tests/_test_files/js/issue74.min_plus.js
  93. +3,408 −0 min_unit_tests/_test_files/js/jquery-1.2.3.js
  94. +168 −0 min_unit_tests/_test_files/minify/QueryString.js
  95. +24 −0 min_unit_tests/_test_files/minify/email.js
  96. +2 −0  min_unit_tests/_test_files/minify/issue73_1.js
  97. +3 −0  min_unit_tests/_test_files/minify/issue73_2.js
  98. +7 −0 min_unit_tests/_test_files/minify/issue89_1.css
  99. +7 −0 min_unit_tests/_test_files/minify/issue89_2.css
  100. +1 −0  min_unit_tests/_test_files/minify/issue89_out.min.css
  101. +2 −0  min_unit_tests/_test_files/minify/lines_bugs.js
  102. +280 −0 min_unit_tests/_test_files/minify/lines_output.js
  103. +5 −0 min_unit_tests/_test_files/minify/minified.css
  104. +12 −0 min_unit_tests/_test_files/minify/minified.js
  105. +137 −0 min_unit_tests/test_HTTP_ConditionalGet.php
  106. +260 −0 min_unit_tests/test_HTTP_Encoder.php
  107. +63 −0 min_unit_tests/test_JSMin.php
  108. +80 −0 min_unit_tests/test_JSMinPlus.php
  109. +213 −0 min_unit_tests/test_Minify.php
  110. +36 −0 min_unit_tests/test_Minify_Build.php
  111. +53 −0 min_unit_tests/test_Minify_CSS.php
  112. +56 −0 min_unit_tests/test_Minify_CSS_UriRewriter.php
  113. +33 −0 min_unit_tests/test_Minify_Cache_APC.php
  114. +54 −0 min_unit_tests/test_Minify_Cache_File.php
  115. +38 −0 min_unit_tests/test_Minify_Cache_Memcache.php
  116. +37 −0 min_unit_tests/test_Minify_CommentPreserver.php
  117. +59 −0 min_unit_tests/test_Minify_HTML.php
  118. +48 −0 min_unit_tests/test_Minify_ImportProcessor.php
  119. +36 −0 min_unit_tests/test_Minify_Lines.php
  120. +17 −0 min_unit_tests/test_all.php
  121. +100 −0 min_unit_tests/test_environment.php
  122. +4 −0 min_unit_tests/test_js_in_browser.html
View
4 min/.htaccess
@@ -0,0 +1,4 @@
+<IfModule mod_rewrite.c>
+RewriteEngine on
+RewriteRule ^([a-z]=.*) index.php?$1 [L,NE]
+</IfModule>
View
75 min/HISTORY.txt
@@ -0,0 +1,75 @@
+Minify Release History
+
+Version 2.1.3
+ * HTTP fixes
+ * ETag generation now valid (different when gzipped)
+ * Vary header always sent when Accept-Encoding is sniffed
+ * Cache-Control no longer has "must-revalidate" due to webkit bug
+ See: http://mrclay.org/index.php/2009/02/24/safari-4-beta-cache-controlmust-revalidate-bug/
+ * Dropped deflate encoding. Browser and proxy support could be buggy.
+ See: http://stackoverflow.com/questions/883841/
+ * File cache now works w/o setting $min_cachePath
+ * Allow setting contentType in Minify_Source objects
+ * No more 5.3 deprecation warnings: split() removed
+
+Version 2.1.2
+ * Javascript fixes
+ * Debug mode no longer confused by "*/*" in strings/RegExps (jQuery)
+ * quote characters inside RegExp literals no longer cause exception
+ * files ending in single-line comments no longer cause code loss
+ * CSS: data: URLs no longer mangled
+ * Optional error logging to Firefox's FirePHP extension
+ * Unit tests to check for common DOCUMENT_ROOT problems
+ * DOCUMENT_ROOT no longer overwritten on IIS servers
+ * Builder app doesn't fail on systems without gzdeflate()
+ * APC caching class included
+
+Version 2.1.1
+ * Bug fix release
+ * Detection and workarounds for zlib.output_compression and non-PHP encoding modules
+ * Zlib not required (mod_rewrite, et.al., can still be used for encoding)
+ * HTML : More IE conditional comments preserved
+ * Minify_groupUri() utility fixed
+
+Version 2.1.0
+ * "min" default application for quick deployment
+ * Minify URI Builder app & bookmarklet for quickly creating minify URIs
+ * Relative URIs in CSS file are fixed automatically by default
+ * "debug" mode for revealing original line #s in combined files
+ * Better IIS support
+ * Improved minifier classes:
+ * JS: preserves IE conditional comments
+ * CSS: smaller output, preserves more hacks and valid CSS syntax,
+ shorter line lengths, other bug fixes
+ * HTML: smaller output, shorter line lengths, other bug fixes
+ * Default Cache-Control: max-age of 30 minutes
+ * Conditional GETs supported even when max-age sent
+ * Experimental memcache cache class (default is files)
+ * Minify_Cache_File has flock()s (by default)
+ * Workaround for Windows mtime reporting bug
+
+Version 2.0.2 beta (2008-06-24)
+ * Fast new cache system. Cached files served almost 3x as fast.
+ * Dropped support of compress encoding (though HTTP_Encoder still supports it)
+
+Version 2.0.1 (2008-05-31)
+ * E_STRICT compliance (Cache_Lite_File).
+
+Version 2.0.0 (2008-05-22)
+ * Complete code overhaul. Minify is now a PEAR-style class and toolkit
+ for building customized minifying file servers.
+ * Content-Encoding: deflate/gzip/compress, based on request headers
+ * Expanded CSS and HTML minifiers with test cases
+ * Easily plug-in 3rd-party minifiers (like Packer)
+ * Plug-able front end controller allows changing the way files are chosen
+ * Compression & encoding modules lazy-loaded as needed (304 responses use
+ use minimal code)
+ * Separate utility classes for HTTP encoding and cache control
+
+Version 1.0.1 (2007-05-05)
+ * Fixed various problems resolving pathnames when hosted on an NFS mount.
+ * Fixed 'undefined constant' notice.
+ * Replaced old JSMin library with a much faster custom implementation.
+
+Version 1.0.0 (2007-05-02)
+ * First release.
View
26 min/LICENSE.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2008 Ryan Grove <ryan@wonko.com>
+Copyright (c) 2008 Steve Clay <steve@mrclay.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name of this project nor the names of its contributors may be
+ used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
132 min/README.txt
@@ -0,0 +1,132 @@
+The files in this directory represent the default Minify setup designed to ease
+integration with your site. This app will combine and minify your Javascript or
+CSS files and serve them with HTTP compression and cache headers.
+
+
+RECOMMENDED
+
+It's recommended to edit config.php to set $min_cachePath to a writeable
+(by PHP) directory on your system. This will improve performance.
+
+
+GETTING STARTED
+
+The quickest way to get started is to use the Minify URI Builder application
+on your website: http://example.com/min/builder/
+
+
+MINIFYING A SINGLE FILE
+
+Let's say you want to serve this file:
+ http://example.com/wp-content/themes/default/default.css
+
+Here's the "Minify URL" for this file:
+ http://example.com/min/?f=wp-content/themes/default/default.css
+
+In other words, the "f" argument is set to the file path from root without the
+initial "/". As CSS files may contain relative URIs, Minify will automatically
+"fix" these by rewriting them as root relative.
+
+
+COMBINING MULTIPLE FILES IN ONE DOWNLOAD
+
+Separate the paths given to "f" with commas.
+
+Let's say you have CSS files at these URLs:
+ http://example.com/scripts/jquery-1.2.6.js
+ http://example.com/scripts/site.js
+
+You can combine these files through Minify by requesting this URL:
+ http://example.com/min/?f=scripts/jquery-1.2.6.js,scripts/site.js
+
+
+SIMPLIFYING URLS WITH A BASE PATH
+
+If you're combining files that share the same ancestor directory, you can use
+the "b" argument to set the base directory for the "f" argument. Do not include
+the leading or trailing "/" characters.
+
+E.g., the following URLs will serve the exact same content:
+ http://example.com/min/?f=scripts/jquery-1.2.6.js,scripts/site.js,scripts/home.js
+ http://example.com/min/?b=scripts&f=jquery-1.2.6.js,site.js,home.js
+
+
+MINIFY URLS IN HTML
+
+In (X)HTML files, don't forget to replace any "&" characters with "&amp;".
+
+
+SPECIFYING ALLOWED DIRECTORIES
+
+By default, Minify will serve any *.css/*.js files within the DOCUMENT_ROOT. If
+you'd prefer to limit Minify's access to certain directories, set the
+$min_serveOptions['minApp']['allowDirs'] array in config.php. E.g. to limit
+to the /js and /themes/default directories, use:
+
+$min_serveOptions['minApp']['allowDirs'] = array('//js', '//themes/default');
+
+
+GROUPS: FASTER PERFORMANCE AND BETTER URLS
+
+For the best performance, edit groupsConfig.php to pre-specify groups of files
+to be combined under preset keys. E.g., here's an example configuration in
+groupsConfig.php:
+
+return array(
+ 'js' => array('//js/Class.js', '//js/email.js')
+);
+
+This pre-selects the following files to be combined under the key "js":
+ http://example.com/js/Class.js
+ http://example.com/js/email.js
+
+You can now serve these files with this simple URL:
+ http://example.com/min/?g=js
+
+
+GROUPS: SPECIFYING FILES OUTSIDE THE DOC_ROOT
+
+In the groupsConfig.php array, the "//" in the file paths is a shortcut for
+the DOCUMENT_ROOT, but you can also specify paths from the root of the filesystem
+or relative to the DOC_ROOT:
+
+return array(
+ 'js' => array(
+ '//js/file.js' // file within DOC_ROOT
+ ,'//../file.js' // file in parent directory of DOC_ROOT
+ ,'C:/Users/Steve/file.js' // file anywhere on filesystem
+ )
+);
+
+
+FAR-FUTURE EXPIRES HEADERS
+
+Minify can send far-future (one year) Expires headers. To enable this you must
+add a number to the querystring (e.g. /min/?g=js&1234 or /min/f=file.js&1234)
+and alter it whenever a source file is changed. If you have a build process you
+can use a build/source control revision number.
+
+If you serve files as a group, you can use the utility function Minify_groupUri()
+to get a "versioned" Minify URI for use in your HTML. E.g.:
+
+<?php
+// add /min/lib to your include_path first!
+require $_SERVER['DOCUMENT_ROOT'] . '/min/utils.php';
+
+$jsUri = Minify_groupUri('js');
+echo "<script type='text/javascript' src='{$jsUri}'></script>";
+
+
+DEBUG MODE
+
+In debug mode, instead of compressing files, Minify sends combined files with
+comments prepended to each line to show the line number in the original source
+file. To enable this, set $min_allowDebugFlag to true in config.php and append
+"&debug=1" to your URIs. E.g. /min/?f=script1.js,script2.js&debug=1
+
+Known issue: files with comment-like strings/regexps can cause problems in this mode.
+
+
+QUESTIONS?
+
+http://groups.google.com/group/minify
View
53 min/README_1.txt
@@ -0,0 +1,53 @@
+WELCOME TO MINIFY 2.1!
+
+Minify is an HTTP content server. It compresses sources of content
+(usually files), combines the result and serves it with appropriate
+HTTP headers. These headers can allow clients to perform conditional
+GETs (serving content only when clients do not have a valid cache)
+and tell clients to cache the file for a period of time.
+More info: http://code.google.com/p/minify/
+
+
+UPGRADING
+
+See UPGRADING.txt for instructions.
+
+
+INSTALLATION AND USAGE:
+
+1. Place the /min/ directory as a child of your DOCUMENT_ROOT
+directory: i.e. you will have: /home/user/www/public_html/min
+
+2. Open http://yourdomain/min/ in a web browser. This will forward
+you to the Minify URI Builder application, which will help you
+quickly start using Minify to serve content on your site.
+
+
+UNIT TESTING:
+
+1. Place the /min_unit_tests/ directory as a child of your DOCUMENT_ROOT
+directory: i.e. you will have: /home/user/www/public_html/min_unit_tests
+
+2. To run unit tests, access: http://yourdomain/min_unit_tests/test_all.php
+
+(If you wish, the other test_*.php files can be run to test individual
+components with more verbose output.)
+
+3. Remove /min_unit_tests/ from your DOCUMENT_ROOT when you are done.
+
+
+EXTRAS:
+
+The min_extras folder contains files for benchmarking using Apache ab on Windows
+and a couple single-use tools. DO NOT place this on your production server.
+
+
+FILE ENCODINGS
+
+Minify *should* work fine with files encoded in UTF-8 or other 8-bit
+encodings like ISO 8859/Windows-1252. By default Minify appends
+";charset=utf-8" to the Content-Type headers it sends.
+
+Leading UTF-8 BOMs are stripped from all sources to prevent
+duplication in output files, and files are converted to Unix newlines.
+
View
35 min/UPGRADING.txt
@@ -0,0 +1,35 @@
+Minify Upgrade Guide
+
+UPGRADING FROM 2.1.*
+
+1. Rename the following files:
+
+ /min/config.php --> /min/old_config.php
+ /min/groupsConfig.php --> /min/old_groupsConfig.php
+
+2. Overwrite all files in /min (and /min_unit_tests) with those from this zip.
+
+3. Delete /min/groupsConfig.php
+
+4. Rename /min/old_groupsConfig.php --> /min/groupsConfig.php
+
+5. Merge your settings in old_config.php into config.php.
+
+ * If you've set $_SERVER['DOCUMENT_ROOT'], instead set the new option
+ $min_documentRoot. This is advantageous on IIS systems because Minify
+ will no longer overwrite the path you specified.
+
+ * $min_errorLogger adds the ability to enable FirePHP logging.
+
+6. (optional) Delete /min/old_config.php and the Minify files from your cache
+ directory (specified in $min_cachePath).
+
+
+INSTALLING FRESH
+
+See README.txt for instructions on installing this app for the first time.
+
+
+SUPPORT
+
+Send a message to http://groups.google.com/group/minify
View
242 min/builder/_index.js
@@ -0,0 +1,242 @@
+var MUB = {
+ _uid : 0
+ ,_minRoot : '/min/?'
+ ,checkRewrite : function () {
+ var testUri = location.pathname.replace(/\/[^\/]*$/, '/rewriteTest.js').substr(1);
+ function fail() {
+ $('#minRewriteFailed')[0].className = 'topNote';
+ };
+ $.ajax({
+ url : '../f=' + testUri + '&' + (new Date()).getTime()
+ ,success : function (data) {
+ if (data === '1') {
+ MUB._minRoot = '/min/';
+ $('span.minRoot').html('/min/');
+ } else
+ fail();
+ }
+ ,error : fail
+ });
+ }
+ /**
+ * Get markup for new source LI element
+ */
+ ,newLi : function () {
+ return '<li id="li' + MUB._uid + '">http://' + location.host + '/<input type=text size=20>'
+ + ' <button title="Remove">x</button> <button title="Include Earlier">&uarr;</button>'
+ + ' <button title="Include Later">&darr;</button> <span></span></li>';
+ }
+ /**
+ * Add new empty source LI and attach handlers to buttons
+ */
+ ,addLi : function () {
+ $('#sources').append(MUB.newLi());
+ var li = $('#li' + MUB._uid)[0];
+ $('button[title=Remove]', li).click(function () {
+ $('#results').hide();
+ var hadValue = !!$('input', li)[0].value;
+ $(li).remove();
+ });
+ $('button[title$=Earlier]', li).click(function () {
+ $(li).prev('li').find('input').each(function () {
+ $('#results').hide();
+ // this = previous li input
+ var tmp = this.value;
+ this.value = $('input', li).val();
+ $('input', li).val(tmp);
+ MUB.updateAllTestLinks();
+ });
+ });
+ $('button[title$=Later]', li).click(function () {
+ $(li).next('li').find('input').each(function () {
+ $('#results').hide();
+ // this = next li input
+ var tmp = this.value;
+ this.value = $('input', li).val();
+ $('input', li).val(tmp);
+ MUB.updateAllTestLinks();
+ });
+ });
+ ++MUB._uid;
+ }
+ /**
+ * In the context of a source LI element, this will analyze the URI in
+ * the INPUT and check the URL on the site.
+ */
+ ,liUpdateTestLink : function () { // call in context of li element
+ if (! $('input', this)[0].value)
+ return;
+ var li = this;
+ $('span', this).html('');
+ var url = 'http://' + location.host + '/'
+ + $('input', this)[0].value.replace(/^\//, '');
+ $.ajax({
+ url : url
+ ,complete : function (xhr, stat) {
+ if ('success' == stat)
+ $('span', li).html('&#x2713;');
+ else {
+ $('span', li).html('<button><b>404! </b> recheck</button>')
+ .find('button').click(function () {
+ MUB.liUpdateTestLink.call(li);
+ });
+ }
+ }
+ ,dataType : 'text'
+ });
+ }
+ /**
+ * Check all source URLs
+ */
+ ,updateAllTestLinks : function () {
+ $('#sources li').each(MUB.liUpdateTestLink);
+ }
+ /**
+ * In a given array of strings, find the character they all have at
+ * a particular index
+ * @param Array arr array of strings
+ * @param Number pos index to check
+ * @return mixed a common char or '' if any do not match
+ */
+ ,getCommonCharAtPos : function (arr, pos) {
+ var i
+ ,l = arr.length
+ ,c = arr[0].charAt(pos);
+ if (c === '' || l === 1)
+ return c;
+ for (i = 1; i < l; ++i)
+ if (arr[i].charAt(pos) !== c)
+ return '';
+ return c;
+ }
+ /**
+ * Get the shortest URI to minify the set of source files
+ * @param Array sources URIs
+ */
+ ,getBestUri : function (sources) {
+ var pos = 0
+ ,base = ''
+ ,c;
+ while (true) {
+ c = MUB.getCommonCharAtPos(sources, pos);
+ if (c === '')
+ break;
+ else
+ base += c;
+ ++pos;
+ }
+ base = base.replace(/[^\/]+$/, '');
+ var uri = MUB._minRoot + 'f=' + sources.join(',');
+ if (base.charAt(base.length - 1) === '/') {
+ // we have a base dir!
+ var basedSources = sources
+ ,i
+ ,l = sources.length;
+ for (i = 0; i < l; ++i) {
+ basedSources[i] = sources[i].substr(base.length);
+ }
+ base = base.substr(0, base.length - 1);
+ var bUri = MUB._minRoot + 'b=' + base + '&f=' + basedSources.join(',');
+ //window.console && console.log([uri, bUri]);
+ uri = uri.length < bUri.length
+ ? uri
+ : bUri;
+ }
+ return uri;
+ }
+ /**
+ * Create the Minify URI for the sources
+ */
+ ,update : function () {
+ MUB.updateAllTestLinks();
+ var sources = []
+ ,ext = false
+ ,fail = false;
+ $('#sources input').each(function () {
+ var m, val;
+ if (! fail && this.value && (m = this.value.match(/\.(css|js)$/))) {
+ var thisExt = m[1];
+ if (ext === false)
+ ext = thisExt;
+ else if (thisExt !== ext) {
+ fail = true;
+ return alert('extensions must match!');
+ }
+ this.value = this.value.replace(/^\//, '');
+ if (-1 != $.inArray(this.value, sources)) {
+ fail = true;
+ return alert('duplicate file!');
+ }
+ sources.push(this.value);
+ }
+ });
+ if (fail || ! sources.length)
+ return;
+ $('#groupConfig').val(" 'keyName' => array('//" + sources.join("', '//") + "'),");
+ var uri = MUB.getBestUri(sources)
+ ,uriH = uri.replace(/</, '&lt;').replace(/>/, '&gt;').replace(/&/, '&amp;');
+ $('#uriA').html(uriH)[0].href = uri;
+ $('#uriHtml').val(
+ ext === 'js'
+ ? '<script type="text/javascript" src="' + uriH + '"></script>'
+ : '<link type="text/css" rel="stylesheet" href="' + uriH + '" />'
+ );
+ $('#results').show();
+ }
+ /**
+ * Handler for the "Add file +" button
+ */
+ ,addButtonClick : function () {
+ $('#results').hide();
+ MUB.addLi();
+ MUB.updateAllTestLinks();
+ $('#update').show().click(MUB.update);
+ $('#sources li:last input')[0].focus();
+ }
+ /**
+ * Runs on DOMready
+ */
+ ,init : function () {
+ $('#app').show();
+ $('#sources').html('');
+ $('#add button').click(MUB.addButtonClick);
+ // make easier to copy text out of
+ $('#uriHtml, #groupConfig').click(function () {
+ this.select();
+ }).focus(function () {
+ this.select();
+ });
+ $('a.ext').attr({target:'_blank'});
+ if (location.hash) {
+ // make links out of URIs from bookmarklet
+ $('#getBm').hide();
+ $('#bmUris').html('<p><strong>Found by bookmarklet:</strong> /<a href=#>'
+ + location.hash.substr(1).split(',').join('</a> | /<a href=#>')
+ + '</a></p>'
+ );
+ $('#bmUris a').click(function () {
+ MUB.addButtonClick();
+ $('#sources li:last input').val(this.innerHTML)
+ MUB.liUpdateTestLink.call($('#sources li:last')[0]);
+ $('#results').hide();
+ return false;
+ }).attr({title:'Add file +'});
+ } else {
+ // copy bookmarklet code into href
+ var bmUri = location.pathname.replace(/\/[^\/]*$/, '/bm.js').substr(1);
+ $.ajax({
+ url : '../?f=' + bmUri
+ ,success : function (code) {
+ $('#bm')[0].href = code
+ .replace('%BUILDER_URL%', location.href)
+ .replace(/\n/g, ' ');
+ }
+ ,dataType : 'text'
+ });
+ $.browser.msie && $('#getBm p:last').append(' Sorry, not supported in MSIE!');
+ MUB.addButtonClick();
+ }
+ MUB.checkRewrite();
+ }
+};
+window.onload = MUB.init;
View
36 min/builder/bm.js
@@ -0,0 +1,36 @@
+javascript:(function() {
+ var d = document
+ ,uris = []
+ ,i = 0
+ ,o
+ ,home = (location + '').split('/').splice(0, 3).join('/') + '/';
+ function add(uri) {
+ return (0 === uri.indexOf(home))
+ && (!/[\?&]/.test(uri))
+ && uris.push(escape(uri.substr(home.length)));
+ };
+ function sheet(ss) {
+ // we must check the domain with add() before accessing ss.cssRules
+ // otherwise a security exception will be thrown
+ if (ss.href && add(ss.href) && ss.cssRules) {
+ var i = 0, r;
+ while (r = ss.cssRules[i++])
+ r.styleSheet && sheet(r.styleSheet);
+ }
+ };
+ while (o = d.getElementsByTagName('script')[i++])
+ o.src && !(o.type && /vbs/i.test(o.type)) && add(o.src);
+ i = 0;
+ while (o = d.styleSheets[i++])
+ /* http://www.w3.org/TR/DOM-Level-2-Style/stylesheets.html#StyleSheets-DocumentStyle-styleSheets
+ document.styleSheet is a list property where [0] accesses the 1st element and
+ [outOfRange] returns null. In IE, styleSheets is a function, and also throws an
+ exception when you check the out of bounds index. (sigh) */
+ sheet(o);
+ if (uris.length)
+ window.open('%BUILDER_URL%#' + uris.join(','));
+ else
+ alert('No js/css files found with URLs within "'
+ + home.split('/')[2]
+ + '".\n(This tool is limited to URLs with the same domain.)');
+})();
View
182 min/builder/index.php
@@ -0,0 +1,182 @@
+<?php
+
+if (phpversion() < 5) {
+ exit('Minify requires PHP5 or greater.');
+}
+
+// check for auto-encoding
+$encodeOutput = (function_exists('gzdeflate')
+ && !ini_get('zlib.output_compression'));
+
+require dirname(__FILE__) . '/../config.php';
+
+if (! $min_enableBuilder) {
+ header('Location: /');
+ exit();
+}
+
+ob_start();
+?>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<head>
+ <meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
+ <title>Minify URI Builder</title>
+ <style type="text/css">
+body {margin:1em 60px;}
+h1, h2, h3 {margin-left:-25px; position:relative;}
+h1 {margin-top:0;}
+#sources {margin:0; padding:0;}
+#sources li {margin:0 0 0 40px}
+#sources li input {margin-left:2px}
+#add {margin:5px 0 1em 40px}
+.hide {display:none}
+#uriTable {border-collapse:collapse;}
+#uriTable td, #uriTable th {padding-top:10px;}
+#uriTable th {padding-right:10px;}
+#groupConfig {font-family:monospace;}
+b {color:#c00}
+.topNote {background: #ff9; display:inline-block; padding:.5em .6em; margin:0 0 1em;}
+.topWarning {background:#c00; color:#fff; padding:.5em .6em; margin:0 0 1em;}
+ </style>
+</head>
+
+<?php if (! isset($min_cachePath)): ?>
+<p class=topNote><strong>Note:</strong> Please set <code>$min_cachePath</code>
+in /min/config.php to improve performance.</p>
+<?php endIf; ?>
+
+<p id=minRewriteFailed class="hide"><strong>Note:</strong> Your webserver does not seem to
+ support mod_rewrite (used in /min/.htaccess). Your Minify URIs will contain "?", which
+<a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/"
+>may reduce the benefit of proxy cache servers</a>.</p>
+
+<h1>Minify URI Builder</h1>
+
+<noscript><p class="topNote">Javascript and a browser supported by jQuery 1.2.6 is required
+for this application.</p></noscript>
+
+<div id=app class=hide>
+
+<p>Create a list of Javascript or CSS files (or 1 is fine) you'd like to combine
+and click [Update].</p>
+
+<ol id=sources><li></li></ol>
+<div id=add><button>Add file +</button></div>
+
+<div id=bmUris></div>
+
+<p><button id=update class=hide>Update</button></p>
+
+<div id=results class=hide>
+
+<h2>Minify URI</h2>
+<p>Place this URI in your HTML to serve the files above combined, minified, compressed and
+with cache headers.</p>
+<table id=uriTable>
+ <tr><th>URI</th><td><a id=uriA class=ext>/min</a> <small>(opens in new window)</small></td></tr>
+ <tr><th>HTML</th><td><input id=uriHtml type=text size=100 readonly></td></tr>
+</table>
+
+<h2>How to serve these files as a group</h2>
+<p>For the best performance you can serve these files as a pre-defined group with a URI
+like: <code><span class=minRoot>/min/?</span>g=keyName</code></p>
+<p>To do this, add a line like this to /min/groupsConfig.php:</p>
+
+<pre><code>return array(
+ <span style="color:#666">... your existing groups here ...</span>
+<input id=groupConfig size=100 type=text readonly>
+);</code></pre>
+
+<p><em>Make sure to replace <code>keyName</code> with a unique key for this group.</em></p>
+</div>
+
+<div id=getBm>
+<h3>Find URIs on a Page</h3>
+<p>You can use the bookmarklet below to fetch all CSS &amp; Javascript URIs from a page
+on your site. When you active it, this page will open in a new window with a list of
+available URIs to add.</p>
+
+<p><a id=bm>Create Minify URIs</a> <small>(right-click, add to bookmarks)</small></p>
+</div>
+
+<h3>Combining CSS files that contain <code>@import</code></h3>
+<p>If your CSS files contain <code>@import</code> declarations, Minify will not
+remove them. Therefore, you will want to remove those that point to files already
+in your list, and move any others to the top of the first file in your list
+(imports below any styles will be ignored by browsers as invalid).</p>
+<p>If you desire, you can use Minify URIs in imports and they will not be touched
+by Minify. E.g. <code>@import "<span class=minRoot>/min/?</span>g=css2";</code></p>
+
+</div><!-- #app -->
+
+<hr>
+<p>Need help? Search or post to the <a class=ext
+href="http://groups.google.com/group/minify">Minify discussion list</a>.</p>
+<p><small>This app is minified :) <a class=ext
+href="http://code.google.com/p/minify/source/browse/trunk/min/builder/index.php">view
+source</a></small></p>
+
+<script type="text/javascript"
+src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
+
+<script type="text/javascript">
+$(function () {
+ // detection of double output encoding
+ var msg = '<\p class=topWarning><\strong>Warning:<\/strong> ';
+ var url = 'ocCheck.php?' + (new Date()).getTime();
+ $.get(url, function (ocStatus) {
+ $.get(url + '&hello=1', function (ocHello) {
+ if (ocHello != 'World!') {
+ msg += 'It appears output is being automatically compressed, interfering '
+ + ' with Minify\'s own compression. ';
+ if (ocStatus == '1')
+ msg += 'The option "zlib.output_compression" is enabled in your PHP configuration. '
+ + 'Minify set this to "0", but it had no effect. This option must be disabled '
+ + 'in php.ini or .htaccess.';
+ else
+ msg += 'The option "zlib.output_compression" is disabled in your PHP configuration '
+ + 'so this behavior is likely due to a server option.';
+ $(document.body).prepend(msg + '<\/p>');
+ } else
+ if (ocStatus == '1')
+ $(document.body).prepend('<\p class=topNote><\strong>Note:</\strong> The option '
+ + '"zlib.output_compression" is enabled in your PHP configuration, but has been '
+ + 'successfully disabled via ini_set(). If you experience mangled output you '
+ + 'may want to consider disabling this option in your PHP configuration.<\/p>'
+ );
+ });
+ });
+});
+</script>
+<script type="text/javascript">
+ // workaround required to test when /min isn't child of web root
+ var src = location.pathname.replace(/\/[^\/]*$/, '/_index.js').substr(1);
+ document.write('<\script type="text/javascript" src="../?f=' + src + '"><\/script>');
+</script>
+
+<?php
+
+$serveOpts = array(
+ 'content' => ob_get_contents()
+ ,'id' => __FILE__
+ ,'lastModifiedTime' => max(
+ // regenerate cache if either of these change
+ filemtime(__FILE__)
+ ,filemtime(dirname(__FILE__) . '/../config.php')
+ )
+ ,'minifyAll' => true
+ ,'encodeOutput' => $encodeOutput
+);
+ob_end_clean();
+
+set_include_path(dirname(__FILE__) . '/../lib' . PATH_SEPARATOR . get_include_path());
+
+require 'Minify.php';
+
+if (0 === stripos(PHP_OS, 'win')) {
+ Minify::setDocRoot(); // we may be on IIS
+}
+Minify::setCache(isset($min_cachePath) ? $min_cachePath : null);
+Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
+
+Minify::serve('Page', $serveOpts);
View
36 min/builder/ocCheck.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * AJAX checks for zlib.output_compression
+ *
+ * @package Minify
+ */
+
+$_oc = ini_get('zlib.output_compression');
+
+// allow access only if builder is enabled
+require dirname(__FILE__) . '/../config.php';
+if (! $min_enableBuilder) {
+ header('Location: /');
+ exit();
+}
+
+if (isset($_GET['hello'])) {
+ // echo 'World!'
+
+ // try to prevent double encoding (may not have an effect)
+ ini_set('zlib.output_compression', '0');
+
+ require $min_libPath . '/HTTP/Encoder.php';
+ HTTP_Encoder::$encodeToIe6 = true; // just in case
+ $he = new HTTP_Encoder(array(
+ 'content' => 'World!'
+ ,'method' => 'deflate'
+ ));
+ $he->encode();
+ $he->sendAll();
+
+} else {
+ // echo status "0" or "1"
+ header('Content-Type: text/plain');
+ echo (int)$_oc;
+}
View
1  min/builder/rewriteTest.js
@@ -0,0 +1 @@
+1
View
156 min/config.php
@@ -0,0 +1,156 @@
+<?php
+/**
+ * Configuration for default Minify application
+ * @package Minify
+ */
+
+
+/**
+ * In 'debug' mode, Minify can combine files with no minification and
+ * add comments to indicate line #s of the original files.
+ *
+ * To allow debugging, set this option to true and add "&debug=1" to
+ * a URI. E.g. /min/?f=script1.js,script2.js&debug=1
+ */
+$min_allowDebugFlag = false;
+
+
+/**
+ * Set to true to log messages to FirePHP (Firefox Firebug addon).
+ * Set to false for no error logging (Minify may be slightly faster).
+ * @link http://www.firephp.org/
+ *
+ * If you want to use a custom error logger, set this to your logger
+ * instance. Your object should have a method log(string $message).
+ *
+ * @todo cache system does not have error logging yet.
+ */
+$min_errorLogger = false;
+
+
+/**
+ * Allow use of the Minify URI Builder app. If you no longer need
+ * this, set to false.
+ **/
+$min_enableBuilder = true;
+
+
+/**
+ * For best performance, specify your temp directory here. Otherwise Minify
+ * will have to load extra code to guess. Some examples below:
+ */
+//$min_cachePath = 'c:\\WINDOWS\\Temp';
+//$min_cachePath = '/tmp';
+//$min_cachePath = preg_replace('/^\\d+;/', '', session_save_path());
+
+
+/**
+ * Leave an empty string to use PHP's $_SERVER['DOCUMENT_ROOT'].
+ *
+ * On some servers, this value may be misconfigured or missing. If so, set this
+ * to your full document root path with no trailing slash.
+ * E.g. '/home/accountname/public_html' or 'c:\\xampp\\htdocs'
+ *
+ * If /min/ is directly inside your document root, just uncomment the
+ * second line. The third line might work on some Apache servers.
+ */
+$min_documentRoot = '';
+//$min_documentRoot = substr(__FILE__, 0, strlen(__FILE__) - 15);
+//$min_documentRoot = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];
+
+
+/**
+ * Cache file locking. Set to false if filesystem is NFS. On at least one
+ * NFS system flock-ing attempts stalled PHP for 30 seconds!
+ */
+$min_cacheFileLocking = true;
+
+
+/**
+ * Combining multiple CSS files can place @import declarations after rules, which
+ * is invalid. Minify will attempt to detect when this happens and place a
+ * warning comment at the top of the CSS output. To resolve this you can either
+ * move the @imports within your CSS files, or enable this option, which will
+ * move all @imports to the top of the output. Note that moving @imports could
+ * affect CSS values (which is why this option is disabled by default).
+ */
+$min_serveOptions['bubbleCssImports'] = false;
+
+
+/**
+ * Maximum age of browser cache in seconds. After this period, the browser
+ * will send another conditional GET. Use a longer period for lower traffic
+ * but you may want to shorten this before making changes if it's crucial
+ * those changes are seen immediately.
+ *
+ * Note: Despite this setting, if you include a number at the end of the
+ * querystring, maxAge will be set to one year. E.g. /min/f=hello.css&123456
+ */
+$min_serveOptions['maxAge'] = 1800;
+
+
+/**
+ * If you'd like to restrict the "f" option to files within/below
+ * particular directories below DOCUMENT_ROOT, set this here.
+ * You will still need to include the directory in the
+ * f or b GET parameters.
+ *
+ * // = shortcut for DOCUMENT_ROOT
+ */
+//$min_serveOptions['minApp']['allowDirs'] = array('//js', '//css');
+
+/**
+ * Set to true to disable the "f" GET parameter for specifying files.
+ * Only the "g" parameter will be considered.
+ */
+$min_serveOptions['minApp']['groupsOnly'] = false;
+
+/**
+ * Maximum # of files that can be specified in the "f" GET parameter
+ */
+$min_serveOptions['minApp']['maxFiles'] = 10;
+
+
+/**
+ * If you minify CSS files stored in symlink-ed directories, the URI rewriting
+ * algorithm can fail. To prevent this, provide an array of link paths to
+ * target paths, where the link paths are within the document root.
+ *
+ * Because paths need to be normalized for this to work, use "//" to substitute
+ * the doc root in the link paths (the array keys). E.g.:
+ * <code>
+ * array('//symlink' => '/real/target/path') // unix
+ * array('//static' => 'D:\\staticStorage') // Windows
+ * </code>
+ */
+$min_symlinks = array();
+
+
+/**
+ * If you upload files from Windows to a non-Windows server, Windows may report
+ * incorrect mtimes for the files. This may cause Minify to keep serving stale
+ * cache files when source file changes are made too frequently (e.g. more than
+ * once an hour).
+ *
+ * Immediately after modifying and uploading a file, use the touch command to
+ * update the mtime on the server. If the mtime jumps ahead by a number of hours,
+ * set this variable to that number. If the mtime moves back, this should not be
+ * needed.
+ *
+ * In the Windows SFTP client WinSCP, there's an option that may fix this
+ * issue without changing the variable below. Under login > environment,
+ * select the option "Adjust remote timestamp with DST".
+ * @link http://winscp.net/eng/docs/ui_login_environment#daylight_saving_time
+ */
+$min_uploaderHoursBehind = 0;
+
+
+/**
+ * Path to Minify's lib folder. If you happen to move it, change
+ * this accordingly.
+ */
+$min_libPath = dirname(__FILE__) . '/lib';
+
+
+// try to disable output_compression (may not have an effect)
+ini_set('zlib.output_compression', '0');
View
34 min/groupsConfig.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Groups configuration for default Minify implementation
+ * @package Minify
+ */
+
+/**
+ * You may wish to use the Minify URI Builder app to suggest
+ * changes. http://yourdomain/min/builder/
+ **/
+
+return array(
+ // 'js' => array('//js/file1.js', '//js/file2.js'),
+ // 'css' => array('//css/file1.css', '//css/file2.css'),
+
+ // custom source example
+ /*'js2' => array(
+ dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
+ // do NOT process this file
+ new Minify_Source(array(
+ 'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
+ 'minifier' => create_function('$a', 'return $a;')
+ ))
+ ),//*/
+
+ /*'js3' => array(
+ dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
+ // do NOT process this file
+ new Minify_Source(array(
+ 'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
+ 'minifier' => array('Minify_Packer', 'minify')
+ ))
+ ),//*/
+);
View
66 min/index.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Front controller for default Minify implementation
+ *
+ * DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
+ *
+ * @package Minify
+ */
+
+define('MINIFY_MIN_DIR', dirname(__FILE__));
+
+// load config
+require MINIFY_MIN_DIR . '/config.php';
+
+// setup include path
+set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
+
+require 'Minify.php';
+
+Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
+Minify::setCache(
+ isset($min_cachePath) ? $min_cachePath : ''
+ ,$min_cacheFileLocking
+);
+
+if ($min_documentRoot) {
+ $_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
+} elseif (0 === stripos(PHP_OS, 'win')) {
+ Minify::setDocRoot(); // IIS may need help
+}
+
+$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
+
+if ($min_allowDebugFlag && isset($_GET['debug'])) {
+ $min_serveOptions['debug'] = true;
+}
+
+if ($min_errorLogger) {
+ require_once 'Minify/Logger.php';
+ if (true === $min_errorLogger) {
+ require_once 'FirePHP.php';
+ Minify_Logger::setLogger(FirePHP::getInstance(true));
+ } else {
+ Minify_Logger::setLogger($min_errorLogger);
+ }
+}
+
+// check for URI versioning
+if (preg_match('/&\\d/', $_SERVER['QUERY_STRING'])) {
+ $min_serveOptions['maxAge'] = 31536000;
+}
+if (isset($_GET['g'])) {
+ // well need groups config
+ $min_serveOptions['minApp']['groups'] = (require MINIFY_MIN_DIR . '/groupsConfig.php');
+}
+if (isset($_GET['f']) || isset($_GET['g'])) {
+ // serve!
+ Minify::serve('MinApp', $min_serveOptions);
+
+} elseif ($min_enableBuilder) {
+ header('Location: builder/');
+ exit();
+} else {
+ header("Location: /");
+ exit();
+}
View
1,370 min/lib/FirePHP.php
@@ -0,0 +1,1370 @@
+<?php
+/**
+ * *** BEGIN LICENSE BLOCK *****
+ *
+ * This file is part of FirePHP (http://www.firephp.org/).
+ *
+ * Software License Agreement (New BSD License)
+ *
+ * Copyright (c) 2006-2008, Christoph Dorn
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Christoph Dorn nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ***** END LICENSE BLOCK *****
+ *
+ * @copyright Copyright (C) 2007-2008 Christoph Dorn
+ * @author Christoph Dorn <christoph@christophdorn.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * @package FirePHP
+ */
+
+
+/**
+ * Sends the given data to the FirePHP Firefox Extension.
+ * The data can be displayed in the Firebug Console or in the
+ * "Server" request tab.
+ *
+ * For more information see: http://www.firephp.org/
+ *
+ * @copyright Copyright (C) 2007-2008 Christoph Dorn
+ * @author Christoph Dorn <christoph@christophdorn.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ * @package FirePHP
+ */
+class FirePHP {
+
+ /**
+ * FirePHP version
+ *
+ * @var string
+ */
+ const VERSION = '0.2.0';
+
+ /**
+ * Firebug LOG level
+ *
+ * Logs a message to firebug console.
+ *
+ * @var string
+ */
+ const LOG = 'LOG';
+
+ /**
+ * Firebug INFO level
+ *
+ * Logs a message to firebug console and displays an info icon before the message.
+ *
+ * @var string
+ */
+ const INFO = 'INFO';
+
+ /**
+ * Firebug WARN level
+ *
+ * Logs a message to firebug console, displays an warning icon before the message and colors the line turquoise.
+ *
+ * @var string
+ */
+ const WARN = 'WARN';
+
+ /**
+ * Firebug ERROR level
+ *
+ * Logs a message to firebug console, displays an error icon before the message and colors the line yellow. Also increments the firebug error count.
+ *
+ * @var string
+ */
+ const ERROR = 'ERROR';
+
+ /**
+ * Dumps a variable to firebug's server panel
+ *
+ * @var string
+ */
+ const DUMP = 'DUMP';
+
+ /**
+ * Displays a stack trace in firebug console
+ *
+ * @var string
+ */
+ const TRACE = 'TRACE';
+
+ /**
+ * Displays an exception in firebug console
+ *
+ * Increments the firebug error count.
+ *
+ * @var string
+ */
+ const EXCEPTION = 'EXCEPTION';
+
+ /**
+ * Displays an table in firebug console
+ *
+ * @var string
+ */
+ const TABLE = 'TABLE';
+
+ /**
+ * Starts a group in firebug console
+ *
+ * @var string
+ */
+ const GROUP_START = 'GROUP_START';
+
+ /**
+ * Ends a group in firebug console
+ *
+ * @var string
+ */
+ const GROUP_END = 'GROUP_END';
+
+ /**
+ * Singleton instance of FirePHP
+ *
+ * @var FirePHP
+ */
+ protected static $instance = null;
+
+ /**
+ * Wildfire protocol message index
+ *
+ * @var int
+ */
+ protected $messageIndex = 1;
+
+ /**
+ * Options for the library
+ *
+ * @var array
+ */
+ protected $options = array();
+
+ /**
+ * Filters used to exclude object members when encoding
+ *
+ * @var array
+ */
+ protected $objectFilters = array();
+
+ /**
+ * A stack of objects used to detect recursion during object encoding
+ *
+ * @var object
+ */
+ protected $objectStack = array();
+
+ /**
+ * Flag to enable/disable logging
+ *
+ * @var boolean
+ */
+ protected $enabled = true;
+
+ /**
+ * The object constructor
+ */
+ function __construct() {
+ $this->options['maxObjectDepth'] = 10;
+ $this->options['maxArrayDepth'] = 20;
+ $this->options['useNativeJsonEncode'] = true;
+ $this->options['includeLineNumbers'] = true;
+ }
+
+ /**
+ * When the object gets serialized only include specific object members.
+ *
+ * @return array
+ */
+ public function __sleep() {
+ return array('options','objectFilters','enabled');
+ }
+
+ /**
+ * Gets singleton instance of FirePHP
+ *
+ * @param boolean $AutoCreate
+ * @return FirePHP
+ */
+ public static function getInstance($AutoCreate=false) {
+ if($AutoCreate===true && !self::$instance) {
+ self::init();
+ }
+ return self::$instance;
+ }
+
+ /**
+ * Creates FirePHP object and stores it for singleton access
+ *
+ * @return FirePHP
+ */
+ public static function init() {
+ return self::$instance = new self();
+ }
+
+ /**
+ * Enable and disable logging to Firebug
+ *
+ * @param boolean $Enabled TRUE to enable, FALSE to disable
+ * @return void
+ */
+ public function setEnabled($Enabled) {
+ $this->enabled = $Enabled;
+ }
+
+ /**
+ * Check if logging is enabled
+ *
+ * @return boolean TRUE if enabled
+ */
+ public function getEnabled() {
+ return $this->enabled;
+ }
+
+ /**
+ * Specify a filter to be used when encoding an object
+ *
+ * Filters are used to exclude object members.
+ *
+ * @param string $Class The class name of the object
+ * @param array $Filter An array or members to exclude
+ * @return void
+ */
+ public function setObjectFilter($Class, $Filter) {
+ $this->objectFilters[$Class] = $Filter;
+ }
+
+ /**
+ * Set some options for the library
+ *
+ * Options:
+ * - maxObjectDepth: The maximum depth to traverse objects (default: 10)
+ * - maxArrayDepth: The maximum depth to traverse arrays (default: 20)
+ * - useNativeJsonEncode: If true will use json_encode() (default: true)
+ * - includeLineNumbers: If true will include line numbers and filenames (default: true)
+ *
+ * @param array $Options The options to be set
+ * @return void
+ */
+ public function setOptions($Options) {
+ $this->options = array_merge($this->options,$Options);
+ }
+
+ /**
+ * Register FirePHP as your error handler
+ *
+ * Will throw exceptions for each php error.
+ */
+ public function registerErrorHandler()
+ {
+ //NOTE: The following errors will not be caught by this error handler:
+ // E_ERROR, E_PARSE, E_CORE_ERROR,
+ // E_CORE_WARNING, E_COMPILE_ERROR,
+ // E_COMPILE_WARNING, E_STRICT
+
+ set_error_handler(array($this,'errorHandler'));
+ }
+
+ /**
+ * FirePHP's error handler
+ *
+ * Throws exception for each php error that will occur.
+ *
+ * @param int $errno
+ * @param string $errstr
+ * @param string $errfile
+ * @param int $errline
+ * @param array $errcontext
+ */
+ public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
+ {
+ // Don't throw exception if error reporting is switched off
+ if (error_reporting() == 0) {
+ return;
+ }
+ // Only throw exceptions for errors we are asking for
+ if (error_reporting() & $errno) {
+ throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
+ }
+ }
+
+ /**
+ * Register FirePHP as your exception handler
+ */
+ public function registerExceptionHandler()
+ {
+ set_exception_handler(array($this,'exceptionHandler'));
+ }
+
+ /**
+ * FirePHP's exception handler
+ *
+ * Logs all exceptions to your firebug console and then stops the script.
+ *
+ * @param Exception $Exception
+ * @throws Exception
+ */
+ function exceptionHandler($Exception) {
+ $this->fb($Exception);
+ }
+
+ /**
+ * Set custom processor url for FirePHP
+ *
+ * @param string $URL
+ */
+ public function setProcessorUrl($URL)
+ {
+ $this->setHeader('X-FirePHP-ProcessorURL', $URL);
+ }
+
+ /**
+ * Set custom renderer url for FirePHP
+ *
+ * @param string $URL
+ */
+ public function setRendererUrl($URL)
+ {
+ $this->setHeader('X-FirePHP-RendererURL', $URL);
+ }
+
+ /**
+ * Start a group for following messages
+ *
+ * @param string $Name
+ * @return true
+ * @throws Exception
+ */
+ public function group($Name) {
+ return $this->fb(null, $Name, FirePHP::GROUP_START);
+ }
+
+ /**
+ * Ends a group you have started before
+ *
+ * @return true
+ * @throws Exception
+ */
+ public function groupEnd() {
+ return $this->fb(null, null, FirePHP::GROUP_END);
+ }
+
+ /**
+ * Log object with label to firebug console
+ *
+ * @see FirePHP::LOG
+ * @param mixes $Object
+ * @param string $Label
+ * @return true
+ * @throws Exception
+ */
+ public function log($Object, $Label=null) {
+ return $this->fb($Object, $Label, FirePHP::LOG);
+ }
+
+ /**
+ * Log object with label to firebug console
+ *
+ * @see FirePHP::INFO
+ * @param mixes $Object
+ * @param string $Label
+ * @return true
+ * @throws Exception
+ */
+ public function info($Object, $Label=null) {
+ return $this->fb($Object, $Label, FirePHP::INFO);
+ }
+
+ /**
+ * Log object with label to firebug console
+ *
+ * @see FirePHP::WARN
+ * @param mixes $Object
+ * @param string $Label
+ * @return true
+ * @throws Exception
+ */
+ public function warn($Object, $Label=null) {
+ return $this->fb($Object, $Label, FirePHP::WARN);
+ }
+
+ /**
+ * Log object with label to firebug console
+ *
+ * @see FirePHP::ERROR
+ * @param mixes $Object
+ * @param string $Label
+ * @return true
+ * @throws Exception
+ */
+ public function error($Object, $Label=null) {
+ return $this->fb($Object, $Label, FirePHP::ERROR);
+ }
+
+ /**
+ * Dumps key and variable to firebug server panel
+ *
+ * @see FirePHP::DUMP
+ * @param string $Key
+ * @param mixed $Variable
+ * @return true
+ * @throws Exception
+ */
+ public function dump($Key, $Variable) {
+ return $this->fb($Variable, $Key, FirePHP::DUMP);
+ }
+
+ /**
+ * Log a trace in the firebug console
+ *
+ * @see FirePHP::TRACE
+ * @param string $Label
+ * @return true
+ * @throws Exception
+ */
+ public function trace($Label) {
+ return $this->fb($Label, FirePHP::TRACE);
+ }
+
+ /**
+ * Log a table in the firebug console
+ *
+ * @see FirePHP::TABLE
+ * @param string $Label
+ * @param string $Table
+ * @return true
+ * @throws Exception
+ */
+ public function table($Label, $Table) {
+ return $this->fb($Table, $Label, FirePHP::TABLE);
+ }
+
+ /**
+ * Check if FirePHP is installed on client
+ *
+ * @return boolean
+ */
+ public function detectClientExtension() {
+ /* Check if FirePHP is installed on client */
+ if(!@preg_match_all('/\sFirePHP\/([\.|\d]*)\s?/si',$this->getUserAgent(),$m) ||
+ !version_compare($m[1][0],'0.0.6','>=')) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Log varible to Firebug
+ *
+ * @see http://www.firephp.org/Wiki/Reference/Fb
+ * @param mixed $Object The variable to be logged
+ * @return true Return TRUE if message was added to headers, FALSE otherwise
+ * @throws Exception
+ */
+ public function fb($Object) {
+
+ if(!$this->enabled) {
+ return false;
+ }
+
+ if (headers_sent($filename, $linenum)) {
+ throw $this->newException('Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.');
+ }
+
+ $Type = null;
+ $Label = null;
+
+ if(func_num_args()==1) {
+ } else
+ if(func_num_args()==2) {
+ switch(func_get_arg(1)) {
+ case self::LOG:
+ case self::INFO:
+ case self::WARN:
+ case self::ERROR:
+ case self::DUMP:
+ case self::TRACE:
+ case self::EXCEPTION:
+ case self::TABLE:
+ case self::GROUP_START:
+ case self::GROUP_END:
+ $Type = func_get_arg(1);
+ break;
+ default:
+ $Label = func_get_arg(1);
+ break;
+ }
+ } else
+ if(func_num_args()==3) {
+ $Type = func_get_arg(2);
+ $Label = func_get_arg(1);
+ } else {
+ throw $this->newException('Wrong number of arguments to fb() function!');
+ }
+
+
+ if(!$this->detectClientExtension()) {
+ return false;
+ }
+
+ $meta = array();
+ $skipFinalObjectEncode = false;
+
+ if($Object instanceof Exception) {
+
+ $meta['file'] = $this->_escapeTraceFile($Object->getFile());
+ $meta['line'] = $Object->getLine();
+
+ $trace = $Object->getTrace();
+ if($Object instanceof ErrorException
+ && isset($trace[0]['function'])
+ && $trace[0]['function']=='errorHandler'
+ && isset($trace[0]['class'])
+ && $trace[0]['class']=='FirePHP') {
+
+ $severity = false;
+ switch($Object->getSeverity()) {
+ case E_WARNING: $severity = 'E_WARNING'; break;
+ case E_NOTICE: $severity = 'E_NOTICE'; break;
+ case E_USER_ERROR: $severity = 'E_USER_ERROR'; break;
+ case E_USER_WARNING: $severity = 'E_USER_WARNING'; break;
+ case E_USER_NOTICE: $severity = 'E_USER_NOTICE'; break;
+ case E_STRICT: $severity = 'E_STRICT'; break;
+ case E_RECOVERABLE_ERROR: $severity = 'E_RECOVERABLE_ERROR'; break;
+ case E_DEPRECATED: $severity = 'E_DEPRECATED'; break;
+ case E_USER_DEPRECATED: $severity = 'E_USER_DEPRECATED'; break;
+ }
+
+ $Object = array('Class'=>get_class($Object),
+ 'Message'=>$severity.': '.$Object->getMessage(),
+ 'File'=>$this->_escapeTraceFile($Object->getFile()),
+ 'Line'=>$Object->getLine(),
+ 'Type'=>'trigger',
+ 'Trace'=>$this->_escapeTrace(array_splice($trace,2)));
+ $skipFinalObjectEncode = true;
+ } else {
+ $Object = array('Class'=>get_class($Object),
+ 'Message'=>$Object->getMessage(),
+ 'File'=>$this->_escapeTraceFile($Object->getFile()),
+ 'Line'=>$Object->getLine(),
+ 'Type'=>'throw',
+ 'Trace'=>$this->_escapeTrace($trace));
+ $skipFinalObjectEncode = true;
+ }
+ $Type = self::EXCEPTION;
+
+ } else
+ if($Type==self::TRACE) {
+
+ $trace = debug_backtrace();
+ if(!$trace) return false;
+ for( $i=0 ; $i<sizeof($trace) ; $i++ ) {
+
+ if(isset($trace[$i]['class'])
+ && isset($trace[$i]['file'])
+ && ($trace[$i]['class']=='FirePHP'
+ || $trace[$i]['class']=='FB')
+ && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php'
+ || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) {
+ /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */
+ } else
+ if(isset($trace[$i]['class'])
+ && isset($trace[$i+1]['file'])
+ && $trace[$i]['class']=='FirePHP'
+ && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') {
+ /* Skip fb() */
+ } else
+ if($trace[$i]['function']=='fb'
+ || $trace[$i]['function']=='trace'
+ || $trace[$i]['function']=='send') {
+ $Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'',
+ 'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'',
+ 'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'',
+ 'Message'=>$trace[$i]['args'][0],
+ 'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'',
+ 'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'',
+ 'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'',
+ 'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1)));
+
+ $skipFinalObjectEncode = true;
+ $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'';
+ $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:'';
+ break;
+ }
+ }
+
+ } else
+ if($Type==self::TABLE) {
+
+ if(isset($Object[0]) && is_string($Object[0])) {
+ $Object[1] = $this->encodeTable($Object[1]);
+ } else {
+ $Object = $this->encodeTable($Object);
+ }
+
+ $skipFinalObjectEncode = true;
+
+ } else {
+ if($Type===null) {
+ $Type = self::LOG;
+ }
+ }
+
+ if($this->options['includeLineNumbers']) {
+ if(!isset($meta['file']) || !isset($meta['line'])) {
+
+ $trace = debug_backtrace();
+ for( $i=0 ; $trace && $i<sizeof($trace) ; $i++ ) {
+
+ if(isset($trace[$i]['class'])
+ && isset($trace[$i]['file'])
+ && ($trace[$i]['class']=='FirePHP'
+ || $trace[$i]['class']=='FB')
+ && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php'
+ || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) {
+ /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */
+ } else
+ if(isset($trace[$i]['class'])
+ && isset($trace[$i+1]['file'])
+ && $trace[$i]['class']=='FirePHP'
+ && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') {
+ /* Skip fb() */
+ } else
+ if(isset($trace[$i]['file'])
+ && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') {
+ /* Skip FB::fb() */
+ } else {
+ $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'';
+ $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:'';
+ break;
+ }
+ }
+
+ }
+ } else {
+ unset($meta['file']);
+ unset($meta['line']);
+ }
+
+ $this->setHeader('X-Wf-Protocol-1','http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
+ $this->setHeader('X-Wf-1-Plugin-1','http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.self::VERSION);
+
+ $structure_index = 1;
+ if($Type==self::DUMP) {
+ $structure_index = 2;
+ $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1');
+ } else {
+ $this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
+ }
+
+ if($Type==self::DUMP) {
+ $msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}';
+ } else {
+ $msg_meta = array('Type'=>$Type);
+ if($Label!==null) {
+ $msg_meta['Label'] = $Label;
+ }
+ if(isset($meta['file'])) {
+ $msg_meta['File'] = $meta['file'];
+ }
+ if(isset($meta['line'])) {
+ $msg_meta['Line'] = $meta['line'];
+ }
+ $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']';
+ }
+
+ $parts = explode("\n",chunk_split($msg, 5000, "\n"));
+
+ for( $i=0 ; $i<count($parts) ; $i++) {
+
+ $part = $parts[$i];
+ if ($part) {
+
+ if(count($parts)>2) {
+ // Message needs to be split into multiple parts
+ $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex,
+ (($i==0)?strlen($msg):'')
+ . '|' . $part . '|'
+ . (($i<count($parts)-2)?'\\':''));
+ } else {
+ $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex,
+ strlen($part) . '|' . $part . '|');
+ }
+
+ $this->messageIndex++;
+
+ if ($this->messageIndex > 99999) {
+ throw new Exception('Maximum number (99,999) of messages reached!');
+ }
+ }
+ }
+
+ $this->setHeader('X-Wf-1-Index',$this->messageIndex-1);
+
+ return true;
+ }
+
+ /**
+ * Standardizes path for windows systems.
+ *
+ * @param string $Path
+ * @return string
+ */
+ protected function _standardizePath($Path) {
+ return preg_replace('/\\\\+/','/',$Path);
+ }
+
+ /**
+ * Escape trace path for windows systems
+ *
+ * @param array $Trace
+ * @return array
+ */
+ protected function _escapeTrace($Trace) {
+ if(!$Trace) return $Trace;
+ for( $i=0 ; $i<sizeof($Trace) ; $i++ ) {
+ if(isset($Trace[$i]['file'])) {
+ $Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']);
+ }
+ if(isset($Trace[$i]['args'])) {
+ $Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']);
+ }
+ }
+ return $Trace;
+ }
+
+ /**
+ * Escape file information of trace for windows systems
+ *
+ * @param string $File
+ * @return string
+ */
+ protected function _escapeTraceFile($File) {
+ /* Check if we have a windows filepath */
+ if(strpos($File,'\\')) {
+ /* First strip down to single \ */
+
+ $file = preg_replace('/\\\\+/','\\',$File);
+
+ return $file;
+ }
+ return $File;
+ }
+
+ /**
+ * Send header
+ *
+ * @param string $Name
+ * @param string_type $Value
+ */
+ protected function setHeader($Name, $Value) {
+ return header($Name.': '.$Value);
+ }
+
+ /**
+ * Get user agent
+ *
+ * @return string|false
+ */
+ protected function getUserAgent() {
+ if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;
+ return $_SERVER['HTTP_USER_AGENT'];
+ }
+
+ /**
+ * Returns a new exception
+ *
+ * @param string $Message
+ * @return Exception
+ */
+ protected function newException($Message) {
+ return new Exception($Message);
+ }
+
+ /**
+ * Encode an object into a JSON string
+ *
+ * Uses PHP's jeson_encode() if available
+ *
+ * @param object $Object The object to be encoded
+ * @return string The JSON string
+ */
+ protected function jsonEncode($Object, $skipObjectEncode=false)
+ {
+ if(!$skipObjectEncode) {
+ $Object = $this->encodeObject($Object);
+ }
+
+ if(function_exists('json_encode')
+ && $this->options['useNativeJsonEncode']!=false) {
+
+ return json_encode($Object);
+ } else {
+ return $this->json_encode($Object);
+ }
+ }
+
+ /**
+ * Encodes a table by encoding each row and column with encodeObject()
+ *
+ * @param array $Table The table to be encoded
+ * @return array
+ */
+ protected function encodeTable($Table) {
+ if(!$Table) return $Table;
+ for( $i=0 ; $i<count($Table) ; $i++ ) {
+ if(is_array($Table[$i])) {
+ for( $j=0 ; $j<count($Table[$i]) ; $j++ ) {
+ $Table[$i][$j] = $this->encodeObject($Table[$i][$j]);
+ }
+ }
+ }
+ return $Table;
+ }
+
+ /**
+ * Encodes an object including members with
+ * protected and private visibility
+ *
+ * @param Object $Object The object to be encoded
+ * @param int $Depth The current traversal depth
+ * @return array All members of the object
+ */
+ protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1)
+ {
+ $return = array();
+
+ if (is_object($Object)) {
+
+ if ($ObjectDepth > $this->options['maxObjectDepth']) {
+ return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **';
+ }
+
+ foreach ($this->objectStack as $refVal) {
+ if ($refVal === $Object) {
+ return '** Recursion ('.get_class($Object).') **';
+ }
+ }
+ array_push($this->objectStack, $Object);
+
+ $return['__className'] = $class = get_class($Object);
+
+ $reflectionClass = new ReflectionClass($class);
+ $properties = array();
+ foreach( $reflectionClass->getProperties() as $property) {
+ $properties[$property->getName()] = $property;
+ }
+
+ $members = (array)$Object;
+
+ foreach( $properties as $raw_name => $property ) {
+
+ $name = $raw_name;
+ if($property->isStatic()) {
+ $name = 'static:'.$name;
+ }
+ if($property->isPublic()) {
+ $name = 'public:'.$name;
+ } else
+ if($property->isPrivate()) {
+ $name = 'private:'.$name;
+ $raw_name = "\0".$class."\0".$raw_name;
+ } else
+ if($property->isProtected()) {
+ $name = 'protected:'.$name;
+ $raw_name = "\0".'*'."\0".$raw_name;
+ }
+
+ if(!(isset($this->objectFilters[$class])
+ && is_array($this->objectFilters[$class])
+ && in_array($raw_name,$this->objectFilters[$class]))) {
+
+ if(array_key_exists($raw_name,$members)
+ && !$property->isStatic()) {
+
+ $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1);
+
+ } else {
+ if(method_exists($property,'setAccessible')) {
+ $property->setAccessible(true);
+ $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1);
+ } else
+ if($property->isPublic()) {
+ $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1);
+ } else {
+ $return[$name] = '** Need PHP 5.3 to get value **';
+ }
+ }
+ } else {
+ $return[$name] = '** Excluded by Filter **';
+ }
+ }
+
+ // Include all members that are not defined in the class
+ // but exist in the object
+ foreach( $members as $raw_name => $value ) {
+
+ $name = $raw_name;
+
+ if ($name{0} == "\0") {
+ $parts = explode("\0", $name);
+ $name = $parts[2];
+ }
+
+ if(!isset($properties[$name])) {
+ $name = 'undeclared:'.$name;
+
+ if(!(isset($this->objectFilters[$class])
+ && is_array($this->objectFilters[$class])