<?php

						/*************************************\

						    Handshakes Professional (3.5)
						            by DZOIC LTD
						       Copyright(c) 2005-2008
						        http://www.dzoic.com

						\*************************************/

						///////////////////////////////////////

						admin_validate_section("mailbox",1);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case 'messages':
									messages();
									break;

								case 'feedback':
									feedbacks();
									break;

								case 'reports':
									reports();
									break;

								case 'sent':
									sent();
									break;

								case 'saved':
									saved();
									break;

								case 'view':
									view_message();
									break;

								case 'view_sent':
									view_sent_message();
									break;

								case 'send':
									message_send();
									break;

								case 'do_send':
									do_send_message();
									break;

								case 'save':
									save_message();
									break;

								case 'delete':
									delete_message();
									break;

								case 'delete_all':
									delete_all_messages();
									break;

							}//switch

						///////////////////////////////////////

function messages(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 1;
	$data["title_id"] = "22";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query = "SELECT COUNT(*) FROM admin_mail WHERE message_type='message' AND mailbox='inbox'";
	$total = $GLOBALS["DB"]->single($sql_query);
	$data["link"] = "index.php?section=mailbox&action=messages";

	$pages = handle_multipages($p,$total,$data["link"],30);
	$start = $p*30;

	$data["link"].= "&p=".$p;

	$sql_query = "SELECT * FROM admin_mail WHERE message_type='message' AND mailbox='inbox'
				  ORDER BY date DESC
				  LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query);

	$data["messages"] = array();

	foreach($result as $res){
		$message["mes_id"]  = $res["mes_id"];
		$message["per_id"]  = $res["per_id"];
		$message["subject"] = (hs_strlen($res["subject"]) > 48) ? hs_substr($res["subject"],0,50) : $res["subject"];
		$message["message"] = nl2br($res["message"]);
		$message["date"]    = extract_time($res["date"],1);
		$message["sender"]  = $GLOBALS["Network"]->card($message["per_id"]);
		$message["viewed"]  = $res["viewed"];
		$message["view"]    = "index.php?section=mailbox&action=view&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["save"]    = "index.php?section=mailbox&action=save&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["reply"]   = "index.php?section=mailbox&action=send&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["delete"]  = "index.php?section=mailbox&action=delete&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$data["messages"][] = $message;
	}//foreach

	$data["pages"] = $pages;

	$layout[0] = "mailbox_messages";

	admin_output($layout,$data);

}//messages

function saved(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 5;
	$data["title_id"] = "22";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query = "SELECT COUNT(*) FROM admin_mail WHERE mailbox='saved'";
	$total = $GLOBALS["DB"]->single($sql_query);
	$data["link"] = "index.php?section=mailbox&action=saved";

	$pages = handle_multipages($p,$total,$data["link"],30);
	$start = $p*30;

	$data["link"].= "&p=".$p;

	$sql_query = "SELECT * FROM admin_mail WHERE mailbox='saved'
				  ORDER BY date DESC
				  LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query);

	$data["messages"] = array();

	foreach($result as $res){
		$message["mes_id"]  = $res["mes_id"];
		$message["per_id"]  = $res["per_id"];
		$message["subject"] = (hs_strlen($res["subject"]) > 48) ? hs_substr($res["subject"],0,50) : $res["subject"];
		$message["message"] = nl2br($res["message"]);
		$message["date"]    = extract_time($res["date"],1);
		$message["sender"]  = $GLOBALS["Network"]->card($message["per_id"]);
		$message["viewed"]  = $res["viewed"];
		$message["view"]    = "index.php?section=mailbox&action=view&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["reply"]   = "index.php?section=mailbox&action=send&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["delete"]  = "index.php?section=mailbox&action=delete&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$data["messages"][] = $message;
	}//foreach

	$data["pages"] = $pages;

	$layout[0] = "mailbox_saved";

	admin_output($layout,$data);

}//saved

function sent(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 4;
	$data["title_id"] = "22";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query = "SELECT COUNT(*) FROM admin_mail WHERE mailbox='sent'";
	$total = $GLOBALS["DB"]->single($sql_query);
	$data["link"] = "index.php?section=mailbox&action=sent";

	$pages = handle_multipages($p,$total,$data["link"],30);
	$start = $p*30;

	$data["link"].= "&p=".$p;

	$sql_query = "SELECT * FROM admin_mail WHERE mailbox='sent'
				  ORDER BY date DESC
				  LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query);

	$data["messages"] = array();

	foreach($result as $res){
		$message["mes_id"]  = $res["mes_id"];
		$message["per_id"]  = $res["per_id"];
		$message["subject"] = (hs_strlen($res["subject"]) > 48) ? hs_substr($res["subject"],0,50) : $res["subject"];
		$message["message"] = nl2br($res["message"]);
		$message["date"]    = extract_time($res["date"],1);
		$message["viewed"]  = $res["viewed"];
		$message["view"]    = "index.php?section=mailbox&action=view_sent&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["delete"]  = "index.php?section=mailbox&action=delete&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$recipients = handle_empties(explode(",",$res["special"]));
		if($recipients[0] == "all")
			$message["recipients"] = "All Members";
		else{
			$message["recipients"] = "";
			$count = 1;
			foreach($recipients as $rec){
				$message["recipients"].= "<a href='".build_link("profile",$rec)."' target='_blank'>".$GLOBALS["Network"]->network_name($rec)."</a>"." (ID: ".$rec.")<br>";
				if($count == 5){
					$message["recipients"].= "...";
					break;
				}//if
				$count++;
			}//foreach
		}//else
		$data["messages"][] = $message;
	}//foreach

	$data["pages"] = $pages;

	$layout[0] = "mailbox_sent";

	admin_output($layout,$data);

}//sent

function feedbacks(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 2;
	$data["title_id"] = "22";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query = "SELECT COUNT(*) FROM admin_mail WHERE message_type='feedback' AND mailbox='inbox'";
	$total = $GLOBALS["DB"]->single($sql_query);
	$data["link"] = "index.php?section=mailbox&action=feedback";

	$pages = handle_multipages($p,$total,$data["link"],30);
	$start = $p*30;

	$data["link"].= "&p=".$p;

	$sql_query = "SELECT * FROM admin_mail WHERE message_type='feedback' AND mailbox='inbox'
				  ORDER BY date DESC
				  LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query);

	$data["messages"] = array();

	foreach($result as $res){
		$message["mes_id"]  = $res["mes_id"];
		$message["per_id"]  = $res["per_id"];
		$message["subject"] = (hs_strlen($res["subject"]) > 48) ? hs_substr($res["subject"],0,50) : $res["subject"];
		$message["message"] = nl2br($res["message"]);
		$message["date"]    = extract_time($res["date"],1);
		$message["sender"]  = $GLOBALS["Network"]->card($message["per_id"]);
		$message["viewed"]  = $res["viewed"];
		$message["view"]    = "index.php?section=mailbox&action=view&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["save"]    = "index.php?section=mailbox&action=save&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["reply"]   = "index.php?section=mailbox&action=send&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$message["delete"]  = "index.php?section=mailbox&action=delete&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$data["messages"][] = $message;
	}//foreach

	$data["pages"] = $pages;

	$layout[0] = "mailbox_feedback";

	admin_output($layout,$data);

}//feedbacks

function reports(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 3;
	$data["title_id"] = "22";
	$p = $GLOBALS["Get"]->val("p");

	$sql_query = "SELECT COUNT(*) FROM admin_mail WHERE message_type!='feedback' AND message_type!='message' AND mailbox='inbox'";
	$total = $GLOBALS["DB"]->single($sql_query);
	$data["link"] = "index.php?section=mailbox&action=reports";

	$pages = handle_multipages($p,$total,$data["link"],30);
	$start = $p*30;

	$data["link"].= "&p=".$p;

	$sql_query = "SELECT * FROM admin_mail WHERE message_type!='feedback' AND message_type!='message' AND mailbox='inbox'
				  ORDER BY message_type,date DESC
				  LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query);

	$data["messages"] = array();

	foreach($result as $res){
		$message["mes_id"]  = $res["mes_id"];
		$message["per_id"]  = $res["per_id"];
		$message["report_type"] = report_type($res["message_type"]);
		$message["report_subject"] = report_subject($res["special"]);
		$message["date"]    = extract_time($res["date"],1);
		$message["sender"]  = $GLOBALS["Network"]->card($message["per_id"]);
		$message["viewed"]  = $res["viewed"];
		$message["delete"]  = "index.php?section=mailbox&action=delete&mes_id=".$res["mes_id"]."&ret_link=".urlencode($data["link"]);
		$data["messages"][] = $message;
	}//foreach

	$data["pages"] = $pages;

	$layout[0] = "mailbox_reports";

	admin_output($layout,$data);

}//reports

function report_type($type){

	switch($type){
		case 'best_of':
			return "Best Of";
		case 'spam':
			return 'Spam';
		case 'mature':
			return 'Mature';
		case 'mis-categorized':
			return 'Mis-Categorized';
	}//switch

}//report_type

function report_subject($special){

	include_once(DOC_ROOT."/classes/Clubs.class.php");
	include_once(DOC_ROOT."/classes/Blogs.class.php");
	include_once(DOC_ROOT."/classes/Classifieds.class.php");
	include_once(DOC_ROOT."/classes/Events.class.php");

	$details = explode("|",$special);

	switch($details[0]){
		case 'blog':
			return "Blog: <a target='_blank' href='".build_link("blog",$details[1])."'>".$GLOBALS["Blogs"]->name($details[1])."</a><br>(ID: ".$details[1].")";
		case 'classified_ad':
			return "Classified Ad: <a target='_blank' href='".build_link("classified_ad",$details[1])."'>".$GLOBALS["Classifieds"]->name($details[1])."</a><br>(ID: ".$details[1].")";
		case 'event':
			return "Event: <a target='_blank' href='".build_link("event",$details[1])."'>".$GLOBALS["Events"]->name($details[1])."</a><br>(ID: ".$details[1].")";
		case 'club':
			return "Club: <a target='_blank' href='".build_link("club",$details[1])."'>".$GLOBALS["Clubs"]->name($details[1])."</a><br>(ID: ".$details[1].")";
	}//switch

}//report_subject

function save_message(){

	admin_validate();

	$mes_id = $GLOBALS["Get"]->val("mes_id");
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	if(is_array($mes_id) && count($mes_id)){
		$sql_query = "UPDATE admin_mail SET mailbox='saved' WHERE mes_id IN (".implode(",",$mes_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($mes_id){
		$sql_query = "UPDATE admin_mail SET mailbox='saved' WHERE mes_id=".$mes_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif

	load_page($ret_link);

}//save_message

function delete_message(){

	admin_validate();

	$mes_id = $GLOBALS["Get"]->val("mes_id");
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	if(is_array($mes_id) && count($mes_id)){
		$sql_query = "DELETE FROM admin_mail WHERE mes_id IN (".implode(",",$mes_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif($mes_id){
		$sql_query = "DELETE FROM admin_mail WHERE mes_id=".$mes_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//elseif

	load_page($ret_link);

}//delete_message

function delete_all_messages(){

	admin_validate();

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	$pro = $GLOBALS["Get"]->val("pro");

	$sql_query = "DELETE FROM admin_mail WHERE ";

	switch($pro){
		case 'message':
			$sql_query.= "message_type='message' AND mailbox='inbox'";
			break;
		case 'feedback':
			$sql_query.= "message_type='feedback' AND mailbox='inbox'";
			break;
		case 'reports':
			$sql_query.= "message_type!='message' AND message_type!='feedback'";
			break;
		case 'sent':
			$sql_query.= "mailbox='sent'";
			break;
		case 'saved':
			$sql_query.= "mailbox='saved'";
			break;
	}//switch

	$GLOBALS["DB"]->execute($sql_query);

	load_page($ret_link);

}//delete_all_messages

function view_message(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 0;
	$data["title_id"] = "22";
	$mes_id = $GLOBALS["Get"]->type_val("mes_id","integer");
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	$sql_query = "SELECT * FROM admin_mail WHERE mes_id=".$mes_id;
	$message = $GLOBALS["DB"]->row($sql_query);

	$data["mes_id"]  = $mes_id;
	$data["date"]    = extract_time($message["date"],1);
	$data["subject"] = $message["subject"];

	switch ($message["type"]){
        case 'text':
            $data["message"] = nl2br($message["message"]);
            break;
        case 'audio':
            $data["message"] = '<script type="text/javascript">
                        AC_FL_RunContent( "codebase","http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0",
                                "width","22",
                                "height","21",
                                "src","/swf/hsaplayer",
                                "quality","high",
                                "allowScriptAccess","always",
                                "bgcolor","#ffffff",
                                "FlashVars","mem_id='.$message["per_id"].'&MainURL='.extract_mainurl().'&hash='.$message["message"].'",
                                "pluginspage","http://www.macromedia.com/go/getflashplayer",
                                "movie","/swf/hsaplayer" );
                        </script>';
            break;
        case 'video':
            $data["message"] = '<br><script type="text/javascript">
                        AC_FL_RunContent( "codebase","http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0",
                                "width","330",
                                "height","271",
                                "src","/swf/player",
                                "quality","high",
                                "allowScriptAccess","always",
                                "bgcolor","#ffffff",
                                "FlashVars","mem_id='.$message["per_id"].'&MainURL='.extract_mainurl().'&hash='.$message["message"].'",
                                "pluginspage","http://www.macromedia.com/go/getflashplayer",
                                "movie","/swf/player" );
                        </script>';
            break;
        default:
            $data["message"] = nl2br($message["message"]);
    }

	$data["sender"]  = $GLOBALS["Network"]->card($message["per_id"]);
	$data["link"]    = $ret_link;
	$data["per_id"]  = $message["per_id"];
	$data["saved"]   = ($message["mailbox"] == "saved") ? 1 : 0;

	if($message["mailbox"] == "saved")
		$data["ret_name"] = "Mailbox - Saved";
	elseif($message["message_type"] == "message")
		$data["ret_name"] = "Mailbox - Messages";
	elseif($message["message_type"] == "feedback")
		$data["ret_name"] = "Mailbox - Feedback";

	$sql_query = "UPDATE admin_mail SET viewed='1' WHERE mes_id=".$mes_id;
	$GLOBALS["DB"]->execute($sql_query);

	$layout[0] = "mailbox_view_message";

	admin_output($layout,$data);

}//view_message

function view_sent_message(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 0;
	$data["title_id"] = "22";
	$mes_id = $GLOBALS["Get"]->type_val("mes_id","integer");
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	$sql_query = "SELECT * FROM admin_mail WHERE mes_id=".$mes_id;
	$message = $GLOBALS["DB"]->row($sql_query);

	$data["mes_id"]  = $mes_id;
	$data["date"]    = extract_time($message["date"],1);
	$data["subject"] = $message["subject"];
	$data["message"] = nl2br($message["message"]);
	$data["link"]    = $ret_link;

	$recipients = handle_empties(explode(",",$message["special"]));
	if($recipients[0] == "all")
		$data["recipients"] = "All Members";
	else{
		$data["recipients"] = "";
		foreach($recipients as $rec){
			$data["recipients"].= "<a href='".build_link("profile",$rec)."' target='_blank'>".$GLOBALS["Network"]->network_name($rec)."</a>"." (ID: ".$rec.")<br>";
		}//foreach
	}//else

	$data["ret_name"] = "Mailbox - Sent";

	$layout[0] = "mailbox_view_sent_message";

	admin_output($layout,$data);

}//view_sent_message

function message_send(){

	admin_validate();

	$data["section"] = "mailbox";
	$data["subsection"] = 0;
	$data["title_id"] = "22";
	$mes_id = $GLOBALS["Get"]->type_val("mes_id","integer");
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	$data["link"] = $ret_link;

	$data["subject"] = $GLOBALS["Get"]->val("subject");
	$data["message"] = $GLOBALS["Get"]->val("message");
	$data["send_to"] = $GLOBALS["Get"]->val("send_to");

	if(!$data["send_to"])
		$data["send_to"] = "external";

	$mem_id = $GLOBALS["Get"]->val("mem_id");
	$pro    = $GLOBALS["Get"]->val("pro");
	if($pro == "all")
		$mem_id = "all";

	if($mes_id){
		$reply = make_reply($mes_id);
		if(empty($data["subject"]))
			$data["subject"] = $reply["subject"];
		if(empty($data["message"]))
			$data["message"] = $reply["message"];

		$mem_id = $reply["mem_id"];
	}//if

	if(is_array($mem_id) && count($mem_id)){
		$data["mem_id"] = $mem_id;
		foreach($mem_id as $mem){
			$data["recipients"].= "<a href='".build_link("profile",$mem)."' target='_blank'>".$GLOBALS["Network"]->network_name($mem)."</a><br>";
		}//foreach
	}//if
	elseif($mem_id){
		$data["mem_id"][] = $mem_id;
		if($mem_id != "all"){
			$data["recipients"] = $GLOBALS["Network"]->network_name($mem_id);
		}//if
		else
			$data["recipients"] = "All Members";
	}//else

	if(!count($data["mem_id"])){
		load_page($data["link"]."&err_id=2");
	}//if

	$layout[0] = "mailbox_send";

	admin_output($layout,$data);

}//message_send

function do_send_message(){

	set_time_limit(0);

	$info = admin_validate();

	$vals = array("mem_id","send_to","subject","message");
	$values = $GLOBALS["Get"]->arr($vals,1);

	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));

	if(!count($values["mem_id"])){
		load_page($ret_link."&err_id=2");
	}//if

	if(empty($values["send_to"]) || empty($values["subject"]) || empty($values["message"])){
		$GLOBALS["aerr_id"] = 10;
		message_send();
		exit;
	}//if

	include_once(DOC_ROOT."/classes/Mail.class.php");

	if($values["send_to"] == "internal" || $values["send_to"] == "both"){
		$mail_details["sender"]  = $info["mem_id"];
		$mail_details["subject"] = $values["subject"];
		$mail_details["body"]    = $values["message"];
		if($values["mem_id"][0] == "all"){
			$sql_query = "SELECT mem_id FROM members WHERE verified='1'";
			$result = $GLOBALS["DB"]->execute($sql_query);
			while($get = mysql_fetch_assoc($result)){
				$GLOBALS["Mail"]->details = $mail_details;
				$GLOBALS["Mail"]->details[recipient] = $get["mem_id"];
				$GLOBALS["Mail"]->send_internal();
			}//while
		}//if
		else{
			foreach($values["mem_id"] as $mem_id){
				$GLOBALS["Mail"]->details = $mail_details;
				$GLOBALS["Mail"]->details[recipient] = $mem_id;
				$GLOBALS["Mail"]->send_internal();
			}//foreach
		}//else
	}//if

	if($values["send_to"] == "external" || $values["send_to"] == "both"){
		$mail_details["sender"]  = "Admin";
		$mail_details["subject"] = stripslashes(html_entity_decode($values["subject"]));
		$mail_details["body"]    = stripslashes(html_entity_decode($values["message"]));
		if($values["mem_id"][0] == "all"){
			$sql_query = "SELECT mem_id FROM members WHERE verified='1'";
			$result = $GLOBALS["DB"]->execute($sql_query);
			while($get = mysql_fetch_assoc($result)){
				$GLOBALS["Mail"]->details = $mail_details;
				$GLOBALS["Mail"]->details["recipient"] = $get["mem_id"];
				$GLOBALS["Mail"]->send_mass();
			}//while
		}//if
		else{
			foreach($values["mem_id"] as $mem_id){
				$GLOBALS["Mail"]->details = $mail_details;
				$GLOBALS["Mail"]->details["recipient"] = $mem_id;
				$GLOBALS["Mail"]->send_mass();
			}//foreach
		}//else
	}//if

	$sql_query = "INSERT INTO admin_mail (subject,message,message_type,mailbox,viewed,date,special)
				  VALUES (':subject:',':message:','message','sent','1',:time:,':special:')";

	$dbvals["subject"] = $values["subject"];
	$dbvals["message"] = $values["message"];
	$dbvals["time"]    = time();
	$dbvals["special"] = implode(",",$values["mem_id"]);

	$GLOBALS["DB"]->values = $dbvals;
	$GLOBALS["DB"]->execute($sql_query);

	load_page($ret_link);

}//do_send_message

function make_reply($mes_id){

	$sql_query="SELECT per_id,subject,message
				FROM admin_mail
				WHERE mes_id=".$mes_id;

	$original = $GLOBALS["DB"]->row($sql_query);

	$subject = $GLOBALS["language"]["re"]." ".$original["subject"];

	$message = hs_ereg_replace("\n","\n> ",strip_tags($original["message"]));
	$message = "\n\n\n\n> ".$message;
	$message = hs_wordwrap($message, 55, "\n> ");

	return array("mem_id" => $original["per_id"], "subject" => $subject, "message" => $message);

}//make_reply_forward

?>