<?php

	Class PHPRC_JSON
	{
		/**
		 * Checks if the given string is valid JSOn or not
		 * @param String $STRING_VAR <br />String to check<br />
		 * @throws Exception
		 * @return boolean
		 */
		Public Static Function REF_IS_JSON_STRING($STRING_VAR)
		{
			Try
			{
				if
				(
					is_array
					(
							json_decode($STRING_VAR, true)
					)
				)
				{
					return true;
				}
				else
				{
					echo 'NOT JSON ARRAY!';
					exit;
					
					return false;
				}
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		
		// =====================================================================
		// ADD NEW JSON PARAMETER
		// =====================================================================
		Public static function REF_ADD($PARAMETER_SET, $PARAMETER_FIELD, $PARAMETER_VALUE)
		{
			// ARRAY VALUES
			if(is_array($PARAMETER_VALUE) === true)
			{
				// Sanitize Parameters
				$PARAMETER_FIELD = self::REF_SAFE($PARAMETER_FIELD);
				
				if(VAR_IS_BLANK($PARAMETER_SET) === true)
				{
					$PARAMETER_SET = "\"" . $PARAMETER_FIELD . "\":" . self::REF_ENCODE_ARRAY($PARAMETER_VALUE) . "";
				}
				else
				{
					$PARAMETER_SET = $PARAMETER_SET . ",\"" . $PARAMETER_FIELD . "\":" . self::REF_ENCODE_ARRAY($PARAMETER_VALUE) . "";
				}		
			}
			
			// NON-ARRAY VALUES
			else
			{
				// Sanitize Parameters
				$PARAMETER_FIELD = self::REF_SAFE($PARAMETER_FIELD);
				$PARAMETER_VALUE = self::REF_SAFE($PARAMETER_VALUE);
			
				if(PHPVC_STR::VAR_IS_BLANK($PARAMETER_SET) === true)
				{
					$PARAMETER_SET = "\"" . $PARAMETER_FIELD . "\":\"" . $PARAMETER_VALUE . "\"";
				}
				else
				{
					$PARAMETER_SET = $PARAMETER_SET . ",\"" . $PARAMETER_FIELD . "\":\"" . $PARAMETER_VALUE . "\"";
				}			
			}
			
			return $PARAMETER_SET;
		}
		
		
		
		// =====================================================================
		// SEND JSON 
		// =====================================================================
		Public static function REF_PRINT($PARAMETER_SET, $USE_ROT47 = false, $PRIVATE_KEY = "")
		{
			global $SUPPRESS_ENCRYPTION;
			
			// SEND RESULT TO CLIENT
			if($USE_ROT47 === false || $SUPPRESS_ENCRYPTION === true)
			{
				header('Content-type: application/json');
				echo '{' . $PARAMETER_SET . '}';
			}
			
			// ENCODE DATA BEFORE SENDING RESULT TO CLIENT
			else
			{
				//echo 'KEY: ' . $PRIVATE_KEY . '<br />';
				echo VAR_ENCRYPT_ROT47
				(
					'{' . $PARAMETER_SET . '}',
					$PRIVATE_KEY
				);
			}
			exit;
		}
	
		// =====================================================================
		// CONVERT TEXT To JSON SAFE STRING
		// =====================================================================
		Public static function REF_SAFE($SRC_TEXT)
		{
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, '\\', "\\\\");
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, '{', "\{");
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, '}', "\}");
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, '"', '\"');
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, "\r\n", "<br />");
			$SRC_TEXT = PHPVC_STR::VAR_REPLACE($SRC_TEXT, "\t", "");
			$SRC_TEXT = nl2br($SRC_TEXT);
			return $SRC_TEXT;
		}
		
		// =====================================================================
		// ENCODE ARRAY STRING TO JSON INTERNAL ARRAY VALUE
		// =====================================================================
		/**
		 * Convert 1D/2D array into JSON string
		 * @param unknown $SRC_ARRAY
		 * @throws Exception
		 * @return void|string
		 */
		Public Static Function REF_TO_JSON($SRC_ARRAY)
		{
			Try
			{
				// INITIALIZE
				$JSON_RESULT = json_encode($SRC_ARRAY);
				
				// IF NOT ARRAY
				if(is_array($SRC_ARRAY) === false)
				{
					return;
				}
				
				// SUCCESS
				else if(isset($JSON_RESULT) === true && $JSON_RESULT <> false)
				{
					return $JSON_RESULT;
				}
				
				// ERROR
				else
				{
					PHPVC_LOG::VAR_ERROR('TO_JSON', self::REF_ERROR());
					throw new Exception(self::REF_ERROR());
				}
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		/**
		 * Reads the last occured JSON error
		 */
		Public Static Function REF_ERROR()
		{
			try
			{
				// Initialize
				$JSON_ERROR = json_last_error();
				
				// Check if not null
				if(isset($JSON_ERROR) === false)
				{
					return "0 [JSON_ERROR_NONE]";
				}
				
				// Read Error Code
				if($JSON_ERROR == 0)
				{
					return "0 [JSON_ERROR_NONE]";
				}
				else if($JSON_ERROR == 1)
				{
					return "1 [JSON_ERROR_DEPTH]";
				}
				else if($JSON_ERROR == 2)
				{
					return "2 [JSON_ERROR_STATE_MISMATCH]";
				}
				else if($JSON_ERROR == 3)
				{
					return "3 [JSON_ERROR_CTRL_CHAR]";
				}
				else if($JSON_ERROR == 4)
				{
					return "4 [JSON_ERROR_SYNTAX]";
				}
				else if($JSON_ERROR == 5)
				{
					return "5 [JSON_ERROR_UTF8]";
				}
				else
				{
					return $JSON_ERROR . " [UNKNOWN]";
				}
			}
			catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		/**
		 * Convert JSON String into Array
		 * @param String $JSON_STRING
		 * @return String()
		 */
		Public Static Function REF_CREATE($JSON_STRING)
		{
			Try
			{
				return json_decode($JSON_STRING, True);				
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
		}
		
		/**
		 * Read value from ARRAY created from JSON STRING
		 * @param String() $JSON_ARRAY <br />Array to read<br /> 
		 * @param String $FIELD_NAME <br />JSON Parameter name of value<br />
		 * @param Int $SUB_INDEX <br />Index of value if JSON value consists of an array<br />
		 * @return String <br />Value of JSON<br />
		 */
		Public Static Function REF_READ($JSON_ARRAY, $FIELD_NAME, $SUB_INDEX = -1)
		{
			// Validate Parameters
			// --------------------------------------------
			if(is_array($JSON_ARRAY) === false)
			{
				Throw PHPVC_ERROR::VAR_RAISERROR(New Exception(PHPVC_ERROR::VAR_ARGUMENT_EXCEPTION_INVALID_ARGUMENT_TYPE("JSON_ARRAY", "ARRAY", False)));
			}
			// --------------------------------------------
			
			
			// Actual Function
			// --------------------------------------------
			Try
			{
				if(isset($JSON_ARRAY, $FIELD_NAME, $SUB_INDEX) === false)
				{
					return '';
				}
				
				if($SUB_INDEX >= 0)
				{
					return $JSON_ARRAY[$FIELD_NAME][$SUB_INDEX];
				}
				else
				{
					return $JSON_ARRAY[$FIELD_NAME];
				}
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
			// --------------------------------------------
		}
		
		/**
		 * Check JSON read parameter value exists or not
		 * @param String() $JSON_ARRAY <br />Array to read<br />
		 * @param String $FIELD_NAME <br />JSON Parameter name of value<br />
		 * @param Int $SUB_INDEX <br />Index of value if JSON value consists of an array<br />
		 * @return Boolean <br />TRUE if exists<br />
		 */
		Public Static Function REF_ISSET($JSON_ARRAY, $FIELD_NAME, $SUB_INDEX = -1)
		{
			// Actual Function
			// --------------------------------------------
			Try
			{
				if(isset($JSON_ARRAY[$FIELD_NAME]) === false)
				{
					return false;
				}
			
				if($SUB_INDEX >= 0)
				{
					if(isset($JSON_ARRAY[$FIELD_NAME][$SUB_INDEX]) === false)
					{
						return false;
					}
					else 
					{
						return true;
					}
				}
				else
				{
					return true;
				}
			}
			Catch(Exception $ERR)
			{
				PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
				Throw $ERR;
			}
			// --------------------------------------------
		}
		
		
	}
?>