-
Notifications
You must be signed in to change notification settings - Fork 6
/
SGW_LetsEncrypt.php
321 lines (294 loc) · 9.63 KB
/
SGW_LetsEncrypt.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace iMSCP\Plugin\SGW_LetsEncrypt;
/**
* i-MSCP LetsEncrypt plugin
* Copyright (C) 2017 Cambell Prince <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
use iMSCP\Event\Event;
use iMSCP\Event\EventManagerInterface;
use iMSCP\Event\Events;
use iMSCP\Plugin\AbstractPlugin;
use iMSCP\Plugin\PluginException;
use iMSCP\Plugin\PluginManager;
use iMSCP\Registry;
/**
* Class iMSCP_Plugin_LetsEncrypt
*/
class SGW_LetsEncrypt extends AbstractPlugin
{
/**
* Plugin initialization
*
* @return void
*/
public function init()
{
l10n_addTranslations(__DIR__ . '/l10n', 'Array', $this->getName());
}
/**
* Register event listeners
*
* @param EventManagerInterface $eventsManager
* @return void
*/
public function register(EventManagerInterface $eventsManager)
{
// TODO Also sub domains? CP 2017-06
$eventsManager->registerListener(
array(
Events::onResellerScriptStart, // Don't think we need this except to enable it at all maybe CP 2017-06
Events::onClientScriptStart,
Events::onAfterDeleteDomainAlias, // Cleanup LetsEncrypt for the domain alias
Events::onAfterDeleteCustomer // Cleanup LetsEncrypt for this domain / customer
),
$this
);
}
/**
* Plugin installation
*
* @throws PluginException
* @param PluginManager $pluginManager
* @return void
*/
public function install(PluginManager $pluginManager)
{
try {
$this->migrateDb('up');
} catch (PluginException $e) {
throw new PluginException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* Plugin update
*
* @throws PluginException When update fail
* @param PluginManager $pluginManager
* @param string $fromVersion Version from which plugin update is initiated
* @param string $toVersion Version to which plugin is updated
* @return void
*/
public function update(PluginManager $pluginManager, $fromVersion, $toVersion)
{
try {
$this->migrateDb('up');
$this->clearTranslations();
// iMSCP_Registry::get('dbConfig')->del('PORT_LetsEncrypt'); // REVIEW What does this do? CP 2017-06
} catch (PluginException $e) {
throw new PluginException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* Plugin uninstallation
*
* @throws PluginException
* @param PluginManager $pluginManager
* @return void
*/
public function uninstall(PluginManager $pluginManager)
{
try {
$this->migrateDb('down');
$this->clearTranslations();
} catch (PluginException $e) {
throw new PluginException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* onResellerScriptStart event listener
*
* @return void
*/
public function onResellerScriptStart()
{
$this->setupNavigation('reseller');
}
/**
* onClientScriptStart event listener
*
* @return void
*/
public function onClientScriptStart()
{
// if (self::customerHasLetsEncrypt($_SESSION['user_id'])) { // TODO CP 2017-06
$this->setupNavigation('client');
// }
}
/**
* onAfterDeleteCustomer event listener
*
* @param Event $event
* @return void
*/
public function onAfterDeleteCustomer(Event $event)
{
// exec_query(
// 'UPDATE letsencrypt SET status = ? WHERE customer_id = ?',
// array('todelete', $event->getParam('customerId'))
// );
}
/**
* onAfterDeleteDomainAlias event listener
*
* @param Event $event
* @return void
*/
public function onAfterDeleteDomainAlias(Event $event)
{
exec_query('UPDATE letsencrypt SET status = ? WHERE alias_id = ?', array(
'todelete', $event->getParam('domainAliasId')
));
}
/**
* Get routes
*
* @return array
*/
public function getRoutes()
{
$pluginDir = $this->getPluginManager()->pluginGetRootDir() . '/' . $this->getName();
return array(
'/reseller/letsencrypt.php' => $pluginDir . '/frontend/reseller/letsencrypt.php',
'/client/letsencrypt.php' => $pluginDir . '/frontend/client/letsencrypt.php',
'/client/letsencrypt_edit.php' => $pluginDir . '/frontend/client/letsencrypt_edit.php',
'/client/test.php' => $pluginDir . '/frontend/client/test.php'
);
}
/**
* Get status of item with errors
*
* @return array
*/
public function getItemWithErrorStatus()
{
$stmt = exec_query(
"
SELECT letsencrypt_id AS item_id, cert_name AS item_name,
'letsencrypt' AS `table`, 'status' AS field
FROM letsencrypt WHERE status NOT IN(?, ?, ?, ?, ?, ?, ?)
",
array('ok', 'disabled', 'toadd', 'tochange', 'toenable', 'todisable', 'todelete')
);
if ($stmt->rowCount()) {
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
return array();
}
/**
* Set status of the given plugin item to 'tochange'
*
* @param string $table Table name
* @param string $field Status field name
* @param int $itemId LetsEncrypt item unique identifier
* @return void
*/
public function changeItemStatus($table, $field, $itemId)
{
if ($table == 'letsencrypt' && $field == 'status') {
exec_query('UPDATE letsencrypt SET `status` = ? WHERE letsencrypt_id = ?', array('tochange', $itemId));
}
}
/**
* Return count of request in progress
*
* @return int
*/
public function getCountRequests()
{
$stmt = exec_query(
'SELECT COUNT(letsencrypt_id) AS cnt FROM letsencrypt WHERE `status` IN (?, ?, ?, ?, ?)',
array('toadd', 'tochange', 'toenable', 'todisable', 'todelete')
);
$row = $stmt->fetchRow(\PDO::FETCH_ASSOC);
return $row['cnt'];
}
/**
* Does the given customer has LetsEncrypt feature activated?
*
* @param int $customerId Customer unique identifier
* @return bool
*/
public static function customerHasLetsEncrypt($customerId)
{
static $hasAccess = NULL;
return true; // TODO Currently everyone has LetsEncrypt available on their account.
// if (NULL === $hasAccess) {
// $stmt = exec_query(
// '
// SELECT COUNT(admin_id) as cnt FROM letsencrypt INNER JOIN admin USING(admin_id)
// WHERE admin_id = ? AND admin_status = ?
// ',
// array($customerId, 'ok')
// );
// $row = $stmt->fetchRow(PDO::FETCH_ASSOC);
// $hasAccess = (bool)$row['cnt'];
// }
// return $hasAccess;
}
/**
* Inject LetsEncrypt links into the navigation object
*
* @param string $level UI level
*/
protected function setupNavigation($level)
{
if (Registry::isRegistered('navigation')) {
/** @var Zend_Navigation $navigation */
$navigation = Registry::get('navigation');
if ($level == 'reseller') {
if (($page = $navigation->findOneBy('uri', '/reseller/users.php'))) {
$page->addPage(array(
'label' => tr('LetsEncrypt'),
'uri' => '/reseller/letsencrypt.php',
'title_class' => 'users',
'privilege_callback' => array(
'name' => 'resellerHasCustomers'
)
));
}
} elseif ($level == 'client') {
if (($page = $navigation->findOneBy('uri', '/client/domains_manage.php'))) {
$page->addPage(array(
'label' => tr('LetsEncrypt'),
'uri' => '/client/letsencrypt.php',
'title_class' => 'domains',
'pages' => array(
'letsencrypt_edit' => array(
'label' => tr('LetsEncrypt Edit'),
'uri' => '/client/letsencrypt_edit.php',
'title_class' => ''
)
)
));
}
}
}
}
/**
* Clear translations if any
*
* @return void
*/
protected function clearTranslations()
{
/** @var Zend_Translate $translator */
$translator = Registry::get('translator');
if ($translator->hasCache()) {
$translator->clearCache($this->getName());
}
}
}