[3.0.0] Upgraded test scripts and other goodies. Also removed some PHP4 cruft.
[htmlpurifier/darkodev.git] / maintenance / common.php
blobaf59baf974d5ae302217e212a1befe035f55b8cd
1 <?php
3 function assertCli() {
4 if (php_sapi_name() != 'cli' && !getenv('PHP_IS_CLI')) {
5 echo 'Script cannot be called from web-browser (if you are calling via cli,
6 set environment variable PHP_IS_CLI to work around this).';
7 exit;
11 /**
12 * Filesystem tools not provided by default; can recursively create, copy
13 * and delete folders. Some template methods are provided for extensibility.
14 * @note This class must be instantiated to be used, although it does
15 * not maintain state.
17 class FSTools
20 /**
21 * Recursively creates a directory
22 * @param string $folder Name of folder to create
23 * @note Adapted from the PHP manual comment 76612
25 function mkdir($folder) {
26 $folders = preg_split("#[\\\\/]#", $folder);
27 $base = '';
28 for($i = 0, $c = count($folders); $i < $c; $i++) {
29 if(empty($folders[$i])) {
30 if (!$i) {
31 // special case for root level
32 $base .= DIRECTORY_SEPARATOR;
34 continue;
36 $base .= $folders[$i];
37 if(!is_dir($base)){
38 mkdir($base);
40 $base .= DIRECTORY_SEPARATOR;
44 /**
45 * Copy a file, or recursively copy a folder and its contents; modified
46 * so that copied files, if PHP, have includes removed
48 * @author Aidan Lister <aidan@php.net>
49 * @version 1.0.1-modified
50 * @link http://aidanlister.com/repos/v/function.copyr.php
51 * @param string $source Source path
52 * @param string $dest Destination path
53 * @return bool Returns TRUE on success, FALSE on failure
55 function copyr($source, $dest) {
56 // Simple copy for a file
57 if (is_file($source)) {
58 return $this->copy($source, $dest);
60 // Make destination directory
61 if (!is_dir($dest)) {
62 mkdir($dest);
64 // Loop through the folder
65 $dir = dir($source);
66 while (false !== $entry = $dir->read()) {
67 // Skip pointers
68 if ($entry == '.' || $entry == '..') {
69 continue;
71 if (!$this->copyable($entry)) {
72 continue;
74 // Deep copy directories
75 if ($dest !== "$source/$entry") {
76 $this->copyr("$source/$entry", "$dest/$entry");
79 // Clean up
80 $dir->close();
81 return true;
84 /**
85 * Stub for PHP's built-in copy function, can be used to overload
86 * functionality
88 function copy($source, $dest) {
89 return copy($source, $dest);
92 /**
93 * Overloadable function that tests a filename for copyability. By
94 * default, everything should be copied; you can restrict things to
95 * ignore hidden files, unreadable files, etc.
97 function copyable($file) {
98 return true;
102 * Delete a file, or a folder and its contents
104 * @author Aidan Lister <aidan@php.net>
105 * @version 1.0.3
106 * @link http://aidanlister.com/repos/v/function.rmdirr.php
107 * @param string $dirname Directory to delete
108 * @return bool Returns TRUE on success, FALSE on failure
110 function rmdirr($dirname)
112 // Sanity check
113 if (!file_exists($dirname)) {
114 return false;
117 // Simple delete for a file
118 if (is_file($dirname) || is_link($dirname)) {
119 return unlink($dirname);
122 // Loop through the folder
123 $dir = dir($dirname);
124 while (false !== $entry = $dir->read()) {
125 // Skip pointers
126 if ($entry == '.' || $entry == '..') {
127 continue;
129 // Recurse
130 $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
133 // Clean up
134 $dir->close();
135 return rmdir($dirname);