4 chdir(dirname(__FILE__
));
5 require_once 'common.php';
10 * Compiles all of HTML Purifier's library files into one big file
11 * named HTMLPurifier.standalone.php. This is usually called during the
16 * Global hash that tracks already loaded includes
18 $GLOBALS['loaded'] = array();
21 * Custom FSTools for this script that overloads some behavior
22 * @warning The overloading of copy() is not necessarily global for
23 * this script. Watch out!
25 class MergeLibraryFSTools
extends FSTools
27 function copyable($entry) {
29 if ($entry[0] == '.') {
34 function copy($source, $dest) {
35 copy_and_remove_includes($source, $dest);
38 $FS = new MergeLibraryFSTools();
41 * Replaces the includes inside PHP source code with the corresponding
43 * @param string $text PHP source code to replace includes from
45 function replace_includes($text) {
46 return preg_replace_callback(
47 "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
48 'replace_includes_callback',
54 * Removes leading PHP tags from included files. Assumes that there is
56 * @note This is safe for files that have internal <?php
57 * @param string $text Text to have leading PHP tag from
59 function remove_php_tags($text) {
60 return substr($text, 5);
64 * Copies the contents of a directory to the standalone directory
65 * @param string $dir Directory to copy
67 function make_dir_standalone($dir) {
69 return $FS->copyr($dir, 'standalone/' . $dir);
73 * Copies the contents of a file to the standalone directory
74 * @param string $file File to copy
76 function make_file_standalone($file) {
78 $FS->mkdirr('standalone/' . dirname($file));
79 copy_and_remove_includes($file, 'standalone/' . $file);
84 * Copies a file to another location recursively, if it is a PHP file
86 * @param string $file Original file
87 * @param string $sfile New location of file
89 function copy_and_remove_includes($file, $sfile) {
90 $contents = file_get_contents($file);
91 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
92 return file_put_contents($sfile, $contents);
96 * @param $matches preg_replace_callback matches array, where index 1
97 * is the filename to include
99 function replace_includes_callback($matches) {
103 'XML/HTMLSax3.php' => 1
105 if (isset($preserve[$file])) {
108 if (isset($GLOBALS['loaded'][$file])) return '';
109 $GLOBALS['loaded'][$file] = true;
110 return replace_includes(remove_php_tags(file_get_contents($file)));
113 echo 'Generating includes file... ';
114 shell_exec('php generate-includes.php');
117 chdir(dirname(__FILE__
) . '/../library/');
119 echo 'Creating full file...';
120 $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
121 $contents = str_replace(
122 // Note that bootstrap is now inside the standalone file
123 "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
124 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
125 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
128 file_put_contents('HTMLPurifier.standalone.php', $contents);
129 echo ' done!' . PHP_EOL
;
131 echo 'Creating standalone directory...';
132 $FS->rmdirr('standalone'); // ensure a clean copy
135 $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
136 make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
137 make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
139 // non-standard inclusion setup
140 make_dir_standalone('HTMLPurifier/ConfigSchema');
141 make_dir_standalone('HTMLPurifier/Language');
142 make_dir_standalone('HTMLPurifier/Filter');
143 make_dir_standalone('HTMLPurifier/Printer');
144 make_file_standalone('HTMLPurifier/Printer.php');
145 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
146 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
148 echo ' done!' . PHP_EOL
;