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