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
|
|
|
* @param \XoopsModule|null $module |
28
|
|
|
* |
29
|
|
|
* @param null|string $requiredVer |
30
|
|
|
* @return bool true if meets requirements, false if not |
31
|
|
|
*/ |
32
|
|
|
public static function checkVerXoops(?\XoopsModule $module = null, ?string $requiredVer = null): bool |
33
|
|
|
{ |
34
|
|
|
$moduleDirName = \basename(\dirname(__DIR__, 2)); |
35
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
36
|
|
|
if (null === $module) { |
37
|
|
|
$module = XoopsModule::getByDirname($moduleDirName); |
38
|
|
|
} |
39
|
|
|
\xoops_loadLanguage('admin', $moduleDirName); |
40
|
|
|
\xoops_loadLanguage('common', $moduleDirName); |
41
|
|
|
|
42
|
|
|
//check for minimum XOOPS version |
43
|
|
|
$currentVer = mb_substr((string) \XOOPS_VERSION, 6); // get the numeric part of string |
44
|
|
|
if (null === $requiredVer) { |
45
|
|
|
$requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string |
|
|
|
|
46
|
|
|
} |
47
|
|
|
$success = true; |
48
|
|
|
|
49
|
|
|
if ($module->versionCompare($currentVer, $requiredVer, '<')) { |
50
|
|
|
$success = false; |
51
|
|
|
$module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_BAD_XOOPS'), $requiredVer, $currentVer)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $success; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Verifies PHP version meets minimum requirements for this module |
59
|
|
|
* @static |
60
|
|
|
* @param \XoopsModule|bool|null $module |
61
|
|
|
* |
62
|
|
|
* @return bool true if meets requirements, false if not |
63
|
|
|
*/ |
64
|
|
|
public static function checkVerPhp(?XoopsModule $module = null) |
65
|
|
|
{ |
66
|
|
|
$moduleDirName = \basename(\dirname(__DIR__, 2)); |
67
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
68
|
|
|
if (null === $module) { |
69
|
|
|
$module = XoopsModule::getByDirname($moduleDirName); |
70
|
|
|
} |
71
|
|
|
\xoops_loadLanguage('admin', $moduleDirName); |
72
|
|
|
\xoops_loadLanguage('common', $moduleDirName); |
73
|
|
|
|
74
|
|
|
// check for minimum PHP version |
75
|
|
|
$success = true; |
76
|
|
|
|
77
|
|
|
$verNum = \PHP_VERSION; |
78
|
|
|
$reqVer = &$module->getInfo('min_php'); |
79
|
|
|
|
80
|
|
|
if (false !== $reqVer && '' !== $reqVer) { |
81
|
|
|
if ($module->versionCompare($verNum, $reqVer, '<')) { |
|
|
|
|
82
|
|
|
$module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_BAD_PHP'), $reqVer, $verNum)); |
|
|
|
|
83
|
|
|
$success = false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $success; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* compares current module version with the latest GitHub release |
92
|
|
|
* @static |
93
|
|
|
* |
94
|
|
|
* @return string|array info about the latest module version, if newer |
95
|
|
|
*/ |
96
|
|
|
public static function checkVerModule(\Xmf\Module\Helper $helper, ?string $source = 'github', ?string $default = 'master'): ?array |
97
|
|
|
{ |
98
|
|
|
$moduleDirName = \basename(\dirname(__DIR__, 2)); |
99
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
100
|
|
|
$module = $helper->getModule(); |
101
|
|
|
$update = ''; |
102
|
|
|
$repository = 'XoopsModules25x/' . $moduleDirName; |
103
|
|
|
// $repository = 'XoopsModules25x/publisher'; //for testing only |
104
|
|
|
$ret = null; |
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); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized' |
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, 512, \JSON_THROW_ON_ERROR); |
|
|
|
|
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((string) $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 && $module->versionCompare($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
|
|
|
|