Completed
Push — master ( 204b42...65a2ef )
by Michael
03:03
created

Userlog_Module_Plugin::getPlugins()   D

Complexity

Conditions 9
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 32
rs 4.909
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       XOOPS Project (http://xoops.org)
14
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author          trabis <[email protected]>
16
 */
17
// irmtfan copy from xoops26 xoops_lib/Xoops/Module/plugin.php class
18
// change XoopsLoad -> self
19
// change $xoops -> $GLOBALS['xoops']
20
// change  Userlog_Module_Plugin_Abstract , Userlog_Module_Plugin
21
// change  $xoops->getActiveModules() -> xoops_getActiveModules()
22
class Userlog_Module_Plugin
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
23
{
24
    /**
25
     * @param string $dirname
26
     * @param string $pluginName
27
     * @param bool   $force
28
     *
29
     * @return bool|Xoops_Module_Plugin_Abstract false if plugin does not exist
30
     */
31
    public static function getPlugin($dirname, $pluginName = 'system', $force = false)
32
    {
33
        $inactiveModules = false;
34
        if ($force) {
35
            $inactiveModules = array($dirname);
36
        }
37
        $available = self::getPlugins($pluginName, $inactiveModules);
38
        if (!in_array($dirname, array_keys($available))) {
39
            return false;
40
        }
41
42
        return $available[$dirname];
43
    }
44
45
    /**
46
     * @param string     $pluginName
47
     * @param array|bool $inactiveModules
48
     *
49
     * @return mixed
50
     */
51
    public static function getPlugins($pluginName = 'system', $inactiveModules = false)
0 ignored issues
show
Coding Style introduced by
getPlugins uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
52
    {
53
        static $plugins = array();
54
        if (!isset($plugins[$pluginName])) {
55
            $plugins[$pluginName] = array();
56
            //$xoops = Xoops::getInstance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
58
            //Load interface for this plugin
59
            if (!self::loadFile($GLOBALS['xoops']->path("modules/{$pluginName}/class/plugin/interface.php"))) {
60
                return $plugins[$pluginName];
61
            }
62
63
            $dirnames = xoops_getActiveModules();
64
            if (is_array($inactiveModules)) {
65
                $dirnames = array_merge($dirnames, $inactiveModules);
66
            }
67
            foreach ($dirnames as $dirname) {
68
                if (self::loadFile($GLOBALS['xoops']->path("modules/{$dirname}/class/plugin/{$pluginName}.php"))
69
                    || self::loadFile($GLOBALS['xoops']->path("modules/{$pluginName}/class/plugin/{$dirname}.php"))
70
                ) {
71
                    $className = ucfirst($dirname) . ucfirst($pluginName) . 'Plugin';
72
                    $interface = ucfirst($pluginName) . 'PluginInterface';
73
                    $class     = new $className($dirname);
74
                    if ($class instanceof Userlog_Module_Plugin_Abstract && $class instanceof $interface) {
75
                        $plugins[$pluginName][$dirname] = $class;
76
                    }
77
                }
78
            }
79
        }
80
81
        return $plugins[$pluginName];
82
    }
83
84
    /**
85
     * @param      $file
86
     * @param bool $once
87
     *
88
     * @return bool
89
     */
90
91
    public static function loadFile($file, $once = true)
92
    {
93
        self::_securityCheck($file);
94
        if (self::fileExists($file)) {
95
            if ($once) {
96
                include_once $file;
97
            } else {
98
                include $file;
99
            }
100
101
            return true;
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @param $file
109
     *
110
     * @return mixed
111
     */
112
113
    public static function fileExists($file)
114
    {
115
        static $included = array();
116
        if (!isset($included[$file])) {
117
            $included[$file] = file_exists($file);
118
        }
119
120
        return $included[$file];
121
    }
122
123
    /**
124
     * @param $filename
125
     */
126
    protected static function _securityCheck($filename)
127
    {
128
        /**
129
         * Security check
130
         */
131
        if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
132
            exit('Security check: Illegal character in filename');
0 ignored issues
show
Coding Style Compatibility introduced by
The method _securityCheck() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
133
        }
134
    }
135
}
136