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