4 chdir(dirname(__FILE__
));
5 require_once 'common.php';
9 * Compiles all of HTML Purifier's library files into one big file
10 * named HTMLPurifier.standalone.php.
14 * Global hash that tracks already loaded includes
16 $GLOBALS['loaded'] = array('HTMLPurifier.php' => true);
19 * Custom FSTools for this script that overloads some behavior
20 * @warning The overloading of copy() is not necessarily global for
21 * this script. Watch out!
23 class MergeLibraryFSTools
extends FSTools
25 function copyable($entry) {
27 if ($entry[0] == '.') {
32 function copy($source, $dest) {
33 copy_and_remove_includes($source, $dest);
36 $FS = new MergeLibraryFSTools();
39 * Replaces the includes inside PHP source code with the corresponding
41 * @param string $text PHP source code to replace includes from
43 function replace_includes($text) {
44 return preg_replace_callback(
45 "/require_once ['\"]([^'\"]+)['\"];/",
46 'replace_includes_callback',
52 * Removes leading PHP tags from included files. Assumes that there is
54 * @note This is safe for files that have internal <?php
55 * @param string $text Text to have leading PHP tag from
57 function remove_php_tags($text) {
58 return substr($text, 5);
62 * Creates an appropriate blank file, recursively generating directories
64 * @param string $file Filename to create blank for
66 function create_blank($file) {
68 $dir = dirname($file);
69 $base = realpath('../tests/blanks/') . DIRECTORY_SEPARATOR
;
71 $FS->mkdir($base . $dir);
73 file_put_contents($base . $file, '');
77 * Copies the contents of a directory to the standalone directory
78 * @param string $dir Directory to copy
80 function make_dir_standalone($dir) {
82 return $FS->copyr($dir, 'standalone/' . $dir);
86 * Copies the contents of a file to the standalone directory
87 * @param string $file File to copy
89 function make_file_standalone($file) {
91 $FS->mkdir('standalone/' . dirname($file));
92 copy_and_remove_includes($file, 'standalone/' . $file);
97 * Copies a file to another location recursively, if it is a PHP file
99 * @param string $file Original file
100 * @param string $sfile New location of file
102 function copy_and_remove_includes($file, $sfile) {
103 $contents = file_get_contents($file);
104 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
105 return file_put_contents($sfile, $contents);
109 * @param $matches preg_replace_callback matches array, where index 1
110 * is the filename to include
112 function replace_includes_callback($matches) {
116 'HTMLPurifier/Lexer/DOMLex.php' => 1,
117 'HTMLPurifier/Printer.php' => 1,
119 'XML/HTMLSax3.php' => 1
121 if (isset($preserve[$file])) {
124 if (isset($GLOBALS['loaded'][$file])) return '';
125 $GLOBALS['loaded'][$file] = true;
127 return replace_includes(remove_php_tags(file_get_contents($file)));
130 chdir(dirname(__FILE__
) . '/../library/');
131 create_blank('HTMLPurifier.php');
133 echo 'Creating full file...';
134 $contents = replace_includes(file_get_contents('HTMLPurifier.php'));
135 $contents = str_replace(
136 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__));",
137 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
138 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
141 file_put_contents('HTMLPurifier.standalone.php', $contents);
142 echo ' done!' . PHP_EOL
;
144 echo 'Creating standalone directory...';
145 $FS->rmdirr('standalone'); // ensure a clean copy
148 $FS->mkdir('standalone/HTMLPurifier/DefinitionCache/Serializer');
149 make_dir_standalone('HTMLPurifier/EntityLookup');
151 // non-standard inclusion setup
152 make_dir_standalone('HTMLPurifier/Language');
154 // optional components
155 make_file_standalone('HTMLPurifier/Printer.php');
156 make_dir_standalone('HTMLPurifier/Printer');
157 make_dir_standalone('HTMLPurifier/Filter');
158 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
161 make_file_standalone('HTMLPurifier/Lexer/DOMLex.php');
162 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
163 echo ' done!' . PHP_EOL
;