LiveDocx.php 10.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Service
 * @subpackage LiveDocx
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: LiveDocx.php 23953 2011-05-03 05:47:39Z ralph $
 */

/**
 * @category   Zend
 * @package    Zend_Service
 * @subpackage LiveDocx
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @since      LiveDocx 1.0
 */
class Zend_Service_LiveDocx
{
    /**
     * LiveDocx service version
     * @since LiveDocx 1.0
     */
    const VERSION = '2.0';

    /**
     * SOAP client used to connect to LiveDocx service
     * @var   Zend_Soap_Client
     * @since LiveDocx 1.0
     */
    protected $_soapClient;

    /**
     * WSDL of LiveDocx web service
     * @var   string
     * @since LiveDocx 1.0
     */
    protected $_wsdl;

    /**
     * Array of credentials (username and password) to log into backend server
     * @var   array
     * @since LiveDocx 1.2
     */
    protected $_credentials;

    /**
     * Set to true, when session is logged into backend server
     * @var   boolean
     * @since LiveDocx 1.2
     */
    protected $_loggedIn;

    /**
     * Constructor
     *
     * Optionally, pass an array of options (or Zend_Config object).
     *
     * If an option with the key 'soapClient' is provided, that value will be
     * used to set the internal SOAP client used to connect to the LiveDocx
     * service.
     *
     * Use 'soapClient' in the case that you have a dedicated or (locally
     * installed) licensed LiveDocx server. For example:
     *
     * {code}
     * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
     *     array (
     *         'username'   => 'myUsername',
     *         'password'   => 'myPassword',
     *         'soapClient' => new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
     *     )
     * );
     * {code}
     *
     * Replace the URI of the WSDL in the constructor of Zend_Soap_Client with
     * that of your dedicated or licensed LiveDocx server.
     *
     * If you are using the public LiveDocx server, simply pass 'username' and
     * 'password'. For example:
     *
     * {code}
     * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
     *     array (
     *         'username' => 'myUsername',
     *         'password' => 'myPassword'
     *     )
     * );
     * {code}
     *
     * If you prefer to not pass the username and password through the
     * constructor, you can also call the following methods:
     *
     * {code}
     * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
     *
     * $phpLiveDocx->setUsername('myUsername')
     *             ->setPassword('myPassword');
     * {/code}
     *
     * Or, if you want to specify your own SoapClient:
     *
     * {code}
     * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
     *
     * $phpLiveDocx->setUsername('myUsername')
     *             ->setPassword('myPassword');
     *
     * $phpLiveDocx->setSoapClient(
     *     new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
     * );
     * {/code}
     *
     * @param  array|Zend_Config $options
     * @return void
     * @throws Zend_Service_LiveDocx_Exception
     * @since  LiveDocx 1.0
     */
    public function __construct($options = null)
    {
        $this->_credentials = array();
        $this->_loggedIn = false;

        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        }

        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    /**
     * Set options
     * One or more of username, password, soapClient
     *
     * @param  array $options
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.2
     */
    public function setOptions(array $options)
    {
        foreach ($options as $key => $value) {
            $method = 'set' . $key;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }

        return $this;
    }

    /**
     * Clean up and log out of LiveDocx service
     *
     * @return boolean
     * @since  LiveDocx 1.0
     */
    public function __destruct()
    {
        return $this->logOut();
    }

    /**
     * Init Soap client - connect to SOAP service
     *
     * @param  string $endpoint
     * @throws Zend_Service_LiveDocx_Exception
     * @return void
     * @since  LiveDocx 1.2
     */
    protected function _initSoapClient($endpoint)
    {
        try {
            require_once 'Zend/Soap/Client.php';
            $this->_soapClient = new Zend_Soap_Client();
            $this->_soapClient->setWsdl($endpoint);
        } catch (Zend_Soap_Client_Exception $e) {
            require_once 'Zend/Service/LiveDocx/Exception.php';
            throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e);
        }
    }

    /**
     * Get SOAP client
     *
     * @return Zend_Soap_Client
     * @since  LiveDocx 1.2
     */
    public function getSoapClient()
    {
        return $this->_soapClient;
    }

    /**
     * Set SOAP client
     *
     * @param  Zend_Soap_Client $soapClient
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.2
     */
    public function setSoapClient(Zend_Soap_Client $soapClient)
    {
        $this->_soapClient = $soapClient;
        return $this;
    }

    /**
     * Log in to LiveDocx service
     *
     * @param string $username
     * @param string $password
     *
     * @throws Zend_Service_LiveDocx_Exception
     * @return boolean
     * @since  LiveDocx 1.2
     */
    public function logIn()
    {
        if (!$this->isLoggedIn()) {
            if (null === $this->getUsername()) {
                require_once 'Zend/Service/LiveDocx/Exception.php';
                throw new Zend_Service_LiveDocx_Exception(
                    'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation'
                );
            }

            if (null === $this->getPassword()) {
                require_once 'Zend/Service/LiveDocx/Exception.php';
                throw new Zend_Service_LiveDocx_Exception(
                    'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation'
                );
            }

            if (null === $this->getSoapClient()) {
                $this->_initSoapClient($this->_wsdl);
            }

            try {
                $this->getSoapClient()->LogIn(array(
                    'username' => $this->getUsername(),
                    'password' => $this->getPassword(),
                ));
                $this->_loggedIn = true;
            } catch (Exception $e) {
                require_once 'Zend/Service/LiveDocx/Exception.php';
                throw new Zend_Service_LiveDocx_Exception(
                    'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e
                );
            }
        }

        return $this->_loggedIn;
    }

    /**
     * Log out of the LiveDocx service
     *
     * @throws Zend_Service_LiveDocx_Exception
     * @return boolean
     * @since  LiveDocx 1.2
     */
    public function logOut()
    {
        if ($this->isLoggedIn()) {
            try {
                $this->getSoapClient()->LogOut();
                $this->_loggedIn = false;
            } catch (Exception $e) {
                require_once 'Zend/Service/LiveDocx/Exception.php';
                throw new Zend_Service_LiveDocx_Exception(
                    'Cannot log out of LiveDocx service', 0, $e
                );
            }
        }

        return $this->_loggedIn;
    }

    /**
     * Return true, if session is currently logged into the backend server
     *
     * @return boolean
     * @since  LiveDocx 1.2
     */
    public function isLoggedIn()
    {
        return $this->_loggedIn;
    }

    /**
     * Set username
     *
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.0
     */
    public function setUsername($username)
    {
        $this->_credentials['username'] = $username;
        return $this;
    }

    /**
     * Set password
     *
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.0
     */
    public function setPassword($password)
    {
        $this->_credentials['password'] = $password;
        return $this;
    }

    /**
     * Set WSDL of LiveDocx web service
     *
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.0
     */
    public function setWsdl($wsdl)
    {
        $this->_wsdl = $wsdl;
        return $this;
    }

    /**
     * Return current username
     *
     * @return string|null
     * @since  LiveDocx 1.0
     */
    public function getUsername()
    {
        if (isset($this->_credentials['username'])) {
            return $this->_credentials['username'];
        }

        return null;
    }

    /**
     * Return current password
     *
     * @return string|null
     * @since  LiveDocx 1.0
     */
    public function getPassword()
    {
        if (isset($this->_credentials['password'])) {
            return $this->_credentials['password'];
        }

        return null;
    }

    /**
     * Return WSDL of LiveDocx web service
     *
     * @return Zend_Service_LiveDocx
     * @since  LiveDocx 1.0
     */
    public function getWsdl()
    {
        return $this->_wsdl;
    }

    /**
     * Return the document format (extension) of a filename
     *
     * @param  string $filename
     * @return string
     * @since  LiveDocx 1.0
     */
    public function getFormat($filename)
    {
        return strtolower(substr(strrchr($filename, '.'), 1));
    }

    /**
     * Return the current API version
     *
     * @return string
     * @since  LiveDocx 1.0
     */
    public function getVersion()
    {
        return self::VERSION;
    }

    /**
     * Compare the current API version with another version
     *
     * @param  string $version (STRING NOT FLOAT)
     * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version)
     * @since  LiveDocx 1.0
     */
    public function compareVersion($version)
    {
        return version_compare($version, $this->getVersion());
    }
}