<?php

require '../phpconfig/config.php';            //Arquivo de configuração
include '../phpconfig/slugfy.php';    //Geração de slugs
include '../phpconfig/resize.php';    //Classe de resige GD2
include '../phpconfig/genpass.php';   //Geração de senhas


$ano = date("Y");
$mes = date('m');

$id_up = $_POST['id_up'];


$uploadDir = sti('up').'galerias/';

// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions

$verifyToken = md5(sti('salt') . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
	$tempFile   = $_FILES['Filedata']['tmp_name'];
	$uploadDir  =  $_SERVER['DOCUMENT_ROOT'].$uploadDir;
	$arch = slugfyUp($_FILES['Filedata']['name']);
	//$name = time()."_".$arch;
	$name = simplesPass(array(1,0,1,0),4)."-".$arch;
	$targetFile = $uploadDir . $name ;

    // Validate the filetype
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

		move_uploaded_file($tempFile, $targetFile);

		$resizeObj = new resize($targetFile);

		// Resize image (options: exact, portrait (pela lagura), landscape(pela altura), auto, crop)
		$resizeObj -> resizeImage(174, 160, 'crop');
		$resizeObj -> saveImage($uploadDir.'thumb1-'.$name, 80);
	
		$resizeObj -> resizeImage(1140, 642, 'portrait');
		$resizeObj -> saveImage($uploadDir.''.$name, 80);

		//SQL imagem table - id, nome, alt, data_cad
		mysql_query("INSERT INTO imagens (nome,   data_cad,capa,ordem, id_emp) VALUES ('$name',now(),0,0, '$id_up')");

/*		$query = mysql_query("SELECT id FROM imagens ORDER BY id DESC LIMIT 1"); 
		$row = mysql_fetch_assoc($query); 
		$aiValue = $row["id"];

		//SQL galeria de imagens
		mysql_query("INSERT INTO galerias (id_img,id_album) VALUES ('$aiValue', '$id_up')");*/


		echo 1;

	} else {

		echo 'Formato de arquivo inválido.';

	}
}
