|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00031 class Zend_Service_LiveDocx 00032 { 00037 const VERSION = '1.2'; 00038 00044 protected $_soapClient; 00045 00051 protected $_wsdl; 00052 00058 protected $_credentials; 00059 00065 protected $_loggedIn; 00066 00132 public function __construct($options = null) 00133 { 00134 $this->_credentials = array(); 00135 $this->_loggedIn = false; 00136 00137 if ($options instanceof Zend_Config) { 00138 $options = $options->toArray(); 00139 } 00140 00141 if (is_array($options)) { 00142 $this->setOptions($options); 00143 } 00144 } 00145 00154 public function setOptions(array $options) 00155 { 00156 foreach ($options as $key => $value) { 00157 $method = 'set' . $key; 00158 if (method_exists($this, $method)) { 00159 $this->$method($value); 00160 } 00161 } 00162 00163 return $this; 00164 } 00165 00172 public function __destruct() 00173 { 00174 return $this->logOut(); 00175 } 00176 00185 protected function _initSoapClient($endpoint) 00186 { 00187 try { 00188 require_once 'Zend/Soap/Client.php'; 00189 $this->_soapClient = new Zend_Soap_Client(); 00190 $this->_soapClient->setWsdl($endpoint); 00191 } catch (Zend_Soap_Client_Exception $e) { 00192 require_once 'Zend/Service/LiveDocx/Exception.php'; 00193 throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e); 00194 } 00195 } 00196 00203 public function getSoapClient() 00204 { 00205 return $this->_soapClient; 00206 } 00207 00215 public function setSoapClient(Zend_Soap_Client $soapClient) 00216 { 00217 $this->_soapClient = $soapClient; 00218 return $this; 00219 } 00220 00231 public function logIn() 00232 { 00233 if (!$this->isLoggedIn()) { 00234 if (null === $this->getUsername()) { 00235 require_once 'Zend/Service/LiveDocx/Exception.php'; 00236 throw new Zend_Service_LiveDocx_Exception( 00237 'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation' 00238 ); 00239 } 00240 00241 if (null === $this->getPassword()) { 00242 require_once 'Zend/Service/LiveDocx/Exception.php'; 00243 throw new Zend_Service_LiveDocx_Exception( 00244 'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation' 00245 ); 00246 } 00247 00248 if (null === $this->getSoapClient()) { 00249 $this->_initSoapClient($this->_wsdl); 00250 } 00251 00252 try { 00253 $this->getSoapClient()->LogIn(array( 00254 'username' => $this->getUsername(), 00255 'password' => $this->getPassword(), 00256 )); 00257 $this->_loggedIn = true; 00258 } catch (Exception $e) { 00259 require_once 'Zend/Service/LiveDocx/Exception.php'; 00260 throw new Zend_Service_LiveDocx_Exception( 00261 'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e 00262 ); 00263 } 00264 } 00265 00266 return $this->_loggedIn; 00267 } 00268 00276 public function logOut() 00277 { 00278 if ($this->isLoggedIn()) { 00279 try { 00280 $this->getSoapClient()->LogOut(); 00281 $this->_loggedIn = false; 00282 } catch (Exception $e) { 00283 require_once 'Zend/Service/LiveDocx/Exception.php'; 00284 throw new Zend_Service_LiveDocx_Exception( 00285 'Cannot log out of LiveDocx service', 0, $e 00286 ); 00287 } 00288 } 00289 00290 return $this->_loggedIn; 00291 } 00292 00299 public function isLoggedIn() 00300 { 00301 return $this->_loggedIn; 00302 } 00303 00310 public function setUsername($username) 00311 { 00312 $this->_credentials['username'] = $username; 00313 return $this; 00314 } 00315 00322 public function setPassword($password) 00323 { 00324 $this->_credentials['password'] = $password; 00325 return $this; 00326 } 00327 00334 public function setWsdl($wsdl) 00335 { 00336 $this->_wsdl = $wsdl; 00337 return $this; 00338 } 00339 00346 public function getUsername() 00347 { 00348 if (isset($this->_credentials['username'])) { 00349 return $this->_credentials['username']; 00350 } 00351 00352 return null; 00353 } 00354 00361 public function getPassword() 00362 { 00363 if (isset($this->_credentials['password'])) { 00364 return $this->_credentials['password']; 00365 } 00366 00367 return null; 00368 } 00369 00376 public function getWsdl() 00377 { 00378 return $this->_wsdl; 00379 } 00380 00388 public function getFormat($filename) 00389 { 00390 return strtolower(substr(strrchr($filename, '.'), 1)); 00391 } 00392 00399 public function getVersion() 00400 { 00401 return self::VERSION; 00402 } 00403 00411 public function compareVersion($version) 00412 { 00413 return version_compare($version, $this->getVersion()); 00414 } 00415 }