1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2008-01-30 22:07:07 +0000 (Wed, 30 Jan 2008) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 2201 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | // Next evolution... |
---|
29 | // Out of parameter WS management |
---|
30 | // The remainer objective is to check |
---|
31 | // - Does Web Service working properly? |
---|
32 | // - Does any access return something really? |
---|
33 | // Give a way to check to the webmaster... |
---|
34 | // These questions are one of module name explanations (checker). |
---|
35 | |
---|
36 | if((!defined("PHPWG_ROOT_PATH")) or (!$conf['allow_web_services'])) |
---|
37 | { |
---|
38 | die('Hacking attempt!'); |
---|
39 | } |
---|
40 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
41 | include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php'); |
---|
42 | |
---|
43 | /** |
---|
44 | * official_req returns the managed requests list in array format |
---|
45 | * FIXME A New list need to be build for ws_checker.php |
---|
46 | * returns array of authrorized request/methods |
---|
47 | * */ |
---|
48 | function official_req() |
---|
49 | { |
---|
50 | $official = array( /* Requests are limited to */ |
---|
51 | 'categories.' /* all categories. methods */ |
---|
52 | , 'categories.getImages' |
---|
53 | , 'categories.getList' |
---|
54 | , 'images.' /* all images. methods */ |
---|
55 | , 'images.getInfo' |
---|
56 | , 'images.addComment' |
---|
57 | , 'images.search' |
---|
58 | , 'tags.' /* all tags. methods */ |
---|
59 | , 'tags.getImages' |
---|
60 | , 'tags.getList' |
---|
61 | ); |
---|
62 | if (function_exists('local_req')) { |
---|
63 | $local = local_req(); |
---|
64 | return array_merge( $official, $local ); |
---|
65 | } |
---|
66 | return $official; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * check_target($string) verifies and corrects syntax of target parameter |
---|
71 | * example : check_target(cat/23,24,24,24,25,27) returns cat/23-25,27 |
---|
72 | * */ |
---|
73 | function check_target($list) |
---|
74 | { |
---|
75 | if ( $list !== '' ) |
---|
76 | { |
---|
77 | $type = explode('/',$list); // Find type list |
---|
78 | if ( !in_array($type[0],array('list','cat','tag') ) ) |
---|
79 | { |
---|
80 | $type[0] = 'list'; // Assume an id list |
---|
81 | } |
---|
82 | $ids = explode( ',',$type[1] ); |
---|
83 | $list = $type[0] . '/'; |
---|
84 | |
---|
85 | // 1,2,21,3,22,4,5,9-12,6,11,12,13,2,4,6, |
---|
86 | |
---|
87 | $result = expand_id_list( $ids ); |
---|
88 | |
---|
89 | // 1,2,3,4,5,6,9,10,11,12,13,21,22, |
---|
90 | // I would like |
---|
91 | // 1-6,9-13,21-22 |
---|
92 | $serial[] = $result[0]; // To be shifted |
---|
93 | foreach ($result as $k => $id) |
---|
94 | { |
---|
95 | $next_less_1 = (isset($result[$k + 1]))? $result[$k + 1] - 1:-1; |
---|
96 | if ( $id == $next_less_1 and end($serial)=='-' ) |
---|
97 | { // nothing to do |
---|
98 | } |
---|
99 | elseif ( $id == $next_less_1 ) |
---|
100 | { |
---|
101 | $serial[]=$id; |
---|
102 | $serial[]='-'; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | $serial[]=$id; // end serie or non serie |
---|
107 | } |
---|
108 | } |
---|
109 | $null = array_shift($serial); // remove first value |
---|
110 | $list .= array_shift($serial); // add the real first one |
---|
111 | $separ = ','; |
---|
112 | foreach ($serial as $id) |
---|
113 | { |
---|
114 | $list .= ($id=='-') ? '' : $separ . $id; |
---|
115 | $separ = ($id=='-') ? '-':','; // add comma except if hyphen |
---|
116 | } |
---|
117 | } |
---|
118 | return $list; |
---|
119 | } |
---|
120 | |
---|
121 | // +-----------------------------------------------------------------------+ |
---|
122 | // | Check Access and exit when user status is not ok | |
---|
123 | // +-----------------------------------------------------------------------+ |
---|
124 | check_status(ACCESS_ADMINISTRATOR); |
---|
125 | |
---|
126 | // accepted queries |
---|
127 | $req_type_list = official_req(); |
---|
128 | |
---|
129 | //--------------------------------------------------------- update informations |
---|
130 | $chk_partner = ''; |
---|
131 | // Is a new access required? |
---|
132 | |
---|
133 | if (isset($_POST['wsa_submit'])) |
---|
134 | { |
---|
135 | // Check $_post (Some values are commented - maybe a future use) |
---|
136 | $add_partner = htmlspecialchars( $_POST['add_partner'], ENT_QUOTES); |
---|
137 | $add_target = check_target( $_POST['add_target']) ; |
---|
138 | $add_end = ( is_numeric($_POST['add_end']) ) ? $_POST['add_end']:0; |
---|
139 | $add_request = htmlspecialchars( $_POST['add_request'], ENT_QUOTES); |
---|
140 | $add_limit = ( is_numeric($_POST['add_limit']) ) ? $_POST['add_limit']:1; |
---|
141 | $add_comment = htmlspecialchars( $_POST['add_comment'], ENT_QUOTES); |
---|
142 | if ( strlen($add_partner) < 8 ) |
---|
143 | { // TODO What? Complete with some MD5... |
---|
144 | } |
---|
145 | $query = ' |
---|
146 | INSERT INTO '.WEB_SERVICES_ACCESS_TABLE.' |
---|
147 | ( `name` , `access` , `start` , `end` , `request` , `limit` , `comment` ) |
---|
148 | VALUES (' . " |
---|
149 | '$add_partner', '$add_target', |
---|
150 | NOW(), |
---|
151 | ADDDATE( NOW(), INTERVAL $add_end DAY), |
---|
152 | '$add_request', '$add_limit', '$add_comment' );"; |
---|
153 | |
---|
154 | pwg_query($query); |
---|
155 | $chk_partner = $add_partner; |
---|
156 | |
---|
157 | $template->assign_block_vars( |
---|
158 | 'update_result', |
---|
159 | array( |
---|
160 | 'UPD_ELEMENT'=> l10n('ws_adding_legend').l10n('ws_success_upd'), |
---|
161 | ) |
---|
162 | ); |
---|
163 | } |
---|
164 | |
---|
165 | // Next, Update selected access |
---|
166 | if (isset($_POST['wsu_submit'])) |
---|
167 | { |
---|
168 | $upd_end = ( is_numeric($_POST['upd_end']) ) ? $_POST['upd_end']:0; |
---|
169 | $settxt = ' end = ADDDATE(NOW(), INTERVAL '. $upd_end .' DAY)'; |
---|
170 | |
---|
171 | if ((isset($_POST['selection'])) and (trim($settxt) != '')) |
---|
172 | { |
---|
173 | $uid = (int) $_POST['selection']; |
---|
174 | $query = ' |
---|
175 | UPDATE '.WEB_SERVICES_ACCESS_TABLE.' |
---|
176 | SET '.$settxt.' |
---|
177 | WHERE id = '.$uid.'; '; |
---|
178 | pwg_query($query); |
---|
179 | $template->assign_block_vars( |
---|
180 | 'update_result', |
---|
181 | array( |
---|
182 | 'UPD_ELEMENT'=> l10n('ws_update_legend').l10n('ws_success_upd'), |
---|
183 | ) |
---|
184 | ); |
---|
185 | } else { |
---|
186 | $template->assign_block_vars( |
---|
187 | 'update_result', |
---|
188 | array( |
---|
189 | 'UPD_ELEMENT'=> l10n('ws_update_legend').l10n('ws_failed_upd'), |
---|
190 | ) |
---|
191 | ); |
---|
192 | } |
---|
193 | } |
---|
194 | // Next, Delete selected access |
---|
195 | |
---|
196 | if (isset($_POST['wsX_submit'])) |
---|
197 | { |
---|
198 | if ((isset($_POST['delete_confirmation'])) |
---|
199 | and (isset($_POST['selection']))) |
---|
200 | { |
---|
201 | $uid = (int) $_POST['selection']; |
---|
202 | $query = 'DELETE FROM '.WEB_SERVICES_ACCESS_TABLE.' |
---|
203 | WHERE id = '.$uid.'; '; |
---|
204 | pwg_query($query); |
---|
205 | $template->assign_block_vars( |
---|
206 | 'update_result', |
---|
207 | array( |
---|
208 | 'UPD_ELEMENT'=> l10n('ws_delete_legend').l10n('ws_success_upd'), |
---|
209 | ) |
---|
210 | ); |
---|
211 | } else { |
---|
212 | $template->assign_block_vars( |
---|
213 | 'update_result', |
---|
214 | array( |
---|
215 | 'UPD_ELEMENT'=> l10n('Not selected / Not confirmed') |
---|
216 | .l10n('ws_failed_upd'), |
---|
217 | ) |
---|
218 | ); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | $template->assign_vars( |
---|
225 | array( |
---|
226 | 'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=web_service', |
---|
227 | ) |
---|
228 | ); |
---|
229 | |
---|
230 | // Build where |
---|
231 | $where = ''; |
---|
232 | $order = ' ORDER BY `id` DESC' ; |
---|
233 | |
---|
234 | $query = ' |
---|
235 | SELECT * |
---|
236 | FROM '.WEB_SERVICES_ACCESS_TABLE.' |
---|
237 | WHERE 1=1 ' |
---|
238 | .$where. |
---|
239 | ' ' |
---|
240 | .$order. |
---|
241 | ';'; |
---|
242 | $result = pwg_query($query); |
---|
243 | $acc_list = mysql_num_rows($result); |
---|
244 | $result = pwg_query($query); |
---|
245 | // +-----------------------------------------------------------------------+ |
---|
246 | // | template init | |
---|
247 | // +-----------------------------------------------------------------------+ |
---|
248 | |
---|
249 | $template->set_filenames( |
---|
250 | array( |
---|
251 | 'ws_checker' => 'admin/ws_checker.tpl' |
---|
252 | ) |
---|
253 | ); |
---|
254 | |
---|
255 | $selected = 'selected="selected"'; |
---|
256 | $num=0; |
---|
257 | if ( $acc_list > 0 ) |
---|
258 | { |
---|
259 | $template->assign_block_vars( |
---|
260 | 'acc_list', array() ); |
---|
261 | } |
---|
262 | |
---|
263 | // Access List |
---|
264 | while ($row = mysql_fetch_array($result)) |
---|
265 | { |
---|
266 | $num++; |
---|
267 | $chk_partner = ( $chk_partner == '' ) ? $row['name'] : $chk_partner; |
---|
268 | $template->assign_block_vars( |
---|
269 | 'acc_list.access', |
---|
270 | array( |
---|
271 | 'CLASS' => ($num % 2 == 1) ? 'row1' : 'row2', |
---|
272 | 'ID' => $row['id'], |
---|
273 | 'NAME' => |
---|
274 | (is_adviser()) ? '*********' : $row['name'], |
---|
275 | 'TARGET' => $row['access'], |
---|
276 | 'END' => $row['end'], |
---|
277 | 'REQUEST' => $row['request'], |
---|
278 | 'LIMIT' => $row['limit'], |
---|
279 | 'COMMENT' => $row['comment'], |
---|
280 | 'SELECTED' => '', |
---|
281 | ) |
---|
282 | ); |
---|
283 | } |
---|
284 | |
---|
285 | $template->assign_block_vars( |
---|
286 | 'add_request', |
---|
287 | array( |
---|
288 | 'VALUE'=> '', |
---|
289 | 'CONTENT' => '', |
---|
290 | 'SELECTED' => $selected, |
---|
291 | ) |
---|
292 | ); |
---|
293 | foreach ($req_type_list as $value) { |
---|
294 | |
---|
295 | $template->assign_block_vars( |
---|
296 | 'add_request', |
---|
297 | array( |
---|
298 | 'VALUE'=> $value, |
---|
299 | 'CONTENT' => $value, |
---|
300 | 'SELECTED' => '', |
---|
301 | ) |
---|
302 | ); |
---|
303 | } |
---|
304 | |
---|
305 | foreach ($conf['ws_allowed_limit'] as $value) { |
---|
306 | $template->assign_block_vars( |
---|
307 | 'add_limit', |
---|
308 | array( |
---|
309 | 'VALUE'=> $value, |
---|
310 | 'CONTENT' => $value, |
---|
311 | 'SELECTED' => ($conf['ws_allowed_limit'][0] == $value) ? $selected:'', |
---|
312 | ) |
---|
313 | ); |
---|
314 | } |
---|
315 | |
---|
316 | // Postponed Start Date |
---|
317 | // By default 0, 1, 2, 3, 5, 7, 14 or 30 days |
---|
318 | foreach ($conf['ws_postponed_start'] as $value) { |
---|
319 | $template->assign_block_vars( |
---|
320 | 'add_start', |
---|
321 | array( |
---|
322 | 'VALUE'=> $value, |
---|
323 | 'CONTENT' => $value, |
---|
324 | 'SELECTED' => ($conf['ws_postponed_start'][0] == $value) ? $selected:'', |
---|
325 | ) |
---|
326 | ); |
---|
327 | } |
---|
328 | |
---|
329 | // Durations (Allowed Web Services Period) |
---|
330 | // By default 10, 5, 2, 1 year(s) or 6, 3, 1 month(s) or 15, 10, 7, 5, 1, 0 day(s) |
---|
331 | foreach ($conf['ws_durations'] as $value) { |
---|
332 | $template->assign_block_vars( |
---|
333 | 'add_end', |
---|
334 | array( |
---|
335 | 'VALUE'=> $value, |
---|
336 | 'CONTENT' => $value, |
---|
337 | 'SELECTED' => ($conf['ws_durations'][3] == $value) ? $selected:'', |
---|
338 | ) |
---|
339 | ); |
---|
340 | if ( $acc_list > 0 ) |
---|
341 | { |
---|
342 | $template->assign_block_vars( |
---|
343 | 'acc_list.upd_end', |
---|
344 | array( |
---|
345 | 'VALUE'=> $value, |
---|
346 | 'CONTENT' => $value, |
---|
347 | 'SELECTED' => ($conf['ws_durations'][3] == $value) ? $selected:'', |
---|
348 | ) |
---|
349 | ); |
---|
350 | } |
---|
351 | } |
---|
352 | if ( $chk_partner !== '' ) |
---|
353 | { |
---|
354 | $request = get_absolute_root_url().'ws.php?method=pwg.getVersion&format=rest&' |
---|
355 | . "partner=$chk_partner" ; |
---|
356 | $session = curl_init($request); |
---|
357 | curl_setopt ($session, CURLOPT_POST, true); |
---|
358 | curl_setopt($session, CURLOPT_HEADER, true); |
---|
359 | curl_setopt($session, CURLOPT_RETURNTRANSFER, true); |
---|
360 | $response = curl_exec($session); |
---|
361 | curl_close($session); |
---|
362 | $status_code = array(); |
---|
363 | preg_match('/\d\d\d/', $response, $status_code); |
---|
364 | switch( $status_code[0] ) { |
---|
365 | case 200: |
---|
366 | $ws_status = l10n('Web Services under control'); |
---|
367 | break; |
---|
368 | case 503: |
---|
369 | $ws_status = 'PhpWebGallery Web Services failed and returned an ' |
---|
370 | . 'HTTP status of 503. Service is unavailable. An internal ' |
---|
371 | . 'problem prevented us from returning data to you.'; |
---|
372 | break; |
---|
373 | case 403: |
---|
374 | $ws_status = 'PhpWebGallery Web Services failed and returned an ' |
---|
375 | . 'HTTP status of 403. Access is forbidden. You do not have ' |
---|
376 | . 'permission to access this resource, or are over ' |
---|
377 | . 'your rate limit.'; |
---|
378 | break; |
---|
379 | case 400: |
---|
380 | // You may want to fall through here and read the specific XML error |
---|
381 | $ws_status = 'PhpWebGallery Web Services failed and returned an ' |
---|
382 | . 'HTTP status of 400. Bad request. The parameters passed ' |
---|
383 | . 'to the service did not match as expected. The exact ' |
---|
384 | . 'error is returned in the XML response.'; |
---|
385 | break; |
---|
386 | default: |
---|
387 | $ws_status = 'PhpWebGallery Web Services returned an unexpected HTTP ' |
---|
388 | . 'status of:' . $status_code[0]; |
---|
389 | } |
---|
390 | $template->assign_block_vars( |
---|
391 | 'acc_list.ws_status', |
---|
392 | array( |
---|
393 | 'VALUE'=> $ws_status, |
---|
394 | ) |
---|
395 | ); |
---|
396 | } |
---|
397 | |
---|
398 | //----------------------------------------------------------- sending html code |
---|
399 | |
---|
400 | $template->assign_var_from_handle('ADMIN_CONTENT', 'ws_checker'); |
---|
401 | |
---|
402 | include_once(PHPWG_ROOT_PATH.'include/ws_core.inc.php'); |
---|
403 | ?> |
---|