forked from XoopsModules25x/modulebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionChecks.php
More file actions
146 lines (133 loc) · 6.26 KB
/
VersionChecks.php
File metadata and controls
146 lines (133 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php declare(strict_types=1);
namespace XoopsModules\Modulebuilder\Common;
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright XOOPS Project (https://xoops.org)
* @license https://www.fsf.org/copyleft/gpl.html GNU public license
* @author mamba <mambax7@gmail.com>
*/
trait VersionChecks
{
/**
* Verifies XOOPS version meets minimum requirements for this module
* @static
*
* @param \XoopsModule|null $module
* @param null|string $requiredVer
* @return bool true if meets requirements, false if not
*/
public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
{
$moduleDirName = \basename(\dirname(__DIR__, 2));
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
if (null === $module) {
$module = \XoopsModule::getByDirname($moduleDirName);
}
\xoops_loadLanguage('admin', $moduleDirName);
\xoops_loadLanguage('common', $moduleDirName);
//check for minimum XOOPS version
$currentVer = \mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
if (null === $requiredVer) {
$requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
}
$success = true;
if ($module->versionCompare($currentVer, $requiredVer)) {
$success = false;
$module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
}
return $success;
}
/**
* Verifies PHP version meets minimum requirements for this module
* @static
*
* @param \XoopsModule|null $module
* @return bool true if meets requirements, false if not
*/
public static function checkVerPhp(\XoopsModule $module = null)
{
$moduleDirName = \basename(\dirname(__DIR__, 2));
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
if (null === $module) {
$module = \XoopsModule::getByDirname($moduleDirName);
}
\xoops_loadLanguage('admin', $moduleDirName);
// check for minimum PHP version
$success = true;
$verNum = \PHP_VERSION;
$reqVer = &$module->getInfo('min_php');
if (false !== $reqVer && '' !== $reqVer) {
if (\version_compare($verNum, $reqVer, '<')) {
$module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
$success = false;
}
}
return $success;
}
/**
* compares current module version with the latest GitHub release
* @static
* @param \Xmf\Module\Helper $helper
* @param string|null $source
* @param string|null $default
*
* @return string|array info about the latest module version, if newer
*/
public static function checkVerModule(\Xmf\Module\Helper $helper, ?string $source = 'github', ?string $default = 'master'): ?array
{
$moduleDirName = \basename(\dirname(__DIR__, 2));
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
$update = '';
$repository = 'XoopsModules25x/' . $moduleDirName;
// $repository = 'XoopsModules25x/publisher'; //for testing only
$ret = null;
$infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
if ('github' === $source) {
if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
\curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
\curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
\curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, false);
\curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
$curlReturn = \curl_exec($curlHandle);
if (false === $curlReturn) {
\trigger_error(\curl_error($curlHandle));
} elseif (mb_strpos($curlReturn, 'Not Found')) {
\trigger_error('Repository Not Found: ' . $infoReleasesUrl);
} else {
$file = \json_decode($curlReturn, false);
$latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
$latestVersion = $file[0]->tag_name;
$prerelease = $file[0]->prerelease;
if ('master' !== $latestVersionLink) {
$update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
}
//"PHP-standardized" version
$latestVersion = \mb_strtolower($latestVersion);
if (false !== mb_strpos($latestVersion, 'final')) {
$latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
$latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
}
$moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
//"PHP-standardized" version
$moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
// $moduleVersion = '1.0'; //for testing only
// $moduleDirName = 'publisher'; //for testing only
if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
$ret = [];
$ret[] = $update;
$ret[] = $latestVersionLink;
}
}
\curl_close($curlHandle);
}
}
return $ret;
}
}