| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | Piwigo - a PHP based picture gallery | |
|---|
| 4 | // +-----------------------------------------------------------------------+ |
|---|
| 5 | // | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
|---|
| 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
|---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
|---|
| 8 | // +-----------------------------------------------------------------------+ |
|---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
|---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
|---|
| 11 | // | the Free Software Foundation | |
|---|
| 12 | // | | |
|---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
|---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
|---|
| 16 | // | General Public License for more details. | |
|---|
| 17 | // | | |
|---|
| 18 | // | You should have received a copy of the GNU General Public License | |
|---|
| 19 | // | along with this program; if not, write to the Free Software | |
|---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
|---|
| 21 | // | USA. | |
|---|
| 22 | // +-----------------------------------------------------------------------+ |
|---|
| 23 | |
|---|
| 24 | /**** IMPLEMENTATION OF WEB SERVICE METHODS ***********************************/ |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Event handler for method invocation security check. Should return a PwgError |
|---|
| 28 | * if the preconditions are not satifsied for method invocation. |
|---|
| 29 | */ |
|---|
| 30 | function ws_isInvokeAllowed($res, $methodName, $params) |
|---|
| 31 | { |
|---|
| 32 | global $conf, $calling_partner_id; |
|---|
| 33 | |
|---|
| 34 | if ( strpos($methodName,'reflection.')===0 ) |
|---|
| 35 | { // OK for reflection |
|---|
| 36 | return $res; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | if ( !is_autorize_status(ACCESS_GUEST) and |
|---|
| 40 | strpos($methodName,'pwg.session.')!==0 ) |
|---|
| 41 | { |
|---|
| 42 | return new PwgError(401, 'Access denied'); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | return $res; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * returns a "standard" (for our web service) array of sql where clauses that |
|---|
| 50 | * filters the images (images table only) |
|---|
| 51 | */ |
|---|
| 52 | function ws_std_image_sql_filter( $params, $tbl_name='' ) |
|---|
| 53 | { |
|---|
| 54 | $clauses = array(); |
|---|
| 55 | if ( is_numeric($params['f_min_rate']) ) |
|---|
| 56 | { |
|---|
| 57 | $clauses[] = $tbl_name.'average_rate>'.$params['f_min_rate']; |
|---|
| 58 | } |
|---|
| 59 | if ( is_numeric($params['f_max_rate']) ) |
|---|
| 60 | { |
|---|
| 61 | $clauses[] = $tbl_name.'average_rate<='.$params['f_max_rate']; |
|---|
| 62 | } |
|---|
| 63 | if ( is_numeric($params['f_min_hit']) ) |
|---|
| 64 | { |
|---|
| 65 | $clauses[] = $tbl_name.'hit>'.$params['f_min_hit']; |
|---|
| 66 | } |
|---|
| 67 | if ( is_numeric($params['f_max_hit']) ) |
|---|
| 68 | { |
|---|
| 69 | $clauses[] = $tbl_name.'hit<='.$params['f_max_hit']; |
|---|
| 70 | } |
|---|
| 71 | if ( isset($params['f_min_date_posted']) ) |
|---|
| 72 | { |
|---|
| 73 | $clauses[] = $tbl_name."date_available>='".$params['f_min_date_posted']."'"; |
|---|
| 74 | } |
|---|
| 75 | if ( isset($params['f_max_date_posted']) ) |
|---|
| 76 | { |
|---|
| 77 | $clauses[] = $tbl_name."date_available<'".$params['f_max_date_posted']."'"; |
|---|
| 78 | } |
|---|
| 79 | if ( isset($params['f_min_date_created']) ) |
|---|
| 80 | { |
|---|
| 81 | $clauses[] = $tbl_name."date_creation>='".$params['f_min_date_created']."'"; |
|---|
| 82 | } |
|---|
| 83 | if ( isset($params['f_max_date_created']) ) |
|---|
| 84 | { |
|---|
| 85 | $clauses[] = $tbl_name."date_creation<'".$params['f_max_date_created']."'"; |
|---|
| 86 | } |
|---|
| 87 | if ( is_numeric($params['f_min_ratio']) ) |
|---|
| 88 | { |
|---|
| 89 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height>'.$params['f_min_ratio']; |
|---|
| 90 | } |
|---|
| 91 | if ( is_numeric($params['f_max_ratio']) ) |
|---|
| 92 | { |
|---|
| 93 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio']; |
|---|
| 94 | } |
|---|
| 95 | if ( $params['f_with_thumbnail'] ) |
|---|
| 96 | { |
|---|
| 97 | $clauses[] = $tbl_name.'tn_ext IS NOT NULL'; |
|---|
| 98 | } |
|---|
| 99 | return $clauses; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * returns a "standard" (for our web service) ORDER BY sql clause for images |
|---|
| 104 | */ |
|---|
| 105 | function ws_std_image_sql_order( $params, $tbl_name='' ) |
|---|
| 106 | { |
|---|
| 107 | $ret = ''; |
|---|
| 108 | if ( empty($params['order']) ) |
|---|
| 109 | { |
|---|
| 110 | return $ret; |
|---|
| 111 | } |
|---|
| 112 | $matches = array(); |
|---|
| 113 | preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i', |
|---|
| 114 | $params['order'], $matches); |
|---|
| 115 | for ($i=0; $i<count($matches[1]); $i++) |
|---|
| 116 | { |
|---|
| 117 | switch ($matches[1][$i]) |
|---|
| 118 | { |
|---|
| 119 | case 'date_created': |
|---|
| 120 | $matches[1][$i] = 'date_creation'; break; |
|---|
| 121 | case 'date_posted': |
|---|
| 122 | $matches[1][$i] = 'date_available'; break; |
|---|
| 123 | case 'rand': case 'random': |
|---|
| 124 | $matches[1][$i] = 'RAND()'; break; |
|---|
| 125 | } |
|---|
| 126 | $sortable_fields = array('id', 'file', 'name', 'hit', 'average_rate', |
|---|
| 127 | 'date_creation', 'date_available', 'RAND()' ); |
|---|
| 128 | if ( in_array($matches[1][$i], $sortable_fields) ) |
|---|
| 129 | { |
|---|
| 130 | if (!empty($ret)) |
|---|
| 131 | $ret .= ', '; |
|---|
| 132 | if ($matches[1][$i] != 'RAND()' ) |
|---|
| 133 | { |
|---|
| 134 | $ret .= $tbl_name; |
|---|
| 135 | } |
|---|
| 136 | $ret .= $matches[1][$i]; |
|---|
| 137 | $ret .= ' '.$matches[2][$i]; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | return $ret; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * returns an array map of urls (thumb/element) for image_row - to be returned |
|---|
| 145 | * in a standard way by different web service methods |
|---|
| 146 | */ |
|---|
| 147 | function ws_std_get_urls($image_row) |
|---|
| 148 | { |
|---|
| 149 | $ret = array( |
|---|
| 150 | 'tn_url' => get_thumbnail_url($image_row), |
|---|
| 151 | 'element_url' => get_element_url($image_row) |
|---|
| 152 | ); |
|---|
| 153 | global $user; |
|---|
| 154 | if ($user['enabled_high'] and $image_row['has_high'] ) |
|---|
| 155 | { |
|---|
| 156 | $ret['high_url'] = get_high_url($image_row); |
|---|
| 157 | } |
|---|
| 158 | return $ret; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * returns an array of image attributes that are to be encoded as xml attributes |
|---|
| 163 | * instead of xml elements |
|---|
| 164 | */ |
|---|
| 165 | function ws_std_get_image_xml_attributes() |
|---|
| 166 | { |
|---|
| 167 | return array( |
|---|
| 168 | 'id','tn_url','element_url','high_url', 'file','width','height','hit' |
|---|
| 169 | ); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * returns PWG version (web service method) |
|---|
| 174 | */ |
|---|
| 175 | function ws_getVersion($params, &$service) |
|---|
| 176 | { |
|---|
| 177 | global $conf; |
|---|
| 178 | if ($conf['show_version']) |
|---|
| 179 | return PHPWG_VERSION; |
|---|
| 180 | else |
|---|
| 181 | return new PwgError(403, 'Forbidden'); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | function ws_caddie_add($params, &$service) |
|---|
| 185 | { |
|---|
| 186 | if (!is_admin()) |
|---|
| 187 | { |
|---|
| 188 | return new PwgError(401, 'Access denied'); |
|---|
| 189 | } |
|---|
| 190 | if ( empty($params['image_id']) ) |
|---|
| 191 | { |
|---|
| 192 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 193 | } |
|---|
| 194 | global $user; |
|---|
| 195 | $query = ' |
|---|
| 196 | SELECT id |
|---|
| 197 | FROM '.IMAGES_TABLE.' LEFT JOIN '.CADDIE_TABLE.' ON id=element_id AND user_id='.$user['id'].' |
|---|
| 198 | WHERE id IN ('.implode(',',$params['image_id']).') |
|---|
| 199 | AND element_id IS NULL'; |
|---|
| 200 | $datas = array(); |
|---|
| 201 | foreach ( array_from_query($query, 'id') as $id ) |
|---|
| 202 | { |
|---|
| 203 | array_push($datas, array('element_id'=>$id, 'user_id'=>$user['id']) ); |
|---|
| 204 | } |
|---|
| 205 | if (count($datas)) |
|---|
| 206 | { |
|---|
| 207 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 208 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
|---|
| 209 | } |
|---|
| 210 | return count($datas); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * returns images per category (web service method) |
|---|
| 215 | */ |
|---|
| 216 | function ws_categories_getImages($params, &$service) |
|---|
| 217 | { |
|---|
| 218 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 219 | global $user, $conf; |
|---|
| 220 | |
|---|
| 221 | $images = array(); |
|---|
| 222 | |
|---|
| 223 | //------------------------------------------------- get the related categories |
|---|
| 224 | $where_clauses = array(); |
|---|
| 225 | foreach($params['cat_id'] as $cat_id) |
|---|
| 226 | { |
|---|
| 227 | $cat_id = (int)$cat_id; |
|---|
| 228 | if ($cat_id<=0) |
|---|
| 229 | continue; |
|---|
| 230 | if ($params['recursive']) |
|---|
| 231 | { |
|---|
| 232 | $where_clauses[] = 'uppercats REGEXP \'(^|,)'.$cat_id.'(,|$)\''; |
|---|
| 233 | } |
|---|
| 234 | else |
|---|
| 235 | { |
|---|
| 236 | $where_clauses[] = 'id='.$cat_id; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | if (!empty($where_clauses)) |
|---|
| 240 | { |
|---|
| 241 | $where_clauses = array( '('. |
|---|
| 242 | implode(' |
|---|
| 243 | OR ', $where_clauses) . ')' |
|---|
| 244 | ); |
|---|
| 245 | } |
|---|
| 246 | $where_clauses[] = get_sql_condition_FandF( |
|---|
| 247 | array('forbidden_categories' => 'id'), |
|---|
| 248 | NULL, true |
|---|
| 249 | ); |
|---|
| 250 | |
|---|
| 251 | $query = ' |
|---|
| 252 | SELECT id, name, permalink, image_order |
|---|
| 253 | FROM '.CATEGORIES_TABLE.' |
|---|
| 254 | WHERE '. implode(' |
|---|
| 255 | AND ', $where_clauses); |
|---|
| 256 | $result = pwg_query($query); |
|---|
| 257 | $cats = array(); |
|---|
| 258 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 259 | { |
|---|
| 260 | $row['id'] = (int)$row['id']; |
|---|
| 261 | $cats[ $row['id'] ] = $row; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | //-------------------------------------------------------- get the images |
|---|
| 265 | if ( !empty($cats) ) |
|---|
| 266 | { |
|---|
| 267 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
|---|
| 268 | $where_clauses[] = 'category_id IN (' |
|---|
| 269 | .implode(',', array_keys($cats) ) |
|---|
| 270 | .')'; |
|---|
| 271 | $where_clauses[] = get_sql_condition_FandF( array( |
|---|
| 272 | 'visible_images' => 'i.id' |
|---|
| 273 | ), null, true |
|---|
| 274 | ); |
|---|
| 275 | |
|---|
| 276 | $order_by = ws_std_image_sql_order($params, 'i.'); |
|---|
| 277 | if ( empty($order_by) |
|---|
| 278 | and count($params['cat_id'])==1 |
|---|
| 279 | and isset($cats[ $params['cat_id'][0] ]['image_order']) |
|---|
| 280 | ) |
|---|
| 281 | { |
|---|
| 282 | $order_by = $cats[ $params['cat_id'][0] ]['image_order']; |
|---|
| 283 | } |
|---|
| 284 | $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; |
|---|
| 285 | |
|---|
| 286 | $query = ' |
|---|
| 287 | SELECT i.*, GROUP_CONCAT(category_id) cat_ids |
|---|
| 288 | FROM '.IMAGES_TABLE.' i |
|---|
| 289 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
|---|
| 290 | WHERE '. implode(' |
|---|
| 291 | AND ', $where_clauses).' |
|---|
| 292 | GROUP BY i.id |
|---|
| 293 | '.$order_by.' |
|---|
| 294 | LIMIT '.$params['per_page']*$params['page'].','.$params['per_page']; |
|---|
| 295 | |
|---|
| 296 | $result = pwg_query($query); |
|---|
| 297 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 298 | { |
|---|
| 299 | $image = array(); |
|---|
| 300 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
|---|
| 301 | { |
|---|
| 302 | if (isset($row[$k])) |
|---|
| 303 | { |
|---|
| 304 | $image[$k] = (int)$row[$k]; |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 308 | { |
|---|
| 309 | $image[$k] = $row[$k]; |
|---|
| 310 | } |
|---|
| 311 | $image = array_merge( $image, ws_std_get_urls($row) ); |
|---|
| 312 | |
|---|
| 313 | $image_cats = array(); |
|---|
| 314 | foreach ( explode(',', $row['cat_ids']) as $cat_id ) |
|---|
| 315 | { |
|---|
| 316 | $url = make_index_url( |
|---|
| 317 | array( |
|---|
| 318 | 'category' => $cats[$cat_id], |
|---|
| 319 | ) |
|---|
| 320 | ); |
|---|
| 321 | $page_url = make_picture_url( |
|---|
| 322 | array( |
|---|
| 323 | 'category' => $cats[$cat_id], |
|---|
| 324 | 'image_id' => $row['id'], |
|---|
| 325 | 'image_file' => $row['file'], |
|---|
| 326 | ) |
|---|
| 327 | ); |
|---|
| 328 | array_push( $image_cats, array( |
|---|
| 329 | WS_XML_ATTRIBUTES => array ( |
|---|
| 330 | 'id' => (int)$cat_id, |
|---|
| 331 | 'url' => $url, |
|---|
| 332 | 'page_url' => $page_url, |
|---|
| 333 | ) |
|---|
| 334 | ) |
|---|
| 335 | ); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | $image['categories'] = new PwgNamedArray( |
|---|
| 339 | $image_cats,'category', array('id','url','page_url') |
|---|
| 340 | ); |
|---|
| 341 | array_push($images, $image); |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | return array( 'images' => |
|---|
| 346 | array ( |
|---|
| 347 | WS_XML_ATTRIBUTES => |
|---|
| 348 | array( |
|---|
| 349 | 'page' => $params['page'], |
|---|
| 350 | 'per_page' => $params['per_page'], |
|---|
| 351 | 'count' => count($images) |
|---|
| 352 | ), |
|---|
| 353 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 354 | ws_std_get_image_xml_attributes() ) |
|---|
| 355 | ) |
|---|
| 356 | ); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * returns a list of categories (web service method) |
|---|
| 362 | */ |
|---|
| 363 | function ws_categories_getList($params, &$service) |
|---|
| 364 | { |
|---|
| 365 | global $user,$conf; |
|---|
| 366 | |
|---|
| 367 | $where = array(); |
|---|
| 368 | |
|---|
| 369 | if (!$params['recursive']) |
|---|
| 370 | { |
|---|
| 371 | if ($params['cat_id']>0) |
|---|
| 372 | $where[] = '(id_uppercat='.(int)($params['cat_id']).' |
|---|
| 373 | OR id='.(int)($params['cat_id']).')'; |
|---|
| 374 | else |
|---|
| 375 | $where[] = 'id_uppercat IS NULL'; |
|---|
| 376 | } |
|---|
| 377 | else if ($params['cat_id']>0) |
|---|
| 378 | { |
|---|
| 379 | $where[] = 'uppercats REGEXP \'(^|,)'. |
|---|
| 380 | (int)($params['cat_id']) |
|---|
| 381 | .'(,|$)\''; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | if ($params['public']) |
|---|
| 385 | { |
|---|
| 386 | $where[] = 'status = "public"'; |
|---|
| 387 | $where[] = 'visible = "true"'; |
|---|
| 388 | $where[]= 'user_id='.$conf['guest_id']; |
|---|
| 389 | } |
|---|
| 390 | else |
|---|
| 391 | { |
|---|
| 392 | $where[]= 'user_id='.$user['id']; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | $query = ' |
|---|
| 396 | SELECT id, name, permalink, uppercats, global_rank, |
|---|
| 397 | nb_images, count_images AS total_nb_images, |
|---|
| 398 | date_last, max_date_last, count_categories AS nb_categories |
|---|
| 399 | FROM '.CATEGORIES_TABLE.' |
|---|
| 400 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id |
|---|
| 401 | WHERE '. implode(' |
|---|
| 402 | AND ', $where); |
|---|
| 403 | |
|---|
| 404 | $result = pwg_query($query); |
|---|
| 405 | |
|---|
| 406 | $cats = array(); |
|---|
| 407 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 408 | { |
|---|
| 409 | $row['url'] = make_index_url( |
|---|
| 410 | array( |
|---|
| 411 | 'category' => $row |
|---|
| 412 | ) |
|---|
| 413 | ); |
|---|
| 414 | foreach( array('id','nb_images','total_nb_images','nb_categories') as $key) |
|---|
| 415 | { |
|---|
| 416 | $row[$key] = (int)$row[$key]; |
|---|
| 417 | } |
|---|
| 418 | array_push($cats, $row); |
|---|
| 419 | } |
|---|
| 420 | usort($cats, 'global_rank_compare'); |
|---|
| 421 | return array( |
|---|
| 422 | 'categories' => |
|---|
| 423 | new PwgNamedArray($cats,'category', |
|---|
| 424 | array('id','url','nb_images','total_nb_images','nb_categories','date_last','max_date_last') |
|---|
| 425 | ) |
|---|
| 426 | ); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | /** |
|---|
| 431 | * returns detailed information for an element (web service method) |
|---|
| 432 | */ |
|---|
| 433 | function ws_images_addComment($params, &$service) |
|---|
| 434 | { |
|---|
| 435 | if (!$service->isPost()) |
|---|
| 436 | { |
|---|
| 437 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 438 | } |
|---|
| 439 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 440 | $query = ' |
|---|
| 441 | SELECT DISTINCT image_id |
|---|
| 442 | FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id |
|---|
| 443 | WHERE commentable="true" |
|---|
| 444 | AND image_id='.$params['image_id']. |
|---|
| 445 | get_sql_condition_FandF( |
|---|
| 446 | array( |
|---|
| 447 | 'forbidden_categories' => 'id', |
|---|
| 448 | 'visible_categories' => 'id', |
|---|
| 449 | 'visible_images' => 'image_id' |
|---|
| 450 | ), |
|---|
| 451 | ' AND' |
|---|
| 452 | ); |
|---|
| 453 | if ( !mysql_num_rows( pwg_query( $query ) ) ) |
|---|
| 454 | { |
|---|
| 455 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
|---|
| 459 | |
|---|
| 460 | $comm = array( |
|---|
| 461 | 'author' => trim( stripslashes($params['author']) ), |
|---|
| 462 | 'content' => trim( stripslashes($params['content']) ), |
|---|
| 463 | 'image_id' => $params['image_id'], |
|---|
| 464 | ); |
|---|
| 465 | |
|---|
| 466 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
|---|
| 467 | |
|---|
| 468 | $comment_action = insert_user_comment( |
|---|
| 469 | $comm, $params['key'], $infos |
|---|
| 470 | ); |
|---|
| 471 | |
|---|
| 472 | switch ($comment_action) |
|---|
| 473 | { |
|---|
| 474 | case 'reject': |
|---|
| 475 | array_push($infos, l10n('comment_not_added') ); |
|---|
| 476 | return new PwgError(403, implode("\n", $infos) ); |
|---|
| 477 | case 'validate': |
|---|
| 478 | case 'moderate': |
|---|
| 479 | $ret = array( |
|---|
| 480 | 'id' => $comm['id'], |
|---|
| 481 | 'validation' => $comment_action=='validate', |
|---|
| 482 | ); |
|---|
| 483 | return new PwgNamedStruct( |
|---|
| 484 | 'comment', |
|---|
| 485 | $ret, |
|---|
| 486 | null, array() |
|---|
| 487 | ); |
|---|
| 488 | default: |
|---|
| 489 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | /** |
|---|
| 494 | * returns detailed information for an element (web service method) |
|---|
| 495 | */ |
|---|
| 496 | function ws_images_getInfo($params, &$service) |
|---|
| 497 | { |
|---|
| 498 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 499 | global $user, $conf; |
|---|
| 500 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 501 | if ( $params['image_id']<=0 ) |
|---|
| 502 | { |
|---|
| 503 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | $query=' |
|---|
| 507 | SELECT * FROM '.IMAGES_TABLE.' |
|---|
| 508 | WHERE id='.$params['image_id']. |
|---|
| 509 | get_sql_condition_FandF( |
|---|
| 510 | array('visible_images' => 'id'), |
|---|
| 511 | ' AND' |
|---|
| 512 | ).' |
|---|
| 513 | LIMIT 1'; |
|---|
| 514 | |
|---|
| 515 | $image_row = mysql_fetch_assoc(pwg_query($query)); |
|---|
| 516 | if ($image_row==null) |
|---|
| 517 | { |
|---|
| 518 | return new PwgError(404, "image_id not found"); |
|---|
| 519 | } |
|---|
| 520 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
|---|
| 521 | |
|---|
| 522 | //-------------------------------------------------------- related categories |
|---|
| 523 | $query = ' |
|---|
| 524 | SELECT id, name, permalink, uppercats, global_rank, commentable |
|---|
| 525 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 526 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
|---|
| 527 | WHERE image_id = '.$image_row['id']. |
|---|
| 528 | get_sql_condition_FandF( |
|---|
| 529 | array( 'forbidden_categories' => 'category_id' ), |
|---|
| 530 | ' AND' |
|---|
| 531 | ).' |
|---|
| 532 | ;'; |
|---|
| 533 | $result = pwg_query($query); |
|---|
| 534 | $is_commentable = false; |
|---|
| 535 | $related_categories = array(); |
|---|
| 536 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 537 | { |
|---|
| 538 | if ($row['commentable']=='true') |
|---|
| 539 | { |
|---|
| 540 | $is_commentable = true; |
|---|
| 541 | } |
|---|
| 542 | unset($row['commentable']); |
|---|
| 543 | $row['url'] = make_index_url( |
|---|
| 544 | array( |
|---|
| 545 | 'category' => $row |
|---|
| 546 | ) |
|---|
| 547 | ); |
|---|
| 548 | |
|---|
| 549 | $row['page_url'] = make_picture_url( |
|---|
| 550 | array( |
|---|
| 551 | 'image_id' => $image_row['id'], |
|---|
| 552 | 'image_file' => $image_row['file'], |
|---|
| 553 | 'category' => $row |
|---|
| 554 | ) |
|---|
| 555 | ); |
|---|
| 556 | $row['id']=(int)$row['id']; |
|---|
| 557 | array_push($related_categories, $row); |
|---|
| 558 | } |
|---|
| 559 | usort($related_categories, 'global_rank_compare'); |
|---|
| 560 | if ( empty($related_categories) ) |
|---|
| 561 | { |
|---|
| 562 | return new PwgError(401, 'Access denied'); |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | //-------------------------------------------------------------- related tags |
|---|
| 566 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
|---|
| 567 | foreach( $related_tags as $i=>$tag) |
|---|
| 568 | { |
|---|
| 569 | $tag['url'] = make_index_url( |
|---|
| 570 | array( |
|---|
| 571 | 'tags' => array($tag) |
|---|
| 572 | ) |
|---|
| 573 | ); |
|---|
| 574 | $tag['page_url'] = make_picture_url( |
|---|
| 575 | array( |
|---|
| 576 | 'image_id' => $image_row['id'], |
|---|
| 577 | 'image_file' => $image_row['file'], |
|---|
| 578 | 'tags' => array($tag), |
|---|
| 579 | ) |
|---|
| 580 | ); |
|---|
| 581 | unset($tag['counter']); |
|---|
| 582 | $tag['id']=(int)$tag['id']; |
|---|
| 583 | $related_tags[$i]=$tag; |
|---|
| 584 | } |
|---|
| 585 | //------------------------------------------------------------- related rates |
|---|
| 586 | $query = ' |
|---|
| 587 | SELECT COUNT(rate) AS count |
|---|
| 588 | , ROUND(AVG(rate),2) AS average |
|---|
| 589 | , ROUND(STD(rate),2) AS stdev |
|---|
| 590 | FROM '.RATE_TABLE.' |
|---|
| 591 | WHERE element_id = '.$image_row['id'].' |
|---|
| 592 | ;'; |
|---|
| 593 | $rating = mysql_fetch_assoc(pwg_query($query)); |
|---|
| 594 | $rating['count'] = (int)$rating['count']; |
|---|
| 595 | |
|---|
| 596 | //---------------------------------------------------------- related comments |
|---|
| 597 | $related_comments = array(); |
|---|
| 598 | |
|---|
| 599 | $where_comments = 'image_id = '.$image_row['id']; |
|---|
| 600 | if ( !is_admin() ) |
|---|
| 601 | { |
|---|
| 602 | $where_comments .= ' |
|---|
| 603 | AND validated="true"'; |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | $query = ' |
|---|
| 607 | SELECT COUNT(id) nb_comments |
|---|
| 608 | FROM '.COMMENTS_TABLE.' |
|---|
| 609 | WHERE '.$where_comments; |
|---|
| 610 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
|---|
| 611 | $nb_comments = (int)$nb_comments; |
|---|
| 612 | |
|---|
| 613 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
|---|
| 614 | { |
|---|
| 615 | $query = ' |
|---|
| 616 | SELECT id, date, author, content |
|---|
| 617 | FROM '.COMMENTS_TABLE.' |
|---|
| 618 | WHERE '.$where_comments.' |
|---|
| 619 | ORDER BY date |
|---|
| 620 | LIMIT '.$params['comments_per_page']*(int)$params['comments_page']. |
|---|
| 621 | ','.$params['comments_per_page']; |
|---|
| 622 | |
|---|
| 623 | $result = pwg_query($query); |
|---|
| 624 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 625 | { |
|---|
| 626 | $row['id']=(int)$row['id']; |
|---|
| 627 | array_push($related_comments, $row); |
|---|
| 628 | } |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | $comment_post_data = null; |
|---|
| 632 | if ($is_commentable and |
|---|
| 633 | (!is_a_guest() |
|---|
| 634 | or (is_a_guest() and $conf['comments_forall'] ) |
|---|
| 635 | ) |
|---|
| 636 | ) |
|---|
| 637 | { |
|---|
| 638 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
|---|
| 639 | $comment_post_data['author'] = $user['username']; |
|---|
| 640 | $comment_post_data['key'] = get_comment_post_key($params['image_id']); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | $ret = $image_row; |
|---|
| 644 | foreach ( array('id','width','height','hit','filesize') as $k ) |
|---|
| 645 | { |
|---|
| 646 | if (isset($ret[$k])) |
|---|
| 647 | { |
|---|
| 648 | $ret[$k] = (int)$ret[$k]; |
|---|
| 649 | } |
|---|
| 650 | } |
|---|
| 651 | foreach ( array('path', 'storage_category_id') as $k ) |
|---|
| 652 | { |
|---|
| 653 | unset($ret[$k]); |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
|---|
| 657 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
|---|
| 658 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','page_url') ); |
|---|
| 659 | if ( isset($comment_post_data) ) |
|---|
| 660 | { |
|---|
| 661 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
|---|
| 662 | } |
|---|
| 663 | $ret['comments'] = array( |
|---|
| 664 | WS_XML_ATTRIBUTES => |
|---|
| 665 | array( |
|---|
| 666 | 'page' => $params['comments_page'], |
|---|
| 667 | 'per_page' => $params['comments_per_page'], |
|---|
| 668 | 'count' => count($related_comments), |
|---|
| 669 | 'nb_comments' => $nb_comments, |
|---|
| 670 | ), |
|---|
| 671 | WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') ) |
|---|
| 672 | ); |
|---|
| 673 | |
|---|
| 674 | return new PwgNamedStruct('image',$ret, null, array('name','comment') ); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | /** |
|---|
| 679 | * rates the image_id in the parameter |
|---|
| 680 | */ |
|---|
| 681 | function ws_images_Rate($params, &$service) |
|---|
| 682 | { |
|---|
| 683 | $image_id = (int)$params['image_id']; |
|---|
| 684 | $query = ' |
|---|
| 685 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
|---|
| 686 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
|---|
| 687 | WHERE id='.$image_id |
|---|
| 688 | .get_sql_condition_FandF( |
|---|
| 689 | array( |
|---|
| 690 | 'forbidden_categories' => 'category_id', |
|---|
| 691 | 'forbidden_images' => 'id', |
|---|
| 692 | ), |
|---|
| 693 | ' AND' |
|---|
| 694 | ).' |
|---|
| 695 | LIMIT 1'; |
|---|
| 696 | if ( mysql_num_rows( pwg_query($query) )==0 ) |
|---|
| 697 | { |
|---|
| 698 | return new PwgError(404, "Invalid image_id or access denied" ); |
|---|
| 699 | } |
|---|
| 700 | $rate = (int)$params['rate']; |
|---|
| 701 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
|---|
| 702 | $res = rate_picture( $image_id, $rate ); |
|---|
| 703 | if ($res==false) |
|---|
| 704 | { |
|---|
| 705 | global $conf; |
|---|
| 706 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
|---|
| 707 | } |
|---|
| 708 | return $res; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | /** |
|---|
| 713 | * returns a list of elements corresponding to a query search |
|---|
| 714 | */ |
|---|
| 715 | function ws_images_search($params, &$service) |
|---|
| 716 | { |
|---|
| 717 | global $page; |
|---|
| 718 | $images = array(); |
|---|
| 719 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
|---|
| 720 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 721 | |
|---|
| 722 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
|---|
| 723 | $order_by = ws_std_image_sql_order($params, 'i.'); |
|---|
| 724 | |
|---|
| 725 | $super_order_by = false; |
|---|
| 726 | if ( !empty($order_by) ) |
|---|
| 727 | { |
|---|
| 728 | global $conf; |
|---|
| 729 | $conf['order_by'] = 'ORDER BY '.$order_by; |
|---|
| 730 | $super_order_by=true; // quick_search_result might be faster |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | $search_result = get_quick_search_results($params['query'], |
|---|
| 734 | $super_order_by, |
|---|
| 735 | implode(',', $where_clauses) |
|---|
| 736 | ); |
|---|
| 737 | |
|---|
| 738 | $image_ids = array_slice( |
|---|
| 739 | $search_result['items'], |
|---|
| 740 | $params['page']*$params['per_page'], |
|---|
| 741 | $params['per_page'] |
|---|
| 742 | ); |
|---|
| 743 | |
|---|
| 744 | if ( count($image_ids) ) |
|---|
| 745 | { |
|---|
| 746 | $query = ' |
|---|
| 747 | SELECT * FROM '.IMAGES_TABLE.' |
|---|
| 748 | WHERE id IN ('.implode(',', $image_ids).')'; |
|---|
| 749 | |
|---|
| 750 | $image_ids = array_flip($image_ids); |
|---|
| 751 | $result = pwg_query($query); |
|---|
| 752 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 753 | { |
|---|
| 754 | $image = array(); |
|---|
| 755 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
|---|
| 756 | { |
|---|
| 757 | if (isset($row[$k])) |
|---|
| 758 | { |
|---|
| 759 | $image[$k] = (int)$row[$k]; |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 763 | { |
|---|
| 764 | $image[$k] = $row[$k]; |
|---|
| 765 | } |
|---|
| 766 | $image = array_merge( $image, ws_std_get_urls($row) ); |
|---|
| 767 | $images[$image_ids[$image['id']]] = $image; |
|---|
| 768 | } |
|---|
| 769 | ksort($images, SORT_NUMERIC); |
|---|
| 770 | $images = array_values($images); |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | return array( 'images' => |
|---|
| 775 | array ( |
|---|
| 776 | WS_XML_ATTRIBUTES => |
|---|
| 777 | array( |
|---|
| 778 | 'page' => $params['page'], |
|---|
| 779 | 'per_page' => $params['per_page'], |
|---|
| 780 | 'count' => count($images) |
|---|
| 781 | ), |
|---|
| 782 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 783 | ws_std_get_image_xml_attributes() ) |
|---|
| 784 | ) |
|---|
| 785 | ); |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | function ws_images_setPrivacyLevel($params, &$service) |
|---|
| 789 | { |
|---|
| 790 | if (!is_admin() || is_adviser() ) |
|---|
| 791 | { |
|---|
| 792 | return new PwgError(401, 'Access denied'); |
|---|
| 793 | } |
|---|
| 794 | if ( empty($params['image_id']) ) |
|---|
| 795 | { |
|---|
| 796 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 797 | } |
|---|
| 798 | global $conf; |
|---|
| 799 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
|---|
| 800 | { |
|---|
| 801 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
|---|
| 802 | } |
|---|
| 803 | $query = ' |
|---|
| 804 | UPDATE '.IMAGES_TABLE.' |
|---|
| 805 | SET level='.(int)$params['level'].' |
|---|
| 806 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
|---|
| 807 | $result = pwg_query($query); |
|---|
| 808 | $affected_rows = mysql_affected_rows(); |
|---|
| 809 | if ($affected_rows) |
|---|
| 810 | { |
|---|
| 811 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 812 | invalidate_user_cache(); |
|---|
| 813 | } |
|---|
| 814 | return $affected_rows; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | function ws_images_add($params, &$service) |
|---|
| 818 | { |
|---|
| 819 | global $conf; |
|---|
| 820 | if (!is_admin() || is_adviser() ) |
|---|
| 821 | { |
|---|
| 822 | return new PwgError(401, 'Access denied'); |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | // name |
|---|
| 826 | // category_id |
|---|
| 827 | // file_content |
|---|
| 828 | // file_sum |
|---|
| 829 | // thumbnail_content |
|---|
| 830 | // thumbnail_sum |
|---|
| 831 | |
|---|
| 832 | // $fh_log = fopen('/tmp/php.log', 'w'); |
|---|
| 833 | // fwrite($fh_log, time()."\n"); |
|---|
| 834 | // fwrite($fh_log, 'input: '.$params['file_sum']."\n"); |
|---|
| 835 | // fwrite($fh_log, 'input: '.$params['thumbnail_sum']."\n"); |
|---|
| 836 | |
|---|
| 837 | // current date |
|---|
| 838 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
|---|
| 839 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
|---|
| 840 | |
|---|
| 841 | // upload directory hierarchy |
|---|
| 842 | $upload_dir = sprintf( |
|---|
| 843 | PHPWG_ROOT_PATH.'upload/%s/%s/%s', |
|---|
| 844 | $year, |
|---|
| 845 | $month, |
|---|
| 846 | $day |
|---|
| 847 | ); |
|---|
| 848 | |
|---|
| 849 | //fwrite($fh_log, $upload_dir."\n"); |
|---|
| 850 | |
|---|
| 851 | // create the upload directory tree if not exists |
|---|
| 852 | if (!is_dir($upload_dir)) { |
|---|
| 853 | umask(0000); |
|---|
| 854 | $recursive = true; |
|---|
| 855 | mkdir($upload_dir, 0777, $recursive); |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | // compute file path |
|---|
| 859 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
|---|
| 860 | $random_string = substr($params['file_sum'], 0, 8); |
|---|
| 861 | $filename_wo_ext = $date_string.'-'.$random_string; |
|---|
| 862 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
|---|
| 863 | |
|---|
| 864 | // dump the photo file |
|---|
| 865 | $fh_file = fopen($file_path, 'w'); |
|---|
| 866 | fwrite($fh_file, base64_decode($params['file_content'])); |
|---|
| 867 | fclose($fh_file); |
|---|
| 868 | chmod($file_path, 0644); |
|---|
| 869 | |
|---|
| 870 | // check dumped file md5sum against expected md5sum |
|---|
| 871 | $dumped_md5 = md5_file($file_path); |
|---|
| 872 | if ($dumped_md5 != $params['file_sum']) { |
|---|
| 873 | return new PwgError(500, 'file transfert failed'); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | // thumbnail directory is a subdirectory of the photo file, hard coded |
|---|
| 877 | // "thumbnail" |
|---|
| 878 | $thumbnail_dir = $upload_dir.'/thumbnail'; |
|---|
| 879 | if (!is_dir($thumbnail_dir)) { |
|---|
| 880 | umask(0000); |
|---|
| 881 | mkdir($thumbnail_dir, 0777); |
|---|
| 882 | } |
|---|
| 883 | |
|---|
| 884 | // thumbnail path, the filename may use a prefix and the extension is |
|---|
| 885 | // always "jpg" (no matter what the real file format is) |
|---|
| 886 | $thumbnail_path = sprintf( |
|---|
| 887 | '%s/%s%s.%s', |
|---|
| 888 | $thumbnail_dir, |
|---|
| 889 | $conf['prefix_thumbnail'], |
|---|
| 890 | $filename_wo_ext, |
|---|
| 891 | 'jpg' |
|---|
| 892 | ); |
|---|
| 893 | |
|---|
| 894 | // dump the thumbnail |
|---|
| 895 | $fh_thumbnail = fopen($thumbnail_path, 'w'); |
|---|
| 896 | fwrite($fh_thumbnail, base64_decode($params['thumbnail_content'])); |
|---|
| 897 | fclose($fh_thumbnail); |
|---|
| 898 | chmod($thumbnail_path, 0644); |
|---|
| 899 | |
|---|
| 900 | // check dumped thumbnail md5 |
|---|
| 901 | $dumped_md5 = md5_file($thumbnail_path); |
|---|
| 902 | if ($dumped_md5 != $params['thumbnail_sum']) { |
|---|
| 903 | return new PwgError(500, 'thumbnail transfert failed'); |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | // fwrite($fh_log, 'output: '.md5_file($file_path)."\n"); |
|---|
| 907 | // fwrite($fh_log, 'output: '.md5_file($thumbnail_path)."\n"); |
|---|
| 908 | |
|---|
| 909 | // database registration |
|---|
| 910 | $insert = array( |
|---|
| 911 | 'file' => $filename_wo_ext.'.jpg', |
|---|
| 912 | 'date_available' => $dbnow, |
|---|
| 913 | 'tn_ext' => 'jpg', |
|---|
| 914 | 'name' => $params['name'], |
|---|
| 915 | 'path' => $file_path, |
|---|
| 916 | ); |
|---|
| 917 | |
|---|
| 918 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 919 | mass_inserts( |
|---|
| 920 | IMAGES_TABLE, |
|---|
| 921 | array_keys($insert), |
|---|
| 922 | array($insert) |
|---|
| 923 | ); |
|---|
| 924 | |
|---|
| 925 | $image_id = mysql_insert_id(); |
|---|
| 926 | |
|---|
| 927 | $insert = array( |
|---|
| 928 | 'category_id' => $params['category_id'], |
|---|
| 929 | 'image_id'=> $image_id, |
|---|
| 930 | ); |
|---|
| 931 | mass_inserts( |
|---|
| 932 | IMAGE_CATEGORY_TABLE, |
|---|
| 933 | array_keys($insert), |
|---|
| 934 | array($insert) |
|---|
| 935 | ); |
|---|
| 936 | |
|---|
| 937 | invalidate_user_cache(); |
|---|
| 938 | |
|---|
| 939 | // fclose($fh_log); |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | /** |
|---|
| 943 | * perform a login (web service method) |
|---|
| 944 | */ |
|---|
| 945 | function ws_session_login($params, &$service) |
|---|
| 946 | { |
|---|
| 947 | global $conf; |
|---|
| 948 | |
|---|
| 949 | if (!$service->isPost()) |
|---|
| 950 | { |
|---|
| 951 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 952 | } |
|---|
| 953 | if (try_log_user($params['username'], $params['password'],false)) |
|---|
| 954 | { |
|---|
| 955 | return true; |
|---|
| 956 | } |
|---|
| 957 | return new PwgError(999, 'Invalid username/password'); |
|---|
| 958 | } |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | /** |
|---|
| 962 | * performs a logout (web service method) |
|---|
| 963 | */ |
|---|
| 964 | function ws_session_logout($params, &$service) |
|---|
| 965 | { |
|---|
| 966 | global $user, $conf; |
|---|
| 967 | if (!is_a_guest()) |
|---|
| 968 | { |
|---|
| 969 | $_SESSION = array(); |
|---|
| 970 | session_unset(); |
|---|
| 971 | session_destroy(); |
|---|
| 972 | setcookie(session_name(),'',0, |
|---|
| 973 | ini_get('session.cookie_path'), |
|---|
| 974 | ini_get('session.cookie_domain') |
|---|
| 975 | ); |
|---|
| 976 | setcookie($conf['remember_me_name'], '', 0, cookie_path()); |
|---|
| 977 | } |
|---|
| 978 | return true; |
|---|
| 979 | } |
|---|
| 980 | |
|---|
| 981 | function ws_session_getStatus($params, &$service) |
|---|
| 982 | { |
|---|
| 983 | global $user; |
|---|
| 984 | $res = array(); |
|---|
| 985 | $res['username'] = is_a_guest() ? 'guest' : $user['username']; |
|---|
| 986 | foreach ( array('status', 'template', 'theme', 'language') as $k ) |
|---|
| 987 | { |
|---|
| 988 | $res[$k] = $user[$k]; |
|---|
| 989 | } |
|---|
| 990 | $res['charset'] = get_pwg_charset(); |
|---|
| 991 | return $res; |
|---|
| 992 | } |
|---|
| 993 | |
|---|
| 994 | |
|---|
| 995 | /** |
|---|
| 996 | * returns a list of tags (web service method) |
|---|
| 997 | */ |
|---|
| 998 | function ws_tags_getList($params, &$service) |
|---|
| 999 | { |
|---|
| 1000 | $tags = get_available_tags(); |
|---|
| 1001 | if ($params['sort_by_counter']) |
|---|
| 1002 | { |
|---|
| 1003 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
|---|
| 1004 | } |
|---|
| 1005 | else |
|---|
| 1006 | { |
|---|
| 1007 | usort($tags, 'tag_alpha_compare'); |
|---|
| 1008 | } |
|---|
| 1009 | for ($i=0; $i<count($tags); $i++) |
|---|
| 1010 | { |
|---|
| 1011 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
|---|
| 1012 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
|---|
| 1013 | $tags[$i]['url'] = make_index_url( |
|---|
| 1014 | array( |
|---|
| 1015 | 'section'=>'tags', |
|---|
| 1016 | 'tags'=>array($tags[$i]) |
|---|
| 1017 | ) |
|---|
| 1018 | ); |
|---|
| 1019 | } |
|---|
| 1020 | return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'counter' )) ); |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | |
|---|
| 1024 | /** |
|---|
| 1025 | * returns a list of images for tags (web service method) |
|---|
| 1026 | */ |
|---|
| 1027 | function ws_tags_getImages($params, &$service) |
|---|
| 1028 | { |
|---|
| 1029 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 1030 | global $conf; |
|---|
| 1031 | |
|---|
| 1032 | // first build all the tag_ids we are interested in |
|---|
| 1033 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
|---|
| 1034 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
|---|
| 1035 | $tags_by_id = array(); |
|---|
| 1036 | foreach( $tags as $tag ) |
|---|
| 1037 | { |
|---|
| 1038 | $tags['id'] = (int)$tag['id']; |
|---|
| 1039 | $tags_by_id[ $tag['id'] ] = $tag; |
|---|
| 1040 | } |
|---|
| 1041 | unset($tags); |
|---|
| 1042 | $tag_ids = array_keys($tags_by_id); |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | $image_ids = array(); |
|---|
| 1046 | $image_tag_map = array(); |
|---|
| 1047 | |
|---|
| 1048 | if ( !empty($tag_ids) ) |
|---|
| 1049 | { // build list of image ids with associated tags per image |
|---|
| 1050 | if ($params['tag_mode_and']) |
|---|
| 1051 | { |
|---|
| 1052 | $image_ids = get_image_ids_for_tags( $tag_ids ); |
|---|
| 1053 | } |
|---|
| 1054 | else |
|---|
| 1055 | { |
|---|
| 1056 | $query = ' |
|---|
| 1057 | SELECT image_id, GROUP_CONCAT(tag_id) tag_ids |
|---|
| 1058 | FROM '.IMAGE_TAG_TABLE.' |
|---|
| 1059 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
|---|
| 1060 | GROUP BY image_id'; |
|---|
| 1061 | $result = pwg_query($query); |
|---|
| 1062 | while ( $row=mysql_fetch_assoc($result) ) |
|---|
| 1063 | { |
|---|
| 1064 | $row['image_id'] = (int)$row['image_id']; |
|---|
| 1065 | array_push( $image_ids, $row['image_id'] ); |
|---|
| 1066 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
|---|
| 1067 | } |
|---|
| 1068 | } |
|---|
| 1069 | } |
|---|
| 1070 | |
|---|
| 1071 | $images = array(); |
|---|
| 1072 | if ( !empty($image_ids)) |
|---|
| 1073 | { |
|---|
| 1074 | $where_clauses = ws_std_image_sql_filter($params); |
|---|
| 1075 | $where_clauses[] = get_sql_condition_FandF( |
|---|
| 1076 | array |
|---|
| 1077 | ( |
|---|
| 1078 | 'forbidden_categories' => 'category_id', |
|---|
| 1079 | 'visible_categories' => 'category_id', |
|---|
| 1080 | 'visible_images' => 'i.id' |
|---|
| 1081 | ), |
|---|
| 1082 | '', true |
|---|
| 1083 | ); |
|---|
| 1084 | $where_clauses[] = 'id IN ('.implode(',',$image_ids).')'; |
|---|
| 1085 | |
|---|
| 1086 | $order_by = ws_std_image_sql_order($params); |
|---|
| 1087 | if (empty($order_by)) |
|---|
| 1088 | { |
|---|
| 1089 | $order_by = $conf['order_by']; |
|---|
| 1090 | } |
|---|
| 1091 | else |
|---|
| 1092 | { |
|---|
| 1093 | $order_by = 'ORDER BY '.$order_by; |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | $query = ' |
|---|
| 1097 | SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i |
|---|
| 1098 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
|---|
| 1099 | WHERE '. implode(' |
|---|
| 1100 | AND ', $where_clauses).' |
|---|
| 1101 | '.$order_by.' |
|---|
| 1102 | LIMIT '.$params['per_page']*$params['page'].','.$params['per_page']; |
|---|
| 1103 | |
|---|
| 1104 | $result = pwg_query($query); |
|---|
| 1105 | while ($row = mysql_fetch_assoc($result)) |
|---|
| 1106 | { |
|---|
| 1107 | $image = array(); |
|---|
| 1108 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
|---|
| 1109 | { |
|---|
| 1110 | if (isset($row[$k])) |
|---|
| 1111 | { |
|---|
| 1112 | $image[$k] = (int)$row[$k]; |
|---|
| 1113 | } |
|---|
| 1114 | } |
|---|
| 1115 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 1116 | { |
|---|
| 1117 | $image[$k] = $row[$k]; |
|---|
| 1118 | } |
|---|
| 1119 | $image = array_merge( $image, ws_std_get_urls($row) ); |
|---|
| 1120 | |
|---|
| 1121 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
|---|
| 1122 | $image_tags = array(); |
|---|
| 1123 | foreach ($image_tag_ids as $tag_id) |
|---|
| 1124 | { |
|---|
| 1125 | $url = make_index_url( |
|---|
| 1126 | array( |
|---|
| 1127 | 'section'=>'tags', |
|---|
| 1128 | 'tags'=> array($tags_by_id[$tag_id]) |
|---|
| 1129 | ) |
|---|
| 1130 | ); |
|---|
| 1131 | $page_url = make_picture_url( |
|---|
| 1132 | array( |
|---|
| 1133 | 'section'=>'tags', |
|---|
| 1134 | 'tags'=> array($tags_by_id[$tag_id]), |
|---|
| 1135 | 'image_id' => $row['id'], |
|---|
| 1136 | 'image_file' => $row['file'], |
|---|
| 1137 | ) |
|---|
| 1138 | ); |
|---|
| 1139 | array_push($image_tags, array( |
|---|
| 1140 | 'id' => (int)$tag_id, |
|---|
| 1141 | 'url' => $url, |
|---|
| 1142 | 'page_url' => $page_url, |
|---|
| 1143 | ) |
|---|
| 1144 | ); |
|---|
| 1145 | } |
|---|
| 1146 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', |
|---|
| 1147 | array('id','url_name','url','page_url') |
|---|
| 1148 | ); |
|---|
| 1149 | array_push($images, $image); |
|---|
| 1150 | } |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | return array( 'images' => |
|---|
| 1154 | array ( |
|---|
| 1155 | WS_XML_ATTRIBUTES => |
|---|
| 1156 | array( |
|---|
| 1157 | 'page' => $params['page'], |
|---|
| 1158 | 'per_page' => $params['per_page'], |
|---|
| 1159 | 'count' => count($images) |
|---|
| 1160 | ), |
|---|
| 1161 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 1162 | ws_std_get_image_xml_attributes() ) |
|---|
| 1163 | ) |
|---|
| 1164 | ); |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | ?> |
|---|