/
hello_pl.php
369 lines (331 loc) · 12.5 KB
/
hello_pl.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* This file is part of PluginLibrary for MyBB.
* Copyright (C) 2011 Andreas Klauer <Andreas.Klauer@metamorpher.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
/**
* DEFINE PLUGINLIBRARY
*
* Define the path to the plugin library, if it isn't defined yet.
*/
if(!defined("PLUGINLIBRARY"))
{
define("PLUGINLIBRARY", MYBB_ROOT."inc/plugins/pluginlibrary.php");
}
$plugins->add_hook("pre_output_page", "hello_pl_world");
$plugins->add_hook("admin_config_plugins_begin", "hello_pl_edit");
function hello_pl_info()
{
global $mybb, $plugins_cache;
$info = array(
"name" => "Hello PluginLibrary!",
"description" => "A sample plugin for developers that demonstrates the features of the PluginLibrary.",
"website" => "http://mods.mybb.com/view/pluginlibrary",
"author" => "Andreas Klauer",
"authorsite" => "mailto:Andreas.Klauer@metamorpher.de",
"version" => "hello_pl.php",
"guid" => "",
"compatibility" => "18*"
);
// Display some extra information when installed and active.
if(hello_pl_is_installed() && $plugins_cache['active']['hello_pl'])
{
global $PL;
$PL or require_once PLUGINLIBRARY;
/**
* URL APPEND
*
* $PL->url_append($url, $params, $sep, $encode)
*
* Append parameters to an URL that may or may not have ?query.
*/
$editurl = $PL->url_append("index.php?module=config-plugins",
array("hello_pl" => "edit",
"my_post_key" => $mybb->post_code));
$undourl = $PL->url_append("index.php",
array("module" => "config-plugins",
"hello_pl" => "undo",
"my_post_key" => $mybb->post_code));
$editurl = "index.php?module=config-plugins&hello_pl=edit&my_post_key=".$mybb->post_code;
$undourl = "index.php?module=config-plugins&hello_pl=undo&my_post_key=".$mybb->post_code;
$info["description"] .= "<br /><a href=\"{$editurl}\">Make edits to hello_pl.php</a>.";
$info["description"] .= " | <a href=\"{$undourl}\">Undo edits to hello_pl.php</a>.";
}
return $info;
}
function hello_pl_is_installed()
{
global $settings;
// This plugin creates settings on install. Check if setting exists.
// Another example would be $db->table_exists() for database tables.
if(isset($settings['hello_pl_foobar']))
{
return true;
}
}
function hello_pl_install()
{
/**
* DEPENDENCY CHECK
*
* If PluginLibrary is missing but required by your plugin,
*
* - use file_exists(PLUGINLIBRARY) to check if it's there
* - use flash_message() to give the user a friendly error message,
* preferably including a download link to the missing dependency.
* - use admin_redirect() to cancel the installation.
*/
if(!file_exists(PLUGINLIBRARY))
{
flash_message("The selected plugin could not be installed because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
admin_redirect("index.php?module=config-plugins");
}
/**
* LOADING
*
* PluginLibrary is not loaded automatically. Load it when required.
*
* - use the global variable $PL to access PluginLibrary functions
* - if $PL is not set, use require to load the PluginLibrary
*/
global $PL;
$PL or require_once PLUGINLIBRARY;
/**
* VERSION CHECK
*
* Only needed if you need at least a specific version of PluginLibrary.
*
* - compare $PL->version to the version number you need.
* - same procedure as DEPENDENCY CHECK
*/
if($PL->version < 11)
{
flash_message("The selected plugin could not be installed because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is too old.", "error");
admin_redirect("index.php?module=config-plugins");
}
}
function hello_pl_uninstall()
{
global $PL;
$PL or require_once PLUGINLIBRARY;
/**
* SETTINGS DELETE
*
* $PL->settings_delete(name, greedy)
*
* Delete one or more setting groups and their settings.
*/
$PL->settings_delete("hello_pl"
// , true /* optional, multiple groups */
);
/**
* TEMPLATES DELETE
*
* $PL->templates_delete(prefix, greedy)
*
* Delete one or more template groups and their templates.
*/
$PL->templates_delete("hellopl"
// , true /* optional, multiple groups */
);
/**
* STYLESHEET DELETE
*
* $PL->stylesheet_delete(name, greedy)
*
* Delete one or more stylesheets.
*/
$PL->stylesheet_delete('hellopl', true);
}
function hello_pl_activate()
{
global $PL, $mybb;
$PL or require_once PLUGINLIBRARY;
/**
* SETTINGS
*
* $PL->settings(name, title, description, list)
*
* Create a setting group with any number of settings with $PL->settings()
* If the setting group already exists, the settings are updated properly.
*/
$PL->settings("hello_pl", // group name and settings prefix
"Hello PluginLibrary!",
"Setting group for the Hello PluginLibrary sample plugin.",
array(
"foobar" => array(
"title" => "Foo Bar",
"description" => "The setting name depends on the prefix (hello_pl) and the key (foobar). The name of this setting is hello_pl_foobar.",
),
"no" => array(
"title" => "Simple Yes/No setting",
"description" => "The default is no. The name of this setting is hello_pl_no.",
),
"yes" => array(
"title" => "Yes/No setting",
"description" => "This one is set to yes. The name of this setting is hello_pl_yes.",
"value" => 1,
),
"text" => array(
"title" => "Text setting",
"description" => "Give me a word. The name of this setting is hello_pl_text.",
"optionscode" => "text",
),
"textarea" => array(
"title" => "Text area (hello_pl_textarea)",
"description" => "Multiple lines. The name of this setting is hello_pl_textarea.",
"optionscode" => "textarea",
"value" => "line1\nline2",
),
)
// , true /* optional, prints a language file */
);
/**
* TEMPLATES
*
* $PL->templates(prefix, title, list)
*
* Create a template group with any number of templates with $PL->templates()
* If the template group already exists, the templates are updated properly.
* Templates edited by users will show up in MyBB's 'Find Updated Templates'
*
* Note: prefix must not contain _ for templates.
*/
$PL->templates("hellopl", // template prefix, must not contain _
"Hello Pluginlibrary!", // you can also use "<lang:your_language_variable>" here
array(
"" => "<p>This is the <b>hellopl</b> template...</p>",
"example" => "<p>...and this is the <i>hellopl_example</i> template.</p>",
"other" => "Another template using {\$hellopl} and {\$hellopl_example}.",
)
);
/**
* STYLESHEET
*
* $PL->stylesheet(name, styles, attachedto)
*
* Create, update and activate a stylesheet. Prefix the name with
* something unique to your plugin to avoid collisions.
*/
$css = "#pluginlibrary { background: #026CB1 url(images/thead_bg.gif); color: white; }";
$PL->stylesheet('hellopl_test', $css);
$PL->stylesheet('hellopl_sendpm',
array(
'#pluginlibrary' => array(
'text-align' => 'center',
'font-style' => 'italic',
'border' => 'dashed red 5px',
),
),
array('private.php' => 'send'));
}
function hello_pl_deactivate()
{
global $PL;
$PL or require_once PLUGINLIBRARY;
/**
* CACHE DELETE
*
* $PL->cache_delete(name, greedy)
*
* Delete one or more caches.
*/
$PL->cache_delete("hello_pl"
// , true /* optional, multiple caches */
);
/**
* STYLESHEET DEACTIVATE
*
* $PL->stylesheet_deactivate(name, greedy)
*
* Deactivate your stylesheet so your plugin's styles won't be loaded
* until your plugin is activated again.
*/
$PL->stylesheet_deactivate('hellopl', true);
}
function hello_pl_edit()
{
global $mybb;
// Only perform edits if we were given the correct post key.
if($mybb->input['my_post_key'] != $mybb->post_code)
{
return;
}
global $PL;
$PL or require_once PLUGINLIBRARY;
/**
* EDIT CORE
*
* $PL->edit_core(name, file, search, apply)
*
* Make or update one or more changes to a core file.
* Edits the file directly or, lacking permissions, returns a string.
*/
if($mybb->input['hello_pl'] == 'edit')
{
$result = $PL->edit_core("hello_pl", "inc/plugins/hello_pl.php",
array('search' => array("\"name\"", "=>", "\"Hello PluginLibrary!\"", ","),
'replace' => "\"name\"=>\"Hello EditCore!\","),
true // optional, try to apply the change
// , $debug // optional, obtain debug info about the edits
);
}
else if($mybb->input['hello_pl'] == 'undo')
{
/**
* UNDO EDIT CORE
*
* $PL->edit_core(name, file)
*
* If you want to undo your changes, leave out the search.
* This undoes your changes (updates your edits to change nothing).
*/
$result = $PL->edit_core("hello_pl", "inc/plugins/hello_pl.php",
array(), // leave search empty, i.e. no edits
true // optional, try to apply the change
);
}
else
{
// bad input parameter
return;
}
if($result === true)
{
// redirect with success
flash_message("The file inc/plugins/hello_pl.php was modified successfully.", "success");
admin_redirect("index.php?module=config-plugins");
}
else
{
// redirect with failure (could offer the result string for download instead)
flash_message("The file inc/plugins/hello_pl.php could not be edited. Are the CHMOD settings correct?", "error");
admin_redirect("index.php?module=config-plugins");
}
}
function hello_pl_world($page)
{
global $templates;
eval("\$example = \"".$templates->get("hellopl_example")."\";");
$page = str_replace("<div id=\"content\">", "<div id=\"content\"><div id=\"pluginlibrary\">Hello PluginLibrary!<p>This is a sample PluginLibrary Plugin (which can be disabled!) that displays this message on all pages.</p>{$example}</div>", $page);
return $page;
}