This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathacc.nsm_addon_updater.php
394 lines (334 loc) · 9.24 KB
/
acc.nsm_addon_updater.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
require PATH_THIRD.'nsm_addon_updater/config.php';
/**
* NSM Addon Updater Accessory
*
* @package NsmAddonUpdater
* @version 1.3.0
* @author Leevi Graham <http://leevigraham.com> - Technical Director, Newism
* @copyright Copyright (c) 2007-2012 Newism <http://newism.com.au>
* @license Commercial - please see LICENSE file included with this distribution
* @link http://ee-garage.com/nsm-example-addon
* @see http://expressionengine.com/public_beta/docs/development/accessories.html
*/
class Nsm_addon_updater_acc
{
/**
* The accessory name
*
* @var string
**/
var $name = NSM_ADDON_UPDATER_NAME;
/**
* Version
*
* @var string
**/
var $version = NSM_ADDON_UPDATER_VERSION;
/**
* Description
*
* @var string
**/
var $description = 'Accessory for NSM Addon Updater.';
/**
* Sections
*
* @var array
**/
var $sections = array();
/**
* Cache lifetime
*
* @var int
**/
var $cache_lifetime = 86400;
/**
* Is the addon in test mode
*
* @var boolean
**/
var $test_mode = false;
/**
* Hide up-to-date addons
*
* @var boolean
**/
var $hide_uptodate = false;
/**
* Hide addons that are incompatible
*
* @var boolean
**/
var $hide_incompatible = true;
/**
* The cache directory for the addon
*
* @var string
**/
var $cache_path = false;
/**
* Constructor
*
* @return void
* @author Leevi Graham
**/
function __construct()
{
$this->addon_id = $this->id = NSM_ADDON_UPDATER_ADDON_ID;
$this->cache_path = APPPATH . 'cache/' . NSM_ADDON_UPDATER_ADDON_ID;
}
/**
* Set the sections and content for the accessory
*
* @access public
* @return void
*/
function set_sections()
{
$EE =& get_instance();
$EE->cp->load_package_js("accessory_tab");
$EE->cp->load_package_css("accessory_tab");
$this->sections['Available Updates'] = $EE->load->view("/accessory/index", array(), true); ;
}
/**
* Set the sections and content for the accessory
*
* @access public
* @return void
*/
function process_ajax_feeds()
{
$EE =& get_instance();
$versions = false;
$EE->load->helper('file');
$this->_prepCacheDirectory();
if ($feeds = $this->_updateFeeds()) {
foreach ($feeds as $addon_id => $feed) {
include PATH_THIRD . '/' . $addon_id . '/config.php';
$latest_version = 0;
$data = array(
'addon_name' => $config['name'],
'installed_version' => $config['version'],
'download' => false,
'extension_class' => $addon_id,
'error' => false,
'row_class' => false,
'latest_version' => 0,
);
if(!$feed instanceof SimpleXMLElement) {
$data = array_merge($data, $feed);
$versions[$addon_id] = $data;
continue;
}
// XML from here on
$namespaces = $feed->getNameSpaces(true);
if (!empty($feed->channel->item)) {
foreach ($feed->channel->item as $version) {
$ee_addon = $version->children($namespaces['ee_addon']);
$version_number = (string) $ee_addon->version;
// version is greater than installed version
if (version_compare($version_number, $config['version'], '>=') && version_compare($version_number, $latest_version, '>')) {
$latest_version = $version_number;
$versions[$addon_id] = array_merge($data, array(
'title' => (string) $version->title,
'notes' => (string) $version->description,
'docs_url' => (string) $version->link,
'download' => false,
'created_at' => $version->pubDate,
'extension_class' => $addon_id,
'latest_version' => $version_number,
'row_class' => 'info',
));
if ($version->enclosure) {
$versions[$addon_id]['download'] = array(
'url' => (string)$version->enclosure['url'],
'type' => (string)$version->enclosure['type'],
'size' => (string)$version->enclosure['length']
);
if (isset($config['nsm_addon_updater']['custom_download_url'])) {
$versions[$addon_id]['download']['url'] = call_user_func(
$config['nsm_addon_updater']['custom_download_url'],
$versions[$addon_id]
);
}
}
}
}
}
// the search for the latest version should be complete now
if (version_compare($config['version'], $latest_version, '>=')) {
// don't hide uptodate addons? output the correct message
if (!$this->hide_uptodate) {
$versions[$addon_id] = array_merge($data, array(
'error' => 'This add-on is up-to-date',
'latest_version' => $latest_version,
'row_class' => 'success',
));
} else {
// remove uptodate addon from list
unset($versions[$addon_id]);
}
}
// clear the config
unset($config);
}
}
$EE->cp->load_package_js("accessory_tab");
$EE->cp->load_package_css("accessory_tab");
echo $EE->load->view("/accessory/updates", array('versions' => $versions), true);
exit;
}
// =======================
// = XML Feeds Functions =
// =======================
/**
* Loads all the feeds from the cache or new from the server
*
* @version 1.0.0
* @since Version 1.0.0
* @access private
* @return array An array of RSS feed XML
**/
public function _updateFeeds()
{
$EE =& get_instance();
libxml_use_internal_errors(true);
require_once PATH_THIRD . NSM_ADDON_UPDATER_ADDON_ID . "/libraries/Epicurl.php";
$sources = false;
$feeds = false;
$mc = EpiCurl::getInstance();
foreach ($EE->addons->_packages as $addon_id => $addon) {
$config_file = PATH_THIRD . '/' . $addon_id . '/config.php';
if (!file_exists($config_file)) {
continue;
}
include $config_file;
$data = false;
# Is there a file with the xml url?
if (isset($config['nsm_addon_updater']['versions_xml'])) {
$url = $config['nsm_addon_updater']['versions_xml'];
# Get the XML again if it isn't in the cache
if ($this->test_mode || ! $response = $this->_readCache(md5($url))) {
log_message('debug', "Checking for updates via CURL: {$addon_id}");
$c = false;
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
$curls[$addon_id] = $mc->addCurl($c);
$response = $curls[$addon_id]->data;
$this->_createCacheFile($response, md5($url));
// if theres an error with the curl request set an error
if (!in_array($curls[$addon_id]->code, array(200, 301, 302))) {
$data = array(
'error' => 'Could not find changelog for add-on',
'row_class' => 'error'
);
}
}
if (!isset($data['error'])) {
# If there isn't an error with the XML
try {
$xml = @simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
$data = $xml;
} catch (Exception $e) {
// problem with data
$data = false;
}
}
// data still false? mark as an error
if (!$data) {
$data = array(
'error' => "There was a problem processing the <a href='{$config['nsm_addon_updater']['versions_xml']}' target='_blank'>versions.xml</a> file for this add-on",
'row_class' => 'error'
);
}
} else {
if (!$this->hide_incompatible) {
$data = array(
'error' => 'Addon doesn\'t have a NSM Addon Updater URL',
'row_class' => ''
);
}
}
if ($data) {
$feeds[$addon_id] = $data;
}
unset($config);
}
return $feeds;
}
/**
* Creates a cache file populated with data based on a URL
*
* @version 1.0.0
* @since Version 1.0.0
* @access private
* @param $data string The data we need to cache
* @param $url string A URL used as a unique identifier
* @return void
**/
private function _createCacheFile($data, $key)
{
$filepath = $this->cache_path ."/". $key . ".txt";
$cache = write_file($filepath, $data);
if (!$cache) {
$this->throwError('Cannot create cache file');
}
}
private function _prepCacheDirectory()
{
if (!is_dir($this->cache_path)) {
if (!mkdir($this->cache_path."", 0777, true)) {
$this->throwError('Cannot create the cache directory');
}
}
if (!is_really_writable($this->cache_path)) {
$this->throwError('Cannot write to the cache directory');
}
}
private function throwError($error){
$EE =& get_instance();
$EE->cp->load_package_js("accessory_tab");
$EE->cp->load_package_css("accessory_tab");
echo $EE->load->view("/accessory/error", array('error' => $error), true);
exit;
}
/**
* Modify the download URL
*
* @version 1.0.0
* @since Version 1.0.0
* @access private
* @param $versions array
* @return array Modified versions URL
**/
private function _readCache($key)
{
$cache = false;
$filepath = $this->cache_path ."/". $key . ".txt";
$cache_timeout = $this->cache_lifetime + (rand(0,10) * 3600);
$cache = read_file($filepath);
// cache exist?
if (!$cache) {
return false;
}
// cache timed out?
if ((filemtime($filepath) + $cache_timeout) < time()) {
@unlink($filepath);
return false;
}
return $cache;
}
/**
* Modify the download URL
*
* @author your name
* @param $param
* @return return type
*/
public static function nsm_addon_updater_download_url($versions)
{
return $versions['download']['url'];
}
}