file.inc.php 9.62 KB
Newer Older
tbrehm's avatar
tbrehm committed
1
<?php
redray's avatar
redray committed
2

tbrehm's avatar
tbrehm committed
3
/*
redray's avatar
redray committed
4
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
tbrehm's avatar
tbrehm committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class file{
redray's avatar
redray committed
32
33
34
35
    
    function rf($file){
      global $app;
      clearstatcache();
36
37
      if(!$fp = fopen ($file, 'rb')){
        $app->log('WARNING: Could not open file '.$file, 2);
redray's avatar
redray committed
38
39
40
41
        return false;
      } else {
        if(filesize($file) > 0){
          $content = fread($fp, filesize($file));
tbrehm's avatar
tbrehm committed
42
        } else {
43
          $content = '';
tbrehm's avatar
tbrehm committed
44
        }
redray's avatar
redray committed
45
46
47
48
49
50
51
52
        fclose($fp);
        return $content;
      }
    }
    
    function wf($file, $content){
      global $app;
      $this->mkdirs(dirname($file));
53
54
      if(!$fp = fopen ($file, 'wb')){
        $app->log('WARNING: Could not open file '.$file, 2);
redray's avatar
redray committed
55
        return false;
tbrehm's avatar
tbrehm committed
56
      } else {
redray's avatar
redray committed
57
58
59
        fwrite($fp,$content);
        fclose($fp);
        return true;
tbrehm's avatar
tbrehm committed
60
61
      }
    }
redray's avatar
redray committed
62
63
64
65
    
    function af($file, $content){
      global $app;
      $this->mkdirs(dirname($file));
66
67
      if(!$fp = fopen ($file, 'ab')){
        $app->log('WARNING: Could not open file '.$file, 2);
redray's avatar
redray committed
68
69
70
71
72
73
        return false;
      } else {
        fwrite($fp,$content);
        fclose($fp);
        return true;
      }
tbrehm's avatar
tbrehm committed
74
    }
redray's avatar
redray committed
75
76
77
78
79
80
81
82
83
84
85
    
    function no_comments($file, $comment = '#'){
      $content = $this->unix_nl($this->rf($file));
      $lines = explode("\n", $content);
      if(!empty($lines)){
        foreach($lines as $line){
          if(strstr($line, $comment)){
            $pos = strpos($line, $comment);
            if($pos != 0){
              $new_lines[] = substr($line,0,$pos);
            } else {
86
              $new_lines[] = '';
tbrehm's avatar
tbrehm committed
87
88
            }
          } else {
redray's avatar
redray committed
89
90
91
92
93
94
95
96
97
            $new_lines[] = $line;
          }
        }
      }
      if(is_array($new_lines)){
        $content_without_comments = implode("\n", $new_lines);
        $new_lines = NULL;
        return $content_without_comments;
      } else {
98
        return '';
redray's avatar
redray committed
99
100
101
102
103
104
105
106
107
108
      }
    }
    
    function manual_entries($file, $separator = '#### MAKE MANUAL ENTRIES BELOW THIS LINE! ####'){
      if(is_file($file)){
        $content = $this->rf($file);
        $parts = explode($separator, $content);
        $manual = "\n".trim($parts[1]);
        return $manual;
      } else {
109
        return '';
redray's avatar
redray committed
110
111
112
113
114
115
116
117
118
119
120
121
122
      }
    }
    
    function remove_blank_lines($input, $file = 1){
      //Leerzeilen löschen
      if($file){
        $content = $this->unix_nl($this->rf($input));
      } else {
        $content = $input;
      }
      $lines = explode("\n", $content);
      if(!empty($lines)){
        foreach($lines as $line){
123
          if(trim($line) != '') $new_lines[] = $line;
redray's avatar
redray committed
124
125
126
127
128
        }
      }
      if(is_array($new_lines)){
        $content = implode("\n", $new_lines);
      } else {
129
        $content = '';
redray's avatar
redray committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      }
      if($file){
        $this->wf($input, $content);
      } else {
        return $content;
      }
    }
    
    function unix_nl($input){
      $output = str_replace("\r\n", "\n", $input);
      $output = str_replace("\r", "\n", $output);
      return $output;
    }
    
    function fileowner($file){
      $owner_id = fileowner($file);
      clearstatcache();
      return $owner_id;
    }
    
    function mkdirs($strPath, $mode = '0755'){
      // Verzeichnisse rekursiv erzeugen
      if(is_dir($strPath)) return true;
      $pStrPath = dirname($strPath);
      if(!$this->mkdirs($pStrPath, $mode)) return false;
      $old_umask = umask(0);
      $ret_val = mkdir($strPath, octdec($mode));
      umask($old_umask);
      return $ret_val;
    }
    
    function edit_dist($var, $val){
      global $$var;
163
      $files = array('/root/ispconfig/dist.inc.php');
redray's avatar
redray committed
164
165
166
167
168
      foreach($files as $file){
        if(is_file($file)){
          $file_content = $this->unix_nl($this->rf($file));
          $lines = explode("\n", $file_content);
          for($i=0;$i<sizeof($lines);$i++){
169
            $parts = explode('=', $lines[$i]);
redray's avatar
redray committed
170
171
172
            if($parts[0] == $var || $parts[0] == '$'.$var.' '){
              $parts[1] = str_replace($$var, $val, $parts[1]);
            }
173
            $lines[$i] = implode('=', $parts);
redray's avatar
redray committed
174
175
176
          }
          $file_content = implode("\n", $lines);
          $this->wf($file, $file_content);
tbrehm's avatar
tbrehm committed
177
178
179
        }
      }
    }
redray's avatar
redray committed
180
    
181
    function getDirectoryListing($dirname, $sortorder = 'a', $show_subdirs = 0, $show_subdirfiles = 0, $exts = '', $ext_save = 1){
redray's avatar
redray committed
182
183
184
185
186
187
188
189
190
191
192
    // This function will return an array with filenames based on the criteria you can set in the variables
    // @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
    // @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
    // Logically subdirnames will not be checked for the required extentions
    // @show_subdirfiles : 0 for NO, 1 for YES - meaning it will show files from the subdirs
    // Files from subdirs will be prefixed with the subdir name and checked for the required extentions.
    // @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
    // If exts is set to "all" then all extentions are allowed
    // @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)
    
       $dirname = realpath($dirname);
193
194
       if (!$exts || empty($exts) || $exts == '') {
           $exts = array('jpg', 'gif', 'jpeg', 'png');
redray's avatar
redray committed
195
196
197
198
199
200
       }
       if ($handle = opendir($dirname)) {
           $filelist = array();
           while (false !== ($file = readdir($handle))) {
    
               // Filter out higher directory references
201
               if ($file != '.' && $file != '..') {
redray's avatar
redray committed
202
                   // Only look at directories or files, filter out symbolic links
203
                   if ( filetype ($dirname.'/'.$file) != 'link') {
redray's avatar
redray committed
204
                       // If it's a file, check against valid extentions and add to the list
205
                       if ( filetype ($dirname.'/'.$file) == 'file' ) {
redray's avatar
redray committed
206
207
208
                           if ($this->checkFileExtension($file, $exts, $ext_save)) {
                                           $filelist[] = $file;
                           }
tbrehm's avatar
tbrehm committed
209
                       }
redray's avatar
redray committed
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)) {
redray's avatar
redray committed
212
213
                           if ($show_subdirs == 1) {
                               $filelist[] = $file;
tbrehm's avatar
tbrehm committed
214
                           }
redray's avatar
redray committed
215
216
                           if ($show_subdirfiles == 1) {
                               $subdirname = $file;
217
                               $subdirfilelist = $this->getDirectoryListing($dirname.'/'.$subdirname.'/', $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
redray's avatar
redray committed
218
                               for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
219
                                   $subdirfilelist[$i] = $subdirname.'/'.$subdirfilelist[$i];
redray's avatar
redray committed
220
221
222
223
                               }
                               $filelist = array_merge($filelist, $subdirfilelist);
                           }
    
tbrehm's avatar
tbrehm committed
224
                       }
redray's avatar
redray committed
225
    
tbrehm's avatar
tbrehm committed
226
227
228
                   }
               }
           }
redray's avatar
redray committed
229
230
231
232
233
           closedir($handle);
    
           // Sort the results
           if (count($filelist) > 1) {
               natcasesort($filelist);
234
               if ($sortorder == 'd' || $sortorder == 'r' ) {
redray's avatar
redray committed
235
236
                   $filelist = array_reverse($filelist, TRUE);
               }
tbrehm's avatar
tbrehm committed
237
           }
redray's avatar
redray committed
238
           return $filelist;
tbrehm's avatar
tbrehm committed
239
       }
redray's avatar
redray committed
240
241
       else {
           return false;
tbrehm's avatar
tbrehm committed
242
       }
redray's avatar
redray committed
243
244
245
246
247
248
249
250
251
    }
    
    function checkFileExtension($filename, $exts, $ext_save = 1){
       $passed = FALSE;
       if ($ext_save == 1) {
           if (preg_match("/^\./", $filename)) {
               return $passed;
           }
       }
252
       if ($exts == 'all') {
tbrehm's avatar
tbrehm committed
253
254
255
                       $passed = TRUE;
           return $passed;
       }
redray's avatar
redray committed
256
       if (is_string($exts)) {
fantu's avatar
fantu committed
257
           if (preg_match("/\.". $exts ."$/i", $filename)) {
redray's avatar
redray committed
258
                           $passed = TRUE;
tbrehm's avatar
tbrehm committed
259
260
               return $passed;
           }
redray's avatar
redray committed
261
262
       } else if (is_array($exts)) {
           foreach ($exts as $theExt) {
fantu's avatar
fantu committed
263
               if (preg_match("/\.". $theExt ."$/i", $filename)) {
redray's avatar
redray committed
264
265
266
267
                   $passed = TRUE;
                   return $passed;
               }
           }
tbrehm's avatar
tbrehm committed
268
       }
redray's avatar
redray committed
269
270
       return $passed;
    }
tbrehm's avatar
tbrehm committed
271
272

}
273
?>