LiveDocx.php 10.0 KB

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