<?php

	require_once('../../../wp-config.php');

	if ( ( isset($_SERVER['HTTPS']) && 'on' == strtolower($_SERVER['HTTPS']) ) && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
		$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
	elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
		$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
	unset($current_user);

	if (!(is_user_logged_in() && current_user_can('edit_posts')))
  		die("Athentication failed!");

	$imagesExts = array('gif','jpg','png');
	$audiosExts = array('wav','mp3');
	
	$acceptedExtsString = "";
	
	function DownloadFile(){
		global $acceptedExtsString, $imagesExts, $audiosExts;
	
		$url = $_POST['upload_url'];
	
		// Prepare accpeted extensions
		$acceptedExts = array();
	
		if ('1' == $_POST['type'])
			$acceptedExts = $imagesExts;
		elseif ('2' == $_POST['type']) 	
			$acceptedExts = $audiosExts;
	
	
		//Retrieve file
		if ($fp_source = @fopen($url, 'rb')) {
			//Get target filename
			$exploded_url = explode( '.', $url );
	
			$ext = array_pop( $exploded_url );
	
			// Check extension
			if (false != $acceptedExts)
				if (false === array_search(strtolower($ext), $acceptedExts)){
					foreach($acceptedExts as $acceptedExt)
						if ($acceptedExtsString == "")
							$acceptedExtsString = $acceptedExt;
						else
							$acceptedExtsString = $acceptedExtsString." - ".$acceptedExt;
					return false;
				}
			
	
			$filename = time() . '_' . str_replace( 'rc_cwp_meta_', '', $_POST["input_name"]) . '.' . $ext;
			
			$directory = dirname(__FILE__) . '/files_flutter/';
	
			$fp_dest = @fopen($directory . $filename,"wb");
			if ($fp_dest == false) return false;
	
			while(!feof($fp_source)) {
				set_time_limit(30);
	
				//if (connection_status()!=0) return false;
	
				$readData = fread($fp_source, 1024*2);
				//if ($readData == false) return false;
				
				fwrite($fp_dest,$readData);
				
			}
			fclose($fp_source) ;
			fclose($fp_dest) ;
			//chmod($directory . $filename, 0644);
	
			return $filename;
		}
		return false;
	
	}

	if (isset($_POST['upload_url']) && (!empty($_POST['upload_url'])))  // file was send from browser
	{
		if ( (substr($_POST['upload_url'],0,4) != "http") && (substr($_POST['upload_url'],0,3) != "ftp"))
			$_POST['upload_url'] = "http://".$_POST['upload_url'];

		$filename = DownloadFile();
		

		if (false == $filename) {
			if ($acceptedExtsString != "") $infoStr = ". Make sure the file ends with: $acceptedExtsString";
// 			$result_msg = "Error downloading file: ".$_POST['upload_url'].$infoStr;
			$result_msg = "<font color='red'><b>Upload Unsuccessful!</b></font>";
		}
		else{
// 			$result_msg = 'The URL '.$_POST['upload_url'].' was downloaded successfuly. Please remember to click the save button.';
			$result_msg = "<font color='green'><b>Successful upload!</b></font>" ;
			$operationSuccess = "true";
		}
		include_once("RCCWP_WritePostPage.php") ;
		$edit_anchor = RCCWP_WritePostPage::snipshot_anchor(FLUTTER_URI.'files_flutter/'.$filename) ;
		echo $result_msg."*".$filename."*".$edit_anchor ;
	}

if( isset($_FILES['Filedata'] ) )
{
	if ($_FILES['Filedata']['error'] == UPLOAD_ERR_OK)  // no error
	{
		$special_chars = array (' ','`','"','\'','\\','/'," ","#","$","%","^","&","*","!","~","‘","\"","’","'","=","?","/","[","]","(",")","|","<",">",";","\\",",");
		$filename = str_replace($special_chars,'',$_FILES['Filedata']['name']);
		$filename = time() . $filename;
		@move_uploaded_file( $_FILES['Filedata']['tmp_name'], dirname(__FILE__) . '/files_flutter/' . $filename );
		@chmod(dirname(__FILE__) . '/files_flutter/' . $filename, 0644);

// 		$result_msg = 'The file '.$_FILES['Filedata']['name'].' was uploaded successfuly. Please remember to click the save button.';
		$result_msg = "<font color='green'><b>Successful upload!</b></font>" ;
		$operationSuccess = "true";
	}
	elseif ($_FILES['Filedata']['error'] == UPLOAD_ERR_INI_SIZE)
		$result_msg = 'The uploaded file exceeds the maximum upload limit';
	else 
		$result_msg = "<font color='red'><b>Upload Unsuccessful!</b></font>";

	include_once("RCCWP_WritePostPage.php") ;
	$edit_anchor = RCCWP_WritePostPage::snipshot_anchor(FLUTTER_URI.'files_flutter/'.$filename) ;
	echo $result_msg."*".$filename."*".$edit_anchor ;
}
/*
	if( isset($_FILES['Filedata'] ) )
	{
		$path = dirname(__FILE__) . '/files_flutter/';
		$path = $path . basename( $_FILES['Filedata']['name'] );
		if( move_uploaded_file($_FILES['Filedata']['tmp_name'], $path) )
		{
			echo 'The file was uploaded successfuly. Please remember to click the save button.'.$_FILES['Filedata']['name'] ;
		}
		else
		{
			echo $_FILES[0]['error'] ;
		}
	}*/
?>