VersionChecks::checkVerXoops()   D
last analyzed

Complexity

Conditions 9
Paths 40

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 40
nop 2
dl 0
loc 39
rs 4.909
c 0
b 0
f 0
1
<?php namespace Xoopsmodules\instruction\common;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright   XOOPS Project (https://xoops.org)
15
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @author      mamba <[email protected]>
17
 */
18
trait VersionChecks
19
{
20
    /**
21
     *
22
     * Verifies XOOPS version meets minimum requirements for this module
23
     * @static
24
     * @param \XoopsModule|null $module
25
     *
26
     * @param null|string       $requiredVer
27
     * @return bool true if meets requirements, false if not
28
     */
29
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
30
    {
31
        $moduleDirName = basename(dirname(dirname(__DIR__)));
32
        if (null === $module) {
33
            $module = \XoopsModule::getByDirname($moduleDirName);
34
        }
35
        xoops_loadLanguage('admin', $moduleDirName);
36
        //check for minimum XOOPS version
37
        $currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string
38
        $currArray  = explode('.', $currentVer);
39
        if (null === $requiredVer) {
40
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
41
        }
42
        $reqArray = explode('.', $requiredVer);
43
        $success  = true;
44
        foreach ($reqArray as $k => $v) {
45
            if (isset($currArray[$k])) {
46
                if ($currArray[$k] > $v) {
47
                    break;
48
                } elseif ($currArray[$k] == $v) {
49
                    continue;
50
                } else {
51
                    $success = false;
52
                    break;
53
                }
54
            } else {
55
                if ((int)$v > 0) { // handles versions like x.x.x.0_RC2
56
                    $success = false;
57
                    break;
58
                }
59
            }
60
        }
61
62
        if (false === $success) {
63
            $module->setErrors(sprintf(_AM_INSTRUCTION_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
64
        }
65
66
        return $success;
67
    }
68
69
    /**
70
     *
71
     * Verifies PHP version meets minimum requirements for this module
72
     * @static
73
     * @param \XoopsModule $module
74
     *
75
     * @return bool true if meets requirements, false if not
76
     */
77
    public static function checkVerPhp(\XoopsModule $module)
78
    {
79
        xoops_loadLanguage('admin', $module->dirname());
80
        // check for minimum PHP version
81
        $success = true;
82
        $verNum  = PHP_VERSION;
83
        $reqVer  = $module->getInfo('min_php');
84
        if (false !== $reqVer && '' !== $reqVer) {
85
            if (version_compare($verNum, $reqVer, '<')) {
86
                $module->setErrors(sprintf(_AM_INSTRUCTION_ERROR_BAD_PHP, $reqVer, $verNum));
87
                $success = false;
88
            }
89
        }
90
91
        return $success;
92
    }
93
}
94