VersionChecks::checkVerPhp()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 1
dl 0
loc 24
rs 9.2222
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico\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
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
trait VersionChecks
22
{
23
    /**
24
     * Verifies XOOPS version meets minimum requirements for this module
25
     * @static
26
     *
27
     * @param \XoopsModule|null $xoopsModule
28
     * @param string|null $requiredVer
29
     * @return bool true if meets requirements, false if not
30
     */
31
    public static function checkVerXoops(
32
        ?\XoopsModule $xoopsModule = null,
33
        $requiredVer = null
34
    ) {
35
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
36
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
37
        if (null === $xoopsModule) {
38
            $xoopsModule = \XoopsModule::getByDirname($moduleDirName);
39
        }
40
        \xoops_loadLanguage('admin', $moduleDirName);
41
        \xoops_loadLanguage('common', $moduleDirName);
42
        //check for minimum XOOPS version
43
        $currentVer = mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
44
        if (null === $requiredVer) {
45
            $requiredVer = '' . $xoopsModule->getInfo('min_xoops'); //making sure it's a string
0 ignored issues
show
Bug introduced by
Are you sure $xoopsModule->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

45
            $requiredVer = '' . /** @scrutinizer ignore-type */ $xoopsModule->getInfo('min_xoops'); //making sure it's a string
Loading history...
46
        }
47
        $success = true;
48
        if (\version_compare($currentVer, $requiredVer, '<')) {
49
            $success = false;
50
            $xoopsModule->setErrors(
51
                \sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer)
52
            );
53
        }
54
55
        return $success;
56
    }
57
58
    /**
59
     * Verifies PHP version meets minimum requirements for this module
60
     * @static
61
     *
62
     * @param \XoopsModule|null $xoopsModule
63
     * @return bool true if meets requirements, false if not
64
     */
65
    public static function checkVerPhp(
66
        ?\XoopsModule $xoopsModule = null
67
    ) {
68
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
69
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
70
        if (null === $xoopsModule) {
71
            $xoopsModule = \XoopsModule::getByDirname($moduleDirName);
72
        }
73
        \xoops_loadLanguage('admin', $moduleDirName);
74
        \xoops_loadLanguage('common', $moduleDirName);
75
        // check for minimum PHP version
76
        $success = true;
77
        $verNum  = \PHP_VERSION;
78
        $reqVer  = &$xoopsModule->getInfo('min_php');
79
        if (false !== $reqVer && '' !== $reqVer && !\is_array($reqVer)) {
80
            if (\version_compare($verNum, $reqVer, '<')) {
81
                $xoopsModule->setErrors(
82
                    \sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum)
83
                );
84
                $success = false;
85
            }
86
        }
87
88
        return $success;
89
    }
90
91
    /**
92
     * compares current module version with the latest GitHub release
93
     * @static
94
     * @param \Xmf\Module\Helper $helper
95
     * @param string|null        $source
96
     * @param string|null        $default
97
     *
98
     * @return string|array info about the latest module version, if newer
99
     */
100
    public static function checkVerModule(
101
        $helper,
102
        $source = 'github',
103
        $default = 'master'
104
    ) {
105
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
106
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
107
        $update             = '';
108
        $repository         = 'XoopsModules25x/' . $moduleDirName;
109
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
110
        $ret             = '';
111
        $infoReleasesUrl = "https://api.github.com/repos/{$repository}/releases";
112
        if ('github' === $source) {
113
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
114
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
115
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
116
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true);
117
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
118
                $curlReturn = \curl_exec($curlHandle);
119
                if (false === $curlReturn) {
120
                    \trigger_error(\curl_error($curlHandle));
121
                } 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

121
                } elseif (false !== mb_strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
122
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
123
                } else {
124
                    $file              = \json_decode($curlReturn, false);
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

124
                    $file              = \json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
125
                    $latestVersionLink = \sprintf(
126
                        "https://github.com/{$repository}/archive/%s.zip",
127
                        $file ? \reset($file)->tag_name : $default
128
                    );
129
                    $latestVersion     = $file[0]->tag_name;
130
                    $prerelease        = $file[0]->prerelease;
131
                    if ('master' !== $latestVersionLink) {
132
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
133
                    }
134
                    //"PHP-standardized" version
135
                    $latestVersion = \mb_strtolower($latestVersion);
136
                    if (false !== mb_strpos($latestVersion, 'final')) {
137
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
138
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
139
                    }
140
                    $moduleVersion = $helper->getConfig('version') . '_' . $helper->getConfig(
141
                            'module_status'
142
                        );
143
                    //"PHP-standardized" version
144
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
145
                    //                    $moduleVersion = '1.0'; //for testing only
146
                    //                    $moduleDirName = 'publisher'; //for testing only
147
                    if (!$prerelease
148
                        && \version_compare(
149
                            $moduleVersion,
150
                            $latestVersion,
151
                            '<'
152
                        )) {
153
                        $ret   = [];
154
                        $ret[] = $update;
155
                        $ret[] = $latestVersionLink;
156
                    }
157
                }
158
                \curl_close($curlHandle);
159
            }
160
        }
161
162
        return $ret;
163
    }
164
}
165