summaryrefslogtreecommitdiff
path: root/app/Core/Plugin/Directory.php
blob: 27c3514ef5079c77f8016eff07f92cc23da63213 (plain)
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
<?php

namespace Kanboard\Core\Plugin;

use Kanboard\Core\Base as BaseCore;

/**
 * Class Directory
 *
 * @package Kanboard\Core\Plugin
 * @author  Frederic Guillot
 */
class Directory extends BaseCore
{
    /**
     * Get all plugins available
     *
     * @access public
     * @param  string $url
     * @return array
     */
    public function getAvailablePlugins($url = PLUGIN_API_URL)
    {
        $plugins = $this->httpClient->getJson($url);
        $plugins = array_filter($plugins, array($this, 'isCompatible'));
        $plugins = array_filter($plugins, array($this, 'isInstallable'));
        return $plugins;
    }

    /**
     * Filter plugins
     *
     * @param array  $plugin
     * @param string $appVersion
     * @return bool
     */
    public function isCompatible(array $plugin, $appVersion = APP_VERSION)
    {
        if (strpos($appVersion, 'master') !== false) {
            return true;
        }

        foreach (array('>=', '>') as $operator) {
            if (strpos($plugin['compatible_version'], $operator) === 0) {
                $pluginVersion = substr($plugin['compatible_version'], strlen($operator));
                return version_compare($appVersion, $pluginVersion, $operator);
            }
        }

        return $plugin['compatible_version'] === $appVersion;
    }

    /**
     * Filter plugins
     *
     * @param array  $plugin
     * @return bool
     */
    public function isInstallable(array $plugin)
    {
        return $plugin['remote_install'];
    }
}