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