Till Brehm
2014-09-22 5686edb520c31701b866708a7a64047fb01efd6d
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') {
33bcd0 215             exec($app->system->getinitcommand($daemon, 'restart').' 2>&1', $retval['output'], $retval['retval']);
7fe908 216
0711af 217         } else {
33bcd0 218             exec($app->system->getinitcommand($daemon, 'reload').' 2>&1', $retval['output'], $retval['retval']);
e2d6ed 219         }
ed7ede 220         
FT 221         // 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?)
921fd6 222         if($web_config['server_type'] == 'nginx' && $retval['retval'] == 0){
FT 223             exec('nginx -t 2>&1', $retval['output'], $retval['retval']);
ed7ede 224         }
615a0a 225         return $retval;
e2d6ed 226     }
7fe908 227
e2c00a 228     function restartPHP_FPM($action = 'restart') {
7fe908 229         global $app, $conf;
MC 230
e2c00a 231         // load the server configuration options
33bcd0 232         $app->uses('getconf,system');
e2c00a 233         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
7fe908 234
e2c00a 235         list($action, $init_script) = explode(':', $action);
7fe908 236
33bcd0 237         if(!$init_script){
FT 238             //$init_script = $conf['init_scripts'].'/'.$web_config['php_fpm_init_script'];
239             $initcommand = $app->system->getinitcommand($web_config['php_fpm_init_script'], $action);
240         } else {
241             $path_parts = pathinfo($init_script);
242             $initcommand = $app->system->getinitcommand($path_parts['basename'], $action, $path_parts['dirname']);
1050bd 243             
9dba8d 244             if($action == 'reload' && $init_script == $conf['init_scripts'].'/'.$web_config['php_fpm_init_script']) {
MC 245                 // we have to do a workaround because of buggy ubuntu fpm reload handling
246                 // @see: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1242376
1050bd 247                 if(file_exists('/etc/os-release')) {
MC 248                     $tmp = file_get_contents('/etc/os-release');
249                     if(preg_match('/^ID=ubuntu/m', $tmp) && preg_match('/^VERSION_ID="14\.04"/m', $tmp)) {
250                         $initcommand = '/sbin/start-stop-daemon --stop --signal USR2 --quiet --pidfile /var/run/php5-fpm.pid --name php5-fpm';
251                     }
252                     unset($tmp);
253                 }
254             }
33bcd0 255         }
7fe908 256
615a0a 257         $retval = array('output' => '', 'retval' => 0);
33bcd0 258         exec($initcommand.' 2>&1', $retval['output'], $retval['retval']);
615a0a 259         return $retval;
e2c00a 260     }
18341e 261
T 262 } // end class
263
47f5e0 264 ?>