Passed
Push — master ( 6d3e95...4ad186 )
by Goffy
03:09
created

class/Common/VersionChecks.php (4 issues)

Labels
1
<?php
2
3
namespace XoopsModules\Wggithub\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(__DIR__, 2));
33
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
34
        if (null === $module) {
35
            $module = \XoopsModule::getByDirname($moduleDirName);
36
        }
37
        \xoops_loadLanguage('admin', $moduleDirName);
0 ignored issues
show
The function xoops_loadLanguage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        /** @scrutinizer ignore-call */ \xoops_loadLanguage('admin', $moduleDirName);
Loading history...
38
        \xoops_loadLanguage('common', $moduleDirName);
39
40
        //check for minimum XOOPS version
41
        //clean version text
42
        $versionText = \strtolower(\str_replace('XOOPS ', '', XOOPS_VERSION));
0 ignored issues
show
The constant XoopsModules\Wggithub\Common\XOOPS_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43
        $versionText = \str_replace(' ', '-', \trim($versionText));
44
        // get the numeric part of string
45
        $posEnd = \strpos($versionText, '-');
46
        if ($posEnd > 0) {
47
            $currentVer = \mb_substr($versionText, 0, $posEnd);
48
        } else {
49
            $currentVer = $versionText;
50
        }
51
        if (null === $requiredVer) {
52
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
53
        }
54
        $success = true;
55
56
        if (\version_compare($currentVer, $requiredVer, '<')) {
57
            $success = false;
58
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
59
        }
60
61
        return $success;
62
    }
63
64
    /**
65
     * Verifies PHP version meets minimum requirements for this module
66
     * @static
67
     *
68
     * @param \XoopsModule|null $module
69
     * @return bool true if meets requirements, false if not
70
     */
71
    public static function checkVerPhp(\XoopsModule $module = null)
72
    {
73
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
74
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
75
        if (null === $module) {
76
            $module = \XoopsModule::getByDirname($moduleDirName);
77
        }
78
        \xoops_loadLanguage('admin', $moduleDirName);
0 ignored issues
show
The function xoops_loadLanguage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        /** @scrutinizer ignore-call */ \xoops_loadLanguage('admin', $moduleDirName);
Loading history...
79
        // check for minimum PHP version
80
        $success = true;
81
82
        $verNum = PHP_VERSION;
83
        $reqVer = &$module->getInfo('min_php');
84
85
        if (false !== $reqVer && '' !== $reqVer) {
86
            if (\version_compare($verNum, $reqVer, '<')) {
87
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
88
                $success = false;
89
            }
90
        }
91
92
        return $success;
93
    }
94
95
    /**
96
     * compares current module version with latest GitHub release
97
     * @static
98
     * @param \Xmf\Module\Helper $helper
0 ignored issues
show
The type Xmf\Module\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
     * @param string|null        $source
100
     * @param string|null        $default
101
     *
102
     * @return string|array info about the latest module version, if newer
103
     */
104
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
105
    {
106
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
107
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
108
        $update             = '';
109
        $repository         = 'XoopsModules25x/' . $moduleDirName;
110
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
111
        $ret             = '';
112
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
113
        if ('github' === $source) {
114
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
115
                \curl_setopt($curlHandle, CURLOPT_URL, $infoReleasesUrl);
116
                \curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
117
                \curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
118
                \curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
119
                $curlReturn = \curl_exec($curlHandle);
120
                if (false === $curlReturn) {
121
                    \trigger_error(\curl_error($curlHandle));
122
                } elseif (mb_strpos($curlReturn, 'Not Found')) {
123
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
124
                } else {
125
                    $file              = \json_decode($curlReturn, false);
126
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
127
                    $latestVersion     = $file[0]->tag_name;
128
                    $prerelease        = $file[0]->prerelease;
129
                    if ('master' !== $latestVersionLink) {
130
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
131
                    }
132
                    //"PHP-standardized" version
133
                    $latestVersion = \mb_strtolower($latestVersion);
134
                    if (false !== mb_strpos($latestVersion, 'final')) {
135
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
136
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
137
                    }
138
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
139
                    //"PHP-standardized" version
140
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
141
                    //                    $moduleVersion = '1.0'; //for testing only
142
                    //                    $moduleDirName = 'publisher'; //for testing only
143
                    if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
144
                        $ret   = [];
145
                        $ret[] = $update;
146
                        $ret[] = $latestVersionLink;
147
                    }
148
                }
149
                \curl_close($curlHandle);
150
            }
151
        }
152
153
        return $ret;
154
    }
155
}
156