<?php 
	// We check in which language we will work
	if (isset($_SESSION["DATAGLOBAL"][0]) && !empty($_SESSION["DATAGLOBAL"][0])) $C->LANGUAGE = $_SESSION["DATAGLOBAL"][0];

	$this->load_langfile('inside/dashboard.php');
	$this->load_langfile('global/global.php');

	// We are here only if you're logged in
	if (!$this->user->is_logged) {
		echo('0: '.$this->lang('dashboard_no_session'));
		die();
	}
	
	$errored = 0;
	$txterror = '';
	
	$action=0;
	
	if (isset($_POST["todo"]) && $_POST["todo"] != '') $action = $this->db1->e($_POST["todo"]);
	if (!is_numeric($action)) {
		$errored = 1;
		$txterror .= 'Error. ';
		echo('0: '.$caderror);
		die();
	}

	// 
	if ($action == 1)	{

		$ip = $idowner = 0;
		
		if (isset($_POST["ip"]) && $_POST["ip"]!='') $ip = $this->db1->e($_POST["ip"]);
		if (isset($_POST["iu"]) && !empty($_POST["iu"])) $idowner = $this->db1->e($_POST["iu"]);
		
		if ($ip == 0) { $errored = 1; $txterror .= 'Error. '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error. '; }
		
	}

	if ($action == 2)	{

		$ip = $idowner = 0;
		
		if (isset($_POST["ip"]) && $_POST["ip"]!='') $ip = $this->db1->e($_POST["ip"]);
		if (isset($_POST["iu"]) && !empty($_POST["iu"])) $idowner = $this->db1->e($_POST["iu"]);
		
		if ($ip == 0) { $errored = 1; $txterror .= 'Error. '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error. '; }
		
	}

	if ($action == 3)	{
		$codpost = '';
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $codpost = $this->db1->e($_POST["cp"]);
		if (empty($codpost)) { $errored = 1; $txterror .= 'Error... '; }
	}

	if ($action == 4)	{
		$comment = $codep = '';
		$ip = $idowner = 0;
		
		if (isset($_POST["ip"]) && $_POST["ip"]!='') $ip = $this->db1->e($_POST["ip"]);
		if (isset($_POST["iu"]) && !empty($_POST["iu"])) $idowner = $this->db1->e($_POST["iu"]);
		if (isset($_POST["c"]) && !empty($_POST["c"])) $comment = $this->db1->e(htmlspecialchars($_POST["c"]));
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $codep = $this->db1->e($_POST["cp"]);
		
		if ($ip == 0) { $errored = 1; $txterror .= 'Error. '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error. '; }
		if (empty($comment)) { $errored = 1; $txterror .= 'Error. '; }
		if (empty($codep)) { $errored = 1; $txterror .= 'Error. '; }
	}
	
	if ($action == 5)	{
		$idcomment = $ip = $idowner = 0;
		
		if (isset($_POST["ip"]) && $_POST["ip"]!='') $ip = $this->db1->e($_POST["ip"]);
		if (isset($_POST["ic"]) && !empty($_POST["ic"])) $idcomment = $this->db1->e($_POST["ic"]);
		if (isset($_POST["io"]) && !empty($_POST["io"])) $idowner = $this->db1->e($_POST["io"]);
		if ($idcomment == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error. '; }
		if ($ip == 0) { $errored = 1; $txterror .= 'Error. '; }
	}
	
	if ($action == 6)	{
		$txtshare = $codep = $id_wall = '';
		$ip = $idowner = $posted_in = $wsee = 0;
		
		if (isset($_POST["ip"]) && $_POST["ip"]!='') $ip = $this->db1->e($_POST["ip"]);
		if (isset($_POST["iu"]) && !empty($_POST["iu"])) $idowner = $this->db1->e($_POST["iu"]);
		if (isset($_POST["tsh"]) && !empty($_POST["tsh"])) $txtshare = $this->db1->e(htmlspecialchars($_POST["tsh"]));
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $codep = $this->db1->e($_POST["cp"]);
		if (isset($_POST["idw"]) && !empty($_POST["idw"])) $id_wall = $this->db1->e($_POST["idw"]);
		if (isset($_POST["pin"])) $posted_in = $this->db1->e($_POST["pin"]);
		if (isset($_POST["wsee"])) $wsee = $this->db1->e($_POST["wsee"]);
		
		if ($ip == 0) { $errored = 1; $txterror .= 'Error. '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error. '; }
		if (empty($codep)) { $errored = 1; $txterror .= 'Error. '; }
		if (empty($id_wall)) { $errored = 1; $txterror .= 'Error. '; }
	}
	
	if ($action == 7 || $action == 8 || $action == 9 || $action == 10) {
		$codpost = '';
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $codpost = $this->db1->e($_POST["cp"]);
		if (empty($codpost)) { $errored = 1; $txterror .= 'Error... '; }
	}

	if ($action == 11) {
		$idcomment = $idowner = 0;
		if (isset($_POST["idc"]) && !empty($_POST["idc"])) $idcomment = $this->db1->e($_POST["idc"]);
		if (isset($_POST["ido"]) && !empty($_POST["ido"])) $idowner = $this->db1->e($_POST["ido"]);
		if ($idcomment == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idowner == 0) { $errored = 1; $txterror .= 'Error... '; }
	}

	if ($action == 12 || $action == 13 || $action == 14) {
		$idcomment = 0;
		if (isset($_POST["idc"]) && !empty($_POST["idc"])) $idcomment = $this->db1->e($_POST["idc"]);
		if ($idcomment == 0) { $errored = 1; $txterror .= 'Error... '; }
	}
	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {
		
		// like
		if ($action == 1) {	
			$r = $this->db1->query("INSERT INTO likes SET idpost=".$ip.", iduser=".$this->user->id.", datewhen='".time()."'");
			$idlike = $this->db1->insert_id();
			$this->db1->query("UPDATE users SET num_likes=num_likes+1 WHERE iduser=".$this->user->id.' LIMIT 1');
			$this->db1->query("UPDATE posts SET numlikes=numlikes+1 WHERE idpost=".$ip.' LIMIT 1');
			//$this->db1->query('INSERT INTO activities SET iduser='.$this->user->id.', action=5, idresult='.$idlike.', iditem='.$idphoto.', typeitem=1, date="'.time().'"');
			if ($this->user->id != $idowner) {
				$this->db1->query("INSERT INTO notifications SET notif_type=2, idresult=".$idlike.", to_user_id=".$idowner.", from_user_id=".$this->user->id.", notif_object_type=1, notif_object_id=".$ip.",date='".time()."'");
				$this->db1->query("UPDATE users SET num_notifications=num_notifications+1 WHERE iduser=".$idowner.' LIMIT 1');
			}
			echo('1: Ok');
			return;
		}

		if ($action == 2) {	
				$r = $this->db1->query("DELETE FROM likes WHERE idpost=".$ip." AND iduser=".$this->user->id);
				//$idlike = $this->db1->insert_id();
				$this->db1->query("UPDATE users SET num_likes=num_likes-1 WHERE iduser=".$this->user->id.' LIMIT 1');
				$this->db1->query("UPDATE posts SET numlikes=numlikes-1 WHERE idpost=".$ip.' LIMIT 1');
				//$this->db1->query('DELETE FROM activities WHERE iduser='.$this->user->id.' AND action=5 AND iditem='.$idphoto.' AND typeitem=1');

				if ($this->user->id != $idowner) {
					$this->db1->query("DELETE FROM notifications WHERE notif_type=2 AND to_user_id=".$idowner." AND from_user_id=".$this->user->id." AND notif_object_type=1 AND notif_object_id=".$ip);
					$nnotifications = $this->network->getNumNotifications($idowner);
					if ($nnotifications <= 0) $nnotifications = 0;
					else $nnotifications = $nnotifications - 1;
					$this->db1->query("UPDATE users SET num_notifications=".$nnotifications." WHERE iduser=".$idowner.' LIMIT 1');
				}
				echo('1: Ok');
				return;
		}

		if ($action == 3) {
			$thepost = new post($codpost);
			$thepost->deletePost();
			unset($thepost);
			echo('1: Ok');
			return;
		}
		
		if ($action == 4) {			
			$r = $this->db1->query("INSERT INTO comments SET idpost=".$ip.", iduser=".$this->user->id.", comment='".$comment."', whendate='".time()."'");
			$idcomment = $this->db1->insert_id();
			$this->db1->query("UPDATE users SET num_comments=num_comments+1 WHERE iduser=".$this->user->id.' LIMIT 1');
			$this->db1->query("UPDATE posts SET numcomments=numcomments+1 WHERE idpost=".$ip.' LIMIT 1');
			//$this->db1->query('INSERT INTO activities SET iduser='.$this->user->id.', action=4, idresult='.$idcomment.', iditem='.$iditem.', typeitem=1, date="'.time().'"');
			if ($this->user->id != $idowner) {
				$this->db1->query("INSERT INTO notifications SET notif_type=3, idresult=".$idcomment.", to_user_id=".$idowner.", from_user_id=".$this->user->id.", notif_object_type=1, notif_object_id=".$ip.",date='".time()."'");
				$this->db1->query("UPDATE users SET num_notifications=num_notifications+1 WHERE iduser=".$idowner.' LIMIT 1');
			}
			
			$htmlReturn = '';
			ob_start();
			//$D->o_comment = stripslashes($comment);
			$D->o_comment = stripslashes($this->db1->fetch_field("SELECT comment FROM comments WHERE idcomment=".$idcomment));
			$D->o_username = stripslashes($this->user->info->username);
			$D->o_firstname = stripslashes($this->user->info->firstname);
			$D->o_lastname = stripslashes($this->user->info->lastname);
			$D->o_ucode = $this->user->info->code;
			$D->o_nameUser = (empty($D->o_firstname) || empty($D->o_lastname))?stripslashes($D->o_username):(stripslashes($D->o_firstname).' '.stripslashes($D->o_lastname));
			$D->o_whendate = time();
			$D->o_avatar =  empty($this->user->info->avatar)?$C->AVATAR_DEFAULT:$this->user->info->avatar;
			$D->o_idcomment = $idcomment;
			$D->o_idUser = $this->user->id;
			$D->o_idpost = $ip;
			$D->o_idUserOwner = $idowner;
			$D->o_codepost = $codep;
			$this->load_template('__onecomment-post.php');	
			$htmlReturn = ob_get_contents();
			ob_end_clean();

			echo("1: ".$htmlReturn);
			return;
		}
		
		if ($action == 5) {
			$this->db1->query("DELETE FROM comments WHERE idcomment=".$idcomment);
			if ($this->db1->affected_rows()>0) {
				$this->db1->query("UPDATE users SET num_comments=num_comments-1 WHERE iduser=".$this->user->id.' LIMIT 1');
				$this->db1->query("UPDATE posts SET numcomments=numcomments-1 WHERE idpost=".$ip.' LIMIT 1');
				//$this->db1->query('DELETE FROM activities WHERE iduser='.$this->user->id.' AND action=4 AND idresult='.$idcomment);

				if ($this->user->id != $idowner) {
					$this->db1->query("DELETE FROM notifications WHERE notif_type=3 AND idresult=".$idcomment." AND from_user_id=".$this->user->id);
					
					$nnotifications = $this->network->getNumNotifications($idowner);
					if ($nnotifications <= 0) $nnotifications = 0;
					else $nnotifications = $nnotifications - 1;
					
					$this->db1->query("UPDATE users SET num_notifications=".$nnotifications." WHERE iduser=".$idowner." LIMIT 1");
				}
			}
			echo('1: Ok');
			return;
		}
		
		if ($action == 6) {
			
			$codep = uniqueCode(11, 1, 'posts', 'code');
			$idwall = $this->network->idwall($id_wall, $posted_in);
			$r = $this->db1->query("INSERT INTO posts SET code='".$codep."', iduser=".$this->user->id.", post='".$txtshare."', typepost='share', valueattach='".$ip.':'.$idowner."', posted_in=".$posted_in.", id_wall=".$idwall.", who_see=".$wsee.", whendate='".time()."'");
			$idpost = $this->db1->insert_id();
			$this->db1->query('INSERT INTO activities SET iduser='.$this->user->id.', idresult='.$idpost.', iduser2='.$idowner.',action=10, iditem='.$ip.', typeitem=1, date="'.time().'"');
			$this->db1->query("UPDATE users SET num_posts=num_posts+1, num_posts_inwall=num_posts_inwall+1 WHERE iduser=".$this->user->id." LIMIT 1");

			$idOwnerPost = $this->network->idOwnerPost($ip);
			if ($this->user->id != $idOwnerPost) {
				$this->db1->query("UPDATE posts SET numshares=numshares+1 WHERE idpost=".$ip." LIMIT 1");
				$this->db1->query("INSERT INTO notifications SET notif_type=7, idresult=".$idpost.", to_user_id=".$idOwnerPost.", from_user_id=".$this->user->id.", notif_object_type=1, notif_object_id=".$ip.",date='".time()."'");
				$this->db1->query("UPDATE users SET num_notifications=num_notifications+1 WHERE iduser=".$idOwnerPost.' LIMIT 1');
			}
			
			if (!empty($txtshare)) {
				preg_match_all('~([#])([^\s#]+)~', str_replace(array('\r', '\n'), ' ', $txtstatus), $matchedHashtags);
				
				if(!empty($matchedHashtags[0])) {
					foreach($matchedHashtags[0] as $match) {
						$hashtag = str_replace('#', '', $match);
						$hashtag = $this->db1->e(($hashtag));
						$this->db1->query("INSERT INTO trends SET iduser=".$this->user->id.", trend='".$hashtag."', idpost=".$idpost.", whendate='".time()."'");
					}
				}
			}

			echo('1: '.$this->lang('global_share_txtokshare'));
			return;
		}

		if ($action == 7) {
			$thepost = new post($codpost, TRUE);
			$thepost->reportPost($this->user->id);
			unset($thepost);
			echo('1: Ok');
			return;
		}

		if ($action == 8) {
			$thepost = new post($codpost);
			$thepost->hiddenPost($this->user->id);
			unset($thepost);
			echo('1: Ok');
			return;
		}
		
		if ($action == 9) {
			$thepost = new post($codpost);
			$thepost->undoReportPost($this->user->id);
			unset($thepost);
			echo('1: Ok');
			return;
		}

		if ($action == 10) {
			$thepost = new post($codpost);
			$thepost->undoHiddenPost($this->user->id);
			unset($thepost);
			echo('1: Ok');
			return;
		}

		if ($action == 11) {
			$numregs = $this->db2->fetch_field("SELECT count(idreport) FROM reports WHERE typeitem=2 AND iditem=".$idcomment);
			if ($numregs == 0) $this->db2->query("INSERT INTO reports SET typeitem=2, iditem=".$idcomment.", idinformer=".$this->user->id.", iddenounced=".$idowner.", whendate='".time()."'");
			$numregs = $this->db2->fetch_field("SELECT count(idhidden) FROM hiddens WHERE typeitem=2 AND iditem=".$idcomment." AND iduser=".$this->user->id);
			if ($numregs == 0) $this->db2->query("INSERT INTO hiddens SET typeitem=2, iditem=".$idcomment.", iduser=".$this->user->id.", whendate='".time()."'");
			echo('1: Ok');
			return;
		}

		if ($action == 12) {
			$numregs = $this->db2->fetch_field("SELECT count(idhidden) FROM hiddens WHERE typeitem=2 AND iditem=".$idcomment." AND iduser=".$this->user->id);
			if ($numregs == 0) $this->db2->query("INSERT INTO hiddens SET typeitem=2, iditem=".$idcomment.", iduser=".$this->user->id.", whendate='".time()."'");
			echo('1: Ok');
			return;
		}
		
		if ($action == 13) {
			$this->db2->query("DELETE FROM reports WHERE typeitem=2 AND iditem=".$idcomment." AND idinformer=".$this->user->id." LIMIT 1");
			$this->db2->query("DELETE FROM hiddens WHERE typeitem=2 AND iditem=".$idcomment." AND iduser=".$this->user->id." LIMIT 1");
			echo('1: Ok');
			return;
		}
		
		if ($action == 14) {
			$this->db2->query("DELETE FROM hiddens WHERE typeitem=2 AND iditem=".$idcomment." AND iduser=".$this->user->id." LIMIT 1");
			echo('1: Ok');
			return;
		}
		
	
	}
?>