]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-plugins.php
Move plugins.php class file to Class/class-plugins.php
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-plugins.php
1 <?php
2 require_once "class-message.php";
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 {
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...
20 * }
21 * ```
22 * Your plugin class must be constructable and contain the following public variables:
23 * $name The name or title of your plugin.
24 * $author The name of the author
25 * $version The version of the plugin
26 * $description A short description of the plugin
27 */
28 class Plugins
29 {
30 static $list = [];
31
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 }
44 static function plugin_exists($name, $version = NULL)
45 {
46 foreach(self::$list as $p)
47 if (!strcmp($p->name,$name) && (!$version || ($version >= $p->version)))
48 return true;
49
50 return false;
51 }
52
53 }
54
55 class Plugin
56 {
57 public $name;
58 public $author;
59 public $version;
60 public $description;
61 public $handle;
62 public $email;
63
64 public $error = NULL;
65 function __construct($handle)
66 {
67 if (!is_dir(UPATH."/plugins/$handle"))
68 $this->error = "Plugin directory \"".UPATH."/plugins/$handle\" doesn't exist";
69
70 else if (!is_file(UPATH."/plugins/$handle/$handle.php"))
71 $this->error = "Plugin file \"".UPATH."/plugins/$handle/$handle.php\" doesn't exist";
72
73 else
74 {
75 require_once UPATH."/plugins/$handle/$handle.php";
76
77 if (!class_exists($handle))
78 $this->error = "Class \"$handle\" doesn't exist";
79
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";
92 elseif (!isset($plugin->email))
93 $this->error = "Plugin email not defined";
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;
101 $this->email = $plugin->email;
102 }
103 }
104 }
105 }
106 }
107
108 if (get_config("plugins"))
109 {
110 foreach(get_config("plugins") as $plugin)
111 Plugins::load($plugin);
112 }
113
114 /* Requires the plugin */
115 function require_plugin($name, $version)
116 {
117 if (!Plugins::plugin_exists($name,$version))
118 die("Missing plugin: $name v$version");
119 }
120
121 /* I'm not a fan of globals */
122 class AuthModLoaded
123 {
124 public static $status = 0;
125 }
126
127 function is_auth_provided()
128 {
129 return AuthModLoaded::$status;
130 }