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