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 public function copyable($entry)
30 if ($entry[0] == '.') {
35 public function copy($source, $dest)
37 copy_and_remove_includes($source, $dest);
40 $FS = new MergeLibraryFSTools();
43 * Replaces the includes inside PHP source code with the corresponding
45 * @param string $text PHP source code to replace includes from
47 function replace_includes($text)
49 // also remove vim modelines
50 return preg_replace_callback(
51 "/require(?:_once)? ['\"]([^'\"]+)['\"];/",
52 'replace_includes_callback',
58 * Removes leading PHP tags from included files. Assumes that there is
59 * no trailing tag. Also removes vim modelines.
60 * @note This is safe for files that have internal <?php
61 * @param string $text Text to have leading PHP tag from
63 function remove_php_tags($text)
65 $text = preg_replace('#// vim:.+#', '', $text);
66 return substr($text, 5);
70 * Copies the contents of a directory to the standalone directory
71 * @param string $dir Directory to copy
73 function make_dir_standalone($dir)
76 return $FS->copyr($dir, 'standalone/' . $dir);
80 * Copies the contents of a file to the standalone directory
81 * @param string $file File to copy
83 function make_file_standalone($file)
86 $FS->mkdirr('standalone/' . dirname($file));
87 copy_and_remove_includes($file, 'standalone/' . $file);
92 * Copies a file to another location recursively, if it is a PHP file
94 * @param string $file Original file
95 * @param string $sfile New location of file
97 function copy_and_remove_includes($file, $sfile)
99 $contents = file_get_contents($file);
100 if (strrchr($file, '.') === '.php') $contents = replace_includes($contents);
101 return file_put_contents($sfile, $contents);
105 * @param $matches preg_replace_callback matches array, where index 1
106 * is the filename to include
108 function replace_includes_callback($matches)
113 'XML/HTMLSax3.php' => 1
115 if (isset($preserve[$file])) {
118 if (isset($GLOBALS['loaded'][$file])) return '';
119 $GLOBALS['loaded'][$file] = true;
120 return replace_includes(remove_php_tags(file_get_contents($file)));
123 echo 'Generating includes file... ';
124 shell_exec('php generate-includes.php');
127 chdir(dirname(__FILE__
) . '/../library/');
129 echo 'Creating full file...';
130 $contents = replace_includes(file_get_contents('HTMLPurifier.includes.php'));
131 $contents = str_replace(
132 // Note that bootstrap is now inside the standalone file
133 "define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));",
134 "define('HTMLPURIFIER_PREFIX', dirname(__FILE__) . '/standalone');
135 set_include_path(HTMLPURIFIER_PREFIX . PATH_SEPARATOR . get_include_path());",
138 file_put_contents('HTMLPurifier.standalone.php', $contents);
139 echo ' done!' . PHP_EOL
;
141 echo 'Creating standalone directory...';
142 $FS->rmdirr('standalone'); // ensure a clean copy
145 $FS->mkdirr('standalone/HTMLPurifier/DefinitionCache/Serializer');
146 make_file_standalone('HTMLPurifier/EntityLookup/entities.ser');
147 make_file_standalone('HTMLPurifier/ConfigSchema/schema.ser');
149 // non-standard inclusion setup
150 make_dir_standalone('HTMLPurifier/ConfigSchema');
151 make_dir_standalone('HTMLPurifier/Language');
152 make_dir_standalone('HTMLPurifier/Filter');
153 make_dir_standalone('HTMLPurifier/Printer');
154 make_file_standalone('HTMLPurifier/Printer.php');
155 make_file_standalone('HTMLPurifier/Lexer/PH5P.php');
157 echo ' done!' . PHP_EOL
;
159 // vim: et sw=4 sts=4