tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
commit | author | age
e2d6ed 1 <?php
436ed8 2
e2d6ed 3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
e2d6ed 5 All rights reserved.
T 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
10     * Redistributions of source code must retain the above copyright notice,
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.
18
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 class system{
32
3b4c28 33     var $FILE = '/root/ispconfig/scripts/lib/classes/ispconfig_system.lib.php';
55da90 34     var $server_id;
B 35     var $server_conf;
36     var $data;
37     
38     /**
39      * Construct for this class
40      *
41      * @return system
42      */
43     public function system(){
cea1e5 44         //global $go_info;
T 45           //$this->server_id = $go_info['isp']['server_id'];
46           //$this->server_conf = $go_info['isp']['server_conf'];
3b4c28 47           $this->server_conf['passwd_datei'] = '/etc/passwd';
J 48           $this->server_conf['shadow_datei'] = '/etc/shadow';
49           $this->server_conf['group_datei'] = '/etc/group';
55da90 50     }
B 51     
52     /**
53      * Get the hostname from the server
54      *
55      * @return string
56      */
57     public function hostname(){
3b4c28 58         $dist = $this->server_conf['dist'];
55da90 59     
B 60          ob_start();
3b4c28 61           passthru('hostname');
55da90 62           $hostname = ob_get_contents();
B 63           ob_end_clean();
64         $hostname = trim($hostname);
65           ob_start();
3b4c28 66           if(!strstr($dist, 'freebsd')){
J 67             passthru('dnsdomainname');
55da90 68           } else {
3b4c28 69             passthru('domainname');
55da90 70           }
B 71           $domainname = ob_get_contents();
72           ob_end_clean();
73           $domainname = trim($domainname);
74           if($domainname != ""){
75             if(!strstr($hostname, $domainname)) $hostname .= ".".$domainname;
76           }
77           return $hostname;
78     }
79     
80     /**
81      * Add an user to the system
82      * 
83      */
84     public function adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
85         global $app;
86           if($this->is_user($user_username)){
87             return false;
88           } else {
89             if(trim($user_username) != '') {
3b4c28 90                 $user_datei = $this->server_conf['passwd_datei'];
J 91                 $shadow_datei = $this->server_conf['shadow_datei'];
55da90 92                 $shell = realpath($shell);
3b4c28 93                 if(trim($passwort) == '') $passwort = '*';
55da90 94                 $new_user = "\n$user_username:x:$uid:$gid:$username:$homedir:$shell\n";
3b4c28 95                 $app->log->msg('USER: '.$new_user);
55da90 96                 $app->file->af($user_datei, $new_user);
3b4c28 97                 if($shadow_datei == '/etc/shadow'){
55da90 98                     $datum = time();
B 99                     $tage = floor($datum/86400);
100                     $new_passwd = "\n$user_username:$passwort:$tage:0:99999:7:::\n";
101                 } else {
102                     $new_passwd = "\n$user_username:$passwort:$uid:$gid::0:0:$username:$homedir:$shell\n";
103                 }
104                 $app->file->af($shadow_datei, $new_passwd);
105                 // TB: leere Zeilen entfernen
106                 $app->file->remove_blank_lines($shadow_datei);
107                 $app->file->remove_blank_lines($user_datei);
108                 // TB: user Sortierung deaktiviert
109                 //$this->order_users_groups();
3b4c28 110                 if($shadow_datei != '/etc/shadow'){
55da90 111                     $app->file->af($shadow_datei, "\n");
B 112                     // TB: leere Zeilen entfernen
113                     $app->file->remove_blank_lines($shadow_datei);
114                     $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
115                 }
116                 return true;
117             }
118       }
119     }
120     
121     /**
122      * Update users when someone edit it
123      *
124      */
125     function updateuser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
126         //* First delete the users
127         $this->deluser($user_username);
128         //* Add the user again
129           $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
130     }
131     
132     /**
133      * Lock the user
134      *
135      */
136     function deactivateuser($user_username){
137         $passwort = str_rot13($this->getpasswd($user_username));
138           $user_attr = $this->get_user_attributes($user_username);
3b4c28 139           $uid = $user_attr['uid'];
J 140           $gid = $user_attr['gid'];
141           $username = $user_attr['name'];
142           $homedir = $user_attr['homedir'];
143           $shell = '/dev/null';
55da90 144           $this->deluser($user_username);
B 145           $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
146     }
147     /**
148      * Delete a user from the system
149      *
150      */
151     function deluser($user_username){
152         global $app;
153           if($this->is_user($user_username)){
3b4c28 154             $user_datei = $this->server_conf['passwd_datei'];
J 155             $shadow_datei = $this->server_conf['shadow_datei'];
55da90 156             $users = $app->file->rf($user_datei);
B 157             $lines = explode("\n", $users);
158             if(is_array($lines)){
159                   $num_lines = sizeof($lines);
160                   for($i=0;$i<$num_lines;$i++){
3b4c28 161                     if(trim($lines[$i]) != ''){
J 162                           list($f1,) = explode(':', $lines[$i]);
55da90 163                           if($f1 != $user_username) $new_lines[] = $lines[$i];
B 164                     }
165                   }
166                   $new_users = implode("\n", $new_lines);
167                   $app->file->wf($user_datei, $new_users);
168                   unset($new_lines);
169                   unset($lines);
170                   unset($new_users);
171             }
172             $app->file->remove_blank_lines($user_datei);
173     
174             $passwds = $app->file->rf($shadow_datei);
175             $lines = explode("\n", $passwds);
176             if(is_array($lines)){
177                   $num_lines = sizeof($lines);
178                   for($i=0;$i<$num_lines;$i++){
3b4c28 179                     if(trim($lines[$i]) != ''){
J 180                           list($f1,) = explode(':', $lines[$i]);
55da90 181                           if($f1 != $user_username) $new_lines[] = $lines[$i];
B 182                     }
183                   }
184                   $new_passwds = implode("\n", $new_lines);
185                   $app->file->wf($shadow_datei, $new_passwds);
186                   unset($new_lines);
187                   unset($lines);
188                   unset($new_passwds);
189             }
190             $app->file->remove_blank_lines($shadow_datei);
191     
3b4c28 192             $group_file = $app->file->rf($this->server_conf['group_datei']);
55da90 193             $group_file_lines = explode("\n", $group_file);
B 194             foreach($group_file_lines as $group_file_line){
3b4c28 195                   if(trim($group_file_line) != ''){
J 196                     list($f1, $f2, $f3, $f4) = explode(':', $group_file_line);
197                     $group_users = explode(',', str_replace(' ', '', $f4));
55da90 198                     if(in_array($user_username, $group_users)){
B 199                           $g_users = array();
200                           foreach($group_users as $group_user){
201                             if($group_user != $user_username) $g_users[] = $group_user;
202                           }
3b4c28 203                           $f4 = implode(',', $g_users);
55da90 204                     }
3b4c28 205                     $new_group_file[] = $f1.':'.$f2.':'.$f3.':'.$f4;
55da90 206                   }
B 207             }
208             $new_group_file = implode("\n", $new_group_file);
3b4c28 209             $app->file->wf($this->server_conf['group_datei'], $new_group_file);
55da90 210             // TB: auskommentiert
B 211             //$this->order_users_groups();
212     
3b4c28 213             if($shadow_datei != '/etc/shadow'){
55da90 214                   $app->file->af($shadow_datei, "\n");
B 215                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
216             }
217             return true;
218           } else {
219             return false;
220           }
221     }
222     
223     /**
224      * Add a usergroup to the system
225      *
226      */
227     function addgroup($group, $gid, $members = ''){
228         global $app;
229           if($this->is_group($group)){
230             return false;
231           } else {
3b4c28 232             $group_datei = $this->server_conf['group_datei'];
J 233             $shadow_datei = $this->server_conf['shadow_datei'];
55da90 234             $new_group = "\n$group:x:$gid:$members\n";
B 235             $app->file->af($group_datei, $new_group);
236     
237             // TB: auskommentiert
238             //$this->order_users_groups();
3b4c28 239             if($shadow_datei != '/etc/shadow'){
55da90 240                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
B 241             }
242             return true;
243           }
244     }
245     
246     /**
247      * Update usersgroup in way to delete and add it again
248      *
249      */
250     function updategroup($group, $gid, $members = ''){
251         $this->delgroup($group);
252           $this->addgroup($group, $gid, $members);
253     }
254     
255     /**
256      * Delete a usergroup from the system
257      *
258      */
259     function delgroup($group){
260         global $app;
261           if($this->is_group($group)){
3b4c28 262             $group_datei = $this->server_conf['group_datei'];
J 263             $shadow_datei = $this->server_conf['shadow_datei'];
55da90 264             $groups = $app->file->rf($group_datei);
B 265             $lines = explode("\n", $groups);
266             if(is_array($lines)){
267                   $num_lines = sizeof($lines);
268                   for($i=0;$i<$num_lines;$i++){
3b4c28 269                     if(trim($lines[$i]) != ''){
J 270                           list($f1,) = explode(':', $lines[$i]);
55da90 271                           if($f1 != $group) $new_lines[] = $lines[$i];
B 272                     }
273                   }
274                   $new_groups = implode("\n", $new_lines);
275                   $app->file->wf($group_datei, $new_groups);
276                   unset($new_lines);
277                   unset($lines);
278                   unset($new_groups);
279             }
280             // TB: auskommentiert
281             //$this->order_users_groups();
3b4c28 282             if($shadow_datei != '/etc/shadow'){
55da90 283                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
B 284             }
285             return true;
286           } else {
287             return false;
288           }
289     }
290     /**
291      * Order usergroups
292      *
293      */
294     function order_users_groups(){
295         global $app;
3b4c28 296           $user_datei = $this->server_conf['passwd_datei'];
J 297           $shadow_datei = $this->server_conf['shadow_datei'];
298           $group_datei = $this->server_conf['group_datei'];
55da90 299     
B 300           $groups = $app->file->no_comments($group_datei);
301           $lines = explode("\n", $groups);
302           if(is_array($lines)){
303             foreach($lines as $line){
3b4c28 304                   if(trim($line) != ''){
J 305                     list($f1, $f2, $f3, $f4) = explode(':', $line);
55da90 306                     $arr[$f3] = $line;
B 307                   }
308             }
309           }
310           ksort($arr);
311           reset($arr);
3b4c28 312           if($shadow_datei != '/etc/shadow'){
55da90 313             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0)."\n");
B 314           }else {
315             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
316           }
317           unset($arr);
318     
319           $users = $app->file->no_comments($user_datei);
320           $lines = explode("\n", $users);
321           if(is_array($lines)){
322             foreach($lines as $line){
323                   if(trim($line) != ""){
3b4c28 324                     list($f1, $f2, $f3,) = explode(':', $line);
J 325                     if($f1 != 'toor'){
55da90 326                           $arr[$f3] = $line;
B 327                     } else {
328                           $arr[70000] = $line;
329                     }
330                   }
331             }
332           }
333           ksort($arr);
334           reset($arr);
335           $app->file->wf($user_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
336           unset($arr);
337     
338           $passwds = $app->file->no_comments($shadow_datei);
339           $lines = explode("\n", $passwds);
340           if(is_array($lines)){
341             foreach($lines as $line){
3b4c28 342                 if(trim($line) != ''){
J 343                     list($f1, $f2, $f3,) = explode(':', $line);
344                     if($f1 != 'toor'){
55da90 345                           $uid = $this->getuid($f1);
B 346                           if(!is_bool($uid)) $arr[$uid] = $line;
347                     } else {
348                           $arr[70000] = $line;
349                     }
350                   }
351             }
352           }
353           ksort($arr);
354           reset($arr);
355           $app->file->wf($shadow_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
356           unset($arr);
357     }
358     
359     /**
360      * Find a user / group id
361      *
362      */
363     function find_uid_gid($min, $max){
364         global $app;
365           if($min < $max && $min >= 0 && $max >= 0 && $min <= 65536 && $max <= 65536 && is_int($min) && is_int($max)){
366             for($i=$min;$i<=$max;$i++){
367                   $uid_arr[$i] = $gid_arr[$i] = 1;
368             }
3b4c28 369             $user_datei = $this->server_conf['passwd_datei'];
J 370             $group_datei = $this->server_conf['group_datei'];
55da90 371     
B 372             $users = $app->file->no_comments($user_datei);
373             $lines = explode("\n", $users);
374             if(is_array($lines)){
375                   foreach($lines as $line){
3b4c28 376                     if(trim($line) != ''){
J 377                           list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
55da90 378                           if($f3 >= $min && $f3 <= $max) unset($uid_arr[$f3]);
B 379                     }
380                   }
381                   if(!empty($uid_arr)){
382                     foreach($uid_arr as $key => $val){
383                           $uids[] = $key;
384                     }
385                     $min_uid = min($uids);
386                     unset($uid_arr);
387                   } else {
388                     return false;
389                   }
390             }
391     
392             $groups = $app->file->no_comments($group_datei);
393             $lines = explode("\n", $groups);
394             if(is_array($lines)){
395                   foreach($lines as $line){
3b4c28 396                     if(trim($line) != ''){
J 397                           list($f1, $f2, $f3, $f4) = explode(':', $line);
55da90 398                           if($f3 >= $min && $f3 <= $max) unset($gid_arr[$f3]);
B 399                     }
400                   }
401                   if(!empty($gid_arr)){
402                     foreach($gid_arr as $key => $val){
403                           $gids[] = $key;
404                     }
405                     $min_gid = min($gids);
406                     unset($gid_arr);
407                   } else {
408                     return false;
409                   }
410             }
411     
412             $result = array_intersect($uids, $gids);
413             $new_id = (max($result));
414             unset($uids);
415             unset($gids);
416             unset($result);
417             if($new_id <= $max){
418                   return $new_id;
419             } else {
420                   return false;
421             }
422           } else {
423             return false;
424           }
425     }
426     
427     /**
428      * Check if the users is really a user into the system
429      *
430      */
431     function is_user($user){
432         global $app;
3b4c28 433           $user_datei = $this->server_conf['passwd_datei'];
55da90 434           $users = $app->file->no_comments($user_datei);
B 435           $lines = explode("\n", $users);
436           if(is_array($lines)){
437             foreach($lines as $line){
3b4c28 438                   if(trim($line) != ''){
J 439                     list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
55da90 440                     if($f1 == $user) return true;
B 441                   }
442             }
443           }
444           return false;
445     }
446     
447     /**
448      * Check if the group is on this system
449      *
450      */
451     function is_group($group){
452         global $app;
3b4c28 453           $group_datei = $this->server_conf['group_datei'];
55da90 454           $groups = $app->file->no_comments($group_datei);
B 455           $lines = explode("\n", $groups);
456           if(is_array($lines)){
457             foreach($lines as $line){
458                   if(trim($line) != ""){
3b4c28 459                     list($f1, $f2, $f3, $f4) = explode(':', $line);
55da90 460                     if($f1 == $group) return true;
B 461                   }
462             }
463           }
464           return false;
465     }
466     
89a57f 467     /*
T 468     // Alternative implementation of the is_group function. Should be faster then the old one To be tested.
469     function is_group($group) {
470     $groupfile = '/etc/group';
471     if(is_file($groupfile)) {
472         $handle = fopen ($groupfile, "r");
473         while (!feof($handle)) {
474             $line = trim(fgets($handle, 4096));
475             if($line != ""){
476                 $parts = explode(":", $line);
477                 if($parts[0] == $group) {
478                     fclose ($handle);
479                     return true;
480                 }
481             }
482         }
483         fclose ($handle);
484     }
485     return false;
486     }
487     */
488     
55da90 489     function root_group(){
B 490         global $app;
3b4c28 491           $group_datei = $this->server_conf['group_datei'];
55da90 492           $groups = $app->file->no_comments($group_datei);
B 493           $lines = explode("\n", $groups);
494           if(is_array($lines)){
495             foreach($lines as $line){
3b4c28 496                   if(trim($line) != ''){
J 497                     list($f1, $f2, $f3, $f4) = explode(':', $line);
55da90 498                     if($f3 == 0) return $f1;
B 499                   }
500             }
501           }
502           return false;
503     }
504     
505     /**
506      * Get the groups of an user
507      *
508      */
509     function get_user_groups($username){
510         global $app;
511           $user_groups = array();
3b4c28 512           $group_datei = $this->server_conf['group_datei'];
55da90 513           $groups = $app->file->no_comments($group_datei);
B 514           $lines = explode("\n", $groups);
515           if(is_array($lines)){
516             foreach($lines as $line){
3b4c28 517                   if(trim($line) != ''){
J 518                     list($f1, $f2, $f3, $f4) = explode(':', $line);
519                     if(intval($f3) < intval($this->server_conf['groupid_von']) && trim($f1) != 'users'){
55da90 520                           $tmp_group_users = explode(',', str_replace(' ', '', $f4));
B 521                           if(in_array($username, $tmp_group_users) && trim($f1) != '') $user_groups[] = $f1;
522                           unset($tmp_group_users);
523                     }
524                   }
525             }
526           }
527           if(!empty($user_groups)) return implode(',', $user_groups);
528           return '';
529     }
530     
531     /**
532      * Get a user password
533      *
534      */
535     function getpasswd($user){
536         global $app;
537           if($this->is_user($user)){
3b4c28 538             $shadow_datei = $this->server_conf['shadow_datei'];
55da90 539             $passwds = $app->file->no_comments($shadow_datei);
B 540             $lines = explode("\n", $passwds);
541             if(is_array($lines)){
542                   foreach($lines as $line){
3b4c28 543                     if(trim($line) != ''){
J 544                           list($f1, $f2,) = explode(':', $line);
55da90 545                           if($f1 == $user) return $f2;
B 546                     }
547                   }
548             }
549           } else {
550             return false;
551           }
552     }
553     
554     /**
555      * Get the user id from an user
556      *
557      */
558     function getuid($user){
559         global $app;
560           if($this->is_user($user)){
3b4c28 561             $user_datei = $this->server_conf['passwd_datei'];
55da90 562             $users = $app->file->no_comments($user_datei);
B 563             $lines = explode("\n", $users);
564             if(is_array($lines)){
565                   foreach($lines as $line){
3b4c28 566                     if(trim($line) != ''){
J 567                           list($f1, $f2, $f3,) = explode(':', $line);
55da90 568                           if($f1 == $user) return $f3;
B 569                     }
570                   }
571             }
572           } else {
573             return false;
574           }
575     }
576     
577     /**
578      * Get all information from a user
579      *
580      */
581     function get_user_attributes($user){
582         global $app;
583           if($this->is_user($user)){
3b4c28 584             $user_datei = $this->server_conf['passwd_datei'];
55da90 585             $users = $app->file->no_comments($user_datei);
B 586             $lines = explode("\n", $users);
587             if(is_array($lines)){
588                   foreach($lines as $line){
3b4c28 589                     if(trim($line) != ''){
J 590                           list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
55da90 591                           if($f1 == $user){
3b4c28 592                             $user_attr['username'] = $f1;
J 593                             $user_attr['x'] = $f2;
594                             $user_attr['uid'] = $f3;
595                             $user_attr['gid'] = $f4;
596                             $user_attr['name'] = $f5;
597                             $user_attr['homedir'] = $f6;
598                             $user_attr['shell'] = $f7;
55da90 599                             return $user_attr;
B 600                           }
601                     }
602                   }
603             }
604           } else {
605             return false;
606           }
607     }
608     
609     /**
610      * Edit the owner of a file
611      *
612      */
c3ac0a 613     function chown($file, $owner, $allow_symlink = false){
c77103 614       global $app;
c3ac0a 615       if($allow_symlink == false && $this->checkpath($file) == false) {
T 616         $app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
617         return false;
55da90 618       }
c77103 619       if(file_exists($file)) {
d87f76 620         if(@chown($file, $owner)) {
T 621             return true;
622         } else {
623             $app->log("chown failed: $file : $owner",LOGLEVEL_DEBUG);
624             return false;
625         }
c77103 626       }
c3ac0a 627     }
T 628     
629     function chgrp($file, $group = '', $allow_symlink = false){
c77103 630       global $app;
c3ac0a 631       if($allow_symlink == false && $this->checkpath($file) == false) {
T 632         $app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
633         return false;
55da90 634       }
c77103 635       if(file_exists($file)) {
d87f76 636         if(@chgrp($file, $group)) {
T 637             return true;
638         } else {
639             $app->log("chgrp failed: $file : $group",LOGLEVEL_DEBUG);
640             return false;
641         }
c77103 642       }
c3ac0a 643     }
T 644     
645     //* Change the mode of a file
646     function chmod($file, $mode, $allow_symlink = false) {
c77103 647         global $app;
c3ac0a 648         if($allow_symlink == false && $this->checkpath($file) == false) {
T 649             $app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
650             return false;
651         }
d87f76 652         if(@chmod($file, $mode)) {
T 653             return true;
654         } else {
655             $app->log("chmod failed: $file : $mode",LOGLEVEL_DEBUG);
656             return false;
657         }
c3ac0a 658     }
T 659     
660     function file_put_contents($filename, $data, $allow_symlink = false) {
c77103 661         global $app;
c3ac0a 662         if($allow_symlink == false && $this->checkpath($filename) == false) {
T 663             $app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
664             return false;
665         }
c77103 666         if(file_exists($filename)) unlink($filename);
c3ac0a 667         return file_put_contents($filename, $data);
T 668     }
669     
670     function file_get_contents($filename, $allow_symlink = false) {
c77103 671         global $app;
c3ac0a 672         if($allow_symlink == false && $this->checkpath($filename) == false) {
T 673             $app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
674             return false;
675         }
b889ed 676         return file_get_contents($filename, $data);
c3ac0a 677     }
T 678     
679     function rename($filename, $new_filename, $allow_symlink = false) {
c77103 680         global $app;
c3ac0a 681         if($allow_symlink == false && $this->checkpath($filename) == false) {
T 682             $app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
683             return false;
684         }
685         return rename($filename, $new_filename);
686     }
687     
688     function mkdir($dirname, $allow_symlink = false) {
c77103 689         global $app;
c3ac0a 690         if($allow_symlink == false && $this->checkpath($dirname) == false) {
T 691             $app->log("Action aborted, file is a symlink: $dirname",LOGLEVEL_WARN);
692             return false;
693         }
d87f76 694         if(@mkdir($dirname)) {
T 695             return true;
696         } else {
697             $app->log("mkdir failed: $dirname",LOGLEVEL_DEBUG);
698             return false;
699         }
c3ac0a 700     }
T 701     
58053e 702     function unlink($filename) {
b889ed 703         if(file_exists($filename) || is_link($filename)) {
c77103 704             return unlink($filename);
T 705         }
c3ac0a 706     }
T 707     
708     function copy($file1,$file2) {
709         return copy($file1,$file2);
710     }
711     
4bd960 712     function touch($file, $allow_symlink = false){
T 713       global $app;
714       if($allow_symlink == false && @file_exists($file) && $this->checkpath($file) == false) {
715         $this->unlink($file);
716       }
717       if(@touch($file)) {
718             return true;
719       } else {
720             $app->log("touch failed: $file",LOGLEVEL_DEBUG);
721             return false;
722       }
723     }
724     
c3ac0a 725     function checkpath($path) {
T 726         $path = trim($path);
727         //* We allow only absolute paths
728         if(substr($path,0,1) != '/') return false;
729         
730         //* We allow only some characters in the path
731         if(!preg_match('/[a-zA-Z0-9_\.\-]{1,}/',$path)) return false;
732         
733         //* Check path for symlinks
c77103 734         $path_parts = explode('/',$path);
c3ac0a 735         $testpath = '';
T 736         foreach($path_parts as $p) {
737             $testpath .= '/'.$p;
738             if(is_link($testpath)) return false;
739         }
740         
741         return true;
55da90 742     }
B 743     
744     /**
745      * Add an user to a specific group
746      *
747      */
748     function add_user_to_group($group, $user = 'admispconfig'){
749         global $app;
3b4c28 750           $group_file = $app->file->rf($this->server_conf['group_datei']);
55da90 751           $group_file_lines = explode("\n", $group_file);
B 752           foreach($group_file_lines as $group_file_line){
3b4c28 753             list($group_name,$group_x,$group_id,$group_users) = explode(':',$group_file_line);
55da90 754             if($group_name == $group){
3b4c28 755                   $group_users = explode(',', str_replace(' ', '', $group_users));
55da90 756                   if(!in_array($user, $group_users)){
B 757                     $group_users[] = $user;
758                   }
3b4c28 759                   $group_users = implode(',', $group_users);
J 760                   if(substr($group_users,0,1) == ',') $group_users = substr($group_users,1);
761                   $group_file_line = $group_name.':'.$group_x.':'.$group_id.':'.$group_users;
55da90 762             }
B 763             $new_group_file[] = $group_file_line;
764           }
765           $new_group_file = implode("\n", $new_group_file);
3b4c28 766           $app->file->wf($this->server_conf['group_datei'], $new_group_file);
J 767           $app->file->remove_blank_lines($this->server_conf['group_datei']);
768           if($this->server_conf['shadow_datei'] != '/etc/shadow'){
769             $app->log->caselog('pwd_mkdb '.$this->server_conf['shadow_datei'].' &> /dev/null', $this->FILE, __LINE__);
55da90 770           }
B 771     }
772     
ff6a68 773     /*
55da90 774     function usermod($user, $groups){
B 775         global $app;
776           if($this->is_user($user)){
3b4c28 777             $groups = explode(',', str_replace(' ', '', $groups));
J 778             $group_file = $app->file->rf($this->server_conf['group_datei']);
55da90 779             $group_file_lines = explode("\n", $group_file);
B 780             foreach($group_file_lines as $group_file_line){
781                   if(trim($group_file_line) != ""){
3b4c28 782                     list($f1, $f2, $f3, $f4) = explode(':', $group_file_line);
J 783                     $group_users = explode(',', str_replace(' ', '', $f4));
55da90 784                     if(!in_array($f1, $groups)){
B 785                           if(in_array($user, $group_users)){
786                             $g_users = array();
787                             foreach($group_users as $group_user){
788                                   if($group_user != $user) $g_users[] = $group_user;
789                             }
3b4c28 790                             $f4 = implode(',', $g_users);
55da90 791                           }
B 792                     } else {
793                           if(!in_array($user, $group_users)){
3b4c28 794                             if(trim($group_users[0]) == '') unset($group_users);
55da90 795                             $group_users[] = $user;
B 796                           }
3b4c28 797                           $f4 = implode(',', $group_users);
55da90 798                     }
3b4c28 799                     $new_group_file[] = $f1.':'.$f2.':'.$f3.':'.$f4;
55da90 800                   }
B 801             }
802             $new_group_file = implode("\n", $new_group_file);
3b4c28 803             $app->file->wf($this->server_conf['group_datei'], $new_group_file);
J 804             $app->file->remove_blank_lines($this->server_conf['group_datei']);
805             if($this->server_conf['shadow_datei'] != '/etc/shadow'){
806                   $app->log->caselog('pwd_mkdb '.$this->server_conf['shadow_datei'].' &> /dev/null', $this->FILE, __LINE__);
55da90 807             }
B 808             return true;
809           } else {
810             return false;
811           }
812     }
ff6a68 813     */
55da90 814     
B 815     /**boot autostart etc
816      *
817      */
818     function rc_edit($service, $rl, $action){
819         // $action = "on|off";
820           global $app;
3b4c28 821           $dist_init_scripts = $app->system->server_conf['dist_init_scripts'];
J 822           $dist_runlevel = $app->system->server_conf['dist_runlevel'];
823           $dist = $app->system->server_conf['dist'];
824           if(trim($dist_runlevel) == ''){ // falls es keine runlevel gibt (FreeBSD)
825             if($action == 'on'){
826                 @symlink($dist_init_scripts.'/'.$service, $dist_init_scripts.'/'.$service.'.sh');
55da90 827             }
3b4c28 828             if($action == 'off'){
J 829                   if(is_link($dist_init_scripts.'/'.$service.'.sh')){
830                     unlink($dist_init_scripts.'/'.$service.'.sh');
55da90 831                   } else {
3b4c28 832                     rename($dist_init_scripts.'/'.$service.'.sh',$dist_init_scripts.'/'.$service);
55da90 833                   }
B 834             }
835           } else { // Linux
836             if(substr($dist, 0,4) == 'suse'){
3b4c28 837                   if($action == 'on'){
55da90 838                     exec("chkconfig --add $service &> /dev/null");
B 839                   }
3b4c28 840                   if($action == 'off'){
55da90 841                     exec("chkconfig --del $service &> /dev/null");
B 842                   }
843             } else {
3b4c28 844                   $runlevels = explode(',', $rl);
55da90 845                   foreach($runlevels as $runlevel){
B 846                     $runlevel = trim($runlevel);
3b4c28 847                     if($runlevel != '' && is_dir($dist_runlevel.'/rc'.$runlevel.'.d')){
J 848                           $handle=opendir($dist_runlevel.'/rc'.$runlevel.'.d');
55da90 849                           while($file = readdir($handle)){
3b4c28 850                             if($file != '.' && $file != '..'){
J 851                                   $target = @readlink($dist_runlevel.'/rc'.$runlevel.'.d/'.$file);
852                                   if(strstr($file, $service) && strstr($target, $service) && substr($file,0,1) == 'S') $ln_arr[$runlevel][] = $dist_runlevel.'/rc'.$runlevel.'.d/'.$file;
55da90 853                             }
B 854                           }
855                           closedir($handle);
856                     }
3b4c28 857                     if($action == 'on'){
J 858                           if(!is_array($ln_arr[$runlevel])) @symlink($dist_init_scripts.'/'.$service, $dist_runlevel.'/rc'.$runlevel.'.d/S99'.$service);
55da90 859                     }
3b4c28 860                     if($action == 'off'){
55da90 861                           if(is_array($ln_arr[$runlevel])){
B 862                             foreach($ln_arr[$runlevel] as $link){
863                                   unlink($link);
864                             }
865                           }
866                     }
867                   }
868             }
869           }
870     }
871     
872     /**
873      * Filter information from the commands
874      *
875      */
876     function grep($content, $string, $params = ''){
877         global $app;
878           // params: i, v, w
879           $content = $app->file->unix_nl($content);
880           $lines = explode("\n", $content);
881           foreach($lines as $line){
882             if(!strstr($params, 'w')){
883                   if(strstr($params, 'i')){
884                     if(strstr($params, 'v')){
885                           if(!stristr($line, $string)) $find[] = $line;
886                     } else {
887                           if(stristr($line, $string)) $find[] = $line;
888                     }
889                   } else {
890                     if(strstr($params, 'v')){
891                           if(!strstr($line, $string)) $find[] = $line;
892                     } else {
893                           if(strstr($line, $string)) $find[] = $line;
894                     }
895                   }
896             } else {
897                   if(strstr($params, 'i')){
898                     if(strstr($params, 'v')){
899                           if(!$app->string->is_word($string, $line, 'i')) $find[] = $line;
900                     } else {
901                           if($app->string->is_word($string, $line, 'i')) $find[] = $line;
902                     }
903                   } else {
904                     if(strstr($params, 'v')){
905                           if(!$app->string->is_word($string, $line)) $find[] = $line;
906                     } else {
907                           if($app->string->is_word($string, $line)) $find[] = $line;
908                     }
909                   }
910             }
911           }
912           if(is_array($find)){
913             $ret_val = implode("\n", $find);
914             if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
915             $find = NULL;
916             return $ret_val;
917           } else {
918             return false;
919           }
920     }
921     
922     /**
923      * Strip content from fields
924      *
925      */
926     function cut($content, $field, $delimiter = ':'){
927         global $app;
928           $content = $app->file->unix_nl($content);
929           $lines = explode("\n", $content);
930           foreach($lines as $line){
931             $elms = explode($delimiter, $line);
932             $find[] = $elms[($field-1)];
933           }
934           if(is_array($find)){
935             $ret_val = implode("\n", $find);
936             if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
937             $find = NULL;
938             return $ret_val;
939           } else {
940             return false;
941           }
942     }
943     
944     /**
945      * Get the content off a file
946      *
947      */
948     function cat($file){
949         global $app;
950           return $app->file->rf($file);
951     }
952     
953     /**
954      * Control services to restart etc
955      *
956      */
957     function daemon_init($daemon, $action){
958         //* $action = start|stop|restart|reload
959           global $app;
3b4c28 960           $dist = $this->server_conf['dist'];
J 961           $dist_init_scripts = $this->server_conf['dist_init_scripts'];
962           if(!strstr($dist, 'freebsd')){
55da90 963             $app->log->caselog("$dist_init_scripts/$daemon $action &> /dev/null", $this->FILE, __LINE__);
B 964           } else {
3b4c28 965             if(is_file($dist_init_scripts.'/'.$daemon.'.sh') || is_link($dist_init_scripts.'/'.$daemon.'.sh')){
J 966                   if($action == 'start' || $action == 'stop'){
967                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh '.$action.' &> /dev/null', $this->FILE, __LINE__);
55da90 968                   } else {
3b4c28 969                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh stop &> /dev/null', $this->FILE, __LINE__);
55da90 970                     sleep(3);
3b4c28 971                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh start &> /dev/null', $this->FILE, __LINE__);
55da90 972                   }
B 973             } else {
3b4c28 974                   if(is_file($dist_init_scripts.'/'.$daemon) || is_link($dist_init_scripts.'/'.$daemon)){
J 975                     if($action == 'start' || $action == 'stop'){
976                           $app->log->caselog($dist_init_scripts.'/'.$daemon.' '.$action.' &> /dev/null', $this->FILE, __LINE__);
55da90 977                     } else {
3b4c28 978                           $app->log->caselog($dist_init_scripts.'/'.$daemon.' stop &> /dev/null', $this->FILE, __LINE__);
55da90 979                           sleep(3);
3b4c28 980                           $app->log->caselog($dist_init_scripts.'/'.$daemon.' start &> /dev/null', $this->FILE, __LINE__);
55da90 981                     }
B 982                   } else {
3b4c28 983                     if(is_file('/etc/rc.d/'.$daemon) || is_link('/etc/rc.d/'.$daemon)){
J 984                           if($action == 'start' || $action == 'stop'){
985                             $app->log->caselog('/etc/rc.d/'.$daemon.' '.$action.' &> /dev/null', $this->FILE, __LINE__);
55da90 986                           } else {
3b4c28 987                             $app->log->caselog('/etc/rc.d/'.$daemon.' stop &> /dev/null', $this->FILE, __LINE__);
55da90 988                             sleep(3);
3b4c28 989                             $app->log->caselog('/etc/rc.d/'.$daemon.' start &> /dev/null', $this->FILE, __LINE__);
55da90 990                           }
B 991                     }
992                   }
993             }
994           }
995     }
996     
997     function netmask($netmask){
3b4c28 998       list($f1,$f2,$f3,$f4) = explode('.', trim($netmask));
J 999       $bin = str_pad(decbin($f1),8,'0',STR_PAD_LEFT).str_pad(decbin($f2),8,'0',STR_PAD_LEFT).str_pad(decbin($f3),8,'0',STR_PAD_LEFT).str_pad(decbin($f4),8,'0',STR_PAD_LEFT);
1000       $parts = explode('0', $bin);
1001       $bin = str_pad($parts[0], 32, '0', STR_PAD_RIGHT);
1002       $bin = wordwrap($bin, 8, '.', 1);
1003       list($f1,$f2,$f3,$f4) = explode('.', trim($bin));
1004       return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1005     }
B 1006     
1007     function binary_netmask($netmask){
3b4c28 1008       list($f1,$f2,$f3,$f4) = explode('.', trim($netmask));
J 1009       $bin = str_pad(decbin($f1),8,'0',STR_PAD_LEFT).str_pad(decbin($f2),8,'0',STR_PAD_LEFT).str_pad(decbin($f3),8,'0',STR_PAD_LEFT).str_pad(decbin($f4),8,'0',STR_PAD_LEFT);
1010       $parts = explode('0', $bin);
1011       return substr_count($parts[0], '1');
55da90 1012     }
B 1013     
1014     function network($ip, $netmask){
1015       $netmask = $this->netmask($netmask);
3b4c28 1016       list($f1,$f2,$f3,$f4) = explode('.', $netmask);
J 1017       $netmask_bin = str_pad(decbin($f1),8,'0',STR_PAD_LEFT).str_pad(decbin($f2),8,'0',STR_PAD_LEFT).str_pad(decbin($f3),8,'0',STR_PAD_LEFT).str_pad(decbin($f4),8,'0',STR_PAD_LEFT);
1018       list($f1,$f2,$f3,$f4) = explode('.', $ip);
1019       $ip_bin = str_pad(decbin($f1),8,'0',STR_PAD_LEFT).str_pad(decbin($f2),8,'0',STR_PAD_LEFT).str_pad(decbin($f3),8,'0',STR_PAD_LEFT).str_pad(decbin($f4),8,'0',STR_PAD_LEFT);
55da90 1020       for($i=0;$i<32;$i++){
B 1021         $network_bin .= substr($netmask_bin,$i,1) * substr($ip_bin,$i,1);
1022       }
3b4c28 1023       $network_bin = wordwrap($network_bin, 8, '.', 1);
J 1024       list($f1,$f2,$f3,$f4) = explode('.', trim($network_bin));
1025       return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1026     }
B 1027     
1028     /**
1029      * Make a broadcast address from an IP number in combination with netmask
1030      *
1031      */
1032     function broadcast($ip, $netmask){
1033         $netmask = $this->netmask($netmask);
1034           $binary_netmask = $this->binary_netmask($netmask);
3b4c28 1035           list($f1,$f2,$f3,$f4) = explode('.', $ip);
J 1036           $ip_bin = str_pad(decbin($f1),8,'0',STR_PAD_LEFT).str_pad(decbin($f2),8,'0',STR_PAD_LEFT).str_pad(decbin($f3),8,'0',STR_PAD_LEFT).str_pad(decbin($f4),8,'0',STR_PAD_LEFT);
1037           $broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask),32,'1',STR_PAD_RIGHT);
1038           $broadcast_bin = wordwrap($broadcast_bin, 8, '.', 1);
1039           list($f1,$f2,$f3,$f4) = explode('.', trim($broadcast_bin));
1040           return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1041     }
B 1042     
1043     /**
1044      * Get the network address information
1045      *
1046      */
1047     function network_info(){
458767 1048         $dist = $this->server_conf['dist'];
55da90 1049           ob_start();
3b4c28 1050           passthru('ifconfig');
55da90 1051           $output = ob_get_contents();
B 1052           ob_end_clean();
1053           $lines = explode("\n", $output);
1054           foreach($lines as $line){
3b4c28 1055             $elms = explode(' ', $line);
J 1056             if(trim($elms[0]) != '' && substr($elms[0],0,1) != "\t"){
55da90 1057                   $elms[0] = trim($elms[0]);
3b4c28 1058                   if(strstr($dist, 'freebsd')) $elms[0] = substr($elms[0],0,-1);
55da90 1059                   $interfaces[] = $elms[0];
B 1060             }
1061           }
1062           if(!empty($interfaces)){
1063             foreach($interfaces as $interface){
1064                 ob_start();
3b4c28 1065                   if(!strstr($dist, 'freebsd')){
J 1066                     passthru('ifconfig '.$interface." | grep -iw 'inet' | cut -f2 -d: | cut -f1 -d' '");
55da90 1067                   } else {
3b4c28 1068                     passthru('ifconfig '.$interface." | grep -iw 'inet' | grep -iv 'inet6' | cut -f2 -d' '");
55da90 1069                   }
B 1070                   $output = trim(ob_get_contents());
1071                   ob_end_clean();
458767 1072                   if($output != ''){
J 1073                     $ifconfig['INTERFACE'][$interface] = $output;
1074                     $ifconfig['IP'][$output] = $interface;
55da90 1075                   }
B 1076             }
1077             if(!empty($ifconfig)){
1078                   return $ifconfig;
1079             } else {
1080                   return false;
1081             }
1082           } else {
1083             return false;
1084           }
1085     }
1086     
1087     /**
1088      * Configure the network settings from the system
1089      *
1090      */
1091     function network_config(){
1092         $ifconfig = $this->network_info();
1093           if($ifconfig){
3b4c28 1094             $main_interface = $ifconfig['IP'][$this->server_conf['server_ip']];
J 1095             if(strstr($main_interface, ':')){
1096                   $parts = explode(':', $main_interface);
55da90 1097                   $main_interface = trim($parts[0]);
B 1098             }
3b4c28 1099             if($main_interface != ''){
J 1100                   $ips = $this->data['isp_server_ip'];
55da90 1101                   if(!empty($ips)){
B 1102                     foreach($ips as $ip){
3b4c28 1103                           if(!isset($ifconfig['IP'][$ip['server_ip']])){
J 1104                             $to_set[] = $ip['server_ip'];
55da90 1105                           } else {
3b4c28 1106                             unset($ifconfig['IP'][$ip['server_ip']]);
55da90 1107                           }
B 1108                     }
3b4c28 1109                     if(!empty($ifconfig['IP'])){
J 1110                           foreach($ifconfig['IP'] as $key => $val){
1111                             if(!strstr($val, 'lo') && !strstr($val, 'lp') && strstr($val, $main_interface)){
1112                                   exec('ifconfig '.$val.' down &> /dev/null');
1113                                   unset($ifconfig['INTERFACE'][$val]);
55da90 1114                             }
B 1115                           }
1116                     }
1117                     if(!empty($to_set)){
1118                          foreach($to_set as $to){
1119                                $i = 0;
1120                                while($i >= 0){
3b4c28 1121                                  if(isset($ifconfig['INTERFACE'][$main_interface.':'.$i])){
55da90 1122                                        $i++;
B 1123                                  } else {
3b4c28 1124                                        $new_interface = $main_interface.':'.$i;
55da90 1125                                        $i = -1;
B 1126                                  }
1127                                }
3b4c28 1128                                exec('ifconfig '.$new_interface.' '.$to.' netmask '.$this->server_conf['server_netzmaske'].' up &> /dev/null');
J 1129                                $ifconfig['INTERFACE'][$new_interface] = $to;
55da90 1130                           }
B 1131                     }
1132                   }
1133             }
1134           }
1135     }
1136     
1137     function quota_dirs(){
1138       global $app;
3b4c28 1139       $content = $app->file->unix_nl($app->file->no_comments('/etc/fstab'));
55da90 1140       $lines = explode("\n", $content);
B 1141       foreach($lines as $line){
1142         $line = trim($line);
3b4c28 1143         if($line != ''){
55da90 1144           $elms = explode("\t", $line);
B 1145           foreach($elms as $elm){
3b4c28 1146             if(trim($elm) != '') $f[] = $elm;
55da90 1147           }
3b4c28 1148           if(!empty($f) && stristr($f[3], 'userquota') && stristr($f[3], 'groupquota')){
55da90 1149             $q_dirs[] = trim($f[1]);
B 1150           }
1151           unset($f);
1152         }
1153       }
1154       if(!empty($q_dirs)){
1155         return $q_dirs;
1156       } else {
1157         return false;
1158       }
1159     }
1160     
1161     /**
1162      * Scan the trash for virusses infection
1163      *
1164      */
1165     function make_trashscan(){
1166         global $app;
1167           //trashscan erstellen
a9d0b2 1168           // Template Ã–ffnen
55da90 1169           $app->tpl->clear_all();
3b4c28 1170           $app->tpl->define( array(table    => 'trashscan.master'));
55da90 1171     
3b4c28 1172           if(!isset($this->server_conf['virusadmin']) || trim($this->server_conf['virusadmin']) == '') $this->server_conf['virusadmin'] = 'admispconfig@localhost';
J 1173           if(substr($this->server_conf['virusadmin'],0,1) == '#'){
1174             $notify = 'no';
55da90 1175           } else {
3b4c28 1176             $notify = 'yes';
55da90 1177           }
B 1178     
1179           // Variablen zuweisen
3b4c28 1180           $app->tpl->assign( array(VIRUSADMIN => $this->server_conf['virusadmin'],
55da90 1181                                NOTIFICATION => $notify));
B 1182     
1183           $app->tpl->parse(TABLE, table);
1184     
1185           $trashscan_text = $app->tpl->fetch();
1186     
3b4c28 1187           $datei = '/home/admispconfig/ispconfig/tools/clamav/bin/trashscan';
55da90 1188           $app->file->wf($datei, $trashscan_text);
B 1189     
3b4c28 1190           chmod($datei, 0755);
J 1191           chown($datei,'admispconfig');
1192           chgrp($datei,'admispconfig');
55da90 1193     }
B 1194     
1195     /**
1196      * Get the current time
1197      *
1198      */
1199     function get_time(){
3b4c28 1200       $addr = 'http://www.ispconfig.org/';
55da90 1201       $timeout = 1;
B 1202       $url_parts = parse_url($addr);
3b4c28 1203       $path = $url_parts['path'];
55da90 1204       $port = 80;
3b4c28 1205       $urlHandle = @fsockopen($url_parts['host'], $port, $errno, $errstr, $timeout);
55da90 1206       if ($urlHandle){
B 1207         socket_set_timeout($urlHandle, $timeout);
1208     
458767 1209         $urlString = 'GET '.$path." HTTP/1.0\r\nHost: ".$url_parts['host']."\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
3b4c28 1210         if ($user) $urlString .= 'Authorization: Basic '.base64_encode($user.':'.$pass)."\r\n";
55da90 1211         $urlString .= "\r\n";
B 1212         fputs($urlHandle, $urlString);
1213     
458767 1214         $month['Jan'] = '01';
J 1215         $month['Feb'] = '02';
1216         $month['Mar'] = '03';
1217         $month['Apr'] = '04';
1218         $month['May'] = '05';
1219         $month['Jun'] = '06';
1220         $month['Jul'] = '07';
1221         $month['Aug'] = '08';
1222         $month['Sep'] = '09';
1223         $month['Oct'] = '10';
1224         $month['Nov'] = '11';
1225         $month['Dec'] = '12';
55da90 1226         $c = 0;
B 1227         $l = 0;
1228         $startzeit = time();
1229         while(!feof($urlHandle) && $c < 2 && $l == 0){
1230           $line = trim(fgets($urlHandle,128));
1231           $response .= $line;
1232           $c = time() - $startzeit;
3b4c28 1233           if($line == '' || substr($line, 0, 5) == 'Date:') $l += 1; // nur den Header auslesen
J 1234           if(substr($line, 0, 5) == 'Date:'){
1235             $parts = explode(' ', $line);
55da90 1236             $tag = $parts[2];
B 1237             $monat = $month[$parts[3]];
1238             $jahr = $parts[4];
3b4c28 1239             list($stunde, $minute, $sekunde) = explode(':', $parts[5]);
55da90 1240             $timestamp = mktime($stunde,$minute,$sekunde,$monat,$tag,$jahr);
B 1241           }
1242         }
1243     
1244         @fclose($urlHandle);
1245     
1246         return $timestamp;
1247       } else {
1248         @fclose($urlHandle);
1249         return false;
1250       }
1251     }
fdb514 1252     
T 1253     function replaceLine($filename,$search_pattern,$new_line,$strict = 0,$append = 1) {
c77103 1254         global $app;
c3ac0a 1255         if($this->checkpath($filename) == false) {
T 1256             $app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
1257             return false;
1258         }
fdb514 1259         $lines = @file($filename);
T 1260         $out = '';
1261         $found = 0;
1262         if(is_array($lines)) {
1263             foreach($lines as $line) {
1264                 if($strict == 0) {
1265                     if(stristr($line,$search_pattern)) {
1266                         $out .= $new_line."\n";
1267                         $found = 1;
1268                     } else {
1269                         $out .= $line;
1270                     }
1271                 } else {
1272                     if(trim($line) == $search_pattern) {
1273                         $out .= $new_line."\n";
1274                         $found = 1;
1275                     } else {
1276                         $out .= $line;
1277                     }
1278                 }
1279             }
1280         }
1281         
1282         if($found == 0) {
1283             //* add \n if the last line does not end with \n or \r
9f56bd 1284             if(substr($out,-1) != "\n" && substr($out,-1) != "\r" && filesize($filename) > 0) $out .= "\n";
fdb514 1285             //* add the new line at the end of the file
9f56bd 1286             if($append == 1) {
T 1287                 $out .= $new_line."\n";
1288             }
fdb514 1289         }
T 1290         file_put_contents($filename,$out);
1291     }
1292     
1293     function removeLine($filename,$search_pattern,$strict = 0) {
c77103 1294     global $app;
c3ac0a 1295     if($this->checkpath($filename) == false) {
T 1296         $app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
1297         return false;
1298     }
fdb514 1299     if($lines = @file($filename)) {
T 1300         $out = '';
1301         foreach($lines as $line) {
1302             if($strict == 0) {
1303                 if(!stristr($line,$search_pattern)) {
1304                     $out .= $line;
1305                 }
1306             } else {
1307                 if(!trim($line) == $search_pattern) {
1308                     $out .= $line;
1309                 }
1310             }
1311         }
1312         file_put_contents($filename,$out);
1313     }
1314     }
1315     
1316     function maildirmake($maildir_path, $user = '', $subfolder = '') {
1317         
1318         global $app;
1319         
1320         if($subfolder != '') {
d2212d 1321             $dir = escapeshellcmd($maildir_path.'/.'.$subfolder);
fdb514 1322         } else {
d2212d 1323             $dir = escapeshellcmd($maildir_path);
2ebadd 1324         }
fb3a98 1325         
T 1326         if(!is_dir($dir)) mkdir($dir, 0700, true);
2ebadd 1327
J 1328         if($user != '' && $user != 'root' && $this->is_user($user)) {
d2212d 1329             $user = escapeshellcmd($user);
2ebadd 1330             // I assume that the name of the (vmail group) is the same as the name of the mail user in ISPConfig 3
J 1331             $group = $user;
c3ac0a 1332             if(is_dir($dir)) $this->chown($dir,$user);
T 1333             if(is_dir($dir)) $this->chgrp($dir,$group);
2ebadd 1334
J 1335             $chown_mdsub = true;
fdb514 1336         }
T 1337         
2ebadd 1338         $maildirsubs = array('cur','new','tmp');
J 1339
1340         foreach ($maildirsubs as $mdsub) {
9f56bd 1341             if(!is_dir($dir.'/'.$mdsub)) mkdir($dir.'/'.$mdsub, 0700, true);
2ebadd 1342             if ($chown_mdsub) {
J 1343                 chown($dir.'/'.$mdsub, $user);
1344                 chgrp($dir.'/'.$mdsub, $group);
1345             }
1346         }
1347
1348         chmod($dir, 0700);
fdb514 1349         
d2212d 1350         /*
fdb514 1351         if($user != '' && $this->is_user($user) && $user != 'root') {
d2212d 1352             $user = escapeshellcmd($user);
3b4c28 1353             // I assume that the name of the (vmail group) is the same as the name of the mail user in ISPConfig 3
a59ad3 1354             $group = $user;
T 1355             exec("chown $user:$group $dir $dir_cur $dir_new $dir_tmp");
fdb514 1356         }
d2212d 1357         */
fdb514 1358         
T 1359         //* Add the subfolder to the subscriptions and courierimapsubscribed files
1360         if($subfolder != '') {
1361             // Courier
1362             if(!is_file($maildir_path.'/courierimapsubscribed')) {
d2212d 1363                 $tmp_file = escapeshellcmd($maildir_path.'/courierimapsubscribed');
3b4c28 1364                 touch($tmp_file);
J 1365                 chmod($tmp_file, 0744);
1366                 chown($tmp_file,'vmail');
1367                 chgrp($tmp_file,'vmail');
fdb514 1368             }
T 1369             $this->replaceLine($maildir_path.'/courierimapsubscribed','INBOX.'.$subfolder,'INBOX.'.$subfolder,1,1);
1370             
1371             // Dovecot
1372             if(!is_file($maildir_path.'/subscriptions')) {
d2212d 1373                 $tmp_file = escapeshellcmd($maildir_path.'/subscriptions');
3b4c28 1374                 touch($tmp_file);
J 1375                 chmod($tmp_file, 0744);
1376                 chown($tmp_file,'vmail');
1377                 chgrp($tmp_file,'vmail');
fdb514 1378             }
T 1379             $this->replaceLine($maildir_path.'/subscriptions',$subfolder,$subfolder,1,1);
1380         }
1381         
1382         $app->log('Created Maildir '.$maildir_path.' with subfolder: '.$subfolder,LOGLEVEL_DEBUG);
1383         
1384     }
355efb 1385     
T 1386     //* Function to create directory paths and chown them to a user and group
1387     function mkdirpath($path, $mode = 0755, $user = '', $group = '') {
1388         $path_parts = explode('/',$path);
1389         $new_path = '';
1390         if(is_array($path_parts)) {
1391             foreach($path_parts as $part) {
1392                 $new_path .= '/'.$part;
1393                 if(!@is_dir($new_path)) {
c3ac0a 1394                     $this->mkdir($new_path);
T 1395                     $this->chmod($new_path,$mode);
1396                     if($user != '') $this->chown($new_path,$user);
1397                     if($group != '') $this->chgrp($new_path,$group);
355efb 1398                 }
T 1399             }
1400         }
1401         
1402     }
42f191 1403     
T 1404     //* Check if a application is installed
1405     function is_installed($appname) {
1406         exec('which '.escapeshellcmd($appname).' 2> /dev/null',$out,$returncode);
1407         if(isset($out[0]) && stristr($out[0],$appname) && $returncode == 0) {
1408             return true;
1409         } else {
1410             return false;
1411         }
1412     }
4b9329 1413     
T 1414     function web_folder_protection($document_root,$protect) {
1415         global $app,$conf;
1416         
c3ac0a 1417         if($this->checkpath($document_root) == false) {
T 1418             $app->log("Action aborted, target is a symlink: $document_root",LOGLEVEL_DEBUG);
1419             return false;
1420         }
1421         
4b9329 1422         //* load the server configuration options
T 1423         $app->uses('getconf');
1424         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
1425         
1426         if($protect == true && $web_config['web_folder_protection'] == 'y') {
1427             //* Add protection
1428             if($document_root != '' && $document_root != '/' && strlen($document_root) > 6 && !stristr($document_root,'..')) exec('chattr +i '.escapeshellcmd($document_root));
1429         } else {
1430             //* Remove protection
1431             if($document_root != '' && $document_root != '/' && strlen($document_root) > 6 && !stristr($document_root,'..')) exec('chattr -i '.escapeshellcmd($document_root));
1432         }
1433     }
ff6a68 1434     
T 1435     function usermod($username, $uid = 0, $gid = 0, $home = '', $shell = '', $password = '', $login = '') {
1436         global $app;
1437         
1438         if($login == '') $login = $username;
1439         
1440         //* Change values in /etc/passwd
1441         $passwd_file_array = file('/etc/passwd');
1442         if(is_array($passwd_file_array)) {
1443             foreach($passwd_file_array as $line) {
1444                 $line = trim($line);
1445                 $parts = explode(':',$line);
1446                 if($parts[0] == $username) {
1447                     if(trim($login) != '' && trim($login) != trim($username)) $parts[0] = trim($login);
1448                     if(!empty($uid)) $parts[2] = trim($uid);
1449                     if(!empty($gid)) $parts[3] = trim($gid);
1450                     if(trim($home) != '') $parts[5] = trim($home);
1451                     if(trim($shell) != '') $parts[6] = trim($shell);
1452                     $new_line = implode(':',$parts);
1453                     copy('/etc/passwd','/etc/passwd~');
1454                     chmod('/etc/passwd~',0600);
1455                     $app->uses('system');
1456                     $app->system->replaceLine('/etc/passwd',$line,$new_line,1,0);
1457                 }
1458             }
1459             unset($passwd_file_array);
1460         }
1461         
1462         //* If username != login, change username in group and gshadow file
1463         if($username  != $login) {
1464             $group_file_array = file('/etc/group');
1465             if(is_array($group_file_array)) {
1466                 foreach($group_file_array as $line) {
1467                     $line = trim($line);
1468                     $parts = explode(':',$line);
1469                     if(strstr($parts[3],$username)) {
1470                         $uparts = explode(',',$parts[3]);
1471                         if(is_array($uparts)) {
1472                             foreach($uparts as $key => $val) {
1473                                 if($val == $username) $uparts[$key] = $login;
1474                             }
1475                         }
1476                         $parts[3] = implode(',',$uparts);
1477                         $new_line = implode(':',$parts);
1478                         copy('/etc/group','/etc/group~');
1479                         chmod('/etc/group~',0600);
1480                         $app->system->replaceLine('/etc/group',$line,$new_line,1,0);
1481                     }
1482                 }
1483             }
1484             unset($group_file_array);
1485             
1486             $gshadow_file_array = file('/etc/gshadow');
1487             if(is_array($gshadow_file_array)) {
1488                 foreach($gshadow_file_array as $line) {
1489                     $line = trim($line);
1490                     $parts = explode(':',$line);
1491                     if(strstr($parts[3],$username)) {
1492                         $uparts = explode(',',$parts[3]);
1493                         if(is_array($uparts)) {
1494                             foreach($uparts as $key => $val) {
1495                                 if($val == $username) $uparts[$key] = $login;
1496                             }
1497                         }
1498                         $parts[3] = implode(',',$uparts);
1499                         $new_line = implode(':',$parts);
1500                         copy('/etc/gshadow','/etc/gshadow~');
1501                         chmod('/etc/gshadow~',0600);
1502                         $app->system->replaceLine('/etc/gshadow',$line,$new_line,1,0);
1503                     }
1504                 }
1505             }
1506             unset($group_file_array);
1507         }
1508         
1509         
1510         //* When password or login name has been changed
1511         if($password != '' || $username  != $login) {
1512             $shadow_file_array = file('/etc/shadow');
1513             if(is_array($shadow_file_array)) {
1514                 foreach($shadow_file_array as $line) {
1515                     $line = trim($line);
1516                     $parts = explode(':',$line);
1517                     if($parts[0] == $username) {
1518                         if(trim($login) != '' && trim($login) != trim($username)) $parts[0] = trim($login);
1519                         if(trim($password) != '') $parts[1] = trim($password);
1520                         $new_line = implode(':',$parts);
1521                         copy('/etc/shadow','/etc/shadow~');
1522                         chmod('/etc/shadow~',0600);
1523                         $app->system->replaceLine('/etc/shadow',$line,$new_line,1,0);
1524                     }
1525                 }
1526             }
1527             unset($shadow_file_array);
1528         }
1529     }
bfcdef 1530     
T 1531     function intval($string, $force_numeric = false) {
1532         if(intval($string) == 2147483647) {
1533             if($force_numeric == true) return floatval($string);
1534             elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2];
1535             else return 0;
1536         } else {
1537             return intval($string);
1538         }
1539     }
526b99 1540     
T 1541     function is_mounted($mountpoint){
1542         $cmd = 'df 2>/dev/null | grep " '.$mountpoint.'$"';
1543         exec($cmd, $output, $return_var);
1544         return $return_var == 0 ? true : false; 
1545     }
e2d6ed 1546
T 1547 }
3b4c28 1548 ?>