<?php

define('IN_COPPERMINE', true);
define('SUBSCRIBE_PHP', true); //language file
require('include/init.inc.php');
$query = (explode('&', $_SERVER['QUERY_STRING'], 2));
($query[1]) ? $referer="displayimage.php?".$query[1] : $referer="index.php";
 if (isset($_GET['subscribe']))
{
	if ((!USER_ID) || (!$CONFIG['enable_user_notification'])) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

	$picture_id = intval($_GET['subscribe']);
	$result = cpg_db_query("SELECT 1 FROM {$CONFIG['TABLE_PICTURES']} WHERE  pid='".$picture_id."'");
	if (!mysql_fetch_row($result)) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);

	$result = cpg_db_query("SELECT 1 FROM {$CONFIG['TABLE_NOTIFY']} WHERE user_id='".(USER_ID)."' AND picture_id='".$picture_id."'");
	if (mysql_fetch_row($result)){
		pageheader($lang_subscribe['sub_already'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
        msg_box($lang_subscribe['sub'], $lang_subscribe['sub_already'], $lang_continue, $referer);
        pagefooter();
	}
	else {
	cpg_db_query("INSERT INTO {$CONFIG['TABLE_NOTIFY']} (user_id, picture_id) VALUES('".(USER_ID)."' ,'".$picture_id."')");
	pageheader($lang_subscribe['sub_succ'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
    msg_box($lang_subscribe['sub'], $lang_subscribe['sub_succ'] , $lang_continue, $referer);
    pagefooter();
	}
}


else if (isset($_GET['unsubscribe']))
{
	if ((!USER_ID) || (!$CONFIG['enable_user_notification'])) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

	$picture_id = intval($_GET['unsubscribe']);
	$result = cpg_db_query("SELECT 1 FROM {$CONFIG['TABLE_PICTURES']} WHERE  pid='".$picture_id."'");
	if (!mysql_fetch_row($result)) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);

	$result = cpg_db_query("SELECT 1 FROM {$CONFIG['TABLE_NOTIFY']} WHERE user_id='".(USER_ID)."' AND picture_id='".$picture_id."'");
	if (!mysql_fetch_row($result)){
		pageheader($lang_subscribe['sub_not'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
        msg_box($lang_subscribe['sub'], $lang_subscribe['sub_not'], $lang_continue, $referer);
        pagefooter();
	}
	else{
		pageheader($lang_subscribe['sub_removed'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
        msg_box($lang_subscribe['sub'], $lang_subscribe['sub_removed'], $lang_continue, $referer);
        pagefooter();
		cpg_db_query("DELETE FROM {$CONFIG['TABLE_NOTIFY']} WHERE user_id='".(USER_ID)."' AND picture_id='".$picture_id."'");
	}
}


else
	cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
?>