tbrehm
2009-09-03 5421462b39a5b41014569af59d3861b9ffe8a44f
commit | author | age
24854c 1 <?php
T 2
3 /*
4 Copyright (c) 2007 - 2009, 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
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 apache2_plugin {
32     
33     var $plugin_name = 'apache2_plugin';
34     var $class_name = 'apache2_plugin';
35     
36     // private variables
37     var $action = '';
38     
39     //* This function is called during ispconfig installation to determine
40     //  if a symlink shall be created for this plugin.
41     function onInstall() {
42         global $conf;
43         
44         if($conf['services']['web'] == true) {
45             return true;
46         } else {
47             return false;
48         }
49         
50     }
51     
52         
53     /*
54          This function is called when the plugin is loaded
55     */
56     
57     function onLoad() {
58         global $app;
59         
60         /*
61         Register for the events
62         */
63         
64         
65         
66         $app->plugins->registerEvent('web_domain_insert',$this->plugin_name,'ssl');
67         $app->plugins->registerEvent('web_domain_update',$this->plugin_name,'ssl');
68         $app->plugins->registerEvent('web_domain_delete',$this->plugin_name,'ssl');
69         
70         $app->plugins->registerEvent('web_domain_insert',$this->plugin_name,'insert');
71         $app->plugins->registerEvent('web_domain_update',$this->plugin_name,'update');
72         $app->plugins->registerEvent('web_domain_delete',$this->plugin_name,'delete');
73         
74         $app->plugins->registerEvent('server_ip_insert',$this->plugin_name,'server_ip');
75         $app->plugins->registerEvent('server_ip_update',$this->plugin_name,'server_ip');
76         $app->plugins->registerEvent('server_ip_delete',$this->plugin_name,'server_ip');
77         
78     }
79     
80     // Handle the creation of SSL certificates
81     function ssl($event_name,$data) {
82         global $app, $conf;
83         
84         if(!is_dir($data["new"]["document_root"]."/ssl")) exec("mkdir -p ".$data["new"]["document_root"]."/ssl");
85         $ssl_dir = $data["new"]["document_root"]."/ssl";
86         $domain = $data["new"]["domain"];
87         $key_file = $ssl_dir.'/'.$domain.".key.org";
88           $key_file2 = $ssl_dir.'/'.$domain.".key";
89           $csr_file = $ssl_dir.'/'.$domain.".csr";
90           $crt_file = $ssl_dir.'/'.$domain.".crt";
91         
92         //* Create a SSL Certificate
93         if($data["new"]["ssl_action"] == 'create') {
94             $rand_file = $ssl_dir."/random_file";
95             $rand_data = md5(uniqid(microtime(),1));
96             for($i=0; $i<1000; $i++){
97                 $rand_data .= md5(uniqid(microtime(),1));
98                 $rand_data .= md5(uniqid(microtime(),1));
99                 $rand_data .= md5(uniqid(microtime(),1));
100                 $rand_data .= md5(uniqid(microtime(),1));
101             }
102             file_put_contents($rand_file, $rand_data);
103
104             $ssl_password = substr(md5(uniqid(microtime(),1)), 0, 15);
105             
106             $ssl_cnf = "        RANDFILE               = $rand_file
107
108         [ req ]
109         default_bits           = 1024
110         default_keyfile        = keyfile.pem
111         distinguished_name     = req_distinguished_name
112         attributes             = req_attributes
113         prompt                 = no
114         output_password        = $ssl_password
115
116         [ req_distinguished_name ]
117         C                      = ".$data['new']['ssl_country']."
118         ST                     = ".$data['new']['ssl_state']."
119         L                      = ".$data['new']['ssl_locality']."
120         O                      = ".$data['new']['ssl_organisation']."
121         OU                     = ".$data['new']['ssl_organisation_unit']."
122         CN                     = $domain
123         emailAddress           = webmaster@".$data['new']['domain']."
124
125         [ req_attributes ]
126         challengePassword              = A challenge password";
127             
128             $ssl_cnf_file = $ssl_dir."/openssl.conf";
129             file_put_contents($ssl_cnf_file,$ssl_cnf);
130             
131             $rand_file = escapeshellcmd($rand_file);
132             $key_file = escapeshellcmd($key_file);
133             $key_file2 = escapeshellcmd($key_file2);
134             $ssl_days = 3650;
135             $csr_file = escapeshellcmd($csr_file);
136             $config_file = escapeshellcmd($ssl_cnf_file);
137             $crt_file = escapeshellcmd($crt_file);
138
139             if(is_file($ssl_cnf_file)){
140                   exec("openssl genrsa -des3 -rand $rand_file -passout pass:$ssl_password -out $key_file 1024 && openssl req -new -passin pass:$ssl_password -passout pass:$ssl_password -key $key_file -out $csr_file -days $ssl_days -config $config_file && openssl req -x509 -passin pass:$ssl_password -passout pass:$ssl_password -key $key_file -in $csr_file -out $crt_file -days $ssl_days -config $config_file && openssl rsa -passin pass:$ssl_password -in $key_file -out $key_file2");
141                 $app->log("Creating SSL Cert for: $domain",LOGLEVEL_DEBUG);
142             }
143
144             exec("chmod 400 $key_file2");
145             @unlink($config_file);
146             @unlink($rand_file);
147             $ssl_request = file_get_contents($csr_file);
148             $ssl_cert = file_get_contents($crt_file);
8c4aa3 149             /* Update the DB of the (local) Server */
24854c 150             $app->db->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert' WHERE domain = '".$data["new"]["domain"]."'");
T 151             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
8c4aa3 152             /* Update also the master-DB of the Server-Farm */
T 153             $app->dbmaster->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert' WHERE domain = '".$data["new"]["domain"]."'");
154             $app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
24854c 155         }
T 156         
157         //* Save a SSL certificate to disk
158         if($data["new"]["ssl_action"] == 'save') {
159             $ssl_dir = $data["new"]["document_root"]."/ssl";
160             $domain = $data["new"]["domain"];
161               $csr_file = $ssl_dir.'/'.$domain.".csr";
162               $crt_file = $ssl_dir.'/'.$domain.".crt";
163             $bundle_file = $ssl_dir.'/'.$domain.".bundle";
164             file_put_contents($csr_file,$data["new"]["ssl_request"]);
165             file_put_contents($crt_file,$data["new"]["ssl_cert"]);
166             if(trim($data["new"]["ssl_bundle"]) != '') file_put_contents($bundle_file,$data["new"]["ssl_bundle"]);
8c4aa3 167             /* Update the DB of the (local) Server */
24854c 168             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
8c4aa3 169             /* Update also the master-DB of the Server-Farm */
T 170             $app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
24854c 171             $app->log("Saving SSL Cert for: $domain",LOGLEVEL_DEBUG);
T 172         }
173         
174         //* Delete a SSL certificate
175         if($data["new"]["ssl_action"] == 'del') {
176             $ssl_dir = $data["new"]["document_root"]."/ssl";
177             $domain = $data["new"]["domain"];
178               $csr_file = $ssl_dir.'/'.$domain.".csr";
179               $crt_file = $ssl_dir.'/'.$domain.".crt";
180             $bundle_file = $ssl_dir.'/'.$domain.".bundle";
181             unlink($csr_file);
182             unlink($crt_file);
183             unlink($bundle_file);
8c4aa3 184             /* Update the DB of the (local) Server */
T 185             $app->db->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '' WHERE domain = '".$data["new"]["domain"]."'");
24854c 186             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
8c4aa3 187             /* Update also the master-DB of the Server-Farm */
T 188             $app->dbmaster->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '' WHERE domain = '".$data["new"]["domain"]."'");
189             $app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
24854c 190             $app->log("Deleting SSL Cert for: $domain",LOGLEVEL_DEBUG);
T 191         }
192         
193         
194     }
195     
196     
197     function insert($event_name,$data) {
198         global $app, $conf;
199         
200         $this->action = 'insert';
201         // just run the update function
202         $this->update($event_name,$data);
203         
204         
205     }
206     
207     
208     function update($event_name,$data) {
209         global $app, $conf;
210         
211         if($this->action != 'insert') $this->action = 'update';
212         
213         if($data["new"]["type"] != "vhost" && $data["new"]["parent_domain_id"] > 0) {
214             
215             $old_parent_domain_id = intval($data["old"]["parent_domain_id"]);
216             $new_parent_domain_id = intval($data["new"]["parent_domain_id"]);
217             
218             // If the parent_domain_id has been chenged, we will have to update the old site as well.
219             if($this->action == 'update' && $data["new"]["parent_domain_id"] != $data["old"]["parent_domain_id"]) {
220                 $tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$old_parent_domain_id." AND active = 'y'");
221                 $data["new"] = $tmp;
222                 $data["old"] = $tmp;
223                 $this->action = 'update';
224                 $this->update($event_name,$data);
225             }
226             
227             // This is not a vhost, so we need to update the parent record instead.
228             $tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$new_parent_domain_id." AND active = 'y'");
229             $data["new"] = $tmp;
230             $data["old"] = $tmp;
231             $this->action = 'update';
232         }
233         
234         // load the server configuration options
235         $app->uses("getconf");
236         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
237         
542146 238         //* Check if this is a chrooted setup
T 239         if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
240             $apache_chrooted = true;
241             $app->log("Info: Apache is chrooted.",LOGLEVEL_DEBUG);
242         } else {
243             $apache_chrooted = false;
244         }
245         
24854c 246         if($data["new"]["document_root"] == '') {
T 247             $app->log("document_root not set",LOGLEVEL_WARN);
248             return 0;
249         }
250         if($data["new"]["system_user"] == 'root' or $data["new"]["system_group"] == 'root') {
251             $app->log("Websites can not be owned by the root user or group.",LOGLEVEL_WARN);
252             return 0;
253         }
254         
255         //* If the client of the site has been changed, we have a change of the document root
256         if($this->action == 'update' && $data["new"]["document_root"] != $data["old"]["document_root"]) {
257             
258             //* Get the old client ID
259             $old_client = $app->dbmaster->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".intval($data["old"]["sys_groupid"]));
260             $old_client_id = intval($old_client["client_id"]);
261             unset($old_client);
262             
263             //* Remove the old symlinks
264             $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
265             if(is_array($tmp_symlinks_array)) {
266                 foreach($tmp_symlinks_array as $tmp_symlink) {
267                     $tmp_symlink = str_replace("[client_id]",$old_client_id,$tmp_symlink);
268                     $tmp_symlink = str_replace("[website_domain]",$data["old"]["domain"],$tmp_symlink);
269                     // Remove trailing slash
270                     if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
271                     // create the symlinks, if not exist
8c4aa3 272                     if(is_link($tmp_symlink)) {
24854c 273                         exec("rm -f ".escapeshellcmd($tmp_symlink));
T 274                         $app->log("Removed Symlink: rm -f ".$tmp_symlink,LOGLEVEL_DEBUG);
275                     }
276                 }
277             }
278             
279             //* Move the site data
280             $tmp_docroot = explode('/',$data["new"]["document_root"]);
281             unset($tmp_docroot[count($tmp_docroot)-1]);
282             $new_dir = implode('/',$tmp_docroot);
283             
284             $tmp_docroot = explode('/',$data["old"]["document_root"]);
285             unset($tmp_docroot[count($tmp_docroot)-1]);
286             $old_dir = implode('/',$tmp_docroot);
287             
288             exec('rm -rf '.$data["new"]["document_root"]);
289             if(!is_dir($new_dir)) exec('mkdir -p '.$new_dir);
290             exec('mv '.$data["old"]["document_root"].' '.$new_dir);
291             $app->log("Moving site to new document root: ".'mv '.$data["old"]["document_root"].' '.$new_dir,LOGLEVEL_DEBUG);
292             
68e917 293             //* Change the owner of the website files to the new website owner
T 294             exec('chown --recursive --from='.escapeshellcmd($data["old"]["system_user"]).':'.escapeshellcmd($data['old']['system_group']).' '.escapeshellcmd($data["new"]["system_user"]).':'.escapeshellcmd($data['new']['system_group']).' '.$new_dir);
295             
24854c 296             //* Change the home directory and group of the website user
T 297             $command = 'usermod';
298             $command .= ' --home '.escapeshellcmd($data["new"]["document_root"]);
299             $command .= ' --gid '.escapeshellcmd($data['new']['system_group']);
300             $command .= ' '.escapeshellcmd($data["new"]["system_user"]);
301             exec($command);
68e917 302             
542146 303             if($apache_chrooted) $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." ".$command);
68e917 304             
24854c 305             
T 306         }
307         
308         //print_r($data);
309         
310         // Check if the directories are there and create them if nescessary.
311         if(!is_dir($data["new"]["document_root"]."/web")) exec("mkdir -p ".$data["new"]["document_root"]."/web");
312         if(!is_dir($data["new"]["document_root"]."/web/error") and $data["new"]["errordocs"]) exec("mkdir -p ".$data["new"]["document_root"]."/web/error");
313         //if(!is_dir($data["new"]["document_root"]."/log")) exec("mkdir -p ".$data["new"]["document_root"]."/log");
314         if(!is_dir($data["new"]["document_root"]."/ssl")) exec("mkdir -p ".$data["new"]["document_root"]."/ssl");
315         if(!is_dir($data["new"]["document_root"]."/cgi-bin")) exec("mkdir -p ".$data["new"]["document_root"]."/cgi-bin");
316         if(!is_dir($data["new"]["document_root"]."/tmp")) exec("mkdir -p ".$data["new"]["document_root"]."/tmp");
317         
318         // Remove the symlink for the site, if site is renamed
319         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
320             if(is_dir('/var/log/ispconfig/httpd/'.$data["old"]["domain"])) exec('rm -rf /var/log/ispconfig/httpd/'.$data["old"]["domain"]);
321             if(is_link($data["old"]["document_root"]."/log")) unlink($data["old"]["document_root"]."/log");
322         }
323         
324         // Create the symlink for the logfiles
325         if(!is_dir('/var/log/ispconfig/httpd/'.$data["new"]["domain"])) exec('mkdir -p /var/log/ispconfig/httpd/'.$data["new"]["domain"]);
326         if(!is_link($data["new"]["document_root"]."/log")) {
327             exec("ln -s /var/log/ispconfig/httpd/".$data["new"]["domain"]." ".$data["new"]["document_root"]."/log");
328             $app->log("Creating Symlink: ln -s /var/log/ispconfig/httpd/".$data["new"]["domain"]." ".$data["new"]["document_root"]."/log",LOGLEVEL_DEBUG);
329         }
330         /*
331         // Create the symlink for the logfiles
332         // This does not work as vlogger can not log trogh symlinks.
333         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
334             if(is_dir($data["old"]["document_root"]."/log")) exec('rm -rf '.$data["old"]["document_root"]."/log");
335             if(is_link('/var/log/ispconfig/httpd/'.$data["old"]["domain"])) unlink('/var/log/ispconfig/httpd/'.$data["old"]["domain"]);
336         }
337         
338         // Create the symlink for the logfiles
339         if(!is_dir($data["new"]["document_root"]."/log")) exec('mkdir -p '.$data["new"]["document_root"]."/log");
340         if(!is_link('/var/log/ispconfig/httpd/'.$data["new"]["domain"])) {
341             exec("ln -s ".$data["new"]["document_root"]."/log /var/log/ispconfig/httpd/".$data["new"]["domain"]);
342             $app->log("Creating Symlink: ln -s ".$data["new"]["document_root"]."/log /var/log/ispconfig/httpd/".$data["new"]["domain"],LOGLEVEL_DEBUG);
343         }
344         */
345     
346         // Get the client ID
347         $client = $app->dbmaster->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".intval($data["new"]["sys_groupid"]));
348         $client_id = intval($client["client_id"]);
349         unset($client);
350         
351         // Remove old symlinks, if site is renamed
352         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
353             $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
354             if(is_array($tmp_symlinks_array)) {
355                 foreach($tmp_symlinks_array as $tmp_symlink) {
356                     $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
357                     $tmp_symlink = str_replace("[website_domain]",$data["old"]["domain"],$tmp_symlink);
358                     // Remove trailing slash
359                     if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
360                     // remove the symlinks, if not exist
361                     if(is_link($tmp_symlink)) {
362                         exec("rm -f ".escapeshellcmd($tmp_symlink));
363                         $app->log("Removed Symlink: rm -f ".$tmp_symlink,LOGLEVEL_DEBUG);
364                     }
365                 }
366             }
367         }
368         
369         // Create the symlinks for the sites
370         $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
371         if(is_array($tmp_symlinks_array)) {
372             foreach($tmp_symlinks_array as $tmp_symlink) {
373                 $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
374                 $tmp_symlink = str_replace("[website_domain]",$data["new"]["domain"],$tmp_symlink);
375                 // Remove trailing slash
376                 if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
377                 //* Remove symlink if target folder has been changed.
378                 if($data["old"]["document_root"] != '' && $data["old"]["document_root"] != $data["new"]["document_root"] && is_link($tmp_symlink)) {
379                     unlink($tmp_symlink);
380                 }
381                 // create the symlinks, if not exist
382                 if(!is_link($tmp_symlink)) {
383                     exec("ln -s ".escapeshellcmd($data["new"]["document_root"])."/ ".escapeshellcmd($tmp_symlink));
384                     $app->log("Creating Symlink: ln -s ".$data["new"]["document_root"]."/ ".$tmp_symlink,LOGLEVEL_DEBUG);
385                 }
386             }
387         }
388         
389         
390         if($this->action == 'insert' && $data["new"]["type"] == 'vhost') {
391             // Copy the error pages
392             if($data["new"]["errordocs"]){
393                 $error_page_path = escapeshellcmd($data["new"]["document_root"])."/web/error/";
394                 if (file_exists("/usr/local/ispconfig/server/conf-custom/error/".substr(escapeshellcmd($conf["language"]),0,2))){
395                     exec("cp /usr/local/ispconfig/server/conf-custom/error/".substr(escapeshellcmd($conf["language"]),0,2)."/* ".$error_page_path);
396                 }
397                 else {
398                     if (file_exists("/usr/local/ispconfig/server/conf-custom/error/400.html")){
399                         exec("cp /usr/local/ispconfig/server/conf-custom/error/*.html ".$error_page_path);
400                     }
401                     else {
402                         exec("cp /usr/local/ispconfig/server/conf/error/".substr(escapeshellcmd($conf["language"]),0,2)."/* ".$error_page_path);
403                     }
404                 }
405                 exec("chmod -R a+r ".$error_page_path);
406             }
407
408             // copy the standard index page
409             if (file_exists("/usr/local/ispconfig/server/conf-custom/index/standard_index.html_".substr(escapeshellcmd($conf["language"]),0,2))){
410                 exec("cp /usr/local/ispconfig/server/conf-custom/index/standard_index.html_".substr(escapeshellcmd($conf["language"]),0,2)." ".escapeshellcmd($data["new"]["document_root"])."/web/index.html");
411             }
412             else {
413                 if (file_exists("/usr/local/ispconfig/server/conf-custom/index/standard_index.html")){
414                     exec("cp /usr/local/ispconfig/server/conf-custom/index/standard_index.html ".escapeshellcmd($data["new"]["document_root"])."/web/index.html");
415                 }
416                 else {
417                     exec("cp /usr/local/ispconfig/server/conf/index/standard_index.html_".substr(escapeshellcmd($conf["language"]),0,2)." ".escapeshellcmd($data["new"]["document_root"])."/web/index.html");
418                     exec("cp /usr/local/ispconfig/server/conf/index/favicon.ico ".escapeshellcmd($data["new"]["document_root"])."/web/");
419                     exec("cp /usr/local/ispconfig/server/conf/index/robots.txt ".escapeshellcmd($data["new"]["document_root"])."/web/");
420                     exec("cp /usr/local/ispconfig/server/conf/index/.htaccess ".escapeshellcmd($data["new"]["document_root"])."/web/");
421                 }
422             }
423             exec("chmod -R a+r ".escapeshellcmd($data["new"]["document_root"])."/web/");
424         
425         //** Copy the error documents on update when the error document checkbox has been activated and was deactivated before
426         } elseif ($this->action == 'update' && $data["new"]["type"] == 'vhost' && $data["old"]["errordocs"] == 0 && $data["new"]["errordocs"] == 1) {
427             
428             $error_page_path = escapeshellcmd($data["new"]["document_root"])."/web/error/";
429             if (file_exists("/usr/local/ispconfig/server/conf-custom/error/".substr(escapeshellcmd($conf["language"]),0,2))){
430                 exec("cp /usr/local/ispconfig/server/conf-custom/error/".substr(escapeshellcmd($conf["language"]),0,2)."/* ".$error_page_path);
431             }
432             else {
433                 if (file_exists("/usr/local/ispconfig/server/conf-custom/error/400.html")){
434                     exec("cp /usr/local/ispconfig/server/conf-custom/error/*.html ".$error_page_path);
435                 }
436                 else {
437                     exec("cp /usr/local/ispconfig/server/conf/error/".substr(escapeshellcmd($conf["language"]),0,2)."/* ".$error_page_path);
438                 }
439             }
440             exec("chmod -R a+r ".$error_page_path);
441         }  // end copy error docs
442         
443         // Create group and user, if not exist
444         $app->uses("system");
445         
446         $groupname = escapeshellcmd($data["new"]["system_group"]);
447         if($data["new"]["system_group"] != '' && !$app->system->is_group($data["new"]["system_group"])) {
448             exec("groupadd $groupname");
542146 449             if($apache_chrooted) $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." groupadd $groupname");
24854c 450             $app->log("Adding the group: $groupname",LOGLEVEL_DEBUG);
T 451         }
452         
453         $username = escapeshellcmd($data["new"]["system_user"]);
454         if($data["new"]["system_user"] != '' && !$app->system->is_user($data["new"]["system_user"])) {
8c4aa3 455             exec("useradd -d ".escapeshellcmd($data["new"]["document_root"])." -g $groupname -G sshusers $username -s /bin/false");
542146 456             if($apache_chrooted) $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." "."useradd -d ".escapeshellcmd($data["new"]["document_root"])." -g $groupname -G sshusers $username -s /bin/false");
24854c 457             $app->log("Adding the user: $username",LOGLEVEL_DEBUG);
T 458         }
459         
460         // Set the quota for the user
461         if($username != '' && $app->system->is_user($username)) {
462             if($data["new"]["hd_quota"] > 0){
463                 $blocks_soft = $data["new"]["hd_quota"] * 1024;
464                 $blocks_hard = $blocks_soft + 1024;
465               } else {
466                 $blocks_soft = $blocks_hard = 0;
467               }
468             exec("setquota -u $username $blocks_soft $blocks_hard 0 0 -a &> /dev/null");
469             exec("setquota -T -u $username 604800 604800 -a &> /dev/null");
470         }
471         
472         if($this->action == 'insert') {
473             // Chown and chmod the directories below the document root
69944a 474             $this->_exec("chown -R $username:$groupname ".escapeshellcmd($data["new"]["document_root"]));
T 475             // The document root itself has to be owned by root in normal level and by the web owner in security level 20
476             if($web_config['security_level'] == 20) {
477                 $this->_exec("chown $username:$groupname ".escapeshellcmd($data["new"]["document_root"]));
478             } else {
479                 $this->_exec("chown root:root ".escapeshellcmd($data["new"]["document_root"]));
480             }
24854c 481         }
T 482         
8c4aa3 483         
T 484         
69944a 485         //* If the security level is set to high
8c4aa3 486         if($web_config['security_level'] == 20) {
T 487             
69944a 488             $this->_exec("chmod 751 ".escapeshellcmd($data["new"]["document_root"]."/"));
T 489             $this->_exec("chmod 751 ".escapeshellcmd($data["new"]["document_root"])."/*");
490             $this->_exec("chmod 710 ".escapeshellcmd($data["new"]["document_root"]."/web"));
8c4aa3 491             
T 492             // make temp direcory writable for the apache user and the website user
69944a 493             $this->_exec("chmod 777 ".escapeshellcmd($data["new"]["document_root"]."/tmp"));
8c4aa3 494             
T 495             $command = 'usermod';
496             $command .= ' --groups sshusers';
497             $command .= ' '.escapeshellcmd($data["new"]["system_user"]);
69944a 498             $this->_exec($command);
542146 499             
T 500             //* if we have a chrooted apache enviroment
501             if($apache_chrooted) {
502                 $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." ".$command);
503                 
504                 //* add the apache user to the client group in the chroot enviroment
505                 $tmp_groupfile = $app->system->server_conf["group_datei"];
506                 $app->system->server_conf["group_datei"] = $web_config['website_basedir'].'/etc/group';
507                 $app->system->add_user_to_group($groupname, escapeshellcmd($web_config['user']));
508                 $app->system->server_conf["group_datei"] = $tmp_groupfile;
509                 unset($tmp_groupfile);
510             }
8c4aa3 511             
T 512             //* add the apache user to the client group
513             $app->system->add_user_to_group($groupname, escapeshellcmd($web_config['user']));
514             
69944a 515             $this->_exec("chown $username:$groupname ".escapeshellcmd($data["new"]["document_root"]));
T 516             
517             /*
518             * Workaround for jailkit: If jailkit is enabled for the site, the 
519             * website root has to be owned by the root user and we have to chmod it to 755 then
520             */
521             
522             //* Check if there is a jailkit user for this site
523             $tmp = $app->db->queryOneRecord("SELECT count(shell_user_id) as number FROM shell_user WHERE parent_domain_id = ".$data["new"]["domain_id"]." AND chroot = 'jailkit'");
524             if($tmp['number'] > 0) {
525                 $this->_exec("chmod 755 ".escapeshellcmd($data["new"]["document_root"]."/"));
526                 $this->_exec("chown root:root ".escapeshellcmd($data["new"]["document_root"]."/"));
527             }
528             unset($tmp);
529             
8c4aa3 530         // If the security Level is set to medium
T 531         } else {
532         
69944a 533             $this->_exec("chmod 755 ".escapeshellcmd($data["new"]["document_root"]."/"));
T 534             $this->_exec("chmod 755 ".escapeshellcmd($data["new"]["document_root"]."/*"));
535             $this->_exec("chown root:root ".escapeshellcmd($data["new"]["document_root"]."/"));
8c4aa3 536         
T 537             // make temp direcory writable for the apache user and the website user
69944a 538             $this->_exec("chmod 777 ".escapeshellcmd($data["new"]["document_root"]."/tmp"));
8c4aa3 539         }
24854c 540         
T 541         
542         // Create the vhost config file
543         $app->load('tpl');
544         
545         $tpl = new tpl();
546         $tpl->newTemplate("vhost.conf.master");
547         
548         $vhost_data = $data["new"];
549         $vhost_data["web_document_root"] = $data["new"]["document_root"]."/web";
550         $vhost_data["web_document_root_www"] = $web_config["website_basedir"]."/".$data["new"]["domain"]."/web";
551         $vhost_data["web_basedir"] = $web_config["website_basedir"];
8c4aa3 552         $vhost_data["security_level"] = $web_config["security_level"];
24854c 553         
T 554         // Check if a SSL cert exists
555         $ssl_dir = $data["new"]["document_root"]."/ssl";
556         $domain = $data["new"]["domain"];
557           $key_file = $ssl_dir.'/'.$domain.".key";
558           $crt_file = $ssl_dir.'/'.$domain.".crt";
559         $bundle_file = $ssl_dir.'/'.$domain.".bundle";
560         
561         if($data["new"]["ssl"] == 'y' && @is_file($crt_file) && @is_file($key_file)) {
562             $vhost_data["ssl_enabled"] = 1;
563             $app->log("Enable SSL for: $domain",LOGLEVEL_DEBUG);
564         } else {
565             $vhost_data["ssl_enabled"] = 0;
566             $app->log("Disable SSL for: $domain",LOGLEVEL_DEBUG);
567         }
568         
569         if(@is_file($bundle_file)) $vhost_data['has_bundle_cert'] = 1;
570         
571         //$vhost_data["document_root"] = $data["new"]["document_root"]."/web";
572         $tpl->setVar($vhost_data);
573         
574         // Rewrite rules
575         $rewrite_rules = array();
576         if($data["new"]["redirect_type"] != '') {
8c4aa3 577             if(substr($data["new"]["redirect_path"],-1) != '/') $data["new"]["redirect_path"] .= '/';
24854c 578             $rewrite_rules[] = array(    'rewrite_domain'     => $data["new"]["domain"],
T 579                                         'rewrite_type'         => ($data["new"]["redirect_type"] == 'no')?'':'['.$data["new"]["redirect_type"].']',
580                                         'rewrite_target'     => $data["new"]["redirect_path"]);
581             
582             switch($data["new"]["subdomain"]) {
583             case 'www':
584                 $rewrite_rules[] = array(    'rewrite_domain'     => 'www.'.$data["new"]["domain"],
585                                             'rewrite_type'         => ($data["new"]["redirect_type"] == 'no')?'':'['.$data["new"]["redirect_type"].']',
586                                             'rewrite_target'     => $data["new"]["redirect_path"]);
587                 break;
588             case '*':
589                 // TODO
590                 //$rewrite_rules[] = array(    'rewrite_domain'     => '*'.$alias["domain"],
591                 //                            'rewrite_type'         => $alias["redirect_type"],
592                 //                            'rewrite_target'     => $alias["redirect_path"]);
593                 break;
594             }
595         }
596         
597         // get alias domains (co-domains and subdomains)
598         $aliases = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ".$data["new"]["domain_id"]." AND active = 'y'");
8c4aa3 599         $server_alias = array();
T 600         switch($data["new"]["subdomain"]) {
24854c 601         case 'www':
8c4aa3 602             $server_alias[] .= 'www.'.$data["new"]["domain"].' ';
24854c 603             break;
T 604         case '*':
8c4aa3 605             $server_alias[] .= '*.'.$data["new"]["domain"].' ';    
24854c 606             break;
T 607         }
608         if(is_array($aliases)) {
609             foreach($aliases as $alias) {
610                 switch($alias["subdomain"]) {
611                 case 'www':
8c4aa3 612                     $server_alias[] .= 'www.'.$alias["domain"].' '.$alias["domain"].' ';
24854c 613                     break;
T 614                 case '*':
8c4aa3 615                     $server_alias[] .= '*.'.$alias["domain"].' '.$alias["domain"].' ';    
24854c 616                     break;
T 617                 default:
8c4aa3 618                     $server_alias[] .= $alias["domain"].' ';            
24854c 619                     break;
T 620                 }
621                 $app->log("Add server alias: $alias[domain]",LOGLEVEL_DEBUG);
622                 // Rewriting
623                 if($alias["redirect_type"] != '') {
8c4aa3 624                     if(substr($data["new"]["redirect_path"],-1) != '/') $data["new"]["redirect_path"] .= '/';
24854c 625                     $rewrite_rules[] = array(    'rewrite_domain'     => $alias["domain"],
T 626                                                 'rewrite_type'         => ($alias["redirect_type"] == 'no')?'':'['.$alias["redirect_type"].']',
627                                                 'rewrite_target'     => $alias["redirect_path"]);
628                     switch($alias["subdomain"]) {
629                     case 'www':
630                         $rewrite_rules[] = array(    'rewrite_domain'     => 'www.'.$alias["domain"],
631                                                     'rewrite_type'         => ($alias["redirect_type"] == 'no')?'':'['.$alias["redirect_type"].']',
632                                                     'rewrite_target'     => $alias["redirect_path"]);
633                         break;
634                     case '*':
635                         // TODO
636                         //$rewrite_rules[] = array(    'rewrite_domain'     => '*'.$alias["domain"],
637                         //                            'rewrite_type'         => $alias["redirect_type"],
638                         //                            'rewrite_target'     => $alias["redirect_path"]);
639                         break;
640                     }
641                 }
642             }
643         }
8c4aa3 644         
T 645         //* If we have some alias records
646         if(count($server_alias) > 0) {
647             $server_alias_str = '';
648             $n = 0;
649             
650             // begin a new ServerAlias line after 30 alias domains
651             foreach($server_alias as $tmp_alias) {
652                 if($n % 30 == 0) $server_alias_str .= "\n    ServerAlias ";
653                 $server_alias_str .= $tmp_alias;
654             }
655             unset($tmp_alias);
656             
657             $tpl->setVar('alias',trim($server_alias_str));
658         } else {
659             $tpl->setVar('alias','');
660         }
661         
24854c 662         if(count($rewrite_rules) > 0) {
T 663             $tpl->setVar('rewrite_enabled',1);
664         } else {
665             $tpl->setVar('rewrite_enabled',0);
666         }
667         $tpl->setLoop('redirects',$rewrite_rules);
668         
669         /** 
670          * install fast-cgi starter script and add script aliasd config 
671          * first we create the script directory if not already created, then copy over the starter script
672          * settings are copied over from the server ini config for now
673          * TODO: Create form for fastcgi configs per site.
674          */
675         
676         if ($data["new"]["php"] == "fast-cgi")
677         {
678             $fastcgi_config = $app->getconf->get_server_config($conf["server_id"], 'fastcgi');
679             
680             $fastcgi_starter_path = str_replace("[system_user]",$data["new"]["system_user"],$fastcgi_config["fastcgi_starter_path"]);
681             $fastcgi_starter_path = str_replace("[client_id]",$client_id,$fastcgi_starter_path);
682             
683             if (!is_dir($fastcgi_starter_path))
684             {
685                 exec("mkdir -p ".escapeshellcmd($fastcgi_starter_path));
68e917 686                 //exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." ".escapeshellcmd($fastcgi_starter_path));
24854c 687                 
T 688                 
689                 $app->log("Creating fastcgi starter script directory: $fastcgi_starter_path",LOGLEVEL_DEBUG);
690             }
691             
68e917 692             exec("chown -R ".$data["new"]["system_user"].":".$data["new"]["system_group"]." ".escapeshellcmd($fastcgi_starter_path));
T 693             
24854c 694             $fcgi_tpl = new tpl();
T 695             $fcgi_tpl->newTemplate("php-fcgi-starter.master");
696                 
697             $fcgi_tpl->setVar('php_ini_path',$fastcgi_config["fastcgi_phpini_path"]);
698             $fcgi_tpl->setVar('document_root',$data["new"]["document_root"]);
699             $fcgi_tpl->setVar('php_fcgi_children',$fastcgi_config["fastcgi_children"]);
700             $fcgi_tpl->setVar('php_fcgi_max_requests',$fastcgi_config["fastcgi_max_requests"]);
701             $fcgi_tpl->setVar('php_fcgi_bin',$fastcgi_config["fastcgi_bin"]);
8c4aa3 702             $fcgi_tpl->setVar('security_level',$web_config["security_level"]);
24854c 703                 
T 704             $fcgi_starter_script = escapeshellcmd($fastcgi_starter_path.$fastcgi_config["fastcgi_starter_script"]);
705             file_put_contents($fcgi_starter_script,$fcgi_tpl->grab());
706             unset($fcgi_tpl);
707             
708             $app->log("Creating fastcgi starter script: $fcgi_starter_script",LOGLEVEL_DEBUG);
709             
710             
711             exec("chmod 755 $fcgi_starter_script");
712             exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." $fcgi_starter_script");
713
714             $tpl->setVar('fastcgi_alias',$fastcgi_config["fastcgi_alias"]);
715             $tpl->setVar('fastcgi_starter_path',$fastcgi_starter_path);
716             $tpl->setVar('fastcgi_starter_script',$fastcgi_config["fastcgi_starter_script"]);
717             
718         }
719         
720         /**
721          * install cgi starter script and add script alias to config.
722          * This is needed to allow cgi with suexec (to do so, we need a bin in the document-path!)
723          * first we create the script directory if not already created, then copy over the starter script.
724          * TODO: we have to fetch the data from the server-settings.
725          */
726
727         if ($data["new"]["php"] == "cgi")
728         {
729             //$cgi_config = $app->getconf->get_server_config($conf["server_id"], 'cgi');
730
731             $cgi_config["cgi_starter_path"] = $web_config["website_basedir"]."/php-cgi-scripts/[system_user]/";
732             $cgi_config["cgi_starter_script"] = "php-cgi-starter";
733             $cgi_config["cgi_bin"] = "/usr/bin/php-cgi";
734
735             $cgi_starter_path = str_replace("[system_user]",$data["new"]["system_user"],$cgi_config["cgi_starter_path"]);
736             $cgi_starter_path = str_replace("[client_id]",$client_id,$cgi_starter_path);
737
738             if (!is_dir($cgi_starter_path))
739             {
740                 exec("mkdir -p ".escapeshellcmd($cgi_starter_path));
741                 exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." ".escapeshellcmd($cgi_starter_path));
742
743                 $app->log("Creating cgi starter script directory: $cgi_starter_path",LOGLEVEL_DEBUG);
744             }
745
746             $cgi_tpl = new tpl();
747             $cgi_tpl->newTemplate("php-cgi-starter.master");
748
749             // This works, because php "rewrites" a symlink to the physical path
750             $cgi_tpl->setVar('open_basedir', $data["new"]["document_root"]); 
751             // This will NOT work!
752             //$cgi_tpl->setVar('open_basedir', "/var/www/" . $data["new"]["domain"]);
753             $cgi_tpl->setVar('php_cgi_bin',$cgi_config["cgi_bin"]);
8c4aa3 754             $cgi_tpl->setVar('security_level',$web_config["security_level"]);
24854c 755
T 756             $cgi_starter_script = escapeshellcmd($cgi_starter_path.$cgi_config["cgi_starter_script"]);
757             file_put_contents($cgi_starter_script,$cgi_tpl->grab());
758             unset($cgi_tpl);
759
760             $app->log("Creating cgi starter script: $cgi_starter_script",LOGLEVEL_DEBUG);
761
762
763             exec("chmod 755 $cgi_starter_script");
764             exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." $cgi_starter_script");
765
766             $tpl->setVar('cgi_starter_path',$cgi_starter_path);
767             $tpl->setVar('cgi_starter_script',$cgi_config["cgi_starter_script"]);
768
769         }
770
771         $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["new"]["domain"].'.vhost');
772         file_put_contents($vhost_file,$tpl->grab());
773         $app->log("Writing the vhost file: $vhost_file",LOGLEVEL_DEBUG);
774         unset($tpl);
775         
776         // Set the symlink to enable the vhost
777         $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["new"]["domain"].'.vhost');
778         if($data["new"]["active"] == 'y' && !is_link($vhost_symlink)) {
779             symlink($vhost_file,$vhost_symlink);
780             $app->log("Creating the symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
781         }
782         
783         // Remove the symlink, if site is inactive
784         if($data["new"]["active"] == 'n' && is_link($vhost_symlink)) {
785             unlink($vhost_symlink);
786             $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
787         }
788         
789         // remove old symlink and vhost file, if domain name of the site has changed
790         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
791             $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["old"]["domain"].'.vhost');
792             unlink($vhost_symlink);
793             $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
794             $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["old"]["domain"].'.vhost');
795             unlink($vhost_file);
796             $app->log("Removing File $vhost_file",LOGLEVEL_DEBUG);
797         }
798         
799         //* Create .htaccess and .htpasswd file for website statistics
8c4aa3 800         if(!is_file($data["new"]["document_root"].'/web/stats/.htaccess') or $data["old"]["document_root"] != $data["new"]["document_root"]) {
24854c 801             if(!is_dir($data["new"]["document_root"].'/web/stats')) mkdir($data["new"]["document_root"].'/web/stats');
T 802             $ht_file = "AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$data["new"]["document_root"]."/.htpasswd_stats\n<limit GET PUT POST>\nrequire valid-user\n</limit>";
803             file_put_contents($data["new"]["document_root"].'/web/stats/.htaccess',$ht_file);
804             chmod($data["new"]["document_root"].'/web/stats/.htaccess',0664);
805             unset($ht_file);
806         }
807         
808         if(!is_file($data["new"]["document_root"].'/.htpasswd_stats') || $data["new"]["stats_password"] != $data["old"]["stats_password"]) {
809             if(trim($data["new"]["stats_password"]) != '') {
810                 $htp_file = 'admin:'.trim($data["new"]["stats_password"]);
811                 file_put_contents($data["new"]["document_root"].'/.htpasswd_stats',$htp_file);
812                 chmod($data["new"]["document_root"].'/.htpasswd_stats',0664);
813                 unset($htp_file);
814             }
815         }
816         
817         
542146 818         if($apache_chrooted) {
T 819             $app->services->restartServiceDelayed('httpd','restart');
820         } else {
821             // request a httpd reload when all records have been processed
822             $app->services->restartServiceDelayed('httpd','reload');
823         }
24854c 824         
8c4aa3 825         //* Unset action to clean it for next processed vhost.
T 826         $this->action = '';
827         
24854c 828     }
T 829     
830     function delete($event_name,$data) {
831         global $app, $conf;
832         
833         // load the server configuration options
834         $app->uses("getconf");
835         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
836         
542146 837         //* Check if this is a chrooted setup
T 838         if($web_config['website_basedir'] != '' && @is_file($web_config['/var/www'].'/etc/passwd')) {
839             $apache_chrooted = true;
840         } else {
841             $apache_chrooted = false;
842         }
24854c 843         
T 844         if($data["old"]["type"] != "vhost" && $data["old"]["parent_domain_id"] > 0) {
845             //* This is a alias domain or subdomain, so we have to update the website instead
846             $parent_domain_id = intval($data["old"]["parent_domain_id"]);
847             $tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$parent_domain_id." AND active = 'y'");
848             $data["new"] = $tmp;
849             $data["old"] = $tmp;
850             $this->action = 'update';
851             // just run the update function
852             $this->update($event_name,$data);
853             
854         } else {
855             //* This is a website
856             // Deleting the vhost file, symlink and the data directory
857             $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["old"]["domain"].'.vhost');
858             unlink($vhost_symlink);
859             $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
860         
861             $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["old"]["domain"].'.vhost');
862             unlink($vhost_file);
863             $app->log("Removing vhost file: $vhost_file",LOGLEVEL_DEBUG);
864         
865             $docroot = escapeshellcmd($data["old"]["document_root"]);
866             if($docroot != '' && !stristr($docroot,'..')) exec("rm -rf $docroot");
867         
868         
869             //remove the php fastgi starter script if available
870             if ($data["old"]["php"] == "fast-cgi")
871             {
872                 $fastcgi_starter_path = str_replace("[system_user]",$data["old"]["system_user"],$web_config["fastcgi_starter_path"]);
873                 if (is_dir($fastcgi_starter_path))
874                 {
875                     exec("rm -rf $fastcgi_starter_path");
876                 }
877             }
878         
879             //remove the php cgi starter script if available
880             if ($data["old"]["php"] == "cgi")
881             {
882                 // TODO: fetch the date from the server-settings
883                 $web_config["cgi_starter_path"] = $web_config["website_basedir"]."/php-cgi-scripts/[system_user]/";
884
885                 $cgi_starter_path = str_replace("[system_user]",$data["old"]["system_user"],$web_config["cgi_starter_path"]);
886                 if (is_dir($cgi_starter_path))
887                 {
888                     exec("rm -rf $cgi_starter_path");
889                 }
890             }
891
892             $app->log("Removing website: $docroot",LOGLEVEL_DEBUG);
893         
894             // Delete the symlinks for the sites
895             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".intval($data["old"]["sys_groupid"]));
896             $client_id = intval($client["client_id"]);
897             unset($client);
898             $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
899             if(is_array($tmp_symlinks_array)) {
900                 foreach($tmp_symlinks_array as $tmp_symlink) {
901                     $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
902                     $tmp_symlink = str_replace("[website_domain]",$data["old"]["domain"],$tmp_symlink);
903                     // Remove trailing slash
904                     if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
905                     // create the symlinks, if not exist
906                     if(is_link($tmp_symlink)) {
907                         unlink($tmp_symlink);
908                         $app->log("Removing symlink: ".$tmp_symlink,LOGLEVEL_DEBUG);
909                     }
910                 }
911             }
912             // end removing symlinks
913         
914             // Delete the log file directory
915             $vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data["old"]["domain"]);
916             if($data["old"]["domain"] != '' && !stristr($vhost_logfile_dir,'..')) exec("rm -rf $vhost_logfile_dir");
917             $app->log("Removing website logfile directory: $vhost_logfile_dir",LOGLEVEL_DEBUG);
918         
919             //delete the web user
920             $command = 'userdel';
921             $command .= ' '.$data["old"]["system_user"];            
922             exec($command);
542146 923             if($apache_chrooted) $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." ".$command);
T 924             
24854c 925         }
T 926     }
927     
928     //* This function is called when a IP on the server is inserted, updated or deleted
929     function server_ip($event_name,$data) {
930         global $app, $conf;
931         
932         // load the server configuration options
933         $app->uses("getconf");
934         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
935         
936         $app->load('tpl');
937         
938         $tpl = new tpl();
939         $tpl->newTemplate("apache_ispconfig.conf.master");
940         $records = $app->db->queryAllRecords("SELECT * FROM server_ip WHERE server_id = ".$conf["server_id"]." AND virtualhost = 'y'");
941         
942         if(count($records) > 0) {
943             $tpl->setLoop('ip_adresses',$records);
944         }
945         
946         $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/ispconfig.conf');
947         file_put_contents($vhost_file,$tpl->grab());
948         $app->log("Writing the conf file: ispconfig.conf",LOGLEVEL_DEBUG);
949         unset($tpl);
950         
951     }
952     
69944a 953     //* Wrapper for exec function for easier debugging
T 954     private function _exec($command) {
955         global $app;
956         $app->log("exec: ".$command,LOGLEVEL_DEBUG);
957         exec($command);
958     }
959     
24854c 960
T 961 } // end class
962
d74308 963 ?>