-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathversion_checker.php
288 lines (233 loc) · 6.53 KB
/
version_checker.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
<?php
//***********************************************************
// Master Script: JNET Version Checker
//***********************************************************
// Requirements
require_once('get_version.php');
class VersionChecker {
// Variables ----------------------------
private $exceptions = true;
private $keyword;
private $list;
private $versions;
private $actions;
public $ListOld;
public $ListUpToDate;
// Methods ------------------------------
/**
* Constructor
*
* @param string $keyword The package we want to inspect
* @param array $list The list with possible packages
* @param array $actions A list of actions to handle after checking the versions
*/
public function __construct($keyword = false, $list = array(), $actions) {
// Typecast
$this->keyword = ($keyword !== false) ? (array) $keyword : false ;
$this->list = (array) $list;
$this->actions = (array) $actions;
// Prepare
$this->versions = new stdClass();
$this->ListOld = new stdClass();
$this->ListUpToDate = new stdClass();
// Begin!
$this->Walk();
}
/**
* Method to walk through the keywords
*/
private function Walk() {
// Check Input
if (empty($this->keyword)) {
$this->keyword = $list;
}
// Walk through keywords
foreach ((array) $this->keyword as $key => $value) {
// Check if we can work with this keyword
if (!in_array($value, $this->list)) {
// Throw Exception
$this->exception('This package is not supported: '.$value);
// Return false
return false;
}
// Get current version
$Versions = new FOSSVersions($value);
$this->versions->{$value} = $Versions->version;
// Get installed versions
$installed = $this->runLookup($value);
// Compare
$this->Compare($value, $installed);
}
// Actions
$this->Actions();
}
/**
* Method to run one of the .sh files
*
* @param string $package The name of the package to look for
* @return array An array of output lines from the shell script
*/
private function runLookup($package) {
// Check if we can work with this package
if (!in_array($package, $this->list) || !file_exists($package.'.sh') || !is_file($package.'.sh')) {
// Throw Exception
$this->exception('This package is not supported: '.$package);
// Return false
return false;
}
// Run the package script
exec('./'.$package.'.sh', $output);
// Return with correct output
if (!isset($output) || empty($output)) {
// Throw Exception
$this->exception('Did not get any output from '.$package.'.sh');
// Return false
return false;
}
// Process lines
foreach ((array) $output as $key => $value) {
$parts = preg_split('/(?!^[\d\.]*)\s/i', $value);
$output[$key] = new stdClass();
$output[$key]->package = $package;
$output[$key]->version = $parts[0];
$output[$key]->path = $parts[1];
}
// Return
return $output;
}
/**
* Method to Compare a list of .sh output to the known last version
*
* @param string $current The current version name
* @param array $output A list of output from a .sh file
* @return array An array of two arrays: 'old' and 'upToDate'
*/
private function Compare($current, $output) {
// Typecast
$current = (string) $current;
$output = (array) $output;
// Get current version
$currentVersion = $this->versions->{$current};
// Prepare arrays
$this->ListOld->{$current} = array();
$this->ListUpToDate->{$current} = array();
// Walk through output
foreach ($output as $key => $value) {
// If the version is not the same as the current one, it must be older
if (!in_array($value->version, $currentVersion)) {
$this->ListOld->{$current}[] = $value;
}
else {
$this->ListUpToDate->{$current}[] = $value;
}
}
// Return
return array('old' => $this->ListOld->{$current}, 'upToDate' => $this->ListUpToDate->{$current});
}
/**
* Method to do some actions after detection
*
* @todo Emails
*/
private function Actions() {
// TODO - Email
// JSON all
if (in_array('json', $this->actions)) {
// Prepare
$data = array();
// Add up to date
foreach ($this->ListUpToDate as $key => $value) {
// Add found packages
foreach ((array) $value as $package) {
$package->uptodate = 1;
$data[] = $package;
}
}
// Add old
foreach ($this->ListOld as $key => $value) {
// Add found packages
foreach ((array) $value as $package) {
$package->uptodate = 0;
$data[] = $package;
}
}
// Encode + Output
echo json_encode($data);
}
// Display Old
if (in_array('old', $this->actions)) {
if (isset($this->ListOld) && !empty($this->ListOld)) {
// Echo title
echo '[+] Not up to date:' . "\n";
// Walk through elements
foreach ((array) $this->ListOld as $key => $value) {
echo "\t" . $value->package . "\n";
echo "\t" . '[ ' . $value->version . ' ] ' . $value->path . "\n\n";
}
}
}
// Display Up To Date
if (in_array('uptodate', $this->actions)) {
if (isset($this->ListUpToDate) && !empty($this->ListUpToDate)) {
// Echo title
echo '[+] Up to date:' . "\n";
// Walk through elements
foreach ((array) $this->ListUpToDate as $key => $value) {
echo "\t" . $value->package . "\n";
echo "\t" . '[ ' . $value->version . ' ] ' . $value->path . "\n\n";
}
}
}
}
/**
* Method to throw exceptions
*
* @param string $message The exception message to throw
*/
private function exception($message) {
// Typecast
$message = (string) $message;
if ((bool) $this->exceptions === true) {
// Throw it
throw new Exception($message);
}
}
}
// Run the class if we're not included
if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) {
// List of possible FOSS CMS'es
$list = array('drupal', 'gallery', 'joomla', 'phpbb', 'wordpress');
// Command line + arguments
if (isset($argv) && isset($argv[1])) {
// Get argv
$a = (array) $argv;
// Shift filename off the beginning of the array
array_shift($a);
// Options (nr. 1 = keyword)
$keyword = $a;
}
// HTTP GET + arguments
elseif(isset($_GET) && isset($_GET['s'])) {
// Get GET data
$keyword = (array) explode('+', $_GET['s']);
}
// Else
else {
// All
$keyword = $list;
}
// Filter keywords
foreach ((array) $keyword as $key => $value) {
if (!in_array($value, $list)) {
// Unset the value if we don't support it
unset($keyword);
}
}
// Reset array keys
$keyword = array_merge($keyword);
// Create new VersionChecker instance
$VersionChecker = new VersionChecker($keyword, $list, array('json'));
// Newline
echo "\n";
}
?>