<?php

	Class PHPRC_APP
	{   	
		/**
		 * Returns the Root directory, or directory of index PHP file
		 * @return String Root directory
		 */
		Public Static Function REF_PATH()
		{
			Return PHPSET::$ROOT;
		}
		
		/*
		 * Returns the host name of current(server's) PC
		 */
		Public Static Function REF_HOSTNAME()
		{
			Try
			{
				Return GetHostName();
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				throw $ERR;
			}
		}
		
		/**
		 * Identifies if the request is came from same machine as server (LocalHost request)
		 * @return boolean
		 */
		Public Static Function REF_LOCAL_REQUEST()
		{
			Try
			{
				if ($_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR'])
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			Catch(Exception $ERR)
			{
				return false;
			}
		}
		
		
		Public Static Function REF_MAX_UPLOAD_SIZE($PREFERRED_EXPRESSION = 'KB')
		{
			
			if(
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'B' &&
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'BYTE' &&
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'BYTES' &&
					
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'KB' &&
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'MB' &&
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'GB' &&
					PHPVC_STR::VAR_UCASE($PREFERRED_EXPRESSION) <> 'TB'
			  )
			{
				Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_UNSUPPORTED_TYPE('PREFERRED_EXPRESSION', false)));
			}

			try 
			{
				$UPLOAD_LIMIT = ini_get('post_max_size');
				
				// Remove B
				if
				(
					StrLen($UPLOAD_LIMIT) >= 2 && 
					PHPVC_STR::VAR_UCASE(PHPVC_STR::VAR_RIGHT($UPLOAD_LIMIT, 1)) == "B"
				)
				{
					$UPLOAD_LIMIT = PHPVC_STR::VAR_DRIGHT($UPLOAD_LIMIT, 1);
				}
				
				// Remove T
				if
				(
					StrLen($UPLOAD_LIMIT) >= 2 &&
					PHPVC_STR::VAR_UCASE(PHPVC_STR::VAR_RIGHT($UPLOAD_LIMIT, 1)) == "T"
				)
				{
					$UPLOAD_LIMIT = PHPVC_STR::VAR_DRIGHT($UPLOAD_LIMIT, 1);
					$UPLOAD_LIMIT = PHPVC_NUM::VAR_NVAL($UPLOAD_LIMIT);
					$UPLOAD_LIMIT = $UPLOAD_LIMIT * 1024 * 1024 * 1024 * 1024;
				}
				// Remove G
				else if
				(
						StrLen($UPLOAD_LIMIT) >= 2 &&
						PHPVC_STR::VAR_UCASE(PHPVC_STR::VAR_RIGHT($UPLOAD_LIMIT, 1)) == "G"
				)
				{
					$UPLOAD_LIMIT = PHPVC_STR::VAR_DRIGHT($UPLOAD_LIMIT, 1);
					$UPLOAD_LIMIT = PHPVC_NUM::VAR_NVAL($UPLOAD_LIMIT);
					$UPLOAD_LIMIT = $UPLOAD_LIMIT * 1024 * 1024 * 1024;
				}
				// Remove M
				else if
				(
						StrLen($UPLOAD_LIMIT) >= 2 &&
						PHPVC_STR::VAR_UCASE(PHPVC_STR::VAR_RIGHT($UPLOAD_LIMIT, 1)) == "M"
				)
				{
					$UPLOAD_LIMIT = PHPVC_STR::VAR_DRIGHT($UPLOAD_LIMIT, 1);
					$UPLOAD_LIMIT = PHPVC_NUM::VAR_NVAL($UPLOAD_LIMIT);
					$UPLOAD_LIMIT = $UPLOAD_LIMIT * 1024 * 1024;
				}
				// Remove K
				else if
				(
						StrLen($UPLOAD_LIMIT) >= 2 &&
						PHPVC_STR::VAR_UCASE(PHPVC_STR::VAR_RIGHT($UPLOAD_LIMIT, 1)) == "K"
				)
				{
					$UPLOAD_LIMIT = PHPVC_STR::VAR_DRIGHT($UPLOAD_LIMIT, 1);
					$UPLOAD_LIMIT = PHPVC_NUM::VAR_NVAL($UPLOAD_LIMIT);
					$UPLOAD_LIMIT = $UPLOAD_LIMIT * 1024;
				}
				
				// Convert to byte
				return $UPLOAD_LIMIT;
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				throw $ERR;
			}
		}
		
	    /**
         * Returns the caller name of current method/event.
         * @param Integer $StackFrameIndex <p>Index of Stack Trace. (0 - Self, 1 - Caller, 2 - Caller of Caller, etc...)</p>
         * @return String Caller name
         */	
		Public Static Function REF_CALLER_NAME($StackFrameIndex = 0)
		{
			Try
			{
				$BACK_TRACE = debug_backtrace();
				$StackFrameIndex += 2;
				
				if(isset($BACK_TRACE) === false || COUNT($BACK_TRACE) <= $StackFrameIndex)
				{
					unset($BACK_TRACE);
					Return "No Backtrace";
				}
				else
				{
					if(STRLEN($BACK_TRACE[$StackFrameIndex]["function"]) <= 0)
					{
						$STACK_TRACE_MESSAGE = $BACK_TRACE[$StackFrameIndex]["function"];
						unset($BACK_TRACE);
						Return $STACK_TRACE_MESSAGE;
					}
					else
					{
						$STACK_TRACE_MESSAGE = '';
						
						if(isset($BACK_TRACE[$StackFrameIndex]["class"]))
						{
							$STACK_TRACE_MESSAGE = $STACK_TRACE_MESSAGE . $BACK_TRACE[$StackFrameIndex]["class"];
						}
						
						if(isset($BACK_TRACE[$StackFrameIndex]["type"]))
						{
							$STACK_TRACE_MESSAGE = $STACK_TRACE_MESSAGE . $BACK_TRACE[$StackFrameIndex]["type"];
						}
						
						if(isset($BACK_TRACE[$StackFrameIndex]["function"]))
						{
							$STACK_TRACE_MESSAGE = $STACK_TRACE_MESSAGE . $BACK_TRACE[$StackFrameIndex]["function"];
						}
			
						if($STACK_TRACE_MESSAGE == '')
						{
							$STACK_TRACE_MESSAGE = 'UNTRACEABLE';
						}
						
						unset($BACK_TRACE);
						Return $STACK_TRACE_MESSAGE;
					}
				}
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Return "";
			}
		}
		
		
		/**
		 * Retrieve client's IP address including proxy sources.
		 * @return String IP Address list.
		 */
		Public Static Function REF_IP_INFO($FETCH_MINIMUM = true)
		{
			try
			{
				$RESULT_IP = "";
				
				// Get only the one IP trusted address
				if($FETCH_MINIMUM == true)
				{
				    if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']))
				    {
				        $RESULT_IP = $_SERVER['REMOTE_ADDR'];
				    }
				    else if(isset($_SERVER['HTTP_CLIENT_IP']) && empty($_SERVER['HTTP_CLIENT_IP']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_CLIENT_IP'];
				    }
				    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && empty($_SERVER['HTTP_X_FORWARDED_FOR']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
				    }
				    else if(isset($_SERVER['HTTP_X_FORWARDED']) && empty($_SERVER['HTTP_X_FORWARDED']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_X_FORWARDED'];
				    }
				    else if(isset($_SERVER['HTTP_FORWARDED_FOR']) && empty($_SERVER['HTTP_FORWARDED_FOR']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_FORWARDED_FOR'];
				    }
				    else if(isset($_SERVER['HTTP_FORWARDED']) && empty($_SERVER['HTTP_FORWARDED']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_FORWARDED'];
				    }
				    else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) === false)
				    {
				        $RESULT_IP = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
				    }
				}
				
				// Fetch whole set of the IP addresses
				else
				{
				    if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']))
				    {
				        $RESULT_IP = $RESULT_IP . 'REMOTE_ADDR: ' . $_SERVER['REMOTE_ADDR'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_CLIENT_IP']) && empty($_SERVER['HTTP_CLIENT_IP']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_CLIENT_IP: ' . $_SERVER['HTTP_CLIENT_IP'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && empty($_SERVER['HTTP_X_FORWARDED_FOR']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_X_FORWARDED_FOR: ' . $_SERVER['HTTP_X_FORWARDED_FOR'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_X_FORWARDED']) && empty($_SERVER['HTTP_X_FORWARDED']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_X_FORWARDED: ' . $_SERVER['HTTP_X_FORWARDED'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_FORWARDED_FOR']) && empty($_SERVER['HTTP_FORWARDED_FOR']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_FORWARDED_FOR: ' . $_SERVER['HTTP_FORWARDED_FOR'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_FORWARDED']) && empty($_SERVER['HTTP_FORWARDED']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_FORWARDED: ' . $_SERVER['HTTP_FORWARDED'] . '; ';
				    }
				    if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) === false)
				    {
				        $RESULT_IP = $RESULT_IP . 'HTTP_X_CLUSTER_CLIENT_IP: ' . $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] . '; ';
				    }
				}

				// Return Result
				return $RESULT_IP;
				
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		
		/**
		 * Check if $_SERVER['REMOTE_ADDR'] if valid or not
		 */
		Public Static Function REF_IP_IS_VALID()
		{
			try
			{
				if(isset($_SERVER['REMOTE_ADDR']))
				{
					if(filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) == true)
					{
						return True;
					}
					else
					{
						return False;
					}
				}
				else
				{
					return False;
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		
		/**
		 * Checks if the current connection is HTTPS or not.
		 * @return Boolean <br />Returns TRUE if the current request is made through HTTPS.<br />.
		 */
		Public Static Function REF_IS_HTTPS()
		{
			try
			{
				if (isset($_SERVER['HTTPS']))
				{
					if(StrToUpper($_SERVER['HTTPS']) == 'ON')
					{
						return true;
					}
					else
					{
						return false;
					}
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		

		/**
		 * Redirects to preferred protocol type (HTTP/HTTPS).
		 * @param String $SET_PROTOCOL <br />Protocol type. Is either HTTP or HTTPS.<br />
		 * @param String $USE_301_MOVED_PERMANENTLY <br />If True, server responses ERROR 301, which tells the search engine to use of such protocol is permanent for requested page.<br />
		 * @return none
		 */
		Public Static Function REF_CHANGE_PROTOCOL($SET_PROTOCOL, $USE_301_MOVED_PERMANENTLY = true)
		{
			try
			{
				// Change to HTTPS protocol
				if ($SET_PROTOCOL == 'HTTPS' && self::REF_IS_HTTPS() === false)
				{
					$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
					if($USE_301_MOVED_PERMANENTLY === true)
					{
						header('HTTP/1.1 301 Moved Permanently');
					}
					header('Location: ' . $redirect);
					exit();
				}
					
				// Change to HTTP protocol
				else if($SET_PROTOCOL == 'HTTP' && self::REF_IS_HTTPS() === true)
				{
					$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];	
					if($USE_301_MOVED_PERMANENTLY === true)
					{
						header('HTTP/1.1 301 Moved Permanently');
					}	
					header('Location: ' . $redirect);
					exit();
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		
		/**
		 * Checks if the current request is done using localhost in URL.
		 * @return Boolean <br />Returns True if the requests is from localhost.<br />
		 */
		Public Static Function REF_IS_LOCALHOST()
		{
			try
			{
				if($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1')
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		

		/**
		 * Retrieves server's IP address.
		 * @return String <br />Server's IP address.<br />
		 */
		Public Static Function REF_SERVER_IP()
		{
			try
			{
				return gethostbyname(gethostname());
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		/**
		 * Redirects to proper server IP, when requests is done through Localhost.
		 * @return none
		 */
		Public Static Function REF_BLOCK_LOCALHOST()
		{
			try
			{
				if(StrToUpper($_SERVER['HTTP_HOST']) == 'LOCALHOST')
				{
					$REDIREC_ADDR = '';
					if(PHPRC_APP::REF_IS_HTTPS() === True)
					{
						$REDIREC_ADDR = 'https://' . self::REF_SERVER_IP() . $_SERVER['REQUEST_URI'];
					}
					else
					{
						$REDIREC_ADDR = 'http://' . self::REF_SERVER_IP() . $_SERVER['REQUEST_URI'];
					}
					header('HTTP/1.1 301 Moved Permanently');
					header('Location: ' . $REDIREC_ADDR);
					exit();
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
	}
?>