tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
commit | author | age
e2d6ed 1 <?php
436ed8 2
e2d6ed 3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
e2d6ed 5 All rights reserved.
T 6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class file{
436ed8 32     
R 33     function rf($file){
34       global $app;
35       clearstatcache();
458767 36       if(!$fp = fopen ($file, 'rb')){
J 37         $app->log('WARNING: Could not open file '.$file, 2);
436ed8 38         return false;
e2d6ed 39       } else {
436ed8 40         if(filesize($file) > 0){
R 41           $content = fread($fp, filesize($file));
42         } else {
458767 43           $content = '';
436ed8 44         }
R 45         fclose($fp);
46         return $content;
e2d6ed 47       }
T 48     }
436ed8 49     
R 50     function wf($file, $content){
51       global $app;
52       $this->mkdirs(dirname($file));
458767 53       if(!$fp = fopen ($file, 'wb')){
J 54         $app->log('WARNING: Could not open file '.$file, 2);
436ed8 55         return false;
R 56       } else {
57         fwrite($fp,$content);
58         fclose($fp);
59         return true;
60       }
e2d6ed 61     }
436ed8 62     
R 63     function af($file, $content){
64       global $app;
65       $this->mkdirs(dirname($file));
458767 66       if(!$fp = fopen ($file, 'ab')){
J 67         $app->log('WARNING: Could not open file '.$file, 2);
436ed8 68         return false;
R 69       } else {
70         fwrite($fp,$content);
71         fclose($fp);
72         return true;
73       }
74     }
75     
76     function no_comments($file, $comment = '#'){
77       $content = $this->unix_nl($this->rf($file));
78       $lines = explode("\n", $content);
79       if(!empty($lines)){
80         foreach($lines as $line){
81           if(strstr($line, $comment)){
82             $pos = strpos($line, $comment);
83             if($pos != 0){
84               $new_lines[] = substr($line,0,$pos);
85             } else {
458767 86               $new_lines[] = '';
e2d6ed 87             }
T 88           } else {
436ed8 89             $new_lines[] = $line;
R 90           }
91         }
92       }
93       if(is_array($new_lines)){
94         $content_without_comments = implode("\n", $new_lines);
95         $new_lines = NULL;
96         return $content_without_comments;
97       } else {
458767 98         return '';
436ed8 99       }
R 100     }
101     
102     function manual_entries($file, $separator = '#### MAKE MANUAL ENTRIES BELOW THIS LINE! ####'){
103       if(is_file($file)){
104         $content = $this->rf($file);
105         $parts = explode($separator, $content);
106         $manual = "\n".trim($parts[1]);
107         return $manual;
108       } else {
458767 109         return '';
436ed8 110       }
R 111     }
112     
113     function remove_blank_lines($input, $file = 1){
a9d0b2 114       //Leerzeilen löschen
436ed8 115       if($file){
R 116         $content = $this->unix_nl($this->rf($input));
117       } else {
118         $content = $input;
119       }
120       $lines = explode("\n", $content);
121       if(!empty($lines)){
122         foreach($lines as $line){
458767 123           if(trim($line) != '') $new_lines[] = $line;
436ed8 124         }
R 125       }
126       if(is_array($new_lines)){
127         $content = implode("\n", $new_lines);
128       } else {
458767 129         $content = '';
436ed8 130       }
R 131       if($file){
132         $this->wf($input, $content);
133       } else {
134         return $content;
135       }
136     }
137     
138     function unix_nl($input){
139       $output = str_replace("\r\n", "\n", $input);
140       $output = str_replace("\r", "\n", $output);
141       return $output;
142     }
143     
144     function fileowner($file){
145       $owner_id = fileowner($file);
146       clearstatcache();
147       return $owner_id;
148     }
149     
150     function mkdirs($strPath, $mode = '0755'){
151       // Verzeichnisse rekursiv erzeugen
152       if(is_dir($strPath)) return true;
153       $pStrPath = dirname($strPath);
154       if(!$this->mkdirs($pStrPath, $mode)) return false;
155       $old_umask = umask(0);
156       $ret_val = mkdir($strPath, octdec($mode));
157       umask($old_umask);
158       return $ret_val;
159     }
160     
161     function edit_dist($var, $val){
162       global $$var;
458767 163       $files = array('/root/ispconfig/dist.inc.php');
436ed8 164       foreach($files as $file){
R 165         if(is_file($file)){
166           $file_content = $this->unix_nl($this->rf($file));
167           $lines = explode("\n", $file_content);
168           for($i=0;$i<sizeof($lines);$i++){
458767 169             $parts = explode('=', $lines[$i]);
436ed8 170             if($parts[0] == $var || $parts[0] == '$'.$var.' '){
R 171               $parts[1] = str_replace($$var, $val, $parts[1]);
172             }
458767 173             $lines[$i] = implode('=', $parts);
436ed8 174           }
R 175           $file_content = implode("\n", $lines);
176           $this->wf($file, $file_content);
177         }
178       }
179     }
180     
458767 181     function getDirectoryListing($dirname, $sortorder = 'a', $show_subdirs = 0, $show_subdirfiles = 0, $exts = '', $ext_save = 1){
436ed8 182     // This function will return an array with filenames based on the criteria you can set in the variables
R 183     // @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
184     // @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
185     // Logically subdirnames will not be checked for the required extentions
186     // @show_subdirfiles : 0 for NO, 1 for YES - meaning it will show files from the subdirs
187     // Files from subdirs will be prefixed with the subdir name and checked for the required extentions.
188     // @exts can be either a string or an array, if not passed to the function, then the default will be a check for common image files
189     // If exts is set to "all" then all extentions are allowed
190     // @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)
191     
192        $dirname = realpath($dirname);
458767 193        if (!$exts || empty($exts) || $exts == '') {
J 194            $exts = array('jpg', 'gif', 'jpeg', 'png');
436ed8 195        }
R 196        if ($handle = opendir($dirname)) {
197            $filelist = array();
198            while (false !== ($file = readdir($handle))) {
199     
200                // Filter out higher directory references
458767 201                if ($file != '.' && $file != '..') {
436ed8 202                    // Only look at directories or files, filter out symbolic links
458767 203                    if ( filetype ($dirname.'/'.$file) != 'link') {
436ed8 204                        // If it's a file, check against valid extentions and add to the list
458767 205                        if ( filetype ($dirname.'/'.$file) == 'file' ) {
436ed8 206                            if ($this->checkFileExtension($file, $exts, $ext_save)) {
R 207                                            $filelist[] = $file;
e2d6ed 208                            }
T 209                        }
436ed8 210                        // If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
458767 211                        else if ( filetype ($dirname.'/'.$file) == 'dir' && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
436ed8 212                            if ($show_subdirs == 1) {
R 213                                $filelist[] = $file;
214                            }
215                            if ($show_subdirfiles == 1) {
216                                $subdirname = $file;
458767 217                                $subdirfilelist = $this->getDirectoryListing($dirname.'/'.$subdirname.'/', $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
436ed8 218                                for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
458767 219                                    $subdirfilelist[$i] = $subdirname.'/'.$subdirfilelist[$i];
436ed8 220                                }
R 221                                $filelist = array_merge($filelist, $subdirfilelist);
222                            }
223     
224                        }
225     
e2d6ed 226                    }
T 227                }
228            }
436ed8 229            closedir($handle);
R 230     
231            // Sort the results
232            if (count($filelist) > 1) {
233                natcasesort($filelist);
458767 234                if ($sortorder == 'd' || $sortorder == 'r' ) {
436ed8 235                    $filelist = array_reverse($filelist, TRUE);
R 236                }
e2d6ed 237            }
436ed8 238            return $filelist;
e2d6ed 239        }
436ed8 240        else {
R 241            return false;
e2d6ed 242        }
436ed8 243     }
R 244     
245     function checkFileExtension($filename, $exts, $ext_save = 1){
246        $passed = FALSE;
247        if ($ext_save == 1) {
248            if (preg_match("/^\./", $filename)) {
e2d6ed 249                return $passed;
T 250            }
251        }
458767 252        if ($exts == 'all') {
436ed8 253                        $passed = TRUE;
R 254            return $passed;
255        }
256        if (is_string($exts)) {
043a0a 257            if (preg_match("/\.". $exts ."$/i", $filename)) {
436ed8 258                            $passed = TRUE;
R 259                return $passed;
260            }
261        } else if (is_array($exts)) {
262            foreach ($exts as $theExt) {
043a0a 263                if (preg_match("/\.". $theExt ."$/i", $filename)) {
436ed8 264                    $passed = TRUE;
R 265                    return $passed;
266                }
267            }
268        }
269        return $passed;
270     }
526b99 271     
T 272     function removeDirectory($dir){
273         //TODO: implement something to delete files/directories recursively that are owned by a certain user or group
274         if(is_dir($dir)){
275             $files = array_diff(scandir($dir), array('.','..'));
276             if(is_array($files) && !empty($files)){
277                 foreach($files as $file){
278                     if(is_dir($dir.'/'.$file)){
279                         $this->removeDirectory($dir.'/'.$file);
280                     } else {
281                         @unlink($dir.'/'.$file);
282                     }
283                 }
284             }
285             @rmdir($dir);
286         }
287     }
e2d6ed 288
T 289 }
10df6d 290 ?>