Completed
Push — master ( 2f6fab...c5b9c9 )
by Goffy
11s queued 10s
created

VersionChecks::checkVerPhp()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25

Duplication

Lines 4
Ratio 16 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 4
loc 25
rs 9.2088
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tools\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
 * @copyright   XOOPS Project (https://xoops.org)
42
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
43
 * @author      mamba <[email protected]>
44
 */
45
trait VersionChecks
46
{
47
    /**
48
     * Verifies XOOPS version meets minimum requirements for this module
49
     * @static
50
     * @param \XoopsModule $module
51
     *
52
     * @param null|string  $requiredVer
53
     * @return bool true if meets requirements, false if not
54
     */
55
    public static function checkVerXoops(XoopsModule $module = null, $requiredVer = null)
56
    {
57
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
58
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
59
        if (null === $module) {
60
            $module = XoopsModule::getByDirname($moduleDirName);
61
        }
62
        xoops_loadLanguage('admin', $moduleDirName);
63
        xoops_loadLanguage('common', $moduleDirName);
64
65
        //check for minimum XOOPS version
66
        $currentVer = mb_substr(XOOPS_VERSION, 6); // get the numeric part of string
67
        if (null === $requiredVer) {
68
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
69
        }
70
        $success = true;
71
72 View Code Duplication
        if (version_compare($currentVer, $requiredVer, '<')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            $success = false;
74
            $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
75
        }
76
77
        return $success;
78
    }
79
80
    /**
81
     * Verifies PHP version meets minimum requirements for this module
82
     * @static
83
     * @param \XoopsModule|null $module
84
     *
85
     * @return bool true if meets requirements, false if not
86
     */
87
    public static function checkVerPhp(XoopsModule $module = null)
88
    {
89
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
90
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
91
        if (null === $module) {
92
            $module = XoopsModule::getByDirname($moduleDirName);
93
        }
94
        xoops_loadLanguage('admin', $moduleDirName);
95
        xoops_loadLanguage('common', $moduleDirName);
96
97
        // check for minimum PHP version
98
        $success = true;
99
100
        $verNum = PHP_VERSION;
101
        $reqVer = &$module->getInfo('min_php');
102
103
        if (false !== $reqVer && '' !== $reqVer) {
104 View Code Duplication
            if (version_compare($verNum, $reqVer, '<')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
106
                $success = false;
107
            }
108
        }
109
110
        return $success;
111
    }
112
113
    /**
114
     *
115
     * compares current module version with latest GitHub release
116
     * @static
117
     * @param \Xmf\Module\Helper $helper
118
     * @param string|null        $source
119
     * @param string|null        $default
120
     *
121
     * @return string|array info about the latest module version, if newer
122
     */
123
124
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
125
    {
126
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
127
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
128
        $update             = '';
129
        $repository         = 'XoopsModules25x/' . $moduleDirName;
130
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
131
        $ret             = '';
132
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
133
        if ('github' === $source) {
134
            if (function_exists('curl_init') && false !== ($curlHandle = curl_init())) {
135
                curl_setopt($curlHandle, CURLOPT_URL, $infoReleasesUrl);
136
                curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
137
                curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
138
                curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
139
                $curlReturn = curl_exec($curlHandle);
140
                if (false === $curlReturn) {
141
                    trigger_error(curl_error($curlHandle));
142
                } elseif (false !== strpos($curlReturn, 'Not Found')) {
143
                    trigger_error('Repository Not Found: ' . $infoReleasesUrl);
144
                } else {
145
                    $file              = json_decode($curlReturn, false);
146
                    $latestVersionLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
147
                    $latestVersion     = $file[0]->tag_name;
148
                    $prerelease        = $file[0]->prerelease;
149
                    if ('master' !== $latestVersionLink) {
150
                        $update = constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
151
                    }
152
                    //"PHP-standardized" version
153
                    $latestVersion = mb_strtolower($latestVersion);
154
                    if (false !== mb_strpos($latestVersion, 'final')) {
155
                        $latestVersion = str_replace('_', '', mb_strtolower($latestVersion));
156
                        $latestVersion = str_replace('final', '', mb_strtolower($latestVersion));
157
                    }
158
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
159
                    //"PHP-standardized" version
160
                    $moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion));
161
                    //                    $moduleVersion = '1.0'; //for testing only
162
                    //                    $moduleDirName = 'publisher'; //for testing only
163
                    if (!$prerelease && version_compare($moduleVersion, $latestVersion, '<')) {
164
                        $ret   = [];
165
                        $ret[] = $update;
166
                        $ret[] = $latestVersionLink;
167
                    }
168
                }
169
                curl_close($curlHandle);
170
            }
171
        }
172
        return $ret;
173
    }
174
}
175