Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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 web_module {
7fe908 32
18341e 33     var $module_name = 'web_module';
T 34     var $class_name = 'web_module';
7fe908 35     var $actions_available = array( 'web_domain_insert',
MC 36         'web_domain_update',
37         'web_domain_delete',
38         'ftp_user_insert',
39         'ftp_user_update',
40         'ftp_user_delete',
41         'shell_user_insert',
42         'shell_user_update',
43         'shell_user_delete',
44         'webdav_user_insert',
45         'webdav_user_update',
46         'webdav_user_delete',
47         'web_folder_insert',
48         'web_folder_update',
49         'web_folder_delete',
50         'web_folder_user_insert',
51         'web_folder_user_update',
52         'web_folder_user_delete',
53         'web_backup_insert',
54         'web_backup_update',
55         'web_backup_delete',
56         'aps_instance_insert',
57         'aps_instance_update',
58         'aps_instance_delete',
59         'aps_instance_setting_insert',
60         'aps_instance_setting_update',
61         'aps_instance_setting_delete',
62         'aps_package_insert',
63         'aps_package_update',
64         'aps_package_delete',
65         'aps_setting_insert',
66         'aps_setting_update',
67         'aps_setting_delete');
68
392450 69     //* This function is called during ispconfig installation to determine
T 70     //  if a symlink shall be created for this plugin.
71     function onInstall() {
72         global $conf;
7fe908 73
392450 74         if($conf['services']['web'] == true) {
T 75             return true;
76         } else {
77             return false;
78         }
7fe908 79
392450 80     }
7fe908 81
18341e 82     /*
T 83          This function is called when the module is loaded
84     */
7fe908 85
18341e 86     function onLoad() {
T 87         global $app;
7fe908 88
18341e 89         /*
7fe908 90         Annonce the actions that where provided by this module, so plugins
18341e 91         can register on them.
T 92         */
7fe908 93
MC 94         $app->plugins->announceEvents($this->module_name, $this->actions_available);
95
18341e 96         /*
T 97         As we want to get notified of any changes on several database tables,
98         we register for them.
7fe908 99
18341e 100         The following function registers the function "functionname"
7fe908 101          to be executed when a record for the table "dbtable" is
18341e 102          processed in the sys_datalog. "classname" is the name of the
T 103          class that contains the function functionname.
104         */
7fe908 105
MC 106         $app->modules->registerTableHook('web_domain', 'web_module', 'process');
107         $app->modules->registerTableHook('ftp_user', 'web_module', 'process');
108         $app->modules->registerTableHook('shell_user', 'web_module', 'process');
109         $app->modules->registerTableHook('webdav_user', 'web_module', 'process');
110         $app->modules->registerTableHook('web_folder', 'web_module', 'process');
111         $app->modules->registerTableHook('web_folder_user', 'web_module', 'process');
112         $app->modules->registerTableHook('web_backup', 'web_module', 'process');
113         $app->modules->registerTableHook('aps_instances', 'web_module', 'process');
114         $app->modules->registerTableHook('aps_instances_settings', 'web_module', 'process');
115         $app->modules->registerTableHook('aps_packages', 'web_module', 'process');
116         $app->modules->registerTableHook('aps_settings', 'web_module', 'process');
117
e2d6ed 118         // Register service
7fe908 119         $app->services->registerService('httpd', 'web_module', 'restartHttpd');
MC 120         $app->services->registerService('php-fpm', 'web_module', 'restartPHP_FPM');
121
18341e 122     }
7fe908 123
18341e 124     /*
T 125      This function is called when a change in one of the registered tables is detected.
126      The function then raises the events for the plugins.
127     */
128
7fe908 129     function process($tablename, $action, $data) {
18341e 130         global $app;
7fe908 131
18341e 132         switch ($tablename) {
7fe908 133         case 'web_domain':
MC 134             if($action == 'i') $app->plugins->raiseEvent('web_domain_insert', $data);
135             if($action == 'u') $app->plugins->raiseEvent('web_domain_update', $data);
136             if($action == 'd') $app->plugins->raiseEvent('web_domain_delete', $data);
18341e 137             break;
7fe908 138         case 'ftp_user':
MC 139             if($action == 'i') $app->plugins->raiseEvent('ftp_user_insert', $data);
140             if($action == 'u') $app->plugins->raiseEvent('ftp_user_update', $data);
141             if($action == 'd') $app->plugins->raiseEvent('ftp_user_delete', $data);
15d78a 142             break;
7fe908 143         case 'shell_user':
MC 144             if($action == 'i') $app->plugins->raiseEvent('shell_user_insert', $data);
145             if($action == 'u') $app->plugins->raiseEvent('shell_user_update', $data);
146             if($action == 'd') $app->plugins->raiseEvent('shell_user_delete', $data);
15d78a 147             break;
7fe908 148         case 'webdav_user':
MC 149             if($action == 'i') $app->plugins->raiseEvent('webdav_user_insert', $data);
150             if($action == 'u') $app->plugins->raiseEvent('webdav_user_update', $data);
151             if($action == 'd') $app->plugins->raiseEvent('webdav_user_delete', $data);
ac933e 152             break;
7fe908 153         case 'web_folder':
MC 154             if($action == 'i') $app->plugins->raiseEvent('web_folder_insert', $data);
155             if($action == 'u') $app->plugins->raiseEvent('web_folder_update', $data);
156             if($action == 'd') $app->plugins->raiseEvent('web_folder_delete', $data);
524077 157             break;
7fe908 158         case 'web_folder_user':
MC 159             if($action == 'i') $app->plugins->raiseEvent('web_folder_user_insert', $data);
160             if($action == 'u') $app->plugins->raiseEvent('web_folder_user_update', $data);
161             if($action == 'd') $app->plugins->raiseEvent('web_folder_user_delete', $data);
524077 162             break;
7fe908 163         case 'web_backup':
MC 164             if($action == 'i') $app->plugins->raiseEvent('web_backup_insert', $data);
165             if($action == 'u') $app->plugins->raiseEvent('web_backup_update', $data);
166             if($action == 'd') $app->plugins->raiseEvent('web_backup_delete', $data);
5a43e7 167             break;
7fe908 168         case 'aps_instances':
MC 169             if($action == 'i') $app->plugins->raiseEvent('aps_instance_insert', $data);
170             if($action == 'u') $app->plugins->raiseEvent('aps_instance_update', $data);
171             if($action == 'd') $app->plugins->raiseEvent('aps_instance_delete', $data);
7d4360 172             break;
7fe908 173         case 'aps_instances_settings':
MC 174             if($action == 'i') $app->plugins->raiseEvent('aps_instance_setting_insert', $data);
175             if($action == 'u') $app->plugins->raiseEvent('aps_instance_setting_update', $data);
176             if($action == 'd') $app->plugins->raiseEvent('aps_instance_setting_delete', $data);
7d4360 177             break;
7fe908 178         case 'aps_packages':
MC 179             if($action == 'i') $app->plugins->raiseEvent('aps_package_insert', $data);
180             if($action == 'u') $app->plugins->raiseEvent('aps_package_update', $data);
181             if($action == 'd') $app->plugins->raiseEvent('aps_package_delete', $data);
7d4360 182             break;
7fe908 183         case 'aps_settings':
MC 184             if($action == 'i') $app->plugins->raiseEvent('aps_setting_insert', $data);
185             if($action == 'u') $app->plugins->raiseEvent('aps_setting_update', $data);
186             if($action == 'd') $app->plugins->raiseEvent('aps_setting_delete', $data);
7d4360 187             break;
18341e 188         } // end switch
T 189     } // end function
7fe908 190
MC 191
e2d6ed 192     // This function is used
T 193     function restartHttpd($action = 'restart') {
7fe908 194         global $app, $conf;
MC 195
0ae8da 196         // load the server configuration options
33bcd0 197         $app->uses('getconf,system');
0ae8da 198         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
7fe908 199
47f5e0 200         $daemon = '';
0ae8da 201         switch ($web_config['server_type']) {
7fe908 202         case 'nginx':
MC 203             $daemon = $web_config['server_type'];
204             break;
205         default:
5686ed 206             if(is_file($conf['init_scripts'] . '/' . 'httpd') || is_dir('/etc/httpd')) {
7fe908 207                 $daemon = 'httpd';
MC 208             } else {
209                 $daemon = 'apache2';
210             }
0711af 211         }
e2c00a 212
615a0a 213         $retval = array('output' => '', 'retval' => 0);
0711af 214         if($action == 'restart') {
6d6643 215             $cmd = $app->system->getinitcommand($daemon, 'restart');
0711af 216         } else {
6d6643 217             $cmd = $app->system->getinitcommand($daemon, 'reload');
e2d6ed 218         }
d22277 219         
MB 220         if($web_config['server_type'] == 'nginx'){
221             $app->log("Checking nginx configuration...", LOGLEVEL_DEBUG);
222             exec('nginx -t 2>&1', $retval['output'], $retval['retval']);
223             if($retval['retval'] == 0){
224                 $app->log("nginx configuration ok!", LOGLEVEL_DEBUG);
225             } else {
226                 $app->log("nginx config test failed!", LOGLEVEL_DEBUG);
227                 return $retval;
228             }
229         }
230         
6d6643 231         exec($cmd.' 2>&1', $retval['output'], $retval['retval']);
d22277 232         
MB 233         // if restart failed despite successful syntax check => try again
234         if($web_config['server_type'] == 'nginx' && $retval['retval'] > 0){
235             sleep(2);
236             exec($cmd.' 2>&1', $retval['output'], $retval['retval']);
237         }
6d6643 238         $app->log("Restarting httpd: $cmd", LOGLEVEL_DEBUG);
ed7ede 239         
FT 240         // nginx: do a syntax check because on some distributions, the init script always returns 0 - even if the syntax is not ok (how stupid is that?)
d22277 241         //if($web_config['server_type'] == 'nginx' && $retval['retval'] == 0){
MB 242             //exec('nginx -t 2>&1', $retval['output'], $retval['retval']);
243         //}
615a0a 244         return $retval;
e2d6ed 245     }
7fe908 246
e2c00a 247     function restartPHP_FPM($action = 'restart') {
7fe908 248         global $app, $conf;
MC 249
e2c00a 250         // load the server configuration options
33bcd0 251         $app->uses('getconf,system');
e2c00a 252         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
7fe908 253
e2c00a 254         list($action, $init_script) = explode(':', $action);
7fe908 255
33bcd0 256         if(!$init_script){
FT 257             //$init_script = $conf['init_scripts'].'/'.$web_config['php_fpm_init_script'];
258             $initcommand = $app->system->getinitcommand($web_config['php_fpm_init_script'], $action);
259         } else {
260             $path_parts = pathinfo($init_script);
261             $initcommand = $app->system->getinitcommand($path_parts['basename'], $action, $path_parts['dirname']);
1050bd 262             
9dba8d 263             if($action == 'reload' && $init_script == $conf['init_scripts'].'/'.$web_config['php_fpm_init_script']) {
MC 264                 // we have to do a workaround because of buggy ubuntu fpm reload handling
265                 // @see: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376
1050bd 266                 if(file_exists('/etc/os-release')) {
MC 267                     $tmp = file_get_contents('/etc/os-release');
e59bb8 268                     //if(preg_match('/^ID=ubuntu/m', $tmp) && preg_match('/^VERSION_ID="14\.04"/m', $tmp)) {
TB 269                     if(preg_match('/^ID=ubuntu/m', $tmp) && preg_match('/^VERSION_ID="14\.04"/m', $tmp) && stristr(phpversion(), 'deb.sury.org') === false) {
1050bd 270                         $initcommand = '/sbin/start-stop-daemon --stop --signal USR2 --quiet --pidfile /var/run/php5-fpm.pid --name php5-fpm';
MC 271                     }
6d6643 272                     // And the next workaround, php-fpm reloads in centos 7 downt work as well.
0d1c10 273                                         /*
6d6643 274                     if(preg_match('/^ID=centos/m', $tmp) && preg_match('/^VERSION_ID="7"/m', $tmp)) {
TB 275                         $initcommand = 'systemctl restart php-fpm.service';
276                     }
0d1c10 277                                         */
6d6643 278                     unset($tmp);
TB 279                 }    
280             }
0d1c10 281             /*
6d6643 282             if($action == 'reload') {
TB 283                 // And the next workaround, php-fpm reloads in centos 7 downt work as well.
284                 if(file_exists('/etc/os-release')) {
285                     $tmp = file_get_contents('/etc/os-release');
286                     // And the next workaround, php-fpm reloads in centos 7 downt work as well.
287                     if(preg_match('/^ID="centos"/m', $tmp) && preg_match('/^VERSION_ID="7"/m', $tmp)) {
288                         $initcommand = 'systemctl restart php-fpm.service';
289                     }
1050bd 290                     unset($tmp);
MC 291                 }
292             }
0d1c10 293                         */
33bcd0 294         }
7fe908 295
615a0a 296         $retval = array('output' => '', 'retval' => 0);
33bcd0 297         exec($initcommand.' 2>&1', $retval['output'], $retval['retval']);
6d6643 298         $app->log("Restarting php-fpm: $initcommand", LOGLEVEL_DEBUG);
615a0a 299         return $retval;
e2c00a 300     }
18341e 301
T 302 } // end class
303
47f5e0 304 ?>