<?php

// Base Settings
define('_VALID_HM', 1);
define('DS', DIRECTORY_SEPARATOR);
define('_HM_BASE', str_replace(DS.'includes'.DS.'tiny_mce'.DS.'plugins'.DS.
	'advimage'.DS.'classes', '', dirname(__FILE__)));
define('_HM_INCLUDES', _HM_BASE.DS.'includes'.DS);
define('_HM_RELPATH', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));
define('_HM_PEAR_DIR', _HM_INCLUDES.'PEAR'.DS);
define('_HM_UPLOAD', _HM_BASE.DS.'upload'.DS);
//define('_HM_RELUPLOAD', _HM_RELPATH.'upload/');
define('_HM_NESTEDSETS_OFF', true);
ini_set('include_path', _HM_PEAR_DIR);

// Including Base Classes
require_once _HM_BASE.DS.'configuration.php';
include_once _HM_INCLUDES.'database.php';
include_once _HM_INCLUDES.'thumbnail.php';

// Start Session and Check Login
@session_start();
$username = @$_SESSION['UserName'];
$userid   = @$_SESSION['RandomUserID'];
$sql = sprintf('SELECT RandomUserID FROM hm_Users 
	WHERE UserName = %s', $db->quoteSmart($username));
$row = $db->getOne($sql);

// User logged in
//if (!DB::isError($row) && strlen($row) > 0 && $userid == $row) {
if(TRUE || isset($_SESSION['RandomUserID'])){


	// determines the supported MIME types, and matching image format
    $image_supported = array();
	if (function_exists('imagetypes')) {
		if (imagetypes() & IMG_GIF)
			$image_supported['image/gif'] = 'gif';
		if (imagetypes() & IMG_JPG) {
			$image_supported['image/jpg'] = 'jpg';
			$image_supported['image/jpeg'] = 'jpg';
			$image_supported['image/pjpeg'] = 'jpg';
		}
		if (imagetypes() & IMG_PNG) {
			$image_supported['image/png'] = 'png';
			$image_supported['image/x-png'] = 'png';
		}
	}
	
	$file = @$_FILES['image_field'];

	// Supported Image ?
	if (isset($image_supported[@$file['type']]) && 
		(int)@$file['error'] == 0 &&
		(int)@$file['size'] > 0) {

		// Creating destination directory
		$year  = date('Y', time());
		$month = date('m', time());
		$day   = date('d', time());
		$path  = _HM_UPLOAD.$year;

		if (!file_exists($path)) @mkdir($path);
		$path .= DS.$month;
		if (!file_exists($path)) @mkdir($path);
		$path .= DS.$day;
		if (!file_exists($path)) @mkdir($path);

		if (file_exists($path)) {

			$uniq = uniqid();
			$src  = $file['tmp_name'];
			$filename = $uniq.'.'.$image_supported[$file['type']];
			$dest = $path.DS.$filename;

			Thumbnail::generate($src, $dest, $max_width);
			if (file_exists($dest)) {
				$uploaded      = true;		
				$upload_failed = false;
			}
		}
	}
}

?>