PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/maintenance/generate-standalone.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 157 lines | 87 code | 21 blank | 49 comment | 5 complexity | b7a259b56abb26363b500c8abe1ff35c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. {
  25. // Skip hidden files
  26. if ($entry[0] == '.')
  27. {
  28. return false;
  29. }
  30. return true;
  31. }
  32. function copy($source, $dest)
  33. {
  34. copy_and_remove_includes($source, $dest);
  35. }
  36. }
  37. $FS = new MergeLibraryFSTools();
  38. /**
  39. * Replaces the includes inside PHP source code with the corresponding
  40. * source.
  41. * @param string $text PHP source code to replace includes from
  42. */
  43. function replace_includes($text)
  44. {
  45. // also remove vim modelines
  46. return preg_replace_callback("/require(?:_once)? ['\"]([^'\"]+)['\"];/", 'replace_includes_callback', $text);
  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. {
  56. $text = preg_replace('#// vim:.+#', '', $text);
  57. return substr($text, 5);
  58. }
  59. /**
  60. * Copies the contents of a directory to the standalone directory
  61. * @param string $dir Directory to copy
  62. */
  63. function make_dir_standalone($dir)
  64. {
  65. global $FS;
  66. return $FS->copyr($dir, 'standalone/' . $dir);
  67. }
  68. /**
  69. * Copies the contents of a file to the standalone directory
  70. * @param string $file File to copy
  71. */
  72. function make_file_standalone($file)
  73. {
  74. global $FS;
  75. $FS->mkdirr('standalone/' . dirname($file));
  76. copy_and_remove_includes($file, 'standalone/' . $file);
  77. return true;
  78. }
  79. /**
  80. * Copies a file to another location recursively, if it is a PHP file
  81. * remove includes
  82. * @param string $file Original file
  83. * @param string $sfile New location of file
  84. */
  85. function copy_and_remove_includes($file, $sfile)
  86. {
  87. $contents = file_get_contents($file);
  88. if (strrchr($file, '.') === '.php')
  89. $contents = replace_includes($contents);
  90. return file_put_contents($sfile, $contents);
  91. }
  92. /**
  93. * @param $matches preg_replace_callback matches array, where index 1
  94. * is the filename to include
  95. */
  96. function replace_includes_callback($matches)
  97. {
  98. $file = $matches[1];
  99. $preserve = array(// PEAR (external)
  100. 'XML/HTMLSax3.php' => 1);
  101. if (isset($preserve[$file]))
  102. {
  103. return $matches[0];
  104. }
  105. if (isset($GLOBALS['loaded'][$file]))
  106. return '';
  107. $GLOBALS['loaded'][$file] = true;
  108. return replace_includes(remove_php_tags(file_get_contents($file)));
  109. }
  110. echo 'Generating includes file... ';
  111. shell_exec('php generate-includes.php');
  112. echo "done!\n";
  113. chdir(dirname(__FILE__) . '/../library/');
  114. echo 'Creating full file...';
  115. $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
  116. $contents = str_replace(// Note that bootstrap is now inside the standalone file
  117. "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));", "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
  118. set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());", $contents);
  119. file_put_contents('HTMLPurifier.standalone.php', $contents);
  120. echo ' done!' . PHP_EOL;
  121. echo 'Creating standalone directory...';
  122. $FS->rmdirr('standalone'); // ensure a clean copy
  123. // data files
  124. $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
  125. make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
  126. make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
  127. // non-standard inclusion setup
  128. make_dir_standalone('HTMLPurifier/ConfigSchema');
  129. make_dir_standalone('HTMLPurifier/Language');
  130. make_dir_standalone('HTMLPurifier/Filter');
  131. make_dir_standalone('HTMLPurifier/Printer');
  132. make_file_standalone('HTMLPurifier/Printer.php');
  133. make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
  134. make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
  135. echo ' done!' . PHP_EOL;
  136. // vim: et sw=4 sts=4