as published by the Free Software Foundation.

  ********************************************
  Coppermine version: 1.5.40
  $HeadURL: https://svn.code.sf.net/p/coppermine/code/trunk/cpg1.5.x/modifyalb.php $
  $Revision: 8830 $
**********************************************/
define('IN_COPPERMINE', true);
define('MODIFYALB_PHP', true);
include "include/init.inc.php";
if ($superCage->get->keyExists('album')) {
    $CLEAN['album'] = $superCage->get->getInt('album');
} else {
    $CLEAN['album'] = 0;
}
if (!(GALLERY_ADMIN_MODE || USER_ADMIN_MODE && user_is_allowed(false))) {
    cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);
}
// Type 0 => input
// 1 => yes/no
// 2 => Category
// 3 => Textarea
// 4 => Album thumbnail
// 5 => Album visibility
// add footnote
$notice1 = ' <a href="#notice1" class="">*</a>';
$help = array();
$help['table'] = '&nbsp;' . cpg_display_help('f=albums.htm&amp;as=album_prop&amp;ae=album_prop_end&amp;top=1', '600', '400');
$help['related_tasks'] = '&nbsp;' . cpg_display_help('f=albums.htm&amp;as=album_prop_controls_related_tasks&amp;ae=album_prop_controls_related_tasks_end', '500', '250');
$help['choose_album'] = '&nbsp;' . cpg_display_help('f=albums.htm&amp;as=album_prop_controls_album_dropdown&amp;ae=album_prop_controls_album_dropdown_end', '500', '250');
$help['album_title'] = '&nbsp;' . cpg_display_help('f=albums.htm&amp;as=album_prop_controls_album_title&amp;ae=album_prop_controls_album_title_end', '500', '250');
Example #2
0
 if (!USER_CAN_UPLOAD_PICTURES && !USER_CAN_CREATE_ALBUMS) {
     if ($CONFIG['log_mode'] != 0) {
         log_write('Denied privileged access to db_input.php (upload picture) for user ' . $USER_DATA['user_name'] . ' at ' . $hdr_ip, CPG_SECURITY_LOG);
     }
     cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
 }
 $album = $superCage->post->getInt('album');
 $title = $superCage->post->getEscaped('title');
 $caption = $superCage->post->getEscaped('caption');
 $keywords = $superCage->post->getEscaped('keywords');
 $user1 = $superCage->post->getEscaped('user1');
 $user2 = $superCage->post->getEscaped('user2');
 $user3 = $superCage->post->getEscaped('user3');
 $user4 = $superCage->post->getEscaped('user4');
 // Check if the album id provided is valid
 if (!(GALLERY_ADMIN_MODE || user_is_allowed())) {
     $result = cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = {$album} AND (owner = " . USER_ID . " OR category = " . (USER_ID + FIRST_USER_CAT) . (USER_CAN_UPLOAD_PICTURES ? ' OR uploads = "YES"' : '') . ")");
     if (mysql_num_rows($result) == 0) {
         cpg_die(ERROR, $lang_db_input_php['unknown_album'], __FILE__, __LINE__);
     }
     $row = mysql_fetch_assoc($result);
     mysql_free_result($result);
     $category = $row['category'];
 } else {
     $result = cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = {$album}");
     if (mysql_num_rows($result) == 0) {
         cpg_die(ERROR, $lang_db_input_php['unknown_album'], __FILE__, __LINE__);
     }
     $row = mysql_fetch_assoc($result);
     mysql_free_result($result);
     $category = $row['category'];
Example #3
0
/**
 * process_post_data()
 *
 * Function to process the form posted
 */
function process_post_data()
{
    global $CONFIG;
    global $user_albums_list, $lang_errors;
    $superCage = Inspekt::makeSuperCage();
    $user_album_set = array();
    foreach ($user_albums_list as $album) {
        $user_album_set[$album['aid']] = 1;
    }
    $pid = $superCage->post->getInt('pid');
    if (!is_array($pid)) {
        cpg_die(CRITICAL_ERROR, $lang_errors['param_missing'], __FILE__, __LINE__);
    }
    $pid_array = $pid;
    if ($superCage->post->keyExists('galleryicon')) {
        $galleryincon = $superCage->post->getInt('galleyicon');
    } else {
        $galleryicon = '';
    }
    foreach ($pid_array as $pid) {
        $aid = $superCage->post->getInt("aid{$pid}");
        $title = get_post_var('title', $pid);
        $caption = get_post_var('caption', $pid);
        $keywords = get_post_var('keywords', $pid);
        $user1 = get_post_var('user1', $pid);
        $user2 = get_post_var('user2', $pid);
        $user3 = get_post_var('user3', $pid);
        $user4 = get_post_var('user4', $pid);
        $delete = false;
        $reset_vcount = false;
        $reset_votes = false;
        $del_comments = false;
        $isgalleryicon = $galleryicon === $pid;
        if ($superCage->post->keyExists('delete' . $pid)) {
            $delete = $superCage->post->getInt('delete' . $pid);
        }
        if ($superCage->post->keyExists('reset_vcount' . $pid)) {
            $reset_vcount = $superCage->post - getInt('reset_vcount' . $pid);
        }
        if ($superCage->post->keyExists('reset_votes' . $pid)) {
            $reset_votes = $superCage->post->getInt('reset_votes' . $pid);
        }
        if ($superCage->post->keyExists('del_comments' . $pid)) {
            $del_comments = $superCage->post->getInt('del_comments' . $pid) || $delete;
        }
        // OVI
        //$query = "SELECT category, filepath, filename, owner_id FROM {$CONFIG['TABLE_PICTURES']}, {$CONFIG['TABLE_ALBUMS']} WHERE {$CONFIG['TABLE_PICTURES']}.aid = {$CONFIG['TABLE_ALBUMS']}.aid AND pid='$pid'";
        $query = "SELECT {$CONFIG['TABLE_PICTURES']}.aid, category, filepath, filename, owner_id, total_filesize FROM {$CONFIG['TABLE_PICTURES']}, {$CONFIG['TABLE_ALBUMS']} WHERE {$CONFIG['TABLE_PICTURES']}.aid = {$CONFIG['TABLE_ALBUMS']}.aid AND pid='{$pid}'";
        $result = cpg_db_query($query);
        if (!mysql_num_rows($result)) {
            cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
        }
        $pic = mysql_fetch_array($result);
        mysql_free_result($result);
        if (!GALLERY_ADMIN_MODE && !MODERATOR_MODE && !USER_ADMIN_MODE && !user_is_allowed() && !$CONFIG['users_can_edit_pics']) {
            if ($pic['category'] != FIRST_USER_CAT + USER_ID) {
                cpg_die(ERROR, $lang_errors['perm_denied'] . "<br />(picture category = {$pic['category']}/ {$pid})", __FILE__, __LINE__);
            }
            if (!isset($user_album_set[$aid])) {
                cpg_die(ERROR, $lang_errors['perm_denied'] . "<br />(target album = {$aid})", __FILE__, __LINE__);
            }
        }
        $update = "aid = '{$aid}'";
        $update .= ", title = '{$title}'";
        $update .= ", caption = '{$caption}'";
        $update .= ", keywords = '{$keywords}'";
        $update .= ", user1 = '{$user1}'";
        $update .= ", user2 = '{$user2}'";
        $update .= ", user3 = '{$user3}'";
        $update .= ", user4 = '{$user4}'";
        if ($isgalleryicon && $pic['category'] > FIRST_USER_CAT) {
            $sql = 'UPDATE ' . $CONFIG['TABLE_PICTURES'] . ' SET galleryicon=0 WHERE owner_id=' . $pic['owner_id'] . ';';
            cpg_db_query($sql);
            $update .= ", galleryicon = " . addslashes($galleryicon);
        }
        if (is_movie($pic['filename'])) {
            $pwidth = $superCage->post->getInt('pwidth' . $pid);
            $pheight = $superCage->post->getInt('pheight' . $pid);
            $update .= ", pwidth = " . $pwidth;
            $update .= ", pheight = " . $pheight;
        }
        if ($reset_vcount) {
            $update .= ", hits = '0'";
            resetDetailHits($pid);
        }
        if ($reset_votes) {
            $update .= ", pic_rating = '0', votes = '0'";
            resetDetailVotes($pid);
        }
        if (GALLERY_ADMIN_MODE || UPLOAD_APPROVAL_MODE || MODERATOR_MODE) {
            if ($superCage->post->keyExists('approved' . $pid)) {
                $approved = $superCage->post->getAlpha('approved' . $pid);
            }
            if ($approved) {
                $update .= ", approved = 'YES'";
            } else {
                $update .= ", approved = 'NO'";
            }
        }
        if ($del_comments) {
            $query = "DELETE FROM {$CONFIG['TABLE_COMMENTS']} WHERE pid='{$pid}'";
            $result = cpg_db_query($query);
        }
        if ($delete) {
            $dir = $CONFIG['fullpath'] . $pic['filepath'];
            $file = $pic['filename'];
            if (!is_writable($dir)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_errors['directory_ro'], $dir), __FILE__, __LINE__);
            }
            ///////// OVI
            $picture_id = $pid;
            $owner_id = $pic['owner_id'];
            $imageContainer = new FileContainer($picture_id, $owner_id);
            $imageContainer->original_path = $dir . $file;
            // check
            $imageContainer->total_filesize = $pic['total_filesize'];
            ///////// OVI
            /* // OVI
                        $files = array ($dir . $file, $dir . $CONFIG['normal_pfx'] . $file, $dir . $CONFIG['orig_pfx'] . $file, $dir . $CONFIG['thumb_pfx'] . $file);
                        foreach ($files as $currFile){
                                if (is_file($currFile)) @unlink($currFile);
                        }
            			*/
            ///////// OVI
            $files = array($dir . $file, $dir . $CONFIG['normal_pfx'] . $file, $dir . $CONFIG['orig_pfx'] . $file, $dir . $CONFIG['thumb_pfx'] . $file);
            foreach ($files as $currFile) {
                if ($currFile != $dir . $file) {
                    $imageContainer->thumb_paths[] = $currFile;
                }
                if (is_file($currFile)) {
                    @unlink($currFile);
                }
            }
            ///////// OVI
            ///// OVI
            global $storage;
            $storage->delete_file($imageContainer);
            ///// OVI
            $query = "DELETE FROM {$CONFIG['TABLE_PICTURES']} WHERE pid='{$pid}' LIMIT 1";
            $result = cpg_db_query($query);
        } else {
            $query = "UPDATE {$CONFIG['TABLE_PICTURES']} SET {$update} WHERE pid='{$pid}' LIMIT 1";
            $result = cpg_db_query($query);
        }
    }
}
Example #4
0
/**
 * process_post_data()
 *
 * Function to process the form posted
 */
function process_post_data()
{
    global $CONFIG, $user_albums_list, $lang_errors;
    $superCage = Inspekt::makeSuperCage();
    //Check if the form token is valid
    if (!checkFormToken()) {
        cpg_die(ERROR, $lang_errors['invalid_form_token'], __FILE__, __LINE__);
    }
    $user_album_set = array();
    $result = cpg_db_query("SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (FIRST_USER_CAT + USER_ID) . " OR owner = " . USER_ID . " OR uploads = 'YES'");
    while ($row = $result->fetchAssoc()) {
        $user_album_set[$row['aid']] = 1;
    }
    $result->free();
    $pid_array = $superCage->post->getInt('pid');
    if (!is_array($pid_array)) {
        cpg_die(CRITICAL_ERROR, $lang_errors['param_missing'], __FILE__, __LINE__);
    }
    if ($superCage->post->keyExists('galleryicon')) {
        $galleryicon = $superCage->post->getInt('galleryicon');
    } else {
        $galleryicon = '';
    }
    foreach ($pid_array as $pid) {
        $aid = $superCage->post->getInt("aid{$pid}");
        $title = get_post_var('title', $pid);
        $caption = get_post_var('caption', $pid);
        $keywords = get_post_var('keywords', $pid);
        $user1 = get_post_var('user1', $pid);
        $user2 = get_post_var('user2', $pid);
        $user3 = get_post_var('user3', $pid);
        $user4 = get_post_var('user4', $pid);
        $delete = false;
        $reset_vcount = false;
        $reset_votes = false;
        $del_comments = false;
        $isgalleryicon = $galleryicon === $pid;
        if ($superCage->post->keyExists('delete' . $pid)) {
            $delete = $superCage->post->getInt('delete' . $pid);
        }
        if ($superCage->post->keyExists('reset_vcount' . $pid)) {
            $reset_vcount = $superCage->post->getInt('reset_vcount' . $pid);
        }
        if ($superCage->post->keyExists('reset_votes' . $pid)) {
            $reset_votes = $superCage->post->getInt('reset_votes' . $pid);
        }
        if ($superCage->post->keyExists('del_comments' . $pid)) {
            $del_comments = $superCage->post->getInt('del_comments' . $pid);
        }
        // We will be selecting pid in the query as we need it in $pic array for the plugin filter
        $query = "SELECT pid, category, filepath, filename, owner_id FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = p.aid WHERE pid = {$pid}";
        $result = cpg_db_query($query);
        if (!$result->numRows()) {
            cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
        }
        $pic = $result->fetchAssoc(true);
        if (!GALLERY_ADMIN_MODE && !MODERATOR_MODE && !USER_ADMIN_MODE && !user_is_allowed() && !$CONFIG['users_can_edit_pics']) {
            if ($pic['category'] != FIRST_USER_CAT + USER_ID) {
                cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
            }
            if (!isset($user_album_set[$aid])) {
                cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
            }
        }
        cpg_trim_keywords($keywords);
        $update = "aid = '{$aid}'";
        $update .= ", title = '{$title}'";
        $update .= ", caption = '{$caption}'";
        $update .= ", keywords = '{$keywords}'";
        $update .= ", user1 = '{$user1}'";
        $update .= ", user2 = '{$user2}'";
        $update .= ", user3 = '{$user3}'";
        $update .= ", user4 = '{$user4}'";
        if ($isgalleryicon && $pic['category'] > FIRST_USER_CAT) {
            cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET galleryicon = 0 WHERE owner_id = {$pic['owner_id']}");
            $update .= ", galleryicon = " . $galleryicon;
        }
        if (is_movie($pic['filename'])) {
            $pwidth = $superCage->post->getInt('pwidth' . $pid);
            $pheight = $superCage->post->getInt('pheight' . $pid);
            $update .= ", pwidth = " . $pwidth;
            $update .= ", pheight = " . $pheight;
        }
        if ($reset_vcount) {
            $update .= ", hits = 0";
            resetDetailHits($pid);
        }
        if ($reset_votes) {
            $update .= ", pic_rating = 0, votes = 0";
            resetDetailVotes($pid);
        }
        if (GALLERY_ADMIN_MODE || UPLOAD_APPROVAL_MODE || MODERATOR_MODE) {
            $approved = '';
            if ($superCage->post->keyExists('approved' . $pid)) {
                $approved = $superCage->post->getAlpha('approved' . $pid);
            }
            if ($approved == 'YES') {
                $update .= ", approved = 'YES'";
            } else {
                $update .= ", approved = 'NO'";
            }
        }
        if ($del_comments || $delete) {
            cpg_db_query("DELETE FROM {$CONFIG['TABLE_COMMENTS']} WHERE pid = {$pid}");
        }
        if ($delete) {
            $dir = $CONFIG['fullpath'] . $pic['filepath'];
            $file = $pic['filename'];
            if (!is_writable($dir)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_errors['directory_ro'], $dir), __FILE__, __LINE__);
            }
            $files = array($dir . $file, $dir . $CONFIG['normal_pfx'] . $file, $dir . $CONFIG['orig_pfx'] . $file, $dir . $CONFIG['thumb_pfx'] . $file);
            // Check for custom thumbnails for non-images
            if (!is_image($file)) {
                $mime_content = cpg_get_type($file);
                $file_base_name = str_replace('.' . $mime_content['extension'], '', basename($file));
                foreach (array('.gif', '.png', '.jpg') as $thumb_extension) {
                    if (file_exists($dir . $CONFIG['thumb_pfx'] . $file_base_name . $thumb_extension)) {
                        // Thumbnail found, check if it's the only file using that thumbnail
                        $count = cpg_db_query("SELECT COUNT(*) FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$pic['filepath']}' AND filename LIKE '{$file_base_name}.%'")->result(0);
                        if ($count == 1) {
                            unset($files[count($files) - 1]);
                            $files[] = $dir . $CONFIG['thumb_pfx'] . $file_base_name . $thumb_extension;
                            break;
                        }
                    }
                }
            }
            foreach ($files as $currFile) {
                if (is_file($currFile)) {
                    @unlink($currFile);
                }
            }
            // Plugin filter to be called before deleting a file
            CPGPluginAPI::action('before_delete_file', $pic);
            cpg_db_query("DELETE FROM {$CONFIG['TABLE_PICTURES']} WHERE pid = {$pid} LIMIT 1");
            cpg_db_query("UPDATE {$CONFIG['TABLE_ALBUMS']} SET thumb = '0' WHERE thumb = '{$pid}'");
            // Plugin filter to be called after a file is deleted
            CPGPluginAPI::action('after_delete_file', $pic);
        } else {
            cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET {$update} WHERE pid = {$pid}");
            // Executes after a file update is committed
            CPGPluginAPI::action('after_edit_file', $pid);
        }
    }
}
function process_post_data()
{
    global $CONFIG, $USER_DATA, $lang_errors, $lang_editpics_php, $superCage;
    //Check if the form token is valid
    if (!checkFormToken()) {
        cpg_die(ERROR, $lang_errors['invalid_form_token'], __FILE__, __LINE__);
    }
    $user_album_set = array();
    $result = cpg_db_query("SELECT aid FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (FIRST_USER_CAT + USER_ID) . " OR owner = " . USER_ID . " OR uploads = 'YES'");
    while ($row = mysql_fetch_assoc($result)) {
        $user_album_set[$row['aid']] = 1;
    }
    mysql_free_result($result);
    $pid = $superCage->post->getInt('id');
    $aid = $superCage->post->getInt('aid');
    $pwidth = $superCage->post->getInt('pwidth');
    $pheight = $superCage->post->getInt('pheight');
    $title = cpgSanitizeUserTextInput($superCage->post->getEscaped('title'));
    $caption = cpgSanitizeUserTextInput($superCage->post->getEscaped('caption'));
    $keywords = cpgSanitizeUserTextInput(utf_replace($superCage->post->getEscaped('keywords')));
    $user1 = cpgSanitizeUserTextInput($superCage->post->getEscaped('user1'));
    $user2 = cpgSanitizeUserTextInput($superCage->post->getEscaped('user2'));
    $user3 = cpgSanitizeUserTextInput($superCage->post->getEscaped('user3'));
    $user4 = cpgSanitizeUserTextInput($superCage->post->getEscaped('user4'));
    $galleryicon = $superCage->post->getInt('galleryicon');
    $isgalleryicon = $galleryicon == $pid;
    $read_exif = $superCage->post->keyExists('read_exif') ? $superCage->post->getInt('read_exif') : 0;
    $reset_vcount = $superCage->post->keyExists('reset_vcount') ? $superCage->post->getInt('reset_vcount') : 0;
    $reset_votes = $superCage->post->keyExists('reset_votes') ? $superCage->post->getInt('reset_votes') : 0;
    $del_comments = $superCage->post->keyExists('del_comments') ? $superCage->post->getInt('del_comments') : 0;
    $result = cpg_db_query("SELECT category, owner_id, url_prefix, filepath, filename, pwidth, pheight, p.aid AS aid FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = p.aid WHERE pid = '{$pid}'");
    if (!mysql_num_rows($result)) {
        cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
    }
    $pic = mysql_fetch_assoc($result);
    mysql_free_result($result);
    if (!GALLERY_ADMIN_MODE && !MODERATOR_MODE && !USER_ADMIN_MODE && !user_is_allowed() && !$CONFIG['users_can_edit_pics']) {
        if ($pic['category'] != FIRST_USER_CAT + USER_ID) {
            cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
        }
        if (!isset($user_album_set[$aid])) {
            cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
        }
    }
    if (!USER_ID || !(GALLERY_ADMIN_MODE || $pic['category'] == FIRST_USER_CAT + USER_ID || $CONFIG['users_can_edit_pics'] && $pic['owner_id'] == USER_ID)) {
        cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);
    }
    $result = cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = '{$aid}'");
    if (!mysql_num_rows($result)) {
        cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
    }
    $new_alb = mysql_fetch_assoc($result);
    mysql_free_result($result);
    cpg_trim_keywords($keywords);
    $update = "aid = '{$aid}'";
    if (is_movie($pic['filename'])) {
        $update .= ", pwidth = " . $pwidth;
        $update .= ", pheight = " . $pheight;
    }
    $update .= ", title = '{$title}'";
    $update .= ", caption = '{$caption}'";
    $update .= ", keywords = '{$keywords}'";
    if (GALLERY_ADMIN_MODE) {
        $approved = $superCage->post->getAlpha('approved');
        $update .= ", approved = '{$approved}'";
    } elseif ($new_alb['category'] < FIRST_USER_CAT && $aid != $pic['aid']) {
        $approved = $USER_DATA['pub_upl_need_approval'] ? 'NO' : 'YES';
        $update .= ", approved = '{$approved}'";
    } elseif ($new_alb['category'] > FIRST_USER_CAT && $aid != $pic['aid'] && $pic['category'] < FIRST_USER_CAT) {
        $approved = $USER_DATA['priv_upl_need_approval'] ? 'NO' : 'YES';
        $update .= ", approved = '{$approved}'";
    }
    $update .= ", user1 = '{$user1}'";
    $update .= ", user2 = '{$user2}'";
    $update .= ", user3 = '{$user3}'";
    $update .= ", user4 = '{$user4}'";
    if ($isgalleryicon && $pic['category'] > FIRST_USER_CAT) {
        $sql = "UPDATE {$CONFIG['TABLE_PICTURES']} SET galleryicon = 0 WHERE owner_id = {$pic['owner_id']}";
        cpg_db_query($sql);
        $update .= ", galleryicon = " . $galleryicon;
    }
    if ($reset_vcount) {
        $update .= ", hits = 0";
        resetDetailHits($pid);
    }
    if ($reset_votes) {
        $update .= ", pic_rating = 0, votes = 0";
        resetDetailVotes($pid);
    }
    if ($read_exif) {
        // If "read exif info again" is checked then just delete the entry from the exif table.
        // The new exif information will automatically be read when someone views the image.
        $query = "DELETE FROM {$CONFIG['TABLE_EXIF']} WHERE pid = '{$pid}'";
        cpg_db_query($query);
    }
    if ($del_comments) {
        $query = "DELETE FROM {$CONFIG['TABLE_COMMENTS']} WHERE pid = '{$pid}'";
        cpg_db_query($query);
    }
    $query = "UPDATE {$CONFIG['TABLE_PICTURES']} SET {$update} WHERE pid='{$pid}' LIMIT 1";
    cpg_db_query($query);
    // Executes after a file update is committed
    CPGPluginAPI::action('after_edit_file', $pid);
    // rename a file
    if ($superCage->post->keyExists('filename')) {
        $post_filename = $superCage->post->getEscaped('filename');
    }
    if ($post_filename != $pic['filename']) {
        if ($CONFIG['make_intermediate'] && cpg_picture_dimension_exceeds_intermediate_limit($pic['pwidth'], $pic['pheight'])) {
            $prefixes = array('fullsize', 'normal', 'thumb');
        } else {
            $prefixes = array('fullsize', 'thumb');
        }
        if ($CONFIG['enable_watermark'] == '1' && ($CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'original')) {
            $prefixes[] = 'orig';
        }
        if (!is_image($pic['filename'])) {
            $prefixes = array('fullsize');
            // Check for custom thumbnails
            $mime_content_old = cpg_get_type($pic['filename']);
            $mime_content_new = cpg_get_type(replace_forbidden($post_filename));
            $file_base_name_old = str_replace('.' . $mime_content_old['extension'], '', basename($pic['filename']));
            foreach (array('.gif', '.png', '.jpg') as $thumb_extension) {
                if (file_exists($CONFIG['fullpath'] . $pic['filepath'] . $CONFIG['thumb_pfx'] . $file_base_name_old . $thumb_extension)) {
                    // Thumbnail found, check if it's the only file using that thumbnail
                    $count = mysql_result(cpg_db_query("SELECT COUNT(*) FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$pic['filepath']}' AND filename LIKE '{$file_base_name_old}.%'"), 0);
                    if ($count == 1) {
                        $prefixes[] = 'thumb';
                        $custom_thumb = TRUE;
                        break;
                    }
                }
            }
        }
        $pic_prefix = array('thumb' => $CONFIG['thumb_pfx'], 'normal' => $CONFIG['normal_pfx'], 'orig' => $CONFIG['orig_pfx'], 'fullsize' => '');
        $files_to_rename = array();
        foreach ($prefixes as $prefix) {
            $oldname = urldecode($CONFIG['fullpath'] . $pic['filepath'] . $pic_prefix[$prefix] . $pic['filename']);
            $filename = replace_forbidden($post_filename);
            $newname = str_replace($pic['filename'], $filename, $oldname);
            if ($custom_thumb == TRUE && $prefix == 'thumb') {
                $oldname = str_replace('.' . $mime_content_old['extension'], $thumb_extension, $oldname);
                $newname = str_replace('.' . $mime_content_new['extension'], $thumb_extension, $newname);
            }
            $old_mime = cpg_get_type($oldname);
            $new_mime = cpg_get_type($newname);
            if ($old_mime['mime'] != $new_mime['mime'] && isset($new_mime['mime'])) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['mime_conv'], $old_mime['mime'], $new_mime['mime']), __FILE__, __LINE__);
            }
            if (!is_known_filetype($newname)) {
                cpg_die(CRITICAL_ERROR, $lang_editpics_php['forb_ext'], __FILE__, __LINE__);
            }
            if (file_exists($newname)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['file_exists'], $newname), __FILE__, __LINE__);
            }
            if (!file_exists($oldname)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['src_file_missing'], $oldname), __FILE__, __LINE__);
            }
            // Check if there will be no conflicts before doing anything
            $files_to_rename[] = array('oldname' => $oldname, 'filename' => $filename, 'newname' => $newname);
        }
        if (count($files_to_rename) > 0) {
            foreach ($files_to_rename as $file) {
                if (rename($file['oldname'], $file['newname'])) {
                    cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET filename = '{$file['filename']}' WHERE pid = '{$pid}' LIMIT 1");
                } else {
                    cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['rename_failed'], $oldname, $newname), __FILE__, __LINE__);
                }
            }
        }
    }
}