|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00027 require_once 'Zend/Gdata.php'; 00028 00032 require_once 'Zend/Gdata/Gapps/UserFeed.php'; 00033 00037 require_once 'Zend/Gdata/Gapps/NicknameFeed.php'; 00038 00042 require_once 'Zend/Gdata/Gapps/EmailListFeed.php'; 00043 00047 require_once 'Zend/Gdata/Gapps/EmailListRecipientFeed.php'; 00048 00049 00067 class Zend_Gdata_Gapps extends Zend_Gdata 00068 { 00069 00070 const APPS_BASE_FEED_URI = 'https://apps-apis.google.com/a/feeds'; 00071 const AUTH_SERVICE_NAME = 'apps'; 00072 00076 const APPS_USER_PATH = '/user/2.0'; 00077 00081 const APPS_NICKNAME_PATH = '/nickname/2.0'; 00082 00086 const APPS_EMAIL_LIST_PATH = '/emailList/2.0'; 00087 00091 const APPS_EMAIL_LIST_RECIPIENT_POSTFIX = '/recipient'; 00092 00098 protected $_domain = null; 00099 00105 public static $namespaces = array( 00106 array('apps', 'http://schemas.google.com/apps/2006', 1, 0) 00107 ); 00108 00118 public function __construct($client = null, $domain = null, $applicationId = 'MyCompany-MyApp-1.0') 00119 { 00120 $this->registerPackage('Zend_Gdata_Gapps'); 00121 $this->registerPackage('Zend_Gdata_Gapps_Extension'); 00122 parent::__construct($client, $applicationId); 00123 $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME); 00124 $this->_domain = $domain; 00125 } 00126 00136 public static function throwServiceExceptionIfDetected($e) { 00137 // Check to make sure that there actually response! 00138 // This can happen if the connection dies before the request 00139 // completes. (See ZF-5949) 00140 $response = $e->getResponse(); 00141 if (!$response) { 00142 require_once('Zend/Gdata/App/IOException.php'); 00143 throw new Zend_Gdata_App_IOException('No HTTP response received (possible connection failure)'); 00144 } 00145 00146 try { 00147 // Check to see if there is an AppsForYourDomainErrors 00148 // datastructure in the response. If so, convert it to 00149 // an exception and throw it. 00150 require_once 'Zend/Gdata/Gapps/ServiceException.php'; 00151 $error = new Zend_Gdata_Gapps_ServiceException(); 00152 $error->importFromString($response->getBody()); 00153 throw $error; 00154 } catch (Zend_Gdata_App_Exception $e2) { 00155 // Unable to convert the response to a ServiceException, 00156 // most likely because the server didn't return an 00157 // AppsForYourDomainErrors document. Throw the original 00158 // exception. 00159 throw $e; 00160 } 00161 } 00162 00178 public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed') 00179 { 00180 try { 00181 return parent::import($uri, $client, $className); 00182 } catch (Zend_Gdata_App_HttpException $e) { 00183 self::throwServiceExceptionIfDetected($e); 00184 } 00185 } 00186 00199 public function get($uri, $extraHeaders = array()) 00200 { 00201 try { 00202 return parent::get($uri, $extraHeaders); 00203 } catch (Zend_Gdata_App_HttpException $e) { 00204 self::throwServiceExceptionIfDetected($e); 00205 } 00206 } 00207 00223 public function post($data, $uri = null, $remainingRedirects = null, 00224 $contentType = null, $extraHeaders = null) 00225 { 00226 try { 00227 return parent::post($data, $uri, $remainingRedirects, $contentType, $extraHeaders); 00228 } catch (Zend_Gdata_App_HttpException $e) { 00229 self::throwServiceExceptionIfDetected($e); 00230 } 00231 } 00232 00248 public function put($data, $uri = null, $remainingRedirects = null, 00249 $contentType = null, $extraHeaders = null) 00250 { 00251 try { 00252 return parent::put($data, $uri, $remainingRedirects, $contentType, $extraHeaders); 00253 } catch (Zend_Gdata_App_HttpException $e) { 00254 self::throwServiceExceptionIfDetected($e); 00255 } 00256 } 00257 00270 public function delete($data, $remainingRedirects = null) 00271 { 00272 try { 00273 return parent::delete($data, $remainingRedirects); 00274 } catch (Zend_Gdata_App_HttpException $e) { 00275 self::throwServiceExceptionIfDetected($e); 00276 } 00277 } 00278 00290 public function setDomain($value) 00291 { 00292 $this->_domain = $value; 00293 } 00294 00303 public function getDomain() 00304 { 00305 return $this->_domain; 00306 } 00307 00317 public function getBaseUrl($domain = null) 00318 { 00319 if ($domain !== null) { 00320 return self::APPS_BASE_FEED_URI . '/' . $domain; 00321 } else if ($this->_domain !== null) { 00322 return self::APPS_BASE_FEED_URI . '/' . $this->_domain; 00323 } else { 00324 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00325 throw new Zend_Gdata_App_InvalidArgumentException( 00326 'Domain must be specified.'); 00327 } 00328 } 00329 00340 public function getUserFeed($location = null) 00341 { 00342 if ($location === null) { 00343 $uri = $this->getBaseUrl() . self::APPS_USER_PATH; 00344 } else if ($location instanceof Zend_Gdata_Query) { 00345 $uri = $location->getQueryUrl(); 00346 } else { 00347 $uri = $location; 00348 } 00349 return parent::getFeed($uri, 'Zend_Gdata_Gapps_UserFeed'); 00350 } 00351 00362 public function getNicknameFeed($location = null) 00363 { 00364 if ($location === null) { 00365 $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH; 00366 } else if ($location instanceof Zend_Gdata_Query) { 00367 $uri = $location->getQueryUrl(); 00368 } else { 00369 $uri = $location; 00370 } 00371 return parent::getFeed($uri, 'Zend_Gdata_Gapps_NicknameFeed'); 00372 } 00373 00385 public function getEmailListFeed($location = null) 00386 { 00387 if ($location === null) { 00388 $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH; 00389 } else if ($location instanceof Zend_Gdata_Query) { 00390 $uri = $location->getQueryUrl(); 00391 } else { 00392 $uri = $location; 00393 } 00394 return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListFeed'); 00395 } 00396 00407 public function getEmailListRecipientFeed($location) 00408 { 00409 if ($location === null) { 00410 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00411 throw new Zend_Gdata_App_InvalidArgumentException( 00412 'Location must not be null'); 00413 } else if ($location instanceof Zend_Gdata_Query) { 00414 $uri = $location->getQueryUrl(); 00415 } else { 00416 $uri = $location; 00417 } 00418 return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListRecipientFeed'); 00419 } 00420 00430 public function getUserEntry($location) 00431 { 00432 if ($location === null) { 00433 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00434 throw new Zend_Gdata_App_InvalidArgumentException( 00435 'Location must not be null'); 00436 } else if ($location instanceof Zend_Gdata_Query) { 00437 $uri = $location->getQueryUrl(); 00438 } else { 00439 $uri = $location; 00440 } 00441 return parent::getEntry($uri, 'Zend_Gdata_Gapps_UserEntry'); 00442 } 00443 00453 public function getNicknameEntry($location) 00454 { 00455 if ($location === null) { 00456 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00457 throw new Zend_Gdata_App_InvalidArgumentException( 00458 'Location must not be null'); 00459 } else if ($location instanceof Zend_Gdata_Query) { 00460 $uri = $location->getQueryUrl(); 00461 } else { 00462 $uri = $location; 00463 } 00464 return parent::getEntry($uri, 'Zend_Gdata_Gapps_NicknameEntry'); 00465 } 00466 00476 public function getEmailListEntry($location) 00477 { 00478 if ($location === null) { 00479 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00480 throw new Zend_Gdata_App_InvalidArgumentException( 00481 'Location must not be null'); 00482 } else if ($location instanceof Zend_Gdata_Query) { 00483 $uri = $location->getQueryUrl(); 00484 } else { 00485 $uri = $location; 00486 } 00487 return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListEntry'); 00488 } 00489 00499 public function getEmailListRecipientEntry($location) 00500 { 00501 if ($location === null) { 00502 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00503 throw new Zend_Gdata_App_InvalidArgumentException( 00504 'Location must not be null'); 00505 } else if ($location instanceof Zend_Gdata_Query) { 00506 $uri = $location->getQueryUrl(); 00507 } else { 00508 $uri = $location; 00509 } 00510 return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry'); 00511 } 00512 00526 public function insertUser($user, $uri = null) 00527 { 00528 if ($uri === null) { 00529 $uri = $this->getBaseUrl() . self::APPS_USER_PATH; 00530 } 00531 $newEntry = $this->insertEntry($user, $uri, 'Zend_Gdata_Gapps_UserEntry'); 00532 return $newEntry; 00533 } 00534 00549 public function insertNickname($nickname, $uri = null) 00550 { 00551 if ($uri === null) { 00552 $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH; 00553 } 00554 $newEntry = $this->insertEntry($nickname, $uri, 'Zend_Gdata_Gapps_NicknameEntry'); 00555 return $newEntry; 00556 } 00557 00572 public function insertEmailList($emailList, $uri = null) 00573 { 00574 if ($uri === null) { 00575 $uri = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH; 00576 } 00577 $newEntry = $this->insertEntry($emailList, $uri, 'Zend_Gdata_Gapps_EmailListEntry'); 00578 return $newEntry; 00579 } 00580 00595 public function insertEmailListRecipient($recipient, $uri = null) 00596 { 00597 if ($uri === null) { 00598 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00599 throw new Zend_Gdata_App_InvalidArgumentException( 00600 'URI must not be null'); 00601 } elseif ($uri instanceof Zend_Gdata_Gapps_EmailListEntry) { 00602 $uri = $uri->getLink('edit')->href; 00603 } 00604 $newEntry = $this->insertEntry($recipient, $uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry'); 00605 return $newEntry; 00606 } 00607 00622 public function __call($method, $args) { 00623 if (preg_match('/^new(\w+Query)/', $method, $matches)) { 00624 $class = $matches[1]; 00625 $foundClassName = null; 00626 foreach ($this->_registeredPackages as $name) { 00627 try { 00628 // Autoloading disabled on next line for compatibility 00629 // with magic factories. See ZF-6660. 00630 if (!class_exists($name . '_' . $class, false)) { 00631 require_once 'Zend/Loader.php'; 00632 @Zend_Loader::loadClass($name . '_' . $class); 00633 } 00634 $foundClassName = $name . '_' . $class; 00635 break; 00636 } catch (Zend_Exception $e) { 00637 // package wasn't here- continue searching 00638 } 00639 } 00640 if ($foundClassName != null) { 00641 $reflectionObj = new ReflectionClass($foundClassName); 00642 // Prepend the domain to the query 00643 $args = array_merge(array($this->getDomain()), $args); 00644 return $reflectionObj->newInstanceArgs($args); 00645 } else { 00646 require_once 'Zend/Gdata/App/Exception.php'; 00647 throw new Zend_Gdata_App_Exception( 00648 "Unable to find '${class}' in registered packages"); 00649 } 00650 } else { 00651 return parent::__call($method, $args); 00652 } 00653 00654 } 00655 00656 // Convenience methods 00657 // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e 00658 00675 public function createUser ($username, $givenName, $familyName, $password, 00676 $passwordHashFunction = null, $quotaLimitInMB = null) { 00677 $user = $this->newUserEntry(); 00678 $user->login = $this->newLogin(); 00679 $user->login->username = $username; 00680 $user->login->password = $password; 00681 $user->login->hashFunctionName = $passwordHashFunction; 00682 $user->name = $this->newName(); 00683 $user->name->givenName = $givenName; 00684 $user->name->familyName = $familyName; 00685 if ($quotaLimitInMB !== null) { 00686 $user->quota = $this->newQuota(); 00687 $user->quota->limit = $quotaLimitInMB; 00688 } 00689 return $this->insertUser($user); 00690 } 00691 00701 public function retrieveUser ($username) { 00702 $query = $this->newUserQuery($username); 00703 try { 00704 $user = $this->getUserEntry($query); 00705 } catch (Zend_Gdata_Gapps_ServiceException $e) { 00706 // Set the user to null if not found 00707 if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) { 00708 $user = null; 00709 } else { 00710 throw $e; 00711 } 00712 } 00713 return $user; 00714 } 00715 00729 public function retrievePageOfUsers ($startUsername = null) { 00730 $query = $this->newUserQuery(); 00731 $query->setStartUsername($startUsername); 00732 return $this->getUserFeed($query); 00733 } 00734 00747 public function retrieveAllUsers () { 00748 return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers()); 00749 } 00750 00769 public function updateUser($username, $userEntry) { 00770 return $this->updateEntry($userEntry, $this->getBaseUrl() . 00771 self::APPS_USER_PATH . '/' . $username); 00772 } 00773 00785 public function suspendUser($username) { 00786 $user = $this->retrieveUser($username); 00787 $user->login->suspended = true; 00788 return $user->save(); 00789 } 00790 00802 public function restoreUser($username) { 00803 $user = $this->retrieveUser($username); 00804 $user->login->suspended = false; 00805 return $user->save(); 00806 } 00807 00817 public function deleteUser($username) { 00818 $this->delete($this->getBaseUrl() . self::APPS_USER_PATH . '/' . 00819 $username); 00820 } 00821 00834 public function createNickname($username, $nickname) { 00835 $entry = $this->newNicknameEntry(); 00836 $nickname = $this->newNickname($nickname); 00837 $login = $this->newLogin($username); 00838 $entry->nickname = $nickname; 00839 $entry->login = $login; 00840 return $this->insertNickname($entry); 00841 } 00842 00852 public function retrieveNickname($nickname) { 00853 $query = $this->newNicknameQuery(); 00854 $query->setNickname($nickname); 00855 try { 00856 $nickname = $this->getNicknameEntry($query); 00857 } catch (Zend_Gdata_Gapps_ServiceException $e) { 00858 // Set the nickname to null if not found 00859 if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) { 00860 $nickname = null; 00861 } else { 00862 throw $e; 00863 } 00864 } 00865 return $nickname; 00866 } 00867 00879 public function retrieveNicknames($username) { 00880 $query = $this->newNicknameQuery(); 00881 $query->setUsername($username); 00882 $nicknameFeed = $this->retrieveAllEntriesForFeed( 00883 $this->getNicknameFeed($query)); 00884 return $nicknameFeed; 00885 } 00886 00900 public function retrievePageOfNicknames ($startNickname = null) { 00901 $query = $this->newNicknameQuery(); 00902 $query->setStartNickname($startNickname); 00903 return $this->getNicknameFeed($query); 00904 } 00905 00918 public function retrieveAllNicknames () { 00919 return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames()); 00920 } 00921 00930 public function deleteNickname($nickname) { 00931 $this->delete($this->getBaseUrl() . self::APPS_NICKNAME_PATH . '/' . $nickname); 00932 } 00933 00944 public function createEmailList($emailList) { 00945 $entry = $this->newEmailListEntry(); 00946 $list = $this->newEmailList(); 00947 $list->name = $emailList; 00948 $entry->emailList = $list; 00949 return $this->insertEmailList($entry); 00950 } 00951 00963 public function retrieveEmailLists($recipient) { 00964 $query = $this->newEmailListQuery(); 00965 $query->recipient = $recipient; 00966 return $this->getEmailListFeed($query); 00967 } 00968 00982 public function retrievePageOfEmailLists ($startNickname = null) { 00983 $query = $this->newEmailListQuery(); 00984 $query->setStartEmailListName($startNickname); 00985 return $this->getEmailListFeed($query); 00986 } 00987 01000 public function retrieveAllEmailLists() { 01001 return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists()); 01002 } 01003 01012 public function deleteEmailList($emailList) { 01013 $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' 01014 . $emailList); 01015 } 01016 01030 public function addRecipientToEmailList($recipientAddress, $emailList) { 01031 $entry = $this->newEmailListRecipientEntry(); 01032 $who = $this->newWho(); 01033 $who->email = $recipientAddress; 01034 $entry->who = $who; 01035 $address = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' . 01036 $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/'; 01037 return $this->insertEmailListRecipient($entry, $address); 01038 } 01039 01055 public function retrievePageOfRecipients ($emailList, 01056 $startRecipient = null) { 01057 $query = $this->newEmailListRecipientQuery(); 01058 $query->setEmailListName($emailList); 01059 $query->setStartRecipient($startRecipient); 01060 return $this->getEmailListRecipientFeed($query); 01061 } 01062 01076 public function retrieveAllRecipients($emailList) { 01077 return $this->retrieveAllEntriesForFeed( 01078 $this->retrievePageOfRecipients($emailList)); 01079 } 01080 01091 public function removeRecipientFromEmailList($recipientAddress, $emailList) { 01092 $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' 01093 . $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/' 01094 . $recipientAddress); 01095 } 01096 01097 }