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

		$namealbum = $description = '';
		$privacy = -1;
		
		if (isset($_POST["namea"]) && !empty($_POST["namea"])) $namealbum = $this->db1->e(htmlspecialchars($_POST["namea"]));
		if (isset($_POST["desc"]) && !empty($_POST["desc"])) $description = $this->db1->e(htmlspecialchars($_POST["desc"]));
		if (isset($_POST["priv"])) $privacy = $this->db1->e($_POST["priv"]);
		
		if (empty($namealbum)) { $errored = 1; $txterror .= 'Error... '; }
		if ($privacy == -1) { $errored = 1; $txterror .= 'Error... '; }

	}

	if ($action == 2)	{

		$codealbum = $namealbum = $description = '';
		$privacy = -1;
		
		if (isset($_POST["codea"]) && !empty($_POST["codea"])) $codealbum = $this->db1->e($_POST["codea"]);
		if (isset($_POST["namea"]) && !empty($_POST["namea"])) $namealbum = $this->db1->e(htmlspecialchars($_POST["namea"]));
		if (isset($_POST["desc"]) && !empty($_POST["desc"])) $description = $this->db1->e(htmlspecialchars($_POST["desc"]));
		if (isset($_POST["priv"])) $privacy = $this->db1->e($_POST["priv"]);
		
		if (empty($codealbum)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($namealbum)) { $errored = 1; $txterror .= 'Error... '; }
		if ($privacy == -1) { $errored = 1; $txterror .= 'Error... '; }

	}
	
	// delete album
	if ($action == 3)	{

		$codealbum = '';
		
		if (isset($_POST["codea"]) && !empty($_POST['codea'])) $codealbum = $this->db1->e(trim($_POST["codea"]));
		
		if (empty($codealbum)) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	// delete photo
	if ($action == 4)	{

		$codealbum = $codephoto = '';
		
		if (isset($_POST["codea"]) && !empty($_POST['codea'])) $codealbum = $this->db1->e(trim($_POST["codea"]));
		if (isset($_POST["codeph"]) && !empty($_POST['codeph'])) $codephoto = $this->db1->e(trim($_POST["codeph"]));
		
		if (empty($codealbum)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($codephoto)) { $errored = 1; $txterror .= 'Error... '; }
		
	}


	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {
		
		if ($action == 1) {	
			
			$code = uniqueCode(11, 1, 'albums', 'code');			
			$old = umask(0); 
			
			$root = '../';
			if (mkdir($root.$C->FOLDER_ALBUMS.$code.'/min1',0777,TRUE)) {
				chmod($root.$C->FOLDER_ALBUMS.$code,0777);
				chmod($root.$C->FOLDER_ALBUMS.$code.'/min1',0777);
				$fnew1 = fopen($root.$C->FOLDER_ALBUMS.$code.'/min1/index.html', "a");
				$fnew2 = fopen($root.$C->FOLDER_ALBUMS.$code.'/index.html', "a");
				umask($old);
				$this->db1->query("INSERT INTO albums SET code='".$code."', iduser=".$this->user->id.", namealbum='".$namealbum."', description='".$description."', who_see=".$privacy.", created='".time()."'");
				$this->db1->query("UPDATE users SET num_albums=num_albums+1 WHERE iduser=".$this->user->id);
				$txtreturn = $code;
				echo('1: '.$txtreturn);
				return;
			} else {
				umask($old);
				$txtreturn = '0: '.$this->lang('dashboard_myalbums_error_nocreatedalbum');
				echo('1: '.$txtreturn);
				return;
			}			
		}

		if ($action == 2) {	
		
			$this->db1->query("UPDATE albums SET namealbum='".$namealbum."', description='".$description."', who_see=".$privacy." WHERE code='".$codealbum."' AND iduser=".$this->user->id." LIMIT 1");
			$txtreturn = $this->lang('dashboard_myalbums_txt_albumupdate');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 3) {
			
			$idpost = $this->db1->fetch_field("SELECT idpost FROM albums WHERE code='".$codealbum."' AND iduser=".$this->user->id." LIMIT 1");
			if ($idpost != 0) {
				$codepost = $this->db1->fetch_field("SELECT code FROM posts WHERE idpost=".$idpost." AND iduser=".$this->user->id." LIMIT 1");
				$thepost = new post($codepost);
				$thepost->deletePost();
				unset($thepost);
			}
			$idalbum = $this->db1->fetch_field("SELECT idalbum FROM albums WHERE code='".$codealbum."' AND iduser=".$this->user->id." LIMIT 1");
			$this->db1->query("DELETE FROM albums_photos WHERE idalbum=".$idalbum." AND iduser=".$this->user->id);
			$this->db1->query("DELETE FROM albums WHERE code='".$codealbum."' AND iduser=".$this->user->id." LIMIT 1");
			$this->db1->query("UPDATE users SET num_albums=num_albums-1 WHERE iduser=".$this->user->id);
			
			$root = '../';
			
			deleteFolder($root.$C->FOLDER_ALBUMS.$codealbum);

			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}

		if ($action == 4) {
			$idalbum = $this->db1->fetch_field("SELECT idalbum FROM albums WHERE code='".$codealbum."' AND iduser=".$this->user->id);
			if ($idalbum) {
				$this->db1->query("DELETE FROM albums_photos WHERE code='".$codephoto."' AND idalbum=".$idalbum." AND iduser=".$this->user->id);
				if ($this->db1->affected_rows()>0) {
					$this->db1->query("UPDATE albums SET numphotos=numphotos-1 WHERE code='".$codealbum."' AND iduser=".$this->user->id." LIMIT 1");
				}
			}

			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}
	
	}
?>