<?php
/**
 * @version $Id: fileupload.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if(!defined('IN_ADMIN') || !isset($_PBDEV['PHP_SELF']) || !preg_match('/[\/\\\\]admincp\.php$/i', $_PBDEV['PHP_SELF']))
{
	exit('Access Denied');
}

allowdo('setcate');

$cscript = 'fileupload';
$system = array();
$actionurl = 'admincp.php?action=fileupload&input_id='.$input_id;

//cate file upload
require_once (PBDIGG_CP.'include/cate.func.php');
$actionurl .= '&item=cate';
if ($submitted)
{
	if ($_POST['filename'])
	{
		$filename = trim($_POST['filename']);
	}
	else
	{
		$filename = 'cate_'.substr(md5($timestamp.random(6)), 6, 6);
	}
	require_once (PBDIGG_ROOT.'include/Upload.class.php');
	$ext = array('gif', 'jpeg', 'jpg', 'png');
	$UPLOAD = new Upload();
	if ($UPLOAD->_count)
	{
		$files = & $UPLOAD->getFiles();
		$filepath = PBDIGG_ROOT.'images/cate/';
		foreach ($files as $file)
		{
			if ($ck = $file->checkFile($ext))
			{
				$actionurl .= '&filename='.$filename;
				showMsg($ck, $actionurl);
			}
			else
			{
				if ($ck = $file->moveFile($filepath, $filename))
				{
					showMsg($ck, $actionurl);
				}
				else
				{
					echo '<script>alert("'.$a_message['icon_upload_success'].'");opener.document.getElementById("'.$input_id.'").value="'.$file->getRealName().'.'.$file->getExtName().'";self.close();</script>';
					exit;
				}
			}
		}
	}
	else
	{
		echo '<script>alert("'.$a_message['noicon_uploaded'].'");location.href="'.$actionurl.'"</script>';
	}
}
else
{
	$system['enable_upLoad'] = 1;
	if (!ini_get('file_uploads') || !($system['max_upload_size'] = ini_get('upload_max_filesize')))
	{
		$system['upload_error'] = $cp_message['upload_unsupport'];
		$system['enable_upLoad'] = 0;
	}
	elseif ($input_id && !preg_match('/^[a-zA-Z0-9_]*?$/i', $input_id))
	{
		$system['upload_error'] = $cp_message['input_id_error'];
		$system['enable_upLoad'] = 0;
	}
	$system['input_id'] = $input_id;
}

$TPL->assign('item', $item);
$TPL->assign('actionurl', $actionurl);
$TPL->assign('system', $system);
?>
