Project

General

Profile

wb-2_10_x / branches / main / framework / addon.precheck.inc.php @ 10

1
<?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: addon.precheck.inc.php 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource        $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/framework/addon.precheck.inc.php $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
16
 *
17
 */
18

    
19
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
if(!defined('WB_PATH')) {
22
    require_once((__DIR__).'/globalExceptionHandler.php');
23
    throw new IllegalFileException();
24
}
25
/* -------------------------------------------------------- */
26
function getVersion($version, $strip_suffix = true)
27
{
28
    /**
29
     * This funtion creates a version string following the major.minor.revision convention
30
     * The minor and revision part of the version may not exceed 999 (three digits)
31
     * An optional suffix part can be added after revision (requires $strip_suffix = false)
32
     *
33
     * EXAMPLES: input --> output
34
     *    5 --> 5.000000; 5.0 --> 5.000000; 5.0.0 --> 5.000000
35
     *     5.2 --> 5.002000; 5.20 --> 5.002000; 5.2.0 --> 5.002000
36
     *     5.21 --> 5.002001; 5.2.1 --> 5.002001;
37
     *     5.27.1 --> 5.027001; 5.2.71 --> 5.002071;
38
     *     5.27.1 rc1 --> 5.027001_RC1 ($strip_suffix:= false)
39
     */
40
    // replace comma by decimal point
41
    $version = str_replace(',', '.', $version);
42

    
43
    // convert version into major.minor.revision numbering system
44
    list($major, $minor, $revision) = explode('.', $version, 3);
45

    
46
    // convert versioning style 5.21 into 5.2.1
47
    if ($revision == '' && strlen(intval($minor)) == 2) {
48
        $revision = substr($minor, -1);
49
        $minor = substr($minor, 0, 1);
50
    }
51

    
52
    // extract possible non numerical suffix from revision part (e.g. Alpha, Beta, RC1)
53
    $suffix = strtoupper(trim(substr($revision, strlen(intval($revision)))));
54

    
55
/*
56
    return (int)$major . '.' . sprintf('%03d', (int)$minor) . sprintf('%03d', (int)$revision) .
57
        (($strip_suffix == false && $suffix != '') ? '_' . $suffix : '');
58
*/
59
    // return standard version number (minor and revision numbers may not exceed 999)
60
    return sprintf('%d.%03d.%03d%s', (int)$major, (int)minor, (int)$revision,
61
    (($strip_suffix == false && $suffix != '') ? '_' . $suffix : ''));
62
}
63

    
64
/**
65
 *    As "version_compare" it self seems only got trouble
66
 *    within words like "Alpha", "Beta" a.s.o. this function
67
 *    only modify the version-string in the way that these words are replaced by values/numbers.
68
 *
69
 *    E.g:    "1.2.3 Beta2" => "1.2.322"
70
 *            "0.1.1 ALPHA" => "0.1.11"
71
 *
72
 *    Notice:    Please keep in mind, that this will not correct the way "version_control"
73
 *            handel "1 < 1.0 < 1.0.0 < 1.0.0.0" and will not correct missformed version-strings
74
 *            below 2.7, e.g. "1.002 released candidate 2.3"
75
 *
76
 *    @since    2.8.0 RC2
77
 *
78
 *    @param    string    A versionstring
79
 *    @return    string    The modificated versionstring
80
 *
81
 */
82
function getVersion2 ($version="") {
83

    
84
    $states = array (
85
        '1' => "alpha",
86
        '2' => "beta",
87
        '4' => "rc",
88
        '8' => "final"
89
    );
90

    
91
    $version = strtolower($version);
92

    
93
    foreach($states as $value=>$keys) $version = str_replace($keys, $value, $version);
94

    
95
    $version = str_replace(" ", "", $version);
96

    
97
    return $version;
98
}
99

    
100
function versionCompare($version1, $version2, $operator = '>=')
101
{
102
    /**
103
     * This funtion performs a comparison of two provided version strings
104
     * The versions are first converted into a string following the major.minor.revision
105
     * convention and performs a version_compare afterwards.
106
     */
107
    // return version_compare(getVersion($version1), getVersion($version2), $operator);
108
    return version_compare(getVersion2($version1), getVersion2($version2), $operator);
109
}
110

    
111
function sortPreCheckArray($precheck_array)
112
{
113
    /**
114
     * This funtion sorts the precheck array to a common format
115
     */
116
    // define desired precheck order
117
    $key_order = array('WB_VERSION', 'WB_ADDONS', 'PHP_VERSION', 'PHP_EXTENSIONS', 'PHP_SETTINGS', 'CUSTOM_CHECKS');
118

    
119
    $temp_array = array();
120
    foreach($key_order as $key) {
121
        if (!isset($precheck_array[$key])) continue;
122
        $temp_array[$key] = $precheck_array[$key];
123
    }
124
    return $temp_array;
125
}
126

    
127
function preCheckAddon($temp_addon_file)
128
{
129
    /**
130
     * This funtion performs pretest upfront of the Add-On installation process.
131
     * The requirements can be specified via the array $PRECHECK which needs to
132
     * be defined in the optional Add-on file precheck.php.
133
     */
134
    global $database, $admin, $TEXT, $HEADING, $MESSAGE;
135

    
136
    // path to the temporary Add-on folder
137
    $temp_path = WB_PATH . '/temp/unzip';
138

    
139
    // check if file precheck.php exists for the Add-On uploaded via WB installation routine
140
    if (!file_exists($temp_path . '/precheck.php')) return;
141

    
142
    // unset any previous declared PRECHECK array
143
    unset($PRECHECK);
144

    
145
    // include Add-On precheck.php file
146
    include($temp_path . '/precheck.php');
147

    
148
    // check if there are any Add-On requirements to check for
149
    if (!(isset($PRECHECK) && count($PRECHECK) > 0)) return;
150

    
151
    // sort precheck array
152
    $PRECHECK = sortPreCheckArray($PRECHECK);
153

    
154
    $failed_checks = 0;
155
    $msg = array();
156
    // check if specified addon requirements are fullfilled
157
    foreach ($PRECHECK as $key => $value) {
158
        switch ($key) {
159
            case 'WB_VERSION':
160
                if (isset($value['VERSION'])) {
161
                    // obtain operator for string comparison if exist
162
                    $operator = (isset($value['OPERATOR']) &&  trim($value['OPERATOR']) != '') ? $value['OPERATOR'] : '>=';
163

    
164
                    // compare versions and extract actual status
165
                    $status = versionCompare(WB_VERSION, $value['VERSION'], $operator);
166
                    $msg[] = array(
167
                        'check'        => 'WB-' . $TEXT['VERSION'] .': ',
168
                        'required'    => htmlentities($operator) . $value['VERSION'],
169
                        'actual'    => WB_VERSION,
170
                        'status'    => $status
171
                    );
172

    
173
                    // increase counter if required
174
                    if (!$status) $failed_checks++;
175
                }
176
                break;
177

    
178
            case 'WB_ADDONS':
179
                if (is_array($PRECHECK['WB_ADDONS'])) {
180
                    foreach($PRECHECK['WB_ADDONS'] as $addon => $values) {
181
                        if (is_array($values)) {
182
                            // extract module version and operator
183
                            $version = (isset($values['VERSION']) &&  trim($values['VERSION']) != '') ? $values['VERSION'] : '';
184
                            $operator = (isset($values['OPERATOR']) &&  trim($values['OPERATOR']) != '') ? $values['OPERATOR'] : '>=';
185
                        } else {
186
                            // no version and operator specified (only check if addon exists)
187
                            $addon = strip_tags($values);
188
                            $version = ''; $operator = '';
189
                        }
190

    
191
                        // check if addon is listed in WB database
192
                        $table = TABLE_PREFIX . 'addons';
193
                        $sql = "SELECT * FROM `$table` WHERE `directory` = '" . $database->escapeString($addon) . "'";
194
                        $results = $database->query($sql);
195

    
196
                        $status = false; $addon_status = $TEXT['NOT_INSTALLED'];
197
                        if ($results && $row = $results->fetchRow()) {
198
                            $status = true;
199
                            $addon_status = $TEXT['INSTALLED'];
200

    
201
                            // compare version if required
202
                            if ($version != '') {
203
                                $status = versionCompare($row['version'], $version, $operator);
204
                                $addon_status = $row['version'];
205
                            }
206
                        }
207

    
208
                        // provide addon status
209
                        $msg[] = array(
210
                            'check'        => '&nbsp; ' . $TEXT['ADDON'] . ': ' . htmlentities($addon),
211
                            'required'    => ($version != '') ? $operator . '&nbsp;' . $version : $TEXT['INSTALLED'],
212
                            'actual'    => $addon_status,
213
                            'status'    => $status
214
                        );
215

    
216
                        // increase counter if required
217
                        if (!$status) $failed_checks++;
218
                    }
219
                }
220
                break;
221

    
222
            case 'PHP_VERSION':
223
                if (isset($value['VERSION'])) {
224
                    // obtain operator for string comparison if exist
225
                    $operator = (isset($value['OPERATOR']) &&  trim($value['OPERATOR']) != '') ? $value['OPERATOR'] : '>=';
226

    
227
                    // compare versions and extract actual status
228
                    $status = versionCompare(PHP_VERSION, $value['VERSION'], $operator);
229
                    $msg[] = array(
230
                        'check'        => 'PHP-' . $TEXT['VERSION'] .': ',
231
                        'required'    => htmlentities($operator) . '&nbsp;' . $value['VERSION'],
232
                        'actual'    => PHP_VERSION,
233
                        'status'    => $status
234
                    );
235

    
236
                    // increase counter if required
237
                    if (!$status) $failed_checks++;
238

    
239
                }
240
                break;
241

    
242
            case 'PHP_EXTENSIONS':
243
                if (is_array($PRECHECK['PHP_EXTENSIONS'])) {
244
                    foreach($PRECHECK['PHP_EXTENSIONS'] as $extension) {
245
                        $status = extension_loaded(strtolower($extension));
246
                        $msg[] = array(
247
                            'check'        => '&nbsp; ' . $TEXT['EXTENSION'] . ': ' . htmlentities($extension),
248
                            'required'    => $TEXT['INSTALLED'],
249
                            'actual'    => ($status) ? $TEXT['INSTALLED'] : $TEXT['NOT_INSTALLED'],
250
                            'status'    => $status
251
                        );
252

    
253
                        // increase counter if required
254
                        if (!$status) $failed_checks++;
255
                    }
256
                }
257
                break;
258

    
259
            case 'PHP_SETTINGS':
260
                if (is_array($PRECHECK['PHP_SETTINGS'])) {
261
                    foreach($PRECHECK['PHP_SETTINGS'] as $setting => $value) {
262
                        $actual_setting = ($temp = ini_get($setting)) ? $temp : 0;
263
                        $status = ($actual_setting == $value);
264

    
265
                        $msg[] = array(
266
                            'check'        => '&nbsp; '. ($setting),
267
                            'required'    => $value,
268
                            'actual'    => $actual_setting,
269
                            'status'    => $status
270
                        );
271

    
272
                        // increase counter if required
273
                        if (!$status) $failed_checks++;
274
                    }
275
                }
276
                break;
277

    
278
            case 'CUSTOM_CHECKS':
279
                if (is_array($PRECHECK['CUSTOM_CHECKS'])) {
280
                    foreach($PRECHECK['CUSTOM_CHECKS'] as $key => $values) {
281
                        $status = (true === array_key_exists('STATUS', $values )) ? $values['STATUS'] : false;
282
                        $msg[] = array(
283
                            'check'        => $key,
284
                            'required'    => $values['REQUIRED'],
285
                            'actual'    => $values['ACTUAL'],
286
                            'status'    => $status
287
                        );
288
                    }
289

    
290
                    // increase counter if required
291
                    if (!$status) $failed_checks++;
292
                }
293
                break;
294
        }
295
    }
296

    
297
    // leave if all requirements are fullfilled
298
    if ($failed_checks == 0) return;
299

    
300
    // output summary table with requirements not fullfilled
301
    echo <<< EOT
302
    <h2>{$HEADING['ADDON_PRECHECK_FAILED']}</h2>
303
    <p>{$MESSAGE['ADDON']['PRECHECK_FAILED']}</p>
304

    
305
    <table width="700px" cellpadding="4" border="0" style="margin: 0.5em; border-collapse: collapse; border: 1px solid silver;">
306
    <tr>
307
        <th>{$TEXT['REQUIREMENT']}:</th>
308
        <th>{$TEXT['REQUIRED']}:</th>
309
        <th>{$TEXT['CURRENT']}:</th>
310
    </tr>
311
EOT;
312

    
313
    foreach($msg as $check) {
314
        echo '<tr>';
315
        $style = $check['status'] ? 'color: #46882B;' : 'color: #C00;';
316
        foreach($check as $key => $value) {
317
            if ($key == 'status') continue;
318

    
319
            echo '<td style="' . $style . '">' . $value . '</td>';
320
        }
321
        echo '</tr>';
322
    }
323
    echo '</table>';
324

    
325
    // delete the temp unzip directory
326
    rm_full_dir($temp_path);
327

    
328
    // delete the temporary zip file of the Add-on
329
    if(file_exists($temp_addon_file)) { unlink($temp_addon_file); }
330

    
331
    // output status message and die
332
    $admin->print_error('');
333
}
334

    
335
?>