[1698] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[1698] | 23 | |
---|
| 24 | /**** IMPLEMENTATION OF WEB SERVICE METHODS ***********************************/ |
---|
| 25 | |
---|
| 26 | /** |
---|
[1768] | 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 | { |
---|
[2572] | 32 | global $conf; |
---|
[2119] | 33 | |
---|
[1849] | 34 | if ( strpos($methodName,'reflection.')===0 ) |
---|
| 35 | { // OK for reflection |
---|
| 36 | return $res; |
---|
| 37 | } |
---|
[2119] | 38 | |
---|
[1849] | 39 | if ( !is_autorize_status(ACCESS_GUEST) and |
---|
| 40 | strpos($methodName,'pwg.session.')!==0 ) |
---|
[1768] | 41 | { |
---|
[1849] | 42 | return new PwgError(401, 'Access denied'); |
---|
| 43 | } |
---|
[2119] | 44 | |
---|
[1768] | 45 | return $res; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
[1698] | 49 | * returns a "standard" (for our web service) array of sql where clauses that |
---|
[1711] | 50 | * filters the images (images table only) |
---|
| 51 | */ |
---|
[1698] | 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 |
---|
[1711] | 104 | */ |
---|
[1698] | 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 | } |
---|
[1711] | 126 | $sortable_fields = array('id', 'file', 'name', 'hit', 'average_rate', |
---|
[1698] | 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 |
---|
[1711] | 146 | */ |
---|
[1698] | 147 | function ws_std_get_urls($image_row) |
---|
| 148 | { |
---|
| 149 | $ret = array( |
---|
[1711] | 150 | 'tn_url' => get_thumbnail_url($image_row), |
---|
[1698] | 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 | |
---|
[1845] | 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 | } |
---|
[1698] | 171 | |
---|
[1781] | 172 | /** |
---|
| 173 | * returns PWG version (web service method) |
---|
| 174 | */ |
---|
[1698] | 175 | function ws_getVersion($params, &$service) |
---|
| 176 | { |
---|
[1852] | 177 | global $conf; |
---|
| 178 | if ($conf['show_version']) |
---|
| 179 | return PHPWG_VERSION; |
---|
| 180 | else |
---|
| 181 | return new PwgError(403, 'Forbidden'); |
---|
[1698] | 182 | } |
---|
| 183 | |
---|
[2429] | 184 | function ws_caddie_add($params, &$service) |
---|
| 185 | { |
---|
| 186 | if (!is_admin()) |
---|
| 187 | { |
---|
| 188 | return new PwgError(401, 'Access denied'); |
---|
| 189 | } |
---|
[2770] | 190 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
[2429] | 191 | if ( empty($params['image_id']) ) |
---|
| 192 | { |
---|
| 193 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 194 | } |
---|
| 195 | global $user; |
---|
| 196 | $query = ' |
---|
| 197 | SELECT id |
---|
| 198 | FROM '.IMAGES_TABLE.' LEFT JOIN '.CADDIE_TABLE.' ON id=element_id AND user_id='.$user['id'].' |
---|
| 199 | WHERE id IN ('.implode(',',$params['image_id']).') |
---|
| 200 | AND element_id IS NULL'; |
---|
| 201 | $datas = array(); |
---|
| 202 | foreach ( array_from_query($query, 'id') as $id ) |
---|
| 203 | { |
---|
| 204 | array_push($datas, array('element_id'=>$id, 'user_id'=>$user['id']) ); |
---|
| 205 | } |
---|
| 206 | if (count($datas)) |
---|
| 207 | { |
---|
| 208 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 209 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
| 210 | } |
---|
| 211 | return count($datas); |
---|
| 212 | } |
---|
[1781] | 213 | |
---|
[1698] | 214 | /** |
---|
[1781] | 215 | * returns images per category (web service method) |
---|
[1711] | 216 | */ |
---|
[1698] | 217 | function ws_categories_getImages($params, &$service) |
---|
| 218 | { |
---|
| 219 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
| 220 | global $user, $conf; |
---|
| 221 | |
---|
| 222 | $images = array(); |
---|
| 223 | |
---|
| 224 | //------------------------------------------------- get the related categories |
---|
| 225 | $where_clauses = array(); |
---|
| 226 | foreach($params['cat_id'] as $cat_id) |
---|
| 227 | { |
---|
| 228 | $cat_id = (int)$cat_id; |
---|
| 229 | if ($cat_id<=0) |
---|
| 230 | continue; |
---|
| 231 | if ($params['recursive']) |
---|
| 232 | { |
---|
| 233 | $where_clauses[] = 'uppercats REGEXP \'(^|,)'.$cat_id.'(,|$)\''; |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
| 237 | $where_clauses[] = 'id='.$cat_id; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | if (!empty($where_clauses)) |
---|
| 241 | { |
---|
| 242 | $where_clauses = array( '('. |
---|
| 243 | implode(' |
---|
| 244 | OR ', $where_clauses) . ')' |
---|
| 245 | ); |
---|
| 246 | } |
---|
[2119] | 247 | $where_clauses[] = get_sql_condition_FandF( |
---|
| 248 | array('forbidden_categories' => 'id'), |
---|
| 249 | NULL, true |
---|
| 250 | ); |
---|
[1698] | 251 | |
---|
| 252 | $query = ' |
---|
[1866] | 253 | SELECT id, name, permalink, image_order |
---|
[1698] | 254 | FROM '.CATEGORIES_TABLE.' |
---|
| 255 | WHERE '. implode(' |
---|
| 256 | AND ', $where_clauses); |
---|
| 257 | $result = pwg_query($query); |
---|
| 258 | $cats = array(); |
---|
| 259 | while ($row = mysql_fetch_assoc($result)) |
---|
| 260 | { |
---|
| 261 | $row['id'] = (int)$row['id']; |
---|
| 262 | $cats[ $row['id'] ] = $row; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | //-------------------------------------------------------- get the images |
---|
| 266 | if ( !empty($cats) ) |
---|
| 267 | { |
---|
| 268 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
| 269 | $where_clauses[] = 'category_id IN (' |
---|
| 270 | .implode(',', array_keys($cats) ) |
---|
| 271 | .')'; |
---|
[1781] | 272 | $where_clauses[] = get_sql_condition_FandF( array( |
---|
| 273 | 'visible_images' => 'i.id' |
---|
| 274 | ), null, true |
---|
| 275 | ); |
---|
[1756] | 276 | |
---|
[1698] | 277 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
[1852] | 278 | if ( empty($order_by) |
---|
| 279 | and count($params['cat_id'])==1 |
---|
| 280 | and isset($cats[ $params['cat_id'][0] ]['image_order']) |
---|
| 281 | ) |
---|
[1698] | 282 | { |
---|
[1852] | 283 | $order_by = $cats[ $params['cat_id'][0] ]['image_order']; |
---|
[1698] | 284 | } |
---|
[1852] | 285 | $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; |
---|
| 286 | |
---|
[1698] | 287 | $query = ' |
---|
| 288 | SELECT i.*, GROUP_CONCAT(category_id) cat_ids |
---|
| 289 | FROM '.IMAGES_TABLE.' i |
---|
| 290 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
---|
| 291 | WHERE '. implode(' |
---|
| 292 | AND ', $where_clauses).' |
---|
| 293 | GROUP BY i.id |
---|
| 294 | '.$order_by.' |
---|
[2770] | 295 | LIMIT '.(int)($params['per_page']*$params['page']).','.(int)$params['per_page']; |
---|
[1698] | 296 | |
---|
| 297 | $result = pwg_query($query); |
---|
| 298 | while ($row = mysql_fetch_assoc($result)) |
---|
| 299 | { |
---|
| 300 | $image = array(); |
---|
| 301 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
| 302 | { |
---|
| 303 | if (isset($row[$k])) |
---|
| 304 | { |
---|
| 305 | $image[$k] = (int)$row[$k]; |
---|
| 306 | } |
---|
| 307 | } |
---|
[1880] | 308 | foreach ( array('file', 'name', 'comment') as $k ) |
---|
[1698] | 309 | { |
---|
| 310 | $image[$k] = $row[$k]; |
---|
| 311 | } |
---|
| 312 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
| 313 | |
---|
| 314 | $image_cats = array(); |
---|
| 315 | foreach ( explode(',', $row['cat_ids']) as $cat_id ) |
---|
| 316 | { |
---|
| 317 | $url = make_index_url( |
---|
| 318 | array( |
---|
[1861] | 319 | 'category' => $cats[$cat_id], |
---|
[1698] | 320 | ) |
---|
| 321 | ); |
---|
| 322 | $page_url = make_picture_url( |
---|
| 323 | array( |
---|
[1861] | 324 | 'category' => $cats[$cat_id], |
---|
[1698] | 325 | 'image_id' => $row['id'], |
---|
| 326 | 'image_file' => $row['file'], |
---|
| 327 | ) |
---|
| 328 | ); |
---|
| 329 | array_push( $image_cats, array( |
---|
| 330 | WS_XML_ATTRIBUTES => array ( |
---|
| 331 | 'id' => (int)$cat_id, |
---|
| 332 | 'url' => $url, |
---|
| 333 | 'page_url' => $page_url, |
---|
| 334 | ) |
---|
| 335 | ) |
---|
| 336 | ); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | $image['categories'] = new PwgNamedArray( |
---|
| 340 | $image_cats,'category', array('id','url','page_url') |
---|
| 341 | ); |
---|
| 342 | array_push($images, $image); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | return array( 'images' => |
---|
| 347 | array ( |
---|
| 348 | WS_XML_ATTRIBUTES => |
---|
| 349 | array( |
---|
| 350 | 'page' => $params['page'], |
---|
| 351 | 'per_page' => $params['per_page'], |
---|
| 352 | 'count' => count($images) |
---|
| 353 | ), |
---|
[1711] | 354 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
[1845] | 355 | ws_std_get_image_xml_attributes() ) |
---|
[1698] | 356 | ) |
---|
| 357 | ); |
---|
| 358 | } |
---|
| 359 | |
---|
[1781] | 360 | |
---|
[1698] | 361 | /** |
---|
[1781] | 362 | * returns a list of categories (web service method) |
---|
[1698] | 363 | */ |
---|
| 364 | function ws_categories_getList($params, &$service) |
---|
| 365 | { |
---|
[1820] | 366 | global $user,$conf; |
---|
[1698] | 367 | |
---|
| 368 | $where = array(); |
---|
| 369 | |
---|
| 370 | if (!$params['recursive']) |
---|
| 371 | { |
---|
| 372 | if ($params['cat_id']>0) |
---|
[1820] | 373 | $where[] = '(id_uppercat='.(int)($params['cat_id']).' |
---|
| 374 | OR id='.(int)($params['cat_id']).')'; |
---|
[1698] | 375 | else |
---|
| 376 | $where[] = 'id_uppercat IS NULL'; |
---|
| 377 | } |
---|
[1820] | 378 | else if ($params['cat_id']>0) |
---|
| 379 | { |
---|
| 380 | $where[] = 'uppercats REGEXP \'(^|,)'. |
---|
| 381 | (int)($params['cat_id']) |
---|
| 382 | .'(,|$)\''; |
---|
| 383 | } |
---|
[1698] | 384 | |
---|
| 385 | if ($params['public']) |
---|
| 386 | { |
---|
| 387 | $where[] = 'status = "public"'; |
---|
[1711] | 388 | $where[] = 'visible = "true"'; |
---|
[1820] | 389 | $where[]= 'user_id='.$conf['guest_id']; |
---|
[1698] | 390 | } |
---|
| 391 | else |
---|
| 392 | { |
---|
[1820] | 393 | $where[]= 'user_id='.$user['id']; |
---|
[1698] | 394 | } |
---|
| 395 | |
---|
[1711] | 396 | $query = ' |
---|
[1866] | 397 | SELECT id, name, permalink, uppercats, global_rank, |
---|
[1845] | 398 | nb_images, count_images AS total_nb_images, |
---|
| 399 | date_last, max_date_last, count_categories AS nb_categories |
---|
[1711] | 400 | FROM '.CATEGORIES_TABLE.' |
---|
| 401 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id |
---|
[1698] | 402 | WHERE '. implode(' |
---|
| 403 | AND ', $where); |
---|
| 404 | |
---|
| 405 | $result = pwg_query($query); |
---|
| 406 | |
---|
| 407 | $cats = array(); |
---|
| 408 | while ($row = mysql_fetch_assoc($result)) |
---|
| 409 | { |
---|
| 410 | $row['url'] = make_index_url( |
---|
| 411 | array( |
---|
[1861] | 412 | 'category' => $row |
---|
[1698] | 413 | ) |
---|
| 414 | ); |
---|
[1845] | 415 | foreach( array('id','nb_images','total_nb_images','nb_categories') as $key) |
---|
[1698] | 416 | { |
---|
| 417 | $row[$key] = (int)$row[$key]; |
---|
| 418 | } |
---|
[2572] | 419 | |
---|
[1698] | 420 | array_push($cats, $row); |
---|
| 421 | } |
---|
| 422 | usort($cats, 'global_rank_compare'); |
---|
| 423 | return array( |
---|
[2548] | 424 | 'categories' => new PwgNamedArray( |
---|
| 425 | $cats, |
---|
| 426 | 'category', |
---|
| 427 | array( |
---|
| 428 | 'id', |
---|
| 429 | 'url', |
---|
| 430 | 'nb_images', |
---|
| 431 | 'total_nb_images', |
---|
| 432 | 'nb_categories', |
---|
| 433 | 'date_last', |
---|
| 434 | 'max_date_last', |
---|
| 435 | ) |
---|
| 436 | ) |
---|
[1698] | 437 | ); |
---|
| 438 | } |
---|
| 439 | |
---|
[2563] | 440 | /** |
---|
| 441 | * returns the list of categories as you can see them in administration (web |
---|
| 442 | * service method). |
---|
| 443 | * |
---|
| 444 | * Only admin can run this method and permissions are not taken into |
---|
| 445 | * account. |
---|
| 446 | */ |
---|
| 447 | function ws_categories_getAdminList($params, &$service) |
---|
| 448 | { |
---|
| 449 | if (!is_admin()) |
---|
| 450 | { |
---|
| 451 | return new PwgError(401, 'Access denied'); |
---|
| 452 | } |
---|
[1781] | 453 | |
---|
[2563] | 454 | $query = ' |
---|
| 455 | SELECT |
---|
| 456 | category_id, |
---|
| 457 | COUNT(*) AS counter |
---|
| 458 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 459 | GROUP BY category_id |
---|
| 460 | ;'; |
---|
| 461 | $nb_images_of = simple_hash_from_query($query, 'category_id', 'counter'); |
---|
| 462 | |
---|
| 463 | $query = ' |
---|
| 464 | SELECT |
---|
| 465 | id, |
---|
| 466 | name, |
---|
| 467 | uppercats, |
---|
| 468 | global_rank |
---|
| 469 | FROM '.CATEGORIES_TABLE.' |
---|
| 470 | ;'; |
---|
| 471 | $result = pwg_query($query); |
---|
| 472 | $cats = array(); |
---|
| 473 | |
---|
| 474 | while ($row = mysql_fetch_assoc($result)) |
---|
| 475 | { |
---|
| 476 | $id = $row['id']; |
---|
| 477 | $row['nb_images'] = isset($nb_images_of[$id]) ? $nb_images_of[$id] : 0; |
---|
| 478 | array_push($cats, $row); |
---|
[2585] | 479 | } |
---|
| 480 | |
---|
[2563] | 481 | usort($cats, 'global_rank_compare'); |
---|
| 482 | return array( |
---|
| 483 | 'categories' => new PwgNamedArray( |
---|
| 484 | $cats, |
---|
| 485 | 'category', |
---|
| 486 | array( |
---|
| 487 | 'id', |
---|
| 488 | 'nb_images', |
---|
| 489 | 'name', |
---|
| 490 | 'uppercats', |
---|
| 491 | 'global_rank', |
---|
| 492 | ) |
---|
| 493 | ) |
---|
| 494 | ); |
---|
| 495 | } |
---|
| 496 | |
---|
[1781] | 497 | /** |
---|
| 498 | * returns detailed information for an element (web service method) |
---|
| 499 | */ |
---|
[1849] | 500 | function ws_images_addComment($params, &$service) |
---|
| 501 | { |
---|
[1852] | 502 | if (!$service->isPost()) |
---|
| 503 | { |
---|
| 504 | return new PwgError(405, "This method requires HTTP POST"); |
---|
| 505 | } |
---|
[1849] | 506 | $params['image_id'] = (int)$params['image_id']; |
---|
| 507 | $query = ' |
---|
[2119] | 508 | SELECT DISTINCT image_id |
---|
[1849] | 509 | FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id |
---|
[2119] | 510 | WHERE commentable="true" |
---|
[1849] | 511 | AND image_id='.$params['image_id']. |
---|
| 512 | get_sql_condition_FandF( |
---|
| 513 | array( |
---|
| 514 | 'forbidden_categories' => 'id', |
---|
| 515 | 'visible_categories' => 'id', |
---|
| 516 | 'visible_images' => 'image_id' |
---|
| 517 | ), |
---|
| 518 | ' AND' |
---|
| 519 | ); |
---|
| 520 | if ( !mysql_num_rows( pwg_query( $query ) ) ) |
---|
| 521 | { |
---|
| 522 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 523 | } |
---|
[2119] | 524 | |
---|
[1849] | 525 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
[2119] | 526 | |
---|
[1849] | 527 | $comm = array( |
---|
| 528 | 'author' => trim( stripslashes($params['author']) ), |
---|
| 529 | 'content' => trim( stripslashes($params['content']) ), |
---|
| 530 | 'image_id' => $params['image_id'], |
---|
| 531 | ); |
---|
| 532 | |
---|
| 533 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
[2119] | 534 | |
---|
| 535 | $comment_action = insert_user_comment( |
---|
[1849] | 536 | $comm, $params['key'], $infos |
---|
| 537 | ); |
---|
| 538 | |
---|
| 539 | switch ($comment_action) |
---|
| 540 | { |
---|
| 541 | case 'reject': |
---|
| 542 | array_push($infos, l10n('comment_not_added') ); |
---|
| 543 | return new PwgError(403, implode("\n", $infos) ); |
---|
| 544 | case 'validate': |
---|
| 545 | case 'moderate': |
---|
[2119] | 546 | $ret = array( |
---|
[1849] | 547 | 'id' => $comm['id'], |
---|
| 548 | 'validation' => $comment_action=='validate', |
---|
| 549 | ); |
---|
| 550 | return new PwgNamedStruct( |
---|
| 551 | 'comment', |
---|
[2119] | 552 | $ret, |
---|
| 553 | null, array() |
---|
[1849] | 554 | ); |
---|
| 555 | default: |
---|
| 556 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | /** |
---|
| 561 | * returns detailed information for an element (web service method) |
---|
| 562 | */ |
---|
[1698] | 563 | function ws_images_getInfo($params, &$service) |
---|
| 564 | { |
---|
| 565 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1849] | 566 | global $user, $conf; |
---|
[1698] | 567 | $params['image_id'] = (int)$params['image_id']; |
---|
| 568 | if ( $params['image_id']<=0 ) |
---|
| 569 | { |
---|
| 570 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 571 | } |
---|
[1781] | 572 | |
---|
[1698] | 573 | $query=' |
---|
| 574 | SELECT * FROM '.IMAGES_TABLE.' |
---|
[1711] | 575 | WHERE id='.$params['image_id']. |
---|
| 576 | get_sql_condition_FandF( |
---|
| 577 | array('visible_images' => 'id'), |
---|
| 578 | ' AND' |
---|
[2516] | 579 | ).' |
---|
| 580 | LIMIT 1'; |
---|
[1711] | 581 | |
---|
[1698] | 582 | $image_row = mysql_fetch_assoc(pwg_query($query)); |
---|
| 583 | if ($image_row==null) |
---|
| 584 | { |
---|
[1852] | 585 | return new PwgError(404, "image_id not found"); |
---|
[1698] | 586 | } |
---|
[1845] | 587 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
---|
[1698] | 588 | |
---|
| 589 | //-------------------------------------------------------- related categories |
---|
| 590 | $query = ' |
---|
[1866] | 591 | SELECT id, name, permalink, uppercats, global_rank, commentable |
---|
[1698] | 592 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
[1849] | 593 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
[2119] | 594 | WHERE image_id = '.$image_row['id']. |
---|
| 595 | get_sql_condition_FandF( |
---|
| 596 | array( 'forbidden_categories' => 'category_id' ), |
---|
| 597 | ' AND' |
---|
| 598 | ).' |
---|
[1698] | 599 | ;'; |
---|
| 600 | $result = pwg_query($query); |
---|
[1849] | 601 | $is_commentable = false; |
---|
[1698] | 602 | $related_categories = array(); |
---|
| 603 | while ($row = mysql_fetch_assoc($result)) |
---|
| 604 | { |
---|
[1849] | 605 | if ($row['commentable']=='true') |
---|
| 606 | { |
---|
| 607 | $is_commentable = true; |
---|
| 608 | } |
---|
| 609 | unset($row['commentable']); |
---|
[1698] | 610 | $row['url'] = make_index_url( |
---|
| 611 | array( |
---|
[1861] | 612 | 'category' => $row |
---|
[1698] | 613 | ) |
---|
| 614 | ); |
---|
| 615 | |
---|
| 616 | $row['page_url'] = make_picture_url( |
---|
| 617 | array( |
---|
| 618 | 'image_id' => $image_row['id'], |
---|
| 619 | 'image_file' => $image_row['file'], |
---|
[1861] | 620 | 'category' => $row |
---|
[1698] | 621 | ) |
---|
| 622 | ); |
---|
[1849] | 623 | $row['id']=(int)$row['id']; |
---|
[1698] | 624 | array_push($related_categories, $row); |
---|
| 625 | } |
---|
| 626 | usort($related_categories, 'global_rank_compare'); |
---|
| 627 | if ( empty($related_categories) ) |
---|
| 628 | { |
---|
| 629 | return new PwgError(401, 'Access denied'); |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | //-------------------------------------------------------------- related tags |
---|
[1815] | 633 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
---|
| 634 | foreach( $related_tags as $i=>$tag) |
---|
[1698] | 635 | { |
---|
[1815] | 636 | $tag['url'] = make_index_url( |
---|
[1698] | 637 | array( |
---|
[1815] | 638 | 'tags' => array($tag) |
---|
[1698] | 639 | ) |
---|
| 640 | ); |
---|
[1815] | 641 | $tag['page_url'] = make_picture_url( |
---|
[1698] | 642 | array( |
---|
| 643 | 'image_id' => $image_row['id'], |
---|
| 644 | 'image_file' => $image_row['file'], |
---|
[1815] | 645 | 'tags' => array($tag), |
---|
[1698] | 646 | ) |
---|
| 647 | ); |
---|
[1815] | 648 | unset($tag['counter']); |
---|
[1849] | 649 | $tag['id']=(int)$tag['id']; |
---|
[1815] | 650 | $related_tags[$i]=$tag; |
---|
[1698] | 651 | } |
---|
[1849] | 652 | //------------------------------------------------------------- related rates |
---|
| 653 | $query = ' |
---|
| 654 | SELECT COUNT(rate) AS count |
---|
| 655 | , ROUND(AVG(rate),2) AS average |
---|
| 656 | , ROUND(STD(rate),2) AS stdev |
---|
| 657 | FROM '.RATE_TABLE.' |
---|
| 658 | WHERE element_id = '.$image_row['id'].' |
---|
| 659 | ;'; |
---|
| 660 | $rating = mysql_fetch_assoc(pwg_query($query)); |
---|
| 661 | $rating['count'] = (int)$rating['count']; |
---|
| 662 | |
---|
[1698] | 663 | //---------------------------------------------------------- related comments |
---|
[1849] | 664 | $related_comments = array(); |
---|
[2119] | 665 | |
---|
[1849] | 666 | $where_comments = 'image_id = '.$image_row['id']; |
---|
| 667 | if ( !is_admin() ) |
---|
| 668 | { |
---|
| 669 | $where_comments .= ' |
---|
| 670 | AND validated="true"'; |
---|
| 671 | } |
---|
| 672 | |
---|
[1698] | 673 | $query = ' |
---|
| 674 | SELECT COUNT(id) nb_comments |
---|
| 675 | FROM '.COMMENTS_TABLE.' |
---|
[1849] | 676 | WHERE '.$where_comments; |
---|
[1698] | 677 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
---|
[1849] | 678 | $nb_comments = (int)$nb_comments; |
---|
[1698] | 679 | |
---|
[1849] | 680 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
---|
| 681 | { |
---|
| 682 | $query = ' |
---|
[1698] | 683 | SELECT id, date, author, content |
---|
| 684 | FROM '.COMMENTS_TABLE.' |
---|
[1849] | 685 | WHERE '.$where_comments.' |
---|
| 686 | ORDER BY date |
---|
[2770] | 687 | LIMIT '.(int)($params['comments_per_page']*$params['comments_page']). |
---|
| 688 | ','.(int)$params['comments_per_page']; |
---|
[1698] | 689 | |
---|
[1849] | 690 | $result = pwg_query($query); |
---|
| 691 | while ($row = mysql_fetch_assoc($result)) |
---|
| 692 | { |
---|
| 693 | $row['id']=(int)$row['id']; |
---|
| 694 | array_push($related_comments, $row); |
---|
| 695 | } |
---|
| 696 | } |
---|
[2119] | 697 | |
---|
[1849] | 698 | $comment_post_data = null; |
---|
[2119] | 699 | if ($is_commentable and |
---|
[2029] | 700 | (!is_a_guest() |
---|
| 701 | or (is_a_guest() and $conf['comments_forall'] ) |
---|
[1849] | 702 | ) |
---|
| 703 | ) |
---|
[1698] | 704 | { |
---|
[1849] | 705 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
[2119] | 706 | $comment_post_data['author'] = $user['username']; |
---|
[1849] | 707 | $comment_post_data['key'] = get_comment_post_key($params['image_id']); |
---|
[1698] | 708 | } |
---|
| 709 | |
---|
[1849] | 710 | $ret = $image_row; |
---|
| 711 | foreach ( array('id','width','height','hit','filesize') as $k ) |
---|
| 712 | { |
---|
| 713 | if (isset($ret[$k])) |
---|
| 714 | { |
---|
| 715 | $ret[$k] = (int)$ret[$k]; |
---|
| 716 | } |
---|
| 717 | } |
---|
| 718 | foreach ( array('path', 'storage_category_id') as $k ) |
---|
| 719 | { |
---|
| 720 | unset($ret[$k]); |
---|
| 721 | } |
---|
[1698] | 722 | |
---|
[1849] | 723 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
---|
[1698] | 724 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
---|
[2585] | 725 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','name','page_url') ); |
---|
[1849] | 726 | if ( isset($comment_post_data) ) |
---|
| 727 | { |
---|
| 728 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
---|
| 729 | } |
---|
[1698] | 730 | $ret['comments'] = array( |
---|
[2119] | 731 | WS_XML_ATTRIBUTES => |
---|
[1849] | 732 | array( |
---|
| 733 | 'page' => $params['comments_page'], |
---|
| 734 | 'per_page' => $params['comments_per_page'], |
---|
| 735 | 'count' => count($related_comments), |
---|
| 736 | 'nb_comments' => $nb_comments, |
---|
| 737 | ), |
---|
| 738 | WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') ) |
---|
[1698] | 739 | ); |
---|
[1845] | 740 | |
---|
[1698] | 741 | return new PwgNamedStruct('image',$ret, null, array('name','comment') ); |
---|
| 742 | } |
---|
| 743 | |
---|
[2435] | 744 | |
---|
[1837] | 745 | /** |
---|
[2435] | 746 | * rates the image_id in the parameter |
---|
| 747 | */ |
---|
| 748 | function ws_images_Rate($params, &$service) |
---|
| 749 | { |
---|
| 750 | $image_id = (int)$params['image_id']; |
---|
| 751 | $query = ' |
---|
| 752 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
---|
| 753 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
| 754 | WHERE id='.$image_id |
---|
| 755 | .get_sql_condition_FandF( |
---|
| 756 | array( |
---|
| 757 | 'forbidden_categories' => 'category_id', |
---|
| 758 | 'forbidden_images' => 'id', |
---|
| 759 | ), |
---|
| 760 | ' AND' |
---|
| 761 | ).' |
---|
| 762 | LIMIT 1'; |
---|
| 763 | if ( mysql_num_rows( pwg_query($query) )==0 ) |
---|
| 764 | { |
---|
| 765 | return new PwgError(404, "Invalid image_id or access denied" ); |
---|
| 766 | } |
---|
| 767 | $rate = (int)$params['rate']; |
---|
| 768 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
---|
| 769 | $res = rate_picture( $image_id, $rate ); |
---|
| 770 | if ($res==false) |
---|
| 771 | { |
---|
| 772 | global $conf; |
---|
| 773 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
---|
| 774 | } |
---|
| 775 | return $res; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | |
---|
| 779 | /** |
---|
[1837] | 780 | * returns a list of elements corresponding to a query search |
---|
| 781 | */ |
---|
| 782 | function ws_images_search($params, &$service) |
---|
| 783 | { |
---|
| 784 | global $page; |
---|
| 785 | $images = array(); |
---|
| 786 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
---|
| 787 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1698] | 788 | |
---|
[2135] | 789 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
| 790 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
[1837] | 791 | |
---|
[2451] | 792 | $super_order_by = false; |
---|
[2135] | 793 | if ( !empty($order_by) ) |
---|
[1837] | 794 | { |
---|
[2135] | 795 | global $conf; |
---|
| 796 | $conf['order_by'] = 'ORDER BY '.$order_by; |
---|
[2451] | 797 | $super_order_by=true; // quick_search_result might be faster |
---|
[1837] | 798 | } |
---|
| 799 | |
---|
[2135] | 800 | $search_result = get_quick_search_results($params['query'], |
---|
[2451] | 801 | $super_order_by, |
---|
| 802 | implode(',', $where_clauses) |
---|
| 803 | ); |
---|
[2119] | 804 | |
---|
[2451] | 805 | $image_ids = array_slice( |
---|
| 806 | $search_result['items'], |
---|
| 807 | $params['page']*$params['per_page'], |
---|
| 808 | $params['per_page'] |
---|
| 809 | ); |
---|
[1837] | 810 | |
---|
| 811 | if ( count($image_ids) ) |
---|
| 812 | { |
---|
| 813 | $query = ' |
---|
| 814 | SELECT * FROM '.IMAGES_TABLE.' |
---|
[2451] | 815 | WHERE id IN ('.implode(',', $image_ids).')'; |
---|
[1837] | 816 | |
---|
[2451] | 817 | $image_ids = array_flip($image_ids); |
---|
[1837] | 818 | $result = pwg_query($query); |
---|
| 819 | while ($row = mysql_fetch_assoc($result)) |
---|
| 820 | { |
---|
| 821 | $image = array(); |
---|
| 822 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
| 823 | { |
---|
| 824 | if (isset($row[$k])) |
---|
| 825 | { |
---|
| 826 | $image[$k] = (int)$row[$k]; |
---|
| 827 | } |
---|
| 828 | } |
---|
[1880] | 829 | foreach ( array('file', 'name', 'comment') as $k ) |
---|
[1837] | 830 | { |
---|
| 831 | $image[$k] = $row[$k]; |
---|
| 832 | } |
---|
| 833 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
[2451] | 834 | $images[$image_ids[$image['id']]] = $image; |
---|
[1837] | 835 | } |
---|
[2451] | 836 | ksort($images, SORT_NUMERIC); |
---|
| 837 | $images = array_values($images); |
---|
[1837] | 838 | } |
---|
| 839 | |
---|
| 840 | |
---|
| 841 | return array( 'images' => |
---|
| 842 | array ( |
---|
| 843 | WS_XML_ATTRIBUTES => |
---|
| 844 | array( |
---|
| 845 | 'page' => $params['page'], |
---|
| 846 | 'per_page' => $params['per_page'], |
---|
| 847 | 'count' => count($images) |
---|
| 848 | ), |
---|
| 849 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
[1845] | 850 | ws_std_get_image_xml_attributes() ) |
---|
[1837] | 851 | ) |
---|
| 852 | ); |
---|
| 853 | } |
---|
| 854 | |
---|
[2413] | 855 | function ws_images_setPrivacyLevel($params, &$service) |
---|
| 856 | { |
---|
| 857 | if (!is_admin() || is_adviser() ) |
---|
| 858 | { |
---|
| 859 | return new PwgError(401, 'Access denied'); |
---|
| 860 | } |
---|
[2770] | 861 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
[2413] | 862 | if ( empty($params['image_id']) ) |
---|
| 863 | { |
---|
| 864 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 865 | } |
---|
| 866 | global $conf; |
---|
| 867 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
---|
| 868 | { |
---|
| 869 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
---|
| 870 | } |
---|
| 871 | $query = ' |
---|
| 872 | UPDATE '.IMAGES_TABLE.' |
---|
| 873 | SET level='.(int)$params['level'].' |
---|
| 874 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
---|
| 875 | $result = pwg_query($query); |
---|
| 876 | $affected_rows = mysql_affected_rows(); |
---|
| 877 | if ($affected_rows) |
---|
| 878 | { |
---|
| 879 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 880 | invalidate_user_cache(); |
---|
| 881 | } |
---|
| 882 | return $affected_rows; |
---|
| 883 | } |
---|
| 884 | |
---|
[2463] | 885 | function ws_images_add($params, &$service) |
---|
| 886 | { |
---|
| 887 | global $conf; |
---|
[2496] | 888 | if (!is_admin() || is_adviser() ) |
---|
| 889 | { |
---|
| 890 | return new PwgError(401, 'Access denied'); |
---|
| 891 | } |
---|
| 892 | |
---|
[2463] | 893 | // name |
---|
| 894 | // category_id |
---|
| 895 | // file_content |
---|
| 896 | // file_sum |
---|
| 897 | // thumbnail_content |
---|
| 898 | // thumbnail_sum |
---|
[2520] | 899 | // rank |
---|
[2496] | 900 | |
---|
[2463] | 901 | // $fh_log = fopen('/tmp/php.log', 'w'); |
---|
| 902 | // fwrite($fh_log, time()."\n"); |
---|
[2553] | 903 | // fwrite($fh_log, 'input rank :'.$params['rank']."\n"); |
---|
[2463] | 904 | // fwrite($fh_log, 'input: '.$params['file_sum']."\n"); |
---|
| 905 | // fwrite($fh_log, 'input: '.$params['thumbnail_sum']."\n"); |
---|
| 906 | |
---|
[2592] | 907 | // does the image already exists ? |
---|
| 908 | $query = ' |
---|
| 909 | SELECT |
---|
| 910 | COUNT(*) AS counter |
---|
| 911 | FROM '.IMAGES_TABLE.' |
---|
| 912 | WHERE md5sum = \''.$params['file_sum'].'\' |
---|
| 913 | ;'; |
---|
| 914 | list($counter) = mysql_fetch_row(pwg_query($query)); |
---|
| 915 | if ($counter != 0) { |
---|
| 916 | return new PwgError(500, 'file already exists'); |
---|
| 917 | } |
---|
| 918 | |
---|
[2463] | 919 | // current date |
---|
| 920 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
| 921 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
---|
[2496] | 922 | |
---|
[2501] | 923 | // upload directory hierarchy |
---|
[2463] | 924 | $upload_dir = sprintf( |
---|
| 925 | PHPWG_ROOT_PATH.'upload/%s/%s/%s', |
---|
| 926 | $year, |
---|
| 927 | $month, |
---|
| 928 | $day |
---|
| 929 | ); |
---|
| 930 | |
---|
[2496] | 931 | //fwrite($fh_log, $upload_dir."\n"); |
---|
| 932 | |
---|
[2501] | 933 | // create the upload directory tree if not exists |
---|
[2463] | 934 | if (!is_dir($upload_dir)) { |
---|
| 935 | umask(0000); |
---|
| 936 | $recursive = true; |
---|
[2786] | 937 | if (!@mkdir($upload_dir, 0777, $recursive)) |
---|
| 938 | { |
---|
| 939 | return new PwgError(500, 'error during directory creation'); |
---|
| 940 | } |
---|
[2463] | 941 | } |
---|
| 942 | |
---|
[2786] | 943 | if (!is_writable($upload_dir)) |
---|
| 944 | { |
---|
| 945 | // last chance to make the directory writable |
---|
| 946 | @chmod($upload_dir, 0777); |
---|
| 947 | |
---|
| 948 | if (!is_writable($upload_dir)) |
---|
| 949 | { |
---|
| 950 | return new PwgError(500, 'directory has no write access'); |
---|
| 951 | } |
---|
| 952 | } |
---|
| 953 | |
---|
[2917] | 954 | secure_directory($upload_dir); |
---|
| 955 | |
---|
[2501] | 956 | // compute file path |
---|
[2463] | 957 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
---|
| 958 | $random_string = substr($params['file_sum'], 0, 8); |
---|
| 959 | $filename_wo_ext = $date_string.'-'.$random_string; |
---|
[2501] | 960 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
---|
[2496] | 961 | |
---|
[2501] | 962 | // dump the photo file |
---|
[2463] | 963 | $fh_file = fopen($file_path, 'w'); |
---|
[2786] | 964 | if (!fwrite($fh_file, base64_decode($params['file_content']))) |
---|
| 965 | { |
---|
| 966 | return new PwgError(500, 'error while writing file'); |
---|
| 967 | } |
---|
[2463] | 968 | fclose($fh_file); |
---|
[2511] | 969 | chmod($file_path, 0644); |
---|
[2463] | 970 | |
---|
[2501] | 971 | // check dumped file md5sum against expected md5sum |
---|
| 972 | $dumped_md5 = md5_file($file_path); |
---|
| 973 | if ($dumped_md5 != $params['file_sum']) { |
---|
[2670] | 974 | return new PwgError(500, 'file transfer failed'); |
---|
[2501] | 975 | } |
---|
[2463] | 976 | |
---|
[2501] | 977 | // thumbnail directory is a subdirectory of the photo file, hard coded |
---|
| 978 | // "thumbnail" |
---|
[2463] | 979 | $thumbnail_dir = $upload_dir.'/thumbnail'; |
---|
| 980 | if (!is_dir($thumbnail_dir)) { |
---|
| 981 | umask(0000); |
---|
[2786] | 982 | if (!@mkdir($thumbnail_dir, 0777)) |
---|
| 983 | { |
---|
| 984 | return new PwgError(500, 'error during thumbnail directory creation'); |
---|
| 985 | } |
---|
[2463] | 986 | } |
---|
[2496] | 987 | |
---|
[2786] | 988 | if (!is_writable($thumbnail_dir)) |
---|
| 989 | { |
---|
| 990 | // last chance to make the directory writable |
---|
| 991 | @chmod($thumbnail_dir, 0777); |
---|
| 992 | |
---|
| 993 | if (!is_writable($thumbnail_dir)) |
---|
| 994 | { |
---|
| 995 | return new PwgError(500, 'thumbnail directory has no write access'); |
---|
| 996 | } |
---|
| 997 | } |
---|
| 998 | |
---|
[2917] | 999 | secure_directory($thumbnail_dir); |
---|
| 1000 | |
---|
[2501] | 1001 | // thumbnail path, the filename may use a prefix and the extension is |
---|
| 1002 | // always "jpg" (no matter what the real file format is) |
---|
[2463] | 1003 | $thumbnail_path = sprintf( |
---|
| 1004 | '%s/%s%s.%s', |
---|
| 1005 | $thumbnail_dir, |
---|
| 1006 | $conf['prefix_thumbnail'], |
---|
| 1007 | $filename_wo_ext, |
---|
| 1008 | 'jpg' |
---|
| 1009 | ); |
---|
[2501] | 1010 | |
---|
| 1011 | // dump the thumbnail |
---|
[2463] | 1012 | $fh_thumbnail = fopen($thumbnail_path, 'w'); |
---|
[2786] | 1013 | if (!fwrite($fh_thumbnail, base64_decode($params['thumbnail_content']))) |
---|
| 1014 | { |
---|
| 1015 | return new PwgError(500, 'error while writing thumbnail'); |
---|
| 1016 | } |
---|
[2463] | 1017 | fclose($fh_thumbnail); |
---|
[2511] | 1018 | chmod($thumbnail_path, 0644); |
---|
[2463] | 1019 | |
---|
| 1020 | // check dumped thumbnail md5 |
---|
[2501] | 1021 | $dumped_md5 = md5_file($thumbnail_path); |
---|
| 1022 | if ($dumped_md5 != $params['thumbnail_sum']) { |
---|
[2670] | 1023 | return new PwgError(500, 'thumbnail transfer failed'); |
---|
[2501] | 1024 | } |
---|
[2463] | 1025 | |
---|
[2670] | 1026 | // high resolution |
---|
| 1027 | if (isset($params['high_content'])) |
---|
| 1028 | { |
---|
| 1029 | // high resolution directory is a subdirectory of the photo file, hard |
---|
| 1030 | // coded "pwg_high" |
---|
| 1031 | $high_dir = $upload_dir.'/pwg_high'; |
---|
| 1032 | if (!is_dir($high_dir)) { |
---|
| 1033 | umask(0000); |
---|
[2786] | 1034 | if (!@mkdir($high_dir, 0777)) |
---|
| 1035 | { |
---|
| 1036 | return new PwgError(500, 'error during high directory creation'); |
---|
| 1037 | } |
---|
[2670] | 1038 | } |
---|
[2463] | 1039 | |
---|
[2786] | 1040 | if (!is_writable($high_dir)) |
---|
| 1041 | { |
---|
| 1042 | // last chance to make the directory writable |
---|
| 1043 | @chmod($high_dir, 0777); |
---|
| 1044 | |
---|
| 1045 | if (!is_writable($high_dir)) |
---|
| 1046 | { |
---|
| 1047 | return new PwgError(500, 'high directory has no write access'); |
---|
| 1048 | } |
---|
| 1049 | } |
---|
| 1050 | |
---|
[2917] | 1051 | secure_directory($high_dir); |
---|
| 1052 | |
---|
[2670] | 1053 | // high resolution path, same name as web size file |
---|
| 1054 | $high_path = sprintf( |
---|
| 1055 | '%s/%s.%s', |
---|
| 1056 | $high_dir, |
---|
| 1057 | $filename_wo_ext, |
---|
| 1058 | 'jpg' |
---|
| 1059 | ); |
---|
| 1060 | |
---|
| 1061 | // dump the high resolution file |
---|
| 1062 | $fh_high = fopen($high_path, 'w'); |
---|
[2786] | 1063 | if (!fwrite($fh_high, base64_decode($params['high_content']))) |
---|
| 1064 | { |
---|
| 1065 | return new PwgError(500, 'error while writing high'); |
---|
| 1066 | } |
---|
[2670] | 1067 | fclose($fh_high); |
---|
| 1068 | chmod($high_path, 0644); |
---|
| 1069 | |
---|
| 1070 | // check dumped thumbnail md5 |
---|
| 1071 | $dumped_md5 = md5_file($high_path); |
---|
| 1072 | if ($dumped_md5 != $params['high_sum']) { |
---|
| 1073 | return new PwgError(500, 'high resolution transfer failed'); |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | $high_filesize = floor(filesize($high_path)/1024); |
---|
| 1077 | } |
---|
| 1078 | |
---|
[2569] | 1079 | list($width, $height) = getimagesize($file_path); |
---|
[2585] | 1080 | |
---|
[2463] | 1081 | // database registration |
---|
| 1082 | $insert = array( |
---|
| 1083 | 'file' => $filename_wo_ext.'.jpg', |
---|
| 1084 | 'date_available' => $dbnow, |
---|
| 1085 | 'tn_ext' => 'jpg', |
---|
| 1086 | 'name' => $params['name'], |
---|
| 1087 | 'path' => $file_path, |
---|
[2569] | 1088 | 'filesize' => floor(filesize($file_path)/1024), |
---|
| 1089 | 'width' => $width, |
---|
| 1090 | 'height' => $height, |
---|
[2592] | 1091 | 'md5sum' => $params['file_sum'], |
---|
[2463] | 1092 | ); |
---|
| 1093 | |
---|
[2569] | 1094 | $info_columns = array( |
---|
| 1095 | 'name', |
---|
| 1096 | 'author', |
---|
| 1097 | 'comment', |
---|
| 1098 | 'level', |
---|
| 1099 | 'date_creation', |
---|
| 1100 | ); |
---|
| 1101 | |
---|
| 1102 | foreach ($info_columns as $key) |
---|
| 1103 | { |
---|
| 1104 | if (isset($params[$key])) |
---|
| 1105 | { |
---|
| 1106 | $insert[$key] = $params[$key]; |
---|
| 1107 | } |
---|
| 1108 | } |
---|
| 1109 | |
---|
[2670] | 1110 | if (isset($params['high_content'])) |
---|
| 1111 | { |
---|
| 1112 | $insert['has_high'] = 'true'; |
---|
| 1113 | $insert['high_filesize'] = $high_filesize; |
---|
| 1114 | } |
---|
| 1115 | |
---|
[2463] | 1116 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1117 | mass_inserts( |
---|
| 1118 | IMAGES_TABLE, |
---|
| 1119 | array_keys($insert), |
---|
| 1120 | array($insert) |
---|
| 1121 | ); |
---|
| 1122 | |
---|
| 1123 | $image_id = mysql_insert_id(); |
---|
| 1124 | |
---|
[2569] | 1125 | // let's add links between the image and the categories |
---|
| 1126 | // |
---|
| 1127 | // $params['categories'] should look like 123,12;456,auto;789 which means: |
---|
| 1128 | // |
---|
| 1129 | // 1. associate with category 123 on rank 12 |
---|
| 1130 | // 2. associate with category 456 on automatic rank |
---|
| 1131 | // 3. associate with category 789 on automatic rank |
---|
| 1132 | if (isset($params['categories'])) |
---|
| 1133 | { |
---|
| 1134 | $cat_ids = array(); |
---|
| 1135 | $rank_on_category = array(); |
---|
| 1136 | $search_current_ranks = false; |
---|
[2585] | 1137 | |
---|
[2569] | 1138 | $tokens = explode(';', $params['categories']); |
---|
| 1139 | foreach ($tokens as $token) |
---|
| 1140 | { |
---|
| 1141 | list($cat_id, $rank) = explode(',', $token); |
---|
[2553] | 1142 | |
---|
[2569] | 1143 | array_push($cat_ids, $cat_id); |
---|
[2585] | 1144 | |
---|
[2569] | 1145 | if (!isset($rank)) |
---|
| 1146 | { |
---|
| 1147 | $rank = 'auto'; |
---|
| 1148 | } |
---|
| 1149 | $rank_on_category[$cat_id] = $rank; |
---|
| 1150 | |
---|
| 1151 | if ($rank == 'auto') |
---|
| 1152 | { |
---|
| 1153 | $search_current_ranks = true; |
---|
| 1154 | } |
---|
| 1155 | } |
---|
| 1156 | |
---|
| 1157 | $cat_ids = array_unique($cat_ids); |
---|
| 1158 | |
---|
| 1159 | if (count($cat_ids) > 0) |
---|
| 1160 | { |
---|
| 1161 | if ($search_current_ranks) |
---|
| 1162 | { |
---|
| 1163 | $query = ' |
---|
[2553] | 1164 | SELECT |
---|
[2569] | 1165 | category_id, |
---|
[2553] | 1166 | MAX(rank) AS max_rank |
---|
| 1167 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1168 | WHERE rank IS NOT NULL |
---|
[2569] | 1169 | AND category_id IN ('.implode(',', $cat_ids).') |
---|
| 1170 | GROUP BY category_id |
---|
[2553] | 1171 | ;'; |
---|
[2569] | 1172 | $current_rank_of = simple_hash_from_query( |
---|
| 1173 | $query, |
---|
| 1174 | 'category_id', |
---|
| 1175 | 'max_rank' |
---|
| 1176 | ); |
---|
| 1177 | |
---|
| 1178 | foreach ($cat_ids as $cat_id) |
---|
| 1179 | { |
---|
| 1180 | if ('auto' == $rank_on_category[$cat_id]) |
---|
| 1181 | { |
---|
[2672] | 1182 | $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; |
---|
[2569] | 1183 | } |
---|
| 1184 | } |
---|
| 1185 | } |
---|
| 1186 | |
---|
| 1187 | $inserts = array(); |
---|
| 1188 | |
---|
| 1189 | foreach ($cat_ids as $cat_id) |
---|
| 1190 | { |
---|
| 1191 | array_push( |
---|
| 1192 | $inserts, |
---|
| 1193 | array( |
---|
| 1194 | 'image_id' => $image_id, |
---|
| 1195 | 'category_id' => $cat_id, |
---|
| 1196 | 'rank' => $rank_on_category[$cat_id], |
---|
| 1197 | ) |
---|
| 1198 | ); |
---|
| 1199 | } |
---|
| 1200 | |
---|
| 1201 | mass_inserts( |
---|
| 1202 | IMAGE_CATEGORY_TABLE, |
---|
| 1203 | array_keys($inserts[0]), |
---|
| 1204 | $inserts |
---|
| 1205 | ); |
---|
[2672] | 1206 | |
---|
| 1207 | update_category($cat_ids); |
---|
[2569] | 1208 | } |
---|
[2553] | 1209 | } |
---|
[2569] | 1210 | |
---|
| 1211 | // and now, let's create tag associations |
---|
| 1212 | if (isset($params['tag_ids'])) |
---|
[2553] | 1213 | { |
---|
[2569] | 1214 | set_tags( |
---|
| 1215 | explode(',', $params['tag_ids']), |
---|
| 1216 | $image_id |
---|
| 1217 | ); |
---|
[2553] | 1218 | } |
---|
[2585] | 1219 | |
---|
[2501] | 1220 | invalidate_user_cache(); |
---|
| 1221 | |
---|
[2463] | 1222 | // fclose($fh_log); |
---|
| 1223 | } |
---|
| 1224 | |
---|
[1781] | 1225 | /** |
---|
| 1226 | * perform a login (web service method) |
---|
| 1227 | */ |
---|
[1698] | 1228 | function ws_session_login($params, &$service) |
---|
| 1229 | { |
---|
| 1230 | global $conf; |
---|
| 1231 | |
---|
| 1232 | if (!$service->isPost()) |
---|
| 1233 | { |
---|
[1852] | 1234 | return new PwgError(405, "This method requires HTTP POST"); |
---|
[1698] | 1235 | } |
---|
[1744] | 1236 | if (try_log_user($params['username'], $params['password'],false)) |
---|
[1698] | 1237 | { |
---|
| 1238 | return true; |
---|
| 1239 | } |
---|
| 1240 | return new PwgError(999, 'Invalid username/password'); |
---|
| 1241 | } |
---|
| 1242 | |
---|
[1781] | 1243 | |
---|
| 1244 | /** |
---|
| 1245 | * performs a logout (web service method) |
---|
| 1246 | */ |
---|
[1698] | 1247 | function ws_session_logout($params, &$service) |
---|
| 1248 | { |
---|
[2029] | 1249 | if (!is_a_guest()) |
---|
[1698] | 1250 | { |
---|
[2757] | 1251 | logout_user(); |
---|
[1698] | 1252 | } |
---|
| 1253 | return true; |
---|
| 1254 | } |
---|
| 1255 | |
---|
| 1256 | function ws_session_getStatus($params, &$service) |
---|
| 1257 | { |
---|
[2356] | 1258 | global $user; |
---|
[1698] | 1259 | $res = array(); |
---|
[2029] | 1260 | $res['username'] = is_a_guest() ? 'guest' : $user['username']; |
---|
[1849] | 1261 | foreach ( array('status', 'template', 'theme', 'language') as $k ) |
---|
| 1262 | { |
---|
| 1263 | $res[$k] = $user[$k]; |
---|
| 1264 | } |
---|
[2126] | 1265 | $res['charset'] = get_pwg_charset(); |
---|
[1698] | 1266 | return $res; |
---|
| 1267 | } |
---|
| 1268 | |
---|
| 1269 | |
---|
[1781] | 1270 | /** |
---|
| 1271 | * returns a list of tags (web service method) |
---|
| 1272 | */ |
---|
[1698] | 1273 | function ws_tags_getList($params, &$service) |
---|
| 1274 | { |
---|
[1711] | 1275 | $tags = get_available_tags(); |
---|
[1698] | 1276 | if ($params['sort_by_counter']) |
---|
| 1277 | { |
---|
| 1278 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
---|
| 1279 | } |
---|
| 1280 | else |
---|
| 1281 | { |
---|
[2409] | 1282 | usort($tags, 'tag_alpha_compare'); |
---|
[1698] | 1283 | } |
---|
| 1284 | for ($i=0; $i<count($tags); $i++) |
---|
| 1285 | { |
---|
[1815] | 1286 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
---|
[1698] | 1287 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
---|
| 1288 | $tags[$i]['url'] = make_index_url( |
---|
| 1289 | array( |
---|
| 1290 | 'section'=>'tags', |
---|
| 1291 | 'tags'=>array($tags[$i]) |
---|
| 1292 | ) |
---|
| 1293 | ); |
---|
| 1294 | } |
---|
[2585] | 1295 | return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'name', 'counter' )) ); |
---|
[1698] | 1296 | } |
---|
| 1297 | |
---|
[2584] | 1298 | /** |
---|
| 1299 | * returns the list of tags as you can see them in administration (web |
---|
| 1300 | * service method). |
---|
| 1301 | * |
---|
| 1302 | * Only admin can run this method and permissions are not taken into |
---|
| 1303 | * account. |
---|
| 1304 | */ |
---|
| 1305 | function ws_tags_getAdminList($params, &$service) |
---|
| 1306 | { |
---|
| 1307 | if (!is_admin()) |
---|
| 1308 | { |
---|
| 1309 | return new PwgError(401, 'Access denied'); |
---|
| 1310 | } |
---|
[2585] | 1311 | |
---|
[2584] | 1312 | $tags = get_all_tags(); |
---|
| 1313 | return array( |
---|
| 1314 | 'tags' => new PwgNamedArray( |
---|
| 1315 | $tags, |
---|
| 1316 | 'tag', |
---|
| 1317 | array( |
---|
| 1318 | 'name', |
---|
| 1319 | 'id', |
---|
| 1320 | 'url_name', |
---|
| 1321 | ) |
---|
| 1322 | ) |
---|
| 1323 | ); |
---|
| 1324 | } |
---|
[1781] | 1325 | |
---|
| 1326 | /** |
---|
| 1327 | * returns a list of images for tags (web service method) |
---|
| 1328 | */ |
---|
[1698] | 1329 | function ws_tags_getImages($params, &$service) |
---|
| 1330 | { |
---|
| 1331 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1816] | 1332 | global $conf; |
---|
[2119] | 1333 | |
---|
[1698] | 1334 | // first build all the tag_ids we are interested in |
---|
[1852] | 1335 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
---|
| 1336 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
---|
[1698] | 1337 | $tags_by_id = array(); |
---|
| 1338 | foreach( $tags as $tag ) |
---|
| 1339 | { |
---|
[1852] | 1340 | $tags['id'] = (int)$tag['id']; |
---|
[1815] | 1341 | $tags_by_id[ $tag['id'] ] = $tag; |
---|
[1698] | 1342 | } |
---|
| 1343 | unset($tags); |
---|
[1852] | 1344 | $tag_ids = array_keys($tags_by_id); |
---|
[1698] | 1345 | |
---|
| 1346 | |
---|
| 1347 | $image_ids = array(); |
---|
| 1348 | $image_tag_map = array(); |
---|
[1781] | 1349 | |
---|
[1698] | 1350 | if ( !empty($tag_ids) ) |
---|
| 1351 | { // build list of image ids with associated tags per image |
---|
| 1352 | if ($params['tag_mode_and']) |
---|
| 1353 | { |
---|
| 1354 | $image_ids = get_image_ids_for_tags( $tag_ids ); |
---|
| 1355 | } |
---|
| 1356 | else |
---|
| 1357 | { |
---|
| 1358 | $query = ' |
---|
| 1359 | SELECT image_id, GROUP_CONCAT(tag_id) tag_ids |
---|
| 1360 | FROM '.IMAGE_TAG_TABLE.' |
---|
| 1361 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
---|
| 1362 | GROUP BY image_id'; |
---|
| 1363 | $result = pwg_query($query); |
---|
| 1364 | while ( $row=mysql_fetch_assoc($result) ) |
---|
| 1365 | { |
---|
| 1366 | $row['image_id'] = (int)$row['image_id']; |
---|
| 1367 | array_push( $image_ids, $row['image_id'] ); |
---|
| 1368 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
---|
| 1369 | } |
---|
| 1370 | } |
---|
| 1371 | } |
---|
| 1372 | |
---|
| 1373 | $images = array(); |
---|
| 1374 | if ( !empty($image_ids)) |
---|
| 1375 | { |
---|
| 1376 | $where_clauses = ws_std_image_sql_filter($params); |
---|
[1711] | 1377 | $where_clauses[] = get_sql_condition_FandF( |
---|
| 1378 | array |
---|
| 1379 | ( |
---|
| 1380 | 'forbidden_categories' => 'category_id', |
---|
| 1381 | 'visible_categories' => 'category_id', |
---|
| 1382 | 'visible_images' => 'i.id' |
---|
| 1383 | ), |
---|
| 1384 | '', true |
---|
| 1385 | ); |
---|
[1698] | 1386 | $where_clauses[] = 'id IN ('.implode(',',$image_ids).')'; |
---|
[1757] | 1387 | |
---|
[1698] | 1388 | $order_by = ws_std_image_sql_order($params); |
---|
| 1389 | if (empty($order_by)) |
---|
| 1390 | { |
---|
| 1391 | $order_by = $conf['order_by']; |
---|
| 1392 | } |
---|
| 1393 | else |
---|
| 1394 | { |
---|
| 1395 | $order_by = 'ORDER BY '.$order_by; |
---|
| 1396 | } |
---|
| 1397 | |
---|
| 1398 | $query = ' |
---|
| 1399 | SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i |
---|
| 1400 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
---|
| 1401 | WHERE '. implode(' |
---|
| 1402 | AND ', $where_clauses).' |
---|
| 1403 | '.$order_by.' |
---|
[2770] | 1404 | LIMIT '.(int)($params['per_page']*$params['page']).','.(int)$params['per_page']; |
---|
[1698] | 1405 | |
---|
| 1406 | $result = pwg_query($query); |
---|
| 1407 | while ($row = mysql_fetch_assoc($result)) |
---|
| 1408 | { |
---|
[2119] | 1409 | $image = array(); |
---|
[1698] | 1410 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
| 1411 | { |
---|
| 1412 | if (isset($row[$k])) |
---|
| 1413 | { |
---|
| 1414 | $image[$k] = (int)$row[$k]; |
---|
| 1415 | } |
---|
| 1416 | } |
---|
[1880] | 1417 | foreach ( array('file', 'name', 'comment') as $k ) |
---|
[1698] | 1418 | { |
---|
| 1419 | $image[$k] = $row[$k]; |
---|
| 1420 | } |
---|
| 1421 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
| 1422 | |
---|
| 1423 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
---|
| 1424 | $image_tags = array(); |
---|
| 1425 | foreach ($image_tag_ids as $tag_id) |
---|
| 1426 | { |
---|
| 1427 | $url = make_index_url( |
---|
| 1428 | array( |
---|
| 1429 | 'section'=>'tags', |
---|
| 1430 | 'tags'=> array($tags_by_id[$tag_id]) |
---|
| 1431 | ) |
---|
| 1432 | ); |
---|
| 1433 | $page_url = make_picture_url( |
---|
| 1434 | array( |
---|
| 1435 | 'section'=>'tags', |
---|
| 1436 | 'tags'=> array($tags_by_id[$tag_id]), |
---|
| 1437 | 'image_id' => $row['id'], |
---|
| 1438 | 'image_file' => $row['file'], |
---|
| 1439 | ) |
---|
| 1440 | ); |
---|
| 1441 | array_push($image_tags, array( |
---|
| 1442 | 'id' => (int)$tag_id, |
---|
| 1443 | 'url' => $url, |
---|
| 1444 | 'page_url' => $page_url, |
---|
| 1445 | ) |
---|
| 1446 | ); |
---|
| 1447 | } |
---|
[1711] | 1448 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', |
---|
| 1449 | array('id','url_name','url','page_url') |
---|
[1698] | 1450 | ); |
---|
| 1451 | array_push($images, $image); |
---|
| 1452 | } |
---|
| 1453 | } |
---|
| 1454 | |
---|
| 1455 | return array( 'images' => |
---|
| 1456 | array ( |
---|
| 1457 | WS_XML_ATTRIBUTES => |
---|
| 1458 | array( |
---|
| 1459 | 'page' => $params['page'], |
---|
| 1460 | 'per_page' => $params['per_page'], |
---|
| 1461 | 'count' => count($images) |
---|
| 1462 | ), |
---|
[1711] | 1463 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
[1845] | 1464 | ws_std_get_image_xml_attributes() ) |
---|
[1698] | 1465 | ) |
---|
| 1466 | ); |
---|
| 1467 | } |
---|
[2583] | 1468 | |
---|
| 1469 | function ws_categories_add($params, &$service) |
---|
| 1470 | { |
---|
[2585] | 1471 | if (!is_admin() or is_adviser()) |
---|
[2583] | 1472 | { |
---|
| 1473 | return new PwgError(401, 'Access denied'); |
---|
| 1474 | } |
---|
| 1475 | |
---|
| 1476 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1477 | |
---|
| 1478 | $creation_output = create_virtual_category( |
---|
| 1479 | $params['name'], |
---|
| 1480 | $params['parent'] |
---|
| 1481 | ); |
---|
| 1482 | |
---|
| 1483 | if (isset($creation_output['error'])) |
---|
| 1484 | { |
---|
| 1485 | return new PwgError(500, $creation_output['error']); |
---|
| 1486 | } |
---|
[2585] | 1487 | |
---|
[2644] | 1488 | invalidate_user_cache(); |
---|
[2757] | 1489 | |
---|
[2583] | 1490 | return $creation_output; |
---|
| 1491 | } |
---|
[2634] | 1492 | |
---|
| 1493 | function ws_tags_add($params, &$service) |
---|
| 1494 | { |
---|
| 1495 | if (!is_admin() or is_adviser()) |
---|
| 1496 | { |
---|
| 1497 | return new PwgError(401, 'Access denied'); |
---|
| 1498 | } |
---|
| 1499 | |
---|
| 1500 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1501 | |
---|
| 1502 | $creation_output = create_tag($params['name']); |
---|
| 1503 | |
---|
| 1504 | if (isset($creation_output['error'])) |
---|
| 1505 | { |
---|
| 1506 | return new PwgError(500, $creation_output['error']); |
---|
| 1507 | } |
---|
| 1508 | |
---|
| 1509 | return $creation_output; |
---|
| 1510 | } |
---|
[2683] | 1511 | |
---|
| 1512 | function ws_images_exist($params, &$service) |
---|
| 1513 | { |
---|
| 1514 | if (!is_admin() or is_adviser()) |
---|
| 1515 | { |
---|
| 1516 | return new PwgError(401, 'Access denied'); |
---|
| 1517 | } |
---|
| 1518 | |
---|
| 1519 | // search among photos the list of photos already added, based on md5sum |
---|
| 1520 | // list |
---|
| 1521 | $md5sums = preg_split( |
---|
| 1522 | '/[\s,;\|]/', |
---|
| 1523 | $params['md5sum_list'], |
---|
| 1524 | -1, |
---|
| 1525 | PREG_SPLIT_NO_EMPTY |
---|
| 1526 | ); |
---|
[2757] | 1527 | |
---|
[2683] | 1528 | $query = ' |
---|
| 1529 | SELECT |
---|
| 1530 | id, |
---|
| 1531 | md5sum |
---|
| 1532 | FROM '.IMAGES_TABLE.' |
---|
[2757] | 1533 | WHERE md5sum IN (\''.implode("','", $md5sums).'\') |
---|
[2683] | 1534 | ;'; |
---|
| 1535 | $id_of_md5 = simple_hash_from_query($query, 'md5sum', 'id'); |
---|
| 1536 | |
---|
| 1537 | $result = array(); |
---|
[2757] | 1538 | |
---|
[2683] | 1539 | foreach ($md5sums as $md5sum) |
---|
| 1540 | { |
---|
| 1541 | $result[$md5sum] = null; |
---|
| 1542 | if (isset($id_of_md5[$md5sum])) |
---|
| 1543 | { |
---|
| 1544 | $result[$md5sum] = $id_of_md5[$md5sum]; |
---|
| 1545 | } |
---|
| 1546 | } |
---|
| 1547 | |
---|
| 1548 | return $result; |
---|
| 1549 | } |
---|
[1698] | 1550 | ?> |
---|