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