VersionChecks::checkVerXoops()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 2
dl 0
loc 23
rs 9.7998
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright   XOOPS Project (https://xoops.org)
17
 * @license     GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author      mamba <[email protected]>
19
 */
20
21
use Xmf\Module\Helper;
22
23
/**
24
 * Trait to check on PHP and XOOPS version
25
 */
26
trait VersionChecks
27
{
28
    /**
29
     * Verifies XOOPS version meets minimum requirements for this module
30
     * @static
31
     * @param \XoopsModule|null $module
32
     *
33
     * @param string|null       $requiredVer
34
     * @return bool true if meets requirements, false if not
35
     */
36
    public static function checkVerXoops(\XoopsModule $module = null, string $requiredVer = null): bool
37
    {
38
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
39
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
40
        if (null === $module) {
41
            $module = \XoopsModule::getByDirname($moduleDirName);
42
        }
43
        \xoops_loadLanguage('admin', $moduleDirName);
44
        \xoops_loadLanguage('common', $moduleDirName);
45
46
        //check for minimum XOOPS version
47
        $currentVer = mb_substr((string) \XOOPS_VERSION, 6); // get the numeric part of string
48
        if (null === $requiredVer) {
49
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('min_xoops') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
50
        }
51
        $success = true;
52
53
        if (\version_compare($currentVer, $requiredVer, '<')) {
54
            $success = false;
55
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
56
        }
57
58
        return $success;
59
    }
60
61
    /**
62
     *  Verifies PHP version meets minimum requirements for this module
63
     *
64
     * @static
65
     *
66
     * @param \XoopsModule|null $module
67
     *
68
     * @return bool true if meets requirements, false if not
69
     */
70
    public static function checkVerPhp(\XoopsModule $module = null): bool
71
    {
72
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
73
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
74
        if (null === $module) {
75
            $module = \XoopsModule::getByDirname($moduleDirName);
76
        }
77
        \xoops_loadLanguage('admin', $moduleDirName);
78
        \xoops_loadLanguage('common', $moduleDirName);
79
80
        // check for minimum PHP version
81
        $success = true;
82
83
        $verNum = \PHP_VERSION;
84
        $reqVer = &$module->getInfo('min_php');
85
86
        if (false !== $reqVer && '' !== $reqVer) {
87
            if (\version_compare($verNum, $reqVer, '<')) {
0 ignored issues
show
Bug introduced by
It seems like $reqVer can also be of type array; however, parameter $version2 of version_compare() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            if (\version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
88
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_BAD_PHP'), $reqVer, $verNum));
0 ignored issues
show
Bug introduced by
It seems like $reqVer can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
89
                $success = false;
90
            }
91
        }
92
93
        return $success;
94
    }
95
96
    /**
97
     *  compares current module version with the latest GitHub release
98
     *
99
     * @static
100
     *
101
     * @param \Xmf\Module\Helper $helper
102
     * @param string|null        $source
103
     * @param string|null        $default
104
     *
105
     * @return null|string[] info about the latest module version, if newer
106
     *
107
     * @psalm-return non-empty-list<string>|null
108
     */
109
    public static function checkVerModule(Helper $helper, ?string $source = 'github', ?string $default = 'master'): ?array
110
    {
111
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
112
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
113
        $update             = '';
114
        $repository         = 'XoopsModules25x/' . $moduleDirName;
115
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
116
        $ret             = null;
117
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
118
        if ('github' === $source) {
119
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
120
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
121
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
122
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
123
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
124
                $curlReturn = \curl_exec($curlHandle);
125
                if (false === $curlReturn) {
126
                    \trigger_error(\curl_error($curlHandle));
127
                } elseif (false !== \mb_strpos($curlReturn, 'Not Found')) {
0 ignored issues
show
Bug introduced by
It seems like $curlReturn can also be of type true; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
                } elseif (false !== \mb_strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
128
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
129
                } else {
130
                    $file              = json_decode($curlReturn, false, 512, JSON_THROW_ON_ERROR);
0 ignored issues
show
Bug introduced by
It seems like $curlReturn can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
                    $file              = json_decode(/** @scrutinizer ignore-type */ $curlReturn, false, 512, JSON_THROW_ON_ERROR);
Loading history...
131
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
132
                    $latestVersion     = $file[0]->tag_name;
133
                    $prerelease        = $file[0]->prerelease;
134
                    if ('master' !== $latestVersionLink) {
135
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
136
                    }
137
                    //"PHP-standardized" version
138
                    $latestVersion = \mb_strtolower((string) $latestVersion);
139
                    if (false !== mb_strpos($latestVersion, 'final')) {
140
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
141
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
142
                    }
143
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
0 ignored issues
show
Bug introduced by
Are you sure $helper->getModule()->getInfo('module_status') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
Bug introduced by
Are you sure $helper->getModule()->getInfo('version') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
                    $moduleVersion = (/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
Loading history...
144
                    //"PHP-standardized" version
145
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
146
                    //                    $moduleVersion = '1.0'; //for testing only
147
                    //                    $moduleDirName = 'publisher'; //for testing only
148
                    if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
149
                        $ret   = [];
150
                        $ret[] = $update;
151
                        $ret[] = $latestVersionLink;
152
                    }
153
                }
154
                \curl_close($curlHandle);
155
            }
156
        }
157
158
        return $ret;
159
    }
160
}
161