<?php
 
/**
 
 * Wapple Architect communication class
 
 * 
 
 * This class allows you to communicate with Wapple Architect's web services via SOAP without having to worry
 
 * about all of the commands to do it.
 
 * 
 
 * @author Rich Gubby
 
 * @version 1.0
 
 * @package WappleArchitect
 
 */ 
 
class waplComms
 
{
 
    /**
 
     * Store SOAP client
 
     * 
 
     * @access public
 
     * @var object
 
     */
 
    public $sClient;
 
    
 
    /**
 
     * Reformatted device header information
 
     * 
 
     * @access public
 
     * @var array
 
     */
 
    public $headers = array();
 
    
 
    /**
 
     * Hold a reference to the parent
 
     * 
 
     * @access public
 
     * @var object
 
     */
 
    public $parent;
 
    
 
    /**
 
     * Constructor class
 
     * 
 
     * @access public
 
     * @return void
 
     */
 
    public function __construct($parent)
 
    {
 
        // Setup reference to parent
 
        $this->parent = $parent;
 
        
 
        // Setup a new SOAP client
 
        if(class_exists('SoapClient'))
 
        {
 
            $this->sClient = new SoapClient('http://webservices.wapple.net/wapl.wsdl', array('connection_timeout' => 5));
 
        } else
 
        {
 
            $this->parent->errorMessage['comms'][] = 'No SOAP client';
 
        }
 
        
 
        // Check simple XML is installed
 
        if(!function_exists('simplexml_load_string'))
 
        {
 
            $this->parent->errorMessage['comms'][] = 'SimpleXML is not installed';
 
        }
 
        
 
        // Setup device headers
 
        $this->getDeviceHeaders();
 
    }
 
    
 
    /**
 
     * Create device headers
 
     * 
 
     * @access public
 
     * @return void
 
     */
 
    public function getDeviceHeaders()
 
    {
 
        if(empty($this->headers))
 
        {
 
            foreach($_SERVER as $key => $val)
 
            {
 
                $this->headers[] = array('name' => $key, 'value' => $val);
 
            }
 
        }
 
    }
 
    
 
    /**
 
     * Check credentials when doing anything
 
     * 
 
     * @param array $options
 
     * @access private
 
     * @return boolean
 
     */
 
    private function __checkCredentials()
 
    {
 
        // Check a dev key has been set
 
        if(!isset($this->parent->devKey) OR $this->parent->devKey == '')
 
        {
 
            $this->parent->errorMessage['comms'][] = 'No dev key set';
 
            return false;
 
        }
 
        
 
        // Check headers have been set
 
        if(empty($this->headers))
 
        {
 
            $this->parent->errorMessage['comms'][] = 'No device headers';
 
            return false;
 
        }
 
        
 
        if(!$this->sClient)
 
        {
 
            $this->parent->errorMessage['comms'][] = 'No SOAP client';
 
            return false;
 
        }
 
        
 
        // Check simple XML is installed
 
        if(!function_exists('simplexml_load_string'))
 
        {
 
            $this->parent->errorMessage['comms'][] = 'SimpleXML is not installed';
 
            return false;
 
        }
 
        
 
        return true;
 
    }
 
    
 
    /**
 
     * Check if a device is mobile or not
 
     * 
 
     * @access protected
 
     * @return boolean
 
     */
 
    public function isMobileDevice()
 
    {
 
        if($this->parent->useCookies == true)
 
        {
 
            if(isset($_COOKIE['isMobile']))
 
            {
 
                return $_COOKIE['isMobile'];
 
            }
 
        }
 
        
 
        // Check everything is setup correctly
 
        if($this->__checkCredentials())
 
        {
 
            if(@$this->sClient->isMobileDevice($this->__getParams()))
 
            {
 
                $this->parent->setCookie('1');
 
                return true;
 
            }
 
        }
 
        $this->parent->setCookie('0');
 
        return false;
 
    }
 
    
 
    /**
 
     * Get mobile device information
 
     * 
 
     * @access protected
 
     * @return object
 
     */
 
    public function getMobileDevice()
 
    {
 
        if($this->__checkCredentials())
 
        {
 
            return simplexml_load_string(@$this->sClient->getMobileDevice($this->__getParams()));
 
        }
 
    }
 
    
 
    /**
 
     * Get markup from WAPL
 
     * 
 
     * @access protected
 
     * @return object
 
     */
 
    public function getMarkupFromWapl($wapl)
 
    {
 
        if($this->__checkCredentials())
 
        {
 
            return simplexml_load_string(@$this->sClient->getMarkupFromWapl($this->__getParams(array('wapl' => $wapl)))); 
 
        }
 
    }
 
    
 
    /**
 
     * Display markup from WAPL
 
     * 
 
     * @param string $wapl
 
     * @access protected
 
     * @return void
 
     */
 
    public function displayMarkupFromWapl($wapl)
 
    {
 
        $this->__displayMarkup($this->getMarkupFromWapl($wapl));
 
    }
 
    
 
    /**
 
     * Get markup from a URL
 
     * 
 
     * @param string $url
 
     * @access protected
 
     * @return object
 
     */
 
    public function getMarkupFromUrl($url)
 
    {
 
        if($this->__checkCredentials())
 
        {
 
            return simplexml_load_string(@$this->sClient->getMarkupFromUrl($this->__getParams(array('waplUrl' => $url))));
 
        }
 
    }
 
    
 
    /**
 
     * Display markup from URL
 
     * 
 
     * @param string $url
 
     * @access protected
 
     * @return void
 
     */
 
    public function displayMarkupFromUrl($url)
 
    {
 
        $this->__displayMarkup($this->getMarkupFromUrl($url));
 
    }
 
    
 
    /**
 
     *  Display markup on screen from XML headers and markup
 
     *  
 
     * @param object $xml
 
     * @access private
 
     * @return void
 
     */
 
    private function __displayMarkup($xml)
 
    {
 
        foreach($xml->header->item as $header)
 
        {
 
            header($header);
 
        }
 
        echo trim($xml->markup);
 
    }
 
    
 
    /**
 
     * Get parameters to pass through in a SOAP call
 
     * 
 
     * @param array $options
 
     * @access private
 
     * @return array
 
     */
 
    private function __getParams($options = array())
 
    {
 
        return array_merge(
 
            array(
 
                'devKey' => $this->parent->devKey,
 
                'deviceHeaders' => $this->headers
 
            ), $options
 
        );
 
    }
 
    
 
 
}
 
?>
 
 |