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