Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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{
b1a6a5 32
MC 33     function rf($file){
34         global $app;
35         clearstatcache();
36         if(!$fp = fopen($file, 'rb')){
37             $app->log('WARNING: Could not open file '.$file, 2);
38             return false;
39         } else {
40             if(filesize($file) > 0){
41                 $content = fread($fp, filesize($file));
42             } else {
43                 $content = '';
44             }
45             fclose($fp);
46             return $content;
47         }
48     }
49
50     function wf($file, $content){
51         global $app;
52         $this->mkdirs(dirname($file));
53         if(!$fp = fopen($file, 'wb')){
54             $app->log('WARNING: Could not open file '.$file, 2);
55             return false;
56         } else {
57             fwrite($fp, $content);
58             fclose($fp);
59             return true;
60         }
61     }
62
63     function af($file, $content){
64         global $app;
65         $this->mkdirs(dirname($file));
66         if(!$fp = fopen($file, 'ab')){
67             $app->log('WARNING: Could not open file '.$file, 2);
68             return false;
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 {
86                         $new_lines[] = '';
87                     }
88                 } else {
89                     $new_lines[] = $line;
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 {
98             return '';
99         }
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 {
109             return '';
110         }
111     }
112
113     function remove_blank_lines($input, $file = 1){
114         //Leerzeilen löschen
115         if($file){
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){
123                 if(trim($line) != '') $new_lines[] = $line;
124             }
125         }
126         if(is_array($new_lines)){
127             $content = implode("\n", $new_lines);
128         } else {
129             $content = '';
130         }
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;
163         $files = array('/root/ispconfig/dist.inc.php');
164         foreach($files as $file){
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++){
169                     $parts = explode('=', $lines[$i]);
170                     if($parts[0] == $var || $parts[0] == '$'.$var.' '){
171                         $parts[1] = str_replace($$var, $val, $parts[1]);
172                     }
173                     $lines[$i] = implode('=', $parts);
174                 }
175                 $file_content = implode("\n", $lines);
176                 $this->wf($file, $file_content);
177             }
178         }
179     }
180
181     function getDirectoryListing($dirname, $sortorder = 'a', $show_subdirs = 0, $show_subdirfiles = 0, $exts = '', $ext_save = 1){
182         // This function will return an array with filenames based on the criteria you can set in the variables
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);
86e699 193         if (empty($exts)) {
b1a6a5 194             $exts = array('jpg', 'gif', 'jpeg', 'png');
MC 195         }
196         if ($handle = opendir($dirname)) {
197             $filelist = array();
198             while (false !== ($file = readdir($handle))) {
199
200                 // Filter out higher directory references
201                 if ($file != '.' && $file != '..') {
202                     // Only look at directories or files, filter out symbolic links
203                     if ( filetype($dirname.'/'.$file) != 'link') {
204                         // If it's a file, check against valid extentions and add to the list
205                         if ( filetype($dirname.'/'.$file) == 'file' ) {
206                             if ($this->checkFileExtension($file, $exts, $ext_save)) {
207                                 $filelist[] = $file;
208                             }
209                         }
210                         // If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
211                         else if ( filetype($dirname.'/'.$file) == 'dir' && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
212                                 if ($show_subdirs == 1) {
213                                     $filelist[] = $file;
214                                 }
215                                 if ($show_subdirfiles == 1) {
216                                     $subdirname = $file;
217                                     $subdirfilelist = $this->getDirectoryListing($dirname.'/'.$subdirname.'/', $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
218                                     for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
219                                         $subdirfilelist[$i] = $subdirname.'/'.$subdirfilelist[$i];
220                                     }
221                                     $filelist = array_merge($filelist, $subdirfilelist);
222                                 }
223
224                             }
225
226                     }
227                 }
228             }
229             closedir($handle);
230
231             // Sort the results
232             if (count($filelist) > 1) {
233                 natcasesort($filelist);
234                 if ($sortorder == 'd' || $sortorder == 'r' ) {
235                     $filelist = array_reverse($filelist, TRUE);
236                 }
237             }
238             return $filelist;
239         }
240         else {
241             return false;
242         }
243     }
244
245     function checkFileExtension($filename, $exts, $ext_save = 1){
246         $passed = FALSE;
247         if ($ext_save == 1) {
248             if (preg_match("/^\./", $filename)) {
249                 return $passed;
250             }
251         }
252         if ($exts == 'all') {
253             $passed = TRUE;
254             return $passed;
255         }
256         if (is_string($exts)) {
257             if (preg_match("/\.". $exts ."$/i", $filename)) {
258                 $passed = TRUE;
259                 return $passed;
260             }
261         } else if (is_array($exts)) {
262                 foreach ($exts as $theExt) {
263                     if (preg_match("/\.". $theExt ."$/i", $filename)) {
264                         $passed = TRUE;
265                         return $passed;
266                     }
267                 }
268             }
269         return $passed;
270     }
271
526b99 272     function removeDirectory($dir){
T 273         //TODO: implement something to delete files/directories recursively that are owned by a certain user or group
274         if(is_dir($dir)){
b1a6a5 275             $files = array_diff(scandir($dir), array('.', '..'));
526b99 276             if(is_array($files) && !empty($files)){
T 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 }
b1a6a5 290
10df6d 291 ?>