<?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/admin.php');

	// We are here only if you're logged in
	if (!$this->user->is_logged || !$this->user->info->is_network_admin) {
		echo('0: '.$this->lang('admin_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();
	}

	// ignore report
	if ($action == 1)	{
		
		$idreport = 0; 
		if (isset($_POST["idr"]) && !empty($_POST["idr"])) $idreport = $this->db1->e(trim($_POST["idr"]));
		
		if ($idreport == 0) { $errored = 1; $txterror .= 'Error... '; }		
		
	}

	// delete post reported
	if ($action == 2)	{
		
		$codpost = '';
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $codpost = $this->db1->e(trim($_POST["cp"]));
		
		if (empty($codpost)) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	// delete comment reported
	if ($action == 3)	{
		
		$idcomment = $iddenunciado = $idpost = $idownerpost = $idreport = $idinformante = 0;

		if (isset($_POST["idc"]) && !empty($_POST["idc"])) $idcomment = $this->db1->e(trim($_POST["idc"]));
		if (isset($_POST["idowp"]) && !empty($_POST["idowp"])) $idownerpost = $this->db1->e(trim($_POST["idowp"]));
		if (isset($_POST["idden"]) && !empty($_POST["idden"])) $iddenunciado = $this->db1->e(trim($_POST["idden"]));
		if (isset($_POST["idp"]) && !empty($_POST["idp"])) $idpost = $this->db1->e(trim($_POST["idp"]));
		if (isset($_POST["idr"]) && !empty($_POST["idr"])) $idreport = $this->db1->e(trim($_POST["idr"]));
		if (isset($_POST["idi"]) && !empty($_POST["idi"])) $idinformante = $this->db1->e(trim($_POST["idi"]));
		
		if ($idcomment == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idownerpost == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($iddenunciado == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idpost == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idreport == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idinformante == 0) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {

		if ($action == 1) {
			$this->db1->query("DELETE FROM reports WHERE idreport=".$idreport." LIMIT 1");
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 2) {
			$thepost = new post($codpost);
			$thepost->deletePost();
			unset($thepost);
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 3) {

			$this->db1->query("DELETE FROM comments WHERE idcomment=".$idcomment);
			$this->db1->query("DELETE FROM reports WHERE idreport=".$idreport." LIMIT 1");
			$this->db1->query("DELETE FROM hiddens WHERE typeitem=2 AND iditem=".$idcomment." AND iduser=".$idinformante." LIMIT 1");
			if ($this->db1->affected_rows()>0) {
				$this->db1->query("UPDATE users SET num_comments=num_comments-1 WHERE iduser=".$iddenunciado.' LIMIT 1');
				$this->db1->query("UPDATE posts SET numcomments=numcomments-1 WHERE idpost=".$idpost.' LIMIT 1');

				if ($iddenunciado != $idownerpost) {
					$this->db1->query("DELETE FROM notifications WHERE notif_type=3 AND idresult=".$idcomment." AND from_user_id=".$iddenunciado);
					
					$nnotifications = $this->network->getNumNotifications($idownerpost);
					if ($nnotifications <= 0) $nnotifications = 0;
					else $nnotifications = $nnotifications - 1;
					
					$this->db1->query("UPDATE users SET num_notifications=".$nnotifications." WHERE iduser=".$idownerpost." LIMIT 1");
				}
			}
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}
			
	}
?>