ce00763166b4003ad5726d05079b7f1c7bdb8f40
[yaffs-website] / vendor / ezyang / htmlpurifier / extras / FSTools.php
1 <?php
2
3 /**
4  * Filesystem tools not provided by default; can recursively create, copy
5  * and delete folders. Some template methods are provided for extensibility.
6  *
7  * @note This class must be instantiated to be used, although it does
8  *       not maintain state.
9  */
10 class FSTools
11 {
12
13     private static $singleton;
14
15     /**
16      * Returns a global instance of FSTools
17      */
18     public static function singleton()
19     {
20         if (empty(FSTools::$singleton)) FSTools::$singleton = new FSTools();
21         return FSTools::$singleton;
22     }
23
24     /**
25      * Sets our global singleton to something else; useful for overloading
26      * functions.
27      */
28     public static function setSingleton($singleton)
29     {
30         FSTools::$singleton = $singleton;
31     }
32
33     /**
34      * Recursively creates a directory
35      * @param string $folder Name of folder to create
36      * @note Adapted from the PHP manual comment 76612
37      */
38     public function mkdirr($folder)
39     {
40         $folders = preg_split("#[\\\\/]#", $folder);
41         $base = '';
42         for($i = 0, $c = count($folders); $i < $c; $i++) {
43             if(empty($folders[$i])) {
44                 if (!$i) {
45                     // special case for root level
46                     $base .= DIRECTORY_SEPARATOR;
47                 }
48                 continue;
49             }
50             $base .= $folders[$i];
51             if(!is_dir($base)){
52                 $this->mkdir($base);
53             }
54             $base .= DIRECTORY_SEPARATOR;
55         }
56     }
57
58     /**
59      * Copy a file, or recursively copy a folder and its contents; modified
60      * so that copied files, if PHP, have includes removed
61      * @note Adapted from http://aidanlister.com/repos/v/function.copyr.php
62      */
63     public function copyr($source, $dest)
64     {
65         // Simple copy for a file
66         if (is_file($source)) {
67             return $this->copy($source, $dest);
68         }
69         // Make destination directory
70         if (!is_dir($dest)) {
71             $this->mkdir($dest);
72         }
73         // Loop through the folder
74         $dir = $this->dir($source);
75         while ( false !== ($entry = $dir->read()) ) {
76             // Skip pointers
77             if ($entry == '.' || $entry == '..') {
78                 continue;
79             }
80             if (!$this->copyable($entry)) {
81                 continue;
82             }
83             // Deep copy directories
84             if ($dest !== "$source/$entry") {
85                 $this->copyr("$source/$entry", "$dest/$entry");
86             }
87         }
88         // Clean up
89         $dir->close();
90         return true;
91     }
92
93     /**
94      * Overloadable function that tests a filename for copyability. By
95      * default, everything should be copied; you can restrict things to
96      * ignore hidden files, unreadable files, etc. This function
97      * applies to copyr().
98      */
99     public function copyable($file)
100     {
101         return true;
102     }
103
104     /**
105      * Delete a file, or a folder and its contents
106      * @note Adapted from http://aidanlister.com/repos/v/function.rmdirr.php
107      */
108     public function rmdirr($dirname)
109     {
110         // Sanity check
111         if (!$this->file_exists($dirname)) {
112             return false;
113         }
114
115         // Simple delete for a file
116         if ($this->is_file($dirname) || $this->is_link($dirname)) {
117             return $this->unlink($dirname);
118         }
119
120         // Loop through the folder
121         $dir = $this->dir($dirname);
122         while (false !== $entry = $dir->read()) {
123             // Skip pointers
124             if ($entry == '.' || $entry == '..') {
125                 continue;
126             }
127             // Recurse
128             $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
129         }
130
131         // Clean up
132         $dir->close();
133         return $this->rmdir($dirname);
134     }
135
136     /**
137      * Recursively globs a directory.
138      */
139     public function globr($dir, $pattern, $flags = NULL)
140     {
141         $files = $this->glob("$dir/$pattern", $flags);
142         if ($files === false) $files = array();
143         $sub_dirs = $this->glob("$dir/*", GLOB_ONLYDIR);
144         if ($sub_dirs === false) $sub_dirs = array();
145         foreach ($sub_dirs as $sub_dir) {
146             $sub_files = $this->globr($sub_dir, $pattern, $flags);
147             $files = array_merge($files, $sub_files);
148         }
149         return $files;
150     }
151
152     /**
153      * Allows for PHP functions to be called and be stubbed.
154      * @warning This function will not work for functions that need
155      *      to pass references; manually define a stub function for those.
156      */
157     public function __call($name, $args)
158     {
159         return call_user_func_array($name, $args);
160     }
161
162 }
163
164 // vim: et sw=4 sts=4