<?php

// Do we have a rant image being uploaded?
function is_valid_upload( $field = 'ranterImage', $index = null ) {
	global $_FILES;
	if( !$_FILES[$field] ) return false;
#		echo "Passed test 1<br>";

	if(is_null($index)) {
		if( '' == $_FILES[$field]['tmp_name'] ) return false;
#			echo "Passed single test 2<br>";
		if( UPLOAD_ERR_NO_FILE == $_FILES[$field]['error'] ) return false;
#			echo "Passed single test 3<br>";
		if( 0 == $_FILES[$field]['size'] ) return false;
#			echo "Passed single test 4<br>";
	} else {
		if( !is_array($_FILES[$field]['tmp_name']) ) return false;
#			echo "Passed array text 2<br>";
		if( '' == $_FILES[$field]['tmp_name'][$index] ) return false;
#			echo "Passed array test 3<br>";
		if( UPLOAD_ERR_NO_FILE == $_FILES[$field]['error'][$index] ) return false;
#			echo "Passed array test 4<br>";
		if( 0 == $_FILES[$field]['size'][$index] ) return false;
#			echo "Passed array test 5<br>";
	}

	return true;
}


function pre_upload_rant_image( $pathtofile ) {
	if( !is_valid_upload() ) return array();
	$image_data = getimagesize( $pathtofile );
	$doing_upload = false;
	$upload_imagetype = null;
	$upload_error = false;

	if( false === $image_data ) {
		$upload_error='<p>Something wronky happened with that upload, getimagesize() returned false!</p>';
	} elseif( 300 > $image_data[0] ) {
		$upload_error='<p>Image too narrow, only '.$image_data[0].'px tall. Rant saved as draft. Try uploading image again.</p>';
	} elseif ( 245 > $image_data[1] ) {
		$upload_error='<p>Image too short, only '.$image_data[1].'px tall. Rant saved as draft. Try uploading image again.</p>';
	} elseif( false === $image_data[2] ) {
		$upload_error='<p>Unknown image type or extension. Upload refused. Rant saved as draft. Try uploading image again.</p>';
	} elseif( !is_uploaded_file( $pathtofile ) ) {
		$upload_error="<p>Something bad happened, that's not a valid file upload! Rant saved as draft. Try uploading image again.</p>";
	} else {
		// Valid upload. It will be moved into place later.
		$upload_imagetype = $image_data[2];
		$doing_upload = true;
	}
	return compact( "upload_error", "doing_upload", "upload_imagetype" );
}

function save_stock_rant_image( $source, $rant ) {
	if( copy( sprintf( '%s/%s/%s', SITE_PATH_ABS,SITE_RANT,$source),
		SITE_PATH_ABS .'/'.get_rantimage_filename($rant) ) ) {
		$upload_info='<p>Default rant image copied.</p>';
	} else {
		$upload_error='<p>There was an error copying the default rant image into place.</p>';
	}
	return compact("upload_info","upload_error");
}

function save_upload_rant_image( $source, $rant ) {
	$destination = SITE_PATH_ABS.'/'.get_rantimage_filename($rant);
	$size = getimagesize($source);

	if(300 == $size[0] && 245 == $size[1]) {
		if( move_uploaded_file($source, $destination) ) {
			$upload_info='<p>New rant image uploaded for rant '. $rant->id .'.</p>';
		} else {
			$upload_error='<p>Something went wrong while moving the uploaded image.</p>';
		}
	} else {
		if( crop_resize($source, $destination) ) {
			$upload_info='<p>New rant image uploaded and resized for rant '. $rant->id .'.</p>';
		} else {
			$upload_error='<p>Something went wrong while transforming the uploaded image.</p>';
		}
	}

	return compact("upload_info","upload_error");
}

function save_upload_rant_attachment( $source, $rant )
{
	global $dbConnection;

	$image_data = getimagesize( $source );
	$dbConnection->executeUpdate('INSERT INTO rant_attachment (rant, media) VALUES (?, ?)', array($rant, $image_data[2]));
	$rant_attachment_id = $dbConnection->lastInsertId();

	if( move_uploaded_file($source, SITE_PATH_ABS.'/'.get_rantattachment_filename($rant_attachment_id) ) ) {
		$upload_info='<p>New rant attachment uploaded for rant '. $rant .'.</p>';
		adminlog('Rant attachment uploaded', MTS_RANT, MTA_ADD);
	} else {
		$upload_error='<p>Something went wrong while storing the attachment.</p>';
	}

	return compact("rant_attachment_id","upload_info","upload_error");
}

?>
