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