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