<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

// Session Cookie workaround
if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
} else if (isset($_GET["PHPSESSID"])) {
	session_id($_GET["PHPSESSID"]);
}

session_start();

require_once('../config.php');
include_once('functions.php');
include_once( ABSPATH . 'include/user_functions.php');
include_once( ABSPATH . 'include/islogged.php');

$allow = 1;

if ( ! $conn_id)
{
	if ( ! ($conn_id = db_connect()))
	{
		$allow = 0;
	}
}

$allowed_ext 	= array('.flv', '.mp4', '.mov', '.wmv', '.divx', '.avi', '.mkv', 
						'.asf', '.wma', '.mp3', '.m4v', '.m4a', '.3gp', '.3g2');
$allowed_type 	= array('application/octet-stream');

$uploadDir 	= _VIDEOS_DIR_PATH;
$uploadFile = $uploadDir . basename($_FILES['Filedata']['name']);

$ext1 = explode('.', $_FILES['Filedata']['name']);
$ext2 = strtolower($ext1[ count($ext1)-1 ]);
$ext2 = '.'. $ext2;

if ( ! in_array($ext2, $allowed_ext) || ! in_array($_FILES['Filedata']['type'], $allowed_type))
{
	$uploadFile = str_replace($ext2, '.flv', $uploadFile);
	$allow = 0;
}

if ($_SERVER['HTTP_ACCEPT_CHARSET'] != '' 
	|| strcmp($_SERVER['HTTP_USER_AGENT'], "Shockwave Flash") != 0 
	|| strcmp($_REQUEST['Upload'], "Submit Query") != 0)
{
	$allow = 0;
}

if ( ! $logged_in || ( ! is_admin() && ! is_moderator() && ! is_editor()))
{
	$allow = 0;
}

if (is_moderator() && mod_cannot('manage_videos'))
{
	$allow = 0;
}

if ( ! is_array($_FILES['Filedata']) || $_FILES['Filedata']['size'] == 0)
{
	$allow = 0;
}

$new_name  = md5($_FILES['Filedata']['name'].rand(1,888));
$new_name  = substr($new_name, 0, 8);
$new_name .= $ext2;
$uploadFile = $uploadDir . $new_name;

if($allow == 1)
{
	$move = @move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadFile);
	
	if ($move !== false)
	{
		$uploadFile = str_replace("\\", "\\\\", $uploadFile);	// IIS path fix

		$result = update_config('last_video', $uploadFile);
		if (is_array($result))
		{
			$fp = @fopen('tmp.pm', "a");
			@fwrite($fp, $uploadFile);
			@fclose($fp);
		}
		exit();
	}
	else if ($move === FALSE)
	{
		echo "Error: Could not upload file!";
	}
	else if ($_FILES['Filedata']['error'] != 0)
	{
		echo $_FILES['Filedata']['error'];
	}
	exit();
}
exit();
?>