source: trunk/admin/ws_checker.php @ 1661

Last change on this file since 1661 was 1661, checked in by vdigital, 17 years ago

Web Service Admin part 2

File size: 13.8 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-12-15 23:16:37 +0200 (ven., 15 dec. 2006) $
10// | last modifier : $Author: vdigital $
11// | revision      : $Revision: 1658 $
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// +-----------------------------------------------------------------------+
29// |                             functions                                 |
30// +-----------------------------------------------------------------------+
31
32// Function to migrate be useful for ws
33function official_req()
34{
35return array(
36    'random'                              /* Random order */
37  , 'list'               /* list on MBt & z0rglub request */
38  , 'maxviewed'             /* hit > 0 and hit desc order */
39  , 'recent'        /* recent = Date_available desc order */
40  , 'highrated'            /* avg_rate > 0 and desc order */
41  , 'oldest'                  /* Date_available asc order */
42  , 'lessviewed'                         /* hit asc order */
43  , 'lowrated'                      /* avg_rate asc order */
44  , 'undescribed'                  /* description missing */
45  , 'unnamed'                         /* new name missing */
46  , 'portraits'     /* width < height (portrait oriented) */
47  , 'landscapes'   /* width > height (landscape oriented) */
48  , 'squares'             /* width ~ height (square form) */
49);
50}
51
52function expand_id_list($ids)
53{
54    $tid = array();
55    foreach ( $ids as $id )
56    {
57      if ( is_numeric($id) )
58      {
59        $tid[] = (int) $id;
60      }
61      else
62      {
63        $range = explode( '-', $id );
64        if ( is_numeric($range[0]) and is_numeric($range[1]) )
65        {
66          $from = min($range[0],$range[1]);
67          $to = max($range[0],$range[1]);
68          for ($i = $from; $i <= $to; $i++) 
69          {
70            $tid[] = (int) $i;
71          }
72        }
73      }
74    }
75    $result = array_unique ($tid); // remove duplicates...
76    sort ($result);
77    return $result;
78}
79
80function check_target($list)
81{
82  if ( $list !== '' )
83  {
84    $type = explode('/',$list); // Find type list
85    if ( !in_array($type[0],array('list','cat','tag') ) )
86    {
87      $type[0] = 'list'; // Assume an id list
88    } 
89    $ids = explode( ',',$type[1] );
90    $list = $type[0] . '/';
91
92    // 1,2,21,3,22,4,5,9-12,6,11,12,13,2,4,6,
93
94    $result = expand_id_list( $ids ); 
95
96    // 1,2,3,4,5,6,9,10,11,12,13,21,22,
97    // I would like
98    // 1-6,9-13,21-22
99    $serial[] = $result[0]; // To be shifted                     
100    foreach ($result as $k => $id)
101    {
102      $next_less_1 = (isset($result[$k + 1]))? $result[$k + 1] - 1:-1;
103      if ( $id == $next_less_1 and end($serial)=='-' )
104      { // nothing to do
105      }
106      elseif ( $id == $next_less_1 )
107      {
108        $serial[]=$id;
109        $serial[]='-';
110      }
111      else
112      {
113        $serial[]=$id;  // end serie or non serie
114      }
115    }
116    $null = array_shift($serial); // remove first value
117    $list .= array_shift($serial); // add the real first one
118    $separ = ',';
119    foreach ($serial as $id)
120    {
121      $list .= ($id=='-') ? '' : $separ . $id;
122      $separ = ($id=='-') ? '-':','; // add comma except if hyphen
123    }
124  }
125  return $list;
126}
127
128// Next evolution...
129// Out of parameter WS management
130// The remainer objective is to check
131//  -  Does Web Service working properly?
132//  -  Does any access return something really?
133//     Give a way to check to the webmaster...
134// These questions are one of module name explainations (checker).
135
136if((!defined("PHPWG_ROOT_PATH")) or (!$conf['allow_web_services']))
137{
138  die('Hacking attempt!');
139}
140include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
141
142// +-----------------------------------------------------------------------+
143// | Check Access and exit when user status is not ok                      |
144// +-----------------------------------------------------------------------+
145check_status(ACCESS_ADMINISTRATOR);
146
147
148// FIXME would be in migration process but could stay here
149// Config parameters
150if (!isset($conf['ws_status']))
151{
152  $conf['ws_status'] = false;
153
154  $query = '
155  INSERT INTO '.CONFIG_TABLE.'
156    (param,value,comment)
157    VALUES
158  (\'ws_status\', \'false\', \'Web Service status\' )
159  ;';
160  pwg_query($query);
161}
162
163// accepted queries
164$req_type_list = official_req();
165
166
167//--------------------------------------------------------- update informations
168
169// Is status temporary changed?
170if (isset($_POST['wss_submit']))
171{
172  $ws_status = get_boolean( $_POST['ws_status'] );      // Requested status
173  $ws_update = $lang['ws_success_upd'];  // Normal update
174  if ($conf['allow_web_services'] == false and $ws_status == true )
175  { /* Set true is disallowed */
176    $ws_status = false;
177    $ws_update = $lang['ws_disallowed'];
178  }
179  if ( $ws_status !== true and $ws_status !== false )
180  { /* Avoiding SQL injection by no change */
181    $ws_status = $conf['ws_status'];
182  }
183  if ($conf['ws_status'] == $ws_status)
184  {
185    $ws_update = $lang['ws_disallowed'];
186  }
187  else
188  {
189    $query = '
190UPDATE '.CONFIG_TABLE.' SET
191 value = \''.boolean_to_string($ws_status).'\'
192WHERE param = \'ws_status\'
193 AND value <> \''.boolean_to_string($ws_status).'\'
194;';
195    pwg_query($query);
196    $conf['ws_status'] = $ws_status;
197  }
198  $template->assign_block_vars(
199    'update_result',
200    array(
201      'UPD_ELEMENT'=> $lang['ws_set_status'].': '.$ws_update,
202      )
203  );
204}
205
206// Next, is a new access required?
207
208if (isset($_POST['wsa_submit']))
209{
210// Check $_post
211$add_partner = htmlspecialchars( $_POST['add_partner'], ENT_QUOTES);
212$add_access = check_target( $_POST['add_access']) ;
213$add_start = ( is_numeric($_POST['add_start']) ) ? $_POST['add_start']:0; 
214$add_end = ( is_numeric($_POST['add_end']) ) ? $_POST['add_end']:0;
215$add_request = ( ctype_alpha($_POST['add_request']) ) ?
216  $_POST['add_request']:'';
217$add_high = ( $_POST['add_high'] == 'true' ) ? 'true':'false';
218$add_normal = ( $_POST['add_normal'] == 'true' ) ? 'true':'false';
219$add_limit = ( is_numeric($_POST['add_limit']) ) ? $_POST['add_limit']:1; 
220$add_comment = htmlspecialchars( $_POST['add_comment'], ENT_QUOTES);
221if ( strlen($add_partner) < 8 )
222{
223}
224  $query = '
225INSERT INTO '.WEB_SERVICES_ACCESS_TABLE.'
226( `name` , `access` , `start` , `end` , `request` ,
227  `high` , `normal` , `limit` , `comment` )
228VALUES (' . "
229  '$add_partner', '$add_access',
230  ADDDATE( NOW(), INTERVAL $add_start DAY),
231  ADDDATE( NOW(), INTERVAL $add_end DAY),
232  '$add_request', '$add_high', '$add_normal', '$add_limit', '$add_comment' );";
233
234  pwg_query($query);
235 
236  $template->assign_block_vars(
237    'update_result',
238    array(
239      'UPD_ELEMENT'=> $lang['ws_adding_legend'].$lang['ws_success_upd'],
240      )
241  );
242}
243
244// Next, Update selected access
245if (isset($_POST['wsu_submit']))
246{
247  $upd_end = ( is_numeric($_POST['upd_end']) ) ? $_POST['upd_end']:0;
248  $settxt = ' end = ADDDATE(NOW(), INTERVAL '. $upd_end .' DAY)';
249
250  if ((isset($_POST['selection'])) and (trim($settxt) != ''))
251  {
252    $uid = (int) $_POST['selection'];
253    $query = '
254    UPDATE '.WEB_SERVICES_ACCESS_TABLE.'
255    SET '.$settxt.'
256    WHERE id = '.$uid.'; ';
257    pwg_query($query);
258    $template->assign_block_vars(
259      'update_result',
260      array(
261        'UPD_ELEMENT'=> $lang['ws_update_legend'].$lang['ws_success_upd'],
262        )
263    );
264  } else {
265    $template->assign_block_vars(
266      'update_result',
267      array(
268        'UPD_ELEMENT'=> $lang['ws_update_legend'].$lang['ws_failed_upd'],
269        )
270    );
271  }
272}
273// Next, Delete selected access
274
275if (isset($_POST['wsX_submit']))
276{
277  if ((isset($_POST['delete_confirmation']))
278   and (isset($_POST['selection'])))
279  {
280    $uid = (int) $_POST['selection'];
281    $query = 'DELETE FROM '.WEB_SERVICES_ACCESS_TABLE.'
282               WHERE id = '.$uid.'; ';
283    pwg_query($query);
284    $template->assign_block_vars(
285      'update_result',
286      array(
287        'UPD_ELEMENT'=> $lang['ws_delete_legend'].$lang['ws_success_upd'],
288        )
289    );
290  } else {
291    $template->assign_block_vars(
292      'update_result',
293      array(
294        'UPD_ELEMENT'=> $lang['Not selected / Not confirmed']
295        .$lang['ws_failed_upd'],
296        )
297    );
298  } 
299}
300
301
302$ws_status = $conf['ws_status'];
303$template->assign_vars(
304  array(
305    'L_CURRENT_STATUS' => ( $ws_status == true ) ? 
306       $lang['ws_enable']:$lang['ws_disable'],
307    'STATUS_YES' => ( $ws_status == true ) ? '':'checked', 
308    'STATUS_NO' => ( $ws_status == true ) ? 'checked':'', 
309    'DEFLT_HIGH_YES' => '',
310    'DEFLT_HIGH_NO' => 'checked',
311    'DEFLT_NORMAL_YES' => '',
312    'DEFLT_NORMAL_NO' => 'checked',
313    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=web_service',   
314    )
315  );
316
317// Build where
318$where = '';
319$order = ' ORDER BY `id` DESC' ;
320
321$query = '
322SELECT *
323  FROM '.WEB_SERVICES_ACCESS_TABLE.'
324WHERE 1=1  '
325.$where.
326' '
327.$order.
328';';
329$result = pwg_query($query);
330$acc_list = mysql_num_rows($result);
331$result = pwg_query($query);
332// +-----------------------------------------------------------------------+
333// |                             template init                             |
334// +-----------------------------------------------------------------------+
335
336$template->set_filenames(
337  array(
338    'ws_checker' => 'admin/ws_checker.tpl'
339    )
340  );
341
342$checked = 'checked="checked"';
343$selected = 'selected="selected"';
344$num=0;
345if ( $acc_list > 0 )
346{
347  $template->assign_block_vars(
348    'acc_list', array() );
349}
350
351// Access List
352while ($row = mysql_fetch_array($result))
353{
354  $num++;
355  $template->assign_block_vars(
356    'acc_list.access',
357     array(
358       'CLASS' => ($num % 2 == 1) ? 'row1' : 'row2',
359       'ID'               => $row['id'],
360       'NAME'             => 
361         (is_adviser()) ? '*********' : $row['name'],       
362       'ACCESS'           => $row['access'],
363       'START'            => $row['start'],
364       'END'              => $row['end'],
365       'FORCE'            => $row['request'],
366       'HIGH'             => $row['high'],
367       'NORMAL'           => $row['normal'],
368       'LIMIT'            => $row['limit'],
369       'COMMENT'          => $row['comment'],
370       'SELECTED'         => '',
371     )
372  );
373}
374
375$template->assign_block_vars(
376  'add_request',
377   array(
378     'VALUE'=> '',
379     'CONTENT' => '',
380     'SELECTED' => $selected,
381   )
382);
383foreach ($req_type_list as $value) {
384
385  $template->assign_block_vars(
386    'add_request',
387     array(
388       'VALUE'=> $value,
389       'CONTENT' => $lang['ws_'.$value],
390       'SELECTED' => '',
391     )
392  );
393}
394
395$columns = array (
396       'ID'               => 'id',
397       'ws_KeyName'       => 'name', 
398       'ws_Access'        => 'ws_access',
399       'ws_Start'         => 'ws_start',
400       'ws_End'           => 'ws_end',
401       'ws_Request'       => 'ws_request',
402       'ws_High'          => 'ws_high',
403       'ws_Normal'        => 'ws_normal',
404       'ws_Limit'         => 'ws_limit',
405       'ws_Comment'       => 'ws_comment',
406);
407
408foreach ($conf['ws_allowed_limit'] as $value) {
409  $template->assign_block_vars(
410    'add_limit',
411     array(
412       'VALUE'=> $value,
413       'CONTENT' => $value,
414       'SELECTED' => ($conf['ws_allowed_limit'][0] == $value) ? $selected:'',
415     )
416  );
417}
418
419// Postponed Start Date
420// By default 0, 1, 2, 3, 5, 7, 14 or 30 days
421foreach ($conf['ws_postponed_start'] as $value) {
422  $template->assign_block_vars(
423    'add_start',
424     array(
425       'VALUE'=> $value,
426       'CONTENT' => $value,
427       'SELECTED' => ($conf['ws_postponed_start'][0] == $value) ? $selected:'',
428     )
429  );
430}
431
432// Durations (Allowed Web Services Period)
433// By default 10, 5, 2, 1 year(s) or 6, 3, 1 month(s) or 15, 10, 7, 5, 1, 0 day(s)
434foreach ($conf['ws_durations'] as $value) {
435  $template->assign_block_vars(
436    'add_end',
437     array(
438       'VALUE'=> $value,
439       'CONTENT' => $value,
440       'SELECTED' => ($conf['ws_durations'][3] == $value) ? $selected:'',
441     )
442  );
443  if ( $acc_list > 0 )
444  {
445    $template->assign_block_vars(
446      'acc_list.upd_end',
447       array(
448         'VALUE'=> $value,
449         'CONTENT' => $value,
450         'SELECTED' => ($conf['ws_durations'][3] == $value) ? $selected:'',
451       )
452    );
453  }
454}
455
456//----------------------------------------------------------- sending html code
457
458$template->assign_var_from_handle('ADMIN_CONTENT', 'ws_checker');
459?>
Note: See TracBrowser for help on using the repository browser.