-
Notifications
You must be signed in to change notification settings - Fork 33
/
addon.php
148 lines (135 loc) · 5.44 KB
/
addon.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
<?php
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016 Phorum Development Team //
// http://www.phorum.org //
// //
// This program is free software. You can redistribute it and/or modify //
// it under the terms of either the current Phorum License (viewable at //
// phorum.org) or the Phorum License that was distributed with this file //
// //
// 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. //
// //
// You should have received a copy of the Phorum License //
// along with this program. //
// //
////////////////////////////////////////////////////////////////////////////////
// This script can be used for implementing addon scripts, using the
// Phorum module system. This allows for full featured scripts, that
// run on their own (outside the hooks in the Phorum code), but which
// do not need to be copied to the Phorum main directory to be run.
// By containing addon scripts in the modules this way, installation and
// maintaining them is easier for the users.
//
//
// IMPLEMENTING AN ADDON SCRIPT:
// -----------------------------
//
// To implement an addon script, the following needs to be done:
//
// 1) Create a module, which contains a function that has to be
// called for running the addon code. For example:
//
// function phorum_mod_foo_youraddonfunction() {
// # Code for implementing the addon goes here.
// # This can of course also be an include of a script
// # to run, using include "./mods/foo/yourscript.php";
// # ...
// }
//
// 2) In the module information, register an addon hook for the function:
//
// hook: addon|phorum_mod_foo_youraddonfunction
//
// 3) Call the addon code through the addon.php script (where 1 in the
// URL indicates the current forum id):
//
// http://your.phorum.site/addon.php?1,module=foo
//
//
// LINKING TO AN ADDON SCRIPT:
// ---------------------------
//
// If you want to link to the addon script, then always use the
// phorum_api_url() function for generating the URL to link to.
//
// $url = phorum_api_url(PHORUM_ADDON_URL, "module=foo");
//
// IMPLEMENTING MULTIPLE ADDON ACTIONS:
// ------------------------------------
//
// Only one addon hook is allowed per module. If your module needs to
// implement multiple addon script actions, then handle this by means
// of extra custom parameters for the addon.php URL, for example:
//
// http://your.phorum.site/addon.php?1,module=foo,action=bar
//
// Using this, your addon function can check $PHORUM["args"]["action"]
// to see what action to perform. Generating an URL for this example
// would look like this:
//
// $url = phorum_api_url(PHORUM_ADDON_URL, "module=foo", "action=bar");
//
define('phorum_page', 'addon');
require_once './common.php';
// Search bots are trying to call this script without query string,
// redirect to homepage.
if (!$_GET && !$_POST && !$PHORUM['args']) {
header('HTTP/1.1 303 See Other');
header('Location: index.php');
exit;
}
// Bail out early if there are no modules enabled that implement
// the addon hook.
if (! isset($PHORUM["hooks"]["addon"])) trigger_error(
'<h1>Modscript Error</h1><br/>' .
'There are no addon hook enabled modules active.',
E_USER_ERROR
);
// Find the module argument. This one can be in the Phorum args,
// $_POST or $_GET (in that order).
$module = NULL;
if (isset($PHORUM['args']['module'])) {
$module = $PHORUM['args']['module'];
} elseif (isset($_POST['module'])) {
$module = $_POST['module'];
} elseif (isset($_GET['module'])) {
$module = $_GET['module'];
}
if ($module === NULL) trigger_error(
'<h1>Modscript Error</h1><br/>' .
'Missing "module" argument.',
E_USER_ERROR
);
$module = basename($module);
// Check if the mod is enabled and does implement the addon hook.
// Filter the list of hooks, so we only keep the one for the
// requested module.
$avail_hooks = $PHORUM["hooks"]["addon"];
$filtered_hooks = array("mods" => array(), "funcs" => array());
foreach ($avail_hooks["mods"] as $id => $checkmodule) {
if ($module == $checkmodule) {
$filtered_hooks["mods"][] = $module;
$filtered_hooks["funcs"][] = $avail_hooks["funcs"][$id];
}
}
if (count($filtered_hooks["mods"]) == 0) trigger_error(
'<h1>Modscript Error</h1>' .
'No addon hook enabled for module "' .
phorum_api_format_htmlspecialchars($module) . '"',
E_USER_ERROR
);
if (count($filtered_hooks["mods"]) > 1) trigger_error(
'<h1>Modsript Error</h1>' .
'More than one addon hook was registered ' .
'in the info for module "' .
phorum_api_format_htmlspecialchars($module) .
'".<br/>Only one addon hook is allowed per module.',
E_USER_ERROR
);
// Run the hook function.
$PHORUM["hooks"]["addon"] = $filtered_hooks;
phorum_api_hook("addon");
?>