Project

General

Profile

1 1420 Luisehahne
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         precheck
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id$
14
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16
 *
17
 */
18
19 1496 DarkViper
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
require_once('globalExceptionHandler.php');
22
if(!defined('WB_PATH')) { throw new IllegalFileException(); }
23
/* -------------------------------------------------------- */
24 1420 Luisehahne
function getVersion($version, $strip_suffix = true)
25
{
26
	/**
27
	 * This funtion creates a version string following the major.minor.revision convention
28
	 * The minor and revision part of the version may not exceed 999 (three digits)
29
	 * An optional suffix part can be added after revision (requires $strip_suffix = false)
30
	 *
31
	 * EXAMPLES: input --> output
32
	 *	5 --> 5.000000; 5.0 --> 5.000000; 5.0.0 --> 5.000000
33
	 * 	5.2 --> 5.002000; 5.20 --> 5.002000; 5.2.0 --> 5.002000
34
	 * 	5.21 --> 5.002001; 5.2.1 --> 5.002001;
35
	 * 	5.27.1 --> 5.027001; 5.2.71 --> 5.002071;
36
	 * 	5.27.1 rc1 --> 5.027001_RC1 ($strip_suffix:= false)
37
	 */
38
	// replace comma by decimal point
39
	$version = str_replace(',', '.', $version);
40
41
	// convert version into major.minor.revision numbering system
42
	list($major, $minor, $revision) = explode('.', $version, 3);
43
44
	// convert versioning style 5.21 into 5.2.1
45
	if ($revision == '' && strlen(intval($minor)) == 2) {
46
		$revision = substr($minor, -1);
47
		$minor = substr($minor, 0, 1);
48
	}
49
50
	// extract possible non numerical suffix from revision part (e.g. Alpha, Beta, RC1)
51
	$suffix = strtoupper(trim(substr($revision, strlen(intval($revision)))));
52
53
/*
54
	return (int)$major . '.' . sprintf('%03d', (int)$minor) . sprintf('%03d', (int)$revision) .
55
		(($strip_suffix == false && $suffix != '') ? '_' . $suffix : '');
56
*/
57
	// return standard version number (minor and revision numbers may not exceed 999)
58
    return sprintf('%d.%03d.%03d%s', (int)$major, (int)minor, (int)$revision,
59
    (($strip_suffix == false && $suffix != '') ? '_' . $suffix : ''));
60
}
61
62
/**
63
 *	As "version_compare" it self seems only got trouble
64
 *	within words like "Alpha", "Beta" a.s.o. this function
65
 *	only modify the version-string in the way that these words are replaced by values/numbers.
66
 *
67
 *	E.g:	"1.2.3 Beta2" => "1.2.322"
68
 *			"0.1.1 ALPHA" => "0.1.11"
69
 *
70
 *	Notice:	Please keep in mind, that this will not correct the way "version_control"
71
 *			handel "1 < 1.0 < 1.0.0 < 1.0.0.0" and will not correct missformed version-strings
72
 *			below 2.7, e.g. "1.002 released candidate 2.3"
73
 *
74
 *	@since	2.8.0 RC2
75
 *
76
 *	@param	string	A versionstring
77
 *	@return	string	The modificated versionstring
78
 *
79
 */
80
function getVersion2 ($version="") {
81
82
	$states = array (
83
		'1' => "alpha",
84
		'2' => "beta",
85
		'4' => "rc",
86
		'8' => "final"
87
	);
88
89
	$version = strtolower($version);
90
91
	foreach($states as $value=>$keys) $version = str_replace($keys, $value, $version);
92
93
	$version = str_replace(" ", "", $version);
94
95
	return $version;
96
}
97
98
function versionCompare($version1, $version2, $operator = '>=')
99
{
100
	/**
101
	 * This funtion performs a comparison of two provided version strings
102
	 * The versions are first converted into a string following the major.minor.revision
103
	 * convention and performs a version_compare afterwards.
104
	 */
105
	// return version_compare(getVersion($version1), getVersion($version2), $operator);
106
	return version_compare(getVersion2($version1), getVersion2($version2), $operator);
107
}
108
109
function sortPreCheckArray($precheck_array)
110
{
111
	/**
112
	 * This funtion sorts the precheck array to a common format
113
	 */
114
	// define desired precheck order
115
	$key_order = array('WB_VERSION', 'WB_ADDONS', 'PHP_VERSION', 'PHP_EXTENSIONS', 'PHP_SETTINGS', 'CUSTOM_CHECKS');
116
117
	$temp_array = array();
118
	foreach($key_order as $key) {
119
		if (!isset($precheck_array[$key])) continue;
120
		$temp_array[$key] = $precheck_array[$key];
121
	}
122
	return $temp_array;
123
}
124
125
function preCheckAddon($temp_addon_file)
126
{
127
	/**
128
	 * This funtion performs pretest upfront of the Add-On installation process.
129
	 * The requirements can be specified via the array $PRECHECK which needs to
130
	 * be defined in the optional Add-on file precheck.php.
131
	 */
132
	global $database, $admin, $TEXT, $HEADING, $MESSAGE;
133
134
	// path to the temporary Add-on folder
135
	$temp_path = WB_PATH . '/temp/unzip';
136
137
	// check if file precheck.php exists for the Add-On uploaded via WB installation routine
138
	if (!file_exists($temp_path . '/precheck.php')) return;
139
140
	// unset any previous declared PRECHECK array
141
	unset($PRECHECK);
142
143
	// include Add-On precheck.php file
144
	include($temp_path . '/precheck.php');
145
146
	// check if there are any Add-On requirements to check for
147
	if (!(isset($PRECHECK) && count($PRECHECK) > 0)) return;
148
149
	// sort precheck array
150
	$PRECHECK = sortPreCheckArray($PRECHECK);
151
152
	$failed_checks = 0;
153
	$msg = array();
154
	// check if specified addon requirements are fullfilled
155
	foreach ($PRECHECK as $key => $value) {
156
		switch ($key) {
157
			case 'WB_VERSION':
158
				if (isset($value['VERSION'])) {
159
					// obtain operator for string comparison if exist
160
					$operator = (isset($value['OPERATOR']) &&  trim($value['OPERATOR']) != '') ? $value['OPERATOR'] : '>=';
161
162
					// compare versions and extract actual status
163
					$status = versionCompare(WB_VERSION, $value['VERSION'], $operator);
164
					$msg[] = array(
165
						'check'		=> 'WB-' . $TEXT['VERSION'] .': ',
166
						'required'	=> htmlentities($operator) . $value['VERSION'],
167
						'actual'	=> WB_VERSION,
168
						'status'	=> $status
169
					);
170
171
					// increase counter if required
172
					if (!$status) $failed_checks++;
173
				}
174
				break;
175
176
			case 'WB_ADDONS':
177
				if (is_array($PRECHECK['WB_ADDONS'])) {
178
					foreach($PRECHECK['WB_ADDONS'] as $addon => $values) {
179
						if (is_array($values)) {
180
							// extract module version and operator
181
							$version = (isset($values['VERSION']) &&  trim($values['VERSION']) != '') ? $values['VERSION'] : '';
182
							$operator = (isset($values['OPERATOR']) &&  trim($values['OPERATOR']) != '') ? $values['OPERATOR'] : '>=';
183
						} else {
184
							// no version and operator specified (only check if addon exists)
185
							$addon = strip_tags($values);
186
							$version = ''; $operator = '';
187
						}
188
189
						// check if addon is listed in WB database
190
						$table = TABLE_PREFIX . 'addons';
191
						$sql = "SELECT * FROM `$table` WHERE `directory` = '" . addslashes($addon) . "'";
192
						$results = $database->query($sql);
193
194
						$status = false; $addon_status = $TEXT['NOT_INSTALLED'];
195
						if ($results && $row = $results->fetchRow()) {
196
							$status = true;
197
							$addon_status = $TEXT['INSTALLED'];
198
199
							// compare version if required
200
							if ($version != '') {
201
								$status = versionCompare($row['version'], $version, $operator);
202
								$addon_status = $row['version'];
203
							}
204
						}
205
206
						// provide addon status
207
						$msg[] = array(
208
							'check'		=> '&nbsp; ' . $TEXT['ADDON'] . ': ' . htmlentities($addon),
209
							'required'	=> ($version != '') ? $operator . '&nbsp;' . $version : $TEXT['INSTALLED'],
210
							'actual'	=> $addon_status,
211
							'status'	=> $status
212
						);
213
214
						// increase counter if required
215
						if (!$status) $failed_checks++;
216
					}
217
				}
218
				break;
219
220
			case 'PHP_VERSION':
221
				if (isset($value['VERSION'])) {
222
					// obtain operator for string comparison if exist
223
					$operator = (isset($value['OPERATOR']) &&  trim($value['OPERATOR']) != '') ? $value['OPERATOR'] : '>=';
224
225
					// compare versions and extract actual status
226
					$status = versionCompare(PHP_VERSION, $value['VERSION'], $operator);
227
					$msg[] = array(
228
						'check'		=> 'PHP-' . $TEXT['VERSION'] .': ',
229
						'required'	=> htmlentities($operator) . '&nbsp;' . $value['VERSION'],
230
						'actual'	=> PHP_VERSION,
231
						'status'	=> $status
232
					);
233
234
					// increase counter if required
235
					if (!$status) $failed_checks++;
236
237
				}
238
				break;
239
240
			case 'PHP_EXTENSIONS':
241
				if (is_array($PRECHECK['PHP_EXTENSIONS'])) {
242
					foreach($PRECHECK['PHP_EXTENSIONS'] as $extension) {
243
						$status = extension_loaded(strtolower($extension));
244
						$msg[] = array(
245
							'check'		=> '&nbsp; ' . $TEXT['EXTENSION'] . ': ' . htmlentities($extension),
246
							'required'	=> $TEXT['INSTALLED'],
247
							'actual'	=> ($status) ? $TEXT['INSTALLED'] : $TEXT['NOT_INSTALLED'],
248
							'status'	=> $status
249
						);
250
251
						// increase counter if required
252
						if (!$status) $failed_checks++;
253
					}
254
				}
255
				break;
256
257
			case 'PHP_SETTINGS':
258
				if (is_array($PRECHECK['PHP_SETTINGS'])) {
259
					foreach($PRECHECK['PHP_SETTINGS'] as $setting => $value) {
260
						$actual_setting = ($temp = ini_get($setting)) ? $temp : 0;
261
						$status = ($actual_setting == $value);
262
263
						$msg[] = array(
264
							'check'		=> '&nbsp; '. ($setting),
265
							'required'	=> $value,
266
							'actual'	=> $actual_setting,
267
							'status'	=> $status
268
						);
269
270
						// increase counter if required
271
						if (!$status) $failed_checks++;
272
					}
273
				}
274
				break;
275
276
			case 'CUSTOM_CHECKS':
277
				if (is_array($PRECHECK['CUSTOM_CHECKS'])) {
278
					foreach($PRECHECK['CUSTOM_CHECKS'] as $key => $values) {
279
						$status = (true === array_key_exists('STATUS', $values )) ? $values['STATUS'] : false;
280
						$msg[] = array(
281
							'check'		=> $key,
282
							'required'	=> $values['REQUIRED'],
283
							'actual'	=> $values['ACTUAL'],
284
							'status'	=> $status
285
						);
286
					}
287
288
					// increase counter if required
289
					if (!$status) $failed_checks++;
290
				}
291
				break;
292
		}
293
	}
294
295
	// leave if all requirements are fullfilled
296
	if ($failed_checks == 0) return;
297
298
	// output summary table with requirements not fullfilled
299
	echo <<< EOT
300
	<h2>{$HEADING['ADDON_PRECHECK_FAILED']}</h2>
301
	<p>{$MESSAGE['ADDON']['PRECHECK_FAILED']}</p>
302
303
	<table width="700px" cellpadding="4" border="0" style="margin: 0.5em; border-collapse: collapse; border: 1px solid silver;">
304
	<tr>
305
		<th>{$TEXT['REQUIREMENT']}:</th>
306
		<th>{$TEXT['REQUIRED']}:</th>
307
		<th>{$TEXT['CURRENT']}:</th>
308
	</tr>
309
EOT;
310
311
	foreach($msg as $check) {
312
		echo '<tr>';
313
		$style = $check['status'] ? 'color: #46882B;' : 'color: #C00;';
314
		foreach($check as $key => $value) {
315
			if ($key == 'status') continue;
316
317
			echo '<td style="' . $style . '">' . $value . '</td>';
318
		}
319
		echo '</tr>';
320
	}
321
	echo '</table>';
322
323
	// delete the temp unzip directory
324
	rm_full_dir($temp_path);
325
326
	// delete the temporary zip file of the Add-on
327
	if(file_exists($temp_addon_file)) { unlink($temp_addon_file); }
328
329
	// output status message and die
330
	$admin->print_error('');
331
}
332
333 925 doc
?>