File indexing completed on 2024-05-12 06:03:06

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage LiveDocx
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Service
0026  * @subpackage LiveDocx
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  * @since      LiveDocx 1.0
0030  */
0031 class Zend_Service_LiveDocx
0032 {
0033     /**
0034      * LiveDocx service version
0035      * @since LiveDocx 1.0
0036      */
0037     const VERSION = '2.0';
0038 
0039     /**
0040      * SOAP client used to connect to LiveDocx service
0041      * @var   Zend_Soap_Client
0042      * @since LiveDocx 1.0
0043      */
0044     protected $_soapClient;
0045 
0046     /**
0047      * WSDL of LiveDocx web service
0048      * @var   string
0049      * @since LiveDocx 1.0
0050      */
0051     protected $_wsdl;
0052 
0053     /**
0054      * Array of credentials (username and password) to log into backend server
0055      * @var   array
0056      * @since LiveDocx 1.2
0057      */
0058     protected $_credentials;
0059 
0060     /**
0061      * Set to true, when session is logged into backend server
0062      * @var   boolean
0063      * @since LiveDocx 1.2
0064      */
0065     protected $_loggedIn;
0066 
0067     /**
0068      * Constructor
0069      *
0070      * Optionally, pass an array of options (or Zend_Config object).
0071      *
0072      * If an option with the key 'soapClient' is provided, that value will be
0073      * used to set the internal SOAP client used to connect to the LiveDocx
0074      * service.
0075      *
0076      * Use 'soapClient' in the case that you have a dedicated or (locally
0077      * installed) licensed LiveDocx server. For example:
0078      *
0079      * {code}
0080      * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
0081      *     array (
0082      *         'username'   => 'myUsername',
0083      *         'password'   => 'myPassword',
0084      *         'soapClient' => new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
0085      *     )
0086      * );
0087      * {code}
0088      *
0089      * Replace the URI of the WSDL in the constructor of Zend_Soap_Client with
0090      * that of your dedicated or licensed LiveDocx server.
0091      *
0092      * If you are using the public LiveDocx server, simply pass 'username' and
0093      * 'password'. For example:
0094      *
0095      * {code}
0096      * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
0097      *     array (
0098      *         'username' => 'myUsername',
0099      *         'password' => 'myPassword'
0100      *     )
0101      * );
0102      * {code}
0103      *
0104      * If you prefer to not pass the username and password through the
0105      * constructor, you can also call the following methods:
0106      *
0107      * {code}
0108      * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
0109      *
0110      * $phpLiveDocx->setUsername('myUsername')
0111      *             ->setPassword('myPassword');
0112      * {/code}
0113      *
0114      * Or, if you want to specify your own SoapClient:
0115      *
0116      * {code}
0117      * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
0118      *
0119      * $phpLiveDocx->setUsername('myUsername')
0120      *             ->setPassword('myPassword');
0121      *
0122      * $phpLiveDocx->setSoapClient(
0123      *     new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
0124      * );
0125      * {/code}
0126      *
0127      * @param  array|Zend_Config $options
0128      * @throws Zend_Service_LiveDocx_Exception
0129      * @since  LiveDocx 1.0
0130      */
0131     public function __construct($options = null)
0132     {
0133         $this->_credentials = array();
0134         $this->_loggedIn = false;
0135 
0136         if ($options instanceof Zend_Config) {
0137             $options = $options->toArray();
0138         }
0139 
0140         if (is_array($options)) {
0141             $this->setOptions($options);
0142         }
0143     }
0144 
0145     /**
0146      * Set options
0147      * One or more of username, password, soapClient
0148      *
0149      * @param  array $options
0150      * @return Zend_Service_LiveDocx
0151      * @since  LiveDocx 1.2
0152      */
0153     public function setOptions(array $options)
0154     {
0155         foreach ($options as $key => $value) {
0156             $method = 'set' . $key;
0157             if (method_exists($this, $method)) {
0158                 $this->$method($value);
0159             }
0160         }
0161 
0162         return $this;
0163     }
0164 
0165     /**
0166      * Clean up and log out of LiveDocx service
0167      *
0168      * @return boolean
0169      * @since  LiveDocx 1.0
0170      */
0171     public function __destruct()
0172     {
0173         return $this->logOut();
0174     }
0175 
0176     /**
0177      * Init Soap client - connect to SOAP service
0178      *
0179      * @param  string $endpoint
0180      * @throws Zend_Service_LiveDocx_Exception
0181      * @return void
0182      * @since  LiveDocx 1.2
0183      */
0184     protected function _initSoapClient($endpoint)
0185     {
0186         try {
0187             // require_once 'Zend/Soap/Client.php';
0188             $this->_soapClient = new Zend_Soap_Client();
0189             $this->_soapClient->setWsdl($endpoint);
0190         } catch (Zend_Soap_Client_Exception $e) {
0191             // require_once 'Zend/Service/LiveDocx/Exception.php';
0192             throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e);
0193         }
0194     }
0195 
0196     /**
0197      * Get SOAP client
0198      *
0199      * @return Zend_Soap_Client
0200      * @since  LiveDocx 1.2
0201      */
0202     public function getSoapClient()
0203     {
0204         return $this->_soapClient;
0205     }
0206 
0207     /**
0208      * Set SOAP client
0209      *
0210      * @param  Zend_Soap_Client $soapClient
0211      * @return Zend_Service_LiveDocx
0212      * @since  LiveDocx 1.2
0213      */
0214     public function setSoapClient(Zend_Soap_Client $soapClient)
0215     {
0216         $this->_soapClient = $soapClient;
0217         return $this;
0218     }
0219 
0220     /**
0221      * Log in to LiveDocx service
0222      *
0223      * @throws Zend_Service_LiveDocx_Exception
0224      * @return boolean
0225      * @since  LiveDocx 1.2
0226      */
0227     public function logIn()
0228     {
0229         if (!$this->isLoggedIn()) {
0230             if (null === $this->getUsername()) {
0231                 // require_once 'Zend/Service/LiveDocx/Exception.php';
0232                 throw new Zend_Service_LiveDocx_Exception(
0233                     'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation'
0234                 );
0235             }
0236 
0237             if (null === $this->getPassword()) {
0238                 // require_once 'Zend/Service/LiveDocx/Exception.php';
0239                 throw new Zend_Service_LiveDocx_Exception(
0240                     'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation'
0241                 );
0242             }
0243 
0244             if (null === $this->getSoapClient()) {
0245                 $this->_initSoapClient($this->_wsdl);
0246             }
0247 
0248             try {
0249                 $this->getSoapClient()->LogIn(array(
0250                     'username' => $this->getUsername(),
0251                     'password' => $this->getPassword(),
0252                 ));
0253                 $this->_loggedIn = true;
0254             } catch (Exception $e) {
0255                 // require_once 'Zend/Service/LiveDocx/Exception.php';
0256                 throw new Zend_Service_LiveDocx_Exception(
0257                     'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e
0258                 );
0259             }
0260         }
0261 
0262         return $this->_loggedIn;
0263     }
0264 
0265     /**
0266      * Log out of the LiveDocx service
0267      *
0268      * @throws Zend_Service_LiveDocx_Exception
0269      * @return boolean
0270      * @since  LiveDocx 1.2
0271      */
0272     public function logOut()
0273     {
0274         if ($this->isLoggedIn()) {
0275             try {
0276                 $this->getSoapClient()->LogOut();
0277                 $this->_loggedIn = false;
0278             } catch (Exception $e) {
0279                 // require_once 'Zend/Service/LiveDocx/Exception.php';
0280                 throw new Zend_Service_LiveDocx_Exception(
0281                     'Cannot log out of LiveDocx service', 0, $e
0282                 );
0283             }
0284         }
0285 
0286         return $this->_loggedIn;
0287     }
0288 
0289     /**
0290      * Return true, if session is currently logged into the backend server
0291      *
0292      * @return boolean
0293      * @since  LiveDocx 1.2
0294      */
0295     public function isLoggedIn()
0296     {
0297         return $this->_loggedIn;
0298     }
0299 
0300     /**
0301      * Set username
0302      *
0303      * @param string $username
0304      * @return Zend_Service_LiveDocx
0305      * @since  LiveDocx 1.0
0306      */
0307     public function setUsername($username)
0308     {
0309         $this->_credentials['username'] = $username;
0310         return $this;
0311     }
0312 
0313     /**
0314      * Set password
0315      *
0316      * @param string $password
0317      * @return Zend_Service_LiveDocx
0318      * @since  LiveDocx 1.0
0319      */
0320     public function setPassword($password)
0321     {
0322         $this->_credentials['password'] = $password;
0323         return $this;
0324     }
0325 
0326     /**
0327      * Set WSDL of LiveDocx web service
0328      *
0329      * @param string $wsdl
0330      * @return Zend_Service_LiveDocx
0331      * @since  LiveDocx 1.0
0332      */
0333     public function setWsdl($wsdl)
0334     {
0335         $this->_wsdl = $wsdl;
0336         return $this;
0337     }
0338 
0339     /**
0340      * Return current username
0341      *
0342      * @return string|null
0343      * @since  LiveDocx 1.0
0344      */
0345     public function getUsername()
0346     {
0347         if (isset($this->_credentials['username'])) {
0348             return $this->_credentials['username'];
0349         }
0350 
0351         return null;
0352     }
0353 
0354     /**
0355      * Return current password
0356      *
0357      * @return string|null
0358      * @since  LiveDocx 1.0
0359      */
0360     public function getPassword()
0361     {
0362         if (isset($this->_credentials['password'])) {
0363             return $this->_credentials['password'];
0364         }
0365 
0366         return null;
0367     }
0368 
0369     /**
0370      * Return WSDL of LiveDocx web service
0371      *
0372      * @return string
0373      * @since  LiveDocx 1.0
0374      */
0375     public function getWsdl()
0376     {
0377         return $this->_wsdl;
0378     }
0379 
0380     /**
0381      * Return the document format (extension) of a filename
0382      *
0383      * @param  string $filename
0384      * @return string
0385      * @since  LiveDocx 1.0
0386      */
0387     public function getFormat($filename)
0388     {
0389         return strtolower(substr(strrchr($filename, '.'), 1));
0390     }
0391 
0392     /**
0393      * Return the current API version
0394      *
0395      * @return string
0396      * @since  LiveDocx 1.0
0397      */
0398     public function getVersion()
0399     {
0400         return self::VERSION;
0401     }
0402 
0403     /**
0404      * Compare the current API version with another version
0405      *
0406      * @param  string $version (STRING NOT FLOAT)
0407      * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version)
0408      * @since  LiveDocx 1.0
0409      */
0410     public function compareVersion($version)
0411     {
0412         return version_compare($version, $this->getVersion());
0413     }
0414 }