diff options
Diffstat (limited to 'admin/include')
-rw-r--r-- | admin/include/configuration_watermark_process.inc.php | 35 | ||||
-rw-r--r-- | admin/include/functions.php | 119 |
2 files changed, 151 insertions, 3 deletions
diff --git a/admin/include/configuration_watermark_process.inc.php b/admin/include/configuration_watermark_process.inc.php index bc1451791..0e07380a7 100644 --- a/admin/include/configuration_watermark_process.inc.php +++ b/admin/include/configuration_watermark_process.inc.php @@ -26,6 +26,21 @@ if( !defined("PHPWG_ROOT_PATH") ) die ("Hacking attempt!"); } +function get_watermark_filename($list, $candidate, $step = 0) +{ + global $change_name; + $change_name = $candidate; + if ($step != 0) + { + $change_name .= '-'.$step; + } + if (in_array($change_name, $list)) + { + return get_watermark_filename($list, $candidate, $step+1); + } + return $change_name.'.png'; +} + $errors = array(); $pwatermark = $_POST['w']; @@ -45,8 +60,22 @@ if (isset($_FILES['watermarkImage']) and !empty($_FILES['watermarkImage']['tmp_n $upload_dir = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks'; if (mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) { - $new_name = get_filename_wo_extension($_FILES['watermarkImage']['name']).'.png'; - $file_path = $upload_dir.'/'.$new_name; + // file name may include exotic chars like single quote, we need a safe name + $new_name = str2url(get_filename_wo_extension($_FILES['watermarkImage']['name'])); + + // we need existing watermarks to avoid overwritting one + $watermark_files = array(); + if ( ($glob=glob(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks/*.png')) !== false) + { + foreach ($glob as $file) + { + $watermark_files[] = get_filename_wo_extension( + substr($file, strlen(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks/')) + ); + } + } + + $file_path = $upload_dir.'/'.get_watermark_filename($watermark_files, $new_name); if (move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path)) { @@ -183,4 +212,4 @@ else $template->assign('watermark', $pwatermark); $template->assign('ferrors', $errors); } -?>
\ No newline at end of file +?> diff --git a/admin/include/functions.php b/admin/include/functions.php index 9a827d1d6..030e7fa31 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -1519,6 +1519,8 @@ function add_tags($tags, $images) return; } + $taglist_before = get_image_tag_ids($images); + // we can't insert twice the same {image_id,tag_id} so we must first // delete lines we'll insert later $query = ' @@ -1545,6 +1547,11 @@ DELETE array_keys($inserts[0]), $inserts ); + + $taglist_after = get_image_tag_ids($images); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + update_images_lastmodified($images_to_update); + invalidate_user_cache_nb_tags(); } @@ -1565,6 +1572,15 @@ function delete_tags($tag_ids) return false; } + // we need the list of impacted images, to update their lastmodified + $query = ' +SELECT + image_id + FROM '.IMAGE_TAG_TABLE.' + WHERE tag_id IN ('.implode(',', $tag_ids).') +;'; + $image_ids = query2array($query, null, 'image_id'); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1579,6 +1595,7 @@ DELETE ;'; pwg_query($query); + update_images_lastmodified($image_ids); invalidate_user_cache_nb_tags(); } @@ -1662,6 +1679,9 @@ function set_tags_of($tags_of) { if (count($tags_of) > 0) { + $taglist_before = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_before', $taglist_before); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1691,11 +1711,83 @@ DELETE ); } + $taglist_after = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_after', $taglist_after); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + global $logger; $logger->debug('$images_to_update', $images_to_update); + + update_images_lastmodified($images_to_update); invalidate_user_cache_nb_tags(); } } /** + * Get list of tag ids for each image. Returns an empty list if the image has + * no tags. + * + * @since 2.9 + * @param array $image_ids + * @return associative array, image_id => list of tag ids + */ +function get_image_tag_ids($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return array(); + } + + $query = ' +SELECT + image_id, + tag_id + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', $image_ids).') +;'; + + $tags_of = array_fill_keys($image_ids, array()); + $image_tags = query2array($query); + foreach ($image_tags as $image_tag) + { + $tags_of[ $image_tag['image_id'] ][] = $image_tag['tag_id']; + } + + return $tags_of; +} + +/** + * Compare the list of tags, for each image. Returns image_ids where tag list has changed. + * + * @since 2.9 + * @param array $taglist_before - for each image_id (key), list of tag ids + * @param array $taglist_after - for each image_id (key), list of tag ids + * @return array - image_ids where the list has changed + */ +function compare_image_tag_lists($taglist_before, $taglist_after) +{ + $images_to_update = array(); + + foreach ($taglist_after as $image_id => $list_after) + { + sort($list_after); + + $list_before = isset($taglist_before[$image_id]) ? $taglist_before[$image_id] : array(); + sort($list_before); + + if ($list_after != $list_before) + { + $images_to_update[] = $image_id; + } + } + + return $images_to_update; +} + +/** * Associate a list of images to a list of categories. * The function will not duplicate links and will preserve ranks. * @@ -2887,3 +2979,30 @@ function save_images_order($category_id, $images) ); mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas); } + +/** + * Force update on images.lastmodified column. Useful when modifying the tag + * list. + * + * @since 2.9 + * @param array $image_ids + */ +function update_images_lastmodified($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return; + } + + $query = ' +UPDATE '.IMAGES_TABLE.' + SET lastmodified = NOW() + WHERE id IN ('.implode(',', $image_ids).') +;'; + pwg_query($query); +} |