HEX
Server: Apache
System: Linux v38079.2is.nl 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: democfellows (10015)
PHP: 8.1.34
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/creativefellows.nl/jhtaxatie.creativefellows.nl/classes/BmwtXmlController.php
<?php
	

	
	class BmwtXmlController{
		
		protected $inspection;
		protected $xml;
		protected $fields = [];
		protected $data = [];
				
	    public function __construct($inspection,$user)
		{
			$this->inspection 	= $inspection;
			$this->user 		= $user;			
		
			$this->set();
			
	    }
		
		private function set(){
		
			$current_rack_id = null;	

			foreach($this->inspection->racks() as $i => $rack)
			{
				$show = false;
				if($current_rack_id != $rack->getId()){ 
					$current_rack_id = $rack->getId();
					$show = true;
				}
				
				$rack_values = [
					"Username" => "rene",
					"Password" => "ty8pqo",
					"Type" => ($rack->rackVersionNew() ? 112 : 38),
					"ParentForm" => 0,
					"Machine" => [
						"Manufacturer" => $this->validateBMWTChars($rack->getBrand()),
						"BuildYear" => preg_replace('/\D/', '', $rack->getYear()),
						"Type" => $this->validateBMWTChars($rack->getType()),//$rack->getRackTypeLabel( $rack->labelId() ), // 0 of 1 => goedgekeurd of goedgekeurd nadat
						"SerialNumber" => $rack->getFileNumber(),
						"CheckupDate" => $rack->getDateBmwt(),
						"StickerNumber" => $rack->getStickerNumber(true),					
						"MixedFleet" => 0,
						"NewMachine" => $rack->newRack($show), // 1 = ja, 0 = nee
						"AdditionalReport" => $rack->isNew() ? "nee" : "ja",
					],						
					"Company" => [
						"Name" => $this->validateBMWTChars($this->inspection->getCompany()),
						"Address" => $this->validateBMWTChars($this->inspection->getAddress()),
						"ZipCode" => $this->inspection->getZipcode(),
						"City" => $this->inspection->getCity(),
						"PhoneNumber" => $this->inspection->getPhone(),
						"Contact" => $this->inspection->getContact(), 
					],
					"Inspector" => $this->user->getFullname(),
					"Findings" => $rack->getLabel() == 0 ? $rack->recommendations() : null,
					"Recommendations" => null,//$rack->recommendations(),
					"CheckupForm" => $rack->getXmlLines(),
					"inspectorSignature" => null,
					"customerSignature" => null,

				];
				
				$this->data[] = $rack_values;
			}
		
			//d($this->data);
			
			//die("<hr>");
		}
		
		public function validateBMWTChars($string)
		{
			return $string = htmlspecialchars($string);
		}
		
		public function generateRackXml(int $rack_index = 0)
		{
		
			$doc  = new DOMDocument('1.0', 'utf-8');
			$doc->formatOutput = true;
			
			$root = $doc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soap:Envelope');
			$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
			$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
			$doc->appendChild($root);
			
			$header = $doc->createElement('soap:Header');
			$root->appendChild($header);
			
			$body = $doc->createElement('soap:Body');
			$root->appendChild($body);
			
			$checkupForm = $doc->createElementNS('https://extranet.bmwt.nl/', 'addCheckupForm');
			$body->appendChild($checkupForm);
			
			// generate the xml for the rack
			$rack = $this->data[$rack_index];
		
			foreach($rack as $key => $value){

				if(!is_array($value)){
					$property = $doc->createElement($key,$value);
					$checkupForm->appendChild($property);
				}
				else{
				
					$group = $doc->createElement($key);
					$checkupForm->appendChild($group);
											
					foreach($value as $key => $value){
					
						if(!is_array($value)){
							$child = $doc->createElement($key,$value);
							$group->appendChild($child);
						}
						else{
							
							$line = $doc->createElement("Line");
							$group->appendChild($line);
							
							foreach($value as $key => $value){
								$child = $doc->createElement($key,$value);
								$line->appendChild($child);
							}
							
						}
						
					
					}
				}
			}
			
			$doc->save("../xml/".$rack["Machine"]["StickerNumber"].".xml");
	
	
		
			return $doc->saveXML();
	
		}
		
		public function post()
		{
			// curl multi handler
			$this->ch_mh = curl_multi_init();
			$this->ch = [];

			// responsecodes
			$this->responsesCodes = [];
			
			// loop racks in inspection
			for($i=0; $i < $this->inspection->countRacks(); $i++){
				
				// get current rack
				$rack_data = $this->inspection->racks()[$i];
			
				// set in responsecodes
				$this->responsesCodes[$i] = [
					"rackid" => $rack_data->getId(),
					"sticker" => $rack_data->getStickerNumber(),
				];
				
				// generate XML						
				$rack_xml = $this->generateRackXml($i); 
				
				// set in curl multi handler
				$this->postBMWT($i,$rack_xml);
			
			}
			
			//die("do not post to BMWT");
			//execute the multi handle
			do {
			   
			    curl_multi_exec($this->ch_mh, $active);
				
			    if ($active) {
			        curl_multi_select($this->ch_mh);
			    }
			
				
			} while ($active && $status == CURLM_OK);
			
			
			foreach(array_keys($this->ch) as $key)
			{ 

				// get response
				$response = curl_multi_getcontent($this->ch[$key]);
				
				$code = $this->getResponseCode($response);	
				$code["time"] = curl_getinfo($this->ch[$key],  CURLINFO_TOTAL_TIME);
				
				
				// set response code of request
				$current = $this->responsesCodes[$key];
				$this->responsesCodes[$key] = array_merge($current,$code);
				
				// remove handler
			  	curl_multi_remove_handle($this->ch_mh, $this->ch[$key]);
			}
			
			//die("donme");
			//curl_multi_close($this->ch_mh);
			
			return $this->responsesCodes;
			
			
		}
		
		private function postBMWT($key,$xml_payload)
		{	
			//die("do not post to BMWT");
			
			ini_set('max_execution_time', 0);
		
			//$start = microtime(true);
			
			$this->ch[$key] = curl_init();

            // debug
			//curl_setopt($this->ch[$key], CURLOPT_VERBOSE, true);
			//curl_setopt($this->ch[$key], CURLOPT_STDERR, fopen('debug.txt', 'w+'));
			
			curl_setopt($this->ch[$key], CURLOPT_ENCODING, '');
			curl_setopt($this->ch[$key], CURLOPT_URL,"https://extranet.bmwt.nl/webservice/checkupform.asmx?WDSL");
            curl_setopt($this->ch[$key], CURLOPT_RETURNTRANSFER,true);		
            //curl_setopt($$this->ch[$key], CURLOPT_TIMEOUT, 45);
            curl_setopt($this->ch[$key], CURLOPT_HTTPHEADER, [
							"POST: https://extranet.bmwt.nl/webservice/checkupform.asmx HTTP/1.1",
			            	"Content-Type: text/xml; charset=utf-8",
			            	"SOAPAction: https://extranet.bmwt.nl/addCheckupForm"
						]);
            curl_setopt($this->ch[$key], CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($this->ch[$key], CURLOPT_POSTFIELDS, $xml_payload);
			curl_setopt($this->ch[$key], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
			
		
			
			// add to multi handler
			curl_multi_add_handle($this->ch_mh, $this->ch[$key]);
	
		}
		
		private function getResponseCode($response)
		{
	
			$clean_xml = str_ireplace(['SOAP-ENV:', 'env:', 'ns1:'], '', $response);
			$xml = simplexml_load_string($clean_xml);
			
			if(!$xml->Body->addCheckupFormResponse) return ["Code" => "server", "Message" => json_decode(json_encode($xml->Body->Fault->faultstring),1)[0]];
					
			return json_decode(json_encode($xml->Body->addCheckupFormResponse->addCheckupFormResult),1); 
			
		}
	
					
			
	}

?>