LiveDocx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage LiveDocx
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage LiveDocx
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @since LiveDocx 1.0
  29. */
  30. class Zend_Service_LiveDocx
  31. {
  32. /**
  33. * LiveDocx service version
  34. * @since LiveDocx 1.0
  35. */
  36. const VERSION = '2.0';
  37. /**
  38. * SOAP client used to connect to LiveDocx service
  39. * @var Zend_Soap_Client
  40. * @since LiveDocx 1.0
  41. */
  42. protected $_soapClient;
  43. /**
  44. * WSDL of LiveDocx web service
  45. * @var string
  46. * @since LiveDocx 1.0
  47. */
  48. protected $_wsdl;
  49. /**
  50. * Array of credentials (username and password) to log into backend server
  51. * @var array
  52. * @since LiveDocx 1.2
  53. */
  54. protected $_credentials;
  55. /**
  56. * Set to true, when session is logged into backend server
  57. * @var boolean
  58. * @since LiveDocx 1.2
  59. */
  60. protected $_loggedIn;
  61. /**
  62. * Constructor
  63. *
  64. * Optionally, pass an array of options (or Zend_Config object).
  65. *
  66. * If an option with the key 'soapClient' is provided, that value will be
  67. * used to set the internal SOAP client used to connect to the LiveDocx
  68. * service.
  69. *
  70. * Use 'soapClient' in the case that you have a dedicated or (locally
  71. * installed) licensed LiveDocx server. For example:
  72. *
  73. * {code}
  74. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  75. * array (
  76. * 'username' => 'myUsername',
  77. * 'password' => 'myPassword',
  78. * 'soapClient' => new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
  79. * )
  80. * );
  81. * {code}
  82. *
  83. * Replace the URI of the WSDL in the constructor of Zend_Soap_Client with
  84. * that of your dedicated or licensed LiveDocx server.
  85. *
  86. * If you are using the public LiveDocx server, simply pass 'username' and
  87. * 'password'. For example:
  88. *
  89. * {code}
  90. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  91. * array (
  92. * 'username' => 'myUsername',
  93. * 'password' => 'myPassword'
  94. * )
  95. * );
  96. * {code}
  97. *
  98. * If you prefer to not pass the username and password through the
  99. * constructor, you can also call the following methods:
  100. *
  101. * {code}
  102. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  103. *
  104. * $phpLiveDocx->setUsername('myUsername')
  105. * ->setPassword('myPassword');
  106. * {/code}
  107. *
  108. * Or, if you want to specify your own SoapClient:
  109. *
  110. * {code}
  111. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  112. *
  113. * $phpLiveDocx->setUsername('myUsername')
  114. * ->setPassword('myPassword');
  115. *
  116. * $phpLiveDocx->setSoapClient(
  117. * new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
  118. * );
  119. * {/code}
  120. *
  121. * @param array|Zend_Config $options
  122. * @throws Zend_Service_LiveDocx_Exception
  123. * @since LiveDocx 1.0
  124. */
  125. public function __construct($options = null)
  126. {
  127. $this->_credentials = array();
  128. $this->_loggedIn = false;
  129. if ($options instanceof Zend_Config) {
  130. $options = $options->toArray();
  131. }
  132. if (is_array($options)) {
  133. $this->setOptions($options);
  134. }
  135. }
  136. /**
  137. * Set options
  138. * One or more of username, password, soapClient
  139. *
  140. * @param array $options
  141. * @return Zend_Service_LiveDocx
  142. * @since LiveDocx 1.2
  143. */
  144. public function setOptions(array $options)
  145. {
  146. foreach ($options as $key => $value) {
  147. $method = 'set' . $key;
  148. if (method_exists($this, $method)) {
  149. $this->$method($value);
  150. }
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Clean up and log out of LiveDocx service
  156. *
  157. * @return boolean
  158. * @since LiveDocx 1.0
  159. */
  160. public function __destruct()
  161. {
  162. return $this->logOut();
  163. }
  164. /**
  165. * Init Soap client - connect to SOAP service
  166. *
  167. * @param string $endpoint
  168. * @throws Zend_Service_LiveDocx_Exception
  169. * @return void
  170. * @since LiveDocx 1.2
  171. */
  172. protected function _initSoapClient($endpoint)
  173. {
  174. try {
  175. require_once 'Zend/Soap/Client.php';
  176. $this->_soapClient = new Zend_Soap_Client();
  177. $this->_soapClient->setWsdl($endpoint);
  178. } catch (Zend_Soap_Client_Exception $e) {
  179. require_once 'Zend/Service/LiveDocx/Exception.php';
  180. throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e);
  181. }
  182. }
  183. /**
  184. * Get SOAP client
  185. *
  186. * @return Zend_Soap_Client
  187. * @since LiveDocx 1.2
  188. */
  189. public function getSoapClient()
  190. {
  191. return $this->_soapClient;
  192. }
  193. /**
  194. * Set SOAP client
  195. *
  196. * @param Zend_Soap_Client $soapClient
  197. * @return Zend_Service_LiveDocx
  198. * @since LiveDocx 1.2
  199. */
  200. public function setSoapClient(Zend_Soap_Client $soapClient)
  201. {
  202. $this->_soapClient = $soapClient;
  203. return $this;
  204. }
  205. /**
  206. * Log in to LiveDocx service
  207. *
  208. * @throws Zend_Service_LiveDocx_Exception
  209. * @return boolean
  210. * @since LiveDocx 1.2
  211. */
  212. public function logIn()
  213. {
  214. if (!$this->isLoggedIn()) {
  215. if (null === $this->getUsername()) {
  216. require_once 'Zend/Service/LiveDocx/Exception.php';
  217. throw new Zend_Service_LiveDocx_Exception(
  218. 'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation'
  219. );
  220. }
  221. if (null === $this->getPassword()) {
  222. require_once 'Zend/Service/LiveDocx/Exception.php';
  223. throw new Zend_Service_LiveDocx_Exception(
  224. 'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation'
  225. );
  226. }
  227. if (null === $this->getSoapClient()) {
  228. $this->_initSoapClient($this->_wsdl);
  229. }
  230. try {
  231. $this->getSoapClient()->LogIn(array(
  232. 'username' => $this->getUsername(),
  233. 'password' => $this->getPassword(),
  234. ));
  235. $this->_loggedIn = true;
  236. } catch (Exception $e) {
  237. require_once 'Zend/Service/LiveDocx/Exception.php';
  238. throw new Zend_Service_LiveDocx_Exception(
  239. 'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e
  240. );
  241. }
  242. }
  243. return $this->_loggedIn;
  244. }
  245. /**
  246. * Log out of the LiveDocx service
  247. *
  248. * @throws Zend_Service_LiveDocx_Exception
  249. * @return boolean
  250. * @since LiveDocx 1.2
  251. */
  252. public function logOut()
  253. {
  254. if ($this->isLoggedIn()) {
  255. try {
  256. $this->getSoapClient()->LogOut();
  257. $this->_loggedIn = false;
  258. } catch (Exception $e) {
  259. require_once 'Zend/Service/LiveDocx/Exception.php';
  260. throw new Zend_Service_LiveDocx_Exception(
  261. 'Cannot log out of LiveDocx service', 0, $e
  262. );
  263. }
  264. }
  265. return $this->_loggedIn;
  266. }
  267. /**
  268. * Return true, if session is currently logged into the backend server
  269. *
  270. * @return boolean
  271. * @since LiveDocx 1.2
  272. */
  273. public function isLoggedIn()
  274. {
  275. return $this->_loggedIn;
  276. }
  277. /**
  278. * Set username
  279. *
  280. * @param string $username
  281. * @return Zend_Service_LiveDocx
  282. * @since LiveDocx 1.0
  283. */
  284. public function setUsername($username)
  285. {
  286. $this->_credentials['username'] = $username;
  287. return $this;
  288. }
  289. /**
  290. * Set password
  291. *
  292. * @param string $password
  293. * @return Zend_Service_LiveDocx
  294. * @since LiveDocx 1.0
  295. */
  296. public function setPassword($password)
  297. {
  298. $this->_credentials['password'] = $password;
  299. return $this;
  300. }
  301. /**
  302. * Set WSDL of LiveDocx web service
  303. *
  304. * @param string $wsdl
  305. * @return Zend_Service_LiveDocx
  306. * @since LiveDocx 1.0
  307. */
  308. public function setWsdl($wsdl)
  309. {
  310. $this->_wsdl = $wsdl;
  311. return $this;
  312. }
  313. /**
  314. * Return current username
  315. *
  316. * @return string|null
  317. * @since LiveDocx 1.0
  318. */
  319. public function getUsername()
  320. {
  321. if (isset($this->_credentials['username'])) {
  322. return $this->_credentials['username'];
  323. }
  324. return null;
  325. }
  326. /**
  327. * Return current password
  328. *
  329. * @return string|null
  330. * @since LiveDocx 1.0
  331. */
  332. public function getPassword()
  333. {
  334. if (isset($this->_credentials['password'])) {
  335. return $this->_credentials['password'];
  336. }
  337. return null;
  338. }
  339. /**
  340. * Return WSDL of LiveDocx web service
  341. *
  342. * @return string
  343. * @since LiveDocx 1.0
  344. */
  345. public function getWsdl()
  346. {
  347. return $this->_wsdl;
  348. }
  349. /**
  350. * Return the document format (extension) of a filename
  351. *
  352. * @param string $filename
  353. * @return string
  354. * @since LiveDocx 1.0
  355. */
  356. public function getFormat($filename)
  357. {
  358. return strtolower(substr(strrchr($filename, '.'), 1));
  359. }
  360. /**
  361. * Return the current API version
  362. *
  363. * @return string
  364. * @since LiveDocx 1.0
  365. */
  366. public function getVersion()
  367. {
  368. return self::VERSION;
  369. }
  370. /**
  371. * Compare the current API version with another version
  372. *
  373. * @param string $version (STRING NOT FLOAT)
  374. * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version)
  375. * @since LiveDocx 1.0
  376. */
  377. public function compareVersion($version)
  378. {
  379. return version_compare($version, $this->getVersion());
  380. }
  381. }