Ignore:
Timestamp:
Feb 6, 2007, 2:02:06 AM (17 years ago)
Author:
rvelices
Message:

web services:

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/ws_functions.inc.php

    • Property svn:keywords changed from Author Date Id Rev URL to Author Date Id Revision URL
    r1777 r1781  
    88// | last update   : $Date$
    99// | last modifier : $Author$
    10 // | revision      : $Rev$
     10// | revision      : $Revision$
    1111// +-----------------------------------------------------------------------+
    1212// | This program is free software; you can redistribute it and/or modify  |
     
    3434{
    3535  global $conf, $calling_partner_id;
    36   if ( !$conf['ws_access_control'])
     36  if ( !$conf['ws_access_control']
     37       or strpos($methodName,'reflection.')===0 )
    3738  {
    3839    return $res; // No controls are requested
     
    4647  if ( empty($row) )
    4748  {
    48     return new PwgError(403, 'Partner id does not exist');
    49   }
     49    return new PwgError(403, 'Partner id does not exist or is expired');
     50  }
     51  if ( !empty($row['request'])
     52      and strpos($methodName, $row['request'])==false )
     53  {
     54    return new PwgError(403, 'Method not allowed');
     55  }
     56
    5057  return $res;
    5158}
     
    5461 * ws_addControls
    5562 * returns additionnal controls if requested
    56  * usable for 99% of Web Service methods 
    57  * 
    58  * - Args 
     63 * usable for 99% of Web Service methods
     64 *
     65 * - Args
    5966 * $methodName: is the requested method
    6067 * $partner: is the key
    6168 * $tbl_name: is the alias_name in the query (sometimes called correlation name)
    62  *            null if !getting picture informations 
     69 *            null if !getting picture informations
    6370 * - Logic
    64  * Access_control is not active: Return 
    65  * Key is incorrect: Return 0 = 1 (False condition for MySQL) 
    66  * One of Params doesn't match with type of request: return 0 = 1 again 
     71 * Access_control is not active: Return
     72 * Key is incorrect: Return 0 = 1 (False condition for MySQL)
     73 * One of Params doesn't match with type of request: return 0 = 1 again
    6774 * Access list(id/cat/tag) is converted in expended image-id list
    6875 * image-id list: converted to an in-where-clause
    69  *   
     76 *
    7077 * The additionnal in-where-clause is return
    71  */       
    72 function ws_addControls( $methodName, $tbl_name )
    73 {
    74   global $conf, $calling_partner_id, $params;
    75   if ( !$conf['ws_access_control'] )
    76   {
    77     return ' 1 = 1 '; // No controls are requested
    78   }
    79  
    80 // Is it an active Partner? 
     78 */
     79function ws_addControls( $methodName, &$params, $tbl_name )
     80{
     81  global $conf, $calling_partner_id;
     82  if ( !$conf['ws_access_control'] or !isset($calling_partner_id) )
     83  {
     84    return '1=1'; // No controls are requested
     85  }
     86
     87// Is it an active Partner?
    8188  $query = '
    8289SELECT * FROM '.WEB_SERVICES_ACCESS_TABLE."
     
    8592$result = pwg_query($query);
    8693  if ( mysql_num_rows( $result ) == 0 )
    87   {     
    88     return ' 0 = 1 '; // Unknown partner or Obsolate agreement
    89   }
    90  
     94  {
     95    return '0=1'; // Unknown partner or Obsolate agreement
     96  }
     97
    9198  $row = mysql_fetch_array($result);
    9299
    93 // Method / Request matching
    94 // Generic is not ready
    95 // For generic you can say... tags. or categories. or images. maybe?
    96   $filter = $row['request'];
    97   $request_method = substr($methodName, 0, strlen($filter)) ;
    98   if ( $filter !== $filter_method )
    99   {
    100     return ' 0 = 1'; // Unauthorized method request
    101   }
    102 // Overide general object limit   
     100// Overide general object limit
    103101  $params['per_page'] = $row['limit'];
    104  
     102
    105103// Target restrict
    106104// 3 cases: list, cat or tag
     
    109107  list($type, $str_ids) = explode('/',$target); // Find type list
    110108
    111   $ids = explode( ',',$str_ids );
    112109// (array) 1,2,21,3,22,4,5,9-12,6,11,12,13,2,4,6,
    113   $arr_ids = expand_id_list( $ids );
    114   $addings = implode(',', $arr_ids); 
    115 // (string) 1,2,3,4,5,6,9,10,11,12,13,21,22, 
    116   if ( $type = 'list')
     110  $arr_ids = expand_id_list( explode( ',',$str_ids ) );
     111  $addings = implode(',', $arr_ids);
     112// (string) 1,2,3,4,5,6,9,10,11,12,13,21,22,
     113  if ( $type == 'list')
    117114  {
    118115    return $tbl_name . 'id IN ( ' . $addings . ' ) ';
    119116  }
    120  
    121   if ( $type = 'cat' )
     117
     118  if ( $type == 'cat' )
    122119  {
    123120    $addings = implode(',', get_image_ids_for_cats($arr_ids));
    124121    return $tbl_name . 'id IN ( ' . $addings . ' ) ';
    125122  }
    126  
    127   if ( $type = 'tag' )
    128   { 
     123
     124  if ( $type == 'tag' )
     125  {
    129126    $addings = implode(',', get_image_ids_for_tags($arr_ids, 'OR'));
    130127    return $tbl_name . 'id IN ( ' . $addings . ' ) ';
    131128  }
    132129  // Unmanaged new type?
    133   return ' 0 = 1 '; // ??? 
     130  return ' 0 = 1 '; // ???
    134131}
    135132
     
    248245
    249246
     247/**
     248 * returns PWG version (web service method)
     249 */
    250250function ws_getVersion($params, &$service)
    251251{
     
    254254}
    255255
    256 /**
    257  * returns images per category (wb service method)
     256
     257/**
     258 * returns images per category (web service method)
    258259 */
    259260function ws_categories_getImages($params, &$service)
     
    309310      .implode(',', array_keys($cats) )
    310311      .')';
    311 
    312     $where_clause[] =
    313           ws_addControls( 'categories.getImages', 'i.' );
    314    
     312    $where_clauses[] = get_sql_condition_FandF( array(
     313          'visible_images' => 'i.id'
     314        ), null, true
     315      );
     316    $where_clauses[] = ws_addControls( 'categories.getImages', $params, 'i.' );
     317
    315318    $order_by = ws_std_image_sql_order($params, 'i.');
    316319    if (empty($order_by))
     
    397400}
    398401
    399 /**
    400  * returns a list of categories
     402
     403/**
     404 * returns a list of categories (web service method)
    401405 */
    402406function ws_categories_getList($params, &$service)
     
    467471}
    468472
     473
     474/**
     475 * returns detailed information for an element (web service method)
     476 */
    469477function ws_images_getInfo($params, &$service)
    470478{
     
    476484    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
    477485  }
    478  
     486
    479487  $query='
    480488SELECT * FROM '.IMAGES_TABLE.'
     
    484492      ' AND'
    485493    ).' AND '.
    486     ws_addControls( 'images.getInfo', '' ).'
     494    ws_addControls( 'images.getInfo', $params, '' ).'
    487495LIMIT 1;';
    488496
     
    601609
    602610
     611/**
     612 * perform a login (web service method)
     613 */
    603614function ws_session_login($params, &$service)
    604615{
     
    616627}
    617628
     629
     630/**
     631 * performs a logout (web service method)
     632 */
    618633function ws_session_logout($params, &$service)
    619634{
     
    643658
    644659
     660/**
     661 * returns a list of tags (web service method)
     662 */
    645663function ws_tags_getList($params, &$service)
    646664{
     
    670688}
    671689
     690
     691/**
     692 * returns a list of images for tags (web service method)
     693 */
    672694function ws_tags_getImages($params, &$service)
    673695{
     
    710732  $image_ids = array();
    711733  $image_tag_map = array();
    712  
     734
    713735  if ( !empty($tag_ids) )
    714736  { // build list of image ids with associated tags per image
     
    748770      );
    749771    $where_clauses[] = 'id IN ('.implode(',',$image_ids).')';
    750     $where_clause[] =
    751             ws_addControls( 'tags.getImages', 'i.' );
     772    $where_clauses[] = ws_addControls( 'tags.getImages', $params, 'i.' );
    752773
    753774    $order_by = ws_std_image_sql_order($params);
     
    831852}
    832853
    833 /**
    834  * official_req returns the managed requests list in array format
    835  * FIXME A New list need to be build for ws_checker.php
    836  * returns array of authrorized request/methods
    837  * */   
    838 function official_req()
    839 {
    840   $official = array(                  /* Requests are limited to             */
    841       'categories.'                          /* all categories. methods */
    842     , 'categories.getImages'                 /* <= see */
    843     , 'categories.getList'                   /* <= see */
    844     , 'images.'                              /* all images. methods */
    845     , 'images.getInfo'                       /* <= see */
    846     , 'tags.'                                /* all tags. methods */
    847     , 'tags.getImages'                       /* <= see */
    848     , 'tags.getList'                         /* <= see */
    849   );
    850   if (function_exists('local_req')) {
    851      $local = local_req();
    852      return array_merge( $official, $local );
    853   }
    854   return $official;
    855 }
    856854
    857855/**
    858856 * expand_id_list($ids) convert a human list expression to a full ordered list
    859857 * example : expand_id_list( array(5,2-3,2) ) returns array( 2, 3, 5)
    860  * */ 
     858 * */
    861859function expand_id_list($ids)
    862860{
    863     $tid = array();
    864     foreach ( $ids as $id )
    865     {
    866       if ( is_numeric($id) )
     861  $tid = array();
     862  foreach ( $ids as $id )
     863  {
     864    if ( is_numeric($id) )
     865    {
     866      $tid[] = (int) $id;
     867    }
     868    else
     869    {
     870      $range = explode( '-', $id );
     871      if ( is_numeric($range[0]) and is_numeric($range[1]) )
    867872      {
    868         $tid[] = (int) $id;
    869       }
    870       else
    871       {
    872         $range = explode( '-', $id );
    873         if ( is_numeric($range[0]) and is_numeric($range[1]) )
     873        $from = min($range[0],$range[1]);
     874        $to = max($range[0],$range[1]);
     875        for ($i = $from; $i <= $to; $i++)
    874876        {
    875           $from = min($range[0],$range[1]);
    876           $to = max($range[0],$range[1]);
    877           for ($i = $from; $i <= $to; $i++)
    878           {
    879             $tid[] = (int) $i;
    880           }
     877          $tid[] = (int) $i;
    881878        }
    882879      }
    883880    }
    884     $result = array_unique ($tid); // remove duplicates...
    885     sort ($result);
    886     return $result;
    887 }
    888 
    889 /**
    890  * check_target($string) verifies and corrects syntax of target parameter
    891  * example : check_target(cat/23,24,24,24,25,27) returns cat/23-25,27
    892  * */
    893 function check_target($list)
    894 {
    895   if ( $list !== '' )
    896   {
    897     $type = explode('/',$list); // Find type list
    898     if ( !in_array($type[0],array('list','cat','tag') ) )
    899     {
    900       $type[0] = 'list'; // Assume an id list
    901     }
    902     $ids = explode( ',',$type[1] );
    903     $list = $type[0] . '/';
    904 
    905     // 1,2,21,3,22,4,5,9-12,6,11,12,13,2,4,6,
    906 
    907     $result = expand_id_list( $ids );
    908 
    909     // 1,2,3,4,5,6,9,10,11,12,13,21,22,
    910     // I would like
    911     // 1-6,9-13,21-22
    912     $serial[] = $result[0]; // To be shifted                     
    913     foreach ($result as $k => $id)
    914     {
    915       $next_less_1 = (isset($result[$k + 1]))? $result[$k + 1] - 1:-1;
    916       if ( $id == $next_less_1 and end($serial)=='-' )
    917       { // nothing to do
    918       }
    919       elseif ( $id == $next_less_1 )
    920       {
    921         $serial[]=$id;
    922         $serial[]='-';
    923       }
    924       else
    925       {
    926         $serial[]=$id;  // end serie or non serie
    927       }
    928     }
    929     $null = array_shift($serial); // remove first value
    930     $list .= array_shift($serial); // add the real first one
    931     $separ = ',';
    932     foreach ($serial as $id)
    933     {
    934       $list .= ($id=='-') ? '' : $separ . $id;
    935       $separ = ($id=='-') ? '-':','; // add comma except if hyphen
    936     }
    937   }
    938   return $list;
    939 }
     881  }
     882  $result = array_unique ($tid); // remove duplicates...
     883  sort ($result);
     884  return $result;
     885}
     886
    940887
    941888/**
    942889 * converts a cat-ids array in image-ids array
    943890 * FIXME Function which should already exist somewhere else
    944  * */ 
     891 * */
    945892function get_image_ids_for_cats($cat_ids)
    946893{
     
    948895  $ret_ids = array();
    949896  $query = '
    950   SELECT DISTINCT image_id 
     897  SELECT DISTINCT image_id
    951898    FROM '.IMAGE_CATEGORY_TABLE.'
    952899  WHERE category_id in ('.$cat_list.')
    953900  ;';
    954   return $array_from_query($query, 'image_id');
     901  return array_from_query($query, 'image_id');
    955902}
    956903
Note: See TracChangeset for help on using the changeset viewer.