Passed
Pull Request — master (#81)
by Michael
02:53
created

class/Common/VersionChecks.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt\Common;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
/**
18
 * Module: Yogurt
19
 *
20
 * @category        Module
21
 * @package         yogurt
22
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 */
26
trait VersionChecks
27
{
28
    /**
29
     * Verifies XOOPS version meets minimum requirements for this module
30
     * @static
31
     *
32
     * @param \XoopsModule|null $xoopsModule
33
     * @param string|null       $requiredVer
34
     * @return bool true if meets requirements, false if not
35
     */
36
    public static function checkVerXoops(
37
        ?\XoopsModule $xoopsModule = null,
38
        $requiredVer = null
39
    ) {
40
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
41
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
42
        if (null === $xoopsModule) {
43
            $xoopsModule = XoopsModule::getByDirname($moduleDirName);
0 ignored issues
show
The type XoopsModules\Yogurt\Common\XoopsModule was not found. Did you mean XoopsModule? If so, make sure to prefix the type with \.
Loading history...
44
        }
45
        \xoops_loadLanguage('admin', $moduleDirName);
46
        \xoops_loadLanguage('common', $moduleDirName);
47
48
        //check for minimum XOOPS version
49
        $currentVer = mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
50
        if (null === $requiredVer) {
51
            $requiredVer = '' . $xoopsModule->getInfo('min_xoops'); //making sure it's a string
52
        }
53
        $success = true;
54
55
        if (\version_compare($currentVer, $requiredVer, '<')) {
56
            $success = false;
57
            $xoopsModule->setErrors(
58
                \sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer)
59
            );
60
        }
61
62
        return $success;
63
    }
64
65
    /**
66
     * Verifies PHP version meets minimum requirements for this module
67
     * @static
68
     *
69
     * @param \XoopsModule|null $xoopsModule
70
     * @return bool true if meets requirements, false if not
71
     */
72
    public static function checkVerPhp(
73
        ?\XoopsModule $xoopsModule = null
74
    ) {
75
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
76
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
77
        if (null === $xoopsModule) {
78
            $xoopsModule = XoopsModule::getByDirname($moduleDirName);
79
        }
80
        \xoops_loadLanguage('admin', $moduleDirName);
81
        \xoops_loadLanguage('common', $moduleDirName);
82
83
        // check for minimum PHP version
84
        $success = true;
85
86
        $verNum = \PHP_VERSION;
87
        $reqVer = &$xoopsModule->getInfo('min_php');
88
89
        if (false !== $reqVer && '' !== $reqVer && !\is_array($reqVer)) {
90
            if (\version_compare($verNum, $reqVer, '<')) {
91
                $xoopsModule->setErrors(
92
                    \sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum)
93
                );
94
                $success = false;
95
            }
96
        }
97
98
        return $success;
99
    }
100
101
    /**
102
     * compares current module version with latest GitHub release
103
     * @static
104
     * @param \Xmf\Module\Helper $helper
105
     * @param string|null        $source
106
     * @param string|null        $default
107
     *
108
     * @return string|array info about the latest module version, if newer
109
     */
110
    public static function checkVerModule(
111
        $helper,
112
        $source = 'github',
113
        $default = 'master'
114
    ) {
115
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
116
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
117
        $update             = '';
118
        $repository         = 'XoopsModules25x/' . $moduleDirName;
119
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
120
        $ret             = '';
121
        $infoReleasesUrl = "https://api.github.com/repos/${repository}/releases";
122
        if ('github' === $source) {
123
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
124
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
125
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
126
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true);
127
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
128
                $curlReturn = \curl_exec($curlHandle);
129
                if (false === $curlReturn) {
130
                    \trigger_error(\curl_error($curlHandle));
131
                } elseif (false !== mb_strpos($curlReturn, 'Not Found')) {
132
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
133
                } else {
134
                    $file              = \json_decode($curlReturn, false);
135
                    $latestVersionLink = \sprintf(
136
                        "https://github.com/${repository}/archive/%s.zip",
137
                        $file ? \reset($file)->tag_name : $default
138
                    );
139
                    $latestVersion     = $file[0]->tag_name;
140
                    $prerelease        = $file[0]->prerelease;
141
                    if ('master' !== $latestVersionLink) {
142
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
143
                    }
144
                    //"PHP-standardized" version
145
                    $latestVersion = mb_strtolower($latestVersion);
146
                    if (false !== mb_strpos($latestVersion, 'final')) {
147
                        $latestVersion = \str_replace('_', '', mb_strtolower($latestVersion));
148
                        $latestVersion = \str_replace('final', '', mb_strtolower($latestVersion));
149
                    }
150
                    $moduleVersion = $helper->getConfig('version') . '_' . $helper->getConfig(
151
                            'module_status'
152
                        );
153
                    //"PHP-standardized" version
154
                    $moduleVersion = \str_replace(' ', '', mb_strtolower($moduleVersion));
155
                    //                    $moduleVersion = '1.0'; //for testing only
156
                    //                    $moduleDirName = 'publisher'; //for testing only
157
                    if (!$prerelease
158
                        && \version_compare(
159
                            $moduleVersion,
160
                            $latestVersion,
161
                            '<'
162
                        )) {
163
                        $ret   = [];
164
                        $ret[] = $update;
165
                        $ret[] = $latestVersionLink;
166
                    }
167
                }
168
                \curl_close($curlHandle);
169
            }
170
        }
171
172
        return $ret;
173
    }
174
}
175