<?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("admin_header.php");
include("../../../mainfile.php");
include(XOOPS_ROOT_PATH."/modules/myalbum/cache/config.php");
include(XOOPS_ROOT_PATH."/modules/myalbum/include/functions.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 $db;
$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/admin/index.php?op=linksConfigMenu",4,_MD_MUSTADDCATFIRST);
		exit();
	}
$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 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");
	$url = $myts->makeTboxData4Save($url);
	$title = $myts->makeTboxData4Save($_POST["title"]);
   	$description = $myts->makeTareaData4Save($_POST["description"]);
   	$dir = $myts->makeTboxData4Save($_POST["dir"]);
	
   	chdir($dir);
   	$dir_name = "$dir";
   	$dir_h = opendir(".");
	xoops_cp_header();
	$filecount = 1;
	OpenTable();
	$timestart = time();
   	while ($file_name = readdir($dir_h) and $timedoing < 21 ) {
   		if (($file_name != "." && $file_name != "..")) { 
   			$ext = preg_replace("/^.+\.([^.]+)$/sU", "\\1", $file_name);
   			$name = str_replace(".$ext", "", $file_name);
   			if ($_POST["title"]=="") {
   				$nameDB = $name;
   			} else {
   				$nameDB = $_POST["title"]." ".$filecount;
   				$filecount++;
   			}
   			if(is_file($file_name) and in_array(strtolower($ext),array("jpg","png","gif"))) {
				$date = time();
				$newid = '\'\'';
				$filesize= filesize("$dir/$name.$ext");
				echo "$timedoing"._MD_SECONDS;
				$dim = GetImageSize("$dir/$name.$ext");
				if ($dim[0] < 1001 and $dim[1] < 1001 ) {
				$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, '$nameDB', '$ext', '$dim[0]', '$dim[1]', $submitter, 1, $date, 0, 0, 0, 0)";
    			$db->query($q) or $eh->show("0013");
				$newid = $db->getInsertId();
	   			print "&nbsp;&nbsp;<a href='photo.php?lid=$newid'>$name</a>\n";
				createBig("$dir/$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);
    			$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");
				print _MD_FINISH."<br /></center>\n";
				} else { print "$name.$ext"._MD_TOOBIG."<br /></center>\n";}
				$timedoing = time() - $timestart;
	   		}
   		}
  	}
	CloseTable();
   	closedir($dir_h);
	xoops_cp_footer();
} else {
	if(!$xoopsUser){
		redirect_header(XOOPS_URL."/user.php",2,_MUSTREGISTERFIRST);
		exit();
	}
   	global $db;
   	xoops_cp_header();
   	OpenTable();
	include_once("../../../class/xoopsformloader.php");
	echo _MD_TIP."<br /><br />".XOOPS_URL."/modules/myalbum/photos/batch/ <br /><br />"._MD_TIP2;
	$form = new XoopsThemeForm(_MD_PHOTOUPLOAD, "batchupload", "batch.php");
	$label = new XoopsFormLabel("", _MD_BATCHBLANK);
	$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");
	$dir = new XoopsFormHidden("dir", XOOPS_ROOT_PATH."/modules/myalbum/photos/batch");
	$op_hidden = new XoopsFormHidden("op", "submit");	
	$submit_button = new XoopsFormButton("", "submit", _SUBMIT, "submit");
	$form->addElement($label);
	$form->addElement($question_text);
	$form->addElement($desc_tarea);
	$form->addElement($cat);
	$form->addElement($dir);
	$form->addElement($op_hidden);
	$form->addElement($submit_button);
	$form->setRequired("avatarfile");
	$form->display();
   	CloseTable();
	
	include("admin_footer.php");
    
}
?>
