| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | Piwigo - a PHP based picture gallery | |
|---|
| 4 | // +-----------------------------------------------------------------------+ |
|---|
| 5 | // | Copyright(C) 2008-2009 Piwigo Team http://piwigo.org | |
|---|
| 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
|---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
|---|
| 8 | // +-----------------------------------------------------------------------+ |
|---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
|---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
|---|
| 11 | // | the Free Software Foundation | |
|---|
| 12 | // | | |
|---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
|---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
|---|
| 16 | // | General Public License for more details. | |
|---|
| 17 | // | | |
|---|
| 18 | // | You should have received a copy of the GNU General Public License | |
|---|
| 19 | // | along with this program; if not, write to the Free Software | |
|---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
|---|
| 21 | // | USA. | |
|---|
| 22 | // +-----------------------------------------------------------------------+ |
|---|
| 23 | |
|---|
| 24 | /**** IMPLEMENTATION OF WEB SERVICE METHODS ***********************************/ |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Event handler for method invocation security check. Should return a PwgError |
|---|
| 28 | * if the preconditions are not satifsied for method invocation. |
|---|
| 29 | */ |
|---|
| 30 | function ws_isInvokeAllowed($res, $methodName, $params) |
|---|
| 31 | { |
|---|
| 32 | global $conf; |
|---|
| 33 | |
|---|
| 34 | if ( strpos($methodName,'reflection.')===0 ) |
|---|
| 35 | { // OK for reflection |
|---|
| 36 | return $res; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | if ( !is_autorize_status(ACCESS_GUEST) and |
|---|
| 40 | strpos($methodName,'pwg.session.')!==0 ) |
|---|
| 41 | { |
|---|
| 42 | return new PwgError(401, 'Access denied'); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | return $res; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * returns a "standard" (for our web service) array of sql where clauses that |
|---|
| 50 | * filters the images (images table only) |
|---|
| 51 | */ |
|---|
| 52 | function ws_std_image_sql_filter( $params, $tbl_name='' ) |
|---|
| 53 | { |
|---|
| 54 | $clauses = array(); |
|---|
| 55 | if ( is_numeric($params['f_min_rate']) ) |
|---|
| 56 | { |
|---|
| 57 | $clauses[] = $tbl_name.'average_rate>'.$params['f_min_rate']; |
|---|
| 58 | } |
|---|
| 59 | if ( is_numeric($params['f_max_rate']) ) |
|---|
| 60 | { |
|---|
| 61 | $clauses[] = $tbl_name.'average_rate<='.$params['f_max_rate']; |
|---|
| 62 | } |
|---|
| 63 | if ( is_numeric($params['f_min_hit']) ) |
|---|
| 64 | { |
|---|
| 65 | $clauses[] = $tbl_name.'hit>'.$params['f_min_hit']; |
|---|
| 66 | } |
|---|
| 67 | if ( is_numeric($params['f_max_hit']) ) |
|---|
| 68 | { |
|---|
| 69 | $clauses[] = $tbl_name.'hit<='.$params['f_max_hit']; |
|---|
| 70 | } |
|---|
| 71 | if ( isset($params['f_min_date_posted']) ) |
|---|
| 72 | { |
|---|
| 73 | $clauses[] = $tbl_name."date_available>='".$params['f_min_date_posted']."'"; |
|---|
| 74 | } |
|---|
| 75 | if ( isset($params['f_max_date_posted']) ) |
|---|
| 76 | { |
|---|
| 77 | $clauses[] = $tbl_name."date_available<'".$params['f_max_date_posted']."'"; |
|---|
| 78 | } |
|---|
| 79 | if ( isset($params['f_min_date_created']) ) |
|---|
| 80 | { |
|---|
| 81 | $clauses[] = $tbl_name."date_creation>='".$params['f_min_date_created']."'"; |
|---|
| 82 | } |
|---|
| 83 | if ( isset($params['f_max_date_created']) ) |
|---|
| 84 | { |
|---|
| 85 | $clauses[] = $tbl_name."date_creation<'".$params['f_max_date_created']."'"; |
|---|
| 86 | } |
|---|
| 87 | if ( is_numeric($params['f_min_ratio']) ) |
|---|
| 88 | { |
|---|
| 89 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height>'.$params['f_min_ratio']; |
|---|
| 90 | } |
|---|
| 91 | if ( is_numeric($params['f_max_ratio']) ) |
|---|
| 92 | { |
|---|
| 93 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio']; |
|---|
| 94 | } |
|---|
| 95 | if ( $params['f_with_thumbnail'] ) |
|---|
| 96 | { |
|---|
| 97 | $clauses[] = $tbl_name.'tn_ext IS NOT NULL'; |
|---|
| 98 | } |
|---|
| 99 | return $clauses; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * returns a "standard" (for our web service) ORDER BY sql clause for images |
|---|
| 104 | */ |
|---|
| 105 | function ws_std_image_sql_order( $params, $tbl_name='' ) |
|---|
| 106 | { |
|---|
| 107 | $ret = ''; |
|---|
| 108 | if ( empty($params['order']) ) |
|---|
| 109 | { |
|---|
| 110 | return $ret; |
|---|
| 111 | } |
|---|
| 112 | $matches = array(); |
|---|
| 113 | preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i', |
|---|
| 114 | $params['order'], $matches); |
|---|
| 115 | for ($i=0; $i<count($matches[1]); $i++) |
|---|
| 116 | { |
|---|
| 117 | switch ($matches[1][$i]) |
|---|
| 118 | { |
|---|
| 119 | case 'date_created': |
|---|
| 120 | $matches[1][$i] = 'date_creation'; break; |
|---|
| 121 | case 'date_posted': |
|---|
| 122 | $matches[1][$i] = 'date_available'; break; |
|---|
| 123 | case 'rand': case 'random': |
|---|
| 124 | $matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break; |
|---|
| 125 | } |
|---|
| 126 | $sortable_fields = array('id', 'file', 'name', 'hit', 'average_rate', |
|---|
| 127 | 'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' ); |
|---|
| 128 | if ( in_array($matches[1][$i], $sortable_fields) ) |
|---|
| 129 | { |
|---|
| 130 | if (!empty($ret)) |
|---|
| 131 | $ret .= ', '; |
|---|
| 132 | if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' ) |
|---|
| 133 | { |
|---|
| 134 | $ret .= $tbl_name; |
|---|
| 135 | } |
|---|
| 136 | $ret .= $matches[1][$i]; |
|---|
| 137 | $ret .= ' '.$matches[2][$i]; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | return $ret; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * returns an array map of urls (thumb/element) for image_row - to be returned |
|---|
| 145 | * in a standard way by different web service methods |
|---|
| 146 | */ |
|---|
| 147 | function ws_std_get_urls($image_row) |
|---|
| 148 | { |
|---|
| 149 | $ret = array( |
|---|
| 150 | 'tn_url' => get_thumbnail_url($image_row), |
|---|
| 151 | 'element_url' => get_element_url($image_row) |
|---|
| 152 | ); |
|---|
| 153 | global $user; |
|---|
| 154 | if ($user['enabled_high'] and $image_row['has_high'] ) |
|---|
| 155 | { |
|---|
| 156 | $ret['high_url'] = get_high_url($image_row); |
|---|
| 157 | } |
|---|
| 158 | return $ret; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * returns an array of image attributes that are to be encoded as xml attributes |
|---|
| 163 | * instead of xml elements |
|---|
| 164 | */ |
|---|
| 165 | function ws_std_get_image_xml_attributes() |
|---|
| 166 | { |
|---|
| 167 | return array( |
|---|
| 168 | 'id','tn_url','element_url','high_url', 'file','width','height','hit' |
|---|
| 169 | ); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * returns PWG version (web service method) |
|---|
| 174 | */ |
|---|
| 175 | function ws_getVersion($params, &$service) |
|---|
| 176 | { |
|---|
| 177 | global $conf; |
|---|
| 178 | if ($conf['show_version']) |
|---|
| 179 | return PHPWG_VERSION; |
|---|
| 180 | else |
|---|
| 181 | return new PwgError(403, 'Forbidden'); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | function ws_caddie_add($params, &$service) |
|---|
| 185 | { |
|---|
| 186 | if (!is_admin()) |
|---|
| 187 | { |
|---|
| 188 | return new PwgError(401, 'Access denied'); |
|---|
| 189 | } |
|---|
| 190 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
|---|
| 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 | } |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * returns images per category (web service method) |
|---|
| 216 | */ |
|---|
| 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 '.DB_REGEX_OPERATOR.' \'(^|,)'.$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 | } |
|---|
| 247 | $where_clauses[] = get_sql_condition_FandF( |
|---|
| 248 | array('forbidden_categories' => 'id'), |
|---|
| 249 | NULL, true |
|---|
| 250 | ); |
|---|
| 251 | |
|---|
| 252 | $query = ' |
|---|
| 253 | SELECT id, name, permalink, image_order |
|---|
| 254 | FROM '.CATEGORIES_TABLE.' |
|---|
| 255 | WHERE '. implode(' |
|---|
| 256 | AND ', $where_clauses); |
|---|
| 257 | $result = pwg_query($query); |
|---|
| 258 | $cats = array(); |
|---|
| 259 | while ($row = pwg_db_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 | .')'; |
|---|
| 272 | $where_clauses[] = get_sql_condition_FandF( array( |
|---|
| 273 | 'visible_images' => 'i.id' |
|---|
| 274 | ), null, true |
|---|
| 275 | ); |
|---|
| 276 | |
|---|
| 277 | $order_by = ws_std_image_sql_order($params, 'i.'); |
|---|
| 278 | if ( empty($order_by) |
|---|
| 279 | and count($params['cat_id'])==1 |
|---|
| 280 | and isset($cats[ $params['cat_id'][0] ]['image_order']) |
|---|
| 281 | ) |
|---|
| 282 | { |
|---|
| 283 | $order_by = $cats[ $params['cat_id'][0] ]['image_order']; |
|---|
| 284 | } |
|---|
| 285 | $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; |
|---|
| 286 | |
|---|
| 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.' |
|---|
| 295 | LIMIT '.(int)$params['per_page'].' OFFSET '.(int)($params['per_page']*$params['page']); |
|---|
| 296 | |
|---|
| 297 | $result = pwg_query($query); |
|---|
| 298 | while ($row = pwg_db_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 | } |
|---|
| 308 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 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( |
|---|
| 319 | 'category' => $cats[$cat_id], |
|---|
| 320 | ) |
|---|
| 321 | ); |
|---|
| 322 | $page_url = make_picture_url( |
|---|
| 323 | array( |
|---|
| 324 | 'category' => $cats[$cat_id], |
|---|
| 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 | ), |
|---|
| 354 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 355 | ws_std_get_image_xml_attributes() ) |
|---|
| 356 | ) |
|---|
| 357 | ); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * returns a list of categories (web service method) |
|---|
| 363 | */ |
|---|
| 364 | function ws_categories_getList($params, &$service) |
|---|
| 365 | { |
|---|
| 366 | global $user,$conf; |
|---|
| 367 | |
|---|
| 368 | $where = array('1=1'); |
|---|
| 369 | $join_type = 'INNER'; |
|---|
| 370 | $join_user = $user['id']; |
|---|
| 371 | |
|---|
| 372 | if (!$params['recursive']) |
|---|
| 373 | { |
|---|
| 374 | if ($params['cat_id']>0) |
|---|
| 375 | $where[] = '(id_uppercat='.(int)($params['cat_id']).' |
|---|
| 376 | OR id='.(int)($params['cat_id']).')'; |
|---|
| 377 | else |
|---|
| 378 | $where[] = 'id_uppercat IS NULL'; |
|---|
| 379 | } |
|---|
| 380 | else if ($params['cat_id']>0) |
|---|
| 381 | { |
|---|
| 382 | $where[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'. |
|---|
| 383 | (int)($params['cat_id']) |
|---|
| 384 | .'(,|$)\''; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | if ($params['public']) |
|---|
| 388 | { |
|---|
| 389 | $where[] = 'status = "public"'; |
|---|
| 390 | $where[] = 'visible = "true"'; |
|---|
| 391 | |
|---|
| 392 | $join_user = $conf['guest_id']; |
|---|
| 393 | } |
|---|
| 394 | elseif (is_admin()) |
|---|
| 395 | { |
|---|
| 396 | // in this very specific case, we don't want to hide empty |
|---|
| 397 | // categories. Function calculate_permissions will only return |
|---|
| 398 | // categories that are either locked or private and not permitted |
|---|
| 399 | // |
|---|
| 400 | // calculate_permissions does not consider empty categories as forbidden |
|---|
| 401 | $forbidden_categories = calculate_permissions($user['id'], $user['status']); |
|---|
| 402 | $where[]= 'id NOT IN ('.$forbidden_categories.')'; |
|---|
| 403 | $join_type = 'LEFT'; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | $query = ' |
|---|
| 407 | SELECT id, name, permalink, uppercats, global_rank, |
|---|
| 408 | nb_images, count_images AS total_nb_images, |
|---|
| 409 | date_last, max_date_last, count_categories AS nb_categories |
|---|
| 410 | FROM '.CATEGORIES_TABLE.' |
|---|
| 411 | '.$join_type.' JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id AND user_id='.$join_user.' |
|---|
| 412 | WHERE '. implode(' |
|---|
| 413 | AND ', $where); |
|---|
| 414 | |
|---|
| 415 | $result = pwg_query($query); |
|---|
| 416 | |
|---|
| 417 | $cats = array(); |
|---|
| 418 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 419 | { |
|---|
| 420 | $row['url'] = make_index_url( |
|---|
| 421 | array( |
|---|
| 422 | 'category' => $row |
|---|
| 423 | ) |
|---|
| 424 | ); |
|---|
| 425 | foreach( array('id','nb_images','total_nb_images','nb_categories') as $key) |
|---|
| 426 | { |
|---|
| 427 | $row[$key] = (int)$row[$key]; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | $row['name'] = strip_tags( |
|---|
| 431 | trigger_event( |
|---|
| 432 | 'render_category_name', |
|---|
| 433 | $row['name'], |
|---|
| 434 | 'ws_categories_getList' |
|---|
| 435 | ) |
|---|
| 436 | ); |
|---|
| 437 | |
|---|
| 438 | array_push($cats, $row); |
|---|
| 439 | } |
|---|
| 440 | usort($cats, 'global_rank_compare'); |
|---|
| 441 | return array( |
|---|
| 442 | 'categories' => new PwgNamedArray( |
|---|
| 443 | $cats, |
|---|
| 444 | 'category', |
|---|
| 445 | array( |
|---|
| 446 | 'id', |
|---|
| 447 | 'url', |
|---|
| 448 | 'nb_images', |
|---|
| 449 | 'total_nb_images', |
|---|
| 450 | 'nb_categories', |
|---|
| 451 | 'date_last', |
|---|
| 452 | 'max_date_last', |
|---|
| 453 | ) |
|---|
| 454 | ) |
|---|
| 455 | ); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | /** |
|---|
| 459 | * returns the list of categories as you can see them in administration (web |
|---|
| 460 | * service method). |
|---|
| 461 | * |
|---|
| 462 | * Only admin can run this method and permissions are not taken into |
|---|
| 463 | * account. |
|---|
| 464 | */ |
|---|
| 465 | function ws_categories_getAdminList($params, &$service) |
|---|
| 466 | { |
|---|
| 467 | if (!is_admin()) |
|---|
| 468 | { |
|---|
| 469 | return new PwgError(401, 'Access denied'); |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | $query = ' |
|---|
| 473 | SELECT |
|---|
| 474 | category_id, |
|---|
| 475 | COUNT(*) AS counter |
|---|
| 476 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 477 | GROUP BY category_id |
|---|
| 478 | ;'; |
|---|
| 479 | $nb_images_of = simple_hash_from_query($query, 'category_id', 'counter'); |
|---|
| 480 | |
|---|
| 481 | $query = ' |
|---|
| 482 | SELECT |
|---|
| 483 | id, |
|---|
| 484 | name, |
|---|
| 485 | uppercats, |
|---|
| 486 | global_rank |
|---|
| 487 | FROM '.CATEGORIES_TABLE.' |
|---|
| 488 | ;'; |
|---|
| 489 | $result = pwg_query($query); |
|---|
| 490 | $cats = array(); |
|---|
| 491 | |
|---|
| 492 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 493 | { |
|---|
| 494 | $id = $row['id']; |
|---|
| 495 | $row['nb_images'] = isset($nb_images_of[$id]) ? $nb_images_of[$id] : 0; |
|---|
| 496 | $row['name'] = strip_tags( |
|---|
| 497 | trigger_event( |
|---|
| 498 | 'render_category_name', |
|---|
| 499 | $row['name'], |
|---|
| 500 | 'ws_categories_getAdminList' |
|---|
| 501 | ) |
|---|
| 502 | ); |
|---|
| 503 | array_push($cats, $row); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | usort($cats, 'global_rank_compare'); |
|---|
| 507 | return array( |
|---|
| 508 | 'categories' => new PwgNamedArray( |
|---|
| 509 | $cats, |
|---|
| 510 | 'category', |
|---|
| 511 | array( |
|---|
| 512 | 'id', |
|---|
| 513 | 'nb_images', |
|---|
| 514 | 'name', |
|---|
| 515 | 'uppercats', |
|---|
| 516 | 'global_rank', |
|---|
| 517 | ) |
|---|
| 518 | ) |
|---|
| 519 | ); |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | /** |
|---|
| 523 | * returns detailed information for an element (web service method) |
|---|
| 524 | */ |
|---|
| 525 | function ws_images_addComment($params, &$service) |
|---|
| 526 | { |
|---|
| 527 | if (!$service->isPost()) |
|---|
| 528 | { |
|---|
| 529 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 530 | } |
|---|
| 531 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 532 | $query = ' |
|---|
| 533 | SELECT DISTINCT image_id |
|---|
| 534 | FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id |
|---|
| 535 | WHERE commentable="true" |
|---|
| 536 | AND image_id='.$params['image_id']. |
|---|
| 537 | get_sql_condition_FandF( |
|---|
| 538 | array( |
|---|
| 539 | 'forbidden_categories' => 'id', |
|---|
| 540 | 'visible_categories' => 'id', |
|---|
| 541 | 'visible_images' => 'image_id' |
|---|
| 542 | ), |
|---|
| 543 | ' AND' |
|---|
| 544 | ); |
|---|
| 545 | if ( !pwg_db_num_rows( pwg_query( $query ) ) ) |
|---|
| 546 | { |
|---|
| 547 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | $comm = array( |
|---|
| 551 | 'author' => trim( stripslashes($params['author']) ), |
|---|
| 552 | 'content' => trim( stripslashes($params['content']) ), |
|---|
| 553 | 'image_id' => $params['image_id'], |
|---|
| 554 | ); |
|---|
| 555 | |
|---|
| 556 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
|---|
| 557 | |
|---|
| 558 | $comment_action = insert_user_comment( |
|---|
| 559 | $comm, $params['key'], $infos |
|---|
| 560 | ); |
|---|
| 561 | |
|---|
| 562 | switch ($comment_action) |
|---|
| 563 | { |
|---|
| 564 | case 'reject': |
|---|
| 565 | array_push($infos, l10n('comment_not_added') ); |
|---|
| 566 | return new PwgError(403, implode("\n", $infos) ); |
|---|
| 567 | case 'validate': |
|---|
| 568 | case 'moderate': |
|---|
| 569 | $ret = array( |
|---|
| 570 | 'id' => $comm['id'], |
|---|
| 571 | 'validation' => $comment_action=='validate', |
|---|
| 572 | ); |
|---|
| 573 | return new PwgNamedStruct( |
|---|
| 574 | 'comment', |
|---|
| 575 | $ret, |
|---|
| 576 | null, array() |
|---|
| 577 | ); |
|---|
| 578 | default: |
|---|
| 579 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | /** |
|---|
| 584 | * returns detailed information for an element (web service method) |
|---|
| 585 | */ |
|---|
| 586 | function ws_images_getInfo($params, &$service) |
|---|
| 587 | { |
|---|
| 588 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 589 | global $user, $conf; |
|---|
| 590 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 591 | if ( $params['image_id']<=0 ) |
|---|
| 592 | { |
|---|
| 593 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | $query=' |
|---|
| 597 | SELECT * FROM '.IMAGES_TABLE.' |
|---|
| 598 | WHERE id='.$params['image_id']. |
|---|
| 599 | get_sql_condition_FandF( |
|---|
| 600 | array('visible_images' => 'id'), |
|---|
| 601 | ' AND' |
|---|
| 602 | ).' |
|---|
| 603 | LIMIT 1'; |
|---|
| 604 | |
|---|
| 605 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 606 | if ($image_row==null) |
|---|
| 607 | { |
|---|
| 608 | return new PwgError(404, "image_id not found"); |
|---|
| 609 | } |
|---|
| 610 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
|---|
| 611 | |
|---|
| 612 | //-------------------------------------------------------- related categories |
|---|
| 613 | $query = ' |
|---|
| 614 | SELECT id, name, permalink, uppercats, global_rank, commentable |
|---|
| 615 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 616 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
|---|
| 617 | WHERE image_id = '.$image_row['id']. |
|---|
| 618 | get_sql_condition_FandF( |
|---|
| 619 | array( 'forbidden_categories' => 'category_id' ), |
|---|
| 620 | ' AND' |
|---|
| 621 | ).' |
|---|
| 622 | ;'; |
|---|
| 623 | $result = pwg_query($query); |
|---|
| 624 | $is_commentable = false; |
|---|
| 625 | $related_categories = array(); |
|---|
| 626 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 627 | { |
|---|
| 628 | if ($row['commentable']=='true') |
|---|
| 629 | { |
|---|
| 630 | $is_commentable = true; |
|---|
| 631 | } |
|---|
| 632 | unset($row['commentable']); |
|---|
| 633 | $row['url'] = make_index_url( |
|---|
| 634 | array( |
|---|
| 635 | 'category' => $row |
|---|
| 636 | ) |
|---|
| 637 | ); |
|---|
| 638 | |
|---|
| 639 | $row['page_url'] = make_picture_url( |
|---|
| 640 | array( |
|---|
| 641 | 'image_id' => $image_row['id'], |
|---|
| 642 | 'image_file' => $image_row['file'], |
|---|
| 643 | 'category' => $row |
|---|
| 644 | ) |
|---|
| 645 | ); |
|---|
| 646 | $row['id']=(int)$row['id']; |
|---|
| 647 | array_push($related_categories, $row); |
|---|
| 648 | } |
|---|
| 649 | usort($related_categories, 'global_rank_compare'); |
|---|
| 650 | if ( empty($related_categories) ) |
|---|
| 651 | { |
|---|
| 652 | return new PwgError(401, 'Access denied'); |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | //-------------------------------------------------------------- related tags |
|---|
| 656 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
|---|
| 657 | foreach( $related_tags as $i=>$tag) |
|---|
| 658 | { |
|---|
| 659 | $tag['url'] = make_index_url( |
|---|
| 660 | array( |
|---|
| 661 | 'tags' => array($tag) |
|---|
| 662 | ) |
|---|
| 663 | ); |
|---|
| 664 | $tag['page_url'] = make_picture_url( |
|---|
| 665 | array( |
|---|
| 666 | 'image_id' => $image_row['id'], |
|---|
| 667 | 'image_file' => $image_row['file'], |
|---|
| 668 | 'tags' => array($tag), |
|---|
| 669 | ) |
|---|
| 670 | ); |
|---|
| 671 | unset($tag['counter']); |
|---|
| 672 | $tag['id']=(int)$tag['id']; |
|---|
| 673 | $related_tags[$i]=$tag; |
|---|
| 674 | } |
|---|
| 675 | //------------------------------------------------------------- related rates |
|---|
| 676 | $query = ' |
|---|
| 677 | SELECT COUNT(rate) AS count |
|---|
| 678 | , ROUND(AVG(rate),2) AS average |
|---|
| 679 | , ROUND(STD(rate),2) AS stdev |
|---|
| 680 | FROM '.RATE_TABLE.' |
|---|
| 681 | WHERE element_id = '.$image_row['id'].' |
|---|
| 682 | ;'; |
|---|
| 683 | $rating = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 684 | $rating['count'] = (int)$rating['count']; |
|---|
| 685 | |
|---|
| 686 | //---------------------------------------------------------- related comments |
|---|
| 687 | $related_comments = array(); |
|---|
| 688 | |
|---|
| 689 | $where_comments = 'image_id = '.$image_row['id']; |
|---|
| 690 | if ( !is_admin() ) |
|---|
| 691 | { |
|---|
| 692 | $where_comments .= ' |
|---|
| 693 | AND validated="true"'; |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | $query = ' |
|---|
| 697 | SELECT COUNT(id) nb_comments |
|---|
| 698 | FROM '.COMMENTS_TABLE.' |
|---|
| 699 | WHERE '.$where_comments; |
|---|
| 700 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
|---|
| 701 | $nb_comments = (int)$nb_comments; |
|---|
| 702 | |
|---|
| 703 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
|---|
| 704 | { |
|---|
| 705 | $query = ' |
|---|
| 706 | SELECT id, date, author, content |
|---|
| 707 | FROM '.COMMENTS_TABLE.' |
|---|
| 708 | WHERE '.$where_comments.' |
|---|
| 709 | ORDER BY date |
|---|
| 710 | LIMIT '.(int)$params['comments_per_page']. |
|---|
| 711 | ' OFFSET '.(int)($params['comments_per_page']*$params['comments_page']); |
|---|
| 712 | |
|---|
| 713 | $result = pwg_query($query); |
|---|
| 714 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 715 | { |
|---|
| 716 | $row['id']=(int)$row['id']; |
|---|
| 717 | array_push($related_comments, $row); |
|---|
| 718 | } |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | $comment_post_data = null; |
|---|
| 722 | if ($is_commentable and |
|---|
| 723 | (!is_a_guest() |
|---|
| 724 | or (is_a_guest() and $conf['comments_forall'] ) |
|---|
| 725 | ) |
|---|
| 726 | ) |
|---|
| 727 | { |
|---|
| 728 | $comment_post_data['author'] = stripslashes($user['username']); |
|---|
| 729 | $comment_post_data['key'] = get_comment_post_key($params['image_id']); |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | $ret = $image_row; |
|---|
| 733 | foreach ( array('id','width','height','hit','filesize') as $k ) |
|---|
| 734 | { |
|---|
| 735 | if (isset($ret[$k])) |
|---|
| 736 | { |
|---|
| 737 | $ret[$k] = (int)$ret[$k]; |
|---|
| 738 | } |
|---|
| 739 | } |
|---|
| 740 | foreach ( array('path', 'storage_category_id') as $k ) |
|---|
| 741 | { |
|---|
| 742 | unset($ret[$k]); |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
|---|
| 746 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
|---|
| 747 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','name','page_url') ); |
|---|
| 748 | if ( isset($comment_post_data) ) |
|---|
| 749 | { |
|---|
| 750 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
|---|
| 751 | } |
|---|
| 752 | $ret['comments'] = array( |
|---|
| 753 | WS_XML_ATTRIBUTES => |
|---|
| 754 | array( |
|---|
| 755 | 'page' => $params['comments_page'], |
|---|
| 756 | 'per_page' => $params['comments_per_page'], |
|---|
| 757 | 'count' => count($related_comments), |
|---|
| 758 | 'nb_comments' => $nb_comments, |
|---|
| 759 | ), |
|---|
| 760 | WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') ) |
|---|
| 761 | ); |
|---|
| 762 | |
|---|
| 763 | return new PwgNamedStruct('image',$ret, null, array('name','comment') ); |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | /** |
|---|
| 768 | * rates the image_id in the parameter |
|---|
| 769 | */ |
|---|
| 770 | function ws_images_Rate($params, &$service) |
|---|
| 771 | { |
|---|
| 772 | $image_id = (int)$params['image_id']; |
|---|
| 773 | $query = ' |
|---|
| 774 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
|---|
| 775 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
|---|
| 776 | WHERE id='.$image_id |
|---|
| 777 | .get_sql_condition_FandF( |
|---|
| 778 | array( |
|---|
| 779 | 'forbidden_categories' => 'category_id', |
|---|
| 780 | 'forbidden_images' => 'id', |
|---|
| 781 | ), |
|---|
| 782 | ' AND' |
|---|
| 783 | ).' |
|---|
| 784 | LIMIT 1'; |
|---|
| 785 | if ( pwg_db_num_rows( pwg_query($query) )==0 ) |
|---|
| 786 | { |
|---|
| 787 | return new PwgError(404, "Invalid image_id or access denied" ); |
|---|
| 788 | } |
|---|
| 789 | $rate = (int)$params['rate']; |
|---|
| 790 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
|---|
| 791 | $res = rate_picture( $image_id, $rate ); |
|---|
| 792 | if ($res==false) |
|---|
| 793 | { |
|---|
| 794 | global $conf; |
|---|
| 795 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
|---|
| 796 | } |
|---|
| 797 | return $res; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | /** |
|---|
| 802 | * returns a list of elements corresponding to a query search |
|---|
| 803 | */ |
|---|
| 804 | function ws_images_search($params, &$service) |
|---|
| 805 | { |
|---|
| 806 | global $page; |
|---|
| 807 | $images = array(); |
|---|
| 808 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
|---|
| 809 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 810 | |
|---|
| 811 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
|---|
| 812 | $order_by = ws_std_image_sql_order($params, 'i.'); |
|---|
| 813 | |
|---|
| 814 | $super_order_by = false; |
|---|
| 815 | if ( !empty($order_by) ) |
|---|
| 816 | { |
|---|
| 817 | global $conf; |
|---|
| 818 | $conf['order_by'] = 'ORDER BY '.$order_by; |
|---|
| 819 | $super_order_by=true; // quick_search_result might be faster |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | $search_result = get_quick_search_results($params['query'], |
|---|
| 823 | $super_order_by, |
|---|
| 824 | implode(',', $where_clauses) |
|---|
| 825 | ); |
|---|
| 826 | |
|---|
| 827 | $image_ids = array_slice( |
|---|
| 828 | $search_result['items'], |
|---|
| 829 | $params['page']*$params['per_page'], |
|---|
| 830 | $params['per_page'] |
|---|
| 831 | ); |
|---|
| 832 | |
|---|
| 833 | if ( count($image_ids) ) |
|---|
| 834 | { |
|---|
| 835 | $query = ' |
|---|
| 836 | SELECT * FROM '.IMAGES_TABLE.' |
|---|
| 837 | WHERE id IN ('.implode(',', $image_ids).')'; |
|---|
| 838 | |
|---|
| 839 | $image_ids = array_flip($image_ids); |
|---|
| 840 | $result = pwg_query($query); |
|---|
| 841 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 842 | { |
|---|
| 843 | $image = array(); |
|---|
| 844 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
|---|
| 845 | { |
|---|
| 846 | if (isset($row[$k])) |
|---|
| 847 | { |
|---|
| 848 | $image[$k] = (int)$row[$k]; |
|---|
| 849 | } |
|---|
| 850 | } |
|---|
| 851 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 852 | { |
|---|
| 853 | $image[$k] = $row[$k]; |
|---|
| 854 | } |
|---|
| 855 | $image = array_merge( $image, ws_std_get_urls($row) ); |
|---|
| 856 | $images[$image_ids[$image['id']]] = $image; |
|---|
| 857 | } |
|---|
| 858 | ksort($images, SORT_NUMERIC); |
|---|
| 859 | $images = array_values($images); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | |
|---|
| 863 | return array( 'images' => |
|---|
| 864 | array ( |
|---|
| 865 | WS_XML_ATTRIBUTES => |
|---|
| 866 | array( |
|---|
| 867 | 'page' => $params['page'], |
|---|
| 868 | 'per_page' => $params['per_page'], |
|---|
| 869 | 'count' => count($images) |
|---|
| 870 | ), |
|---|
| 871 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 872 | ws_std_get_image_xml_attributes() ) |
|---|
| 873 | ) |
|---|
| 874 | ); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | function ws_images_setPrivacyLevel($params, &$service) |
|---|
| 878 | { |
|---|
| 879 | if (!is_admin() || is_adviser() ) |
|---|
| 880 | { |
|---|
| 881 | return new PwgError(401, 'Access denied'); |
|---|
| 882 | } |
|---|
| 883 | if (!$service->isPost()) |
|---|
| 884 | { |
|---|
| 885 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 886 | } |
|---|
| 887 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
|---|
| 888 | if ( empty($params['image_id']) ) |
|---|
| 889 | { |
|---|
| 890 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 891 | } |
|---|
| 892 | global $conf; |
|---|
| 893 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
|---|
| 894 | { |
|---|
| 895 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | $query = ' |
|---|
| 899 | UPDATE '.IMAGES_TABLE.' |
|---|
| 900 | SET level='.(int)$params['level'].' |
|---|
| 901 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
|---|
| 902 | $result = pwg_query($query); |
|---|
| 903 | $affected_rows = pwg_db_changes(); |
|---|
| 904 | if ($affected_rows) |
|---|
| 905 | { |
|---|
| 906 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 907 | invalidate_user_cache(); |
|---|
| 908 | } |
|---|
| 909 | return $affected_rows; |
|---|
| 910 | } |
|---|
| 911 | |
|---|
| 912 | function ws_images_add_chunk($params, &$service) |
|---|
| 913 | { |
|---|
| 914 | ws_logfile('[ws_images_add_chunk] welcome'); |
|---|
| 915 | // data |
|---|
| 916 | // original_sum |
|---|
| 917 | // type {thumb, file, high} |
|---|
| 918 | // position |
|---|
| 919 | |
|---|
| 920 | if (!is_admin() || is_adviser() ) |
|---|
| 921 | { |
|---|
| 922 | return new PwgError(401, 'Access denied'); |
|---|
| 923 | } |
|---|
| 924 | |
|---|
| 925 | if (!$service->isPost()) |
|---|
| 926 | { |
|---|
| 927 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 928 | } |
|---|
| 929 | |
|---|
| 930 | foreach ($params as $param_key => $param_value) { |
|---|
| 931 | if ('data' == $param_key) { |
|---|
| 932 | continue; |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | ws_logfile( |
|---|
| 936 | sprintf( |
|---|
| 937 | '[ws_images_add_chunk] input param "%s" : "%s"', |
|---|
| 938 | $param_key, |
|---|
| 939 | is_null($param_value) ? 'NULL' : $param_value |
|---|
| 940 | ) |
|---|
| 941 | ); |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | $upload_dir = PHPWG_ROOT_PATH.'upload/buffer'; |
|---|
| 945 | |
|---|
| 946 | // create the upload directory tree if not exists |
|---|
| 947 | if (!is_dir($upload_dir)) { |
|---|
| 948 | umask(0000); |
|---|
| 949 | $recursive = true; |
|---|
| 950 | if (!@mkdir($upload_dir, 0777, $recursive)) |
|---|
| 951 | { |
|---|
| 952 | return new PwgError(500, 'error during buffer directory creation'); |
|---|
| 953 | } |
|---|
| 954 | } |
|---|
| 955 | |
|---|
| 956 | if (!is_writable($upload_dir)) |
|---|
| 957 | { |
|---|
| 958 | // last chance to make the directory writable |
|---|
| 959 | @chmod($upload_dir, 0777); |
|---|
| 960 | |
|---|
| 961 | if (!is_writable($upload_dir)) |
|---|
| 962 | { |
|---|
| 963 | return new PwgError(500, 'buffer directory has no write access'); |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | secure_directory($upload_dir); |
|---|
| 968 | |
|---|
| 969 | $filename = sprintf( |
|---|
| 970 | '%s-%s-%05u.block', |
|---|
| 971 | $params['original_sum'], |
|---|
| 972 | $params['type'], |
|---|
| 973 | $params['position'] |
|---|
| 974 | ); |
|---|
| 975 | |
|---|
| 976 | ws_logfile('[ws_images_add_chunk] data length : '.strlen($params['data'])); |
|---|
| 977 | |
|---|
| 978 | $bytes_written = file_put_contents( |
|---|
| 979 | $upload_dir.'/'.$filename, |
|---|
| 980 | base64_decode($params['data']) |
|---|
| 981 | ); |
|---|
| 982 | |
|---|
| 983 | if (false === $bytes_written) { |
|---|
| 984 | return new PwgError( |
|---|
| 985 | 500, |
|---|
| 986 | 'an error has occured while writting chunk '.$params['position'].' for '.$params['type'] |
|---|
| 987 | ); |
|---|
| 988 | } |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | function merge_chunks($output_filepath, $original_sum, $type) |
|---|
| 992 | { |
|---|
| 993 | ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath); |
|---|
| 994 | |
|---|
| 995 | if (is_file($output_filepath)) |
|---|
| 996 | { |
|---|
| 997 | unlink($output_filepath); |
|---|
| 998 | |
|---|
| 999 | if (is_file($output_filepath)) |
|---|
| 1000 | { |
|---|
| 1001 | new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath); |
|---|
| 1002 | exit(); |
|---|
| 1003 | } |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | $upload_dir = PHPWG_ROOT_PATH.'upload/buffer'; |
|---|
| 1007 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
|---|
| 1008 | $chunks = array(); |
|---|
| 1009 | |
|---|
| 1010 | if ($handle = opendir($upload_dir)) |
|---|
| 1011 | { |
|---|
| 1012 | while (false !== ($file = readdir($handle))) |
|---|
| 1013 | { |
|---|
| 1014 | if (preg_match($pattern, $file)) |
|---|
| 1015 | { |
|---|
| 1016 | ws_logfile($file); |
|---|
| 1017 | array_push($chunks, $upload_dir.'/'.$file); |
|---|
| 1018 | } |
|---|
| 1019 | } |
|---|
| 1020 | closedir($handle); |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | sort($chunks); |
|---|
| 1024 | |
|---|
| 1025 | if (function_exists('memory_get_usage')) { |
|---|
| 1026 | ws_logfile('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage()); |
|---|
| 1027 | } |
|---|
| 1028 | |
|---|
| 1029 | $i = 0; |
|---|
| 1030 | |
|---|
| 1031 | foreach ($chunks as $chunk) |
|---|
| 1032 | { |
|---|
| 1033 | $string = file_get_contents($chunk); |
|---|
| 1034 | |
|---|
| 1035 | if (function_exists('memory_get_usage')) { |
|---|
| 1036 | ws_logfile('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage()); |
|---|
| 1037 | } |
|---|
| 1038 | |
|---|
| 1039 | if (!file_put_contents($output_filepath, $string, FILE_APPEND)) |
|---|
| 1040 | { |
|---|
| 1041 | new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath); |
|---|
| 1042 | exit(); |
|---|
| 1043 | } |
|---|
| 1044 | |
|---|
| 1045 | unlink($chunk); |
|---|
| 1046 | } |
|---|
| 1047 | |
|---|
| 1048 | if (function_exists('memory_get_usage')) { |
|---|
| 1049 | ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage()); |
|---|
| 1050 | } |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | /* |
|---|
| 1054 | * The $file_path must be the path of the basic "web sized" photo |
|---|
| 1055 | * The $type value will automatically modify the $file_path to the corresponding file |
|---|
| 1056 | */ |
|---|
| 1057 | function add_file($file_path, $type, $original_sum, $file_sum) |
|---|
| 1058 | { |
|---|
| 1059 | $file_path = file_path_for_type($file_path, $type); |
|---|
| 1060 | |
|---|
| 1061 | $upload_dir = dirname($file_path); |
|---|
| 1062 | if (substr(PHP_OS, 0, 3) == 'WIN') |
|---|
| 1063 | { |
|---|
| 1064 | $upload_dir = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir); |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | ws_logfile('[add_file] file_path : '.$file_path); |
|---|
| 1068 | ws_logfile('[add_file] upload_dir : '.$upload_dir); |
|---|
| 1069 | |
|---|
| 1070 | if (!is_dir($upload_dir)) { |
|---|
| 1071 | umask(0000); |
|---|
| 1072 | $recursive = true; |
|---|
| 1073 | if (!@mkdir($upload_dir, 0777, $recursive)) |
|---|
| 1074 | { |
|---|
| 1075 | new PwgError(500, '[add_file] error during '.$type.' directory creation'); |
|---|
| 1076 | exit(); |
|---|
| 1077 | } |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | if (!is_writable($upload_dir)) |
|---|
| 1081 | { |
|---|
| 1082 | // last chance to make the directory writable |
|---|
| 1083 | @chmod($upload_dir, 0777); |
|---|
| 1084 | |
|---|
| 1085 | if (!is_writable($upload_dir)) |
|---|
| 1086 | { |
|---|
| 1087 | new PwgError(500, '[add_file] '.$type.' directory has no write access'); |
|---|
| 1088 | exit(); |
|---|
| 1089 | } |
|---|
| 1090 | } |
|---|
| 1091 | |
|---|
| 1092 | secure_directory($upload_dir); |
|---|
| 1093 | |
|---|
| 1094 | // merge the thumbnail |
|---|
| 1095 | merge_chunks($file_path, $original_sum, $type); |
|---|
| 1096 | chmod($file_path, 0644); |
|---|
| 1097 | |
|---|
| 1098 | // check dumped thumbnail md5 |
|---|
| 1099 | $dumped_md5 = md5_file($file_path); |
|---|
| 1100 | if ($dumped_md5 != $file_sum) { |
|---|
| 1101 | new PwgError(500, '[add_file] '.$type.' transfer failed'); |
|---|
| 1102 | exit(); |
|---|
| 1103 | } |
|---|
| 1104 | |
|---|
| 1105 | list($width, $height) = getimagesize($file_path); |
|---|
| 1106 | $filesize = floor(filesize($file_path)/1024); |
|---|
| 1107 | |
|---|
| 1108 | return array( |
|---|
| 1109 | 'width' => $width, |
|---|
| 1110 | 'height' => $height, |
|---|
| 1111 | 'filesize' => $filesize, |
|---|
| 1112 | ); |
|---|
| 1113 | } |
|---|
| 1114 | |
|---|
| 1115 | function ws_images_addFile($params, &$service) |
|---|
| 1116 | { |
|---|
| 1117 | // image_id |
|---|
| 1118 | // type {thumb, file, high} |
|---|
| 1119 | // sum |
|---|
| 1120 | |
|---|
| 1121 | global $conf; |
|---|
| 1122 | if (!is_admin() || is_adviser() ) |
|---|
| 1123 | { |
|---|
| 1124 | return new PwgError(401, 'Access denied'); |
|---|
| 1125 | } |
|---|
| 1126 | |
|---|
| 1127 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 1128 | if ($params['image_id'] <= 0) |
|---|
| 1129 | { |
|---|
| 1130 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | // |
|---|
| 1134 | // what is the path? |
|---|
| 1135 | // |
|---|
| 1136 | $query = ' |
|---|
| 1137 | SELECT |
|---|
| 1138 | path, |
|---|
| 1139 | md5sum |
|---|
| 1140 | FROM '.IMAGES_TABLE.' |
|---|
| 1141 | WHERE id = '.$params['image_id'].' |
|---|
| 1142 | ;'; |
|---|
| 1143 | list($file_path, $original_sum) = mysql_fetch_row(pwg_query($query)); |
|---|
| 1144 | |
|---|
| 1145 | // TODO only files added with web API can be updated with web API |
|---|
| 1146 | |
|---|
| 1147 | // |
|---|
| 1148 | // makes sure directories are there and call the merge_chunks |
|---|
| 1149 | // |
|---|
| 1150 | $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); |
|---|
| 1151 | |
|---|
| 1152 | // |
|---|
| 1153 | // update basic metadata from file |
|---|
| 1154 | // |
|---|
| 1155 | $update = array(); |
|---|
| 1156 | |
|---|
| 1157 | if ('high' == $params['type']) |
|---|
| 1158 | { |
|---|
| 1159 | $update['high_filesize'] = $infos['filesize']; |
|---|
| 1160 | $update['has_high'] = 'true'; |
|---|
| 1161 | } |
|---|
| 1162 | |
|---|
| 1163 | if ('file' == $params['type']) |
|---|
| 1164 | { |
|---|
| 1165 | $update['filesize'] = $infos['filesize']; |
|---|
| 1166 | $update['width'] = $infos['width']; |
|---|
| 1167 | $update['height'] = $infos['height']; |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | // we may have nothing to update at database level, for example with a |
|---|
| 1171 | // thumbnail update |
|---|
| 1172 | if (count($update) > 0) |
|---|
| 1173 | { |
|---|
| 1174 | $update['id'] = $params['image_id']; |
|---|
| 1175 | |
|---|
| 1176 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1177 | mass_updates( |
|---|
| 1178 | IMAGES_TABLE, |
|---|
| 1179 | array( |
|---|
| 1180 | 'primary' => array('id'), |
|---|
| 1181 | 'update' => array_diff(array_keys($update), array('id')) |
|---|
| 1182 | ), |
|---|
| 1183 | array($update) |
|---|
| 1184 | ); |
|---|
| 1185 | } |
|---|
| 1186 | } |
|---|
| 1187 | |
|---|
| 1188 | function ws_images_add($params, &$service) |
|---|
| 1189 | { |
|---|
| 1190 | global $conf; |
|---|
| 1191 | if (!is_admin() || is_adviser() ) |
|---|
| 1192 | { |
|---|
| 1193 | return new PwgError(401, 'Access denied'); |
|---|
| 1194 | } |
|---|
| 1195 | |
|---|
| 1196 | foreach ($params as $param_key => $param_value) { |
|---|
| 1197 | ws_logfile( |
|---|
| 1198 | sprintf( |
|---|
| 1199 | '[pwg.images.add] input param "%s" : "%s"', |
|---|
| 1200 | $param_key, |
|---|
| 1201 | is_null($param_value) ? 'NULL' : $param_value |
|---|
| 1202 | ) |
|---|
| 1203 | ); |
|---|
| 1204 | } |
|---|
| 1205 | |
|---|
| 1206 | // does the image already exists ? |
|---|
| 1207 | $query = ' |
|---|
| 1208 | SELECT |
|---|
| 1209 | COUNT(*) AS counter |
|---|
| 1210 | FROM '.IMAGES_TABLE.' |
|---|
| 1211 | WHERE md5sum = \''.$params['original_sum'].'\' |
|---|
| 1212 | ;'; |
|---|
| 1213 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
|---|
| 1214 | if ($counter != 0) { |
|---|
| 1215 | return new PwgError(500, 'file already exists'); |
|---|
| 1216 | } |
|---|
| 1217 | |
|---|
| 1218 | // current date |
|---|
| 1219 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
|---|
| 1220 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
|---|
| 1221 | |
|---|
| 1222 | // upload directory hierarchy |
|---|
| 1223 | $upload_dir = sprintf( |
|---|
| 1224 | PHPWG_ROOT_PATH.'upload/%s/%s/%s', |
|---|
| 1225 | $year, |
|---|
| 1226 | $month, |
|---|
| 1227 | $day |
|---|
| 1228 | ); |
|---|
| 1229 | |
|---|
| 1230 | // compute file path |
|---|
| 1231 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
|---|
| 1232 | $random_string = substr($params['file_sum'], 0, 8); |
|---|
| 1233 | $filename_wo_ext = $date_string.'-'.$random_string; |
|---|
| 1234 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
|---|
| 1235 | |
|---|
| 1236 | // add files |
|---|
| 1237 | $file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']); |
|---|
| 1238 | $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']); |
|---|
| 1239 | |
|---|
| 1240 | if (isset($params['high_sum'])) |
|---|
| 1241 | { |
|---|
| 1242 | $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']); |
|---|
| 1243 | } |
|---|
| 1244 | |
|---|
| 1245 | // database registration |
|---|
| 1246 | $insert = array( |
|---|
| 1247 | 'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg', |
|---|
| 1248 | 'date_available' => $dbnow, |
|---|
| 1249 | 'tn_ext' => 'jpg', |
|---|
| 1250 | 'name' => $params['name'], |
|---|
| 1251 | 'path' => $file_path, |
|---|
| 1252 | 'filesize' => $file_infos['filesize'], |
|---|
| 1253 | 'width' => $file_infos['width'], |
|---|
| 1254 | 'height' => $file_infos['height'], |
|---|
| 1255 | 'md5sum' => $params['original_sum'], |
|---|
| 1256 | ); |
|---|
| 1257 | |
|---|
| 1258 | $info_columns = array( |
|---|
| 1259 | 'name', |
|---|
| 1260 | 'author', |
|---|
| 1261 | 'comment', |
|---|
| 1262 | 'level', |
|---|
| 1263 | 'date_creation', |
|---|
| 1264 | ); |
|---|
| 1265 | |
|---|
| 1266 | foreach ($info_columns as $key) |
|---|
| 1267 | { |
|---|
| 1268 | if (isset($params[$key])) |
|---|
| 1269 | { |
|---|
| 1270 | $insert[$key] = $params[$key]; |
|---|
| 1271 | } |
|---|
| 1272 | } |
|---|
| 1273 | |
|---|
| 1274 | if (isset($params['high_sum'])) |
|---|
| 1275 | { |
|---|
| 1276 | $insert['has_high'] = 'true'; |
|---|
| 1277 | $insert['high_filesize'] = $high_infos['filesize']; |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1281 | mass_inserts( |
|---|
| 1282 | IMAGES_TABLE, |
|---|
| 1283 | array_keys($insert), |
|---|
| 1284 | array($insert) |
|---|
| 1285 | ); |
|---|
| 1286 | |
|---|
| 1287 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
|---|
| 1288 | |
|---|
| 1289 | // let's add links between the image and the categories |
|---|
| 1290 | if (isset($params['categories'])) |
|---|
| 1291 | { |
|---|
| 1292 | ws_add_image_category_relations($image_id, $params['categories']); |
|---|
| 1293 | } |
|---|
| 1294 | |
|---|
| 1295 | // and now, let's create tag associations |
|---|
| 1296 | if (isset($params['tag_ids']) and !empty($params['tag_ids'])) |
|---|
| 1297 | { |
|---|
| 1298 | set_tags( |
|---|
| 1299 | explode(',', $params['tag_ids']), |
|---|
| 1300 | $image_id |
|---|
| 1301 | ); |
|---|
| 1302 | } |
|---|
| 1303 | |
|---|
| 1304 | // update metadata from the uploaded file (exif/iptc) |
|---|
| 1305 | require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
|---|
| 1306 | update_metadata(array($image_id=>$file_path)); |
|---|
| 1307 | |
|---|
| 1308 | invalidate_user_cache(); |
|---|
| 1309 | } |
|---|
| 1310 | |
|---|
| 1311 | /** |
|---|
| 1312 | * perform a login (web service method) |
|---|
| 1313 | */ |
|---|
| 1314 | function ws_session_login($params, &$service) |
|---|
| 1315 | { |
|---|
| 1316 | global $conf; |
|---|
| 1317 | |
|---|
| 1318 | if (!$service->isPost()) |
|---|
| 1319 | { |
|---|
| 1320 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 1321 | } |
|---|
| 1322 | if (try_log_user($params['username'], $params['password'],false)) |
|---|
| 1323 | { |
|---|
| 1324 | return true; |
|---|
| 1325 | } |
|---|
| 1326 | return new PwgError(999, 'Invalid username/password'); |
|---|
| 1327 | } |
|---|
| 1328 | |
|---|
| 1329 | |
|---|
| 1330 | /** |
|---|
| 1331 | * performs a logout (web service method) |
|---|
| 1332 | */ |
|---|
| 1333 | function ws_session_logout($params, &$service) |
|---|
| 1334 | { |
|---|
| 1335 | if (!is_a_guest()) |
|---|
| 1336 | { |
|---|
| 1337 | logout_user(); |
|---|
| 1338 | } |
|---|
| 1339 | return true; |
|---|
| 1340 | } |
|---|
| 1341 | |
|---|
| 1342 | function ws_session_getStatus($params, &$service) |
|---|
| 1343 | { |
|---|
| 1344 | global $user; |
|---|
| 1345 | $res = array(); |
|---|
| 1346 | $res['username'] = is_a_guest() ? 'guest' : stripslashes($user['username']); |
|---|
| 1347 | foreach ( array('status', 'template', 'theme', 'language') as $k ) |
|---|
| 1348 | { |
|---|
| 1349 | $res[$k] = $user[$k]; |
|---|
| 1350 | } |
|---|
| 1351 | $res['charset'] = get_pwg_charset(); |
|---|
| 1352 | return $res; |
|---|
| 1353 | } |
|---|
| 1354 | |
|---|
| 1355 | |
|---|
| 1356 | /** |
|---|
| 1357 | * returns a list of tags (web service method) |
|---|
| 1358 | */ |
|---|
| 1359 | function ws_tags_getList($params, &$service) |
|---|
| 1360 | { |
|---|
| 1361 | $tags = get_available_tags(); |
|---|
| 1362 | if ($params['sort_by_counter']) |
|---|
| 1363 | { |
|---|
| 1364 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
|---|
| 1365 | } |
|---|
| 1366 | else |
|---|
| 1367 | { |
|---|
| 1368 | usort($tags, 'tag_alpha_compare'); |
|---|
| 1369 | } |
|---|
| 1370 | for ($i=0; $i<count($tags); $i++) |
|---|
| 1371 | { |
|---|
| 1372 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
|---|
| 1373 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
|---|
| 1374 | $tags[$i]['url'] = make_index_url( |
|---|
| 1375 | array( |
|---|
| 1376 | 'section'=>'tags', |
|---|
| 1377 | 'tags'=>array($tags[$i]) |
|---|
| 1378 | ) |
|---|
| 1379 | ); |
|---|
| 1380 | } |
|---|
| 1381 | return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'name', 'counter' )) ); |
|---|
| 1382 | } |
|---|
| 1383 | |
|---|
| 1384 | /** |
|---|
| 1385 | * returns the list of tags as you can see them in administration (web |
|---|
| 1386 | * service method). |
|---|
| 1387 | * |
|---|
| 1388 | * Only admin can run this method and permissions are not taken into |
|---|
| 1389 | * account. |
|---|
| 1390 | */ |
|---|
| 1391 | function ws_tags_getAdminList($params, &$service) |
|---|
| 1392 | { |
|---|
| 1393 | if (!is_admin()) |
|---|
| 1394 | { |
|---|
| 1395 | return new PwgError(401, 'Access denied'); |
|---|
| 1396 | } |
|---|
| 1397 | |
|---|
| 1398 | $tags = get_all_tags(); |
|---|
| 1399 | return array( |
|---|
| 1400 | 'tags' => new PwgNamedArray( |
|---|
| 1401 | $tags, |
|---|
| 1402 | 'tag', |
|---|
| 1403 | array( |
|---|
| 1404 | 'name', |
|---|
| 1405 | 'id', |
|---|
| 1406 | 'url_name', |
|---|
| 1407 | ) |
|---|
| 1408 | ) |
|---|
| 1409 | ); |
|---|
| 1410 | } |
|---|
| 1411 | |
|---|
| 1412 | /** |
|---|
| 1413 | * returns a list of images for tags (web service method) |
|---|
| 1414 | */ |
|---|
| 1415 | function ws_tags_getImages($params, &$service) |
|---|
| 1416 | { |
|---|
| 1417 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 1418 | global $conf; |
|---|
| 1419 | |
|---|
| 1420 | // first build all the tag_ids we are interested in |
|---|
| 1421 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
|---|
| 1422 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
|---|
| 1423 | $tags_by_id = array(); |
|---|
| 1424 | foreach( $tags as $tag ) |
|---|
| 1425 | { |
|---|
| 1426 | $tags['id'] = (int)$tag['id']; |
|---|
| 1427 | $tags_by_id[ $tag['id'] ] = $tag; |
|---|
| 1428 | } |
|---|
| 1429 | unset($tags); |
|---|
| 1430 | $tag_ids = array_keys($tags_by_id); |
|---|
| 1431 | |
|---|
| 1432 | |
|---|
| 1433 | $image_ids = array(); |
|---|
| 1434 | $image_tag_map = array(); |
|---|
| 1435 | |
|---|
| 1436 | if ( !empty($tag_ids) ) |
|---|
| 1437 | { // build list of image ids with associated tags per image |
|---|
| 1438 | if ($params['tag_mode_and']) |
|---|
| 1439 | { |
|---|
| 1440 | $image_ids = get_image_ids_for_tags( $tag_ids ); |
|---|
| 1441 | } |
|---|
| 1442 | else |
|---|
| 1443 | { |
|---|
| 1444 | $query = ' |
|---|
| 1445 | SELECT image_id, GROUP_CONCAT(tag_id) tag_ids |
|---|
| 1446 | FROM '.IMAGE_TAG_TABLE.' |
|---|
| 1447 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
|---|
| 1448 | GROUP BY image_id'; |
|---|
| 1449 | $result = pwg_query($query); |
|---|
| 1450 | while ( $row=pwg_db_fetch_assoc($result) ) |
|---|
| 1451 | { |
|---|
| 1452 | $row['image_id'] = (int)$row['image_id']; |
|---|
| 1453 | array_push( $image_ids, $row['image_id'] ); |
|---|
| 1454 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
|---|
| 1455 | } |
|---|
| 1456 | } |
|---|
| 1457 | } |
|---|
| 1458 | |
|---|
| 1459 | $images = array(); |
|---|
| 1460 | if ( !empty($image_ids)) |
|---|
| 1461 | { |
|---|
| 1462 | $where_clauses = ws_std_image_sql_filter($params); |
|---|
| 1463 | $where_clauses[] = get_sql_condition_FandF( |
|---|
| 1464 | array |
|---|
| 1465 | ( |
|---|
| 1466 | 'forbidden_categories' => 'category_id', |
|---|
| 1467 | 'visible_categories' => 'category_id', |
|---|
| 1468 | 'visible_images' => 'i.id' |
|---|
| 1469 | ), |
|---|
| 1470 | '', true |
|---|
| 1471 | ); |
|---|
| 1472 | $where_clauses[] = 'id IN ('.implode(',',$image_ids).')'; |
|---|
| 1473 | |
|---|
| 1474 | $order_by = ws_std_image_sql_order($params); |
|---|
| 1475 | if (empty($order_by)) |
|---|
| 1476 | { |
|---|
| 1477 | $order_by = $conf['order_by']; |
|---|
| 1478 | } |
|---|
| 1479 | else |
|---|
| 1480 | { |
|---|
| 1481 | $order_by = 'ORDER BY '.$order_by; |
|---|
| 1482 | } |
|---|
| 1483 | |
|---|
| 1484 | $query = ' |
|---|
| 1485 | SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i |
|---|
| 1486 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
|---|
| 1487 | WHERE '. implode(' |
|---|
| 1488 | AND ', $where_clauses).' |
|---|
| 1489 | '.$order_by.' |
|---|
| 1490 | LIMIT '.(int)$params['per_page'].' OFFSET '.(int)($params['per_page']*$params['page']); |
|---|
| 1491 | |
|---|
| 1492 | $result = pwg_query($query); |
|---|
| 1493 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 1494 | { |
|---|
| 1495 | $image = array(); |
|---|
| 1496 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
|---|
| 1497 | { |
|---|
| 1498 | if (isset($row[$k])) |
|---|
| 1499 | { |
|---|
| 1500 | $image[$k] = (int)$row[$k]; |
|---|
| 1501 | } |
|---|
| 1502 | } |
|---|
| 1503 | foreach ( array('file', 'name', 'comment') as $k ) |
|---|
| 1504 | { |
|---|
| 1505 | $image[$k] = $row[$k]; |
|---|
| 1506 | } |
|---|
| 1507 | $image = array_merge( $image, ws_std_get_urls($row) ); |
|---|
| 1508 | |
|---|
| 1509 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
|---|
| 1510 | $image_tags = array(); |
|---|
| 1511 | foreach ($image_tag_ids as $tag_id) |
|---|
| 1512 | { |
|---|
| 1513 | $url = make_index_url( |
|---|
| 1514 | array( |
|---|
| 1515 | 'section'=>'tags', |
|---|
| 1516 | 'tags'=> array($tags_by_id[$tag_id]) |
|---|
| 1517 | ) |
|---|
| 1518 | ); |
|---|
| 1519 | $page_url = make_picture_url( |
|---|
| 1520 | array( |
|---|
| 1521 | 'section'=>'tags', |
|---|
| 1522 | 'tags'=> array($tags_by_id[$tag_id]), |
|---|
| 1523 | 'image_id' => $row['id'], |
|---|
| 1524 | 'image_file' => $row['file'], |
|---|
| 1525 | ) |
|---|
| 1526 | ); |
|---|
| 1527 | array_push($image_tags, array( |
|---|
| 1528 | 'id' => (int)$tag_id, |
|---|
| 1529 | 'url' => $url, |
|---|
| 1530 | 'page_url' => $page_url, |
|---|
| 1531 | ) |
|---|
| 1532 | ); |
|---|
| 1533 | } |
|---|
| 1534 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', |
|---|
| 1535 | array('id','url_name','url','page_url') |
|---|
| 1536 | ); |
|---|
| 1537 | array_push($images, $image); |
|---|
| 1538 | } |
|---|
| 1539 | } |
|---|
| 1540 | |
|---|
| 1541 | return array( 'images' => |
|---|
| 1542 | array ( |
|---|
| 1543 | WS_XML_ATTRIBUTES => |
|---|
| 1544 | array( |
|---|
| 1545 | 'page' => $params['page'], |
|---|
| 1546 | 'per_page' => $params['per_page'], |
|---|
| 1547 | 'count' => count($images) |
|---|
| 1548 | ), |
|---|
| 1549 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
|---|
| 1550 | ws_std_get_image_xml_attributes() ) |
|---|
| 1551 | ) |
|---|
| 1552 | ); |
|---|
| 1553 | } |
|---|
| 1554 | |
|---|
| 1555 | function ws_categories_add($params, &$service) |
|---|
| 1556 | { |
|---|
| 1557 | if (!is_admin() or is_adviser()) |
|---|
| 1558 | { |
|---|
| 1559 | return new PwgError(401, 'Access denied'); |
|---|
| 1560 | } |
|---|
| 1561 | |
|---|
| 1562 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1563 | |
|---|
| 1564 | $creation_output = create_virtual_category( |
|---|
| 1565 | $params['name'], |
|---|
| 1566 | $params['parent'] |
|---|
| 1567 | ); |
|---|
| 1568 | |
|---|
| 1569 | if (isset($creation_output['error'])) |
|---|
| 1570 | { |
|---|
| 1571 | return new PwgError(500, $creation_output['error']); |
|---|
| 1572 | } |
|---|
| 1573 | |
|---|
| 1574 | invalidate_user_cache(); |
|---|
| 1575 | |
|---|
| 1576 | return $creation_output; |
|---|
| 1577 | } |
|---|
| 1578 | |
|---|
| 1579 | function ws_tags_add($params, &$service) |
|---|
| 1580 | { |
|---|
| 1581 | if (!is_admin() or is_adviser()) |
|---|
| 1582 | { |
|---|
| 1583 | return new PwgError(401, 'Access denied'); |
|---|
| 1584 | } |
|---|
| 1585 | |
|---|
| 1586 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1587 | |
|---|
| 1588 | $creation_output = create_tag($params['name']); |
|---|
| 1589 | |
|---|
| 1590 | if (isset($creation_output['error'])) |
|---|
| 1591 | { |
|---|
| 1592 | return new PwgError(500, $creation_output['error']); |
|---|
| 1593 | } |
|---|
| 1594 | |
|---|
| 1595 | return $creation_output; |
|---|
| 1596 | } |
|---|
| 1597 | |
|---|
| 1598 | function ws_images_exist($params, &$service) |
|---|
| 1599 | { |
|---|
| 1600 | if (!is_admin() or is_adviser()) |
|---|
| 1601 | { |
|---|
| 1602 | return new PwgError(401, 'Access denied'); |
|---|
| 1603 | } |
|---|
| 1604 | |
|---|
| 1605 | // search among photos the list of photos already added, based on md5sum |
|---|
| 1606 | // list |
|---|
| 1607 | $md5sums = preg_split( |
|---|
| 1608 | '/[\s,;\|]/', |
|---|
| 1609 | $params['md5sum_list'], |
|---|
| 1610 | -1, |
|---|
| 1611 | PREG_SPLIT_NO_EMPTY |
|---|
| 1612 | ); |
|---|
| 1613 | |
|---|
| 1614 | $query = ' |
|---|
| 1615 | SELECT |
|---|
| 1616 | id, |
|---|
| 1617 | md5sum |
|---|
| 1618 | FROM '.IMAGES_TABLE.' |
|---|
| 1619 | WHERE md5sum IN (\''.implode("','", $md5sums).'\') |
|---|
| 1620 | ;'; |
|---|
| 1621 | $id_of_md5 = simple_hash_from_query($query, 'md5sum', 'id'); |
|---|
| 1622 | |
|---|
| 1623 | $result = array(); |
|---|
| 1624 | |
|---|
| 1625 | foreach ($md5sums as $md5sum) |
|---|
| 1626 | { |
|---|
| 1627 | $result[$md5sum] = null; |
|---|
| 1628 | if (isset($id_of_md5[$md5sum])) |
|---|
| 1629 | { |
|---|
| 1630 | $result[$md5sum] = $id_of_md5[$md5sum]; |
|---|
| 1631 | } |
|---|
| 1632 | } |
|---|
| 1633 | |
|---|
| 1634 | return $result; |
|---|
| 1635 | } |
|---|
| 1636 | |
|---|
| 1637 | function ws_images_checkFiles($params, &$service) |
|---|
| 1638 | { |
|---|
| 1639 | if (!is_admin() or is_adviser()) |
|---|
| 1640 | { |
|---|
| 1641 | return new PwgError(401, 'Access denied'); |
|---|
| 1642 | } |
|---|
| 1643 | |
|---|
| 1644 | // input parameters |
|---|
| 1645 | // |
|---|
| 1646 | // image_id |
|---|
| 1647 | // thumbnail_sum |
|---|
| 1648 | // file_sum |
|---|
| 1649 | // high_sum |
|---|
| 1650 | |
|---|
| 1651 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 1652 | if ($params['image_id'] <= 0) |
|---|
| 1653 | { |
|---|
| 1654 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 1655 | } |
|---|
| 1656 | |
|---|
| 1657 | $query = ' |
|---|
| 1658 | SELECT |
|---|
| 1659 | path |
|---|
| 1660 | FROM '.IMAGES_TABLE.' |
|---|
| 1661 | WHERE id = '.$params['image_id'].' |
|---|
| 1662 | ;'; |
|---|
| 1663 | $result = pwg_query($query); |
|---|
| 1664 | if (mysql_num_rows($result) == 0) { |
|---|
| 1665 | return new PwgError(404, "image_id not found"); |
|---|
| 1666 | } |
|---|
| 1667 | list($path) = mysql_fetch_row($result); |
|---|
| 1668 | |
|---|
| 1669 | $ret = array(); |
|---|
| 1670 | |
|---|
| 1671 | foreach (array('thumb', 'file', 'high') as $type) { |
|---|
| 1672 | $param_name = $type; |
|---|
| 1673 | if ('thumb' == $type) { |
|---|
| 1674 | $param_name = 'thumbnail'; |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | if (isset($params[$param_name.'_sum'])) { |
|---|
| 1678 | $type_path = file_path_for_type($path, $type); |
|---|
| 1679 | if (!is_file($type_path)) { |
|---|
| 1680 | $ret[$param_name] = 'missing'; |
|---|
| 1681 | } |
|---|
| 1682 | else { |
|---|
| 1683 | if (md5_file($type_path) != $params[$param_name.'_sum']) { |
|---|
| 1684 | $ret[$param_name] = 'differs'; |
|---|
| 1685 | } |
|---|
| 1686 | else { |
|---|
| 1687 | $ret[$param_name] = 'equals'; |
|---|
| 1688 | } |
|---|
| 1689 | } |
|---|
| 1690 | } |
|---|
| 1691 | } |
|---|
| 1692 | |
|---|
| 1693 | return $ret; |
|---|
| 1694 | } |
|---|
| 1695 | |
|---|
| 1696 | function file_path_for_type($file_path, $type='thumb') |
|---|
| 1697 | { |
|---|
| 1698 | // resolve the $file_path depending on the $type |
|---|
| 1699 | if ('thumb' == $type) { |
|---|
| 1700 | $file_path = get_thumbnail_location( |
|---|
| 1701 | array( |
|---|
| 1702 | 'path' => $file_path, |
|---|
| 1703 | 'tn_ext' => 'jpg', |
|---|
| 1704 | ) |
|---|
| 1705 | ); |
|---|
| 1706 | } |
|---|
| 1707 | |
|---|
| 1708 | if ('high' == $type) { |
|---|
| 1709 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 1710 | $file_path = get_high_location( |
|---|
| 1711 | array( |
|---|
| 1712 | 'path' => $file_path, |
|---|
| 1713 | 'has_high' => 'true' |
|---|
| 1714 | ) |
|---|
| 1715 | ); |
|---|
| 1716 | } |
|---|
| 1717 | |
|---|
| 1718 | return $file_path; |
|---|
| 1719 | } |
|---|
| 1720 | |
|---|
| 1721 | function ws_images_setInfo($params, &$service) |
|---|
| 1722 | { |
|---|
| 1723 | global $conf; |
|---|
| 1724 | if (!is_admin() || is_adviser() ) |
|---|
| 1725 | { |
|---|
| 1726 | return new PwgError(401, 'Access denied'); |
|---|
| 1727 | } |
|---|
| 1728 | |
|---|
| 1729 | if (!$service->isPost()) |
|---|
| 1730 | { |
|---|
| 1731 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 1732 | } |
|---|
| 1733 | |
|---|
| 1734 | $params['image_id'] = (int)$params['image_id']; |
|---|
| 1735 | if ($params['image_id'] <= 0) |
|---|
| 1736 | { |
|---|
| 1737 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
|---|
| 1738 | } |
|---|
| 1739 | |
|---|
| 1740 | $query=' |
|---|
| 1741 | SELECT * |
|---|
| 1742 | FROM '.IMAGES_TABLE.' |
|---|
| 1743 | WHERE id = '.$params['image_id'].' |
|---|
| 1744 | ;'; |
|---|
| 1745 | |
|---|
| 1746 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 1747 | if ($image_row == null) |
|---|
| 1748 | { |
|---|
| 1749 | return new PwgError(404, "image_id not found"); |
|---|
| 1750 | } |
|---|
| 1751 | |
|---|
| 1752 | // database registration |
|---|
| 1753 | $update = array(); |
|---|
| 1754 | |
|---|
| 1755 | $info_columns = array( |
|---|
| 1756 | 'name', |
|---|
| 1757 | 'author', |
|---|
| 1758 | 'comment', |
|---|
| 1759 | 'level', |
|---|
| 1760 | 'date_creation', |
|---|
| 1761 | ); |
|---|
| 1762 | |
|---|
| 1763 | foreach ($info_columns as $key) |
|---|
| 1764 | { |
|---|
| 1765 | if (isset($params[$key])) |
|---|
| 1766 | { |
|---|
| 1767 | if ('fill_if_empty' == $params['single_value_mode']) |
|---|
| 1768 | { |
|---|
| 1769 | if (empty($image_row[$key])) |
|---|
| 1770 | { |
|---|
| 1771 | $update[$key] = $params[$key]; |
|---|
| 1772 | } |
|---|
| 1773 | } |
|---|
| 1774 | elseif ('replace' == $params['single_value_mode']) |
|---|
| 1775 | { |
|---|
| 1776 | $update[$key] = $params[$key]; |
|---|
| 1777 | } |
|---|
| 1778 | else |
|---|
| 1779 | { |
|---|
| 1780 | new PwgError( |
|---|
| 1781 | 500, |
|---|
| 1782 | '[ws_images_setInfo]' |
|---|
| 1783 | .' invalid parameter single_value_mode "'.$params['single_value_mode'].'"' |
|---|
| 1784 | .', possible values are {fill_if_empty, replace}.' |
|---|
| 1785 | ); |
|---|
| 1786 | exit(); |
|---|
| 1787 | } |
|---|
| 1788 | } |
|---|
| 1789 | } |
|---|
| 1790 | |
|---|
| 1791 | if (count(array_keys($update)) > 0) |
|---|
| 1792 | { |
|---|
| 1793 | $update['id'] = $params['image_id']; |
|---|
| 1794 | |
|---|
| 1795 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1796 | mass_updates( |
|---|
| 1797 | IMAGES_TABLE, |
|---|
| 1798 | array( |
|---|
| 1799 | 'primary' => array('id'), |
|---|
| 1800 | 'update' => array_diff(array_keys($update), array('id')) |
|---|
| 1801 | ), |
|---|
| 1802 | array($update) |
|---|
| 1803 | ); |
|---|
| 1804 | } |
|---|
| 1805 | |
|---|
| 1806 | if (isset($params['categories'])) |
|---|
| 1807 | { |
|---|
| 1808 | ws_add_image_category_relations( |
|---|
| 1809 | $params['image_id'], |
|---|
| 1810 | $params['categories'], |
|---|
| 1811 | ('replace' == $params['multiple_value_mode'] ? true : false) |
|---|
| 1812 | ); |
|---|
| 1813 | } |
|---|
| 1814 | |
|---|
| 1815 | // and now, let's create tag associations |
|---|
| 1816 | if (isset($params['tag_ids'])) |
|---|
| 1817 | { |
|---|
| 1818 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1819 | |
|---|
| 1820 | $tag_ids = explode(',', $params['tag_ids']); |
|---|
| 1821 | |
|---|
| 1822 | if ('replace' == $params['multiple_value_mode']) |
|---|
| 1823 | { |
|---|
| 1824 | set_tags( |
|---|
| 1825 | $tag_ids, |
|---|
| 1826 | $params['image_id'] |
|---|
| 1827 | ); |
|---|
| 1828 | } |
|---|
| 1829 | elseif ('append' == $params['multiple_value_mode']) |
|---|
| 1830 | { |
|---|
| 1831 | add_tags( |
|---|
| 1832 | $tag_ids, |
|---|
| 1833 | array($params['image_id']) |
|---|
| 1834 | ); |
|---|
| 1835 | } |
|---|
| 1836 | else |
|---|
| 1837 | { |
|---|
| 1838 | new PwgError( |
|---|
| 1839 | 500, |
|---|
| 1840 | '[ws_images_setInfo]' |
|---|
| 1841 | .' invalid parameter multiple_value_mode "'.$params['multiple_value_mode'].'"' |
|---|
| 1842 | .', possible values are {replace, append}.' |
|---|
| 1843 | ); |
|---|
| 1844 | exit(); |
|---|
| 1845 | } |
|---|
| 1846 | } |
|---|
| 1847 | |
|---|
| 1848 | invalidate_user_cache(); |
|---|
| 1849 | } |
|---|
| 1850 | |
|---|
| 1851 | function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false) |
|---|
| 1852 | { |
|---|
| 1853 | // let's add links between the image and the categories |
|---|
| 1854 | // |
|---|
| 1855 | // $params['categories'] should look like 123,12;456,auto;789 which means: |
|---|
| 1856 | // |
|---|
| 1857 | // 1. associate with category 123 on rank 12 |
|---|
| 1858 | // 2. associate with category 456 on automatic rank |
|---|
| 1859 | // 3. associate with category 789 on automatic rank |
|---|
| 1860 | $cat_ids = array(); |
|---|
| 1861 | $rank_on_category = array(); |
|---|
| 1862 | $search_current_ranks = false; |
|---|
| 1863 | |
|---|
| 1864 | $tokens = explode(';', $categories_string); |
|---|
| 1865 | foreach ($tokens as $token) |
|---|
| 1866 | { |
|---|
| 1867 | @list($cat_id, $rank) = explode(',', $token); |
|---|
| 1868 | |
|---|
| 1869 | if (!preg_match('/^\d+$/', $cat_id)) |
|---|
| 1870 | { |
|---|
| 1871 | continue; |
|---|
| 1872 | } |
|---|
| 1873 | |
|---|
| 1874 | array_push($cat_ids, $cat_id); |
|---|
| 1875 | |
|---|
| 1876 | if (!isset($rank)) |
|---|
| 1877 | { |
|---|
| 1878 | $rank = 'auto'; |
|---|
| 1879 | } |
|---|
| 1880 | $rank_on_category[$cat_id] = $rank; |
|---|
| 1881 | |
|---|
| 1882 | if ($rank == 'auto') |
|---|
| 1883 | { |
|---|
| 1884 | $search_current_ranks = true; |
|---|
| 1885 | } |
|---|
| 1886 | } |
|---|
| 1887 | |
|---|
| 1888 | $cat_ids = array_unique($cat_ids); |
|---|
| 1889 | |
|---|
| 1890 | if (count($cat_ids) == 0) |
|---|
| 1891 | { |
|---|
| 1892 | new PwgError( |
|---|
| 1893 | 500, |
|---|
| 1894 | '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"' |
|---|
| 1895 | ); |
|---|
| 1896 | exit(); |
|---|
| 1897 | } |
|---|
| 1898 | |
|---|
| 1899 | $query = ' |
|---|
| 1900 | SELECT |
|---|
| 1901 | id |
|---|
| 1902 | FROM '.CATEGORIES_TABLE.' |
|---|
| 1903 | WHERE id IN ('.implode(',', $cat_ids).') |
|---|
| 1904 | ;'; |
|---|
| 1905 | $db_cat_ids = array_from_query($query, 'id'); |
|---|
| 1906 | |
|---|
| 1907 | $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids); |
|---|
| 1908 | if (count($unknown_cat_ids) != 0) |
|---|
| 1909 | { |
|---|
| 1910 | new PwgError( |
|---|
| 1911 | 500, |
|---|
| 1912 | '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids) |
|---|
| 1913 | ); |
|---|
| 1914 | exit(); |
|---|
| 1915 | } |
|---|
| 1916 | |
|---|
| 1917 | $to_update_cat_ids = array(); |
|---|
| 1918 | |
|---|
| 1919 | // in case of replace mode, we first check the existing associations |
|---|
| 1920 | $query = ' |
|---|
| 1921 | SELECT |
|---|
| 1922 | category_id |
|---|
| 1923 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 1924 | WHERE image_id = '.$image_id.' |
|---|
| 1925 | ;'; |
|---|
| 1926 | $existing_cat_ids = array_from_query($query, 'category_id'); |
|---|
| 1927 | |
|---|
| 1928 | if ($replace_mode) |
|---|
| 1929 | { |
|---|
| 1930 | $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids); |
|---|
| 1931 | if (count($to_remove_cat_ids) > 0) |
|---|
| 1932 | { |
|---|
| 1933 | $query = ' |
|---|
| 1934 | DELETE |
|---|
| 1935 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 1936 | WHERE image_id = '.$image_id.' |
|---|
| 1937 | AND category_id IN ('.implode(', ', $to_remove_cat_ids).') |
|---|
| 1938 | ;'; |
|---|
| 1939 | pwg_query($query); |
|---|
| 1940 | update_category($to_remove_cat_ids); |
|---|
| 1941 | } |
|---|
| 1942 | } |
|---|
| 1943 | |
|---|
| 1944 | $new_cat_ids = array_diff($cat_ids, $existing_cat_ids); |
|---|
| 1945 | if (count($new_cat_ids) == 0) |
|---|
| 1946 | { |
|---|
| 1947 | return true; |
|---|
| 1948 | } |
|---|
| 1949 | |
|---|
| 1950 | if ($search_current_ranks) |
|---|
| 1951 | { |
|---|
| 1952 | $query = ' |
|---|
| 1953 | SELECT |
|---|
| 1954 | category_id, |
|---|
| 1955 | MAX(rank) AS max_rank |
|---|
| 1956 | FROM '.IMAGE_CATEGORY_TABLE.' |
|---|
| 1957 | WHERE rank IS NOT NULL |
|---|
| 1958 | AND category_id IN ('.implode(',', $new_cat_ids).') |
|---|
| 1959 | GROUP BY category_id |
|---|
| 1960 | ;'; |
|---|
| 1961 | $current_rank_of = simple_hash_from_query( |
|---|
| 1962 | $query, |
|---|
| 1963 | 'category_id', |
|---|
| 1964 | 'max_rank' |
|---|
| 1965 | ); |
|---|
| 1966 | |
|---|
| 1967 | foreach ($new_cat_ids as $cat_id) |
|---|
| 1968 | { |
|---|
| 1969 | if (!isset($current_rank_of[$cat_id])) |
|---|
| 1970 | { |
|---|
| 1971 | $current_rank_of[$cat_id] = 0; |
|---|
| 1972 | } |
|---|
| 1973 | |
|---|
| 1974 | if ('auto' == $rank_on_category[$cat_id]) |
|---|
| 1975 | { |
|---|
| 1976 | $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; |
|---|
| 1977 | } |
|---|
| 1978 | } |
|---|
| 1979 | } |
|---|
| 1980 | |
|---|
| 1981 | $inserts = array(); |
|---|
| 1982 | |
|---|
| 1983 | foreach ($new_cat_ids as $cat_id) |
|---|
| 1984 | { |
|---|
| 1985 | array_push( |
|---|
| 1986 | $inserts, |
|---|
| 1987 | array( |
|---|
| 1988 | 'image_id' => $image_id, |
|---|
| 1989 | 'category_id' => $cat_id, |
|---|
| 1990 | 'rank' => $rank_on_category[$cat_id], |
|---|
| 1991 | ) |
|---|
| 1992 | ); |
|---|
| 1993 | } |
|---|
| 1994 | |
|---|
| 1995 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 1996 | mass_inserts( |
|---|
| 1997 | IMAGE_CATEGORY_TABLE, |
|---|
| 1998 | array_keys($inserts[0]), |
|---|
| 1999 | $inserts |
|---|
| 2000 | ); |
|---|
| 2001 | |
|---|
| 2002 | update_category($new_cat_ids); |
|---|
| 2003 | } |
|---|
| 2004 | |
|---|
| 2005 | function ws_categories_setInfo($params, &$service) |
|---|
| 2006 | { |
|---|
| 2007 | global $conf; |
|---|
| 2008 | if (!is_admin() || is_adviser() ) |
|---|
| 2009 | { |
|---|
| 2010 | return new PwgError(401, 'Access denied'); |
|---|
| 2011 | } |
|---|
| 2012 | |
|---|
| 2013 | if (!$service->isPost()) |
|---|
| 2014 | { |
|---|
| 2015 | return new PwgError(405, "This method requires HTTP POST"); |
|---|
| 2016 | } |
|---|
| 2017 | |
|---|
| 2018 | // category_id |
|---|
| 2019 | // name |
|---|
| 2020 | // comment |
|---|
| 2021 | |
|---|
| 2022 | $params['category_id'] = (int)$params['category_id']; |
|---|
| 2023 | if ($params['category_id'] <= 0) |
|---|
| 2024 | { |
|---|
| 2025 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
|---|
| 2026 | } |
|---|
| 2027 | |
|---|
| 2028 | // database registration |
|---|
| 2029 | $update = array( |
|---|
| 2030 | 'id' => $params['category_id'], |
|---|
| 2031 | ); |
|---|
| 2032 | |
|---|
| 2033 | $info_columns = array( |
|---|
| 2034 | 'name', |
|---|
| 2035 | 'comment', |
|---|
| 2036 | ); |
|---|
| 2037 | |
|---|
| 2038 | $perform_update = false; |
|---|
| 2039 | foreach ($info_columns as $key) |
|---|
| 2040 | { |
|---|
| 2041 | if (isset($params[$key])) |
|---|
| 2042 | { |
|---|
| 2043 | $perform_update = true; |
|---|
| 2044 | $update[$key] = $params[$key]; |
|---|
| 2045 | } |
|---|
| 2046 | } |
|---|
| 2047 | |
|---|
| 2048 | if ($perform_update) |
|---|
| 2049 | { |
|---|
| 2050 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
|---|
| 2051 | mass_updates( |
|---|
| 2052 | CATEGORIES_TABLE, |
|---|
| 2053 | array( |
|---|
| 2054 | 'primary' => array('id'), |
|---|
| 2055 | 'update' => array_diff(array_keys($update), array('id')) |
|---|
| 2056 | ), |
|---|
| 2057 | array($update) |
|---|
| 2058 | ); |
|---|
| 2059 | } |
|---|
| 2060 | |
|---|
| 2061 | } |
|---|
| 2062 | |
|---|
| 2063 | function ws_logfile($string) |
|---|
| 2064 | { |
|---|
| 2065 | global $conf; |
|---|
| 2066 | |
|---|
| 2067 | if (!$conf['ws_enable_log']) { |
|---|
| 2068 | return true; |
|---|
| 2069 | } |
|---|
| 2070 | |
|---|
| 2071 | file_put_contents( |
|---|
| 2072 | $conf['ws_log_filepath'], |
|---|
| 2073 | '['.date('c').'] '.$string."\n", |
|---|
| 2074 | FILE_APPEND |
|---|
| 2075 | ); |
|---|
| 2076 | } |
|---|
| 2077 | ?> |
|---|