source: trunk/admin/ws_checker.php @ 2288

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