VersionChecks::checkVerPhp()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25

Duplication

Lines 4
Ratio 16 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 4
loc 25
rs 8.8977
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Smallworld\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     http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @author      mamba <[email protected]>
19
 */
20
trait VersionChecks
21
{
22
    /**
23
     * Verifies XOOPS version meets minimum requirements for this module
24
     * @static
25
     *
26
     * @param \XoopsModule|null $module
27
     * @param null|string       $requiredVer
28
     * @return bool true if meets requirements, false if not
29
     */
30
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
31
    {
32
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
33
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
34
        if (null === $module) {
35
            $module = \XoopsModule::getByDirname($moduleDirName);
36
        }
37
        xoops_loadLanguage('admin', $moduleDirName);
38
        xoops_loadLanguage('common', $moduleDirName);
39
40
        //check for minimum XOOPS version
41
        $currentVer = mb_substr(XOOPS_VERSION, 6); // get the numeric part of string
42
        if (null === $requiredVer) {
43
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
44
        }
45
        $success = true;
46
47 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...
48
            $success = false;
49
            $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
50
        }
51
52
        return $success;
53
    }
54
55
    /**
56
     * Verifies PHP version meets minimum requirements for this module
57
     * @static
58
     *
59
     * @param \XoopsModule|null $module
60
     * @return bool true if meets requirements, false if not
61
     */
62
    public static function checkVerPhp(\XoopsModule $module = null)
63
    {
64
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
65
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
66
        if (null === $module) {
67
            $module = \XoopsModule::getByDirname($moduleDirName);
68
        }
69
        xoops_loadLanguage('admin', $moduleDirName);
70
        xoops_loadLanguage('common', $moduleDirName);
71
72
        // check for minimum PHP version
73
        $success = true;
74
75
        $verNum = PHP_VERSION;
76
        $reqVer = &$module->getInfo('min_php');
77
78
        if (false !== $reqVer && '' !== $reqVer && !is_array($reqVer)) {
79 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...
80
                $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
81
                $success = false;
82
            }
83
        }
84
85
        return $success;
86
    }
87
88
    /**
89
     * compares current module version with latest GitHub release
90
     * @static
91
     * @param \Xmf\Module\Helper $helper
92
     * @param string|null        $source
93
     * @param string|null        $default
94
     *
95
     * @return string|array info about the latest module version, if newer
96
     */
97
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
98
    {
99
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
100
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
101
        $update             = '';
102
        $repository         = 'XoopsModules25x/' . $moduleDirName;
103
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
104
        $ret             = '';
105
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
106
        if ('github' === $source) {
107
            if (function_exists('curl_init') && false !== ($curlHandle = curl_init())) {
108
                curl_setopt($curlHandle, CURLOPT_URL, $infoReleasesUrl);
109
                curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
110
                curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
111
                curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
112
                $curlReturn = curl_exec($curlHandle);
113
                if (false === $curlReturn) {
114
                    trigger_error(curl_error($curlHandle));
115
                } elseif (false !== mb_strpos($curlReturn, 'Not Found')) {
116
                    trigger_error('Repository Not Found: ' . $infoReleasesUrl);
117
                } else {
118
                    $file              = json_decode($curlReturn, false);
119
                    $latestVersionLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
120
                    $latestVersion     = $file[0]->tag_name;
121
                    $prerelease        = $file[0]->prerelease;
122
                    if ('master' !== $latestVersionLink) {
123
                        $update = constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
124
                    }
125
                    //"PHP-standardized" version
126
                    $latestVersion = mb_strtolower($latestVersion);
127
                    if (false !== mb_strpos($latestVersion, 'final')) {
128
                        $latestVersion = str_replace('_', '', mb_strtolower($latestVersion));
129
                        $latestVersion = str_replace('final', '', mb_strtolower($latestVersion));
130
                    }
131
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
132
                    //"PHP-standardized" version
133
                    $moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion));
134
                    //                    $moduleVersion = '1.0'; //for testing only
135
                    //                    $moduleDirName = 'publisher'; //for testing only
136
                    if (!$prerelease && version_compare($moduleVersion, $latestVersion, '<')) {
137
                        $ret   = [];
138
                        $ret[] = $update;
139
                        $ret[] = $latestVersionLink;
140
                    }
141
                }
142
                curl_close($curlHandle);
143
            }
144
        }
145
146
        return $ret;
147
    }
148
}
149