Skip to content

Commit 62cc315

Browse files
committed
Allow plugins to be installed from GitLab
1 parent 3209434 commit 62cc315

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

public_html/lists/admin/plugins.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131
}
3232

3333
$packageurl = trim($_POST['pluginurl']);
34+
$source = pluginSource($packageurl);
3435

35-
//# verify the url against known locations, and require it to be "zip".
36-
//# let's hope Github keeps this structure for a while
37-
if (!preg_match('~^https?://github\.com/([\w\-_]+)/([\w\-_]+)/archive/(.+)\.zip$~i', $packageurl, $regs)) {
38-
echo Error(s('Invalid download URL, please reload the page and try again'));
36+
if ($source === false) {
37+
echo Error(s('Unsupported plugin source'));
38+
39+
return;
40+
}
41+
42+
if (!preg_match($source['packageRegex'], $packageurl, $regs)) {
43+
echo Error(s('Incorrect format of plugin URL'));
3944

4045
return;
4146
}
@@ -215,7 +220,8 @@
215220
$latestVersion = $plugin->checkForUpdate($pluginDetails);
216221

217222
if ($latestVersion === null) {
218-
$latestVersion = getLatestTag($pluginDetails['developer'], $pluginDetails['projectName']);
223+
$source = pluginSource($pluginDetails['installUrl']);
224+
$latestVersion = getLatestTag($source['tagUrlFormat'], $pluginDetails['developer'], $pluginDetails['projectName']);
219225
$updateAvailable = $latestVersion === null ? false : version_compare($latestVersion, $plugin->version) > 0;
220226
} else {
221227
$updateAvailable = (bool)$latestVersion;
@@ -382,20 +388,21 @@ function filterLink($filterParam, $count, $caption)
382388
}
383389

384390
/**
385-
* Query GitHub for the latest tag of a plugin.
391+
* Query for the latest tag of a plugin.
386392
* Cache the result of each query for 24 hours to limit the number of API calls.
387393
*
388394
* @link https://developer.github.com/v3/repos/#list-tags
389395
* @link https://developer.github.com/v3/#rate-limiting
390396
*
397+
* @param string $tagUrlFormat
391398
* @param string $developer
392399
* @param string $repository
393400
*
394401
* @return string|null the name of the latest tag or null when that cannot be retrieved
395402
*/
396-
function getLatestTag($developer, $repository)
403+
function getLatestTag($tagUrlFormat, $developer, $repository)
397404
{
398-
$tagUrl = "https://api.github.com/repos/$developer/$repository/tags";
405+
$tagUrl = sprintf($tagUrlFormat, $developer, $repository);
399406
$ttl = 24 * 60 * 60;
400407
$content = fetchUrl($tagUrl, array(), $ttl);
401408

@@ -410,3 +417,25 @@ function getLatestTag($developer, $repository)
410417

411418
return $tags[0]->name;
412419
}
420+
421+
function pluginSource($url)
422+
{
423+
$pluginSources = [
424+
'github.com' => [
425+
'packageRegex' => '~^https?://github\.com/([\w\-_]+)/([\w\-_]+)/archive/(.+)\.zip$~i',
426+
'tagUrlFormat' => 'https://api.github.com/repos/%s/%s/tags',
427+
],
428+
'gitlab.com' => [
429+
'packageRegex' => '~^https://gitlab\.com/([\w\-_]+)/([\w\-_]+)/-/archive/([^/]+)/[\w\-_]+\.zip$~i',
430+
'tagUrlFormat' => 'https://gitlab.com/api/v4/projects/%s%%2F%s/repository/tags',
431+
],
432+
];
433+
434+
foreach ($pluginSources as $id => $source) {
435+
if (strpos($url, $id) !== false) {
436+
return $source;
437+
}
438+
}
439+
440+
return false;
441+
}

0 commit comments

Comments
 (0)