<?php
	Class PHPRC_HEADERS
	{
		/**
		 * Returns 401 Too Unauthorized Response. (will terminate PHP)
		 */
		Public Static Function REF_401_UNAUTHORIZED()
		{
			Header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized", true, 401);
			Exit();
		}
		
		/**
		 * Returns 404 Not Found Response. (will terminate PHP)
		 * @param String $REDIRECT_URL <br />URL of redirect page</br>
		 */
		Public Static Function REF_404_NOT_FOUND($REDIRECT_URL)
		{
			Header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
			echo '<html><head>';
			echo '<meta http-equiv="Refresh" content="0;url=' . $REDIRECT_URL . '" />';
			echo '</head><body></body></html>';
			Exit();
		}
		
		/**
		 * Returns 429 Too Many Request Response. (will terminate PHP)
		 */
		Public Static Function REF_429_TOO_MANY_REQUESTS()
		{
			Header($_SERVER["SERVER_PROTOCOL"]." 429 Too Many Requests", true, 429);
			Exit();
		}
		
		
		/**
		 * Returns 301 Moved Permanently Response. (will terminate PHP)
		 * @param String $REDIRECT_URL <br />URL of redirect page</br>
		 */
		Public Static Function REF_301_MOVED_PERMANENTLY($REDIRECT_URL)
		{
			Header($_SERVER["SERVER_PROTOCOL"]." 301 Moved Permanently", true, 301);
			Header("Location: " . $REDIRECT_URL);
			Exit();
		}

		/**
		 * Returns 503 Service Unavailable Response. (will terminate PHP)
		 */
		Public Static Function REF_503_SERVICE_UNAVAILABLE()
		{
			Header($_SERVER["SERVER_PROTOCOL"]." 503 Service Unavailable", true, 503);
			Exit();
		}
		
		/**
		 * Transfer local file as downloadable (will terminate PHP)
		 * @param String $FILE_LOCATION <br />Location of file<br />
		 * @param String $FILE_NAME <br />Name of file in download. If null, system will use BaseName().<br />
		 */
		Public Static Function REF_FILE_TRANSFER($FILE_LOCATION, $FILE_NAME)
		{
			$F_NAME = BaseName($FILE_LOCATION);
			$F_PATH = PHPVC_STR::VAR_LEFT($FILE_LOCATION, StrLen($FILE_LOCATION) - StrLen($F_NAME));
			
			// File not found
			if(PHPRC_FSO_FILE::REF_EXISTS($F_NAME, $F_PATH) === false)
			{
				Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND($FILE_LOCATION, "File Directory", False)));
			}
			$F_SIZE = FileSize($FILE_LOCATION);
			
			// Use BaseName
			if(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === true)
			{
				$FILE_NAME = $F_NAME;
			}
			
			// Prepare for File Transfer
			ini_set("zlib.output_compression", 0);
			//Header('Content-Description: File Transfer');
			Header('Content-Type: application/octet-stream');
			//Header("Content-Transfer-Encoding: Binary"); 
			Header('Content-Disposition: attachment; filename="' . $FILE_NAME . '"');
			Header('Expires: 0');
			Header('Cache-Control: must-revalidate');
			Header('Pragma: public');
			Header('Content-Length: ' . $F_SIZE);
			readfile($FILE_LOCATION);
			exit;
		}
		
		
		
		/**
		 * Load Image file
		 * @param String $IMAGE_PATH
		 * @param Boolean $AUTO_MODE if TRUE, when the file is not a image, just return. Default FALSE
		 */
		Public Static Function REF_IMAGE($IMAGE_PATH, $AUTO_MODE = false)
		{
		    // Get File Extension
		    // -----------------------------------------------
			$FILE_EXTENTION = StrToUpper(PHPRC_FSO_FILE::REF_EXTENSION($IMAGE_PATH));
			
			// Handle if no extension is given
			// -----------------------------------------------
			if
			(
				PHPVC_STR::VAR_IS_BLANK($FILE_EXTENTION) || 
				strtoupper($FILE_EXTENTION) == "IMG"
			)
			{
				// Remove IMG extension
				if(PHPVC_STR::VAR_CONTAINS_RIGHT($IMAGE_PATH, '.img', false))
				{
					$IMAGE_PATH = PHPVC_STR::VAR_DRIGHT($IMAGE_PATH, StrLen('.img'));
				}
				
				// Get Path
				$FILE_PATH = PHPRC_FSO_FILE::REF_PATH($IMAGE_PATH);
				$FILE_NAME = PHPRC_FSO_FILE::REF_NAME($IMAGE_PATH);
				
				// Locate Image
				if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.gif', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.gif';
					$FILE_EXTENTION = "GIF";
				}
				if
				(
						PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.GIF', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.GIF';
					$FILE_EXTENTION = "GIF";
				}
				
				else if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.jpg', $FILE_PATH)
				)	
				{
					$IMAGE_PATH = $IMAGE_PATH . '.jpg';
					$FILE_EXTENTION = "JPG";
				}
				else if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.JPG', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.JPG';
					$FILE_EXTENTION = "JPG";
				}
				
				else if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.jpeg', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.jpeg';
					$FILE_EXTENTION = "JPEG";
				}
				else if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.JPEG', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.JPEG';
					$FILE_EXTENTION = "JPEG";
				}
				
				else if 
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.png', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.png';
					$FILE_EXTENTION = "PNG";
				}
				else if
				(
					PHPRC_FSO_FILE::REF_EXISTS($FILE_NAME . '.PNG', $FILE_PATH)
				)
				{
					$IMAGE_PATH = $IMAGE_PATH . '.PNG';
					$FILE_EXTENTION = "PNG";
				}
				else
				{
					echo 'HEADER: unable to locate image file [' . $IMAGE_PATH . ']';
					exit;
				}
			}
			
			// Prepare Header
			// -----------------------------------------------
			if(StrToUpper($FILE_EXTENTION) == "GIF")
			{
				Header('Content-Type: image/gif');
			}
			elseif
			(
				StrToUpper($FILE_EXTENTION) == "JPG" ||
				StrToUpper($FILE_EXTENTION) == "JPEG"
			)
			{
				Header('Content-Type: image/jpeg');
			}
			elseif(StrToUpper($FILE_EXTENTION) == "PNG")
			{
				Header('Content-Type: image/png');
			}
			else if($AUTO_MODE == true)
			{
			    return;
			}
			else 
			{
				echo 'unsupported header [' . $FILE_EXTENTION . ']';
				exit;
			}
	
			// Return image
			// -----------------------------------------------
			if(ISSET($_GET['IMG_WIDTH']) || ISSET($_GET['IMG_HEIGHT']))
			{
				PHPRC_IMAGE::REF_DISPOSE();
				PHPRC_IMAGE::REF_LOAD($IMAGE_PATH);
				
				if
				(
						ISSET($_GET['IMG_WIDTH']) &&
						PHPVC_NUM::VAR_LONGVAL($_GET['IMG_WIDTH']) < PHPRC_IMAGE::REF_GET_WIDTH()
				)
				{
					PHPRC_IMAGE::REF_RESIZE(-1,-1,-1, PHPVC_NUM::VAR_LONGVAL($_GET['IMG_WIDTH']));
					PHPRC_IMAGE::REF_REPLACE();
				}
				
				if 
				(
						ISSET($_GET['IMG_HEIGHT']) &&
						PHPVC_NUM::VAR_LONGVAL($_GET['IMG_HEIGHT']) < PHPRC_IMAGE::REF_GET_HEIGHT()
				)
				{
					PHPRC_IMAGE::REF_RESIZE(-1,-1,-1, PHPVC_NUM::VAR_LONGVAL($_GET['IMG_HEIGHT']));
					PHPRC_IMAGE::REF_REPLACE();		
				}
				
				PHPRC_IMAGE::REF_COPY();
				PHPRC_IMAGE::REF_PRINT();
			}
			else 
			{
				echo PHPRC_FSO_FILE::REF_READALL
				(
					PHPRC_FSO_FILE::REF_NAME($IMAGE_PATH),
					PHPRC_FSO_FILE::REF_PATH($IMAGE_PATH)
				);
			}
			
			exit();		
		}
	
		
		/**
		 * Load Font file
		 * @param String $FONT_PATH
		 * @param Boolean $AUTO_MODE if TRUE, when the file is not a image, just return. Default FALSE
		 */
		Public Static Function REF_FONT($FONT_PATH, $AUTO_MODE = false)
		{	    
		    // Get File Extension
		    // -----------------------------------------------
		    $FILE_EXTENTION = StrToUpper(PHPRC_FSO_FILE::REF_EXTENSION($FONT_PATH));
		    	    
		    // Prepare Header
		    // -----------------------------------------------
		    if(StrToUpper($FILE_EXTENTION) == "OTF")
		    {
		        Header('Content-Type: font/opentype');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "TTF")
		    {
		        Header('Content-Type: font/truetype');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "WOFF")
		    {
		        Header('Content-Type: application/font-woff');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "EOT")
		    {
		        Header('Content-Type: application/vnd.ms-fontobject');
		    }
		    else if($AUTO_MODE == true)
		    {
		        return;
		    }
		    else
		    {
		        echo 'unsupported header [' . $FILE_EXTENTION . ']';
		        exit;
		    }
		    	    
		    // Return Font
		    // -----------------------------------------------
	        echo PHPRC_FSO_FILE::REF_READALL
	        (
	            PHPRC_FSO_FILE::REF_NAME($FONT_PATH),
	            PHPRC_FSO_FILE::REF_PATH($FONT_PATH)
	        );	    
		    exit();
		}
		
		
		/**
		 * Load media file, MP3/MP4
		 * @param String $FILE_LOCATION
		 * @param String $FILE_NAME
		 * @param Boolean $AUTO_MODE if TRUE, when the file is not a image, just return. Default FALSE
		 */
		Public Static Function REF_MEDIA($FILE_LOCATION, $FILE_NAME, $AUTO_MODE = false)
		{
		    // Get File Extension
		    // -----------------------------------------------
			$F_NAME = BaseName($FILE_LOCATION);
			$F_PATH = PHPVC_STR::VAR_LEFT($FILE_LOCATION, StrLen($FILE_LOCATION) - StrLen($F_NAME));
			$FILE_EXTENTION = StrToUpper(PHPRC_FSO_FILE::REF_EXTENSION($FILE_LOCATION));
			
			// File not found
			// -----------------------------------------------
			if(PHPRC_FSO_FILE::REF_EXISTS($F_NAME, $F_PATH) === false)
			{
				Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_ITEM_NOT_FOUND($FILE_LOCATION, "File Directory", False)));
			}
			
			// Check File Type
			// -----------------------------------------------
			if(StrToUpper($FILE_EXTENTION) == "MP3")
			{
			    header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
			}
			elseif(StrToUpper($FILE_EXTENTION) == "MP4")
			{
			    header('Content-Type: video/mp4');
			}
			elseif(StrToUpper($FILE_EXTENTION) == "FLV")
			{
			    header('Content-Type: video/flv');
			}
			elseif(StrToUpper($FILE_EXTENTION) == "MOV")
			{
			    header('Content-Type: application/octet-stream');
			}
			elseif(StrToUpper($FILE_EXTENTION) == "WEBM")
			{
			    header('Content-Type: video/webm');
			}
			else if(StrToUpper($FILE_EXTENTION) == "ZIP")
			{
			    header('Content-Type: application/zip');
			}
			else if(StrToUpper($FILE_EXTENTION) == "RAR")
			{
			    header('Content-Type: application/x-rar-compressed, application/octet-stream');
			}
			else if(StrToUpper($FILE_EXTENTION) == "7Z")
			{
			    header('Content-Type: application/x-7z-compressed, application/octet-stream');
			}
			
			else if($AUTO_MODE == true)
			{
			    return;
			}
			else
			{
			    echo 'unsupported header [' . $FILE_EXTENTION . ']';
			    exit;
			}
			
			// Use BaseName
			// -----------------------------------------------
			if(PHPVC_STR::VAR_IS_BLANK($FILE_NAME) === true)
			{
				$FILE_NAME = $F_NAME;
			}
			
			// Load Mp3, Mp4
			// -----------------------------------------------
			$F_SIZE = FileSize($FILE_LOCATION);
			header('Content-length: ' . $F_SIZE);
			Header('Content-Disposition: attachment; filename="' . $FILE_NAME . '"');
			header("Content-Transfer-Encoding: binary");
			readfile($FILE_LOCATION);
			exit;
		}
		

		/**
		 * Load Document file (.pdf)
		 * @param String $DOCUMENT_PATH
		 * @param Boolean $AUTO_MODE if TRUE, when the file is not a image, just return. Default FALSE
		 */
		Public Static Function REF_DOCUMENT($DOCUMENT_PATH, $AUTO_MODE = false)
		{
		    // Get File Extension
		    // -----------------------------------------------
		    $FILE_EXTENTION = StrToUpper(PHPRC_FSO_FILE::REF_EXTENSION($DOCUMENT_PATH));
		    
		    // Prepare Header
		    // -----------------------------------------------
		    if(StrToUpper($FILE_EXTENTION) == "PDF")
		    {
		        Header('Content-type:application/pdf');
		    }  
		    else if(StrToUpper($FILE_EXTENTION) == "XLS")
		    {
		        Header('Content-Type: application/vnd.ms-excel');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "XLSX")
		    {
		        Header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "DOC")
		    {
		        Header('Content-Type: application/vnd.ms-word');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "DOCX")
		    {
		        Header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "PPT")
		    {
		        Header('Content-Type: application/vnd.ms-powerpoint');
		    }
		    else if(StrToUpper($FILE_EXTENTION) == "PPTX")
		    {
		        Header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
		    }
		    else if($AUTO_MODE == true)
		    {
		        return;
		    }
		    else
		    {
		        echo 'unsupported header [' . $FILE_EXTENTION . ']';
		        exit;
		    }
		    
		    // Return Font
		    // -----------------------------------------------
		    echo PHPRC_FSO_FILE::REF_READALL
		    (
		        PHPRC_FSO_FILE::REF_NAME($DOCUMENT_PATH),
		        PHPRC_FSO_FILE::REF_PATH($DOCUMENT_PATH)
		    );
		    exit();
		}
		
	}
?>