oliver
2007-08-27 cc3fb3d95d21724942a4fcc6f54d9a157940d119
commit | author | age
9200ad 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
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 /*
32     This function returns a string that describes the installed
33     linux distribution. e.g. debian40 for Debian Linux 4.0
34
35 */
36
c87c0a 37
P 38
39 /*
40 Comments to completion forever ;-)
41 commandline arguments
42 $argv[1]
43
44
45 <?
46 echo "Total argument passed are : $argc \n";
47 for( $i = 0 ; $i <= $argc -1 ;$i++)
48 {
49 echo "Argument $i : $argv[$i] \n";
50 }
51 ?> 
52
53 */
54 error_reporting(E_ALL|E_STRICT);
55
56
57 $FILE = realpath('../install.php');
58
cc3fb3 59 //** Get distribution identifier
9200ad 60 function get_distname() {
cc3fb3 61     
O 62     //** Debian
63     if(file_exists('/etc/debian_version')) {
64     
65         if(trim(file_get_contents('/etc/debian_version')) == '4.0') {
66             $distname = 'debian40';
67         }
68     }
69     
70     //** Redhat
71     elseif(file_exists("/etc/redhat_release")) {
72     
73     }
74     
9200ad 75     return $distname;
T 76 }
77
78 function sread() {
74030b 79     $f = fopen('/dev/stdin', 'r');
P 80     $input = fgets($f, 255);
81     fclose($f);
9200ad 82     return rtrim($input);
T 83 }
84
239ce8 85 function swrite($text = '') {
9200ad 86     echo $text;
T 87 }
88
239ce8 89 function swriteln($text = '') {
9200ad 90     echo $text."\n";
T 91 }
92
93 function ilog($msg){
66b4f9 94       exec("echo `date` \"- [ISPConfig] - \"".$msg." >> ".ISPC_LOG_FILE);
9200ad 95 }
T 96
97 function error($msg){
c87c0a 98     ilog($msg);
P 99     die($msg."\n");
9200ad 100 }
T 101
102 function caselog($command, $file = '', $line = '', $success = '', $failure = ''){
c87c0a 103     exec($command,$arr,$ret_val);
P 104     $arr = NULL;
105     if(!empty($file) && !empty($line)){
106         $pre = $file.', Line '.$line.': ';
107     } else {
108         $pre = '';
109     }
110     if($ret_val != 0){
111         if($failure == '') $failure = 'could not '.$command;
112         ilog($pre.'WARNING: '.$failure);
113     } else {
114         if($success == '') $success = $command;
115         ilog($pre.$success);
116     }
9200ad 117 }
T 118
119 function phpcaselog($ret_val, $msg, $file = '', $line = ''){
c87c0a 120     if(!empty($file) && !empty($line)){
P 121         $pre = $file.', Line '.$line.': ';
122     } else {
123         $pre = '';
124     }
125     if($ret_val == true){
126         ilog($pre.$msg);
127     } else {
128         ilog($pre.'WARNING: could not '.$msg);
129     }
130     return $ret_val;
9200ad 131 }
T 132
133 function mkdirs($strPath, $mode = '0755'){
2ffaf4 134     if(isset($strPath) && $strPath != ''){
c87c0a 135         //* Verzeichnisse rekursiv erzeugen
P 136         if(is_dir($strPath)){
137             return true;
138         }
139         $pStrPath = dirname($strPath);
140         if(!mkdirs($pStrPath, $mode)){
141             return false;
142         }
143         $old_umask = umask(0);
144         $ret_val = mkdir($strPath, octdec($mode));
145         umask($old_umask);
146         return $ret_val;
147     }
148     return false;
9200ad 149 }
T 150
151 function rf($file){
c87c0a 152     clearstatcache();
P 153     if(!$fp = fopen ($file, 'rb')){
154         ilog('WARNING: could not open file '.$file);
155     }
156     return filesize($file) > 0 ? fread($fp, filesize($file)) : '';
9200ad 157 }
T 158
159 function wf($file, $content){
c87c0a 160     mkdirs(dirname($file));
P 161     if(!$fp = fopen ($file, 'wb')){
162         ilog('WARNING: could not open file '.$file);
163     }
164     fwrite($fp, $content);
165     fclose($fp);
9200ad 166 }
T 167
168 function af($file, $content){
c87c0a 169     mkdirs(dirname($file));
P 170     if(!$fp = fopen ($file, 'ab')){
171         ilog('WARNING: could not open file '.$file);
172     }
173     fwrite($fp,$content);
174     fclose($fp);
9200ad 175 }
T 176
177 function aftsl($file, $content){
c87c0a 178     if(!$fp = fopen ($file, 'ab')){
P 179         ilog('WARNING: could not open file '.$file);
180     }
181     fwrite($fp,$content);
182     fclose($fp);
9200ad 183 }
T 184
185 function unix_nl($input){
c87c0a 186     $output = str_replace("\r\n", "\n", $input);
P 187     $output = str_replace("\r", "\n", $output);
188     return $output;
9200ad 189 }
T 190
191 function remove_blank_lines($input, $file = 1){
c87c0a 192     //TODO ? Leerzeilen l�schen
P 193     if($file){
194         $content = unix_nl(rf($input)); // WTF -pedro !
195     }else{
196         $content = $input;
197     }
198     $lines = explode("\n", $content);
199     if(!empty($lines)){
200         foreach($lines as $line){
201             if(trim($line) != '') $new_lines[] = $line;
202         }
203     }
204     if(is_array($new_lines)){
205         $content = implode("\n", $new_lines);
206     } else {
207         $content = '';
208     }
209     if($file){
210         wf($input, $content);
211     }else{
212         return $content;
213     }
9200ad 214 }
T 215
216 function no_comments($file, $comment = '#'){
c87c0a 217     $content = unix_nl(rf($file));
P 218     $lines = explode("\n", $content);
219     if(!empty($lines)){
220         foreach($lines as $line){
221             if(strstr($line, $comment)){
222                 $pos = strpos($line, $comment);
223                 if($pos != 0){
224                     $new_lines[] = substr($line,0,$pos);
225                 }else{
226                     $new_lines[] = '';
227                 }
228             }else{
229                 $new_lines[] = $line;
230             }
231         }
232     }
233     if(is_array($new_lines)){
234         $content_without_comments = implode("\n", $new_lines);
235         $new_lines = NULL;
236         return $content_without_comments;
237     } else {
238         return '';
239     }
9200ad 240 }
T 241
242 function find_includes($file){
243   global $httpd_root;
244   clearstatcache();
245   if(is_file($file) && filesize($file) > 0){
246     $includes[] = $file;
247     $inhalt = unix_nl(no_comments($file));
248     $lines = explode("\n", $inhalt);
249     if(!empty($lines)){
250       foreach($lines as $line){
2ffaf4 251         if(stristr($line, 'include ')){
P 252           $include_file = str_replace("\n", '', trim(shell_exec("echo \"$line\" | awk '{print \$2}'")));
253           if(substr($include_file,0,1) != '/'){
254             $include_file = $httpd_root.'/'.$include_file;
9200ad 255           }
T 256           if(is_file($include_file)){
257             if($further_includes = find_includes($include_file)){
258               $includes = array_merge($includes, $further_includes);
259             }
260           } else {
2ffaf4 261             if(strstr($include_file, '*')){
9200ad 262               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$9}'"));
T 263               if(!empty($more_files)){
264                 foreach($more_files as $more_file){
265                   if(is_file($more_file)){
266                     if($further_includes = find_includes($more_file)){
267                       $includes = array_merge($includes, $further_includes);
268                     }
269                   }
270                 }
271               }
272               unset($more_files);
273               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$10}'"));
274               if(!empty($more_files)){
275                 foreach($more_files as $more_file){
276                   if(is_file($more_file)){
277                     if($further_includes = find_includes($more_file)){
278                       $includes = array_merge($includes, $further_includes);
279                     }
280                   }
281                 }
282               }
283             }
284           }
285         }
286       }
287     }
288   }
289   if(is_array($includes)){
290     $includes = array_unique($includes);
291     return $includes;
292   } else {
293     return false;
294   }
295 }
296
297 function comment_out($file, $string){
c87c0a 298     $inhalt = no_comments($file);
P 299     $gesamt_inhalt = rf($file);
300     $modules = explode(',', $string);
301     foreach($modules as $val){
302         $val = trim($val);
303         if(strstr($inhalt, $val)){
304             $gesamt_inhalt = str_replace($val, '##ISPConfig INSTALL## '.$val, $gesamt_inhalt);
305         }
306     }
307     wf($file, $gesamt_inhalt);
9200ad 308 }
T 309
310 function is_word($string, $text, $params = ''){
c87c0a 311   //* params: i ??
P 312   return preg_match("/\b$string\b/$params", $text);
313   /*
9200ad 314   if(preg_match("/\b$string\b/$params", $text)) {
T 315     return true;
316   } else {
317     return false;
318   }
c87c0a 319   */
9200ad 320 }
T 321
322 function grep($content, $string, $params = ''){
323   // params: i, v, w
324   $content = unix_nl($content);
325   $lines = explode("\n", $content);
326   foreach($lines as $line){
327     if(!strstr($params, 'w')){
328       if(strstr($params, 'i')){
329         if(strstr($params, 'v')){
330           if(!stristr($line, $string)) $find[] = $line;
331         } else {
332           if(stristr($line, $string)) $find[] = $line;
333         }
334       } else {
335         if(strstr($params, 'v')){
336           if(!strstr($line, $string)) $find[] = $line;
337         } else {
338           if(strstr($line, $string)) $find[] = $line;
339         }
340       }
341     } else {
342       if(strstr($params, 'i')){
343         if(strstr($params, 'v')){
344           if(!is_word($string, $line, 'i')) $find[] = $line;
345         } else {
346           if(is_word($string, $line, 'i')) $find[] = $line;
347         }
348       } else {
349         if(strstr($params, 'v')){
350           if(!is_word($string, $line)) $find[] = $line;
351         } else {
352           if(is_word($string, $line)) $find[] = $line;
353         }
354       }
355     }
356   }
357   if(is_array($find)){
358     $ret_val = implode("\n", $find);
359     if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
360     $find = NULL;
361     return $ret_val;
362   } else {
363     return false;
364   }
365 }
366
367 function edit_xinetd_conf($service){
c87c0a 368     $xinetd_conf = '/etc/xinetd.conf';
P 369     $contents = unix_nl(rf($xinetd_conf));
370     $lines = explode("\n", $contents);
371     $j = sizeof($lines);
372     for($i=0;$i<sizeof($lines);$i++){
373         if(grep($lines[$i], $service, 'w')){
374             $fundstelle_anfang = $i;
375             $j = $i;
376             $parts = explode($lines[$i], $contents);
377         }
378         if($j < sizeof($lines)){
379             if(strstr($lines[$i], '}')){
380                 $fundstelle_ende = $i;
381                 $j = sizeof($lines);
382             }
383         }
384     }
385     if(isset($fundstelle_anfang) && isset($fundstelle_ende)){
386         for($i=$fundstelle_anfang;$i<=$fundstelle_ende;$i++){
387             if(strstr($lines[$i], 'disable')){
388                 $disable = explode('=', $lines[$i]);
389                 $disable[1] = ' yes';
390                 $lines[$i] = implode('=', $disable);
391             }
392         }
393     }
394     $fundstelle_anfang = NULL;
395     $fundstelle_ende = NULL;
396     $contents = implode("\n", $lines);
397     wf($xinetd_conf, $contents);
9200ad 398 }
T 399
c87c0a 400 ?>