4 require_once 'common.php';
8 * Compiles all of HTML Purifier's library files into one big file
9 * named HTMLPurifier.standalone.php.
13 * Global hash that tracks already loaded includes
15 $GLOBALS['loaded'] = array('HTMLPurifier.php' => true);
18 * Custom FSTools for this script that overloads some behavior
19 * @warning The overloading of copy() is not necessarily global for
20 * this script. Watch out!
22 class MergeLibraryFSTools
extends FSTools
24 function copyable($entry) {
26 if ($entry[0] == '.') {
31 function copy($source, $dest) {
32 copy_and_remove_includes($source, $dest);
35 $FS = new MergeLibraryFSTools();
38 * Replaces the includes inside PHP source code with the corresponding
40 * @param string $text PHP source code to replace includes from
42 function replace_includes($text) {
43 return preg_replace_callback(
44 "/require_once ['\"]([^'\"]+)['\"];/",
45 'replace_includes_callback',
51 * Removes leading PHP tags from included files. Assumes that there is
53 * @note This is safe for files that have internal <?php
54 * @param string $text Text to have leading PHP tag from
56 function remove_php_tags($text) {
57 return substr($text, 5);
61 * Creates an appropriate blank file, recursively generating directories
63 * @param string $file Filename to create blank for
65 function create_blank($file) {
67 $dir = dirname($file);
68 $base = realpath('../tests/blanks/') . DIRECTORY_SEPARATOR
;
70 $FS->mkdir($base . $dir);
72 file_put_contents($base . $file, '');
76 * Copies the contents of a directory to the standalone directory
77 * @param string $dir Directory to copy
79 function make_dir_standalone($dir) {
81 return $FS->copyr($dir, 'standalone/' . $dir);
85 * Copies the contents of a file to the standalone directory
86 * @param string $file File to copy
88 function make_file_standalone($file) {
90 $FS->mkdir('standalone/' . dirname($file));
91 copy_and_remove_includes($file, 'standalone/' . $file);
96 * Copies a file to another location recursively, if it is a PHP file
98 * @param string $file Original file
99 * @param string $sfile New location of file
101 function copy_and_remove_includes($file, $sfile) {
102 $contents = file_get_contents($file);
103 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
104 return file_put_contents($sfile, $contents);
108 * @param $matches preg_replace_callback matches array, where index 1
109 * is the filename to include
111 function replace_includes_callback($matches) {
115 'HTMLPurifier/Lexer/DOMLex.php' => 1,
116 'HTMLPurifier/Printer.php' => 1,
118 'XML/HTMLSax3.php' => 1
120 if (isset($preserve[$file])) {
123 if (isset($GLOBALS['loaded'][$file])) return '';
124 $GLOBALS['loaded'][$file] = true;
126 return replace_includes(remove_php_tags(file_get_contents($file)));
129 chdir(dirname(__FILE__
) . '/../library/');
130 create_blank('HTMLPurifier.php');
132 echo 'Creating full file...';
133 $contents = replace_includes(file_get_contents('HTMLPurifier.php'));
134 $contents = str_replace(
135 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__));",
136 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
137 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
140 file_put_contents('HTMLPurifier.standalone.php', $contents);
141 echo ' done!' . PHP_EOL
;
143 echo 'Creating standalone directory...';
144 $FS->rmdirr('standalone'); // ensure a clean copy
147 $FS->mkdir('standalone/HTMLPurifier/DefinitionCache/Serializer');
148 make_dir_standalone('HTMLPurifier/EntityLookup');
150 // non-standard inclusion setup
151 make_dir_standalone('HTMLPurifier/Language');
153 // optional components
154 make_file_standalone('HTMLPurifier/Printer.php');
155 make_dir_standalone('HTMLPurifier/Printer');
156 make_dir_standalone('HTMLPurifier/Filter');
157 make_file_standalone('HTMLPurifier/Lexer/PEARSax3.php');
160 make_file_standalone('HTMLPurifier/Lexer/DOMLex.php');
161 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
162 echo ' done!' . PHP_EOL
;