Project

General

Profile

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