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