/
UpdateCheck.php
245 lines (220 loc) · 7.44 KB
/
UpdateCheck.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
<?php
/**
* LimeSurvey
* Copyright (C) 2007-2015 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
use LimeSurvey\Menu\Menu;
/**
* Plugin to check for extension updates after a super admin logs in.
* Uses the ExtensionInstaller library.
*
* @since 2018-10-04
* @author LimeSurvey GmbH
*/
class UpdateCheck extends PluginBase
{
/**
* Where to save plugin settings etc.
* @var string
*/
protected $storage = 'DbStorage';
/** @inheritdoc, this plugin didn't have any public method */
public $allowedPublicMethods = array('checkAll');
/**
* @return void
*/
public function init()
{
$this->subscribe('afterSuccessfulLogin');
$this->subscribe('beforeControllerAction');
$this->subscribe('beforePluginManagerMenuRender');
}
/**
* After super admin log in, check date of next update check and set flag.
* @return void
*/
public function afterSuccessfulLogin()
{
if (Permission::model()->hasGlobalPermission('superadmin')) {
// NB: $nextCheck will be set to "now" if next_extension_update_check is empty.
// Hence it needs to be initialised *before* today.
$nextCheck = new DateTime($this->get('next_extension_update_check'));
$today = new DateTime("now");
if ($nextCheck <= $today) {
// Set flag.
Yii::app()->session['do_extensions_update_check'] = true;
}
}
}
/**
* If we're in an admin controller and the flag is set, render the JavaScript that
* will Ajax the checkAll() URL and push a notification.
* @return void
*/
public function beforeControllerAction()
{
$controller = $this->getEvent()->get('controller');
$doUpdateCheckFlag = Yii::app()->session['do_extensions_update_check'];
if ($controller == 'admin' && $doUpdateCheckFlag) {
// Render some JavaScript that will Ajax call update check.
$this->spitOutUrl();
$this->registerMyScript();
// Unset flag.
Yii::app()->session['do_extensions_update_check'] = false;
// Set date for next check.
$today = new DateTime("now");
$this->set('next_extension_update_check', $today->add(new DateInterval('P1D'))->format('Y-m-d H:i:s'));
}
}
/**
* @return void
*/
public function beforePluginManagerMenuRender()
{
$notificationUpdateUrl = Notification::getUpdateUrl();
$event = $this->event;
$event->append(
'extraMenus',
[
new Menu(
[
'href' => $this->getCheckUrl(),
'iconClass' => 'ri-refresh-line',
'label' => gT('Find updates'),
'tooltip' => gT('Check all extensions for available updates.'),
'onClick' => <<<JS
$("#ls-loading").show();
$.ajax(
{
url: this.href,
data: {},
method: "GET",
success: function() {
$("#ls-loading").hide();
LS.updateNotificationWidget("$notificationUpdateUrl");
},
}
);
return false;
JS
]
)
]
);
}
/**
* Used to check for available updates for all plugins.
* This method should be run at super admin login, max once every day.
* Run by Ajax to avoid increased page load time.
* This method can also be run manually for testing.
* @return void
*/
public function checkAll()
{
$service = \Yii::app()->extensionUpdaterServiceLocator;
// Get one updater class for each extension.
list($updaters, $errors) = $service->getAllUpdaters();
/** @var string[] */
$messages = [];
/** @var boolean */
$foundSecurityVersion = false;
foreach ($updaters as $updater) {
try {
$versions = $updater->fetchVersions();
if ($updater->foundSecurityVersion($versions)) {
$foundSecurityVersion = true;
}
if ($versions) {
$messages[] = $updater->getVersionMessage($versions);
}
} catch (\Throwable $ex) {
$errors[] = $updater->getExtensionName() . ': ' . $ex->getMessage();
}
}
// Compose notification.
if ($messages || $errors) {
$this->composeNotification($messages, $errors, $foundSecurityVersion);
}
}
/**
* Compose messages and errors into a nice notification message. Extra annoying if
* $foundSecurityVersion is set to true.
* @param string[] $messages
* @param string[] $errors
* @param bool $foundSecurityVersion
* @return void
*/
protected function composeNotification(array $messages, array $errors, bool $foundSecurityVersion)
{
$superadmins = User::model()->getSuperAdmins();
$title = $foundSecurityVersion ? gT('Security updates available') : gT('Updates available');
$displayClass = $foundSecurityVersion ? 'danger' : '';
$importance = $foundSecurityVersion ? Notification::HIGH_IMPORTANCE : Notification::NORMAL_IMPORTANCE;
$message = implode($messages);
if ($errors) {
$message .= '<hr/><i class="ri-alert-fil"></i> '
. gT('Errors happened during the update check. Please notify the extension authors for support.')
. '<ul>'
. '<li>' . implode('</li><li>', $errors) . '</li>';
}
UniqueNotification::broadcast(
[
'title' => $title,
'display_class' => $displayClass,
'message' => $message,
'importance' => $importance
],
$superadmins
);
}
/**
* @return void
*/
protected function spitOutUrl()
{
$url = $this->getCheckUrl();
$notificationUpdateUrl = Notification::getUpdateUrl();
$script = <<<JS
// Namespace
var LS = LS || {};
LS.plugin = LS.plugin || {};
LS.plugin.updateCheck = LS.plugin.updateCheck || {};
LS.plugin.updateCheck.url = '$url';
LS.plugin.updateCheck.notificationUpdateUrl = '$notificationUpdateUrl';
JS;
Yii::app()->clientScript->registerScript(
'updatecheckurls',
$script,
CClientScript::POS_HEAD
);
}
/**
* @return string
*/
protected function getCheckUrl()
{
return Yii::app()->createUrl(
'admin/pluginhelper',
[
'sa' => 'ajax',
'plugin' => 'updateCheck',
'method' => 'checkAll'
]
);
}
/**
* @return void
*/
protected function registerMyScript()
{
$assetsUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets/js');
Yii::app()->clientScript->registerScriptFile($assetsUrl . '/updateCheck.js');
}
}