-
Notifications
You must be signed in to change notification settings - Fork 988
/
UpdateCheck.php
151 lines (134 loc) · 4.61 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
<?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.
*/
/**
* Plugin to check for extension updates after a super admin logs in.
* Uses the ExtensionInstaller library.
*
* @since 2018-10-04
* @author Olle Haerstedt
*/
class UpdateCheck extends PluginBase
{
/**
* Where to save plugin settings etc.
* @var string
*/
protected $storage = 'DbStorage';
/**
* @return void
*/
public function init()
{
$this->subscribe('afterSuccessfulLogin');
$this->subscribe('beforeControllerAction');
}
/**
* @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;
}
}
}
/**
* @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->registerScript();
// 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'));
}
}
/**
* 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.
* @return void
*/
public function checkAll()
{
$service = \Yii::app()->extensionUpdaterServiceLocator;
// Get one updater class for each extension type (PluginUpdater, ThemeUpdater, etc).
list($updaters, $errors) = $service->getAllUpdaters();
/** @var string[] */
$messages = [];
foreach ($updaters as $updater) {
try {
list($extensionName, $extensionType, $availableVersions) = $updater->getAvailableUpdates();
if ($availableVersions) {
$messages[] = sprintf(
gT('There are updates available for %s %s, new version number(s): %s.'),
$extensionType,
$extensionName,
implode(', ', $availableVersions)
);
}
} catch (\Throwable $ex) {
$errors[] = $ex->getMessage();
}
}
if ($messages) {
$superadmins = User::model()->getSuperAdmins();
UniqueNotification::broadcast(
[
'title' => gT('Updates available'),
'message' => implode('<br/>', $messages) . '<br/>' . implode('<br/>', $errors)
],
$superadmins
);
}
}
/**
* @return void
*/
protected function spitOutUrl()
{
$data = [
'url' => Yii::app()->createUrl(
'admin/pluginhelper',
array(
'sa' => 'ajax',
'plugin' => 'updateCheck',
'method' => 'checkAll'
)
),
'notificationUpdateUrl' => Notification::getUpdateUrl()
];
echo $this->api->renderTwig(__DIR__ . '/views/index.twig', $data);
}
/**
* @return void
*/
protected function registerScript()
{
$assetsUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets/js');
Yii::app()->clientScript->registerScriptFile($assetsUrl . '/updateCheck.js');
}
}