tbrehm
2008-12-26 7ff27228bdf12d4f5f6c303cf95d01aac9cb291e
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
c87c0a 36
P 37
38 /*
39 Comments to completion forever ;-)
40 commandline arguments
41 $argv[1]
42
43
44 <?
45 echo "Total argument passed are : $argc \n";
46 for( $i = 0 ; $i <= $argc -1 ;$i++)
47 {
48 echo "Argument $i : $argv[$i] \n";
49 }
50 ?> 
51
52 */
53 error_reporting(E_ALL|E_STRICT);
54
55
56 $FILE = realpath('../install.php');
57
cc3fb3 58 //** Get distribution identifier
9200ad 59 function get_distname() {
cc3fb3 60     
f629e2 61     $distname = '';
T 62     
63     //** Debian or Ubuntu
cc3fb3 64     if(file_exists('/etc/debian_version')) {
O 65     
66         if(trim(file_get_contents('/etc/debian_version')) == '4.0') {
344393 67             $distname = 'Debian';
T 68             $distver = '4.0';
69             $distid = 'debian40';
90511b 70             $distbaseid = 'debian';
03ade5 71             swriteln("Operating System: Debian 4.0 or compatible\n");
f629e2 72         }
7ff272 73         if(trim(file_get_contents('/etc/debian_version')) == '5.0' || trim(file_get_contents('/etc/debian_version')) == 'lenny/sid') {
344393 74             $distname = 'Debian';
T 75             $distver = 'Lenny/Sid';
76             $distid = 'debian40';
90511b 77             $distbaseid = 'debian';
03ade5 78             swriteln("Operating System: Debian Lenny/Sid or compatible\n");
cc3fb3 79         }
O 80     }
81     
7d89f5 82     //** OpenSuSE
T 83     elseif(file_exists("/etc/SuSE-release")) {
84         if(stristr(file_get_contents('/etc/SuSE-release'),'11.0')) {
344393 85             $distname = 'openSUSE';
T 86             $distver = '11.0';
87             $distid = 'opensuse110';
90511b 88             $distbaseid = 'opensuse';
7d89f5 89             swriteln("Operating System: openSUSE 11.0 or compatible\n");
T 90         }
91     }
92     
93     
cc3fb3 94     //** Redhat
0711af 95     elseif(file_exists("/etc/redhat-release")) {
T 96         
97         $content = file_get_contents('/etc/redhat-release');
98         
99         if(stristr($content,'Fedora release 9 (Sulphur)')) {
344393 100             $distname = 'Fedora';
T 101             $distver = '9';
102             $distid = 'fedora9';
90511b 103             $distbaseid = 'fedora';
0711af 104             swriteln("Operating System: Fedora 9 or compatible\n");
T 105         }
106         
663619 107         if(stristr($content,'CentOS release 5.2 (Final)')) {
T 108             $distname = 'CentOS';
109             $distver = '5.2';
110             $distid = 'centos52';
111             $distbaseid = 'fedora';
112             swriteln("Operating System: CentOS 5.2 or compatible\n");
113         }
114         
0711af 115         
7d89f5 116     } else {
T 117         die('unrecognized linux distribution');
cc3fb3 118     }
O 119     
90511b 120     return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
9200ad 121 }
T 122
123 function sread() {
ba66cd 124     $input = fgets(STDIN);
9200ad 125     return rtrim($input);
T 126 }
127
239ce8 128 function swrite($text = '') {
9200ad 129     echo $text;
T 130 }
131
239ce8 132 function swriteln($text = '') {
9200ad 133     echo $text."\n";
T 134 }
135
136 function ilog($msg){
66b4f9 137       exec("echo `date` \"- [ISPConfig] - \"".$msg." >> ".ISPC_LOG_FILE);
9200ad 138 }
T 139
140 function error($msg){
c87c0a 141     ilog($msg);
P 142     die($msg."\n");
9200ad 143 }
T 144
145 function caselog($command, $file = '', $line = '', $success = '', $failure = ''){
c87c0a 146     exec($command,$arr,$ret_val);
P 147     $arr = NULL;
148     if(!empty($file) && !empty($line)){
149         $pre = $file.', Line '.$line.': ';
150     } else {
151         $pre = '';
152     }
153     if($ret_val != 0){
154         if($failure == '') $failure = 'could not '.$command;
155         ilog($pre.'WARNING: '.$failure);
156     } else {
157         if($success == '') $success = $command;
158         ilog($pre.$success);
159     }
9200ad 160 }
T 161
162 function phpcaselog($ret_val, $msg, $file = '', $line = ''){
c87c0a 163     if(!empty($file) && !empty($line)){
P 164         $pre = $file.', Line '.$line.': ';
165     } else {
166         $pre = '';
167     }
168     if($ret_val == true){
169         ilog($pre.$msg);
170     } else {
171         ilog($pre.'WARNING: could not '.$msg);
172     }
173     return $ret_val;
9200ad 174 }
T 175
176 function mkdirs($strPath, $mode = '0755'){
2ffaf4 177     if(isset($strPath) && $strPath != ''){
c87c0a 178         //* Verzeichnisse rekursiv erzeugen
P 179         if(is_dir($strPath)){
180             return true;
181         }
182         $pStrPath = dirname($strPath);
183         if(!mkdirs($pStrPath, $mode)){
184             return false;
185         }
186         $old_umask = umask(0);
187         $ret_val = mkdir($strPath, octdec($mode));
188         umask($old_umask);
189         return $ret_val;
190     }
191     return false;
9200ad 192 }
T 193
194 function rf($file){
c87c0a 195     clearstatcache();
P 196     if(!$fp = fopen ($file, 'rb')){
197         ilog('WARNING: could not open file '.$file);
198     }
199     return filesize($file) > 0 ? fread($fp, filesize($file)) : '';
9200ad 200 }
T 201
202 function wf($file, $content){
c87c0a 203     mkdirs(dirname($file));
P 204     if(!$fp = fopen ($file, 'wb')){
205         ilog('WARNING: could not open file '.$file);
206     }
207     fwrite($fp, $content);
208     fclose($fp);
9200ad 209 }
T 210
211 function af($file, $content){
c87c0a 212     mkdirs(dirname($file));
P 213     if(!$fp = fopen ($file, 'ab')){
214         ilog('WARNING: could not open file '.$file);
215     }
216     fwrite($fp,$content);
217     fclose($fp);
9200ad 218 }
T 219
220 function aftsl($file, $content){
c87c0a 221     if(!$fp = fopen ($file, 'ab')){
P 222         ilog('WARNING: could not open file '.$file);
223     }
224     fwrite($fp,$content);
225     fclose($fp);
9200ad 226 }
T 227
228 function unix_nl($input){
c87c0a 229     $output = str_replace("\r\n", "\n", $input);
P 230     $output = str_replace("\r", "\n", $output);
231     return $output;
9200ad 232 }
T 233
234 function remove_blank_lines($input, $file = 1){
c87c0a 235     //TODO ? Leerzeilen l�schen
P 236     if($file){
237         $content = unix_nl(rf($input)); // WTF -pedro !
238     }else{
239         $content = $input;
240     }
241     $lines = explode("\n", $content);
242     if(!empty($lines)){
243         foreach($lines as $line){
244             if(trim($line) != '') $new_lines[] = $line;
245         }
246     }
247     if(is_array($new_lines)){
248         $content = implode("\n", $new_lines);
249     } else {
250         $content = '';
251     }
252     if($file){
253         wf($input, $content);
254     }else{
255         return $content;
256     }
9200ad 257 }
T 258
259 function no_comments($file, $comment = '#'){
c87c0a 260     $content = unix_nl(rf($file));
P 261     $lines = explode("\n", $content);
262     if(!empty($lines)){
263         foreach($lines as $line){
264             if(strstr($line, $comment)){
265                 $pos = strpos($line, $comment);
266                 if($pos != 0){
267                     $new_lines[] = substr($line,0,$pos);
268                 }else{
269                     $new_lines[] = '';
270                 }
271             }else{
272                 $new_lines[] = $line;
273             }
274         }
275     }
276     if(is_array($new_lines)){
277         $content_without_comments = implode("\n", $new_lines);
278         $new_lines = NULL;
279         return $content_without_comments;
280     } else {
281         return '';
282     }
9200ad 283 }
T 284
285 function find_includes($file){
286   global $httpd_root;
287   clearstatcache();
288   if(is_file($file) && filesize($file) > 0){
289     $includes[] = $file;
290     $inhalt = unix_nl(no_comments($file));
291     $lines = explode("\n", $inhalt);
292     if(!empty($lines)){
293       foreach($lines as $line){
2ffaf4 294         if(stristr($line, 'include ')){
P 295           $include_file = str_replace("\n", '', trim(shell_exec("echo \"$line\" | awk '{print \$2}'")));
296           if(substr($include_file,0,1) != '/'){
297             $include_file = $httpd_root.'/'.$include_file;
9200ad 298           }
T 299           if(is_file($include_file)){
300             if($further_includes = find_includes($include_file)){
301               $includes = array_merge($includes, $further_includes);
302             }
303           } else {
2ffaf4 304             if(strstr($include_file, '*')){
9200ad 305               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$9}'"));
T 306               if(!empty($more_files)){
307                 foreach($more_files as $more_file){
308                   if(is_file($more_file)){
309                     if($further_includes = find_includes($more_file)){
310                       $includes = array_merge($includes, $further_includes);
311                     }
312                   }
313                 }
314               }
315               unset($more_files);
316               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$10}'"));
317               if(!empty($more_files)){
318                 foreach($more_files as $more_file){
319                   if(is_file($more_file)){
320                     if($further_includes = find_includes($more_file)){
321                       $includes = array_merge($includes, $further_includes);
322                     }
323                   }
324                 }
325               }
326             }
327           }
328         }
329       }
330     }
331   }
332   if(is_array($includes)){
333     $includes = array_unique($includes);
334     return $includes;
335   } else {
336     return false;
337   }
338 }
339
340 function comment_out($file, $string){
c87c0a 341     $inhalt = no_comments($file);
P 342     $gesamt_inhalt = rf($file);
343     $modules = explode(',', $string);
344     foreach($modules as $val){
345         $val = trim($val);
346         if(strstr($inhalt, $val)){
347             $gesamt_inhalt = str_replace($val, '##ISPConfig INSTALL## '.$val, $gesamt_inhalt);
348         }
349     }
350     wf($file, $gesamt_inhalt);
9200ad 351 }
T 352
353 function is_word($string, $text, $params = ''){
c87c0a 354   //* params: i ??
P 355   return preg_match("/\b$string\b/$params", $text);
356   /*
9200ad 357   if(preg_match("/\b$string\b/$params", $text)) {
T 358     return true;
359   } else {
360     return false;
361   }
c87c0a 362   */
9200ad 363 }
T 364
365 function grep($content, $string, $params = ''){
366   // params: i, v, w
367   $content = unix_nl($content);
368   $lines = explode("\n", $content);
369   foreach($lines as $line){
370     if(!strstr($params, 'w')){
371       if(strstr($params, 'i')){
372         if(strstr($params, 'v')){
373           if(!stristr($line, $string)) $find[] = $line;
374         } else {
375           if(stristr($line, $string)) $find[] = $line;
376         }
377       } else {
378         if(strstr($params, 'v')){
379           if(!strstr($line, $string)) $find[] = $line;
380         } else {
381           if(strstr($line, $string)) $find[] = $line;
382         }
383       }
384     } else {
385       if(strstr($params, 'i')){
386         if(strstr($params, 'v')){
387           if(!is_word($string, $line, 'i')) $find[] = $line;
388         } else {
389           if(is_word($string, $line, 'i')) $find[] = $line;
390         }
391       } else {
392         if(strstr($params, 'v')){
393           if(!is_word($string, $line)) $find[] = $line;
394         } else {
395           if(is_word($string, $line)) $find[] = $line;
396         }
397       }
398     }
399   }
400   if(is_array($find)){
401     $ret_val = implode("\n", $find);
402     if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
403     $find = NULL;
404     return $ret_val;
405   } else {
406     return false;
407   }
408 }
409
410 function edit_xinetd_conf($service){
c87c0a 411     $xinetd_conf = '/etc/xinetd.conf';
P 412     $contents = unix_nl(rf($xinetd_conf));
413     $lines = explode("\n", $contents);
414     $j = sizeof($lines);
415     for($i=0;$i<sizeof($lines);$i++){
416         if(grep($lines[$i], $service, 'w')){
417             $fundstelle_anfang = $i;
418             $j = $i;
419             $parts = explode($lines[$i], $contents);
420         }
421         if($j < sizeof($lines)){
422             if(strstr($lines[$i], '}')){
423                 $fundstelle_ende = $i;
424                 $j = sizeof($lines);
425             }
426         }
427     }
428     if(isset($fundstelle_anfang) && isset($fundstelle_ende)){
429         for($i=$fundstelle_anfang;$i<=$fundstelle_ende;$i++){
430             if(strstr($lines[$i], 'disable')){
431                 $disable = explode('=', $lines[$i]);
432                 $disable[1] = ' yes';
433                 $lines[$i] = implode('=', $disable);
434             }
435         }
436     }
437     $fundstelle_anfang = NULL;
438     $fundstelle_ende = NULL;
439     $contents = implode("\n", $lines);
440     wf($xinetd_conf, $contents);
9200ad 441 }
T 442
4f7028 443 //* Converts a ini string to array
T 444 function ini_to_array($ini) {
445     $config = '';
446     $ini = str_replace("\r\n", "\n", $ini);
447     $lines = explode("\n", $ini);
448     foreach($lines as $line) {
449         $line = trim($line);                
450         if($line != '') {
451             if(preg_match("/^\[([\w\d_]+)\]$/", $line, $matches)) {
452                 $section = strtolower($matches[1]);
453             } elseif(preg_match("/^([\w\d_]+)=(.*)$/", $line, $matches) && $section != null) {
454                 $item = trim($matches[1]);
455                 $config[$section][$item] = trim($matches[2]);
456             }
457         }
458     }
459     return $config;
460 }
461     
462     
463 //* Converts a config array to a string
a171d6 464 function array_to_ini($config_array = '') {
4f7028 465     if($config_array == '') $config_array = $this->config;
T 466     $content = '';
467     foreach($config_array as $section => $data) {
468         $content .= "[$section]\n";
469         foreach($data as $item => $value) {
470             if($item != ''){
471                 $content .= "$item=$value\n";
472             }
473         }
474         $content .= "\n";
475     }
476     return $content;
477 }
478
b73329 479 function is_user($user){
T 480   global $mod;
481   $user_datei = '/etc/passwd';
482   $users = no_comments($user_datei);
483   $lines = explode("\n", $users);
484   if(is_array($lines)){
485     foreach($lines as $line){
486       if(trim($line) != ""){
487         list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(":", $line);
488         if($f1 == $user) return true;
489       }
490     }
491   }
492   return false;
493 }
494
495 function is_group($group){
496   global $mod;
497   $group_datei = '/etc/group';
498   $groups = no_comments($group_datei);
499   $lines = explode("\n", $groups);
500   if(is_array($lines)){
501     foreach($lines as $line){
502       if(trim($line) != ""){
503         list($f1, $f2, $f3, $f4) = explode(":", $line);
504         if($f1 == $group) return true;
505       }
506     }
507   }
508   return false;
509 }
510
0711af 511 function replaceLine($filename,$search_pattern,$new_line,$strict = 0) {
663619 512     if($lines = @file($filename)) {
0711af 513         $out = '';
T 514         $found = 0;
515         foreach($lines as $line) {
516             if($strict == 0) {
517                 if(stristr($line,$search_pattern)) {
518                     $out .= $new_line."\n";
519                     $found = 1;
520                 } else {
521                     $out .= $line;
522                 }
523             } else {
524                 if(trim($line) == $search_pattern) {
525                     $out .= $new_line."\n";
526                     $found = 1;
527                 } else {
528                     $out .= $line;
529                 }
530             }
531         }
532         if($found == 0) {
533             $out .= $new_line."\n";
534         }
535         file_put_contents($filename,$out);
663619 536     }
0711af 537 }
T 538     
539 function removeLine($filename,$search_pattern,$strict = 0) {
663619 540     if($lines = @file($filename)) {
0711af 541         $out = '';
T 542         foreach($lines as $line) {
543             if($strict == 0) {
544                 if(!stristr($line,$search_pattern)) {
545                     $out .= $line;
546                 }
547             } else {
548                 if(!trim($line) == $search_pattern) {
549                     $out .= $line;
550                 }
551             }
552         }
553         file_put_contents($filename,$out);
663619 554     }
0711af 555 }
T 556
0a1f02 557 function is_installed($appname) {
T 558     exec('which '.escapeshellcmd($appname),$out);
aee6fd 559     if(isset($out[0]) && stristr($out[0],$appname)) {
0a1f02 560         return true;
T 561     } else {
562         return false;
563     }
564 }
565
4f7028 566
T 567
d1386a 568 ?>