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