<?php

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

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

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

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

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

							switch($action){

								case 'members':
									manage_members();
									break;

								case 'ban_member':
									ban_member();
									break;

								case 'unban_member':
									unban_member();
									break;

								case 'delete_member':
									do_delete_member();
									break;

								case 'edit_member':
									edit_member();
									break;

								case 'do_edit_member':
									do_edit_member();
									break;

								case 'edit_photo':
									edit_photo();
									break;

								case 'do_edit_photo':
									do_edit_photo();
									break;

								case 'delete_photo':
									do_delete_photo();
									break;

							}//switch

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

function manage_members(){

	admin_validate();

	$data["section"] = "manage";
	$data["subsection"] = 1;
	$data["title_id"] = "30";

	$search_link = "index.php?section=manage_members&action=members";

	$form = array (
		"search_for",
		"search_text",
		"sort_by",
		"sort_order",
		"only_banned",
		"only_with_photo",
		"unverified",
		"country",
		"state",
		"city",
		"gender",
		"age_from",
		"age_to"
	);

	foreach($form as $el){
		$values[$el] = $GLOBALS["Get"]->val($el);
		$search_link.= "&".$el."=".urlencode(stripslashes(html_entity_decode($values[$el])));
	}//foreach

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

	if(empty($values["gender"]))
		$values["gender"] = 0;

	$sql_query_total = "SELECT count(members.mem_id) ";
	$sql_query_result = "SELECT members.mem_id ";

	$sql_query="FROM members,profiles,network
				WHERE members.mem_id=profiles.mem_id
				AND members.mem_id=network.mem_id
				AND members.mem_id > 100000";

	if(!empty($values["search_for"]) && hs_strlen($values["search_text"]) > 0){
		$values["search_text"] = admin_replace_search_string($values["search_text"]);
		switch($values["search_for"]){
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND members.mem_id IN (".$values["search_text"].")";
				break;
			case 'email':
				$emails = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($emails as $email){
					$sql_query.= " members.email LIKE '".$email."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'fname':
				$fnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($fnames as $fname){
					$sql_query.= " members.fname LIKE '".$fname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
			case 'lname':
				$lnames = explode(",",$values["search_text"]);
				$sql_query.= " AND ( ";
				foreach($lnames as $lname){
					$sql_query.= " members.lname LIKE '".$lname."' OR ";
				}//foreach
				$sql_query = rtrim($sql_query,' OR ');
				$sql_query.= " ) ";
				break;
		}//switch
	}//if

	if($values["only_banned"])
		$sql_query.= " AND members.banned = '1' ";

	if($values["only_with_photo"])
		$sql_query.= " AND profiles.photo <> 'awaiting.gif' ";

	if(!$values["unverified"])
		$sql_query.= " AND members.verified = '1' ";

	if($values["country"])
		$sql_query.= " AND profiles.country = '".$values["country"]."' ";

	if($values["state"])
		$sql_query.= " AND profiles.state = '".$values["state"]."' ";

	if($values["city"])
		$sql_query.= " AND profiles.city = '".$values["city"]."' ";

	if($values["gender"])
		$sql_query.= " AND profiles.gender = '".$values["gender"]."' ";

	if($values["age_from"]){
		include_once(DOC_ROOT."/functions/adodb-time.php");
		$born_from = adodb_mktime(23,59,59,date("n"),date("j"),date("Y")-$values["age_from"]);
		$sql_query.= " AND profiles.birthdate <= ".$born_from." ";
	}//if

	if($values["age_to"]){
		include_once(DOC_ROOT."/functions/adodb-time.php");
		$born_to = adodb_mktime(23,59,59,date("n")-1,date("j"),date("Y")-$values["age_to"]-1);
		$sql_query.= " AND profiles.birthdate >= ".$born_to." ";
	}//if

	if(!empty($values["sort_by"])){
		switch($values["sort_by"]){
			case 'email':
				$sql_query.= " ORDER BY members.email ";
				break;
			case 'fname':
				$sql_query.= " ORDER BY members.fname ";
				break;
			case 'lname':
				$sql_query.= " ORDER BY members.lname ";
				break;
			case 'jdate':
				$sql_query.= " ORDER BY profiles.joined ";
				break;
			case 'activity':
				$sql_query.= " ORDER BY profiles.last_click ";
				break;
			case 'handshakes':
				$sql_query.= " ORDER BY LENGTH(network.friends) ";
				break;
			case 'mlevel':
				$sql_query.= " ORDER BY members.membership ";
				break;
		}//switch
		if(!empty($values["sort_order"])){
			$sql_query.= $values["sort_order"];
		}//if
	}//if

	$start = $p*30;

	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);

	$sql_query.= " LIMIT ".$start.",30";
	$result = $GLOBALS["DB"]->result($sql_query_result.$sql_query);

	$data["cards"] = admin_build_cards($result,$search_link."&p=".$p);
	$data["cards_total"] = count($result);

	$data["pages"] = handle_multipages($p,$total,$search_link,30);
	$data["values"] = $values;

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

	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_StatesPopulate(FormHandler,AllowEmpty){
		for(var k = FormHandler.state.options.length-1; k>=0; k--){
			FormHandler.state.options[k] = null;
		}//for
		for(var k = FormHandler.city.options.length-1; k>=0; k--){
			FormHandler.city.options[k] = null;
		}//for
		FormHandler.state.options[0] = new Option("...loading...");
		FormHandler.city.options[0] = new Option("...loading...");
		var Country = FormHandler.country.options[FormHandler.country.selectedIndex].value;
		var xmlHttpReq = false;
    	var self = this;
    	// Mozilla/Safari
    	if (window.XMLHttpRequest) {
        	self.xmlHttpReq = new XMLHttpRequest();
    	}//if
    	// IE
    	else if (window.ActiveXObject) {
        	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}//else if
    	self.xmlHttpReq.open('POST', "../index.php", true);
    	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.state,'state');
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.city,'city');
    	}//if
        }//function
    	self.xmlHttpReq.send("handler=geo&action=states&country="+Country+"&allow_empty="+AllowEmpty);
	}//HS_StatesPopulate
	function HS_CitiesPopulate(FormHandler,AllowEmpty){
		for(var k = FormHandler.city.options.length-1; k>=0; k--){
			FormHandler.city.options[k] = null;
		}//for
		FormHandler.city.options[0] = new Option("...loading...");
		var Country = FormHandler.country.options[FormHandler.country.selectedIndex].value;
		var State = FormHandler.state.options[FormHandler.state.selectedIndex].value;
		var xmlHttpReq = false;
    	var self = this;
    	// Mozilla/Safari
    	if (window.XMLHttpRequest) {
        	self.xmlHttpReq = new XMLHttpRequest();
    	}//if
    	// IE
    	else if (window.ActiveXObject) {
        	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}//else if
    	self.xmlHttpReq.open('POST', "../index.php", true);
    	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.city,'city');
    	}//if
        }//function
    	self.xmlHttpReq.send("handler=geo&action=cities&&country="+Country+"&state="+State+"&allow_empty="+AllowEmpty);
	}//HS_CitiesPopulate
	function HS_PullData(XMLData,FieldHandler,ItemName){
		for(var k = FieldHandler.options.length-1; k>=0; k--){
			FieldHandler.options[k] = null;
		}//for
		var Items = XMLData.getElementsByTagName(ItemName);
    	for(var i=0;i<Items.length;i++){
    		var ItemID = Items[i].firstChild.firstChild.nodeValue;
    		var ItemName = Items[i].firstChild.nextSibling.firstChild.nodeValue;
    		FieldHandler.options[i] = new Option(ItemName,ItemID);
    	}//for
	}//HS_PullData
//-->
</script>
HTMLTEXT;

	$layout[0] = "members_list";

	admin_output($layout,$data);

}//manage_members

function ban_member(){

	admin_validate();

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

	if(!empty($mem_id)){

	$sql_query="UPDATE members
				SET banned='1'
				WHERE mem_id";

	if(is_array($mem_id))
		$sql_query.= " IN (".implode(",",$mem_id).") ";
	else
		$sql_query.= "=".$mem_id;

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

	}//if

	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);
	load_page($ret_link);

}//ban_member

function unban_member(){

	admin_validate();

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

	if(!empty($mem_id)){

	$sql_query="UPDATE members
				SET banned='0'
				WHERE mem_id";

	if(is_array($mem_id))
		$sql_query.= " IN (".implode(",",$mem_id).") ";
	else
		$sql_query.= "=".$mem_id;

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

	}//if

	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);

	load_page($ret_link);

}//unban_member

function do_delete_member(){

	$admin = admin_validate();

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

	include_once(DOC_ROOT."/functions/delete_functions.php");

	if(is_array($mem_id) && count($mem_id) > 0){
		foreach($mem_id as $id){
			delete_member($id);
		}//foreach
	}//if
	elseif(!empty($mem_id))
		delete_member($mem_id);

	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);
	load_page($ret_link);

}//do_delete_member

function edit_member(){

	$data["section"] = "manage";
	$data["subsection"] = 1;
	$data["title_id"] = "30";
	$admin = admin_validate();
	$mem_id = $GLOBALS["Get"]->val("mem_id");
	$ret_link  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	$ret_link2 = "index.php?section=manage_members&action=edit_member&mem_id=".$mem_id."&ret_link=".urlencode($ret_link);

	$account_pages[] = "account_settings";
	$account_pages[] = "account_personalize";

	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/profile_sections.inc.php");

	foreach($profile_sections as $psection){
		$account_pages[] = "account_".$psection["section"];
	}//foreach

	$sql_query = "SELECT association,page FROM associations WHERE assoc_type='form'
				  AND theme='".$GLOBALS["THEME"]."' AND (0 ";
	foreach($account_pages as $acc_page){
		$sql_query.= " OR page = '".$acc_page."'";
	}//foreach
	$sql_query.= ")";

	$result = $GLOBALS["DB"]->result($sql_query);
	$forms = array();
	foreach($result as $res){
		$forms[$res["page"]][] = $res["association"];
	}//foreach

	reset($account_pages);
	$form_ars = array();
	foreach($account_pages as $page){
		if(is_array($forms[$page])){
		foreach($forms[$page] as $form){
			$form_ars[] = $form;
		}//foreach
		}//if
	}//foreach

	foreach($form_ars as $form_name){
		if($form_name != "account_change_password" && $form_name != "login")
			build_form($form_name,$mem_id);
	}//foreach

	$data["htmlareas"] = "";
	$forms = $GLOBALS["forms"];
	foreach($forms as $form){
		while(list($key,$val) = each($form["types"])){
			if($val == "htmlarea"){
				$data["htmlareas"].= "html_".$form["names"][$key].",";
			}//if
		}//while
	}//foreach

	$sql_query="SELECT *
				FROM photos
				WHERE mem_id=".$mem_id;

	$result = $GLOBALS["DB"]->result($sql_query);
	$photos = array();
	foreach($result as $res){
		$photo["photo"]   = $res["photo_small"];
		$photo["caption"] = hs_wordwrap($res["caption"],50);
		$photo["pho_id"]  = $res["pho_id"];
		$photo["mem_id"]  = $res["mem_id"];
		$photo["delete"]  = "index.php?section=manage_photos&action=delete&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link2);
		$photo["edit"]    = "javascript:HS_Popup('index.php?section=manage_members&action=edit_photo&pho_id=".$res["pho_id"]."','width=400,height=400,scrollbars=no,menubar=no,resizable=no,location=no')";
		$photo["approve"] = "index.php?section=manage_photos&action=approve&pho_id=".$res["pho_id"]."&ret_link=".urlencode($ret_link2);
		$photos[] = $photo;
	}//foreach

	$data["photos"] = $photos;

	$data["values"]["mem_id"] = $mem_id;
	$data["link"] = $ret_link;

	$member_info = $GLOBALS["Member"]->info($mem_id);
	$getpaid_packages = admin_get_getpaid_packages();

	if(count($getpaid_packages)){

	$data["packages"][0] = "Awaiting for payment";
	while(list($key,$val) = each($getpaid_packages)){
		$data["packages"][$key] = $val;
	}//while

	if(!$member_info["access_end"] && $member_info["membership"])
		$data["access_end"][type] = "unlimited";
	elseif($member_info["membership"]){
		$data["access_end"][type] = "date";
		$data["access_end"][date] = date("d/m/Y",$member_info["access_end"]);
	}//else

	$data["member_info"] = $member_info;

	}//if

	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_StatesPopulate(FormHandler,AllowEmpty){
		for(var k = FormHandler.state.options.length-1; k>=0; k--){
			FormHandler.state.options[k] = null;
		}//for
		for(var k = FormHandler.city.options.length-1; k>=0; k--){
			FormHandler.city.options[k] = null;
		}//for
		FormHandler.state.options[0] = new Option("...loading...");
		FormHandler.city.options[0] = new Option("...loading...");
		var Country = FormHandler.country.options[FormHandler.country.selectedIndex].value;
		var xmlHttpReq = false;
    	var self = this;
    	// Mozilla/Safari
    	if (window.XMLHttpRequest) {
        	self.xmlHttpReq = new XMLHttpRequest();
    	}//if
    	// IE
    	else if (window.ActiveXObject) {
        	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}//else if
    	self.xmlHttpReq.open('POST', "../index.php", true);
    	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.state,'state');
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.city,'city');
    	}//if
        }//function
    	self.xmlHttpReq.send("handler=geo&action=states&country="+Country+"&allow_empty="+AllowEmpty);
	}//HS_StatesPopulate
	function HS_CitiesPopulate(FormHandler,AllowEmpty){
		for(var k = FormHandler.city.options.length-1; k>=0; k--){
			FormHandler.city.options[k] = null;
		}//for
		FormHandler.city.options[0] = new Option("...loading...");
		var Country = FormHandler.country.options[FormHandler.country.selectedIndex].value;
		var State = FormHandler.state.options[FormHandler.state.selectedIndex].value;
		var xmlHttpReq = false;
    	var self = this;
    	// Mozilla/Safari
    	if (window.XMLHttpRequest) {
        	self.xmlHttpReq = new XMLHttpRequest();
    	}//if
    	// IE
    	else if (window.ActiveXObject) {
        	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    	}//else if
    	self.xmlHttpReq.open('POST', "../index.php", true);
    	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
	        HS_PullData(self.xmlHttpReq.responseXML,FormHandler.city,'city');
    	}//if
        }//function
    	self.xmlHttpReq.send("handler=geo&action=cities&&country="+Country+"&state="+State+"&allow_empty="+AllowEmpty);
	}//HS_CitiesPopulate
	function HS_PullData(XMLData,FieldHandler,ItemName){
		for(var k = FieldHandler.options.length-1; k>=0; k--){
			FieldHandler.options[k] = null;
		}//for
		var Items = XMLData.getElementsByTagName(ItemName);
    	for(var i=0;i<Items.length;i++){
    		var ItemID = Items[i].firstChild.firstChild.nodeValue;
    		var ItemName = Items[i].firstChild.nextSibling.firstChild.nodeValue;
    		FieldHandler.options[i] = new Option(ItemName,ItemID);
    	}//for
	}//HS_PullData
//-->
</script>
HTMLTEXT;

	$layout[0] = "edit_member";
	admin_output($layout,$data);

}//edit_member

function do_edit_member(){

	$admin = admin_validate();
	$mem_id = $GLOBALS["Get"]->val("mem_id");
	$ret_link = $GLOBALS["Get"]->val("ret_link");

	$account_pages[] = "account_settings";
	$account_pages[] = "account_personalize";

	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/profile_sections.inc.php");

	foreach($profile_sections as $psection){
		$account_pages[] = "account_".$psection["section"];
	}//foreach

	$sql_query = "SELECT association FROM associations WHERE assoc_type='form'
				  AND theme='".$GLOBALS["THEME"]."' AND (0 ";
	foreach($account_pages as $acc_page){
		$sql_query.= " OR page = '".$acc_page."'";
	}//foreach
	$sql_query.= ")";

	$result = $GLOBALS["DB"]->result($sql_query);
	$form_ars = array();
	foreach($result as $res){
		$form_ars[] = $res["association"];
	}//foreach

	include_once(DOC_ROOT."/functions/form_functions.php");

	foreach($form_ars as $form_name){
		if($form_name != "account_change_password" && $form_name != "login"){

			$values = form_handle_input($form_name,0,1);

			if(!extract_null_value($values["err_id"])){
				$GLOBALS["uerr_id"] = $values["err_id"];
				edit_member();
				exit;
			}//if

			if($form_name == "account_basics"){
				//building member's geo information
				$geo = build_geo_position($values["country"],$values["state"],$values["city"],$values["postal_code"]);
				$values["latitude"]    = $geo["latitude"];
				$values["longitude"]   = $geo["longitude"];
				$values["state"]       = $geo["state"];
				$values["city"]        = $geo["city"];
			}//if

			if($form_name == "account_personalize"){
				$info = $GLOBALS["Member"]->info($mem_id);
				include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/modules/profiles.inc.php");
				if($info["folder"] != $values["folder"]){
					if(file_exists($profiles["path"]."/".$values["folder"]) && is_dir($profiles["path"]."/".$values["folder"]))
						$exists = 1;
					else
						$exists = 0;
					if($exists){
						$GLOBALS["uerr_id"] = 24;
						edit_member();
						exit;
					}//if
					$folder1 = $profiles["path"]."/".$info["folder"];
					$folder2 = $profiles["path"]."/".$values["folder"];
					rename($folder1,$folder2);
					$info["folder"] = $values["folder"];
				}//if
			}//if

			load_db_update($form_name,$values,array("mem_id",$mem_id));
		}//if
	}//foreach

	if(admin_getpaid_isenabled()){

	$membership  = $GLOBALS["Get"]->val("membership");
	$access_type = $GLOBALS["Get"]->val("access_type");
	$access_end  = $GLOBALS["Get"]->val("access_end");

	if($access_type == "date"){
		$date = explode("/",$access_end);
		$access_end_value = mktime(0,0,0,$date[1],$date[0],$date[2]);
	}//if
	else
		$access_end_value = 0;

	$sql_query="UPDATE members
				SET membership='".$membership."',access_end=".$access_end_value."
				WHERE mem_id=".$mem_id;

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

	}//if

	load_page("index.php?section=manage_members&action=edit_member&mem_id=".$mem_id."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));

}//do_edit_member

function edit_photo(){

	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");

	$sql_query="SELECT photo,caption
				FROM photos
				WHERE pho_id=".$pho_id;

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

	$data["image"] = $result["photo"];
	$data["caption"] = $result["caption"];
	$data["pho_id"] = $pho_id;
	$data["section"] = "manage_members";
	$data["action"] = "do_edit_photo";
	$data["title_id"] = "31";
	$layout[0] = "edit_photo";
	admin_popup_output($layout,$data);

}//edit_photo

function do_edit_photo(){

	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");
	$caption = $GLOBALS["Get"]->val("caption");

	$sql_query="UPDATE photos
				SET caption=':0:'
				WHERE pho_id=".$pho_id;

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

	admin_popup_close(0);

}//do_edit_photo

function do_delete_photo(){

	$admin = admin_validate();
	$pho_id = $GLOBALS["Get"]->val("pho_id");
	$ret_link = $GLOBALS["Get"]->val("ret_link");

	include_once(DOC_ROOT."/functions/delete_functions.php");

	$mem_id = delete_photo($pho_id);

	load_page("index.php?section=manage_members&action=edit_member&mem_id=".$mem_id."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));

}//do_delete_photo

?>