<?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) {
		
		$D->is_logged = 1;
		
		$errored = 0;
		$txterror = '';
		
		$action=0;
		
		$wsee = -1;
		$codalbum = '';

		if (isset($_POST["codea"]) && $_POST["codea"] != '') {
			$codalbum = $this->db1->e($_POST["codea"]);
		}
		
		if (empty($codalbum)) {
			$errored = 1;
			$txterror = $this->lang('global_post_txterror6');
		}

		if (isset($_POST["wsee"]) && $_POST["wsee"]!=-1) {
			$wsee = $this->db1->e($_POST["wsee"]);
		}
		
		if ($wsee == -1) {
			$errored = 1;
			$txterror = $this->lang('global_post_txterror6');
		}
				
		if ($errored == 0) {

			$images_post = $_FILES['images_post'];
			$numphotos = count($images_post['name']);
			
			if ($images_post['name'][0]) {	
						
				if ($numphotos > $C->NUM_PHOTOS_BLOCK_UPLOAD) {
					$errored = 1;
					$txterror = $this->lang('dashboard_myalbums_error_toomanyimages').' '.$C->NUM_PHOTOS_BLOCK_UPLOAD;
				} else {
					
					$photos = array();
					$tmp_photos = array();
					
					for ($i = 0; $i < $numphotos; $i++) {
						if ($images_post['size'][$i] > $C->SIZE_PHOTO || $images_post['size'][$i]==0){
							$errored = 1;
							$txterror = $this->lang('dashboard_myalbums_error_verylarge').': '.$images_post['name'][$i];
							break;
						}

						$loadedtype = $images_post['type'][$i];
						if ($loadedtype=="image/jpeg" || $loadedtype=="image/gif" || $loadedtype=="image/png") {
							switch ($loadedtype) {
								case "image/jpeg":
									$extens = '.jpg';
									break;
								case "image/gif":
									$extens = '.gif';		
									break;
								case "image/png":
									$extens = '.png';
									break;
							}
							
						} else {
							$errored = 1;
							$txterror = $this->lang('dashboard_myalbums_error_fileinvalid').': '.$images_post['name'][$i];
							break;
						}
						
						$tmp_photos[] = $images_post['tmp_name'][$i];
						$codep = uniqueCode(11, 1, 'albums_photos', 'code');
						$codephotos[] = $codep;
						$photos[] = $codep.$extens;

					}
					
					if ($errored == 0) {
						
						$infoalbum = $this->db1->fetch("SELECT idalbum, idpost, who_see, namealbum, description FROM albums WHERE code='".$codalbum."' AND iduser=".$this->user->id." LIMIT 1");
						$idpost = $infoalbum->idpost;
						$idalbum = $infoalbum->idalbum;
						$whosee = $infoalbum->who_see;
						$txtpost = empty($infoalbum->description)?$this->db1->e(htmlspecialchars(stripslashes($infoalbum->namealbum))):$this->db1->e(htmlspecialchars(stripslashes($infoalbum->description)));

						$qphotos = 0;
						foreach($photos as $key => $fname) {
							move_uploaded_file($tmp_photos[$key], '../'.$C->FOLDER_ALBUMS.$codalbum.'/'.$fname);
							$thumbnail = new SmartImage('../'.$C->FOLDER_ALBUMS.$codalbum.'/'.$fname, true);
							$thumbnail->mycrop($C->widthPhotoThumbail, $C->widthPhotoThumbail, 'center'); 
							$thumbnail->saveImage('../'.$C->FOLDER_ALBUMS.$codalbum.'/min1/'.$fname);
							$thumbnail->close();
							$this->db1->query("INSERT INTO albums_photos SET code='".$codephotos[$key]."', idalbum=".$idalbum.", iduser=".$this->user->id.", photo='".$fname."', whendate='".time()."'");
							$qphotos++;
						}
						unset($thumbnail);
						
						$this->db1->query("UPDATE albums SET numphotos=numphotos+".$qphotos." WHERE code='".$codalbum."' AND iduser=".$this->user->id." LIMIT 1");
						
						if ($idpost != 0) {
							$therepost = $this->db1->fetch_field('SELECT count(idpost) FROM posts WHERE idpost='.$idpost.' AND iduser='.$this->user->id.' LIMIT 1');
							if ($therepost == 0) $idpost = 0;							
						}
						
						if ($idpost == 0) { 
							$codepost = uniqueCode(11, 1, 'posts', 'code');
							$this->db1->query("INSERT INTO posts SET code='".$codepost."', posted_in=0, id_wall=".$this->user->id.", iduser=".$this->user->id.", whendate='".time()."', typepost='album', who_see=".$wsee.", post='".$txtpost."'");
							$idnewpost = $this->db1->insert_id();
							
							$this->db1->query("UPDATE albums SET idpost=".$idnewpost." WHERE code='".$codalbum."' AND iduser=".$this->user->id." LIMIT 1");
							$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");
							
						} else {
							
							$this->db1->query("UPDATE posts SET whendate='".time()."', post='".$txtpost."' WHERE idpost='".$idpost."' AND iduser=".$this->user->id." LIMIT 1");
							
						}

					}

				}
			}
			
		} // end $errored == 0
				
		if ($errored==1) {
			$message = '0: '.$txterror;
		} else {
			$message = '1: '.$codalbum;	
		}
	}

?>
<script language="javascript" type="text/javascript">
	window.top.window.endPostear('<?php echo  $this->db1->e($message); ?>');
</script>