tbrehm
2008-08-18 b7332908d8ed550bf727df95a344b59ab1216e7c
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() {
ba66cd 86     $input = fgets(STDIN);
9200ad 87     return rtrim($input);
T 88 }
89
239ce8 90 function swrite($text = '') {
9200ad 91     echo $text;
T 92 }
93
239ce8 94 function swriteln($text = '') {
9200ad 95     echo $text."\n";
T 96 }
97
98 function ilog($msg){
66b4f9 99       exec("echo `date` \"- [ISPConfig] - \"".$msg." >> ".ISPC_LOG_FILE);
9200ad 100 }
T 101
102 function error($msg){
c87c0a 103     ilog($msg);
P 104     die($msg."\n");
9200ad 105 }
T 106
107 function caselog($command, $file = '', $line = '', $success = '', $failure = ''){
c87c0a 108     exec($command,$arr,$ret_val);
P 109     $arr = NULL;
110     if(!empty($file) && !empty($line)){
111         $pre = $file.', Line '.$line.': ';
112     } else {
113         $pre = '';
114     }
115     if($ret_val != 0){
116         if($failure == '') $failure = 'could not '.$command;
117         ilog($pre.'WARNING: '.$failure);
118     } else {
119         if($success == '') $success = $command;
120         ilog($pre.$success);
121     }
9200ad 122 }
T 123
124 function phpcaselog($ret_val, $msg, $file = '', $line = ''){
c87c0a 125     if(!empty($file) && !empty($line)){
P 126         $pre = $file.', Line '.$line.': ';
127     } else {
128         $pre = '';
129     }
130     if($ret_val == true){
131         ilog($pre.$msg);
132     } else {
133         ilog($pre.'WARNING: could not '.$msg);
134     }
135     return $ret_val;
9200ad 136 }
T 137
138 function mkdirs($strPath, $mode = '0755'){
2ffaf4 139     if(isset($strPath) && $strPath != ''){
c87c0a 140         //* Verzeichnisse rekursiv erzeugen
P 141         if(is_dir($strPath)){
142             return true;
143         }
144         $pStrPath = dirname($strPath);
145         if(!mkdirs($pStrPath, $mode)){
146             return false;
147         }
148         $old_umask = umask(0);
149         $ret_val = mkdir($strPath, octdec($mode));
150         umask($old_umask);
151         return $ret_val;
152     }
153     return false;
9200ad 154 }
T 155
156 function rf($file){
c87c0a 157     clearstatcache();
P 158     if(!$fp = fopen ($file, 'rb')){
159         ilog('WARNING: could not open file '.$file);
160     }
161     return filesize($file) > 0 ? fread($fp, filesize($file)) : '';
9200ad 162 }
T 163
164 function wf($file, $content){
c87c0a 165     mkdirs(dirname($file));
P 166     if(!$fp = fopen ($file, 'wb')){
167         ilog('WARNING: could not open file '.$file);
168     }
169     fwrite($fp, $content);
170     fclose($fp);
9200ad 171 }
T 172
173 function af($file, $content){
c87c0a 174     mkdirs(dirname($file));
P 175     if(!$fp = fopen ($file, 'ab')){
176         ilog('WARNING: could not open file '.$file);
177     }
178     fwrite($fp,$content);
179     fclose($fp);
9200ad 180 }
T 181
182 function aftsl($file, $content){
c87c0a 183     if(!$fp = fopen ($file, 'ab')){
P 184         ilog('WARNING: could not open file '.$file);
185     }
186     fwrite($fp,$content);
187     fclose($fp);
9200ad 188 }
T 189
190 function unix_nl($input){
c87c0a 191     $output = str_replace("\r\n", "\n", $input);
P 192     $output = str_replace("\r", "\n", $output);
193     return $output;
9200ad 194 }
T 195
196 function remove_blank_lines($input, $file = 1){
c87c0a 197     //TODO ? Leerzeilen l�schen
P 198     if($file){
199         $content = unix_nl(rf($input)); // WTF -pedro !
200     }else{
201         $content = $input;
202     }
203     $lines = explode("\n", $content);
204     if(!empty($lines)){
205         foreach($lines as $line){
206             if(trim($line) != '') $new_lines[] = $line;
207         }
208     }
209     if(is_array($new_lines)){
210         $content = implode("\n", $new_lines);
211     } else {
212         $content = '';
213     }
214     if($file){
215         wf($input, $content);
216     }else{
217         return $content;
218     }
9200ad 219 }
T 220
221 function no_comments($file, $comment = '#'){
c87c0a 222     $content = unix_nl(rf($file));
P 223     $lines = explode("\n", $content);
224     if(!empty($lines)){
225         foreach($lines as $line){
226             if(strstr($line, $comment)){
227                 $pos = strpos($line, $comment);
228                 if($pos != 0){
229                     $new_lines[] = substr($line,0,$pos);
230                 }else{
231                     $new_lines[] = '';
232                 }
233             }else{
234                 $new_lines[] = $line;
235             }
236         }
237     }
238     if(is_array($new_lines)){
239         $content_without_comments = implode("\n", $new_lines);
240         $new_lines = NULL;
241         return $content_without_comments;
242     } else {
243         return '';
244     }
9200ad 245 }
T 246
247 function find_includes($file){
248   global $httpd_root;
249   clearstatcache();
250   if(is_file($file) && filesize($file) > 0){
251     $includes[] = $file;
252     $inhalt = unix_nl(no_comments($file));
253     $lines = explode("\n", $inhalt);
254     if(!empty($lines)){
255       foreach($lines as $line){
2ffaf4 256         if(stristr($line, 'include ')){
P 257           $include_file = str_replace("\n", '', trim(shell_exec("echo \"$line\" | awk '{print \$2}'")));
258           if(substr($include_file,0,1) != '/'){
259             $include_file = $httpd_root.'/'.$include_file;
9200ad 260           }
T 261           if(is_file($include_file)){
262             if($further_includes = find_includes($include_file)){
263               $includes = array_merge($includes, $further_includes);
264             }
265           } else {
2ffaf4 266             if(strstr($include_file, '*')){
9200ad 267               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$9}'"));
T 268               if(!empty($more_files)){
269                 foreach($more_files as $more_file){
270                   if(is_file($more_file)){
271                     if($further_includes = find_includes($more_file)){
272                       $includes = array_merge($includes, $further_includes);
273                     }
274                   }
275                 }
276               }
277               unset($more_files);
278               $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$10}'"));
279               if(!empty($more_files)){
280                 foreach($more_files as $more_file){
281                   if(is_file($more_file)){
282                     if($further_includes = find_includes($more_file)){
283                       $includes = array_merge($includes, $further_includes);
284                     }
285                   }
286                 }
287               }
288             }
289           }
290         }
291       }
292     }
293   }
294   if(is_array($includes)){
295     $includes = array_unique($includes);
296     return $includes;
297   } else {
298     return false;
299   }
300 }
301
302 function comment_out($file, $string){
c87c0a 303     $inhalt = no_comments($file);
P 304     $gesamt_inhalt = rf($file);
305     $modules = explode(',', $string);
306     foreach($modules as $val){
307         $val = trim($val);
308         if(strstr($inhalt, $val)){
309             $gesamt_inhalt = str_replace($val, '##ISPConfig INSTALL## '.$val, $gesamt_inhalt);
310         }
311     }
312     wf($file, $gesamt_inhalt);
9200ad 313 }
T 314
315 function is_word($string, $text, $params = ''){
c87c0a 316   //* params: i ??
P 317   return preg_match("/\b$string\b/$params", $text);
318   /*
9200ad 319   if(preg_match("/\b$string\b/$params", $text)) {
T 320     return true;
321   } else {
322     return false;
323   }
c87c0a 324   */
9200ad 325 }
T 326
327 function grep($content, $string, $params = ''){
328   // params: i, v, w
329   $content = unix_nl($content);
330   $lines = explode("\n", $content);
331   foreach($lines as $line){
332     if(!strstr($params, 'w')){
333       if(strstr($params, 'i')){
334         if(strstr($params, 'v')){
335           if(!stristr($line, $string)) $find[] = $line;
336         } else {
337           if(stristr($line, $string)) $find[] = $line;
338         }
339       } else {
340         if(strstr($params, 'v')){
341           if(!strstr($line, $string)) $find[] = $line;
342         } else {
343           if(strstr($line, $string)) $find[] = $line;
344         }
345       }
346     } else {
347       if(strstr($params, 'i')){
348         if(strstr($params, 'v')){
349           if(!is_word($string, $line, 'i')) $find[] = $line;
350         } else {
351           if(is_word($string, $line, 'i')) $find[] = $line;
352         }
353       } else {
354         if(strstr($params, 'v')){
355           if(!is_word($string, $line)) $find[] = $line;
356         } else {
357           if(is_word($string, $line)) $find[] = $line;
358         }
359       }
360     }
361   }
362   if(is_array($find)){
363     $ret_val = implode("\n", $find);
364     if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
365     $find = NULL;
366     return $ret_val;
367   } else {
368     return false;
369   }
370 }
371
372 function edit_xinetd_conf($service){
c87c0a 373     $xinetd_conf = '/etc/xinetd.conf';
P 374     $contents = unix_nl(rf($xinetd_conf));
375     $lines = explode("\n", $contents);
376     $j = sizeof($lines);
377     for($i=0;$i<sizeof($lines);$i++){
378         if(grep($lines[$i], $service, 'w')){
379             $fundstelle_anfang = $i;
380             $j = $i;
381             $parts = explode($lines[$i], $contents);
382         }
383         if($j < sizeof($lines)){
384             if(strstr($lines[$i], '}')){
385                 $fundstelle_ende = $i;
386                 $j = sizeof($lines);
387             }
388         }
389     }
390     if(isset($fundstelle_anfang) && isset($fundstelle_ende)){
391         for($i=$fundstelle_anfang;$i<=$fundstelle_ende;$i++){
392             if(strstr($lines[$i], 'disable')){
393                 $disable = explode('=', $lines[$i]);
394                 $disable[1] = ' yes';
395                 $lines[$i] = implode('=', $disable);
396             }
397         }
398     }
399     $fundstelle_anfang = NULL;
400     $fundstelle_ende = NULL;
401     $contents = implode("\n", $lines);
402     wf($xinetd_conf, $contents);
9200ad 403 }
T 404
4f7028 405 //* Converts a ini string to array
T 406 function ini_to_array($ini) {
407     $config = '';
408     $ini = str_replace("\r\n", "\n", $ini);
409     $lines = explode("\n", $ini);
410     foreach($lines as $line) {
411         $line = trim($line);                
412         if($line != '') {
413             if(preg_match("/^\[([\w\d_]+)\]$/", $line, $matches)) {
414                 $section = strtolower($matches[1]);
415             } elseif(preg_match("/^([\w\d_]+)=(.*)$/", $line, $matches) && $section != null) {
416                 $item = trim($matches[1]);
417                 $config[$section][$item] = trim($matches[2]);
418             }
419         }
420     }
421     return $config;
422 }
423     
424     
425 //* Converts a config array to a string
a171d6 426 function array_to_ini($config_array = '') {
4f7028 427     if($config_array == '') $config_array = $this->config;
T 428     $content = '';
429     foreach($config_array as $section => $data) {
430         $content .= "[$section]\n";
431         foreach($data as $item => $value) {
432             if($item != ''){
433                 $content .= "$item=$value\n";
434             }
435         }
436         $content .= "\n";
437     }
438     return $content;
439 }
440
b73329 441 function is_user($user){
T 442   global $mod;
443   $user_datei = '/etc/passwd';
444   $users = no_comments($user_datei);
445   $lines = explode("\n", $users);
446   if(is_array($lines)){
447     foreach($lines as $line){
448       if(trim($line) != ""){
449         list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(":", $line);
450         if($f1 == $user) return true;
451       }
452     }
453   }
454   return false;
455 }
456
457 function is_group($group){
458   global $mod;
459   $group_datei = '/etc/group';
460   $groups = no_comments($group_datei);
461   $lines = explode("\n", $groups);
462   if(is_array($lines)){
463     foreach($lines as $line){
464       if(trim($line) != ""){
465         list($f1, $f2, $f3, $f4) = explode(":", $line);
466         if($f1 == $group) return true;
467       }
468     }
469   }
470   return false;
471 }
472
4f7028 473
T 474
c87c0a 475 ?>