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