[1698] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[3046] | 5 | // | Copyright(C) 2008-2009 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | } |
---|
[2765] | 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.' |
---|
[2765] | 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 | $comm = array( |
---|
| 526 | 'author' => trim( stripslashes($params['author']) ), |
---|
| 527 | 'content' => trim( stripslashes($params['content']) ), |
---|
| 528 | 'image_id' => $params['image_id'], |
---|
| 529 | ); |
---|
| 530 | |
---|
| 531 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
[2119] | 532 | |
---|
| 533 | $comment_action = insert_user_comment( |
---|
[1849] | 534 | $comm, $params['key'], $infos |
---|
| 535 | ); |
---|
| 536 | |
---|
| 537 | switch ($comment_action) |
---|
| 538 | { |
---|
| 539 | case 'reject': |
---|
| 540 | array_push($infos, l10n('comment_not_added') ); |
---|
| 541 | return new PwgError(403, implode("\n", $infos) ); |
---|
| 542 | case 'validate': |
---|
| 543 | case 'moderate': |
---|
[2119] | 544 | $ret = array( |
---|
[1849] | 545 | 'id' => $comm['id'], |
---|
| 546 | 'validation' => $comment_action=='validate', |
---|
| 547 | ); |
---|
| 548 | return new PwgNamedStruct( |
---|
| 549 | 'comment', |
---|
[2119] | 550 | $ret, |
---|
| 551 | null, array() |
---|
[1849] | 552 | ); |
---|
| 553 | default: |
---|
| 554 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
---|
| 555 | } |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | /** |
---|
| 559 | * returns detailed information for an element (web service method) |
---|
| 560 | */ |
---|
[1698] | 561 | function ws_images_getInfo($params, &$service) |
---|
| 562 | { |
---|
| 563 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1849] | 564 | global $user, $conf; |
---|
[1698] | 565 | $params['image_id'] = (int)$params['image_id']; |
---|
| 566 | if ( $params['image_id']<=0 ) |
---|
| 567 | { |
---|
| 568 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 569 | } |
---|
[1781] | 570 | |
---|
[1698] | 571 | $query=' |
---|
| 572 | SELECT * FROM '.IMAGES_TABLE.' |
---|
[1711] | 573 | WHERE id='.$params['image_id']. |
---|
| 574 | get_sql_condition_FandF( |
---|
| 575 | array('visible_images' => 'id'), |
---|
| 576 | ' AND' |
---|
[2516] | 577 | ).' |
---|
| 578 | LIMIT 1'; |
---|
[1711] | 579 | |
---|
[1698] | 580 | $image_row = mysql_fetch_assoc(pwg_query($query)); |
---|
| 581 | if ($image_row==null) |
---|
| 582 | { |
---|
[1852] | 583 | return new PwgError(404, "image_id not found"); |
---|
[1698] | 584 | } |
---|
[1845] | 585 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
---|
[1698] | 586 | |
---|
| 587 | //-------------------------------------------------------- related categories |
---|
| 588 | $query = ' |
---|
[1866] | 589 | SELECT id, name, permalink, uppercats, global_rank, commentable |
---|
[1698] | 590 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
[1849] | 591 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
[2119] | 592 | WHERE image_id = '.$image_row['id']. |
---|
| 593 | get_sql_condition_FandF( |
---|
| 594 | array( 'forbidden_categories' => 'category_id' ), |
---|
| 595 | ' AND' |
---|
| 596 | ).' |
---|
[1698] | 597 | ;'; |
---|
| 598 | $result = pwg_query($query); |
---|
[1849] | 599 | $is_commentable = false; |
---|
[1698] | 600 | $related_categories = array(); |
---|
| 601 | while ($row = mysql_fetch_assoc($result)) |
---|
| 602 | { |
---|
[1849] | 603 | if ($row['commentable']=='true') |
---|
| 604 | { |
---|
| 605 | $is_commentable = true; |
---|
| 606 | } |
---|
| 607 | unset($row['commentable']); |
---|
[1698] | 608 | $row['url'] = make_index_url( |
---|
| 609 | array( |
---|
[1861] | 610 | 'category' => $row |
---|
[1698] | 611 | ) |
---|
| 612 | ); |
---|
| 613 | |
---|
| 614 | $row['page_url'] = make_picture_url( |
---|
| 615 | array( |
---|
| 616 | 'image_id' => $image_row['id'], |
---|
| 617 | 'image_file' => $image_row['file'], |
---|
[1861] | 618 | 'category' => $row |
---|
[1698] | 619 | ) |
---|
| 620 | ); |
---|
[1849] | 621 | $row['id']=(int)$row['id']; |
---|
[1698] | 622 | array_push($related_categories, $row); |
---|
| 623 | } |
---|
| 624 | usort($related_categories, 'global_rank_compare'); |
---|
| 625 | if ( empty($related_categories) ) |
---|
| 626 | { |
---|
| 627 | return new PwgError(401, 'Access denied'); |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | //-------------------------------------------------------------- related tags |
---|
[1815] | 631 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
---|
| 632 | foreach( $related_tags as $i=>$tag) |
---|
[1698] | 633 | { |
---|
[1815] | 634 | $tag['url'] = make_index_url( |
---|
[1698] | 635 | array( |
---|
[1815] | 636 | 'tags' => array($tag) |
---|
[1698] | 637 | ) |
---|
| 638 | ); |
---|
[1815] | 639 | $tag['page_url'] = make_picture_url( |
---|
[1698] | 640 | array( |
---|
| 641 | 'image_id' => $image_row['id'], |
---|
| 642 | 'image_file' => $image_row['file'], |
---|
[1815] | 643 | 'tags' => array($tag), |
---|
[1698] | 644 | ) |
---|
| 645 | ); |
---|
[1815] | 646 | unset($tag['counter']); |
---|
[1849] | 647 | $tag['id']=(int)$tag['id']; |
---|
[1815] | 648 | $related_tags[$i]=$tag; |
---|
[1698] | 649 | } |
---|
[1849] | 650 | //------------------------------------------------------------- related rates |
---|
| 651 | $query = ' |
---|
| 652 | SELECT COUNT(rate) AS count |
---|
| 653 | , ROUND(AVG(rate),2) AS average |
---|
| 654 | , ROUND(STD(rate),2) AS stdev |
---|
| 655 | FROM '.RATE_TABLE.' |
---|
| 656 | WHERE element_id = '.$image_row['id'].' |
---|
| 657 | ;'; |
---|
| 658 | $rating = mysql_fetch_assoc(pwg_query($query)); |
---|
| 659 | $rating['count'] = (int)$rating['count']; |
---|
| 660 | |
---|
[1698] | 661 | //---------------------------------------------------------- related comments |
---|
[1849] | 662 | $related_comments = array(); |
---|
[2119] | 663 | |
---|
[1849] | 664 | $where_comments = 'image_id = '.$image_row['id']; |
---|
| 665 | if ( !is_admin() ) |
---|
| 666 | { |
---|
| 667 | $where_comments .= ' |
---|
| 668 | AND validated="true"'; |
---|
| 669 | } |
---|
| 670 | |
---|
[1698] | 671 | $query = ' |
---|
| 672 | SELECT COUNT(id) nb_comments |
---|
| 673 | FROM '.COMMENTS_TABLE.' |
---|
[1849] | 674 | WHERE '.$where_comments; |
---|
[1698] | 675 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
---|
[1849] | 676 | $nb_comments = (int)$nb_comments; |
---|
[1698] | 677 | |
---|
[1849] | 678 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
---|
| 679 | { |
---|
| 680 | $query = ' |
---|
[1698] | 681 | SELECT id, date, author, content |
---|
| 682 | FROM '.COMMENTS_TABLE.' |
---|
[1849] | 683 | WHERE '.$where_comments.' |
---|
| 684 | ORDER BY date |
---|
[2765] | 685 | LIMIT '.(int)($params['comments_per_page']*$params['comments_page']). |
---|
| 686 | ','.(int)$params['comments_per_page']; |
---|
[1698] | 687 | |
---|
[1849] | 688 | $result = pwg_query($query); |
---|
| 689 | while ($row = mysql_fetch_assoc($result)) |
---|
| 690 | { |
---|
| 691 | $row['id']=(int)$row['id']; |
---|
| 692 | array_push($related_comments, $row); |
---|
| 693 | } |
---|
| 694 | } |
---|
[2119] | 695 | |
---|
[1849] | 696 | $comment_post_data = null; |
---|
[2119] | 697 | if ($is_commentable and |
---|
[2029] | 698 | (!is_a_guest() |
---|
| 699 | or (is_a_guest() and $conf['comments_forall'] ) |
---|
[1849] | 700 | ) |
---|
| 701 | ) |
---|
[1698] | 702 | { |
---|
[2119] | 703 | $comment_post_data['author'] = $user['username']; |
---|
[1849] | 704 | $comment_post_data['key'] = get_comment_post_key($params['image_id']); |
---|
[1698] | 705 | } |
---|
| 706 | |
---|
[1849] | 707 | $ret = $image_row; |
---|
| 708 | foreach ( array('id','width','height','hit','filesize') as $k ) |
---|
| 709 | { |
---|
| 710 | if (isset($ret[$k])) |
---|
| 711 | { |
---|
| 712 | $ret[$k] = (int)$ret[$k]; |
---|
| 713 | } |
---|
| 714 | } |
---|
| 715 | foreach ( array('path', 'storage_category_id') as $k ) |
---|
| 716 | { |
---|
| 717 | unset($ret[$k]); |
---|
| 718 | } |
---|
[1698] | 719 | |
---|
[1849] | 720 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
---|
[1698] | 721 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
---|
[2585] | 722 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','name','page_url') ); |
---|
[1849] | 723 | if ( isset($comment_post_data) ) |
---|
| 724 | { |
---|
| 725 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
---|
| 726 | } |
---|
[1698] | 727 | $ret['comments'] = array( |
---|
[2119] | 728 | WS_XML_ATTRIBUTES => |
---|
[1849] | 729 | array( |
---|
| 730 | 'page' => $params['comments_page'], |
---|
| 731 | 'per_page' => $params['comments_per_page'], |
---|
| 732 | 'count' => count($related_comments), |
---|
| 733 | 'nb_comments' => $nb_comments, |
---|
| 734 | ), |
---|
| 735 | WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') ) |
---|
[1698] | 736 | ); |
---|
[1845] | 737 | |
---|
[1698] | 738 | return new PwgNamedStruct('image',$ret, null, array('name','comment') ); |
---|
| 739 | } |
---|
| 740 | |
---|
[2435] | 741 | |
---|
[1837] | 742 | /** |
---|
[2435] | 743 | * rates the image_id in the parameter |
---|
| 744 | */ |
---|
| 745 | function ws_images_Rate($params, &$service) |
---|
| 746 | { |
---|
| 747 | $image_id = (int)$params['image_id']; |
---|
| 748 | $query = ' |
---|
| 749 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
---|
| 750 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
| 751 | WHERE id='.$image_id |
---|
| 752 | .get_sql_condition_FandF( |
---|
| 753 | array( |
---|
| 754 | 'forbidden_categories' => 'category_id', |
---|
| 755 | 'forbidden_images' => 'id', |
---|
| 756 | ), |
---|
| 757 | ' AND' |
---|
| 758 | ).' |
---|
| 759 | LIMIT 1'; |
---|
| 760 | if ( mysql_num_rows( pwg_query($query) )==0 ) |
---|
| 761 | { |
---|
| 762 | return new PwgError(404, "Invalid image_id or access denied" ); |
---|
| 763 | } |
---|
| 764 | $rate = (int)$params['rate']; |
---|
| 765 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
---|
| 766 | $res = rate_picture( $image_id, $rate ); |
---|
| 767 | if ($res==false) |
---|
| 768 | { |
---|
| 769 | global $conf; |
---|
| 770 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
---|
| 771 | } |
---|
| 772 | return $res; |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | |
---|
| 776 | /** |
---|
[1837] | 777 | * returns a list of elements corresponding to a query search |
---|
| 778 | */ |
---|
| 779 | function ws_images_search($params, &$service) |
---|
| 780 | { |
---|
| 781 | global $page; |
---|
| 782 | $images = array(); |
---|
| 783 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
---|
| 784 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1698] | 785 | |
---|
[2135] | 786 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
| 787 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
[1837] | 788 | |
---|
[2451] | 789 | $super_order_by = false; |
---|
[2135] | 790 | if ( !empty($order_by) ) |
---|
[1837] | 791 | { |
---|
[2135] | 792 | global $conf; |
---|
| 793 | $conf['order_by'] = 'ORDER BY '.$order_by; |
---|
[2451] | 794 | $super_order_by=true; // quick_search_result might be faster |
---|
[1837] | 795 | } |
---|
| 796 | |
---|
[2135] | 797 | $search_result = get_quick_search_results($params['query'], |
---|
[2451] | 798 | $super_order_by, |
---|
| 799 | implode(',', $where_clauses) |
---|
| 800 | ); |
---|
[2119] | 801 | |
---|
[2451] | 802 | $image_ids = array_slice( |
---|
| 803 | $search_result['items'], |
---|
| 804 | $params['page']*$params['per_page'], |
---|
| 805 | $params['per_page'] |
---|
| 806 | ); |
---|
[1837] | 807 | |
---|
| 808 | if ( count($image_ids) ) |
---|
| 809 | { |
---|
| 810 | $query = ' |
---|
| 811 | SELECT * FROM '.IMAGES_TABLE.' |
---|
[2451] | 812 | WHERE id IN ('.implode(',', $image_ids).')'; |
---|
[1837] | 813 | |
---|
[2451] | 814 | $image_ids = array_flip($image_ids); |
---|
[1837] | 815 | $result = pwg_query($query); |
---|
| 816 | while ($row = mysql_fetch_assoc($result)) |
---|
| 817 | { |
---|
| 818 | $image = array(); |
---|
| 819 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
| 820 | { |
---|
| 821 | if (isset($row[$k])) |
---|
| 822 | { |
---|
| 823 | $image[$k] = (int)$row[$k]; |
---|
| 824 | } |
---|
| 825 | } |
---|
[1880] | 826 | foreach ( array('file', 'name', 'comment') as $k ) |
---|
[1837] | 827 | { |
---|
| 828 | $image[$k] = $row[$k]; |
---|
| 829 | } |
---|
| 830 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
[2451] | 831 | $images[$image_ids[$image['id']]] = $image; |
---|
[1837] | 832 | } |
---|
[2451] | 833 | ksort($images, SORT_NUMERIC); |
---|
| 834 | $images = array_values($images); |
---|
[1837] | 835 | } |
---|
| 836 | |
---|
| 837 | |
---|
| 838 | return array( 'images' => |
---|
| 839 | array ( |
---|
| 840 | WS_XML_ATTRIBUTES => |
---|
| 841 | array( |
---|
| 842 | 'page' => $params['page'], |
---|
| 843 | 'per_page' => $params['per_page'], |
---|
| 844 | 'count' => count($images) |
---|
| 845 | ), |
---|
| 846 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
[1845] | 847 | ws_std_get_image_xml_attributes() ) |
---|
[1837] | 848 | ) |
---|
| 849 | ); |
---|
| 850 | } |
---|
| 851 | |
---|
[2413] | 852 | function ws_images_setPrivacyLevel($params, &$service) |
---|
| 853 | { |
---|
| 854 | if (!is_admin() || is_adviser() ) |
---|
| 855 | { |
---|
| 856 | return new PwgError(401, 'Access denied'); |
---|
| 857 | } |
---|
[2765] | 858 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
[2413] | 859 | if ( empty($params['image_id']) ) |
---|
| 860 | { |
---|
| 861 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 862 | } |
---|
| 863 | global $conf; |
---|
| 864 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
---|
| 865 | { |
---|
| 866 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
---|
| 867 | } |
---|
| 868 | $query = ' |
---|
| 869 | UPDATE '.IMAGES_TABLE.' |
---|
| 870 | SET level='.(int)$params['level'].' |
---|
| 871 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
---|
| 872 | $result = pwg_query($query); |
---|
| 873 | $affected_rows = mysql_affected_rows(); |
---|
| 874 | if ($affected_rows) |
---|
| 875 | { |
---|
| 876 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 877 | invalidate_user_cache(); |
---|
| 878 | } |
---|
| 879 | return $affected_rows; |
---|
| 880 | } |
---|
| 881 | |
---|
[3192] | 882 | function ws_images_add_chunk($params, &$service) |
---|
| 883 | { |
---|
| 884 | // data |
---|
| 885 | // original_sum |
---|
| 886 | // type {thumb, file, high} |
---|
| 887 | // position |
---|
| 888 | |
---|
| 889 | if (!is_admin() || is_adviser() ) |
---|
| 890 | { |
---|
| 891 | return new PwgError(401, 'Access denied'); |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | $upload_dir = PHPWG_ROOT_PATH.'upload/buffer'; |
---|
| 895 | |
---|
| 896 | // create the upload directory tree if not exists |
---|
| 897 | if (!is_dir($upload_dir)) { |
---|
| 898 | umask(0000); |
---|
| 899 | $recursive = true; |
---|
| 900 | if (!@mkdir($upload_dir, 0777, $recursive)) |
---|
| 901 | { |
---|
| 902 | return new PwgError(500, 'error during buffer directory creation'); |
---|
| 903 | } |
---|
| 904 | } |
---|
| 905 | |
---|
| 906 | if (!is_writable($upload_dir)) |
---|
| 907 | { |
---|
| 908 | // last chance to make the directory writable |
---|
| 909 | @chmod($upload_dir, 0777); |
---|
| 910 | |
---|
| 911 | if (!is_writable($upload_dir)) |
---|
| 912 | { |
---|
| 913 | return new PwgError(500, 'buffer directory has no write access'); |
---|
| 914 | } |
---|
| 915 | } |
---|
| 916 | |
---|
| 917 | secure_directory($upload_dir); |
---|
| 918 | |
---|
| 919 | $filename = sprintf( |
---|
| 920 | '%s-%s-%05u.block', |
---|
| 921 | $params['original_sum'], |
---|
| 922 | $params['type'], |
---|
| 923 | $params['position'] |
---|
| 924 | ); |
---|
| 925 | |
---|
[3239] | 926 | ws_logfile('[ws_images_add_chunk] data length : '.strlen($params['data'])); |
---|
| 927 | |
---|
[3192] | 928 | $bytes_written = file_put_contents( |
---|
| 929 | $upload_dir.'/'.$filename, |
---|
[3239] | 930 | base64_decode($params['data']) |
---|
[3192] | 931 | ); |
---|
| 932 | |
---|
| 933 | if (false === $bytes_written) { |
---|
| 934 | return new PwgError( |
---|
| 935 | 500, |
---|
| 936 | 'an error has occured while writting chunk '.$params['position'].' for '.$params['type'] |
---|
| 937 | ); |
---|
| 938 | } |
---|
| 939 | } |
---|
| 940 | |
---|
| 941 | function merge_chunks($output_filepath, $original_sum, $type) |
---|
| 942 | { |
---|
| 943 | ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath); |
---|
| 944 | |
---|
[4345] | 945 | if (is_file($output_filepath)) |
---|
| 946 | { |
---|
| 947 | unlink($output_filepath); |
---|
| 948 | |
---|
| 949 | if (is_file($output_filepath)) |
---|
| 950 | { |
---|
| 951 | new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath); |
---|
| 952 | exit(); |
---|
| 953 | } |
---|
| 954 | } |
---|
| 955 | |
---|
[3192] | 956 | $upload_dir = PHPWG_ROOT_PATH.'upload/buffer'; |
---|
| 957 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
---|
| 958 | $chunks = array(); |
---|
| 959 | |
---|
| 960 | if ($handle = opendir($upload_dir)) |
---|
| 961 | { |
---|
| 962 | while (false !== ($file = readdir($handle))) |
---|
| 963 | { |
---|
| 964 | if (preg_match($pattern, $file)) |
---|
| 965 | { |
---|
| 966 | ws_logfile($file); |
---|
| 967 | array_push($chunks, $upload_dir.'/'.$file); |
---|
| 968 | } |
---|
| 969 | } |
---|
| 970 | closedir($handle); |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | sort($chunks); |
---|
[3239] | 974 | |
---|
| 975 | ws_logfile('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage()); |
---|
[3513] | 976 | |
---|
| 977 | $i = 0; |
---|
[3192] | 978 | |
---|
[3239] | 979 | foreach ($chunks as $chunk) |
---|
| 980 | { |
---|
| 981 | $string = file_get_contents($chunk); |
---|
| 982 | |
---|
| 983 | ws_logfile('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage()); |
---|
| 984 | |
---|
| 985 | if (!file_put_contents($output_filepath, $string, FILE_APPEND)) |
---|
| 986 | { |
---|
[4328] | 987 | new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath); |
---|
| 988 | exit(); |
---|
[3239] | 989 | } |
---|
| 990 | |
---|
[3192] | 991 | unlink($chunk); |
---|
| 992 | } |
---|
[3239] | 993 | |
---|
| 994 | ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage()); |
---|
[3192] | 995 | } |
---|
| 996 | |
---|
[4328] | 997 | /* |
---|
| 998 | * The $file_path must be the path of the basic "web sized" photo |
---|
| 999 | * The $type value will automatically modify the $file_path to the corresponding file |
---|
| 1000 | */ |
---|
| 1001 | function add_file($file_path, $type, $original_sum, $file_sum) |
---|
| 1002 | { |
---|
[4344] | 1003 | $file_path = file_path_for_type($file_path, $type); |
---|
[4328] | 1004 | |
---|
| 1005 | $upload_dir = dirname($file_path); |
---|
| 1006 | |
---|
| 1007 | if (!is_dir($upload_dir)) { |
---|
| 1008 | umask(0000); |
---|
| 1009 | $recursive = true; |
---|
| 1010 | if (!@mkdir($upload_dir, 0777, $recursive)) |
---|
| 1011 | { |
---|
| 1012 | new PwgError(500, '[add_file] error during '.$type.' directory creation'); |
---|
| 1013 | exit(); |
---|
| 1014 | } |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | if (!is_writable($upload_dir)) |
---|
| 1018 | { |
---|
| 1019 | // last chance to make the directory writable |
---|
| 1020 | @chmod($upload_dir, 0777); |
---|
| 1021 | |
---|
| 1022 | if (!is_writable($upload_dir)) |
---|
| 1023 | { |
---|
| 1024 | new PwgError(500, '[add_file] '.$type.' directory has no write access'); |
---|
| 1025 | exit(); |
---|
| 1026 | } |
---|
| 1027 | } |
---|
| 1028 | |
---|
| 1029 | secure_directory($upload_dir); |
---|
| 1030 | |
---|
| 1031 | // merge the thumbnail |
---|
| 1032 | merge_chunks($file_path, $original_sum, $type); |
---|
| 1033 | chmod($file_path, 0644); |
---|
| 1034 | |
---|
| 1035 | // check dumped thumbnail md5 |
---|
| 1036 | $dumped_md5 = md5_file($file_path); |
---|
| 1037 | if ($dumped_md5 != $file_sum) { |
---|
| 1038 | new PwgError(500, '[add_file] '.$type.' transfer failed'); |
---|
| 1039 | exit(); |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | list($width, $height) = getimagesize($file_path); |
---|
| 1043 | $filesize = floor(filesize($file_path)/1024); |
---|
| 1044 | |
---|
| 1045 | return array( |
---|
| 1046 | 'width' => $width, |
---|
| 1047 | 'height' => $height, |
---|
| 1048 | 'filesize' => $filesize, |
---|
| 1049 | ); |
---|
| 1050 | } |
---|
| 1051 | |
---|
[4345] | 1052 | function ws_images_addFile($params, &$service) |
---|
| 1053 | { |
---|
| 1054 | // image_id |
---|
| 1055 | // type {thumb, file, high} |
---|
| 1056 | // sum |
---|
| 1057 | |
---|
| 1058 | global $conf; |
---|
| 1059 | if (!is_admin() || is_adviser() ) |
---|
| 1060 | { |
---|
| 1061 | return new PwgError(401, 'Access denied'); |
---|
| 1062 | } |
---|
| 1063 | |
---|
| 1064 | $params['image_id'] = (int)$params['image_id']; |
---|
| 1065 | if ($params['image_id'] <= 0) |
---|
| 1066 | { |
---|
| 1067 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 1068 | } |
---|
| 1069 | |
---|
| 1070 | // |
---|
| 1071 | // what is the path? |
---|
| 1072 | // |
---|
| 1073 | $query = ' |
---|
| 1074 | SELECT |
---|
| 1075 | path, |
---|
| 1076 | md5sum |
---|
| 1077 | FROM '.IMAGES_TABLE.' |
---|
| 1078 | WHERE id = '.$params['image_id'].' |
---|
| 1079 | ;'; |
---|
| 1080 | list($file_path, $original_sum) = mysql_fetch_row(pwg_query($query)); |
---|
| 1081 | |
---|
| 1082 | // TODO only files added with web API can be updated with web API |
---|
| 1083 | |
---|
| 1084 | // |
---|
| 1085 | // makes sure directories are there and call the merge_chunks |
---|
| 1086 | // |
---|
| 1087 | $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); |
---|
| 1088 | |
---|
| 1089 | // |
---|
| 1090 | // update basic metadata from file |
---|
| 1091 | // |
---|
| 1092 | $update = array(); |
---|
| 1093 | |
---|
| 1094 | if ('high' == $params['type']) |
---|
| 1095 | { |
---|
| 1096 | $update['high_filesize'] = $infos['filesize']; |
---|
| 1097 | $update['has_high'] = 'true'; |
---|
| 1098 | } |
---|
| 1099 | |
---|
| 1100 | if ('file' == $params['type']) |
---|
| 1101 | { |
---|
| 1102 | $update['filesize'] = $infos['filesize']; |
---|
| 1103 | $update['width'] = $infos['width']; |
---|
| 1104 | $update['height'] = $infos['height']; |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | // we may have nothing to update at database level, for example with a |
---|
| 1108 | // thumbnail update |
---|
| 1109 | if (count($update) > 0) |
---|
| 1110 | { |
---|
| 1111 | $update['id'] = $params['image_id']; |
---|
| 1112 | |
---|
| 1113 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1114 | mass_updates( |
---|
| 1115 | IMAGES_TABLE, |
---|
| 1116 | array( |
---|
| 1117 | 'primary' => array('id'), |
---|
| 1118 | 'update' => array_diff(array_keys($update), array('id')) |
---|
| 1119 | ), |
---|
| 1120 | array($update) |
---|
| 1121 | ); |
---|
| 1122 | } |
---|
| 1123 | } |
---|
| 1124 | |
---|
[2463] | 1125 | function ws_images_add($params, &$service) |
---|
| 1126 | { |
---|
| 1127 | global $conf; |
---|
[2496] | 1128 | if (!is_admin() || is_adviser() ) |
---|
| 1129 | { |
---|
| 1130 | return new PwgError(401, 'Access denied'); |
---|
| 1131 | } |
---|
| 1132 | |
---|
[3661] | 1133 | foreach ($params as $param_key => $param_value) { |
---|
| 1134 | ws_logfile( |
---|
| 1135 | sprintf( |
---|
| 1136 | '[pwg.images.add] input param "%s" : "%s"', |
---|
| 1137 | $param_key, |
---|
| 1138 | is_null($param_value) ? 'NULL' : $param_value |
---|
| 1139 | ) |
---|
| 1140 | ); |
---|
| 1141 | } |
---|
[2496] | 1142 | |
---|
[2592] | 1143 | // does the image already exists ? |
---|
| 1144 | $query = ' |
---|
| 1145 | SELECT |
---|
| 1146 | COUNT(*) AS counter |
---|
| 1147 | FROM '.IMAGES_TABLE.' |
---|
[3064] | 1148 | WHERE md5sum = \''.$params['original_sum'].'\' |
---|
[2592] | 1149 | ;'; |
---|
| 1150 | list($counter) = mysql_fetch_row(pwg_query($query)); |
---|
| 1151 | if ($counter != 0) { |
---|
| 1152 | return new PwgError(500, 'file already exists'); |
---|
| 1153 | } |
---|
| 1154 | |
---|
[2463] | 1155 | // current date |
---|
| 1156 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
| 1157 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
---|
[2496] | 1158 | |
---|
[2501] | 1159 | // upload directory hierarchy |
---|
[2463] | 1160 | $upload_dir = sprintf( |
---|
| 1161 | PHPWG_ROOT_PATH.'upload/%s/%s/%s', |
---|
| 1162 | $year, |
---|
| 1163 | $month, |
---|
| 1164 | $day |
---|
| 1165 | ); |
---|
| 1166 | |
---|
[2501] | 1167 | // compute file path |
---|
[2463] | 1168 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
---|
| 1169 | $random_string = substr($params['file_sum'], 0, 8); |
---|
| 1170 | $filename_wo_ext = $date_string.'-'.$random_string; |
---|
[2501] | 1171 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
---|
[2496] | 1172 | |
---|
[4328] | 1173 | // add files |
---|
| 1174 | $file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']); |
---|
| 1175 | $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']); |
---|
[2463] | 1176 | |
---|
[3192] | 1177 | if (isset($params['high_sum'])) |
---|
[2670] | 1178 | { |
---|
[4328] | 1179 | $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']); |
---|
[2670] | 1180 | } |
---|
| 1181 | |
---|
[2463] | 1182 | // database registration |
---|
| 1183 | $insert = array( |
---|
| 1184 | 'file' => $filename_wo_ext.'.jpg', |
---|
| 1185 | 'date_available' => $dbnow, |
---|
| 1186 | 'tn_ext' => 'jpg', |
---|
| 1187 | 'name' => $params['name'], |
---|
| 1188 | 'path' => $file_path, |
---|
[4328] | 1189 | 'filesize' => $file_infos['filesize'], |
---|
| 1190 | 'width' => $file_infos['width'], |
---|
| 1191 | 'height' => $file_infos['height'], |
---|
[3064] | 1192 | 'md5sum' => $params['original_sum'], |
---|
[2463] | 1193 | ); |
---|
| 1194 | |
---|
[2569] | 1195 | $info_columns = array( |
---|
| 1196 | 'name', |
---|
| 1197 | 'author', |
---|
| 1198 | 'comment', |
---|
| 1199 | 'level', |
---|
| 1200 | 'date_creation', |
---|
| 1201 | ); |
---|
| 1202 | |
---|
| 1203 | foreach ($info_columns as $key) |
---|
| 1204 | { |
---|
| 1205 | if (isset($params[$key])) |
---|
| 1206 | { |
---|
| 1207 | $insert[$key] = $params[$key]; |
---|
| 1208 | } |
---|
| 1209 | } |
---|
| 1210 | |
---|
[3192] | 1211 | if (isset($params['high_sum'])) |
---|
[2670] | 1212 | { |
---|
| 1213 | $insert['has_high'] = 'true'; |
---|
[4328] | 1214 | $insert['high_filesize'] = $high_infos['filesize']; |
---|
[2670] | 1215 | } |
---|
| 1216 | |
---|
[2463] | 1217 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1218 | mass_inserts( |
---|
| 1219 | IMAGES_TABLE, |
---|
| 1220 | array_keys($insert), |
---|
| 1221 | array($insert) |
---|
| 1222 | ); |
---|
| 1223 | |
---|
| 1224 | $image_id = mysql_insert_id(); |
---|
| 1225 | |
---|
[2569] | 1226 | // let's add links between the image and the categories |
---|
| 1227 | if (isset($params['categories'])) |
---|
| 1228 | { |
---|
[2722] | 1229 | ws_add_image_category_relations($image_id, $params['categories']); |
---|
[2553] | 1230 | } |
---|
[2569] | 1231 | |
---|
| 1232 | // and now, let's create tag associations |
---|
[3659] | 1233 | if (isset($params['tag_ids']) and !empty($params['tag_ids'])) |
---|
[2553] | 1234 | { |
---|
[2569] | 1235 | set_tags( |
---|
| 1236 | explode(',', $params['tag_ids']), |
---|
| 1237 | $image_id |
---|
| 1238 | ); |
---|
[2553] | 1239 | } |
---|
[2585] | 1240 | |
---|
[2501] | 1241 | invalidate_user_cache(); |
---|
[2463] | 1242 | } |
---|
| 1243 | |
---|
[1781] | 1244 | /** |
---|
| 1245 | * perform a login (web service method) |
---|
| 1246 | */ |
---|
[1698] | 1247 | function ws_session_login($params, &$service) |
---|
| 1248 | { |
---|
| 1249 | global $conf; |
---|
| 1250 | |
---|
| 1251 | if (!$service->isPost()) |
---|
| 1252 | { |
---|
[1852] | 1253 | return new PwgError(405, "This method requires HTTP POST"); |
---|
[1698] | 1254 | } |
---|
[1744] | 1255 | if (try_log_user($params['username'], $params['password'],false)) |
---|
[1698] | 1256 | { |
---|
| 1257 | return true; |
---|
| 1258 | } |
---|
| 1259 | return new PwgError(999, 'Invalid username/password'); |
---|
| 1260 | } |
---|
| 1261 | |
---|
[1781] | 1262 | |
---|
| 1263 | /** |
---|
| 1264 | * performs a logout (web service method) |
---|
| 1265 | */ |
---|
[1698] | 1266 | function ws_session_logout($params, &$service) |
---|
| 1267 | { |
---|
[2029] | 1268 | if (!is_a_guest()) |
---|
[1698] | 1269 | { |
---|
[2756] | 1270 | logout_user(); |
---|
[1698] | 1271 | } |
---|
| 1272 | return true; |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | function ws_session_getStatus($params, &$service) |
---|
| 1276 | { |
---|
[2356] | 1277 | global $user; |
---|
[1698] | 1278 | $res = array(); |
---|
[2029] | 1279 | $res['username'] = is_a_guest() ? 'guest' : $user['username']; |
---|
[1849] | 1280 | foreach ( array('status', 'template', 'theme', 'language') as $k ) |
---|
| 1281 | { |
---|
| 1282 | $res[$k] = $user[$k]; |
---|
| 1283 | } |
---|
[2126] | 1284 | $res['charset'] = get_pwg_charset(); |
---|
[1698] | 1285 | return $res; |
---|
| 1286 | } |
---|
| 1287 | |
---|
| 1288 | |
---|
[1781] | 1289 | /** |
---|
| 1290 | * returns a list of tags (web service method) |
---|
| 1291 | */ |
---|
[1698] | 1292 | function ws_tags_getList($params, &$service) |
---|
| 1293 | { |
---|
[1711] | 1294 | $tags = get_available_tags(); |
---|
[1698] | 1295 | if ($params['sort_by_counter']) |
---|
| 1296 | { |
---|
| 1297 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
---|
| 1298 | } |
---|
| 1299 | else |
---|
| 1300 | { |
---|
[2409] | 1301 | usort($tags, 'tag_alpha_compare'); |
---|
[1698] | 1302 | } |
---|
| 1303 | for ($i=0; $i<count($tags); $i++) |
---|
| 1304 | { |
---|
[1815] | 1305 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
---|
[1698] | 1306 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
---|
| 1307 | $tags[$i]['url'] = make_index_url( |
---|
| 1308 | array( |
---|
| 1309 | 'section'=>'tags', |
---|
| 1310 | 'tags'=>array($tags[$i]) |
---|
| 1311 | ) |
---|
| 1312 | ); |
---|
| 1313 | } |
---|
[2585] | 1314 | return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'name', 'counter' )) ); |
---|
[1698] | 1315 | } |
---|
| 1316 | |
---|
[2584] | 1317 | /** |
---|
| 1318 | * returns the list of tags as you can see them in administration (web |
---|
| 1319 | * service method). |
---|
| 1320 | * |
---|
| 1321 | * Only admin can run this method and permissions are not taken into |
---|
| 1322 | * account. |
---|
| 1323 | */ |
---|
| 1324 | function ws_tags_getAdminList($params, &$service) |
---|
| 1325 | { |
---|
| 1326 | if (!is_admin()) |
---|
| 1327 | { |
---|
| 1328 | return new PwgError(401, 'Access denied'); |
---|
| 1329 | } |
---|
[2585] | 1330 | |
---|
[2584] | 1331 | $tags = get_all_tags(); |
---|
| 1332 | return array( |
---|
| 1333 | 'tags' => new PwgNamedArray( |
---|
| 1334 | $tags, |
---|
| 1335 | 'tag', |
---|
| 1336 | array( |
---|
| 1337 | 'name', |
---|
| 1338 | 'id', |
---|
| 1339 | 'url_name', |
---|
| 1340 | ) |
---|
| 1341 | ) |
---|
| 1342 | ); |
---|
| 1343 | } |
---|
[1781] | 1344 | |
---|
| 1345 | /** |
---|
| 1346 | * returns a list of images for tags (web service method) |
---|
| 1347 | */ |
---|
[1698] | 1348 | function ws_tags_getImages($params, &$service) |
---|
| 1349 | { |
---|
| 1350 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
[1816] | 1351 | global $conf; |
---|
[2119] | 1352 | |
---|
[1698] | 1353 | // first build all the tag_ids we are interested in |
---|
[1852] | 1354 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
---|
| 1355 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
---|
[1698] | 1356 | $tags_by_id = array(); |
---|
| 1357 | foreach( $tags as $tag ) |
---|
| 1358 | { |
---|
[1852] | 1359 | $tags['id'] = (int)$tag['id']; |
---|
[1815] | 1360 | $tags_by_id[ $tag['id'] ] = $tag; |
---|
[1698] | 1361 | } |
---|
| 1362 | unset($tags); |
---|
[1852] | 1363 | $tag_ids = array_keys($tags_by_id); |
---|
[1698] | 1364 | |
---|
| 1365 | |
---|
| 1366 | $image_ids = array(); |
---|
| 1367 | $image_tag_map = array(); |
---|
[1781] | 1368 | |
---|
[1698] | 1369 | if ( !empty($tag_ids) ) |
---|
| 1370 | { // build list of image ids with associated tags per image |
---|
| 1371 | if ($params['tag_mode_and']) |
---|
| 1372 | { |
---|
| 1373 | $image_ids = get_image_ids_for_tags( $tag_ids ); |
---|
| 1374 | } |
---|
| 1375 | else |
---|
| 1376 | { |
---|
| 1377 | $query = ' |
---|
| 1378 | SELECT image_id, GROUP_CONCAT(tag_id) tag_ids |
---|
| 1379 | FROM '.IMAGE_TAG_TABLE.' |
---|
| 1380 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
---|
| 1381 | GROUP BY image_id'; |
---|
| 1382 | $result = pwg_query($query); |
---|
| 1383 | while ( $row=mysql_fetch_assoc($result) ) |
---|
| 1384 | { |
---|
| 1385 | $row['image_id'] = (int)$row['image_id']; |
---|
| 1386 | array_push( $image_ids, $row['image_id'] ); |
---|
| 1387 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
---|
| 1388 | } |
---|
| 1389 | } |
---|
| 1390 | } |
---|
| 1391 | |
---|
| 1392 | $images = array(); |
---|
| 1393 | if ( !empty($image_ids)) |
---|
| 1394 | { |
---|
| 1395 | $where_clauses = ws_std_image_sql_filter($params); |
---|
[1711] | 1396 | $where_clauses[] = get_sql_condition_FandF( |
---|
| 1397 | array |
---|
| 1398 | ( |
---|
| 1399 | 'forbidden_categories' => 'category_id', |
---|
| 1400 | 'visible_categories' => 'category_id', |
---|
| 1401 | 'visible_images' => 'i.id' |
---|
| 1402 | ), |
---|
| 1403 | '', true |
---|
| 1404 | ); |
---|
[1698] | 1405 | $where_clauses[] = 'id IN ('.implode(',',$image_ids).')'; |
---|
[1757] | 1406 | |
---|
[1698] | 1407 | $order_by = ws_std_image_sql_order($params); |
---|
| 1408 | if (empty($order_by)) |
---|
| 1409 | { |
---|
| 1410 | $order_by = $conf['order_by']; |
---|
| 1411 | } |
---|
| 1412 | else |
---|
| 1413 | { |
---|
| 1414 | $order_by = 'ORDER BY '.$order_by; |
---|
| 1415 | } |
---|
| 1416 | |
---|
| 1417 | $query = ' |
---|
| 1418 | SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i |
---|
| 1419 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
---|
| 1420 | WHERE '. implode(' |
---|
| 1421 | AND ', $where_clauses).' |
---|
| 1422 | '.$order_by.' |
---|
[2765] | 1423 | LIMIT '.(int)($params['per_page']*$params['page']).','.(int)$params['per_page']; |
---|
[1698] | 1424 | |
---|
| 1425 | $result = pwg_query($query); |
---|
| 1426 | while ($row = mysql_fetch_assoc($result)) |
---|
| 1427 | { |
---|
[2119] | 1428 | $image = array(); |
---|
[1698] | 1429 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
| 1430 | { |
---|
| 1431 | if (isset($row[$k])) |
---|
| 1432 | { |
---|
| 1433 | $image[$k] = (int)$row[$k]; |
---|
| 1434 | } |
---|
| 1435 | } |
---|
[1880] | 1436 | foreach ( array('file', 'name', 'comment') as $k ) |
---|
[1698] | 1437 | { |
---|
| 1438 | $image[$k] = $row[$k]; |
---|
| 1439 | } |
---|
| 1440 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
| 1441 | |
---|
| 1442 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
---|
| 1443 | $image_tags = array(); |
---|
| 1444 | foreach ($image_tag_ids as $tag_id) |
---|
| 1445 | { |
---|
| 1446 | $url = make_index_url( |
---|
| 1447 | array( |
---|
| 1448 | 'section'=>'tags', |
---|
| 1449 | 'tags'=> array($tags_by_id[$tag_id]) |
---|
| 1450 | ) |
---|
| 1451 | ); |
---|
| 1452 | $page_url = make_picture_url( |
---|
| 1453 | array( |
---|
| 1454 | 'section'=>'tags', |
---|
| 1455 | 'tags'=> array($tags_by_id[$tag_id]), |
---|
| 1456 | 'image_id' => $row['id'], |
---|
| 1457 | 'image_file' => $row['file'], |
---|
| 1458 | ) |
---|
| 1459 | ); |
---|
| 1460 | array_push($image_tags, array( |
---|
| 1461 | 'id' => (int)$tag_id, |
---|
| 1462 | 'url' => $url, |
---|
| 1463 | 'page_url' => $page_url, |
---|
| 1464 | ) |
---|
| 1465 | ); |
---|
| 1466 | } |
---|
[1711] | 1467 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', |
---|
| 1468 | array('id','url_name','url','page_url') |
---|
[1698] | 1469 | ); |
---|
| 1470 | array_push($images, $image); |
---|
| 1471 | } |
---|
| 1472 | } |
---|
| 1473 | |
---|
| 1474 | return array( 'images' => |
---|
| 1475 | array ( |
---|
| 1476 | WS_XML_ATTRIBUTES => |
---|
| 1477 | array( |
---|
| 1478 | 'page' => $params['page'], |
---|
| 1479 | 'per_page' => $params['per_page'], |
---|
| 1480 | 'count' => count($images) |
---|
| 1481 | ), |
---|
[1711] | 1482 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
[1845] | 1483 | ws_std_get_image_xml_attributes() ) |
---|
[1698] | 1484 | ) |
---|
| 1485 | ); |
---|
| 1486 | } |
---|
[2583] | 1487 | |
---|
| 1488 | function ws_categories_add($params, &$service) |
---|
| 1489 | { |
---|
[2585] | 1490 | if (!is_admin() or is_adviser()) |
---|
[2583] | 1491 | { |
---|
| 1492 | return new PwgError(401, 'Access denied'); |
---|
| 1493 | } |
---|
| 1494 | |
---|
| 1495 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1496 | |
---|
| 1497 | $creation_output = create_virtual_category( |
---|
| 1498 | $params['name'], |
---|
| 1499 | $params['parent'] |
---|
| 1500 | ); |
---|
| 1501 | |
---|
| 1502 | if (isset($creation_output['error'])) |
---|
| 1503 | { |
---|
| 1504 | return new PwgError(500, $creation_output['error']); |
---|
| 1505 | } |
---|
[2585] | 1506 | |
---|
[2644] | 1507 | invalidate_user_cache(); |
---|
[2756] | 1508 | |
---|
[2583] | 1509 | return $creation_output; |
---|
| 1510 | } |
---|
[2634] | 1511 | |
---|
| 1512 | function ws_tags_add($params, &$service) |
---|
| 1513 | { |
---|
| 1514 | if (!is_admin() or is_adviser()) |
---|
| 1515 | { |
---|
| 1516 | return new PwgError(401, 'Access denied'); |
---|
| 1517 | } |
---|
| 1518 | |
---|
| 1519 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1520 | |
---|
| 1521 | $creation_output = create_tag($params['name']); |
---|
| 1522 | |
---|
| 1523 | if (isset($creation_output['error'])) |
---|
| 1524 | { |
---|
| 1525 | return new PwgError(500, $creation_output['error']); |
---|
| 1526 | } |
---|
| 1527 | |
---|
| 1528 | return $creation_output; |
---|
| 1529 | } |
---|
[2683] | 1530 | |
---|
| 1531 | function ws_images_exist($params, &$service) |
---|
| 1532 | { |
---|
| 1533 | if (!is_admin() or is_adviser()) |
---|
| 1534 | { |
---|
| 1535 | return new PwgError(401, 'Access denied'); |
---|
| 1536 | } |
---|
| 1537 | |
---|
| 1538 | // search among photos the list of photos already added, based on md5sum |
---|
| 1539 | // list |
---|
| 1540 | $md5sums = preg_split( |
---|
| 1541 | '/[\s,;\|]/', |
---|
| 1542 | $params['md5sum_list'], |
---|
| 1543 | -1, |
---|
| 1544 | PREG_SPLIT_NO_EMPTY |
---|
| 1545 | ); |
---|
[2756] | 1546 | |
---|
[2683] | 1547 | $query = ' |
---|
| 1548 | SELECT |
---|
| 1549 | id, |
---|
| 1550 | md5sum |
---|
| 1551 | FROM '.IMAGES_TABLE.' |
---|
[2756] | 1552 | WHERE md5sum IN (\''.implode("','", $md5sums).'\') |
---|
[2683] | 1553 | ;'; |
---|
| 1554 | $id_of_md5 = simple_hash_from_query($query, 'md5sum', 'id'); |
---|
| 1555 | |
---|
| 1556 | $result = array(); |
---|
[2756] | 1557 | |
---|
[2683] | 1558 | foreach ($md5sums as $md5sum) |
---|
| 1559 | { |
---|
| 1560 | $result[$md5sum] = null; |
---|
| 1561 | if (isset($id_of_md5[$md5sum])) |
---|
| 1562 | { |
---|
| 1563 | $result[$md5sum] = $id_of_md5[$md5sum]; |
---|
| 1564 | } |
---|
| 1565 | } |
---|
| 1566 | |
---|
| 1567 | return $result; |
---|
| 1568 | } |
---|
[2722] | 1569 | |
---|
[4344] | 1570 | function ws_images_checkFiles($params, &$service) |
---|
| 1571 | { |
---|
| 1572 | if (!is_admin() or is_adviser()) |
---|
| 1573 | { |
---|
| 1574 | return new PwgError(401, 'Access denied'); |
---|
| 1575 | } |
---|
| 1576 | |
---|
| 1577 | // input parameters |
---|
| 1578 | // |
---|
| 1579 | // image_id |
---|
| 1580 | // thumbnail_sum |
---|
| 1581 | // file_sum |
---|
| 1582 | // high_sum |
---|
| 1583 | |
---|
| 1584 | $params['image_id'] = (int)$params['image_id']; |
---|
| 1585 | if ($params['image_id'] <= 0) |
---|
| 1586 | { |
---|
| 1587 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 1588 | } |
---|
| 1589 | |
---|
| 1590 | $query = ' |
---|
| 1591 | SELECT |
---|
| 1592 | path |
---|
| 1593 | FROM '.IMAGES_TABLE.' |
---|
| 1594 | WHERE id = '.$params['image_id'].' |
---|
| 1595 | ;'; |
---|
| 1596 | $result = pwg_query($query); |
---|
| 1597 | if (mysql_num_rows($result) == 0) { |
---|
| 1598 | return new PwgError(404, "image_id not found"); |
---|
| 1599 | } |
---|
| 1600 | list($path) = mysql_fetch_row($result); |
---|
| 1601 | |
---|
| 1602 | $ret = array(); |
---|
| 1603 | |
---|
| 1604 | foreach (array('thumb', 'file', 'high') as $type) { |
---|
| 1605 | $param_name = $type; |
---|
| 1606 | if ('thumb' == $type) { |
---|
| 1607 | $param_name = 'thumbnail'; |
---|
| 1608 | } |
---|
| 1609 | |
---|
| 1610 | if (isset($params[$param_name.'_sum'])) { |
---|
| 1611 | $type_path = file_path_for_type($path, $type); |
---|
| 1612 | if (!is_file($type_path)) { |
---|
| 1613 | $ret[$param_name] = 'missing'; |
---|
| 1614 | } |
---|
| 1615 | else { |
---|
| 1616 | if (md5_file($type_path) != $params[$param_name.'_sum']) { |
---|
| 1617 | $ret[$param_name] = 'differs'; |
---|
| 1618 | } |
---|
| 1619 | else { |
---|
| 1620 | $ret[$param_name] = 'equals'; |
---|
| 1621 | } |
---|
| 1622 | } |
---|
| 1623 | } |
---|
| 1624 | } |
---|
| 1625 | |
---|
| 1626 | return $ret; |
---|
| 1627 | } |
---|
| 1628 | |
---|
| 1629 | function file_path_for_type($file_path, $type='thumb') |
---|
| 1630 | { |
---|
| 1631 | // resolve the $file_path depending on the $type |
---|
| 1632 | if ('thumb' == $type) { |
---|
| 1633 | $file_path = get_thumbnail_location( |
---|
| 1634 | array( |
---|
| 1635 | 'path' => $file_path, |
---|
| 1636 | 'tn_ext' => 'jpg', |
---|
| 1637 | ) |
---|
| 1638 | ); |
---|
| 1639 | } |
---|
| 1640 | |
---|
| 1641 | if ('high' == $type) { |
---|
| 1642 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
| 1643 | $file_path = get_high_location( |
---|
| 1644 | array( |
---|
| 1645 | 'path' => $file_path, |
---|
| 1646 | 'has_high' => 'true' |
---|
| 1647 | ) |
---|
| 1648 | ); |
---|
| 1649 | } |
---|
| 1650 | |
---|
| 1651 | return $file_path; |
---|
| 1652 | } |
---|
| 1653 | |
---|
[2722] | 1654 | function ws_images_setInfo($params, &$service) |
---|
| 1655 | { |
---|
| 1656 | global $conf; |
---|
| 1657 | if (!is_admin() || is_adviser() ) |
---|
| 1658 | { |
---|
| 1659 | return new PwgError(401, 'Access denied'); |
---|
| 1660 | } |
---|
| 1661 | |
---|
| 1662 | // name |
---|
| 1663 | // category_id |
---|
| 1664 | // file_content |
---|
| 1665 | // file_sum |
---|
| 1666 | // thumbnail_content |
---|
| 1667 | // thumbnail_sum |
---|
[2756] | 1668 | |
---|
[2722] | 1669 | $params['image_id'] = (int)$params['image_id']; |
---|
| 1670 | if ($params['image_id'] <= 0) |
---|
| 1671 | { |
---|
| 1672 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
| 1673 | } |
---|
| 1674 | |
---|
| 1675 | $query=' |
---|
| 1676 | SELECT * |
---|
| 1677 | FROM '.IMAGES_TABLE.' |
---|
| 1678 | WHERE id = '.$params['image_id'].' |
---|
| 1679 | ;'; |
---|
| 1680 | |
---|
| 1681 | $image_row = mysql_fetch_assoc(pwg_query($query)); |
---|
| 1682 | if ($image_row == null) |
---|
| 1683 | { |
---|
| 1684 | return new PwgError(404, "image_id not found"); |
---|
| 1685 | } |
---|
| 1686 | |
---|
| 1687 | // database registration |
---|
| 1688 | $update = array( |
---|
| 1689 | 'id' => $params['image_id'], |
---|
| 1690 | ); |
---|
| 1691 | |
---|
| 1692 | $info_columns = array( |
---|
| 1693 | 'name', |
---|
| 1694 | 'author', |
---|
| 1695 | 'comment', |
---|
| 1696 | 'level', |
---|
| 1697 | 'date_creation', |
---|
| 1698 | ); |
---|
| 1699 | |
---|
| 1700 | $perform_update = false; |
---|
| 1701 | foreach ($info_columns as $key) |
---|
| 1702 | { |
---|
| 1703 | if (isset($params[$key])) |
---|
| 1704 | { |
---|
| 1705 | $perform_update = true; |
---|
| 1706 | $update[$key] = $params[$key]; |
---|
| 1707 | } |
---|
| 1708 | } |
---|
| 1709 | |
---|
| 1710 | if ($perform_update) |
---|
| 1711 | { |
---|
| 1712 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1713 | mass_updates( |
---|
| 1714 | IMAGES_TABLE, |
---|
| 1715 | array( |
---|
| 1716 | 'primary' => array('id'), |
---|
| 1717 | 'update' => array_diff(array_keys($update), array('id')) |
---|
| 1718 | ), |
---|
| 1719 | array($update) |
---|
| 1720 | ); |
---|
| 1721 | } |
---|
[2756] | 1722 | |
---|
[2722] | 1723 | if (isset($params['categories'])) |
---|
| 1724 | { |
---|
| 1725 | ws_add_image_category_relations( |
---|
| 1726 | $params['image_id'], |
---|
| 1727 | $params['categories'] |
---|
| 1728 | ); |
---|
| 1729 | } |
---|
| 1730 | |
---|
| 1731 | // and now, let's create tag associations |
---|
| 1732 | if (isset($params['tag_ids'])) |
---|
| 1733 | { |
---|
| 1734 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1735 | add_tags( |
---|
| 1736 | explode(',', $params['tag_ids']), |
---|
| 1737 | array($params['image_id']) |
---|
| 1738 | ); |
---|
| 1739 | } |
---|
| 1740 | |
---|
| 1741 | invalidate_user_cache(); |
---|
| 1742 | } |
---|
| 1743 | |
---|
| 1744 | function ws_add_image_category_relations($image_id, $categories_string) |
---|
| 1745 | { |
---|
| 1746 | // let's add links between the image and the categories |
---|
| 1747 | // |
---|
| 1748 | // $params['categories'] should look like 123,12;456,auto;789 which means: |
---|
| 1749 | // |
---|
| 1750 | // 1. associate with category 123 on rank 12 |
---|
| 1751 | // 2. associate with category 456 on automatic rank |
---|
| 1752 | // 3. associate with category 789 on automatic rank |
---|
| 1753 | $cat_ids = array(); |
---|
| 1754 | $rank_on_category = array(); |
---|
| 1755 | $search_current_ranks = false; |
---|
| 1756 | |
---|
| 1757 | $tokens = explode(';', $categories_string); |
---|
| 1758 | foreach ($tokens as $token) |
---|
| 1759 | { |
---|
[2918] | 1760 | @list($cat_id, $rank) = explode(',', $token); |
---|
[2722] | 1761 | |
---|
| 1762 | array_push($cat_ids, $cat_id); |
---|
| 1763 | |
---|
| 1764 | if (!isset($rank)) |
---|
| 1765 | { |
---|
| 1766 | $rank = 'auto'; |
---|
| 1767 | } |
---|
| 1768 | $rank_on_category[$cat_id] = $rank; |
---|
| 1769 | |
---|
| 1770 | if ($rank == 'auto') |
---|
| 1771 | { |
---|
| 1772 | $search_current_ranks = true; |
---|
| 1773 | } |
---|
| 1774 | } |
---|
| 1775 | |
---|
| 1776 | $cat_ids = array_unique($cat_ids); |
---|
| 1777 | |
---|
| 1778 | if (count($cat_ids) > 0) |
---|
| 1779 | { |
---|
| 1780 | if ($search_current_ranks) |
---|
| 1781 | { |
---|
| 1782 | $query = ' |
---|
| 1783 | SELECT |
---|
| 1784 | category_id, |
---|
| 1785 | MAX(rank) AS max_rank |
---|
| 1786 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 1787 | WHERE rank IS NOT NULL |
---|
| 1788 | AND category_id IN ('.implode(',', $cat_ids).') |
---|
| 1789 | GROUP BY category_id |
---|
| 1790 | ;'; |
---|
| 1791 | $current_rank_of = simple_hash_from_query( |
---|
| 1792 | $query, |
---|
| 1793 | 'category_id', |
---|
| 1794 | 'max_rank' |
---|
| 1795 | ); |
---|
| 1796 | |
---|
| 1797 | foreach ($cat_ids as $cat_id) |
---|
| 1798 | { |
---|
[2921] | 1799 | if (!isset($current_rank_of[$cat_id])) |
---|
| 1800 | { |
---|
| 1801 | $current_rank_of[$cat_id] = 0; |
---|
| 1802 | } |
---|
[3147] | 1803 | |
---|
[2722] | 1804 | if ('auto' == $rank_on_category[$cat_id]) |
---|
| 1805 | { |
---|
| 1806 | $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; |
---|
| 1807 | } |
---|
| 1808 | } |
---|
| 1809 | } |
---|
| 1810 | |
---|
| 1811 | $inserts = array(); |
---|
| 1812 | |
---|
| 1813 | foreach ($cat_ids as $cat_id) |
---|
| 1814 | { |
---|
| 1815 | array_push( |
---|
| 1816 | $inserts, |
---|
| 1817 | array( |
---|
| 1818 | 'image_id' => $image_id, |
---|
| 1819 | 'category_id' => $cat_id, |
---|
| 1820 | 'rank' => $rank_on_category[$cat_id], |
---|
| 1821 | ) |
---|
| 1822 | ); |
---|
| 1823 | } |
---|
| 1824 | |
---|
| 1825 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1826 | mass_inserts( |
---|
| 1827 | IMAGE_CATEGORY_TABLE, |
---|
| 1828 | array_keys($inserts[0]), |
---|
| 1829 | $inserts |
---|
| 1830 | ); |
---|
| 1831 | |
---|
| 1832 | update_category($cat_ids); |
---|
| 1833 | } |
---|
| 1834 | } |
---|
[3192] | 1835 | |
---|
[3453] | 1836 | function ws_categories_setInfo($params, &$service) |
---|
| 1837 | { |
---|
| 1838 | global $conf; |
---|
| 1839 | if (!is_admin() || is_adviser() ) |
---|
| 1840 | { |
---|
| 1841 | return new PwgError(401, 'Access denied'); |
---|
| 1842 | } |
---|
| 1843 | |
---|
| 1844 | // category_id |
---|
| 1845 | // name |
---|
| 1846 | // comment |
---|
| 1847 | |
---|
| 1848 | $params['category_id'] = (int)$params['category_id']; |
---|
| 1849 | if ($params['category_id'] <= 0) |
---|
| 1850 | { |
---|
| 1851 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
| 1852 | } |
---|
| 1853 | |
---|
| 1854 | // database registration |
---|
| 1855 | $update = array( |
---|
| 1856 | 'id' => $params['category_id'], |
---|
| 1857 | ); |
---|
| 1858 | |
---|
| 1859 | $info_columns = array( |
---|
| 1860 | 'name', |
---|
| 1861 | 'comment', |
---|
| 1862 | ); |
---|
| 1863 | |
---|
| 1864 | $perform_update = false; |
---|
| 1865 | foreach ($info_columns as $key) |
---|
| 1866 | { |
---|
| 1867 | if (isset($params[$key])) |
---|
| 1868 | { |
---|
| 1869 | $perform_update = true; |
---|
| 1870 | $update[$key] = $params[$key]; |
---|
| 1871 | } |
---|
| 1872 | } |
---|
| 1873 | |
---|
| 1874 | if ($perform_update) |
---|
| 1875 | { |
---|
| 1876 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 1877 | mass_updates( |
---|
| 1878 | CATEGORIES_TABLE, |
---|
| 1879 | array( |
---|
| 1880 | 'primary' => array('id'), |
---|
| 1881 | 'update' => array_diff(array_keys($update), array('id')) |
---|
| 1882 | ), |
---|
| 1883 | array($update) |
---|
| 1884 | ); |
---|
| 1885 | } |
---|
| 1886 | |
---|
| 1887 | } |
---|
| 1888 | |
---|
[3192] | 1889 | function ws_logfile($string) |
---|
| 1890 | { |
---|
[3661] | 1891 | global $conf; |
---|
| 1892 | |
---|
| 1893 | if (!$conf['ws_enable_log']) { |
---|
| 1894 | return true; |
---|
| 1895 | } |
---|
[3192] | 1896 | |
---|
| 1897 | file_put_contents( |
---|
[3661] | 1898 | $conf['ws_log_filepath'], |
---|
[3192] | 1899 | '['.date('c').'] '.$string."\n", |
---|
| 1900 | FILE_APPEND |
---|
| 1901 | ); |
---|
| 1902 | } |
---|
[1698] | 1903 | ?> |
---|