<?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');

	// 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)	{

		$title = $url = $description= '';
		$idcat = $idsubcat = 0;
		
		if (isset($_POST["tp"]) && !empty($_POST["tp"])) $title = $this->db1->e(htmlspecialchars($_POST["tp"]));
		if (isset($_POST["up"]) && !empty($_POST["up"])) $url = $this->db1->e(htmlspecialchars($_POST["up"]));
		if (isset($_POST["dp"]) && !empty($_POST["dp"])) $description = $this->db1->e(htmlspecialchars($_POST["dp"]));
		if (isset($_POST["idc"]) && !empty($_POST["idc"])) $idcat = $this->db1->e($_POST["idc"]);
		if (isset($_POST["idsc"]) && !empty($_POST["idsc"])) $idsubcat = $this->db1->e($_POST["idsc"]);
		
		if (empty($title)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($url)) { $errored = 1; $txterror .= 'Error... '; }
		if (!is_numeric($idcat) || $idcat == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (!is_numeric($idsubcat) || $idsubcat == 0) { $errored = 1; $txterror .= 'Error... '; }
		
	}

	if ($action == 2)	{

		$title = $description= $cpage = '';
		$idcat = $idsubcat = 0;
		
		if (isset($_POST["tp"]) && !empty($_POST["tp"])) $title = $this->db1->e(htmlspecialchars($_POST["tp"]));
		if (isset($_POST["dp"]) && !empty($_POST["dp"])) $description = $this->db1->e(htmlspecialchars($_POST["dp"]));
		if (isset($_POST["idc"]) && !empty($_POST["idc"])) $idcat = $this->db1->e($_POST["idc"]);
		if (isset($_POST["idsc"]) && !empty($_POST["idsc"])) $idsubcat = $this->db1->e($_POST["idsc"]);
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $cpage = $this->db1->e($_POST["cp"]);
		
		if (empty($cpage)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($title)) { $errored = 1; $txterror .= 'Error... '; }
		if (!is_numeric($idcat) || $idcat == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (!is_numeric($idsubcat) || $idsubcat == 0) { $errored = 1; $txterror .= 'Error... '; }

	}
	
	if ($action == 3)	{
		$loadedimage = $cpage = '';
		$didchanges = 0;
		if (isset($_POST["dch"]) && !empty($_POST["dch"])) $didchanges = $this->db1->e($_POST["dch"]);
		if (isset($_POST["limg"]) && !empty($_POST["limg"])) $loadedimage = $this->db1->e($_POST["limg"]);
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $cpage = $this->db1->e($_POST["cp"]);
		
		if (empty($cpage)) { $errored = 1; $txterror .= 'Error... '; }
		if ($didchanges == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($loadedimage)) { $errored = 1; $txterror .= 'Error... '; }
	}
	
	if ($action == 4)	{
		$loadedimage = $cpage = '';
		$didchanges = 0;
		if (isset($_POST["dch"]) && !empty($_POST["dch"])) $didchanges = $this->db1->e($_POST["dch"]);
		if (isset($_POST["limg"]) && !empty($_POST["limg"])) $loadedimage = $this->db1->e($_POST["limg"]);
		if (isset($_POST["cp"]) && !empty($_POST["cp"])) $cpage = $this->db1->e($_POST["cp"]);
		
		if (empty($cpage)) { $errored = 1; $txterror .= 'Error... '; }
		if ($didchanges == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($loadedimage)) { $errored = 1; $txterror .= 'Error... '; }
	}

	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {
		
		if ($action == 1) {	
			if (!validateUsername($url)) {
				echo('0: '.$this->lang('dashboard_pages_error_urlnovalid'));
				return;
			}
			
			// verify that no files with the same name registrant username
			if( file_exists($C->INCPATH.'controllers/'.strtolower($url).'.php') ) {
				$errored = 1;
				$txterror = $this->lang('dashboard_pages_error_urlnovalid');
				echo("0: ".$txterror); die();
			}
			if( file_exists($C->INCPATH.'../'.strtolower($url)) ) {
				$errored = 1;
				$txterror = $this->lang('dashboard_pages_error_urlnovalid');
				echo("0: ".$txterror); die();
			}
	
			$numu = $this->db1->fetch_field("SELECT count(idpage) FROM pages WHERE url='".$url."'");
			if ($numu != 0) {
				echo('0: '.$this->lang('dashboard_pages_error_urlnovalid'));
				return;
			}

			$numu = $this->db1->fetch_field("SELECT count(iduser) FROM users WHERE username='".$url."'");
			if ($numu != 0) {
				echo('0: '.$this->lang('dashboard_pages_error_urlnovalid'));
				return;
			}

			$numu = $this->db1->fetch_field("SELECT count(idgroup) FROM groups WHERE url='".$url."'");
			if ($numu != 0) {
				echo('0: '.$this->lang('dashboard_pages_error_urlnovalid'));
				return;
			}
			
			$code = uniqueCode(11, 1, 'pages', 'code');
			
			$this->db1->query("INSERT INTO pages SET code='".$code."', idowner=".$this->user->id.", idcategory=".$idcat.", idsubcategory=".$idsubcat.", title='".$title."', url='".$url."', description='".$description."', created='".time()."', num_likes=1, num_followers=1");
			
			$idpagen = $this->db1->insert_id();
			
			$this->db1->query("INSERT INTO pages_likes SET idpage=".$idpagen.", iduser=".$this->user->id.", whendate='".time()."'");
			
			$this->db1->query("INSERT INTO relations SET leader=".$idpagen.", subscriber=".$this->user->id.", type_leader=1, rltdate='".time()."'");
			
			$this->db1->query("UPDATE users SET num_pages=num_pages+1 WHERE iduser=".$this->user->id." LIMIT 1");
			
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}

		if ($action == 2) {	
			$this->db1->query("UPDATE pages SET idcategory=".$idcat.", idsubcategory=".$idsubcat.", title='".$title."', description='".$description."'  WHERE code='".$cpage."' LIMIT 1");
			$txtreturn = $this->lang('dashboard_pages_update_txt_msgok');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 3) {	
			if ($didchanges == 1) {
				$ext = explode(".",$loadedimage);
				$finalphoto = $cpage.'.'.$ext[count($ext)-1];
				
				$this->db1->query("UPDATE pages SET avatar_page='".$finalphoto."' WHERE code='".$cpage."' LIMIT 1");
				
				$raiz = '../';
				$tmppath = $C->FOLDER_TMP;
				$avatarfolder = $C->FOLDER_AVATAR_PAGES;
								
				// move photo file
				if (file_exists($raiz.$tmppath.$loadedimage)) {
					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);

					$thumbnail->myresize($C->widthAvatar0, 'width');
					$thumbnail->saveImage($raiz.$avatarfolder.$finalphoto);
					$thumbnail->close();
					
					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAvatar1, $C->heightAvatar1);
					$thumbnail->saveImage($raiz.$avatarfolder.'min1/'.$finalphoto);				
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAvatar2, $C->heightAvatar2);
					$thumbnail->saveImage($raiz.$avatarfolder.'min2/'.$finalphoto);
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAvatar3, $C->heightAvatar3);
					$thumbnail->saveImage($raiz.$avatarfolder.'min3/'.$finalphoto);
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAvatar4, $C->heightAvatar4);
					$thumbnail->saveImage($raiz.$avatarfolder.'min4/'.$finalphoto);
					$thumbnail->close();
					
					copy($raiz.$tmppath.$loadedimage, '../'.$C->FOLDER_AVATAR_PAGES.'origins/'.$finalphoto);
					
					unlink($raiz.$tmppath.$loadedimage);
			
					$txtreturn = $this->lang('dashboard_pages_update_txt_ok');
					echo("1: ".$txtreturn);
					return;
				} else {
					$txtreturn=$this->lang('dashboard_pages_update_txt_noupdate');
					echo("0: ".$txtreturn);
					return;
				}		
			} else {
				$txtreturn = $this->leng('dashboard_pages_update_txt_msg3');
				echo("0: ".$txtreturn);
				return;
			}
		}
		
		if ($action == 4) {	
			if ($didchanges == 1) {
				$ext = explode(".",$loadedimage);
				$finalphoto = $cpage.'.'.$ext[count($ext)-1];
				
				$this->db1->query("UPDATE pages SET cover_page='".$finalphoto."' WHERE code='".$cpage."' LIMIT 1");
				
				$raiz = '../';
				$tmppath = $C->FOLDER_TMP;
				$coversfolder = $C->FOLDER_COVERS_PAGES;
								
				// move photo file
				if (file_exists($raiz.$tmppath.$loadedimage)) {
					
					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthCover1, $C->heightCover1);
					$thumbnail->saveImage($raiz.$coversfolder.'min1/'.$finalphoto);				
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthCover2, $C->heightCover2);
					$thumbnail->saveImage($raiz.$coversfolder.'min2/'.$finalphoto);				
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthCover3, $C->heightCover3);
					$thumbnail->saveImage($raiz.$coversfolder.'min3/'.$finalphoto);				
					$thumbnail->close();
					
					copy($raiz.$tmppath.$loadedimage, '../'.$C->FOLDER_COVERS_PAGES.$finalphoto);
					
					unlink($raiz.$tmppath.$loadedimage);
			
					$txtreturn = $this->lang('dashboard_pages_update_txt_cover_ok');
					echo("1: ".$txtreturn);
					return;
				} else {
					$txtreturn=$this->lang('dashboard_pages_update_txt_cover_noupdate');
					echo("0: ".$txtreturn);
					return;
				}		
			} else {
				$txtreturn = $this->leng('dashboard_pages_update_txt_cover_msg4');
				echo("0: ".$txtreturn);
				return;
			}
		}
	
	}
?>