-
Notifications
You must be signed in to change notification settings - Fork 2
/
domains_controller.php
215 lines (188 loc) · 7.78 KB
/
domains_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Domain Manager parent controller
*
* @link https://www.blesta.com Blesta
*/
class DomainsController extends AppController
{
/**
* Require admin to be login and setup the view
*/
public function preAction()
{
$this->structure->setDefaultView(APPDIR);
parent::preAction();
$this->requireLogin();
// Auto load language for the controller
Language::loadLang(
[Loader::fromCamelCase(get_class($this))],
null,
dirname(__FILE__) . DS . 'language' . DS
);
Language::loadLang(
'domains_controller',
null,
dirname(__FILE__) . DS . 'language' . DS
);
// If this is an admin controller, set the portal type to admin
$this->portal = 'client';
if (substr($this->controller, 0, 5) == 'admin') {
$this->portal = 'admin';
}
// Override default view directory
$this->view->view = "default";
$this->orig_structure_view = $this->structure->view;
$this->structure->view = "default";
// Restore structure view location of the admin portal
$this->structure->setDefaultView(APPDIR);
$this->structure->setView(null, $this->orig_structure_view);
// Set the left nav for all settings pages to affiliate_leftnav
if ($this->portal == 'admin') {
$this->set(
'left_nav',
$this->getLeftNav()
);
// Set the page title language term
$page_title = Loader::toCamelCase($this->controller) . '.'
. Loader::fromCamelCase($this->action ?? 'index') . '.page_title';
$this->structure->set('page_title', Language::_($page_title, true));
}
}
/**
* Get the domains left navigation bar
*
* @return string The partial view of the domains left navigation bar
*/
protected function getLeftNav()
{
Language::loadLang('admin_domains', null, PLUGINDIR . 'domains' . DS . 'language' . DS);
return $this->partial('admin_domains_leftnav', ['current_tab' => $this->controller]);
}
/**
* Gets a list of possible domain actions
*
* @return array A list of possible domain actions and their language
*/
protected function getDomainActions()
{
return [
'change_auto_renewal' => Language::_('DomainsController.getDomainActions.change_auto_renewal', true),
'change_registrar' => Language::_('DomainsController.getDomainActions.change_registrar', true),
'domain_renewal' => Language::_('DomainsController.getDomainActions.domain_renewal', true),
'update_nameservers' => Language::_('DomainsController.getDomainActions.update_nameservers', true),
'push_to_client' => Language::_('DomainsController.getDomainActions.push_to_client', true),
'unparent' => Language::_('DomainsController.getDomainActions.unparent', true)
];
}
/**
* Updates the given domains
*
* @param array $data An array of POST data including:
*
* - service_ids An array of each service ID
* - action The action to perform, e.g. "change_auto_renewal"
* @return mixed An array of errors, or false otherwise
*/
protected function updateDomains(array $data)
{
$this->uses(['Services', 'Domains.DomainsDomains']);
// Require authorization to update a client's service
if (!$this->authorized('admin_clients', 'editservice')) {
$this->flashMessage('error', Language::_('AppController.!error.unauthorized_access', true), null, false);
$this->redirect($this->base_uri . 'clients/');
}
// Only include service IDs in the list
$service_ids = [];
if (isset($data['service_ids'])) {
foreach ((array)$data['service_ids'] as $service_id) {
if (is_numeric($service_id)) {
$service_ids[] = $service_id;
}
}
}
$data['service_ids'] = $service_ids;
$data['action'] = ($data['action'] ?? null);
$errors = false;
switch ($data['action']) {
case 'change_auto_renewal':
// Schedule cancellation or remove scheduled cancellations for each service
foreach ($data['service_ids'] as $service_id) {
if (isset($data['auto_renewal']) && $data['auto_renewal'] == 'off') {
$this->Services->cancel($service_id, ['date_canceled' => 'end_of_term']);
} else {
$this->Services->unCancel($service_id);
}
if (($errors = $this->Services->errors())) {
break;
}
}
break;
case 'change_registrar':
foreach ($data['service_ids'] as $service_id) {
$this->DomainsDomains->updateRegistrar($service_id, $data['module_id'] ?? null);
if (($errors = $this->DomainsDomains->errors())) {
break;
}
}
break;
case 'domain_renewal':
foreach ($data['service_ids'] as $service_id) {
$this->DomainsDomains->renewDomain($service_id, $data['years'] ?? 1);
if (($errors = $this->DomainsDomains->errors())) {
break;
}
}
break;
case 'update_nameservers':
foreach ($data['service_ids'] as $service_id) {
$this->DomainsDomains->updateNameservers($service_id, $data['nameservers'] ?? []);
if (($errors = $this->DomainsDomains->errors())) {
break;
}
}
break;
case 'push_to_client':
foreach ($data['service_ids'] as $service_id) {
// Get service
$service = $this->Services->get($service_id);
if (!$service) {
break;
}
// Move service
$service_id = $this->Services->move($service->id, $this->post['client_id'] ?? $data['client_id']);
if (($errors = $this->Services->errors())) {
return $errors;
}
if (empty($service_id)) {
$errors = ['move' => ['error' => Language::_('DomainsController.!error.move_error', true)]];
}
}
break;
case 'unparent':
foreach ($data['service_ids'] as $service_id) {
Loader::loadModels($this, ['Services']);
// Get service
$service = $this->Services->get($service_id);
if (!$service) {
break;
}
// Skip if the service is not a child
if (empty($service->parent_service_id)) {
continue;
}
// Remove override price
$pricing = ['override_price' => null, 'override_currency' => null];
$this->Services->edit($service_id, $pricing, true);
// Remove parent service
$parent_service = ['parent_service_id' => null];
$this->Services->edit($service_id, $parent_service, true);
if (($errors = $this->Services->errors())) {
return $errors;
}
}
break;
}
return $errors;
}
}