<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Modified for EXOOPS by MAALBAK //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.org/					     //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
include("header.php");
// MyTextSanitizer object
include_once(XOOPS_ROOT_PATH."/class/xoopstree.php");
include_once(XOOPS_ROOT_PATH."/class/module.errorhandler.php");
include(XOOPS_ROOT_PATH."/class/upload.class.php");
global $myalbum_managed,$_POST,$_FILES;
$eh = new ErrorHandler; //ErrorHandler object
$mytree = new XoopsTree($db->prefix("myalbum_cat"),"cid","pid");
if(isset($_POST['submit']) && $_POST['submit'] != ""){
	if(!$xoopsUser){
		redirect_header(XOOPS_URL."/user.php",2,_MUSTREGISTERFIRST);
		exit();
	}
    	if(!isset($_POST['submitter']) || $_POST['submitter']=="") {
                $submitter = $xoopsUser->uid();
    	}else{
		$submitter = $_POST['submitter'];
	}
// Check if Title exist
    	if ($_POST["title"]=="") {
        	$eh->show("1001");
    	}
// Check if Photo exist
	$file = $_FILES['image']['name'];
    	if ($file=="" || !isset($file)) {
        	$eh->show("7000");
    	}
// Check if Description exist
    	if ($_POST['description']=="") {
        	$eh->show("1008");
    	}
	if ( !empty($_POST['cid']) ) {
    		$cid = $_POST['cid'];
	} else {
		$cid = 0;
	}
	$newid = $db->genId($db->prefix("myalbum_photos")."_lid_seq");
	
//	echo "<h1>".$GLOBALS['uploadFileName'][0]."<br />".$_FILES[$field]['tmp_name']."<br />".$_FILES[$field]['name']."<br />".$bcoosConfig['avatar_allow_upload']."</h1>";
	if ( !empty($_FILES['image']['name']) ) {
		$upload = new Upload();
		$upload->setAllowedMimeTypes(".gif|.jpeg|.jpg|.png");
		$upload->setMaxImageSize($myalbum_width, $myalbum_heigth);
		$upload->setMaxFileSize($myalbum_fsize);
		$tmp_name = 'tmp_'.rand();
		$upload->setDestinationFileName("image");
		$upload->setFileName($tmp_name);
		$upload->setUploadPath(XOOPS_ROOT_PATH."/modules/myalbum/photos");
		$result = $upload->doUpload();
		if ( $result ) {
			$url = $myts->makeTboxData4Save($upload->FileName.".".$upload->getFileExtension());
			$title = $myts->makeTboxData4Save($_POST["title"]);
   			$email = $myts->makeTboxData4Save($_POST["email"]);
   			$description = $myts->makeTareaData4Save($_POST["description"]);
			$date = time();
			$ext = $upload->getFileExtension();
			$dim = GetImageSize(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext");
			$q = "INSERT INTO ".$db->prefix("myalbum_photos")." (lid, cid, title, ext, res_x, res_y, submitter, status, date, hits, rating, votes, comments) VALUES ($newid, $cid, '$title', '$ext', '$dim[0]', '$dim[1]', $submitter, $myalbum_managed, $date, 0, 0, 0, 0)";
    			$db->query($q) or $eh->show("0013");
			if($newid == 0){
				$newid = $db->getInsertId();
			}
			createBig(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext", $newid, $ext);
			chmod(XOOPS_ROOT_PATH."/modules/myalbum/photos/$newid.$ext",0666);
			createThumb(XOOPS_ROOT_PATH."/modules/myalbum/photos/$newid.$ext", $newid, $ext);
			chmod(XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$newid.$ext",0666);
			unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext");
    		$db->query("INSERT INTO ".$db->prefix("myalbum_text")." (lid, description) VALUES ($newid, '$description')") or $eh->show("0013");
			$dim1 = GetImageSize(XOOPS_ROOT_PATH."/modules/myalbum/photos/$newid.$ext");
			$db->query("UPDATE ".$db->prefix("myalbum_photos")." SET res_x='$dim1[0]', res_y='$dim1[1]' WHERE lid='$newid'") or $eh->show("0013");
			redirect_header("index.php",2,_MD_RECEIVED."");
			exit();
		} else {
			$errors = $upload->getUploadErrors();
			include(XOOPS_ROOT_PATH."/header.php");
			OpenTable();
			echo "<b>"._MI_MYALBUM_NAME."</b>";
			echo "<p><strong>::Errors occured::</strong><br />\n";
			while(list($filename,$values) = each($errors)) {
				"File: " . print $filename . "<br />";
				$count = count($values);
				for($i=0; $i<$count; $i++) {
					echo "==>" . $values[$i] . "<br />";
				}
			}
			echo "</p>";
			CloseTable();
			include(XOOPS_ROOT_PATH."/footer.php");
			exit();
		}
	}
	redirect_header("submit.php",2,_MD_FILEERROR);
// daniel
}else{
	if(!$xoopsUser){
		redirect_header(XOOPS_URL."/user.php",2,_MUSTREGISTERFIRST);
		exit();
	}
	$result = $db->query("SELECT count(*) as count FROM ".$db->prefix("myalbum_cat")."",$mylinks_newlinks,0);
    list($count) = $db->fetchRow($result);
	if($count == 0){
		redirect_header(XOOPS_URL."/modules/myalbum",2,_MD_MUSTADDCATFIRST);
		exit();
	}
	include(XOOPS_ROOT_PATH."/header.php");
   	OpenTable();
   	echo "<b>"._MI_MYALBUM_NAME."</b>";
	include_once("../../class/xoopsformloader.php");
	$form = new XoopsThemeForm(_MD_PHOTOUPLOAD, "uploadavatar", "submit.php");
	$pixels_label = new XoopsFormLabel(_MD_MAXPIXEL, $myalbum_width." x ".$myalbum_heigth);
	$size_label = new XoopsFormLabel(_MD_MAXSIZE, $myalbum_fsize);
	$form->setExtra("enctype='multipart/form-data'");
	$question_text = new XoopsFormText(_TITLE, "title", 50, 255);
	$cat = new XoopsFormSelect(_CATEGORY, "cid", $cid);
	$tree = $mytree->getChildTreeArray();
	foreach ($tree as $leaf ) {
		$leaf['prefix'] = substr($leaf['prefix'], 0, -1);
		$leaf['prefix'] = str_replace(".","--",$leaf['prefix']);
		$cat -> addOption($leaf['cid'],$leaf['prefix'].$leaf['title']);
	}
	$desc_tarea = new XoopsFormTextarea(_DESCRIPTION, "description");
	$file_form = new XoopsFormFile(_MD_SELECTFILE, "image", $myalbum_fsize);
	$op_hidden = new XoopsFormHidden("op", "submit");
	$counter_hidden = new XoopsFormHidden("fieldCounter", 1);
	$submit_button = new XoopsFormButton("", "submit", _SUBMIT, "submit");
	$form->addElement($pixels_label);
	$form->addElement($size_label);
	$form->addElement($question_text);
	$form->addElement($desc_tarea);
	$form->addElement($cat);
	$form->addElement($file_form);
	$form->addElement($counter_hidden);
	$form->addElement($op_hidden);
	$form->addElement($submit_button);
	//$form->setRequired("image");
	$form->display();
   	CloseTable();
}
include("footer.php");
?>
