generate-standalone.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/php
  2. <?php
  3. chdir(dirname(__FILE__));
  4. require_once 'common.php';
  5. assertCli();
  6. /**
  7. * @file
  8. * Compiles all of HTML Purifier's library files into one big file
  9. * named HTMLPurifier.standalone.php. This is usually called during the
  10. * release process.
  11. */
  12. /**
  13. * Global hash that tracks already loaded includes
  14. */
  15. $GLOBALS['loaded'] = array();
  16. /**
  17. * Custom FSTools for this script that overloads some behavior
  18. * @warning The overloading of copy() is not necessarily global for
  19. * this script. Watch out!
  20. */
  21. class MergeLibraryFSTools extends FSTools
  22. {
  23. function copyable($entry) {
  24. // Skip hidden files
  25. if ($entry[0] == '.') {
  26. return false;
  27. }
  28. return true;
  29. }
  30. function copy($source, $dest) {
  31. copy_and_remove_includes($source, $dest);
  32. }
  33. }
  34. $FS = new MergeLibraryFSTools();
  35. /**
  36. * Replaces the includes inside PHP source code with the corresponding
  37. * source.
  38. * @param string $text PHP source code to replace includes from
  39. */
  40. function replace_includes($text) {
  41. // also remove vim modelines
  42. return preg_replace_callback(
  43. "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
  44. 'replace_includes_callback',
  45. $text
  46. );
  47. }
  48. /**
  49. * Removes leading PHP tags from included files. Assumes that there is
  50. * no trailing tag. Also removes vim modelines.
  51. * @note This is safe for files that have internal <?php
  52. * @param string $text Text to have leading PHP tag from
  53. */
  54. function remove_php_tags($text) {
  55. $text = preg_replace('#// vim:.+#', '', $text);
  56. return substr($text, 5);
  57. }
  58. /**
  59. * Copies the contents of a directory to the standalone directory
  60. * @param string $dir Directory to copy
  61. */
  62. function make_dir_standalone($dir) {
  63. global $FS;
  64. return $FS->copyr($dir, 'standalone/' . $dir);
  65. }
  66. /**
  67. * Copies the contents of a file to the standalone directory
  68. * @param string $file File to copy
  69. */
  70. function make_file_standalone($file) {
  71. global $FS;
  72. $FS->mkdirr('standalone/' . dirname($file));
  73. copy_and_remove_includes($file, 'standalone/' . $file);
  74. return true;
  75. }
  76. /**
  77. * Copies a file to another location recursively, if it is a PHP file
  78. * remove includes
  79. * @param string $file Original file
  80. * @param string $sfile New location of file
  81. */
  82. function copy_and_remove_includes($file, $sfile) {
  83. $contents = file_get_contents($file);
  84. if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
  85. return file_put_contents($sfile, $contents);
  86. }
  87. /**
  88. * @param $matches preg_replace_callback matches array, where index 1
  89. * is the filename to include
  90. */
  91. function replace_includes_callback($matches) {
  92. $file = $matches[1];
  93. $preserve = array(
  94. // PEAR (external)
  95. 'XML/HTMLSax3.php' => 1
  96. );
  97. if (isset($preserve[$file])) {
  98. return $matches[0];
  99. }
  100. if (isset($GLOBALS['loaded'][$file])) return '';
  101. $GLOBALS['loaded'][$file] = true;
  102. return replace_includes(remove_php_tags(file_get_contents($file)));
  103. }
  104. echo 'Generating includes file... ';
  105. shell_exec('php generate-includes.php');
  106. echo "done!\n";
  107. chdir(dirname(__FILE__) . '/../library/');
  108. echo 'Creating full file...';
  109. $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
  110. $contents = str_replace(
  111. // Note that bootstrap is now inside the standalone file
  112. "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
  113. "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
  114. set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
  115. $contents
  116. );
  117. file_put_contents('HTMLPurifier.standalone.php', $contents);
  118. echo ' done!' . PHP_EOL;
  119. echo 'Creating standalone directory...';
  120. $FS->rmdirr('standalone'); // ensure a clean copy
  121. // data files
  122. $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
  123. make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
  124. make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
  125. // non-standard inclusion setup
  126. make_dir_standalone('HTMLPurifier/ConfigSchema');
  127. make_dir_standalone('HTMLPurifier/Language');
  128. make_dir_standalone('HTMLPurifier/Filter');
  129. make_dir_standalone('HTMLPurifier/Printer');
  130. make_file_standalone('HTMLPurifier/Printer.php');
  131. make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
  132. echo ' done!' . PHP_EOL;
  133. // vim: et sw=4 sts=4