<?php 
/*  use at your own risk...
 0.1    1st public version
 0.2    process only jpg-images
 0.3    should now work with no images, added some error control (erros in English hard coded)
        KNOWN PROBLEM: custom thumb could be overwriten and not restored!
 */ 
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

$thisplugin->add_action('page_start','uebernehme');
$thisplugin->add_filter('file_data','menu');

function menu($data)
{
    global $CONFIG, $CURRENT_ALBUM_DATA;
    require 'plugins/wunschliste/settings.php';
    if ($CURRENT_ALBUM_DATA['category'] == $wl_category) {
        if (GALLERY_ADMIN_MODE)
            $msg = $wl_admin;
        elseif (USER_ID != 0)
            $msg = $wl_user;
        $data['menu'] .= " <a href='?wl_pid=".$data['pid']."' class='admin_menu' onclick='return confirm(\"" .$msg . "?\"); return false'>" . $msg . "</a>";
    }
    return $data;
}

function uebernehme()
{
    global $CONFIG, $lang_back;
    if (isset($_GET['wl_pid'])){
        require('plugins/wunschliste/settings.php');
        require('include/picmgmt.inc.php');
        $pid = (int) $_GET['wl_pid'];
        $result = cpg_db_query("SELECT * FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = p.aid WHERE p.pid = '$pid' LIMIT 1");
        $row = mysql_fetch_assoc($result);
        if (!(($row['category'] == $wl_category && USER_ID != 0) || GALLERY_ADMIN_MODE)) die('Access denied');
        $image = $CONFIG['fullpath'] . $row['filepath'] . $row['filename'];
        $normal = $CONFIG['fullpath'] . $row['filepath'] . $CONFIG['normal_pfx'] . $row['filename'];
        $thumb = $CONFIG['fullpath'] . $row['filepath'] . $CONFIG['thumb_pfx'] . $row['filename'];
        $wl_image_ext = "." . pathinfo($wl_image, PATHINFO_EXTENSION); 
        $file_ext = "." . pathinfo($row['filename'], PATHINFO_EXTENSION);
        $file_basename = basename($row['filename'], $file_ext); 
        $wl_thumb = $CONFIG['fullpath'] . $row['filepath'] . $CONFIG['thumb_pfx'] . $file_basename . $wl_image_ext;
        $total_filesize = $row['total_filesize'];
        if(GALLERY_ADMIN_MODE) { // Admin -> reset
            $neue_caption =  $row['caption'];
            if (substr($neue_caption,0,$wl_note_len) != $wl_note) {
                load_template();
                pageheader($wl_name, "<META http-equiv=\"refresh\" content=\"2;url=thumbnails.php?album={$row['aid']}&page=1\">");
                msg_box($wl_name, $wl_msg_ups, $lang_back, 'javascript:history.back()');
                pagefooter();
                ob_end_flush();            
                exit;        
            }
            $neue_caption = substr($neue_caption,$wl_note_len);
            if (file_exists($wl_thumb) and !unlink($wl_thumb))
                die("Could not unlink: " . $wl_thumb);
            if (is_image($image) and !resize_image($image, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']))
				die("Could not resize: " . $image . " ->" . $thumb);
			$total_filesize = $row['filesize'] + (file_exists($normal) ? filesize($normal) : 0) + (file_exists($thumb) ? filesize($thumb) : 0);
        } else { // User -> set
            if (substr($row['caption'],0,$wl_note_len) == $wl_note) { // already taken by someone
                load_template();
                pageheader($wl_name, "<META http-equiv=\"refresh\" content=\"2;url=thumbnails.php?album={$row['aid']}&page=1\">");
                msg_box($wl_name, $wl_msg_already, $lang_back, 'javascript:history.back()');
                pagefooter();
                ob_end_flush();            
                exit;
            }
            $neue_caption = $wl_note . $row['caption'];
            if (!resize_image($wl_image, $wl_thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']))
                die("Could not Resize: " . $wl_image . " -> " . $wl_thumb);
			$total_filesize = $row['filesize'] + (file_exists($normal) ? filesize($normal) : 0) + (file_exists($wl_thumb) ? filesize($wl_thumb) : 0);
        }
        cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET total_filesize = '$total_filesize', caption = '$neue_caption' WHERE pid = '$pid' LIMIT 1");
        $CONFIG['site_url'] = rtrim($CONFIG['site_url'], '/');
        header( "refresh: 0; url=thumbnails.php?album={$row['aid']}&page=1" );
        die();
    }
}
?>
