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