<?php

						/*************************************\
						
						    Handshakes Professional (3.3)
						            by DZOIC LTD
						       Copyright(c) 2005-2007
						        http://www.dzoic.com
						
						\*************************************/
						
						include_once(DOC_ROOT."/classes/Blogs.class.php");

						///////////////////////////////////////
						
						admin_validate_section("manage",2);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':
									manage_blogs();
									break;
									
								case 'delete':
									do_delete_blogs();
									break;
									
								case 'edit':
									edit_blog();
									break;
									
								case 'save':
									save_blog();
									break;
									
								case 'featured_add':
									featured_add_blog();
									break;
									
								case 'featured_remove':
									featured_remove_blog();
									break;
								
								case 'edit_post':
									edit_blog_post();
									break;
									
								case 'save_post':
									save_blog_post();
									break;
									
								case 'delete_post':
									delete_blog_post();
									
								case 'delete_comment':
									delete_blog_comment();
									
							}//switch
							
						///////////////////////////////////////
						
function manage_blogs(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 2;
	$data["title_id"] = "23";	
	$p = $GLOBALS["Get"]->val("p");
	
	$sql_query_total = "SELECT COUNT(blg_id) ";
	$sql_query_result = "SELECT * ";
	
	$sql_query = "FROM blogs,members
				  WHERE blogs.mem_id=members.mem_id
				  AND blogs.blg_id > 100000";
	
	$valnames = array("search_text","search_for","sort_by","sort_order");
	
	$values = $GLOBALS["Get"]->arr($valnames,1);
	
	if(!empty($values["search_text"]) && !empty($values["search_for"])){
		
		switch($values["search_for"]){
			case 'blg_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND blg_id=".$values["search_text"];
				break;
			case 'title':
				$sql_query.= " AND title LIKE '%".$values["search_text"]."%' ";
				break;
			case 'description':
				$sql_query.= " AND description LIKE '%".$values["search_text"]."%' ";
				break;
			case 'mem_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND blogs.mem_id=".$values["search_text"];
				break;
			case 'fname':
				$sql_query.= " AND members.fname LIKE '".$values["search_text"]."%' ";
				break;
			case 'lname':
				$sql_query.= " AND members.lname LIKE '".$values["search_text"]."%' ";
				break;
			case 'email':
				$sql_query.= " AND members.email LIKE '".$values["search_text"]."%' ";
				break;
		}//switch
		
	}//if
	
	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY posted DESC ";
			break;
		case 'title':
			$sql_query.= " ORDER BY title ".$values["sort_order"];
			break;
		case 'blg_id':
			$sql_query.= " ORDER BY blg_id ".$values["sort_order"];
			break;
		case 'posted':
			$sql_query.= " ORDER BY posted ".$values["sort_order"];
			break;
		case 'featured':
			$sql_query.= " ORDER BY featured ".$values["sort_order"];
			break;
	}//switch
	
	$data["values"] = $values;
	$data["blogs"] = array();
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
	
	$sql_query.= " LIMIT ".$start.",30";
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$search_link = "index.php?section=manage_blogs";	
	while(list($key,$val) = each($values)){
		$search_link.= "&".$key."=".urlencode(stripslashes(html_entity_decode($val)));
	}//while
	$data["pages"] = handle_multipages($p,$total,$search_link,30);
	
	$data["link"] = $search_link."&p=".$p;
	
	foreach($results as $result){
		if(hs_strlen($result["title"] > 30))
			$result["title"] = hs_substr($result["title"],0,27)."...";
			
		$blog["id"] = $result["blg_id"];
		$blog["title"] = $result["title"];
		$blog["author"] = implode(" ",$GLOBALS["Member"]->name($result["mem_id"]));
		$blog["date"] = extract_date($result["posted"]);
		$blog["view"] = build_link("blog",$result["blg_id"]);
		$blog["edit"] = "index.php?section=manage_blogs&action=edit&blg_id=".$result["blg_id"]."&ret_link=".urlencode($data["link"]);
		$blog["delete"] = "index.php?section=manage_blogs&action=delete&blg_id=".$result["blg_id"]."&ret_link=".urlencode($data["link"]);
		$blog["featured"] = $result["featured"];
		$blog["featured_add"] = "index.php?section=manage_blogs&action=featured_add&blg_id=".$result["blg_id"]."&ret_link=".urlencode($data["link"]);
		$blog["featured_remove"] = "index.php?section=manage_blogs&action=featured_remove&blg_id=".$result["blg_id"]."&ret_link=".urlencode($data["link"]);
		$data["blogs"][] = $blog;
	}//foreach
	
	$layout[0] = "blogs_list";
	
	admin_output($layout,$data);
	
}//manage_blogs

function edit_blog(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 2;
	$data["title_id"] = "23";	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$blg_id = $GLOBALS["Get"]->type_val("blg_id","integer");
	$data["blg_id"] = $blg_id;
	
	build_form("blogs_edit",$blg_id);
	
	$sql_query = "SELECT * FROM blogs_posts
				  WHERE blg_id=".$blg_id." ORDER BY posted DESC";
	
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	if(empty($ret_link)) $ret_link = $GLOBALS["Get"]->referer();
	$ret_link = html_entity_decode($ret_link);
	$ret_link = stripslashes($ret_link);
	$ret_link = urlencode($ret_link);
	
	$ret_link2 = urlencode("index.php?section=manage_blogs&action=edit&blg_id=".$blg_id."&ret_link=".$ret_link);
	
	foreach($result as $res){
		$id = $res["pst_id"];		
		$posts[$id]["title"] = $res["title"];
		$posts[$id]["message"] = hs_substr(strip_tags(html_entity_decode($res["message"])),0,50)."...";
		$posts[$id]["view"] = build_link("blog",$blg_id)."/index.php?pst_id=".$res["pst_id"];
		$posts[$id]["edit"] = "index.php?section=manage_blogs&action=edit_post&blg_id=".$blg_id."&pst_id=".$res["pst_id"]."&ret_link=".$ret_link2;
		$posts[$id]["delete"] = "index.php?section=manage_blogs&action=delete_post&blg_id=".$blg_id."&pst_id=".$res["pst_id"]."&ret_link=".$ret_link;
		$sql_query = "SELECT * FROM blogs_comments WHERE blg_id=".$blg_id." AND pst_id=".$res["pst_id"];
		$cresult = $GLOBALS["DB"]->result($sql_query);
		foreach($cresult as $com){
			$subid = $com["com_id"];
			$comments[$id][$subid]["author"] = $GLOBALS["Network"]->network_name($com["mem_id"]);
			$comments[$id][$subid]["comment"] = hs_substr($com["comment"],0,50)."...";
			$comments[$id][$subid]["delete"] = "index.php?section=manage_blogs&action=delete_comment&blg_id=".$blg_id."&pst_id=".$res["pst_id"]."&com_id=".$com["com_id"]."&ret_link=".$ret_link;
		}//foreach
	}//foreach
	
	$data["posts"] = $posts;
	$data["comments"] = $comments;
	
	$layout[0] = "edit_blog";
	
	admin_output($layout,$data);
	
}//edit_blog

function save_blog(){
	
	admin_validate();
	
	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/modules/blogs.inc.php");
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$values = form_handle_input("blogs_edit",1,1);
	
	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_blog();
		exit;
	}//if
	
	$values["blg_id"] = $GLOBALS["Get"]->type_val("blg_id","integer");
	
	$sql_query = "SELECT * FROM blogs
				  WHERE blg_id=".$values["blg_id"];
	
	$blog = $GLOBALS["DB"]->row($sql_query);
	
	if($blog["folder"] != $values["folder"]){
		$exists = $GLOBALS["Blogs"]->verify_folder($values["folder"]);
		if($exists){
			$GLOBALS["uerr_id"] = 24;
			edit_blog();
			exit;
		}//if
		if(file_exists($blogs["path"]."/".$blog["folder"]))
		rename($blogs["path"]."/".$blog["folder"],$blogs["path"]."/".$values["folder"]);
	}//if
	
	settype($values["audience"],"integer");
	
	if(!empty($values["photo"]["tmp"])){
   		
   		if(hs_ereg("gif",$values["photo"]["type"])){
   			$extension = ".gif";
   		}//if
   		elseif(hs_ereg("jpeg|jpg",$values["photo"]["type"])){
   			$extension = ".jpeg";
   		}//elseif
	
		$photo = handle_photo_resize($values["photo"]["tmp"],$extension);
		
		if(!empty($blog["photo"]))
			@unlink(DOC_ROOT."/photos/".$blog["photo"]);
		
	}//if
	else{
		$photo["ex"]    = $blog["photo"];
		$photo["med"]   = $blog["photo_med"];
		$photo["small"] = $blog["photo_small"];
	}//else
		
	$sql_query = "UPDATE blogs SET title=':title:',description=':description:',folder=':folder:',audience=':audience:',
				  photo=':photo:',photo_med=':photo_med:',photo_small=':photo_small:'
				  WHERE blg_id=:blg_id:";
	
	$values["photo"]       = $photo["ex"];
	$values["photo_med"]   = $photo["med"];
	$values["photo_small"] = $photo["small"];
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	load_page("index.php?section=manage_blogs&action=edit&blg_id=".$values["blg_id"]."&ret_link=".urlencode(html_entity_decode(stripslashes($ret_link))));
	
}//save_blog

function do_delete_blogs(){
	
	admin_validate();
	
	$blg_id = $GLOBALS["Get"]->val("blg_id");
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	if(is_array($blg_id) && count($blg_id) > 0){
		foreach($blg_id as $id){
			delete_blog($id);
		}//foreach
	}//if
	elseif(!empty($blg_id))
		delete_blog($blg_id);
		
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));		
	load_page($ret_link);
	
}//do_delete_blogs

function edit_blog_post(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 2;
	$data["title_id"] = "23";	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$blg_id = $GLOBALS["Get"]->type_val("blg_id","integer");
	$pst_id = $GLOBALS["Get"]->type_val("pst_id","integer");
	$data["blg_id"] = $blg_id;
	$data["pst_id"] = $pst_id;
	
	build_form("blogs_edit_post",$pst_id);
	
	$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 photo_med FROM blogs_posts
				  WHERE pst_id=".$pst_id;
	
	$data["photo"] = $GLOBALS["DB"]->single($sql_query);
	
	$layout[0] = "edit_blog_post";
	
	admin_output($layout,$data);
	
}//edit_blog_post

function save_blog_post(){
	
	include_once(DOC_ROOT."/functions/form_functions.php");
	$values = form_handle_input("blogs_edit_post",1,1);
	
	if(!extract_null_value($values["err_id"])){
		$GLOBALS["uerr_id"] = $values["err_id"];
		edit_blog_post();
		exit;
	}//if
	
	$sql_query = "SELECT photo,photo_med,photo_small
   				  FROM blogs_posts
   				  WHERE blg_id=".$values["blg_id"]."
   				  AND pst_id=".$values["pst_id"];
   		
   	$photo_ex = $GLOBALS["DB"]->row($sql_query);
	
	if(!empty($values["photo"][tmp])){

   		if(hs_ereg("gif",$values["photo"][type])){
   			$extension = ".gif";
   		}//if
   		elseif(hs_ereg("jpeg|jpg",$values["photo"][type])){
   			$extension = ".jpeg";
   		}//elseif

   		$photo = build_thumbnailes($values["photo"][tmp],$extension);
   		
   		if($photo_ex["photo"]){
   			@unlink(DOC_ROOT."/photos/".$photo_ex["photo"]);
   			@unlink(DOC_ROOT."/photos/".$photo_ex["photo_med"]);
   			@unlink(DOC_ROOT."/photos/".$photo_ex["photo_small"]);
   		}//if

   	}//if
   	else{
   		$photo = array(
   			"ex"    => $photo_ex["photo"],
   			"med"   => $photo_ex["photo_med"],
   			"small" => $photo_ex["photo_small"]
   		);
   	}//else
   	
   	if($values["as_main"]){
   		$sql_query = "UPDATE blogs SET photo=':ex:',photo_med=':med:',photo_small=':small:'
   					  WHERE blg_id=:blg_id:";
   		$dbarray = $photo;
   		$dbarray["blg_id"] = $values["blg_id"];
   		$GLOBALS["DB"]->values = $dbarray;
   		$GLOBALS["DB"]->execute($sql_query);
   	}//if
   	
   	$sql_query = "UPDATE blogs_posts 
   				  SET title=':title:',message=':message:',message_plain=':message_plain:',photo=':photo:',photo_med=':photo_med:',photo_small=':photo_small:'
   				  WHERE blg_id=:blg_id: AND pst_id=:pst_id:";
   	
   	$dbvalues = $values;
   	$dbvalues["photo"]         = $photo["ex"];
   	$dbvalues["photo_med"]     = $photo["med"];
   	$dbvalues["photo_small"]   = $photo["small"];
   	$dbvalues["message_plain"] = strip_tags(html_entity_decode($values["message"]));
   	
   	$GLOBALS["DB"]->values = $dbvalues;
   	$GLOBALS["DB"]->execute($sql_query);
   	
   	$sql_query = "UPDATE blogs SET updated=".time()." WHERE blg_id=".$values["blg_id"];
   	$GLOBALS["DB"]->execute($sql_query);
   	
   	$values["ret_link"] = html_entity_decode($values["ret_link"]);
   	$values["ret_link"] = stripslashes($values["ret_link"]);
   	
   	load_page($values["ret_link"]);
	
}//save_blog_post

function featured_add_blog(){
	
	admin_validate();
	
	$blg_id = $GLOBALS["Get"]->val("blg_id");
	
	$sql_query = "UPDATE blogs SET featured='1',featured_end=0 WHERE blg_id=".$blg_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_add_blog

function featured_remove_blog(){
	
	admin_validate();
	
	$blg_id = $GLOBALS["Get"]->val("blg_id");
	
	$sql_query = "UPDATE blogs SET featured='0' WHERE blg_id=".$blg_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_remove_blog

function delete_blog_post(){
	
	admin_validate();
	
	$blg_id = $GLOBALS["Get"]->val("blg_id");
	$pst_id = $GLOBALS["Get"]->val("pst_id");
	
	$sql_query = "SELECT photo,photo_med,photo_small,hash, mem_id
				  FROM blogs_posts
				  WHERE blg_id=".$blg_id." AND pst_id=".$pst_id;
	
	$post = $GLOBALS["DB"]->row($sql_query);
	
	if($post["photo"]){
		$sql_query = "SELECT photo FROM blogs
					  WHERE blg_id=".$blg_id;
		$main_photo = $GLOBALS["DB"]->single($sql_query);
		if($main_photo == $post["photo"]){
			$sql_query = "UPDATE blogs SET photo='',photo_med='',photo_small=''
						  WHERE blg_id=".$blg_id;
			$GLOBALS["DB"]->execute($sql_query);
		}//if
		@unlink(DOC_ROOT."/photos/".$post["photo"]);
   		@unlink(DOC_ROOT."/photos/".$post["photo_med"]);
   		@unlink(DOC_ROOT."/photos/".$post["photo_small"]);
	}//if
	include_once(DOC_ROOT."/functions/delete_functions.php");
	if ($post["hash"]){
		//REMOVE MEDIA FILE
		delete_stream($post["hash"]);
	}
	//DELETE COMMENTS
	$sql_query = "SELECT com_id,hash FROM blogs_comments WHERE pst_id = ".$pst_id;
	$comments = $GLOBALS["DB"]->result($sql_query);
	foreach($comments as $res){
		if($res["hash"])
			delete_stream($res["hash"]);
	}//foreach
	$sql_query = "DELETE FROM blogs_comments WHERE pst_id=".$pst_id." AND blg_id=".$blg_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "DELETE FROM blogs_posts WHERE blg_id=".$blg_id." AND pst_id=".$pst_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//delete_blog_post

function delete_blog_comment(){
	
	admin_validate();
	
	$blg_id = $GLOBALS["Get"]->val("blg_id");
	$pst_id = $GLOBALS["Get"]->val("pst_id");
	$com_id = $GLOBALS["Get"]->val("com_id");
	
	//GET COMMENT FOR MEDIA DELETION
	$sql_query = "SELECT hash, mem_id FROM blogs_comments WHERE blg_id=".$blg_id." AND pst_id=".$pst_id." AND com_id=".$com_id;
	$comment = $GLOBALS["DB"]->row($sql_query);
	if ($comment["hash"]){
		//REMOVE MEDIA FILE
		include_once(DOC_ROOT."/functions/delete_functions.php");
		delete_stream($comment["hash"]);
	}
	$sql_query = "DELETE FROM blogs_comments WHERE pst_id=".$pst_id." AND blg_id=".$blg_id." AND com_id=".$com_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//delete_blog_comment

?>