tbrehm
2008-07-10 37d7556980539287f08c8640bf0bb55523c55b69
commit | author | age
18341e 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
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';
e2d6ed 34     var $class_name = 'apache2_plugin';
18341e 35     
9cb713 36     // private variables
T 37     var $action = '';
38     
18341e 39         
T 40     /*
41          This function is called when the plugin is loaded
42     */
43     
44     function onLoad() {
45         global $app;
46         
47         /*
48         Register for the events
49         */
50         
0a466d 51         
T 52         
53         $app->plugins->registerEvent('web_domain_insert',$this->plugin_name,'ssl');
54         $app->plugins->registerEvent('web_domain_update',$this->plugin_name,'ssl');
55         $app->plugins->registerEvent('web_domain_delete',$this->plugin_name,'ssl');
56         
18341e 57         $app->plugins->registerEvent('web_domain_insert',$this->plugin_name,'insert');
T 58         $app->plugins->registerEvent('web_domain_update',$this->plugin_name,'update');
59         $app->plugins->registerEvent('web_domain_delete',$this->plugin_name,'delete');
60         
af8f1b 61         $app->plugins->registerEvent('server_ip_insert',$this->plugin_name,'server_ip');
T 62         $app->plugins->registerEvent('server_ip_update',$this->plugin_name,'server_ip');
63         $app->plugins->registerEvent('server_ip_delete',$this->plugin_name,'server_ip');
64         
18341e 65     }
0a466d 66     
T 67     // Handle the creation of SSL certificates
68     function ssl($event_name,$data) {
69         global $app, $conf;
70         
71         if(!is_dir($data["new"]["document_root"]."/ssl")) exec("mkdir -p ".$data["new"]["document_root"]."/ssl");
72         $ssl_dir = $data["new"]["document_root"]."/ssl";
73         $domain = $data["new"]["domain"];
74         $key_file = $ssl_dir.'/'.$domain.".key.org";
75           $key_file2 = $ssl_dir.'/'.$domain.".key";
76           $csr_file = $ssl_dir.'/'.$domain.".csr";
77           $crt_file = $ssl_dir.'/'.$domain.".crt";
78         
79         //* Create a SSL Certificate
80         if($data["new"]["ssl_action"] == 'create') {
81             $rand_file = $ssl_dir."/random_file";
82             $rand_data = md5(uniqid(microtime(),1));
83             for($i=0; $i<1000; $i++){
84                 $rand_data .= md5(uniqid(microtime(),1));
85                 $rand_data .= md5(uniqid(microtime(),1));
86                 $rand_data .= md5(uniqid(microtime(),1));
87                 $rand_data .= md5(uniqid(microtime(),1));
88             }
89             file_put_contents($rand_file, $rand_data);
90
91             $ssl_password = substr(md5(uniqid(microtime(),1)), 0, 15);
92             
93             $ssl_cnf = "        RANDFILE               = $rand_file
94
95         [ req ]
96         default_bits           = 1024
97         default_keyfile        = keyfile.pem
98         distinguished_name     = req_distinguished_name
99         attributes             = req_attributes
100         prompt                 = no
101         output_password        = $ssl_password
102
103         [ req_distinguished_name ]
4132c6 104         C                      = ".$data['new']['ssl_country']."
T 105         ST                     = ".$data['new']['ssl_state']."
106         L                      = ".$data['new']['ssl_locality']."
107         O                      = ".$data['new']['ssl_organisation']."
108         OU                     = ".$data['new']['ssl_organisation_unit']."
0a466d 109         CN                     = $domain
4132c6 110         emailAddress           = webmatser@".$data['new']['domain']."
0a466d 111
T 112         [ req_attributes ]
113         challengePassword              = A challenge password";
114             
115             $ssl_cnf_file = $ssl_dir."/openssl.conf";
4132c6 116             file_put_contents($ssl_cnf_file,$ssl_cnf);
0a466d 117             
T 118             $rand_file = escapeshellcmd($rand_file);
119             $key_file = escapeshellcmd($key_file);
120             $key_file2 = escapeshellcmd($key_file2);
121             $ssl_days = 3650;
122             $csr_file = escapeshellcmd($csr_file);
026b48 123             $config_file = escapeshellcmd($ssl_cnf_file);
4132c6 124             $crt_file = escapeshellcmd($crt_file);
0a466d 125
T 126             if(is_file($ssl_cnf_file)){
4132c6 127                   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");
026b48 128                 $app->log("Creating SSL Cert for: $domain",LOGLEVEL_DEBUG);
0a466d 129             }
T 130
131             exec("chmod 400 $key_file2");
4132c6 132             @unlink($config_file);
T 133             @unlink($rand_file);
0a466d 134             $ssl_request = file_get_contents($csr_file);
T 135             $ssl_cert = file_get_contents($crt_file);
4132c6 136             $app->db->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert' WHERE domain = '".$data["new"]["domain"]."'");
T 137             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
0a466d 138         }
T 139         
140         //* Save a SSL certificate to disk
141         if($data["new"]["ssl_action"] == 'save') {
026b48 142             $ssl_dir = $data["new"]["document_root"]."/ssl";
T 143             $domain = $data["new"]["domain"];
144               $csr_file = $ssl_dir.'/'.$domain.".csr";
145               $crt_file = $ssl_dir.'/'.$domain.".crt";
146             $bundle_file = $ssl_dir.'/'.$domain.".bundle";
147             file_put_contents($csr_file,$data["new"]["ssl_request"]);
148             file_put_contents($crt_file,$data["new"]["ssl_cert"]);
149             if(trim($data["new"]["ssl_bundle"]) != '') file_put_contents($bundle_file,$data["new"]["ssl_bundle"]);
4132c6 150             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
026b48 151             $app->log("Saving SSL Cert for: $domain",LOGLEVEL_DEBUG);
T 152         }
153         
154         //* Delete a SSL certificate
155         if($data["new"]["ssl_action"] == 'del') {
156             $ssl_dir = $data["new"]["document_root"]."/ssl";
157             $domain = $data["new"]["domain"];
158               $csr_file = $ssl_dir.'/'.$domain.".csr";
159               $crt_file = $ssl_dir.'/'.$domain.".crt";
160             $bundle_file = $ssl_dir.'/'.$domain.".bundle";
161             unlink($csr_file);
162             unlink($crt_file);
163             unlink($bundle_file);
4132c6 164             $app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data["new"]["domain"]."'");
026b48 165             $app->log("Deleting SSL Cert for: $domain",LOGLEVEL_DEBUG);
0a466d 166         }
T 167         
168         
169     }
170     
18341e 171     
T 172     function insert($event_name,$data) {
173         global $app, $conf;
174         
9cb713 175         $this->action = 'insert';
e2d6ed 176         // just run the update function
T 177         $this->update($event_name,$data);
18341e 178         
T 179         
180     }
181     
182     
183     function update($event_name,$data) {
184         global $app, $conf;
185         
9cb713 186         if($this->action != 'insert') $this->action = 'update';
a35764 187         
T 188         if($data["new"]["type"] != "vhost" && $data["new"]["parent_domain_id"] > 0) {
189             // This is not a vhost, so we need to update the parent record instead.
190             $parent_domain_id = intval($data["new"]["parent_domain_id"]);
4fae7e 191             $tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$parent_domain_id." AND active = 'y'");
a35764 192             $data["new"] = $tmp;
T 193             $data["old"] = $tmp;
194         }
195         
196         
18341e 197         // load the server configuration options
T 198         $app->uses("getconf");
199         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
200         
e2d6ed 201         if($data["new"]["document_root"] == '') {
T 202             $app->log("document_root not set",LOGLEVEL_WARN);
a35764 203             return 0;
T 204         }
205         if($data["new"]["system_user"] == 'root' or $data["new"]["system_group"] == 'root') {
206             $app->log("Websites can not be owned by the root user or group.",LOGLEVEL_WARN);
e2d6ed 207             return 0;
T 208         }
209         
210         //print_r($data);
211         
212         // Check if the directories are there and create them if nescessary.
213         if(!is_dir($data["new"]["document_root"]."/web")) exec("mkdir -p ".$data["new"]["document_root"]."/web");
313e33 214         if(!is_dir($data["new"]["document_root"]."/web/error")) exec("mkdir -p ".$data["new"]["document_root"]."/web/error");
T 215         //if(!is_dir($data["new"]["document_root"]."/log")) exec("mkdir -p ".$data["new"]["document_root"]."/log");
e2d6ed 216         if(!is_dir($data["new"]["document_root"]."/ssl")) exec("mkdir -p ".$data["new"]["document_root"]."/ssl");
T 217         if(!is_dir($data["new"]["document_root"]."/cgi-bin")) exec("mkdir -p ".$data["new"]["document_root"]."/cgi-bin");
218         
e2c5f8 219         // Remove the symlink for the site, if site is renamed
T 220         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
37d755 221             if(is_dir('/var/log/ispconfig/httpd/'.$data["old"]["domain"])) exec('rm -rf /var/log/ispconfig/httpd/'.$data["old"]["domain"]);
T 222             if(is_link($data["old"]["document_root"]."/log")) unlink($data["old"]["document_root"]."/log");
223         }
224         
225         // Create the symlink for the logfiles
226         if(!is_dir('/var/log/ispconfig/httpd/'.$data["new"]["domain"])) exec('mkdir -p /var/log/ispconfig/httpd/'.$data["new"]["domain"]);
227         if(!is_link($data["new"]["document_root"]."/log")) {
228             exec("ln -s /var/log/ispconfig/httpd/".$data["new"]["domain"]." ".$data["new"]["document_root"]."/log");
229             $app->log("Creating Symlink: ln -s /var/log/ispconfig/httpd/".$data["new"]["domain"]." ".$data["new"]["document_root"]."/log",LOGLEVEL_DEBUG);
230         }
231         /*
232         // Create the symlink for the logfiles
233         // This does not work as vlogger can not log trogh symlinks.
234         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
a91fdb 235             if(is_dir($data["old"]["document_root"]."/log")) exec('rm -rf '.$data["old"]["document_root"]."/log");
D 236             if(is_link('/var/log/ispconfig/httpd/'.$data["old"]["domain"])) unlink('/var/log/ispconfig/httpd/'.$data["old"]["domain"]);
e2c5f8 237         }
T 238         
313e33 239         // Create the symlink for the logfiles
a91fdb 240         if(!is_dir($data["new"]["document_root"]."/log")) exec('mkdir -p '.$data["new"]["document_root"]."/log");
D 241         if(!is_link('/var/log/ispconfig/httpd/'.$data["new"]["domain"])) {
242             exec("ln -s ".$data["new"]["document_root"]."/log /var/log/ispconfig/httpd/".$data["new"]["domain"]);
243             $app->log("Creating Symlink: ln -s ".$data["new"]["document_root"]."/log /var/log/ispconfig/httpd/".$data["new"]["domain"],LOGLEVEL_DEBUG);
641781 244         }
37d755 245         */
a91fdb 246     
e2c5f8 247         // Get the client ID
15d78a 248         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".intval($data["new"]["sys_groupid"]));
T 249         $client_id = intval($client["client_id"]);
250         unset($client);
e2c5f8 251         
T 252         // Remove old symlinks, if site is renamed
253         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
254             $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
255             if(is_array($tmp_symlinks_array)) {
256                 foreach($tmp_symlinks_array as $tmp_symlink) {
257                     $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
318ace 258                     $tmp_symlink = str_replace("[website_domain]",$data["old"]["domain"],$tmp_symlink);
e2c5f8 259                     // Remove trailing slash
T 260                     if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
261                     // create the symlinks, if not exist
262                     if(!is_link($tmp_symlink)) {
263                         exec("rm -f ".escapeshellcmd($tmp_symlink));
264                         $app->log("Removed Symlink: rm -f ".$tmp_symlink,LOGLEVEL_DEBUG);
265                     }
266                 }
267             }
268         }
269         
270         // Create the symlinks for the sites
15d78a 271         $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
0a466d 272         if(is_array($tmp_symlinks_array)) {
T 273             foreach($tmp_symlinks_array as $tmp_symlink) {
274                 $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
275                 $tmp_symlink = str_replace("[website_domain]",$data["new"]["domain"],$tmp_symlink);
276                 // Remove trailing slash
277                 if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
278                 // create the symlinks, if not exist
279                 if(!is_link($tmp_symlink)) {
280                     exec("ln -s ".escapeshellcmd($data["new"]["document_root"])."/ ".escapeshellcmd($tmp_symlink));
281                     $app->log("Creating Symlink: ln -s ".$data["new"]["document_root"]."/ ".$tmp_symlink,LOGLEVEL_DEBUG);
282                 }
15d78a 283             }
T 284         }
e2d6ed 285         
e2c5f8 286         
9cb713 287         if($this->action == 'insert') {
T 288             // Copy the error pages
289             $error_page_path = escapeshellcmd($data["new"]["document_root"])."/web/error/";
290             exec("cp /usr/local/ispconfig/server/conf/error/".substr(escapeshellcmd($conf["language"]),0,2)."/* ".$error_page_path);
c6d48b 291             exec("chmod -R +r ".$error_page_path);
e2d6ed 292         
9cb713 293             // copy the standard index page
T 294             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");
c6d48b 295             exec("chmod +r ".escapeshellcmd($data["new"]["document_root"])."/web/index.html");
9cb713 296         }
30aa08 297         
e2d6ed 298         // Create group and user, if not exist
T 299         $app->uses("system");
300         
301         $groupname = escapeshellcmd($data["new"]["system_group"]);
302         if($data["new"]["system_group"] != '' && !$app->system->is_group($data["new"]["system_group"])) {
303             exec("groupadd $groupname");
304             $app->log("Adding the group: $groupname",LOGLEVEL_DEBUG);
305         }
306         
307         $username = escapeshellcmd($data["new"]["system_user"]);
308         if($data["new"]["system_user"] != '' && !$app->system->is_user($data["new"]["system_user"])) {
910093 309             exec("useradd -d ".escapeshellcmd($data["new"]["document_root"])." -g $groupname $username -s /bin/false");
e2d6ed 310             $app->log("Adding the user: $username",LOGLEVEL_DEBUG);
T 311         }
312         
a35764 313         // Set the quota for the user
T 314         if($username != '' && $app->system->is_user($username)) {
315             if($data["new"]["hd_quota"] > 0){
316                 $blocks_soft = $data["new"]["hd_quota"] * 1024;
317                 $blocks_hard = $blocks_soft + 1024;
318               } else {
319                 $blocks_soft = $blocks_hard = 0;
320               }
321             exec("setquota -u $username $blocks_soft $blocks_hard 0 0 -a &> /dev/null");
322             exec("setquota -T -u $username 604800 604800 -a &> /dev/null");
323         }
324         
325         
326         
e2d6ed 327         // Chown and chmod the directories
T 328         exec("chown -R $username:$groupname ".escapeshellcmd($data["new"]["document_root"]));
329         
c6d48b 330         
e2d6ed 331         // Create the vhost config file
T 332         $app->load('tpl');
333         
334         $tpl = new tpl();
335         $tpl->newTemplate("vhost.conf.master");
336         
337         $vhost_data = $data["new"];
a35764 338         $vhost_data["web_document_root"] = $data["new"]["document_root"]."/web";
026b48 339         
T 340         // Check if a SSL cert exists
341         $ssl_dir = $data["new"]["document_root"]."/ssl";
342         $domain = $data["new"]["domain"];
343           $key_file = $ssl_dir.'/'.$domain.".key";
344           $crt_file = $ssl_dir.'/'.$domain.".crt";
345         $bundle_file = $ssl_dir.'/'.$domain.".bundle";
346         
4132c6 347         if($data["new"]["ssl"] == 'y' && @is_file($crt_file) && @is_file($key_file)) {
026b48 348             $vhost_data["ssl_enabled"] = 1;
T 349             $app->log("Enable SSL for: $domain",LOGLEVEL_DEBUG);
350         } else {
351             $vhost_data["ssl_enabled"] = 0;
352             $app->log("Disable SSL for: $domain",LOGLEVEL_DEBUG);
353         }
354         
355         if(@is_file($bundle_file)) $vhost_data['has_bundle_cert'] = 1;
356         
a35764 357         //$vhost_data["document_root"] = $data["new"]["document_root"]."/web";
e2d6ed 358         $tpl->setVar($vhost_data);
T 359         
a35764 360         // Rewrite rules
T 361         $rewrite_rules = array();
362         if($data["new"]["redirect_type"] != '') {
363             $rewrite_rules[] = array(    'rewrite_domain'     => $data["new"]["domain"],
364                                         'rewrite_type'         => $data["new"]["redirect_type"],
365                                         'rewrite_target'     => $data["new"]["redirect_path"]);
366         }
367         
368         // get alias domains (co-domains and subdomains)
4fae7e 369         $aliases = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ".$data["new"]["domain_id"]." AND active = 'y'");
e2d6ed 370         $server_alias = '';
a35764 371         if(is_array($aliases)) {
T 372             foreach($aliases as $alias) {
373                 $server_alias .= $alias["domain"].' ';
374                 $app->log("Add server alias: $alias[domain]",LOGLEVEL_DEBUG);
375                 // Rewriting
376                 if($alias["redirect_type"] != '') {
377                     $rewrite_rules[] = array(    'rewrite_domain'     => $alias["domain"],
378                                                 'rewrite_type'         => $alias["redirect_type"],
379                                                 'rewrite_target'     => $alias["redirect_path"]);
380                 }
381             }
e2d6ed 382         }
T 383         $tpl->setVar('alias',trim($server_alias));
df7382 384         if(count($rewrite_rules) > 0) {
T 385             $tpl->setVar('rewrite_enabled',1);
386         } else {
387             $tpl->setVar('rewrite_enabled',0);
388         }
a35764 389         $tpl->setLoop('redirects',$rewrite_rules);
e2d6ed 390         
9b9ba4 391         /** 
D 392          * install fast-cgi starter script and add script aliasd config 
393          * first we create the script directory if not already created, then copy over the starter script
394          * settings are copied over from the server ini config for now
395          * TODO: Create form for fastcgi configs per site.
396          */
397         
398         if ($data["new"]["php"] == "fast-cgi")
399         {
6a95c8 400             $fastcgi_config = $app->getconf->get_server_config($conf["server_id"], 'fastcgi');
D 401             
402             $fastcgi_starter_path = str_replace("[system_user]",$data["new"]["system_user"],$fastcgi_config["fastcgi_starter_path"]);
61d290 403             $fastcgi_starter_path = str_replace("[client_id]",$client_id,$fastcgi_starter_path);
D 404             
9b9ba4 405             if (!is_dir($fastcgi_starter_path))
D 406             {
61d290 407                 exec("mkdir -p ".escapeshellcmd($fastcgi_starter_path));
D 408                 exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." ".escapeshellcmd($fastcgi_starter_path));
6a95c8 409                 
D 410                 
411                 $app->log("Creating fastcgi starter script directory: $fastcgi_starter_path",LOGLEVEL_DEBUG);
9b9ba4 412             }
D 413             
414             $fcgi_tpl = new tpl();
415             $fcgi_tpl->newTemplate("php-fcgi-starter.master");
416                 
6a95c8 417             $fcgi_tpl->setVar('php_ini_path',$fastcgi_config["fastcgi_phpini_path"]);
9b9ba4 418             $fcgi_tpl->setVar('document_root',$data["new"]["document_root"]);
6a95c8 419             $fcgi_tpl->setVar('php_fcgi_children',$fastcgi_config["fastcgi_children"]);
D 420             $fcgi_tpl->setVar('php_fcgi_max_requests',$fastcgi_config["fastcgi_max_requests"]);
421             $fcgi_tpl->setVar('php_fcgi_bin',$fastcgi_config["fastcgi_bin"]);
9b9ba4 422                 
61d290 423             $fcgi_starter_script = escapeshellcmd($fastcgi_starter_path.$fastcgi_config["fastcgi_starter_script"]);
9b9ba4 424             file_put_contents($fcgi_starter_script,$fcgi_tpl->grab());
D 425             unset($fcgi_tpl);
6a95c8 426             
D 427             $app->log("Creating fastcgi starter script: $fcgi_starter_script",LOGLEVEL_DEBUG);
428             
9b9ba4 429             
D 430             exec("chmod 755 $fcgi_starter_script");
431             exec("chown ".$data["new"]["system_user"].":".$data["new"]["system_group"]." $fcgi_starter_script");
432
6a95c8 433             $tpl->setVar('fastcgi_alias',$fastcgi_config["fastcgi_alias"]);
9b9ba4 434             $tpl->setVar('fastcgi_starter_path',$fastcgi_starter_path);
D 435             
436         }
437         
e2d6ed 438         $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["new"]["domain"].'.vhost');
T 439         file_put_contents($vhost_file,$tpl->grab());
440         $app->log("Writing the vhost file: $vhost_file",LOGLEVEL_DEBUG);
441         unset($tpl);
442         
443         // Set the symlink to enable the vhost
444         $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["new"]["domain"].'.vhost');
445         if($data["new"]["active"] == 'y' && !is_link($vhost_symlink)) {
446             symlink($vhost_file,$vhost_symlink);
447             $app->log("Creating the symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
448         }
449         
450         // Remove the symlink, if site is inactive
451         if($data["new"]["active"] == 'n' && is_link($vhost_symlink)) {
452             unlink($vhost_symlink);
453             $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
454         }
455         
6724a4 456         // remove old symlink and vhost file, if domain name of the site has changed
318ace 457         if($this->action == 'update' && $data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"]) {
T 458             $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["old"]["domain"].'.vhost');
459             unlink($vhost_symlink);
460             $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
6724a4 461             $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["old"]["domain"].'.vhost');
T 462             unlink($vhost_file);
463             $app->log("Removing File $vhost_file",LOGLEVEL_DEBUG);
318ace 464         }
T 465         
e2d6ed 466         // request a httpd reload when all records have been processed
T 467         $app->services->restartServiceDelayed('httpd','reload');
18341e 468         
T 469     }
470     
471     function delete($event_name,$data) {
472         global $app, $conf;
473         
e2d6ed 474         // load the server configuration options
T 475         $app->uses("getconf");
476         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
477         
478         // Deleting the vhost file, symlink and the data directory
479         $vhost_symlink = escapeshellcmd($web_config["vhost_conf_enabled_dir"].'/'.$data["old"]["domain"].'.vhost');
480         unlink($vhost_symlink);
481         $app->log("Removing symlink: $vhost_symlink => $vhost_file",LOGLEVEL_DEBUG);
482         
483         $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["old"]["domain"].'.vhost');
484         unlink($vhost_file);
485         $app->log("Removing vhost file: $vhost_file",LOGLEVEL_DEBUG);
486         
487         $docroot = escapeshellcmd($data["old"]["document_root"]);
488         if($docroot != '' && !stristr($docroot,'..')) exec("rm -rf $docroot");
a91fdb 489         
5a8b3e 490         
a91fdb 491         //remove the php fastgi starter script if available
D 492         if ($data["old"]["php"] == "fast-cgi")
493         {
494             $fastcgi_starter_path = str_replace("[system_user]",$data["old"]["system_user"],$web_config["fastcgi_starter_path"]);
495             if (is_dir($fastcgi_starter_path))
496             {
497                     exec("rm -rf $fastcgi_starter_path");
498             }
499         }
500         
e2d6ed 501         $app->log("Removing website: $docroot",LOGLEVEL_DEBUG);
18341e 502         
0a466d 503         // Delete the symlinks for the sites
T 504         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".intval($data["old"]["sys_groupid"]));
505         $client_id = intval($client["client_id"]);
506         unset($client);
507         $tmp_symlinks_array = explode(':',$web_config["website_symlinks"]);
508         if(is_array($tmp_symlinks_array)) {
509             foreach($tmp_symlinks_array as $tmp_symlink) {
510                 $tmp_symlink = str_replace("[client_id]",$client_id,$tmp_symlink);
511                 $tmp_symlink = str_replace("[website_domain]",$data["old"]["domain"],$tmp_symlink);
512                 // Remove trailing slash
513                 if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
514                 // create the symlinks, if not exist
515                 if(is_link($tmp_symlink)) {
4132c6 516                     unlink($tmp_symlink);
0a466d 517                     $app->log("Removing symlink: ".$tmp_symlink,LOGLEVEL_DEBUG);
T 518                 }
519             }
520         }
521         // end removing symlinks
522         
eded9d 523         // Delete the log file directory
T 524         $vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data["old"]["domain"]);
525         if($data["old"]["domain"] != '' && !stristr($vhost_logfile_dir,'..')) exec("rm -rf $vhost_logfile_dir");
526         $app->log("Removing website logfile directory: $vhost_logfile_dir",LOGLEVEL_DEBUG);
527         
6a95c8 528         //delete the web user
D 529         $command = 'userdel';
530         $command .= ' '.$data["old"]["system_user"];            
531         exec($command);
18341e 532     }
T 533     
af8f1b 534     //* This function is called when a IP on the server is inserted, updated or deleted
T 535     function server_ip($event_name,$data) {
536         global $app, $conf;
537         
538         // Here we write the name virtualhost directives
539         // NameVirtualHost IP:80
540         // NameVirtualHost IP:443
541         
542     }
543     
18341e 544
T 545 } // end class
546
547 ?>