1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Enhanced API Admin |
4
|
|
|
* A plugin to manage web services API keys directly from within the Elgg admin console |
5
|
|
|
* |
6
|
|
|
* @package ElggAPIAdmin |
7
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2 |
8
|
|
|
* |
9
|
|
|
* @author Federico Mestrone |
10
|
|
|
* @copyright Moodsdesign Ltd 2012 |
11
|
|
|
* @link http://www.moodsdesign.com |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
* Based upon: |
16
|
|
|
* |
17
|
|
|
* Elgg API Admin |
18
|
|
|
* Upgraded to Elgg 1.8 (tested on 1.8.8) and added rename and regenerate actions |
19
|
|
|
* |
20
|
|
|
* @package ElggAPIAdmin |
21
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2 |
22
|
|
|
* |
23
|
|
|
* @author Curverider Ltd |
24
|
|
|
* @copyright Curverider Ltd 2011 |
25
|
|
|
* @link http://www.elgg.org |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
elgg_register_event_handler('init','system','apiadmin_init'); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initialise the API Admin tool on init,system |
32
|
|
|
* |
33
|
|
|
* @param unknown_type $event |
34
|
|
|
* @param unknown_type $object_type |
35
|
|
|
* @param unknown_type $object |
36
|
|
|
*/ |
37
|
|
|
function apiadmin_init($event, $object_type, $object = null) { |
38
|
|
|
// Add pages and menu items to the admin area |
39
|
|
|
elgg_register_admin_menu_item('administer', 'apiadmin', 'administer_utilities', 501); |
40
|
|
|
elgg_register_admin_menu_item('administer', 'apilog', 'statistics', 501); |
41
|
|
|
//elgg_register_admin_menu_item('administer', 'apistats', 'statistics', 501); |
42
|
|
|
|
43
|
|
|
// Hook into delete to revoke secret keys |
44
|
|
|
elgg_register_event_handler('delete', 'object', 'apiadmin_delete_key'); |
45
|
|
|
|
46
|
|
|
// Register some actions |
47
|
|
|
$plugindir = dirname(__FILE__); |
48
|
|
|
elgg_register_action('apiadmin/revokekey', $plugindir . '/actions/apiadmin/revokekey.php', 'admin'); |
49
|
|
|
elgg_register_action('apiadmin/generate', $plugindir . '/actions/apiadmin/generate.php', 'admin'); |
50
|
|
|
elgg_register_action('apiadmin/renamekey', $plugindir . '/actions/apiadmin/renamekey.php', 'admin'); |
51
|
|
|
elgg_register_action('apiadmin/regenerate', $plugindir . '/actions/apiadmin/regenerate.php', 'admin'); |
52
|
|
|
|
53
|
|
|
if ( elgg_get_plugin_setting('enable_stats', 'apiadmin') == 'on' ) { |
54
|
|
|
// Register hook for 'api_key', 'use' for stats purposes |
55
|
|
|
elgg_register_plugin_hook_handler('api_key', 'use', 'apiadmin_apikey_use'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ( elgg_is_active_plugin('version_check') ) { |
59
|
|
|
version_check_register_plugin('apiadmin'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Event handler for when an API key is deleted |
65
|
|
|
* |
66
|
|
|
* @param unknown_type $event |
67
|
|
|
* @param unknown_type $object_type |
68
|
|
|
* @param unknown_type $object |
69
|
|
|
*/ |
70
|
|
|
function apiadmin_delete_key($event, $object_type, $object = null) { |
71
|
|
|
global $CONFIG; |
72
|
|
|
|
73
|
|
|
if ( ($object) && ($object->subtype === get_subtype_id('object', 'api_key')) ) { |
74
|
|
|
// Delete secret key |
75
|
|
|
return remove_api_user($CONFIG->site_id, $object->public); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
function apiadmin_apikey_use($hook, $type, $returnvalue, $params) { |
82
|
|
|
global $CONFIG; |
83
|
|
|
$handler = sanitise_string($_GET['handler']); |
84
|
|
|
$request = sanitise_string($_GET['request']); |
85
|
|
|
$method = sanitise_string($_GET['method']); |
86
|
|
|
$api_key = sanitise_string($params); |
87
|
|
|
$remote_address = sanitise_string($_SERVER['REMOTE_ADDR']); |
88
|
|
|
$user_agent = sanitise_string($_SERVER['HTTP_USER_AGENT']); |
89
|
|
|
// `id` bigint(20) `timestamp` int(11) `api_key` varchar(40) `handler` varchar(256) `request` varchar(256) `method` varchar(256) |
90
|
|
|
$sql = sprintf("INSERT INTO %s VALUES(NULL, %d, '%s', '%s', '%s', '%s', '%s', '%s')", $CONFIG->dbprefix . 'apiadmin_log', |
91
|
|
|
time(), |
92
|
|
|
$api_key, |
93
|
|
|
$handler, |
94
|
|
|
$request, |
95
|
|
|
$method, |
96
|
|
|
$remote_address, |
97
|
|
|
$user_agent |
98
|
|
|
); |
99
|
|
|
$result = insert_data($sql); |
100
|
|
|
if ( $result != 1 ) { |
101
|
|
|
error_log("Could not save stats for $api_key ($method)"); |
102
|
|
|
} |
103
|
|
|
return $returnvalue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retrieve the API Access log based on a number of parameters. |
108
|
|
|
* |
109
|
|
|
* @param int|array $by_key The guid(s) of the key(s) to filter in |
110
|
|
|
* @param string $handler The event you are searching on. |
111
|
|
|
* @param string $request The class of object it effects. |
112
|
|
|
* @param string $method The type |
113
|
|
|
* @param string $remote_address The subtype. |
114
|
|
|
* @param int $limit Maximum number of responses to return. |
115
|
|
|
* @param int $offset Offset of where to start. |
116
|
|
|
* @param bool $count Return count or not |
117
|
|
|
* @param int $timebefore Lower time limit |
118
|
|
|
* @param int $timeafter Upper time limit |
119
|
|
|
* @param int $object_id GUID of an object |
120
|
|
|
* @param str $ip_address The IP address. |
|
|
|
|
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
function apiadmin_get_usage_log($by_key = '', $handler = '', $request = '', $method = '', $remote_address = '', |
124
|
|
|
$limit = 10, $offset = 0, $count = false, $timebefore = 0, $timeafter = 0, $object_id = 0 |
125
|
|
|
) { |
126
|
|
|
|
127
|
|
|
global $CONFIG; |
128
|
|
|
|
129
|
|
|
$limit = (int)$limit; |
130
|
|
|
$offset = (int)$offset; |
131
|
|
|
|
132
|
|
|
$where = array(); |
133
|
|
|
|
134
|
|
|
/* |
135
|
|
|
$by_user_orig = $by_user; |
136
|
|
|
if (is_array($by_user) && sizeof($by_user) > 0) { |
137
|
|
|
foreach ($by_user as $key => $val) { |
138
|
|
|
$by_user[$key] = (int) $val; |
139
|
|
|
} |
140
|
|
|
} else { |
141
|
|
|
$by_user = (int)$by_user; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$event = sanitise_string($event); |
145
|
|
|
$class = sanitise_string($class); |
146
|
|
|
$type = sanitise_string($type); |
147
|
|
|
$subtype = sanitise_string($subtype); |
148
|
|
|
$ip_address = sanitise_string($ip_address); |
149
|
|
|
|
150
|
|
|
if ($by_user_orig !== "" && $by_user_orig !== false && $by_user_orig !== null) { |
151
|
|
|
if (is_int($by_user)) { |
152
|
|
|
$where[] = "performed_by_guid=$by_user"; |
153
|
|
|
} else if (is_array($by_user)) { |
154
|
|
|
$where [] = "performed_by_guid in (" . implode(",", $by_user) . ")"; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
if ($event != "") { |
158
|
|
|
$where[] = "event='$event'"; |
159
|
|
|
} |
160
|
|
|
if ($class !== "") { |
161
|
|
|
$where[] = "object_class='$class'"; |
162
|
|
|
} |
163
|
|
|
if ($type != "") { |
164
|
|
|
$where[] = "object_type='$type'"; |
165
|
|
|
} |
166
|
|
|
if ($subtype !== "") { |
167
|
|
|
$where[] = "object_subtype='$subtype'"; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($timebefore) { |
171
|
|
|
$where[] = "time_created < " . ((int) $timebefore); |
172
|
|
|
} |
173
|
|
|
if ($timeafter) { |
174
|
|
|
$where[] = "time_created > " . ((int) $timeafter); |
175
|
|
|
} |
176
|
|
|
if ($object_id) { |
177
|
|
|
$where[] = "object_id = " . ((int) $object_id); |
178
|
|
|
} |
179
|
|
|
if ($ip_address) { |
180
|
|
|
$where[] = "ip_address = '$ip_address'"; |
181
|
|
|
} |
182
|
|
|
*/ |
183
|
|
|
|
184
|
|
|
$select = '*'; |
185
|
|
|
if ( $count ) { |
186
|
|
|
$select = 'count(*) as count'; |
187
|
|
|
} |
188
|
|
|
$query = "SELECT $select FROM {$CONFIG->dbprefix}apiadmin_log WHERE 1 "; |
189
|
|
|
foreach ( $where as $w ) { |
190
|
|
|
$query .= " AND $w"; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ( !$count ) { |
194
|
|
|
$query .= ' ORDER BY time_created DESC'; |
195
|
|
|
$query .= " LIMIT $offset, $limit"; // Add order and limit |
196
|
|
|
} |
197
|
|
|
|
198
|
|
View Code Duplication |
if ( $count ) { |
199
|
|
|
$numrows = get_data_row($query); |
200
|
|
|
if ( $numrows ) { |
|
|
|
|
201
|
|
|
return $numrows->count; |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
return get_data($query); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.