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