SOAP/REST combinatie API php

Combinatie API zowel RESTful als SOAP, in php met een WSDL

/**
	 *  AOZ Soap Class
	 */
	class soapServiceFunctions { 
		public $authorized 	= false;
		public $testmode	= false;

		/**
		 *  Check auhtorization
		 */
		function authorization($header) {
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			global $_SERVER;
			if ( isset($header->username) && isset($header->hash) ) 
			{ 
				if ( $header->username == "testuser" && $header->hash == "7f3be433c9bab15315e23fbb4664d33c6074211b" ) {
					$this->authorized 			= true;
					$this->testmode				= true;
					$this->userData["userid"] 	= 1;
				} else {
					$this->userData	= $this->auth->checkWSDLAccess($header->username, $header->hash, $_SERVER["REMOTE_ADDR"]);
					if ( $this->userData ) $this->authorized 	= true;
					$this->userData["userid"] 	= $this->userData["id"];
				}
			} 
		} 

		/**
		 *  Exception handler
		 */
		function serviceAPIException($errNr, $msg, $call)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			switch ( $this->serviceType ) {
				case "SOAP":
					$this->soap->fault($errNr, $msg, $call, "Contact the AOZ support department on 076 - 74 10 100.");
				break;;
				default:
					throw new Exception($errNr . "::" . $msg . "::" . $call . "]->" . "Contact the AOZ support department on 076 - 74 10 100.");
				break;;
			}
		}
		
		/**
		 *  Request for bag (basisregistratie adressen information ...
		 */
		function someFunction(
					$firstName,
					$lastName,
		) {	
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			
			return(
				array(
					"processData"				=> $this->serviceConversion($this->procData),
					"aozBagRequest"				=> $this->serviceConversion($rs),
					"dataResources"				=> null	
				)
			);
		}

		/**
		 *  Convert to API 
		 */
		function serviceConversion($dataSet)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			switch ( $this->serviceType ) {
				case "SOAP":
					return(new SoapVar($dataSet, SOAP_ENC_OBJECT));
				break;;
				default:
					return($dataSet);
				break;;
			}
		}
		
		/**
		 *  API Constructor ...
		 */
		function __construct($soapService, $sql, $auth, $crypt, $conversionTbl, $serviceType = "SOAP")
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			global $logData;
			$this->logData			= &$logData;
			$this->soap 			= $soapService;
			$this->sql 				= $sql;
			$this->auth 			= $auth;
			$this->crypt			= $crypt;
			$this->conversionTbl	= $conversionTbl;
			$this->serviceType		= $serviceType;
			$this->logData["API"]	= $serviceType;
			$this->locationImages	= true;
			$this->imageHashes		= array();
			$this->procData			= array(
											"calls"		=> 0,
											"resources"	=> 0,
											"runtime"	=> microtime(true),
											"date"		=> time(),
											"errors"	=> 0,
											"warnings"	=> 0,
											"images"	=> 0,
											"timezone"	=> date_default_timezone_get()
										);
		}
	} 
	
	/**
	 *  RESTful API class
	 */
	abstract class RESTFullAPI extends soapServiceFunctions { 
		/**
		 *  Request vars
		 */
		protected $method		= "";
		protected $endpoint		= "";
		protected $verb			= "";
		protected $args			= Array();
		protected $file			= null;
		protected $username		= "";
		protected $hash			= "";
		
		/**
		 *  API Entrypoint
		 */
		
		/**
		 *  Response function
		 */
		function __response($data, $statusCode = 200)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			header("HTTP/1.1 " . $statusCode . " " . $this->__requestStatus($statusCode));
			return json_encode($data);
		}
		
		/**
		 *  Cleanup input data
		 */
		function __cleanInput($data)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			$clean_input = Array();
			if (is_array($data)) {
				foreach ($data as $k => $v) {
					$clean_input[$k] = $this->__cleanInput($v);
				}
			} else {
				$clean_input = trim(strip_tags($data));
			}
			return $clean_input;
		}
		
		/**
		 *  Check request status
		 */
		function __requestStatus($statusCode)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			$statusCodes = array(
								200 => "OK",
								404 => "Not Found",
								405 => "Method Not Allowed",
								500 => "Internal Server Error"
							);
			return($statusCodes[$statusCode] ? $statusCodes[$statusCode] : $statusCodes[500]);
		}
		
		/**
		 *  RESTful API constructor
		 */
		function __construct($requestInput)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			global $server, $sql, $auth, $crypt, $conversionTbl, $apiClass;
			$apiClass = $this;
			
			/**
			 * Response headers 
			 */
			header("Access-Control-Allow-Orgin: *");
			header("Access-Control-Allow-Methods: *");
			header("Content-Type: application/json");
			
			/**
			 *  Parse arguments
			 */
			$this->args 	= explode("/", rtrim($requestInput));
			$this->endpoint	= array_shift($this->args);
			$this->method	= $_SERVER["REQUEST_METHOD"];
			
			/**
			 *  Check request method
			 */
			if ( $this->method == "POST" && array_key_exists("HTTP_X_HTTP_METHOD", $_SERVER) )
			{
				switch ( $_SERVER["HTTP_X_HTTP_METHOD"] ) {
					case "DELETE":
					case "PUT":
						$this->method = $_SERVER["HTTP_X_HTTP_METHOD"];
					break;;
					default:
						$this->serviceAPIException("400", "Unexpected headers found ...", __CLASS__ . "::" . __FUNCTION__);
					break;;
				}
			}
			
			/**
			 *  Parse data for correct REQUEST method
			 */
			switch ( $this->method ) {
				case "DELETE":
				case "POST":
					$this->request 	= $this->__cleanInput($_POST);
				break;;
				case "GET":
					$this->request 	= $this->__cleanInput($_GET);
				break;;
				case "PUT":
					$this->request 	= $this->__cleanInput($_GET);
					$this->file		= file_get_contents("php://input");
				break;;
				default:
					$this->__response("Invalid method", 405);
				break;;
			}
			
			parent::__construct($server, $sql, $auth, $crypt, $conversionTbl, "RESTFUL");
		}
		
		/**
		 *  Get Arguments from class
		 */
		function getArgs()
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			return($this->args);
		}
		
		function processAPI() {
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			if (method_exists($this, $this->endpoint)) {
				$cc 		= 0;
				$arguments 	= "";
				foreach ($this->args as $var ) {
					$varList[$cc] = $var;
					$arguments .= "\$varList[$cc], ";
					$cc++;
				}
				
				$reflection = new ReflectionMethod('soapServiceClass', $this->endpoint);
				if ( $reflection->getNumberOfRequiredParameters() > $cc ) {
					$this->serviceAPIException("428", "Required arguments missing ...", __CLASS__ . "::" . __FUNCTION__);
				} else {
					$arguments  = substr($arguments, 0, strlen($arguments) - 2);
					$call 		= "\$result = \$this->{\$this->endpoint}(" . $arguments . ");";
					eval($call);
					return $this->__response($result);
				}
			}
			return $this->__response("No Endpoint: $this->endpoint", 404);
		}
		
		/**
		 *  RESTful API deconstructor
		 */
		function __deconstruct()
		{
			
		}
	}
	
	/**
	 *  This RESTful API entrypoint
	 */
	class RESTApi extends RESTFullAPI {		
		/**
		 *  API Constructor
		 */
		function __construct($request, $origin, $start)
		{
			logSysCall(( __CLASS__ == "" ? __FUNCTION__ : __METHOD__), __LINE__, __FILE__);
			$request = strstr($request, $start);
			parent::__construct($request);
			
			if ( isset($this->request["username"]) && isset($this->request["hash"]) ) 
			{ 
				/**
				 *  Are we a test user?
				 */
				if ( $this->request["username"] == "testuser" && $this->request["hash"] == "7f3be433c9bab15315e23fbb4664d33c6074211b" ) {
					$this->authorized 			= true;
					$this->testmode				= true;
					$this->userData["userid"] 	= 1;
				} else {
					/**
					 *  Check user WSDL access
					 */
					$this->userData	= $this->auth->checkWSDLAccess($this->request["username"], $this->request["hash"], $_SERVER["REMOTE_ADDR"]);
					if ( $this->userData ) $this->authorized 	= true;
					$this->userData["userid"] 	= $this->userData["id"];
				}
			} else {
				$this->serviceAPIException("400", "Missing authorization parameters ...", __CLASS__ . "::" . __FUNCTION__);
			}

		}
	}
	
	/**
	 *  Check API request type, RESTful / SOAP
	 */
	$requestHeaders 		= getallheaders();
	$logData["headers"]		= json_encode($requestHeaders);
	$logData["url"]			= $_SERVER["REQUEST_URI"];
	$logData["rawRequest"]	= file_get_contents("php://input");
	$logData["ip"]			= $_SERVER["REMOTE_ADDR"];
	
	/**
	 *  API Processing
	 */
	if ( array_key_exists("SOAPAction", $requestHeaders) || isset($_GET["wsdl"]) ) {	
		/**
		 *  SOAP/API Service Entrypoint
		 */
		if ( isset($_GET["wsdl"]) ) {
			header("Content-type: text/xml");
			/**
			 *  AOZ_WSE_ENDPOINT_PEOPLE_SEARCH Set service endpoint
			 */
			echo str_replace("%%AOZ_WSE_ENDPOINT_PEOPLE_SEARCH%%", AOZ_WSE_ENDPOINT_PEOPLE_SEARCH, file_get_contents("aozWse.wsdl"));
		} else {
			$soapServiceOptions = array(
									"soap_version"	=> SOAP_1_2,
									"cache_wsdl"	=> WSDL_CACHE_NONE,
									"send_errors"	=> true
									);
			$server = new SoapServer("aozWse.wsdl", $soapServiceOptions);
			$server->setClass("soapServiceClass", $server, $sql, $auth, $crypt, $conversionTbl);
			$server->handle();
		}
		$logData["vars"]		= "";
	} else {
		/**
		 *  RESTful API Service Entrypoint
		 */
		if ( array_key_exists("request", $_REQUEST) ) {
			if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
				$_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
			}

			try {
				$API = new RESTApi($_REQUEST['request'], $_SERVER['HTTP_ORIGIN'], $_REQUEST['start']);
				echo $API->processAPI();
				$logData["vars"] = json_encode($API->getArgs());
			} catch (Exception $e) {
				echo json_encode(Array('error' => $e->getMessage()));
			}
		} else {
			echo "RESTful API Endpoint";
		}
	}