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
2
3
namespace XoopsModules\Xlanguage\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
use XoopsModule;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
44
 * @copyright   XOOPS Project (https://xoops.org)
45
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
46
 * @author      mamba <[email protected]>
47
 */
48
trait VersionChecks
49
{
50
    /**
51
     * Verifies XOOPS version meets minimum requirements for this module
52
     * @static
53
     * @param \XoopsModule|null $module
54
     *
55
     * @param null|string       $requiredVer
56
     * @return bool true if meets requirements, false if not
57
     */
58
    public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null)
59
    {
60
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
61
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
62
        if (null === $module) {
63
            $module = XoopsModule::getByDirname($moduleDirName);
64
        }
65
        \xoops_loadLanguage('admin', $moduleDirName);
66
        \xoops_loadLanguage('common', $moduleDirName);
67
68
        //check for minimum XOOPS version
69
        $currentVer = mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
70
        if (null === $requiredVer) {
71
            $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

71
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
72
        }
73
        $success = true;
74
75
        if (\version_compare($currentVer, $requiredVer, '<')) {
76
            $success = false;
77
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
78
        }
79
80
        return $success;
81
    }
82
83
    /**
84
     * Verifies PHP version meets minimum requirements for this module
85
     * @static
86
     * @param \XoopsModule|bool|null $module
87
     *
88
     * @return bool true if meets requirements, false if not
89
     */
90
    public static function checkVerPhp(XoopsModule $module = null)
91
    {
92
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
93
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
94
        if (null === $module) {
95
            $module = XoopsModule::getByDirname($moduleDirName);
96
        }
97
        \xoops_loadLanguage('admin', $moduleDirName);
98
        \xoops_loadLanguage('common', $moduleDirName);
99
100
        // check for minimum PHP version
101
        $success = true;
102
103
        $verNum = \PHP_VERSION;
104
        $reqVer = &$module->getInfo('min_php');
105
106
        if (false !== $reqVer && '' !== $reqVer && !\is_array($reqVer)) {
107
            if (\version_compare($verNum, $reqVer, '<')) {
108
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
109
                $success = false;
110
            }
111
        }
112
113
        return $success;
114
    }
115
116
    /**
117
     *
118
     * compares current module version with latest GitHub release
119
     * @static
120
     * @param \Xmf\Module\Helper $helper
121
     * @param string|null        $source
122
     * @param string|null        $default
123
     *
124
     * @return string|array info about the latest module version, if newer
125
     */
126
127
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
128
    {
129
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
130
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
131
        $update             = '';
132
        $repository         = 'XoopsModules25x/' . $moduleDirName;
133
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
134
        $ret             = '';
135
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
136
        if ('github' === $source) {
137
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
138
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
139
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
140
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
141
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
142
                $curlReturn = \curl_exec($curlHandle);
143
                if (false === $curlReturn) {
144
                    \trigger_error(\curl_error($curlHandle));
145
                } elseif (false !== \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 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

145
                } elseif (false !== \strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
146
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
147
                } else {
148
                    $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

148
                    $file              = json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
149
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
150
                    $latestVersion     = $file[0]->tag_name;
151
                    $prerelease        = $file[0]->prerelease;
152
                    if ('master' !== $latestVersionLink) {
153
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
154
                    }
155
                    //"PHP-standardized" version
156
                    $latestVersion = mb_strtolower($latestVersion);
157
                    if (false !== mb_strpos($latestVersion, 'final')) {
158
                        $latestVersion = \str_replace('_', '', mb_strtolower($latestVersion));
159
                        $latestVersion = \str_replace('final', '', mb_strtolower($latestVersion));
160
                    }
161
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
0 ignored issues
show
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

161
                    $moduleVersion = (/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
Loading history...
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

161
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
162
                    //"PHP-standardized" version
163
                    $moduleVersion = \str_replace(' ', '', mb_strtolower($moduleVersion));
164
                    //                    $moduleVersion = '1.0'; //for testing only
165
                    //                    $moduleDirName = 'publisher'; //for testing only
166
                    if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
167
                        $ret   = [];
168
                        $ret[] = $update;
169
                        $ret[] = $latestVersionLink;
170
                    }
171
                }
172
                \curl_close($curlHandle);
173
            }
174
        }
175
        return $ret;
176
    }
177
}
178