Marius Cramer
2014-03-19 0a0ca4f6a79f15308c6c2c6b4b7d1de9c8454e3b
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
996bad 10         * Redistributions of source code must retain the above copyright notice,
M 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.
9200ad 18
T 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
10df6d 33     Linux distribution. e.g. debian40 for Debian GNU/Linux 4.0
9200ad 34 */
T 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 }
996bad 50 ?>
c87c0a 51
P 52 */
53 error_reporting(E_ALL|E_STRICT);
54
55
56 $FILE = realpath('../install.php');
57
cc3fb3 58 //** Get distribution identifier
220bb9 59 //** IMPORTANT!
dead5c 60 //   This is the same code as in server/lib/classes/monitor_tools.inc.php
beb172 61 //   So if you change it here, you also have to change it in there!
9200ad 62 function get_distname() {
996bad 63
f629e2 64     $distname = '';
1b40a8 65     $distver = '';
T 66     $distid = '';
67     $distbaseid = '';
996bad 68
f629e2 69     //** Debian or Ubuntu
cc3fb3 70     if(file_exists('/etc/debian_version')) {
f35123 71         if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu')) {
T 72             if (strstr(trim(file_get_contents('/etc/issue')), 'LTS')) {
73                 $lts=" LTS";
74             } else {
75                 $lts="";
76             }
77
78             $issue=file_get_contents('/etc/issue');
79             $distname = 'Ubuntu';
80             $distid = 'debian40';
81             $distbaseid = 'debian';
7fe908 82             $ver = explode(' ', $issue);
f35123 83             $ver = array_filter($ver);
T 84             $ver = next($ver);
7fe908 85             $mainver = explode('.', $ver);
f35123 86             $mainver = array_filter($mainver);
T 87             $mainver = current($mainver).'.'.next($mainver);
88             switch ($mainver){
7fe908 89             case "12.10":
MC 90                 $relname = "(Quantal Quetzal)";
f35123 91                 break;
7fe908 92             case "12.04":
MC 93                 $relname = "(Precise Pangolin)";
f35123 94                 break;
7fe908 95             case "11.10":
MC 96                 $relname = "(Oneiric Ocelot)";
f35123 97                 break;
7fe908 98             case "11.14":
MC 99                 $relname = "(Natty Narwhal)";
f35123 100                 break;
7fe908 101             case "10.10":
MC 102                 $relname = "(Maverick Meerkat)";
f35123 103                 break;
7fe908 104             case "10.04":
MC 105                 $relname = "(Lucid Lynx)";
f35123 106                 break;
7fe908 107             case "9.10":
MC 108                 $relname = "(Karmic Koala)";
f35123 109                 break;
7fe908 110             case "9.04":
MC 111                 $relname = "(Jaunty Jackpole)";
f35123 112                 break;
7fe908 113             case "8.10":
f35123 114                 $relname = "(Intrepid Ibex)";
T 115                 break;
7fe908 116             case "8.04":
MC 117                 $relname = "(Hardy Heron)";
f35123 118                 break;
7fe908 119             case "7.10":
MC 120                 $relname = "(Gutsy Gibbon)";
f35123 121                 break;
7fe908 122             case "7.04":
MC 123                 $relname = "(Feisty Fawn)";
f35123 124                 break;
7fe908 125             case "6.10":
MC 126                 $relname = "(Edgy Eft)";
f35123 127                 break;
7fe908 128             case "6.06":
MC 129                 $relname = "(Dapper Drake)";
f35123 130                 break;
7fe908 131             case "5.10":
MC 132                 $relname = "(Breezy Badger)";
f35123 133                 break;
7fe908 134             case "5.04":
MC 135                 $relname = "(Hoary Hedgehog)";
f35123 136                 break;
7fe908 137             case "4.10":
MC 138                 $relname = "(Warty Warthog)";
f35123 139                 break;
7fe908 140             default:
MC 141                 $relname = "UNKNOWN";
f35123 142             }
T 143             $distver = $ver.$lts." ".$relname;
996bad 144             swriteln("Operating System: ".$distver."\n");
f35123 145         } elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
344393 146             $distname = 'Debian';
T 147             $distver = '4.0';
148             $distid = 'debian40';
90511b 149             $distbaseid = 'debian';
03ade5 150             swriteln("Operating System: Debian 4.0 or compatible\n");
7fe908 151         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
344393 152             $distname = 'Debian';
1cb2e1 153             $distver = 'Lenny';
344393 154             $distid = 'debian40';
90511b 155             $distbaseid = 'debian';
1cb2e1 156             swriteln("Operating System: Debian Lenny or compatible\n");
7fe908 157         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
1cb2e1 158             $distname = 'Debian';
F 159             $distver = 'Squeeze/Sid';
94927b 160             $distid = 'debian60';
1cb2e1 161             $distbaseid = 'debian';
94927b 162             swriteln("Operating System: Debian 6.0 (Squeeze/Sid) or compatible\n");
82ff62 163         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || substr(trim(file_get_contents('/etc/debian_version')),0,2) == '7.' || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') {
996bad 164             $distname = 'Debian';
M 165             $distver = 'Wheezy/Sid';
d6aef1 166             $distid = 'debian60';
996bad 167             $distbaseid = 'debian';
d6aef1 168             swriteln("Operating System: Debian 7.0 (Wheezy/Sid) or compatible\n");
996bad 169         } else {
1b40a8 170             $distname = 'Debian';
T 171             $distver = 'Unknown';
172             $distid = 'debian40';
173             $distbaseid = 'debian';
174             swriteln("Operating System: Debian or compatible, unknown version.\n");
cc3fb3 175         }
O 176     }
996bad 177
7d89f5 178     //** OpenSuSE
e38d14 179     elseif(file_exists('/etc/SuSE-release')) {
7fe908 180         if(stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
344393 181             $distname = 'openSUSE';
T 182             $distver = '11.0';
183             $distid = 'opensuse110';
90511b 184             $distbaseid = 'opensuse';
7d89f5 185             swriteln("Operating System: openSUSE 11.0 or compatible\n");
7fe908 186         } elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
a21c72 187             $distname = 'openSUSE';
T 188             $distver = '11.1';
189             $distid = 'opensuse110';
190             $distbaseid = 'opensuse';
191             swriteln("Operating System: openSUSE 11.1 or compatible\n");
7fe908 192         } elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
1b40a8 193             $distname = 'openSUSE';
df7e6d 194             $distver = '11.2';
T 195             $distid = 'opensuse112';
1b40a8 196             $distbaseid = 'opensuse';
T 197             swriteln("Operating System: openSUSE 11.2 or compatible\n");
198         }  else {
199             $distname = 'openSUSE';
200             $distver = 'Unknown';
df7e6d 201             $distid = 'opensuse112';
1b40a8 202             $distbaseid = 'opensuse';
T 203             swriteln("Operating System: openSUSE or compatible, unknown version.\n");
a21c72 204         }
7d89f5 205     }
996bad 206
M 207
cc3fb3 208     //** Redhat
e38d14 209     elseif(file_exists('/etc/redhat-release')) {
996bad 210
0711af 211         $content = file_get_contents('/etc/redhat-release');
996bad 212
7fe908 213         if(stristr($content, 'Fedora release 9 (Sulphur)')) {
344393 214             $distname = 'Fedora';
T 215             $distver = '9';
216             $distid = 'fedora9';
90511b 217             $distbaseid = 'fedora';
0711af 218             swriteln("Operating System: Fedora 9 or compatible\n");
7fe908 219         } elseif(stristr($content, 'Fedora release 10 (Cambridge)')) {
5939ea 220             $distname = 'Fedora';
F 221             $distver = '10';
222             $distid = 'fedora9';
223             $distbaseid = 'fedora';
224             swriteln("Operating System: Fedora 10 or compatible\n");
7fe908 225         } elseif(stristr($content, 'Fedora release 10')) {
e08bdc 226             $distname = 'Fedora';
T 227             $distver = '11';
228             $distid = 'fedora9';
229             $distbaseid = 'fedora';
230             swriteln("Operating System: Fedora 11 or compatible\n");
7fe908 231         } elseif(stristr($content, 'CentOS release 5.2 (Final)')) {
663619 232             $distname = 'CentOS';
T 233             $distver = '5.2';
234             $distid = 'centos52';
235             $distbaseid = 'fedora';
236             swriteln("Operating System: CentOS 5.2 or compatible\n");
7fe908 237         } elseif(stristr($content, 'CentOS release 5.3 (Final)')) {
1b40a8 238             $distname = 'CentOS';
T 239             $distver = '5.3';
edebc4 240             $distid = 'centos53';
1b40a8 241             $distbaseid = 'fedora';
T 242             swriteln("Operating System: CentOS 5.3 or compatible\n");
7fe908 243         } elseif(stristr($content, 'CentOS release 5')) {
fb3a98 244             $distname = 'CentOS';
T 245             $distver = 'Unknown';
246             $distid = 'centos53';
247             $distbaseid = 'fedora';
248             swriteln("Operating System: CentOS 5 or compatible\n");
1b40a8 249         } else {
T 250             $distname = 'Redhat';
251             $distver = 'Unknown';
252             $distid = 'fedora9';
253             $distbaseid = 'fedora';
254             swriteln("Operating System: Redhat or compatible, unknown version.\n");
663619 255         }
cb8c86 256     }
996bad 257
cb8c86 258     //** Gentoo
996bad 259     elseif(file_exists('/etc/gentoo-release')) {
M 260
261         $content = file_get_contents('/etc/gentoo-release');
262
7fe908 263         preg_match_all('/([0-9]{1,2})/', $content, $version);
996bad 264         $distname = 'Gentoo';
M 265         $distver = $version[0][0].$version[0][1];
266         $distid = 'gentoo';
267         $distbaseid = 'gentoo';
268         swriteln("Operating System: Gentoo $distver or compatible\n");
269
7d89f5 270     } else {
e38d14 271         die('Unrecognized GNU/Linux distribution');
cc3fb3 272     }
996bad 273
90511b 274     return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
9200ad 275 }
T 276
277 function sread() {
7fe908 278     $input = fgets(STDIN);
MC 279     return rtrim($input);
9200ad 280 }
T 281
239ce8 282 function swrite($text = '') {
9200ad 283     echo $text;
T 284 }
285
239ce8 286 function swriteln($text = '') {
9200ad 287     echo $text."\n";
T 288 }
289
290 function ilog($msg){
7fe908 291     exec("echo `date` \"- [ISPConfig] - \"".$msg.' >> '.ISPC_LOG_FILE);
9200ad 292 }
T 293
294 function error($msg){
c87c0a 295     ilog($msg);
P 296     die($msg."\n");
9200ad 297 }
T 298
299 function caselog($command, $file = '', $line = '', $success = '', $failure = ''){
7fe908 300     exec($command, $arr, $ret_val);
c87c0a 301     $arr = NULL;
P 302     if(!empty($file) && !empty($line)){
303         $pre = $file.', Line '.$line.': ';
304     } else {
305         $pre = '';
306     }
307     if($ret_val != 0){
308         if($failure == '') $failure = 'could not '.$command;
309         ilog($pre.'WARNING: '.$failure);
310     } else {
311         if($success == '') $success = $command;
312         ilog($pre.$success);
313     }
9200ad 314 }
T 315
316 function phpcaselog($ret_val, $msg, $file = '', $line = ''){
c87c0a 317     if(!empty($file) && !empty($line)){
P 318         $pre = $file.', Line '.$line.': ';
319     } else {
320         $pre = '';
321     }
322     if($ret_val == true){
323         ilog($pre.$msg);
324     } else {
325         ilog($pre.'WARNING: could not '.$msg);
326     }
327     return $ret_val;
9200ad 328 }
T 329
330 function mkdirs($strPath, $mode = '0755'){
2ffaf4 331     if(isset($strPath) && $strPath != ''){
c87c0a 332         //* Verzeichnisse rekursiv erzeugen
P 333         if(is_dir($strPath)){
334             return true;
335         }
336         $pStrPath = dirname($strPath);
337         if(!mkdirs($pStrPath, $mode)){
338             return false;
339         }
340         $old_umask = umask(0);
341         $ret_val = mkdir($strPath, octdec($mode));
342         umask($old_umask);
343         return $ret_val;
344     }
345     return false;
9200ad 346 }
T 347
615a0a 348 function rfsel($file, $file2) {
7fe908 349     clearstatcache();
MC 350     if(is_file($file)) return rf($file);
351     else return rf($file2);
615a0a 352 }
T 353
9200ad 354 function rf($file){
c87c0a 355     clearstatcache();
b77113 356     if(is_file($file)) {
7fe908 357         if(!$fp = fopen($file, 'rb')){
b77113 358             ilog('WARNING: could not open file '.$file);
T 359         }
360         return filesize($file) > 0 ? fread($fp, filesize($file)) : '';
361     } else {
362         return '';
c87c0a 363     }
9200ad 364 }
T 365
366 function wf($file, $content){
c87c0a 367     mkdirs(dirname($file));
7fe908 368     if(!$fp = fopen($file, 'wb')){
c87c0a 369         ilog('WARNING: could not open file '.$file);
P 370     }
371     fwrite($fp, $content);
372     fclose($fp);
9200ad 373 }
T 374
375 function af($file, $content){
c87c0a 376     mkdirs(dirname($file));
7fe908 377     if(!$fp = fopen($file, 'ab')){
c87c0a 378         ilog('WARNING: could not open file '.$file);
P 379     }
7fe908 380     fwrite($fp, $content);
c87c0a 381     fclose($fp);
9200ad 382 }
T 383
384 function aftsl($file, $content){
7fe908 385     if(!$fp = fopen($file, 'ab')){
c87c0a 386         ilog('WARNING: could not open file '.$file);
P 387     }
7fe908 388     fwrite($fp, $content);
c87c0a 389     fclose($fp);
9200ad 390 }
T 391
392 function unix_nl($input){
c87c0a 393     $output = str_replace("\r\n", "\n", $input);
P 394     $output = str_replace("\r", "\n", $output);
395     return $output;
9200ad 396 }
T 397
398 function remove_blank_lines($input, $file = 1){
c87c0a 399     //TODO ? Leerzeilen l�schen
P 400     if($file){
401         $content = unix_nl(rf($input)); // WTF -pedro !
402     }else{
403         $content = $input;
404     }
405     $lines = explode("\n", $content);
406     if(!empty($lines)){
407         foreach($lines as $line){
408             if(trim($line) != '') $new_lines[] = $line;
409         }
410     }
411     if(is_array($new_lines)){
412         $content = implode("\n", $new_lines);
413     } else {
414         $content = '';
415     }
416     if($file){
417         wf($input, $content);
418     }else{
419         return $content;
420     }
9200ad 421 }
T 422
423 function no_comments($file, $comment = '#'){
c87c0a 424     $content = unix_nl(rf($file));
P 425     $lines = explode("\n", $content);
426     if(!empty($lines)){
427         foreach($lines as $line){
428             if(strstr($line, $comment)){
429                 $pos = strpos($line, $comment);
430                 if($pos != 0){
7fe908 431                     $new_lines[] = substr($line, 0, $pos);
c87c0a 432                 }else{
P 433                     $new_lines[] = '';
434                 }
435             }else{
436                 $new_lines[] = $line;
437             }
438         }
439     }
440     if(is_array($new_lines)){
441         $content_without_comments = implode("\n", $new_lines);
442         $new_lines = NULL;
443         return $content_without_comments;
444     } else {
445         return '';
446     }
9200ad 447 }
T 448
449 function comment_out($file, $string){
c87c0a 450     $inhalt = no_comments($file);
P 451     $gesamt_inhalt = rf($file);
452     $modules = explode(',', $string);
453     foreach($modules as $val){
454         $val = trim($val);
455         if(strstr($inhalt, $val)){
456             $gesamt_inhalt = str_replace($val, '##ISPConfig INSTALL## '.$val, $gesamt_inhalt);
457         }
458     }
459     wf($file, $gesamt_inhalt);
9200ad 460 }
T 461
462 function is_word($string, $text, $params = ''){
996bad 463     //* params: i ??
M 464     return preg_match("/\b$string\b/$params", $text);
465     /*
466     if(preg_match("/\b$string\b/$params", $text)) {
467         return true;
468     } else {
469         return false;
470     }
471     */
9200ad 472 }
T 473
474 function grep($content, $string, $params = ''){
996bad 475     // params: i, v, w
M 476     $content = unix_nl($content);
477     $lines = explode("\n", $content);
478     foreach($lines as $line){
479         if(!strstr($params, 'w')){
480             if(strstr($params, 'i')){
481                 if(strstr($params, 'v')){
482                     if(!stristr($line, $string)) $find[] = $line;
483                 } else {
484                     if(stristr($line, $string)) $find[] = $line;
485                 }
486             } else {
487                 if(strstr($params, 'v')){
488                     if(!strstr($line, $string)) $find[] = $line;
489                 } else {
490                     if(strstr($line, $string)) $find[] = $line;
491                 }
492             }
493         } else {
494             if(strstr($params, 'i')){
495                 if(strstr($params, 'v')){
496                     if(!is_word($string, $line, 'i')) $find[] = $line;
497                 } else {
498                     if(is_word($string, $line, 'i')) $find[] = $line;
499                 }
500             } else {
501                 if(strstr($params, 'v')){
502                     if(!is_word($string, $line)) $find[] = $line;
503                 } else {
504                     if(is_word($string, $line)) $find[] = $line;
505                 }
506             }
507         }
508     }
509     if(is_array($find)){
510         $ret_val = implode("\n", $find);
7fe908 511         if(substr($ret_val, -1) != "\n") $ret_val .= "\n";
996bad 512         $find = NULL;
M 513         return $ret_val;
514     } else {
515         return false;
516     }
9200ad 517 }
T 518
519 function edit_xinetd_conf($service){
c87c0a 520     $xinetd_conf = '/etc/xinetd.conf';
P 521     $contents = unix_nl(rf($xinetd_conf));
522     $lines = explode("\n", $contents);
523     $j = sizeof($lines);
524     for($i=0;$i<sizeof($lines);$i++){
525         if(grep($lines[$i], $service, 'w')){
526             $fundstelle_anfang = $i;
527             $j = $i;
528             $parts = explode($lines[$i], $contents);
529         }
530         if($j < sizeof($lines)){
531             if(strstr($lines[$i], '}')){
532                 $fundstelle_ende = $i;
533                 $j = sizeof($lines);
534             }
535         }
536     }
537     if(isset($fundstelle_anfang) && isset($fundstelle_ende)){
538         for($i=$fundstelle_anfang;$i<=$fundstelle_ende;$i++){
539             if(strstr($lines[$i], 'disable')){
540                 $disable = explode('=', $lines[$i]);
541                 $disable[1] = ' yes';
542                 $lines[$i] = implode('=', $disable);
543             }
544         }
545     }
546     $fundstelle_anfang = NULL;
547     $fundstelle_ende = NULL;
548     $contents = implode("\n", $lines);
549     wf($xinetd_conf, $contents);
9200ad 550 }
T 551
4f7028 552 //* Converts a ini string to array
T 553 function ini_to_array($ini) {
554     $config = '';
555     $ini = str_replace("\r\n", "\n", $ini);
556     $lines = explode("\n", $ini);
557     foreach($lines as $line) {
7fe908 558         $line = trim($line);
4f7028 559         if($line != '') {
T 560             if(preg_match("/^\[([\w\d_]+)\]$/", $line, $matches)) {
561                 $section = strtolower($matches[1]);
562             } elseif(preg_match("/^([\w\d_]+)=(.*)$/", $line, $matches) && $section != null) {
563                 $item = trim($matches[1]);
564                 $config[$section][$item] = trim($matches[2]);
565             }
566         }
567     }
568     return $config;
569 }
996bad 570
M 571
4f7028 572 //* Converts a config array to a string
a171d6 573 function array_to_ini($config_array = '') {
4f7028 574     if($config_array == '') $config_array = $this->config;
T 575     $content = '';
576     foreach($config_array as $section => $data) {
577         $content .= "[$section]\n";
578         foreach($data as $item => $value) {
579             if($item != ''){
7fe908 580                 $content .= "$item=$value\n";
MC 581             }
4f7028 582         }
T 583         $content .= "\n";
584     }
585     return $content;
586 }
587
b73329 588 function is_user($user){
996bad 589     global $mod;
M 590     $user_datei = '/etc/passwd';
591     $users = no_comments($user_datei);
592     $lines = explode("\n", $users);
593     if(is_array($lines)){
594         foreach($lines as $line){
595             if(trim($line) != ''){
596                 list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
597                 if($f1 == $user) return true;
598             }
599         }
600     }
601     return false;
b73329 602 }
T 603
604 function is_group($group){
996bad 605     global $mod;
M 606     $group_datei = '/etc/group';
607     $groups = no_comments($group_datei);
608     $lines = explode("\n", $groups);
609     if(is_array($lines)){
610         foreach($lines as $line){
611             if(trim($line) != ''){
612                 list($f1, $f2, $f3, $f4) = explode(':', $line);
613                 if($f1 == $group) return true;
614             }
615         }
616     }
617     return false;
b73329 618 }
T 619
7fe908 620 function replaceLine($filename, $search_pattern, $new_line, $strict = 0, $append = 1) {
663619 621     if($lines = @file($filename)) {
0711af 622         $out = '';
T 623         $found = 0;
624         foreach($lines as $line) {
625             if($strict == 0) {
7fe908 626                 if(stristr($line, $search_pattern)) {
0711af 627                     $out .= $new_line."\n";
T 628                     $found = 1;
629                 } else {
630                     $out .= $line;
631                 }
632             } else {
633                 if(trim($line) == $search_pattern) {
634                     $out .= $new_line."\n";
635                     $found = 1;
636                 } else {
637                     $out .= $line;
638                 }
639             }
d78ed1 640             if (!$found) {
TB 641                 if (trim($line) == $new_line) {
642                     $found = 1;
643                 }
644             }
0711af 645         }
T 646         if($found == 0) {
b9dbe7 647             //* add \n if the last line does not end with \n or \r
7fe908 648             if(substr($out, -1) != "\n" && substr($out, -1) != "\r") $out .= "\n";
b9dbe7 649             //* add the new line at the end of the file
f28f40 650             if($append == 1) $out .= $new_line."\n";
0711af 651         }
7fe908 652         file_put_contents($filename, $out);
663619 653     }
0711af 654 }
996bad 655
7fe908 656 function removeLine($filename, $search_pattern, $strict = 0) {
663619 657     if($lines = @file($filename)) {
0711af 658         $out = '';
T 659         foreach($lines as $line) {
660             if($strict == 0) {
7fe908 661                 if(!stristr($line, $search_pattern)) {
0711af 662                     $out .= $line;
T 663                 }
664             } else {
665                 if(!trim($line) == $search_pattern) {
666                     $out .= $line;
667                 }
668             }
669         }
7fe908 670         file_put_contents($filename, $out);
663619 671     }
0711af 672 }
T 673
8eca28 674 function hasLine($filename, $search_pattern, $strict = 0) {
MC 675     if($lines = @file($filename)) {
676         foreach($lines as $line) {
677             if($strict == 0) {
678                 if(stristr($line, $search_pattern)) {
679                     return true;
680                 }
681             } else {
682                 if(trim($line) == $search_pattern) {
683                     return true;
684                 }
685             }
686         }
687     }
688     return false;
689 }
690
0a1f02 691 function is_installed($appname) {
7fe908 692     exec('which '.escapeshellcmd($appname).' 2> /dev/null', $out, $returncode);
MC 693     if(isset($out[0]) && stristr($out[0], $appname) && $returncode == 0) {
0a1f02 694         return true;
T 695     } else {
696         return false;
697     }
698 }
699
3250e5 700 /*
934c7d 701 * Get the port number of the ISPConfig controlpanel vhost
T 702 */
703
704 function get_ispconfig_port_number() {
705     global $conf;
4ffb51 706     if($conf['nginx']['installed'] == true){
F 707         $ispconfig_vhost_file = $conf['nginx']['vhost_conf_dir'].'/ispconfig.vhost';
708         $regex = '/listen (\d+)/';
709     } else {
710         $ispconfig_vhost_file = $conf['apache']['vhost_conf_dir'].'/ispconfig.vhost';
711         $regex = '/\<VirtualHost.*\:(\d{1,})\>/';
712     }
996bad 713
934c7d 714     if(is_file($ispconfig_vhost_file)) {
T 715         $tmp = file_get_contents($ispconfig_vhost_file);
7fe908 716         preg_match($regex, $tmp, $matches);
0799f8 717         $port_number = @intval($matches[1]);
934c7d 718         if($port_number > 0) {
T 719             return $port_number;
720         } else {
721             return '8080';
722         }
723     }
724 }
725
4ae2a0 726 /*
d0356f 727 * Get the port number of the ISPConfig apps vhost
TB 728 */
729
730 function get_apps_vhost_port_number() {
731     global $conf;
732     if($conf['nginx']['installed'] == true){
733         $ispconfig_vhost_file = $conf['nginx']['vhost_conf_dir'].'/apps.vhost';
734         $regex = '/listen (\d+)/';
735     } else {
736         $ispconfig_vhost_file = $conf['apache']['vhost_conf_dir'].'/apps.vhost';
737         $regex = '/\<VirtualHost.*\:(\d{1,})\>/';
738     }
739
740     if(is_file($ispconfig_vhost_file)) {
741         $tmp = file_get_contents($ispconfig_vhost_file);
742         preg_match($regex, $tmp, $matches);
743         $port_number = @intval($matches[1]);
744         if($port_number > 0) {
745             return $port_number;
746         } else {
747             return '8081';
748         }
749     }
750 }
751
752 /*
4ae2a0 753 * Get the port number of the ISPConfig controlpanel vhost
T 754 */
755
756 function is_ispconfig_ssl_enabled() {
757     global $conf;
758     $ispconfig_vhost_file = $conf['apache']['vhost_conf_dir'].'/ispconfig.vhost';
759
760     if(is_file($ispconfig_vhost_file)) {
761         $tmp = file_get_contents($ispconfig_vhost_file);
7fe908 762         if(stristr($tmp, 'SSLCertificateFile')) {
4ae2a0 763             return true;
T 764         } else {
765             return false;
766         }
767     }
768 }
769
7fe908 770 /**
MC 771  Function to find the hash file for timezone detection
772  (c) 2012 Marius Cramer, pixcept KG, m.cramer@pixcept.de
773  */
774
775
3898c9 776 function find_hash_file($hash, $dir, $basedir = '') {
7fe908 777     $res = opendir($dir);
MC 778     if(!$res) return false;
779
780     if(substr($basedir, -1) === '/') $basedir = substr($basedir, 0, strlen($basedir) - 1);
781     if(substr($dir, -1) === '/') $dir = substr($dir, 0, strlen($dir) - 1);
782     if($basedir === '') $basedir = $dir;
783
784     while($cur = readdir($res)) {
785         if($cur == '.' || $cur == '..') continue;
786         $entry = $dir.'/'.$cur;
787         if(is_dir($entry)) {
788             $result = find_hash_file($hash, $entry, $basedir);
789             if($result !== false) return $result;
790         } elseif(md5_file($entry) === $hash) {
791             $entry = substr($entry, strlen($basedir) + 1);
792             if(substr($entry, 0, 7) === '/posix/') $entry = substr($entry, 7);
793             return $entry;
794         }
795     }
796     closedir($res);
797     return false;
3898c9 798 }
T 799
7fe908 800
MC 801 /**
802  Function to get the timezone of the Linux system
803  (c) 2012 Marius Cramer, pixcept KG, m.cramer@pixcept.de
804  */
3898c9 805 function get_system_timezone() {
7fe908 806     $timezone = false;
MC 807     if(file_exists('/etc/timezone') && is_readable('/etc/timezone')) {
808         $timezone = trim(file_get_contents('/etc/timezone'));
809         if(file_exists('/usr/share/zoneinfo/' . $timezone) == false) $timezone = false;
810     }
3898c9 811
7fe908 812     if(!$timezone && is_link('/etc/localtime')) {
MC 813         $timezone = readlink('/etc/localtime');
814         $timezone = str_replace('/usr/share/zoneinfo/', '', $timezone);
696af9 815         $timezone = str_replace('..', '', $timezone);
7fe908 816         if(substr($timezone, 0, 6) === 'posix/') $timezone = substr($timezone, 6);
MC 817     } elseif(!$timezone) {
818         $hash = md5_file('/etc/localtime');
819         $timezone = find_hash_file($hash, '/usr/share/zoneinfo');
820     }
3898c9 821
7fe908 822     if(!$timezone) {
MC 823         exec('date +%Z', $tzinfo);
824         $timezone = $tzinfo[0];
825     }
da860c 826     
MC 827     if(substr($timezone, 0, 1) === '/') $timezone = substr($timezone, 1);
7fe908 828
MC 829     return $timezone;
3898c9 830 }
4f7028 831
ccbf14 832 function getapacheversion($get_minor = false) {
60b700 833     global $app;
MC 834     
835     $cmd = '';
836     if(is_installed('apache2ctl')) $cmd = 'apache2ctl -v';
837     elseif(is_installed('apachectl')) $cmd = 'apachectl -v';
838     else {
839         $app->log("Could not check apache version, apachectl not found.", LOGLEVEL_WARN);
840         return '2.2';
841     }
842     
843     exec($cmd, $output, $return_var);
844     if($return_var != 0 || !$output[0]) {
845         $app->log("Could not check apache version, apachectl did not return any data.", LOGLEVEL_WARN);
846         return '2.2';
847     }
848     
849     if(preg_match('/version:\s*Apache\/(\d+)(\.(\d+)(\.(\d+))*)?(\D|$)/i', $output[0], $matches)) {
850         return $matches[1] . (isset($matches[3]) ? '.' . $matches[3] : '') . (isset($matches[5]) && $get_minor == true ? '.' . $matches[5] : '');
851     } else {
852         $app->log("Could not check apache version, did not find version string in apachectl output.", LOGLEVEL_WARN);
853         return '2.2';
854     }
855 }
856
857 function getapachemodules() {
858     global $app;
859     
860     $cmd = '';
861     if(is_installed('apache2ctl')) $cmd = 'apache2ctl -t -D DUMP_MODULES';
862     elseif(is_installed('apachectl')) $cmd = 'apachectl -t -D DUMP_MODULES';
863     else {
864         $app->log("Could not check apache modules, apachectl not found.", LOGLEVEL_WARN);
865         return array();
866     }
867     
0a0ca4 868     exec($cmd . ' 2>/dev/null', $output, $return_var);
60b700 869     if($return_var != 0 || !$output[0]) {
MC 870         $app->log("Could not check apache modules, apachectl did not return any data.", LOGLEVEL_WARN);
871         return array();
872     }
873     
874     $modules = array();
875     for($i = 0; $i < count($output); $i++) {
876         if(preg_match('/^\s*(\w+)\s+\((shared|static)\)\s*$/', $output[$i], $matches)) {
877             $modules[] = $matches[1];
ccbf14 878         }
TB 879     }
60b700 880     
MC 881     return $modules;
882 }
4f7028 883
d1386a 884 ?>