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