<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * @file
 * Dynamically generated connection details for a Service Provider.
 * This file is used for cron based API 'Actions'
 * This is from Naksh Soft, the API remote client enabled connection.
 * @link https://www.nakshsoft.com UnlockingPortal Client Remote API. @endlink
 */

define("SUWP_UPAPI_REQUESTFORMAT_ACCOUNTINFO_SUAPIPROVIDERNUM", "JSON");
define("SUWP_UPAPI_DHRUFUSION_URL_ACCOUNTINFO_SUAPIPROVIDERNUM", $suwp_dhru_url);
define("SUWP_UPAPI_USERNAME_ACCOUNTINFO_SUAPIPROVIDERNUM", $suwp_dhru_username);
define("SUWP_UPAPI_API_ACCESS_KEY_ACCOUNTINFO_SUAPIPROVIDERNUM", $suwp_dhru_api_key);


if (!extension_loaded('curl')) {
  trigger_error('cURL extension not installed', E_USER_ERROR);
}
class StockunlocksUPAPI_ACCOUNTINFO_SUAPIPROVIDERNUM {
    var $objCurl;
    var $result = array();
    var $xml='';

    function getResult()
    {
      return $this->result;
    }
      
    function checkError($result)
    {
      if(isset($result['ERR']))
      {
        // echo '<h2> Error Code: ' . $result['STATUS'] . '</h2>';
        // echo '<h3>' . $result['ERR'] . '</h3>';
        exit;
      }
    }
    
      function action($toDo, $parameters = array())
      {
      if (is_string($toDo))
      {
        if (is_array($parameters))
        {
          $parameters['apiKey'] = SUWP_UPAPI_API_ACCESS_KEY_ACCOUNTINFO_SUAPIPROVIDERNUM;
          $parameters['userId'] = SUWP_UPAPI_USERNAME_ACCOUNTINFO_SUAPIPROVIDERNUM;
          $parameters['action'] = $toDo;
          $this->objCurl = curl_init( );
          curl_setopt( $this->objCurl, CURLOPT_HEADER, false );
          curl_setopt( $this->objCurl, CURLOPT_CONNECTTIMEOUT, 10 );
          curl_setopt( $this->objCurl, CURLOPT_TIMEOUT, 60 );
          curl_setopt( $this->objCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
          // curl_setopt( $this->objCurl, CURLOPT_FOLLOWLOCATION, true );
          curl_setopt( $this->objCurl, CURLOPT_RETURNTRANSFER, true );
          if( is_array( $parameters ) ):
            $vars = implode( '&', $parameters);
          endif;
          curl_setopt( $this->objCurl, CURLOPT_URL, SUWP_UPAPI_DHRUFUSION_URL_ACCOUNTINFO_SUAPIPROVIDERNUM .'/UPAPI_api/index.php');
          curl_setopt( $this->objCurl, CURLOPT_POST, true );
          curl_setopt( $this->objCurl, CURLOPT_POSTFIELDS, $parameters);
          $this->result = curl_exec( $this->objCurl );
          curl_close($this->objCurl);
        }
      }
      }

    function XmlToArray($xml)
    {
      $this->xml = $xml;	
    }

    function _struct_to_array($values, &$i)
    {
      $child = array(); 
      if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']); 
      
      while ($i++ < count($values)) { 
        switch ($values[$i]['type']) { 
          case 'cdata': 
                array_push($child, $values[$i]['value']); 
          break; 
          
          case 'complete': 
            $name = $values[$i]['tag'];
            if( !empty($name)  && array_key_exists('value', $values[$i]) ) {
              $child[$name]= ($values[$i]['value'])?($values[$i]['value']):''; 
              if(isset($values[$i]['attributes'])) {					
                $child[$name] = $values[$i]['attributes']; 
              } 
            }	
              break; 
          
          case 'open': 
            $name = $values[$i]['tag']; 
            $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
            $child[$name][$size] = $this->_struct_to_array($values, $i); 
          break;
          
          case 'close': 
                return $child; 
          break; 
        }
      }
      return $child; 
    }//_struct_to_array
    function createArray()
    { 
      $xml    = $this->xml;
      $values = array(); 
      $index  = array(); 
      $array  = array(); 
      $parser = xml_parser_create(); 
      xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
      xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
      xml_parse_into_struct($parser, $xml, $values, $index);
      xml_parser_free($parser);
      $i = 0; 
      $name = $values[$i]['tag']; 
      $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : ''; 
      $array[$name] = $this->_struct_to_array($values, $i); 
      return $array; 
    }//createArray
}
