]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - Classes/class-plugins.php
Move plugins.php class file to Class/class-plugins.php
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-plugins.php
CommitLineData
90dc8f2b 1<?php
6ae326cb 2require_once "class-message.php";
90dc8f2b
VP
3
4
5/** Check for plugins and load them.
6 *
7 * This expects your plugin folder to be located in `plugins/` and that the directory name,
8 * constructor file name and class name are identical.
9 * For example:
10 * You must have a file structure like this: plugins/myplugin/myplugin.php
11 * Which contains a class like this:
12 * ```
13 * class myplugin {
55fd88eb
VP
14 * $name = "My plugin";
15 * $author = "Joe Bloggs";
16 * $version "1.0";
17 * $desc = "This is my plugin and it does stuff";
18 *
19 * // rest of code here...
90dc8f2b
VP
20 * }
21 * ```
22 * Your plugin class must be constructable and contain the following public variables:
55fd88eb 23 * $name The name or title of your plugin.
90dc8f2b
VP
24 * $author The name of the author
25 * $version The version of the plugin
55fd88eb 26 * $description A short description of the plugin
90dc8f2b
VP
27*/
28class Plugins
29{
55fd88eb 30 static $list = [];
90dc8f2b 31
55fd88eb
VP
32 static function load($modname)
33 {
34 $plugin = new Plugin($modname);
35 if ($plugin->error)
36 {
37 Message::Fail("Warning: Plugin \"$modname\" failed to load: $plugin->error");
38 }
39 else
40 {
41 self::$list[] = $plugin;
42 }
43 }
4f3f84af
VP
44 static function plugin_exists($name, $version = NULL)
45 {
46 foreach(self::$list as $p)
e47cb29f 47 if (!strcmp($p->name,$name) && (!$version || ($version >= $p->version)))
4f3f84af
VP
48 return true;
49
50 return false;
51 }
52
90dc8f2b
VP
53}
54
55class Plugin
56{
55fd88eb
VP
57 public $name;
58 public $author;
59 public $version;
60 public $description;
61 public $handle;
b65f0496 62 public $email;
90dc8f2b 63
55fd88eb
VP
64 public $error = NULL;
65 function __construct($handle)
66 {
195214b2
VP
67 if (!is_dir(UPATH."/plugins/$handle"))
68 $this->error = "Plugin directory \"".UPATH."/plugins/$handle\" doesn't exist";
90dc8f2b 69
195214b2
VP
70 else if (!is_file(UPATH."/plugins/$handle/$handle.php"))
71 $this->error = "Plugin file \"".UPATH."/plugins/$handle/$handle.php\" doesn't exist";
90dc8f2b 72
55fd88eb
VP
73 else
74 {
93a3dfcb 75 require_once UPATH."/plugins/$handle/$handle.php";
90dc8f2b 76
55fd88eb
VP
77 if (!class_exists($handle))
78 $this->error = "Class \"$handle\" doesn't exist";
90dc8f2b 79
55fd88eb
VP
80 else
81 {
82 $plugin = new $handle();
83
84 if (!isset($plugin->name))
85 $this->error = "Plugin name not defined";
86 elseif (!isset($plugin->author))
87 $this->error = "Plugin author not defined";
88 elseif (!isset($plugin->version))
89 $this->error = "Plugin version not defined";
90 elseif (!isset($plugin->description))
91 $this->error = "Plugin description not defined";
b65f0496
VP
92 elseif (!isset($plugin->email))
93 $this->error = "Plugin email not defined";
55fd88eb
VP
94 else
95 {
96 $this->handle = $handle;
97 $this->name = $plugin->name;
98 $this->author = $plugin->author;
99 $this->version = $plugin->version;
100 $this->description = $plugin->description;
b65f0496 101 $this->email = $plugin->email;
55fd88eb
VP
102 }
103 }
104 }
105 }
90dc8f2b
VP
106}
107
ea90b321 108if (get_config("plugins"))
90dc8f2b 109{
ea90b321 110 foreach(get_config("plugins") as $plugin)
55fd88eb 111 Plugins::load($plugin);
4f3f84af
VP
112}
113
114/* Requires the plugin */
115function require_plugin($name, $version)
116{
117 if (!Plugins::plugin_exists($name,$version))
118 die("Missing plugin: $name v$version");
c00c34d2
VP
119}
120
c00c34d2
VP
121/* I'm not a fan of globals */
122class AuthModLoaded
123{
124 public static $status = 0;
125}
126
127function is_auth_provided()
128{
129 return AuthModLoaded::$status;
c51ca81c 130}