commit | author | age
|
73813a
|
1 |
<?php |
MC |
2 |
/** |
|
3 |
* sites_web_vhost_domain_plugin plugin |
|
4 |
* |
|
5 |
* @author Julio Montoya <gugli100@gmail.com> BeezNest 2010 |
|
6 |
*/ |
|
7 |
|
|
8 |
|
|
9 |
class sites_web_vhost_domain_plugin { |
|
10 |
|
|
11 |
var $plugin_name = 'sites_web_vhost_domain_plugin'; |
|
12 |
var $class_name = 'sites_web_vhost_domain_plugin'; |
|
13 |
|
|
14 |
// TODO: This function is a duplicate from the one in interface/web/sites/web_domain_edit.php |
|
15 |
// There should be a single "token replacement" function to be called from modules and |
|
16 |
// from the main code. |
|
17 |
// Returna a "3/2/1" path hash from a numeric id '123' |
|
18 |
function id_hash($id, $levels) { |
|
19 |
$hash = "" . $id % 10 ; |
|
20 |
$id /= 10 ; |
|
21 |
$levels -- ; |
|
22 |
while ( $levels > 0 ) { |
|
23 |
$hash .= "/" . $id % 10 ; |
|
24 |
$id /= 10 ; |
|
25 |
$levels-- ; |
|
26 |
} |
|
27 |
return $hash; |
|
28 |
} |
|
29 |
|
|
30 |
/* |
|
31 |
This function is called when the plugin is loaded |
|
32 |
*/ |
|
33 |
function onLoad() { |
|
34 |
global $app; |
|
35 |
//Register for the events |
|
36 |
$app->plugin->registerEvent('sites:web_vhost_domain:on_after_insert', 'sites_web_vhost_domain_plugin', 'sites_web_vhost_domain_edit'); |
|
37 |
$app->plugin->registerEvent('sites:web_vhost_domain:on_after_update', 'sites_web_vhost_domain_plugin', 'sites_web_vhost_domain_edit'); |
|
38 |
} |
|
39 |
|
|
40 |
/* |
|
41 |
Function to create the sites_web_domain rule and insert it into the custom rules |
|
42 |
*/ |
|
43 |
function sites_web_vhost_domain_edit($event_name, $page_form) { |
|
44 |
global $app, $conf; |
|
45 |
|
|
46 |
$vhostdomain_type = 'domain'; |
|
47 |
if($page_form->dataRecord['type'] == 'vhostalias') $vhostdomain_type = 'aliasdomain'; |
|
48 |
elseif($page_form->dataRecord['type'] == 'vhostsubdomain') $vhostdomain_type = 'subdomain'; |
|
49 |
|
|
50 |
// make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it |
|
51 |
// also make sure that the user can not delete domain created by a admin |
|
52 |
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) { |
|
53 |
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]); |
|
54 |
$app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$page_form->id); |
|
55 |
} |
|
56 |
if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($page_form->dataRecord["client_group_id"])) { |
|
57 |
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]); |
|
58 |
$app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$page_form->id); |
|
59 |
} |
|
60 |
// Get configuration for the web system |
|
61 |
$app->uses("getconf"); |
|
62 |
$web_config = $app->getconf->get_server_config($app->functions->intval($page_form->dataRecord['server_id']), 'web'); |
|
63 |
if(isset($app->tform) && is_object($app->tform)) $web_rec = $app->tform->getDataRecord($page_form->id); |
|
64 |
else $web_rec = $app->remoting_lib->getDataRecord($page_form->id); |
|
65 |
|
|
66 |
if($vhostdomain_type == 'domain') { |
|
67 |
$document_root = str_replace("[website_id]", $page_form->id, $web_config["website_path"]); |
|
68 |
$document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root); |
|
69 |
$document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root); |
|
70 |
$document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root); |
|
71 |
$document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root); |
|
72 |
|
|
73 |
// get the ID of the client |
|
74 |
if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) { |
|
75 |
$client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]); |
|
76 |
$client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id"); |
|
77 |
$client_id = $app->functions->intval($client["client_id"]); |
|
78 |
} elseif (isset($page_form->dataRecord["client_group_id"])) { |
|
79 |
$client_group_id = $page_form->dataRecord["client_group_id"]; |
|
80 |
$client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval(@$page_form->dataRecord["client_group_id"])); |
|
81 |
$client_id = $app->functions->intval($client["client_id"]); |
|
82 |
} else { |
ebbe63
|
83 |
$client_group_id = $page_form->dataRecord["client_group_id"]; |
73813a
|
84 |
$client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($page_form->dataRecord["client_group_id"])); |
MC |
85 |
$client_id = $app->functions->intval($client["client_id"]); |
|
86 |
} |
|
87 |
|
ebbe63
|
88 |
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id"); |
MC |
89 |
$client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1); |
|
90 |
|
73813a
|
91 |
// Set the values for document_root, system_user and system_group |
MC |
92 |
$system_user = $app->db->quote('web'.$page_form->id); |
|
93 |
$system_group = $app->db->quote('client'.$client_id); |
|
94 |
|
|
95 |
$document_root = str_replace("[client_id]", $client_id, $document_root); |
|
96 |
$document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root); |
|
97 |
$document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root); |
|
98 |
$document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root); |
|
99 |
$document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root); |
|
100 |
$document_root = $app->db->quote($document_root); |
|
101 |
|
|
102 |
if($event_name == 'sites:web_vhost_domain:on_after_update') { |
|
103 |
if(($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) && isset($page_form->dataRecord["client_group_id"]) && $page_form->dataRecord["client_group_id"] != $page_form->oldDataRecord["sys_groupid"]) { |
|
104 |
|
|
105 |
$sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root' WHERE domain_id = ".$page_form->id; |
|
106 |
$app->db->query($sql); |
|
107 |
|
|
108 |
// Update the FTP user(s) too |
|
109 |
$records = $app->db->queryAllRecords("SELECT ftp_user_id FROM ftp_user WHERE parent_domain_id = ".$page_form->id); |
|
110 |
foreach($records as $rec) { |
|
111 |
$app->db->datalogUpdate('ftp_user', "sys_userid = '".$app->functions->intval($web_rec['sys_userid'])."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."', uid = '$system_user', gid = '$system_group', dir = '$document_root'", 'ftp_user_id', $app->functions->intval($rec['ftp_user_id'])); |
|
112 |
} |
|
113 |
unset($records); |
|
114 |
unset($rec); |
|
115 |
|
|
116 |
// Update the Shell user(s) too |
|
117 |
$records = $app->db->queryAllRecords("SELECT shell_user_id FROM shell_user WHERE parent_domain_id = ".$page_form->id); |
|
118 |
foreach($records as $rec) { |
|
119 |
$app->db->datalogUpdate('shell_user', "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."', puser = '$system_user', pgroup = '$system_group', dir = '$document_root'", 'shell_user_id', $app->functions->intval($rec['shell_user_id'])); |
|
120 |
} |
|
121 |
unset($records); |
|
122 |
unset($rec); |
|
123 |
|
|
124 |
//* Update all subdomains and alias domains |
|
125 |
$records = $app->db->queryAllRecords("SELECT domain_id, `domain`, `type`, `web_folder` FROM web_domain WHERE parent_domain_id = ".$page_form->id); |
|
126 |
foreach($records as $rec) { |
|
127 |
$update_columns = "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."'"; |
|
128 |
if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') { |
|
129 |
$php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$rec['web_folder'], $web_config["php_open_basedir"]); |
|
130 |
$php_open_basedir = str_replace("[website_domain]/web", $rec['domain'].'/'.$rec['web_folder'], $php_open_basedir); |
|
131 |
$php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir); |
|
132 |
$php_open_basedir = $app->db->quote(str_replace("[website_domain]", $rec['domain'], $php_open_basedir)); |
|
133 |
|
|
134 |
$update_columns .= ", document_root = '".$document_root."', `php_open_basedir` = '".$php_open_basedir."'"; |
|
135 |
} |
|
136 |
$app->db->datalogUpdate('web_domain', $update_columns, 'domain_id', $rec['domain_id']); |
|
137 |
} |
|
138 |
unset($records); |
|
139 |
unset($rec); |
|
140 |
|
|
141 |
//* Update all databases |
|
142 |
$records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$page_form->id); |
|
143 |
foreach($records as $rec) { |
|
144 |
$app->db->datalogUpdate('web_database', "sys_userid = '".$app->functions->intval($web_rec['sys_userid'])."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."'", 'database_id', $app->functions->intval($rec['database_id'])); |
|
145 |
} |
|
146 |
unset($records); |
|
147 |
unset($rec); |
|
148 |
|
|
149 |
} |
|
150 |
|
|
151 |
//* If the domain name has been changed, we will have to change all subdomains + APS instances |
|
152 |
if(!empty($page_form->dataRecord["domain"]) && !empty($page_form->oldDataRecord["domain"]) && $page_form->dataRecord["domain"] != $page_form->oldDataRecord["domain"]) { |
|
153 |
$records = $app->db->queryAllRecords("SELECT domain_id,domain FROM web_domain WHERE (type = 'subdomain' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND domain LIKE '%.".$app->db->quote($page_form->oldDataRecord["domain"])."'"); |
|
154 |
foreach($records as $rec) { |
|
155 |
$subdomain = $app->db->quote(str_replace($page_form->oldDataRecord["domain"], $page_form->dataRecord["domain"], $rec['domain'])); |
|
156 |
$app->db->datalogUpdate('web_domain', "domain = '".$subdomain."'", 'domain_id', $rec['domain_id']); |
|
157 |
} |
|
158 |
unset($records); |
|
159 |
unset($rec); |
|
160 |
unset($subdomain); |
|
161 |
|
|
162 |
// Update APS instances |
|
163 |
$records = $app->db->queryAllRecords("SELECT id, instance_id FROM aps_instances_settings WHERE name = 'main_domain' AND value = '".$app->db->quote($page_form->oldDataRecord["domain"])."'"); |
|
164 |
if(is_array($records) && !empty($records)){ |
|
165 |
foreach($records as $rec){ |
|
166 |
$app->db->datalogUpdate('aps_instances_settings', "value = '".$app->db->quote($page_form->dataRecord["domain"])."'", 'id', $rec['id']); |
|
167 |
// Reinstall of package needed? |
|
168 |
//$app->db->datalogUpdate('aps_instances', "instance_status = '1'", 'id', $rec['instance_id']); |
|
169 |
} |
|
170 |
} |
|
171 |
unset($records); |
|
172 |
unset($rec); |
|
173 |
} |
|
174 |
|
|
175 |
//* Set allow_override if empty |
|
176 |
if($web_rec['allow_override'] == '') { |
|
177 |
$sql = "UPDATE web_domain SET allow_override = '".$app->db->quote($web_config["htaccess_allow_override"])."' WHERE domain_id = ".$page_form->id; |
|
178 |
$app->db->query($sql); |
|
179 |
} |
|
180 |
|
|
181 |
//* Set php_open_basedir if empty or domain or client has been changed |
|
182 |
if(empty($web_rec['php_open_basedir']) || |
|
183 |
(!empty($page_form->dataRecord["domain"]) && !empty($page_form->oldDataRecord["domain"]) && $page_form->dataRecord["domain"] != $page_form->oldDataRecord["domain"])) { |
|
184 |
$php_open_basedir = $web_rec['php_open_basedir']; |
|
185 |
$php_open_basedir = $app->db->quote(str_replace($page_form->oldDataRecord['domain'], $web_rec['domain'], $php_open_basedir)); |
|
186 |
$sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$page_form->id; |
|
187 |
$app->db->query($sql); |
|
188 |
} |
|
189 |
if(empty($web_rec['php_open_basedir']) || |
|
190 |
(isset($page_form->dataRecord["client_group_id"]) && $page_form->dataRecord["client_group_id"] != $page_form->oldDataRecord["sys_groupid"])) { |
|
191 |
$document_root = $app->db->quote(str_replace("[client_id]", $client_id, $document_root)); |
|
192 |
$php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]); |
|
193 |
$php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir)); |
|
194 |
$sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$page_form->id; |
|
195 |
$app->db->query($sql); |
|
196 |
} |
|
197 |
|
|
198 |
//* Change database backup options when web backup options have been changed |
|
199 |
if(isset($page_form->dataRecord['backup_interval']) && ($page_form->dataRecord['backup_interval'] != $page_form->oldDataRecord['backup_interval'] || $page_form->dataRecord['backup_copies'] != $page_form->oldDataRecord['backup_copies'])) { |
|
200 |
//* Update all databases |
|
201 |
$backup_interval = $app->functions->intval($page_form->dataRecord['backup_interval']); |
|
202 |
$backup_copies = $app->functions->intval($page_form->dataRecord['backup_copies']); |
|
203 |
$records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$page_form->id); |
|
204 |
foreach($records as $rec) { |
|
205 |
$app->db->datalogUpdate('web_database', "backup_interval = '$backup_interval', backup_copies = '$backup_copies'", 'database_id', $rec['database_id']); |
|
206 |
} |
|
207 |
unset($records); |
|
208 |
unset($rec); |
|
209 |
unset($backup_copies); |
|
210 |
unset($backup_interval); |
|
211 |
} |
|
212 |
|
|
213 |
//* Change vhost subdomain and alias ip/ipv6 if domain ip/ipv6 has changed |
|
214 |
if(isset($page_form->dataRecord['ip_address']) && ($page_form->dataRecord['ip_address'] != $page_form->oldDataRecord['ip_address'] || $page_form->dataRecord['ipv6_address'] != $page_form->oldDataRecord['ipv6_address'])) { |
|
215 |
$records = $app->db->queryAllRecords("SELECT domain_id FROM web_domain WHERE (type = 'vhostsubdomain' OR type = 'vhostalias') AND parent_domain_id = ".$page_form->id); |
|
216 |
foreach($records as $rec) { |
|
217 |
$app->db->datalogUpdate('web_domain', "ip_address = '".$app->db->quote($web_rec['ip_address'])."', ipv6_address = '".$app->db->quote($web_rec['ipv6_address'])."'", 'domain_id', $rec['domain_id']); |
|
218 |
} |
|
219 |
unset($records); |
|
220 |
unset($rec); |
|
221 |
} |
|
222 |
} else { |
|
223 |
$php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]); |
|
224 |
$php_open_basedir = $app->db->quote(str_replace("[website_domain]", $page_form->dataRecord['domain'], $php_open_basedir)); |
|
225 |
|
|
226 |
$htaccess_allow_override = $app->db->quote($web_config["htaccess_allow_override"]); |
|
227 |
$sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$page_form->id; |
|
228 |
$app->db->query($sql); |
|
229 |
} |
|
230 |
} else { |
|
231 |
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = '" . $app->functions->intval($page_form->dataRecord['parent_domain_id']) . "'"); |
|
232 |
|
|
233 |
// Set the values for document_root, system_user and system_group |
|
234 |
$system_user = $app->db->quote($parent_domain['system_user']); |
|
235 |
$system_group = $app->db->quote($parent_domain['system_group']); |
|
236 |
$document_root = $app->db->quote($parent_domain['document_root']); |
|
237 |
$php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$page_form->dataRecord['web_folder'], $web_config["php_open_basedir"]); |
|
238 |
$php_open_basedir = str_replace("[website_domain]/web", $page_form->dataRecord['domain'].'/'.$page_form->dataRecord['web_folder'], $php_open_basedir); |
|
239 |
$php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir); |
|
240 |
$php_open_basedir = $app->db->quote(str_replace("[website_domain]", $page_form->dataRecord['domain'], $php_open_basedir)); |
|
241 |
$htaccess_allow_override = $app->db->quote($parent_domain['allow_override']); |
|
242 |
$sql = "UPDATE web_domain SET sys_groupid = ".$app->functions->intval($parent_domain['sys_groupid']).",system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$page_form->id; |
|
243 |
$app->db->query($sql); |
|
244 |
} |
|
245 |
} |
|
246 |
|
|
247 |
} |