VersionChecks   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 130
rs 10
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkVerModule() 0 56 11
A checkVerPhp() 0 22 6
A checkVerXoops() 0 23 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News\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
trait VersionChecks
21
{
22
    /**
23
     * Verifies XOOPS version meets minimum requirements for this module
24
     * @static
25
     * @param \XoopsModule|null $module
26
     * @param null|string  $requiredVer
27
     * @return bool true if meets requirements, false if not
28
     */
29
    public static function checkVerXoops(?\XoopsModule $module, string $requiredVer = null): bool
30
    {
31
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
32
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
33
        if (null === $module) {
34
            $module = \XoopsModule::getByDirname($moduleDirName);
35
        }
36
        \xoops_loadLanguage('admin', $moduleDirName);
37
        \xoops_loadLanguage('common', $moduleDirName);
38
39
        //check for minimum XOOPS version
40
        $currentVer = mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
41
        if (null === $requiredVer) {
42
            $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

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

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

134
                                         ->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()
Loading history...
135
                                                                             ->getInfo('module_status'));
136
                //"PHP-standardized" version
137
                $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
138
                //                    $moduleVersion = '1.0'; //for testing only
139
                //                    $moduleDirName = 'publisher'; //for testing only
140
                if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
141
                    $ret   = [];
142
                    $ret[] = $update;
143
                    $ret[] = $latestVersionLink;
144
                }
145
            }
146
            \curl_close($curlHandle);
147
        }
148
149
        return $ret;
150
    }
151
}
152