-
Notifications
You must be signed in to change notification settings - Fork 2
/
acc.devotee.php
343 lines (297 loc) · 7.57 KB
/
acc.devotee.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Devot:ee Monitor
*
* @package ExpressionEngine
* @subpackage Add-ons
* @category Accessories
* @author Visual Chefs, LLC
* @copyright Copyright (c) 2011-2012, Visual Chefs, LLC
*/
class Devotee_acc {
/**
* Accessory information
*/
public $name = 'devot:ee';
public $id = 'devot-ee';
public $version = '1.0.4';
public $description = 'Monitor your add-ons for updates.';
public $sections = array();
/**
* @var object
* @access protected
*/
protected $EE;
/**
* @var array
* @access protected
*/
protected $_addons = array();
/**
* @var string
* @access protected
*/
protected $_cache_path;
/**
* @var int
* @access protected
*/
protected $_cache_time;
/**
* @var string
* @access protected
*/
protected $theme_url;
/**
* Constructor
*/
public function __construct()
{
$this->EE =& get_instance();
// set cache settings
$this->_cache_path = APPPATH.'cache/devotee/';
$this->_cache_time = 60*60; // 1 hour
// create cache folder if it doesn't exist
if(!is_dir($this->_cache_path))
{
mkdir($this->_cache_path, 0777);
}
// set theme url
$this->theme_url = $this->EE->config->item('theme_folder_url') . 'third_party/devotee/';
}
/**
* Install accessory
*/
public function install()
{
return TRUE;
}
/**
* Update accessory
*/
public function update()
{
return TRUE;
}
/**
* Uninstall accessory
*/
public function uninstall()
{
return TRUE;
}
/**
* Set accessory sections
*/
public function set_sections()
{
$this->sections['Add-on Information'] = $this->_get_addons();
// add assets to cp
$this->EE->cp->add_to_head('<link rel="stylesheet" href="' . $this->theme_url . 'styles/accessory.css" />');
$this->EE->cp->add_to_head('<script type="text/javascript" src="' . $this->theme_url . 'scripts/accessory.js"></script>');
}
/**
* Get installed addon information
*
* @access protected
* @return string
*/
protected function _get_addons()
{
$this->EE->load->helper('file');
// load json services if not available in php
if( ! function_exists('json_decode'))
{
$this->EE->load->library('Services_json');
}
// cache file
$cache_file = $this->_cache_path.'addons';
// if cache is still good, use it
if(file_exists($cache_file) AND filemtime($cache_file) > (time() - $this->_cache_time))
{
$updates = read_file($cache_file);
}
// fetch new content if cache expired
else
{
$this->EE->load->helper('directory');
$this->EE->load->library('addons');
$this->EE->load->model('addons_model');
$this->EE->load->library('api');
// scan third_party folder
$map = directory_map(PATH_THIRD, 2);
// return if nothing found
if($map === FALSE)
{
return 'No third-party addons were found.';
}
// get fieldtypes because the addons library doesn't give all the info
$this->EE->api->instantiate('channel_fields');
$fieldtypes = $this->EE->api_channel_fields->fetch_all_fieldtypes();
// set third-party addons
$third_party = array_intersect_key($this->EE->addons->_packages, $map);
// get all installed addons
$installed = array(
'modules' => $this->EE->addons->get_installed('modules'),
'plugins' => $this->EE->addons_model->get_plugins(),
'extensions' => $this->EE->addons->get_installed('extensions'),
'fieldtypes' => $this->EE->addons->get_installed('fieldtypes'),
'accessories' => $this->EE->addons->get_installed('accessories')
);
foreach($third_party as $package => $types)
{
if(array_key_exists($package, $this->_addons))
{
continue;
}
// check for module
if(array_key_exists($package, $installed['modules']))
{
$addon = $installed['modules'][$package];
$this->_set_addon_info($package, $addon['name'], $addon['module_version'], $types);
}
// check for plugin
elseif(array_key_exists($package, $installed['plugins']))
{
$addon = $installed['plugins'][$package];
$this->_set_addon_info($package, $addon['pi_name'], $addon['pi_version'], $types);
}
// check for extension
elseif(array_key_exists($package, $installed['extensions']))
{
$addon = $installed['extensions'][$package];
$this->_set_addon_info($package, $addon['name'], $addon['version'], $types);
}
// check for fieldtype
elseif(array_key_exists($package, $installed['fieldtypes']))
{
$addon = $fieldtypes[$package];
$this->_set_addon_info($package, $addon['name'], $addon['version'], $types);
}
// check for accessory
elseif(array_key_exists($package, $installed['accessories']))
{
$addon = $installed['accessories'][$package];
// we need to load the class if not devotee to get more info
if($package != 'devotee')
{
$acc_path = PATH_THIRD.strtolower($package).'/';
if(class_exists($acc_path))
{
$this->EE->load->add_package_path($acc_path, FALSE);
$acc = new $addon['class']();
$this->EE->load->remove_package_path($acc_path);
}
}
// if devotee accessory, we already have the info!
else
{
$acc = array(
'name' => $this->name,
'version' => $this->version
);
$acc = (object) $acc;
}
if(isset($acc))
{
$this->_set_addon_info($package, $acc->name, $acc->version, $types);
}
}
}
$updates = $this->_get_updates();
write_file($cache_file, $updates);
}
// return view
return $this->EE->load->view('accessory', array(
'updates' => json_decode($updates),
'last_check' => filemtime($cache_file)
), TRUE);
}
/**
* Set addon info
*
* @param string
* @param string
* @param string
* @param array
* @access protected
*/
protected function _set_addon_info($package, $name, $version, $types)
{
$this->_addons[$package] = array(
'name' => $name,
'version' => $version,
'types' => $this->_abbreviate_types(array_keys($types))
);
}
/**
* Get update info from API
*
* @access protected
* @return string
*/
protected function _get_updates()
{
$data = array(
'data' => $this->_addons,
'site_ip' => $this->EE->input->ip_address(),
);
$ch = curl_init('http://expressionmonitor.com:8080/updates');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $this->EE->javascript->generate_json($data, TRUE),
CURLOPT_HTTPHEADER => array(
'Content-type: application/json'
)
));
$response = curl_exec($ch);
curl_close($ch);
if( ! $response)
{
$response = $this->EE->javascript->generate_json(array(
'error' => 'The API could not be reached. Try again later.'
), TRUE);
}
return $response;
}
/**
* Create an abbreviated list of add-on types, and designates whether the current add-on
* is of a particular type
*
* @param array
* @return array
* @access protected
*/
protected function _abbreviate_types($types = array())
{
$available_types = array(
'module' => 'MOD',
'extension' => 'EXT',
'plugin' => 'PLG',
'fieldtype' => 'FLD',
'accessory' => 'ACC'
);
$abbrevs = array();
foreach($available_types as $key => $abbrev)
{
$abbrevs[$abbrev] = (in_array($key, $types)) ? TRUE : FALSE;
}
return $abbrevs;
}
/**
* AJAX method for clearing cache and reloading the addons list
*/
public function process_refresh()
{
if(AJAX_REQUEST)
{
// delete cache
$this->EE->functions->delete_directory(APPPATH.'cache/devotee');
// output html from view
echo $this->_get_addons();
exit;
}
}
}
/* End of file acc.ee_monitor.php */