Gapps.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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_Gdata
  17. * @subpackage Gapps
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Gdata
  23. */
  24. require_once 'Zend/Gdata.php';
  25. /**
  26. * @see Zend_Gdata_Gapps_UserFeed
  27. */
  28. require_once 'Zend/Gdata/Gapps/UserFeed.php';
  29. /**
  30. * @see Zend_Gdata_Gapps_NicknameFeed
  31. */
  32. require_once 'Zend/Gdata/Gapps/NicknameFeed.php';
  33. /**
  34. * @see Zend_Gdata_Gapps_EmailListFeed
  35. */
  36. require_once 'Zend/Gdata/Gapps/EmailListFeed.php';
  37. /**
  38. * @see Zend_Gdata_Gapps_EmailListRecipientFeed
  39. */
  40. require_once 'Zend/Gdata/Gapps/EmailListRecipientFeed.php';
  41. /**
  42. * Service class for interacting with the Google Apps Provisioning API.
  43. *
  44. * Like other service classes in this module, this class provides access via
  45. * an HTTP client to Google servers for working with entries and feeds.
  46. *
  47. * Because of the nature of this API, all access must occur over an
  48. * authenticated connection.
  49. *
  50. * @link http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html
  51. *
  52. * @category Zend
  53. * @package Zend_Gdata
  54. * @subpackage Gapps
  55. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  56. * @license http://framework.zend.com/license/new-bsd New BSD License
  57. */
  58. class Zend_Gdata_Gapps extends Zend_Gdata
  59. {
  60. const APPS_BASE_FEED_URI = 'https://apps-apis.google.com/a/feeds';
  61. const AUTH_SERVICE_NAME = 'apps';
  62. /**
  63. * Path to user feeds on the Google Apps server.
  64. */
  65. const APPS_USER_PATH = '/user/2.0';
  66. /**
  67. * Path to nickname feeds on the Google Apps server.
  68. */
  69. const APPS_NICKNAME_PATH = '/nickname/2.0';
  70. /**
  71. * Path to email list feeds on the Google Apps server.
  72. */
  73. const APPS_EMAIL_LIST_PATH = '/emailList/2.0';
  74. /**
  75. * Path to email list recipient feeds on the Google Apps server.
  76. */
  77. const APPS_EMAIL_LIST_RECIPIENT_POSTFIX = '/recipient';
  78. /**
  79. * The domain which is being administered via the Provisioning API.
  80. *
  81. * @var string
  82. */
  83. protected $_domain = null;
  84. /**
  85. * Namespaces used for Zend_Gdata_Gapps
  86. *
  87. * @var array
  88. */
  89. public static $namespaces = array(
  90. array('apps', 'http://schemas.google.com/apps/2006', 1, 0)
  91. );
  92. /**
  93. * Create Gdata_Gapps object
  94. *
  95. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  96. * when communicating with the Google Apps servers.
  97. * @param string $domain (optional) The Google Apps domain which is to be
  98. * accessed.
  99. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  100. */
  101. public function __construct($client = null, $domain = null, $applicationId = 'MyCompany-MyApp-1.0')
  102. {
  103. $this->registerPackage('Zend_Gdata_Gapps');
  104. $this->registerPackage('Zend_Gdata_Gapps_Extension');
  105. parent::__construct($client, $applicationId);
  106. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  107. $this->_domain = $domain;
  108. }
  109. /**
  110. * Convert an exception to an ServiceException if an AppsForYourDomain
  111. * XML document is contained within the original exception's HTTP
  112. * response. If conversion fails, throw the original error.
  113. *
  114. * @param Zend_Gdata_Exception $e The exception to convert.
  115. * @throws Zend_Gdata_Gapps_ServiceException
  116. * @throws mixed
  117. */
  118. public static function throwServiceExceptionIfDetected($e) {
  119. // Check to make sure that there actually response!
  120. // This can happen if the connection dies before the request
  121. // completes. (See ZF-5949)
  122. $response = $e->getResponse();
  123. if (!$response) {
  124. require_once('Zend/Gdata/App/IOException.php');
  125. throw new Zend_Gdata_App_IOException('No HTTP response received (possible connection failure)');
  126. }
  127. try {
  128. // Check to see if there is an AppsForYourDomainErrors
  129. // datastructure in the response. If so, convert it to
  130. // an exception and throw it.
  131. require_once 'Zend/Gdata/Gapps/ServiceException.php';
  132. $error = new Zend_Gdata_Gapps_ServiceException();
  133. $error->importFromString($response->getBody());
  134. throw $error;
  135. } catch (Zend_Gdata_App_Exception $e2) {
  136. // Unable to convert the response to a ServiceException,
  137. // most likely because the server didn't return an
  138. // AppsForYourDomainErrors document. Throw the original
  139. // exception.
  140. throw $e;
  141. }
  142. }
  143. /**
  144. * Imports a feed located at $uri.
  145. * This method overrides the default behavior of Zend_Gdata_App,
  146. * providing support for Zend_Gdata_Gapps_ServiceException.
  147. *
  148. * @param string $uri
  149. * @param Zend_Http_Client $client (optional) The client used for
  150. * communication
  151. * @param string $className (optional) The class which is used as the
  152. * return type
  153. * @throws Zend_Gdata_App_Exception
  154. * @throws Zend_Gdata_App_HttpException
  155. * @throws Zend_Gdata_Gapps_ServiceException
  156. * @return Zend_Gdata_App_Feed
  157. */
  158. public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed')
  159. {
  160. try {
  161. return parent::import($uri, $client, $className);
  162. } catch (Zend_Gdata_App_HttpException $e) {
  163. self::throwServiceExceptionIfDetected($e);
  164. }
  165. }
  166. /**
  167. * GET a URI using client object.
  168. * This method overrides the default behavior of Zend_Gdata_App,
  169. * providing support for Zend_Gdata_Gapps_ServiceException.
  170. *
  171. * @param string $uri GET URI
  172. * @param array $extraHeaders Extra headers to add to the request, as an
  173. * array of string-based key/value pairs.
  174. * @throws Zend_Gdata_App_HttpException
  175. * @throws Zend_Gdata_Gapps_ServiceException
  176. * @return Zend_Http_Response
  177. */
  178. public function get($uri, $extraHeaders = array())
  179. {
  180. try {
  181. return parent::get($uri, $extraHeaders);
  182. } catch (Zend_Gdata_App_HttpException $e) {
  183. self::throwServiceExceptionIfDetected($e);
  184. }
  185. }
  186. /**
  187. * POST data with client object.
  188. * This method overrides the default behavior of Zend_Gdata_App,
  189. * providing support for Zend_Gdata_Gapps_ServiceException.
  190. *
  191. * @param mixed $data The Zend_Gdata_App_Entry or XML to post
  192. * @param string $uri (optional) POST URI
  193. * @param integer $remainingRedirects (optional)
  194. * @param string $contentType Content-type of the data
  195. * @param array $extraHaders Extra headers to add tot he request
  196. * @return Zend_Http_Response
  197. * @throws Zend_Gdata_App_HttpException
  198. * @throws Zend_Gdata_App_InvalidArgumentException
  199. * @throws Zend_Gdata_Gapps_ServiceException
  200. */
  201. public function post($data, $uri = null, $remainingRedirects = null,
  202. $contentType = null, $extraHeaders = null)
  203. {
  204. try {
  205. return parent::post($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
  206. } catch (Zend_Gdata_App_HttpException $e) {
  207. self::throwServiceExceptionIfDetected($e);
  208. }
  209. }
  210. /**
  211. * PUT data with client object
  212. * This method overrides the default behavior of Zend_Gdata_App,
  213. * providing support for Zend_Gdata_Gapps_ServiceException.
  214. *
  215. * @param mixed $data The Zend_Gdata_App_Entry or XML to post
  216. * @param string $uri (optional) PUT URI
  217. * @param integer $remainingRedirects (optional)
  218. * @param string $contentType Content-type of the data
  219. * @param array $extraHaders Extra headers to add tot he request
  220. * @return Zend_Http_Response
  221. * @throws Zend_Gdata_App_HttpException
  222. * @throws Zend_Gdata_App_InvalidArgumentException
  223. * @throws Zend_Gdata_Gapps_ServiceException
  224. */
  225. public function put($data, $uri = null, $remainingRedirects = null,
  226. $contentType = null, $extraHeaders = null)
  227. {
  228. try {
  229. return parent::put($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
  230. } catch (Zend_Gdata_App_HttpException $e) {
  231. self::throwServiceExceptionIfDetected($e);
  232. }
  233. }
  234. /**
  235. * DELETE entry with client object
  236. * This method overrides the default behavior of Zend_Gdata_App,
  237. * providing support for Zend_Gdata_Gapps_ServiceException.
  238. *
  239. * @param mixed $data The Zend_Gdata_App_Entry or URL to delete
  240. * @param integer $remainingRedirects (optional)
  241. * @return void
  242. * @throws Zend_Gdata_App_HttpException
  243. * @throws Zend_Gdata_App_InvalidArgumentException
  244. * @throws Zend_Gdata_Gapps_ServiceException
  245. */
  246. public function delete($data, $remainingRedirects = null)
  247. {
  248. try {
  249. return parent::delete($data, $remainingRedirects);
  250. } catch (Zend_Gdata_App_HttpException $e) {
  251. self::throwServiceExceptionIfDetected($e);
  252. }
  253. }
  254. /**
  255. * Set domain for this service instance. This should be a fully qualified
  256. * domain, such as 'foo.example.com'.
  257. *
  258. * This value is used when calculating URLs for retrieving and posting
  259. * entries. If no value is specified, a URL will have to be manually
  260. * constructed prior to using any methods which interact with the Google
  261. * Apps provisioning service.
  262. *
  263. * @param string $value The domain to be used for this session.
  264. */
  265. public function setDomain($value)
  266. {
  267. $this->_domain = $value;
  268. }
  269. /**
  270. * Get domain for this service instance. This should be a fully qualified
  271. * domain, such as 'foo.example.com'. If no domain is set, null will be
  272. * returned.
  273. *
  274. * @return string The domain to be used for this session, or null if not
  275. * set.
  276. */
  277. public function getDomain()
  278. {
  279. return $this->_domain;
  280. }
  281. /**
  282. * Returns the base URL used to access the Google Apps service, based
  283. * on the current domain. The current domain can be temporarily
  284. * overridden by providing a fully qualified domain as $domain.
  285. *
  286. * @param string $domain (optional) A fully-qualified domain to use
  287. * instead of the default domain for this service instance.
  288. * @throws Zend_Gdata_App_InvalidArgumentException
  289. */
  290. public function getBaseUrl($domain = null)
  291. {
  292. if ($domain !== null) {
  293. return self::APPS_BASE_FEED_URI . '/' . $domain;
  294. } else if ($this->_domain !== null) {
  295. return self::APPS_BASE_FEED_URI . '/' . $this->_domain;
  296. } else {
  297. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  298. throw new Zend_Gdata_App_InvalidArgumentException(
  299. 'Domain must be specified.');
  300. }
  301. }
  302. /**
  303. * Retrieve a UserFeed containing multiple UserEntry objects.
  304. *
  305. * @param mixed $location (optional) The location for the feed, as a URL
  306. * or Query.
  307. * @return Zend_Gdata_Gapps_UserFeed
  308. * @throws Zend_Gdata_App_Exception
  309. * @throws Zend_Gdata_App_HttpException
  310. * @throws Zend_Gdata_Gapps_ServiceException
  311. */
  312. public function getUserFeed($location = null)
  313. {
  314. if ($location === null) {
  315. $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
  316. } else if ($location instanceof Zend_Gdata_Query) {
  317. $uri = $location->getQueryUrl();
  318. } else {
  319. $uri = $location;
  320. }
  321. return parent::getFeed($uri, 'Zend_Gdata_Gapps_UserFeed');
  322. }
  323. /**
  324. * Retreive NicknameFeed object containing multiple NicknameEntry objects.
  325. *
  326. * @param mixed $location (optional) The location for the feed, as a URL
  327. * or Query.
  328. * @return Zend_Gdata_Gapps_NicknameFeed
  329. * @throws Zend_Gdata_App_Exception
  330. * @throws Zend_Gdata_App_HttpException
  331. * @throws Zend_Gdata_Gapps_ServiceException
  332. */
  333. public function getNicknameFeed($location = null)
  334. {
  335. if ($location === null) {
  336. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  337. } else if ($location instanceof Zend_Gdata_Query) {
  338. $uri = $location->getQueryUrl();
  339. } else {
  340. $uri = $location;
  341. }
  342. return parent::getFeed($uri, 'Zend_Gdata_Gapps_NicknameFeed');
  343. }
  344. /**
  345. * Retreive EmailListFeed object containing multiple EmailListEntry
  346. * objects.
  347. *
  348. * @param mixed $location (optional) The location for the feed, as a URL
  349. * or Query.
  350. * @return Zend_Gdata_Gapps_EmailListFeed
  351. * @throws Zend_Gdata_App_Exception
  352. * @throws Zend_Gdata_App_HttpException
  353. * @throws Zend_Gdata_Gapps_ServiceException
  354. */
  355. public function getEmailListFeed($location = null)
  356. {
  357. if ($location === null) {
  358. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  359. } else if ($location instanceof Zend_Gdata_Query) {
  360. $uri = $location->getQueryUrl();
  361. } else {
  362. $uri = $location;
  363. }
  364. return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListFeed');
  365. }
  366. /**
  367. * Retreive EmailListRecipientFeed object containing multiple
  368. * EmailListRecipientEntry objects.
  369. *
  370. * @param mixed $location The location for the feed, as a URL or Query.
  371. * @return Zend_Gdata_Gapps_EmailListRecipientFeed
  372. * @throws Zend_Gdata_App_Exception
  373. * @throws Zend_Gdata_App_HttpException
  374. * @throws Zend_Gdata_Gapps_ServiceException
  375. */
  376. public function getEmailListRecipientFeed($location)
  377. {
  378. if ($location === null) {
  379. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  380. throw new Zend_Gdata_App_InvalidArgumentException(
  381. 'Location must not be null');
  382. } else if ($location instanceof Zend_Gdata_Query) {
  383. $uri = $location->getQueryUrl();
  384. } else {
  385. $uri = $location;
  386. }
  387. return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListRecipientFeed');
  388. }
  389. /**
  390. * Retreive a single UserEntry object.
  391. *
  392. * @param mixed $location The location for the feed, as a URL or Query.
  393. * @return Zend_Gdata_Gapps_UserEntry
  394. * @throws Zend_Gdata_App_Exception
  395. * @throws Zend_Gdata_App_HttpException
  396. * @throws Zend_Gdata_Gapps_ServiceException
  397. */
  398. public function getUserEntry($location)
  399. {
  400. if ($location === null) {
  401. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  402. throw new Zend_Gdata_App_InvalidArgumentException(
  403. 'Location must not be null');
  404. } else if ($location instanceof Zend_Gdata_Query) {
  405. $uri = $location->getQueryUrl();
  406. } else {
  407. $uri = $location;
  408. }
  409. return parent::getEntry($uri, 'Zend_Gdata_Gapps_UserEntry');
  410. }
  411. /**
  412. * Retreive a single NicknameEntry object.
  413. *
  414. * @param mixed $location The location for the feed, as a URL or Query.
  415. * @return Zend_Gdata_Gapps_NicknameEntry
  416. * @throws Zend_Gdata_App_Exception
  417. * @throws Zend_Gdata_App_HttpException
  418. * @throws Zend_Gdata_Gapps_ServiceException
  419. */
  420. public function getNicknameEntry($location)
  421. {
  422. if ($location === null) {
  423. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  424. throw new Zend_Gdata_App_InvalidArgumentException(
  425. 'Location must not be null');
  426. } else if ($location instanceof Zend_Gdata_Query) {
  427. $uri = $location->getQueryUrl();
  428. } else {
  429. $uri = $location;
  430. }
  431. return parent::getEntry($uri, 'Zend_Gdata_Gapps_NicknameEntry');
  432. }
  433. /**
  434. * Retreive a single EmailListEntry object.
  435. *
  436. * @param mixed $location The location for the feed, as a URL or Query.
  437. * @return Zend_Gdata_Gapps_EmailListEntry
  438. * @throws Zend_Gdata_App_Exception
  439. * @throws Zend_Gdata_App_HttpException
  440. * @throws Zend_Gdata_Gapps_ServiceException
  441. */
  442. public function getEmailListEntry($location)
  443. {
  444. if ($location === null) {
  445. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  446. throw new Zend_Gdata_App_InvalidArgumentException(
  447. 'Location must not be null');
  448. } else if ($location instanceof Zend_Gdata_Query) {
  449. $uri = $location->getQueryUrl();
  450. } else {
  451. $uri = $location;
  452. }
  453. return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListEntry');
  454. }
  455. /**
  456. * Retreive a single EmailListRecipientEntry object.
  457. *
  458. * @param mixed $location The location for the feed, as a URL or Query.
  459. * @return Zend_Gdata_Gapps_EmailListRecipientEntry
  460. * @throws Zend_Gdata_App_Exception
  461. * @throws Zend_Gdata_App_HttpException
  462. * @throws Zend_Gdata_Gapps_ServiceException
  463. */
  464. public function getEmailListRecipientEntry($location)
  465. {
  466. if ($location === null) {
  467. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  468. throw new Zend_Gdata_App_InvalidArgumentException(
  469. 'Location must not be null');
  470. } else if ($location instanceof Zend_Gdata_Query) {
  471. $uri = $location->getQueryUrl();
  472. } else {
  473. $uri = $location;
  474. }
  475. return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
  476. }
  477. /**
  478. * Create a new user from a UserEntry.
  479. *
  480. * @param Zend_Gdata_Gapps_UserEntry $user The user entry to insert.
  481. * @param string $uri (optional) The URI where the user should be
  482. * uploaded to. If null, the default user creation URI for
  483. * this domain will be used.
  484. * @return Zend_Gdata_Gapps_UserEntry The inserted user entry as
  485. * returned by the server.
  486. * @throws Zend_Gdata_App_Exception
  487. * @throws Zend_Gdata_App_HttpException
  488. * @throws Zend_Gdata_Gapps_ServiceException
  489. */
  490. public function insertUser($user, $uri = null)
  491. {
  492. if ($uri === null) {
  493. $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
  494. }
  495. $newEntry = $this->insertEntry($user, $uri, 'Zend_Gdata_Gapps_UserEntry');
  496. return $newEntry;
  497. }
  498. /**
  499. * Create a new nickname from a NicknameEntry.
  500. *
  501. * @param Zend_Gdata_Gapps_NicknameEntry $nickname The nickname entry to
  502. * insert.
  503. * @param string $uri (optional) The URI where the nickname should be
  504. * uploaded to. If null, the default nickname creation URI for
  505. * this domain will be used.
  506. * @return Zend_Gdata_Gapps_NicknameEntry The inserted nickname entry as
  507. * returned by the server.
  508. * @throws Zend_Gdata_App_Exception
  509. * @throws Zend_Gdata_App_HttpException
  510. * @throws Zend_Gdata_Gapps_ServiceException
  511. */
  512. public function insertNickname($nickname, $uri = null)
  513. {
  514. if ($uri === null) {
  515. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  516. }
  517. $newEntry = $this->insertEntry($nickname, $uri, 'Zend_Gdata_Gapps_NicknameEntry');
  518. return $newEntry;
  519. }
  520. /**
  521. * Create a new email list from an EmailListEntry.
  522. *
  523. * @param Zend_Gdata_Gapps_EmailListEntry $emailList The email list entry
  524. * to insert.
  525. * @param string $uri (optional) The URI where the email list should be
  526. * uploaded to. If null, the default email list creation URI for
  527. * this domain will be used.
  528. * @return Zend_Gdata_Gapps_EmailListEntry The inserted email list entry
  529. * as returned by the server.
  530. * @throws Zend_Gdata_App_Exception
  531. * @throws Zend_Gdata_App_HttpException
  532. * @throws Zend_Gdata_Gapps_ServiceException
  533. */
  534. public function insertEmailList($emailList, $uri = null)
  535. {
  536. if ($uri === null) {
  537. $uri = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH;
  538. }
  539. $newEntry = $this->insertEntry($emailList, $uri, 'Zend_Gdata_Gapps_EmailListEntry');
  540. return $newEntry;
  541. }
  542. /**
  543. * Create a new email list recipient from an EmailListRecipientEntry.
  544. *
  545. * @param Zend_Gdata_Gapps_EmailListRecipientEntry $recipient The recipient
  546. * entry to insert.
  547. * @param string $uri (optional) The URI where the recipient should be
  548. * uploaded to. If null, the default recipient creation URI for
  549. * this domain will be used.
  550. * @return Zend_Gdata_Gapps_EmailListRecipientEntry The inserted
  551. * recipient entry as returned by the server.
  552. * @throws Zend_Gdata_App_Exception
  553. * @throws Zend_Gdata_App_HttpException
  554. * @throws Zend_Gdata_Gapps_ServiceException
  555. */
  556. public function insertEmailListRecipient($recipient, $uri = null)
  557. {
  558. if ($uri === null) {
  559. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  560. throw new Zend_Gdata_App_InvalidArgumentException(
  561. 'URI must not be null');
  562. } elseif ($uri instanceof Zend_Gdata_Gapps_EmailListEntry) {
  563. $uri = $uri->getLink('edit')->href;
  564. }
  565. $newEntry = $this->insertEntry($recipient, $uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
  566. return $newEntry;
  567. }
  568. /**
  569. * Provides a magic factory method to instantiate new objects with
  570. * shorter syntax than would otherwise be required by the Zend Framework
  571. * naming conventions. For more information, see Zend_Gdata_App::__call().
  572. *
  573. * This overrides the default behavior of __call() so that query classes
  574. * do not need to have their domain manually set when created with
  575. * a magic factory method.
  576. *
  577. * @see Zend_Gdata_App::__call()
  578. * @param string $method The method name being called
  579. * @param array $args The arguments passed to the call
  580. * @throws Zend_Gdata_App_Exception
  581. */
  582. public function __call($method, $args) {
  583. if (preg_match('/^new(\w+Query)/', $method, $matches)) {
  584. $class = $matches[1];
  585. $foundClassName = null;
  586. foreach ($this->_registeredPackages as $name) {
  587. try {
  588. require_once 'Zend/Loader.php';
  589. @Zend_Loader::loadClass("${name}_${class}");
  590. $foundClassName = "${name}_${class}";
  591. break;
  592. } catch (Zend_Exception $e) {
  593. // package wasn't here- continue searching
  594. }
  595. }
  596. if ($foundClassName != null) {
  597. $reflectionObj = new ReflectionClass($foundClassName);
  598. // Prepend the domain to the query
  599. $args = array_merge(array($this->getDomain()), $args);
  600. return $reflectionObj->newInstanceArgs($args);
  601. } else {
  602. require_once 'Zend/Gdata/App/Exception.php';
  603. throw new Zend_Gdata_App_Exception(
  604. "Unable to find '${class}' in registered packages");
  605. }
  606. } else {
  607. return parent::__call($method, $args);
  608. }
  609. }
  610. // Convenience methods
  611. // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e
  612. /**
  613. * Create a new user entry and send it to the Google Apps servers.
  614. *
  615. * @param string $username The username for the new user.
  616. * @param string $givenName The given name for the new user.
  617. * @param string $familyName The family name for the new user.
  618. * @param string $password The password for the new user as a plaintext string
  619. * (if $passwordHashFunction is null) or a SHA-1 hashed
  620. * value (if $passwordHashFunction = 'SHA-1').
  621. * @param string $quotaLimitInMB (optional) The quota limit for the new user in MB.
  622. * @return Zend_Gdata_Gapps_UserEntry (optional) The new user entry as returned by
  623. * server.
  624. * @throws Zend_Gdata_App_Exception
  625. * @throws Zend_Gdata_App_HttpException
  626. * @throws Zend_Gdata_Gapps_ServiceException
  627. */
  628. public function createUser ($username, $givenName, $familyName, $password,
  629. $passwordHashFunction = null, $quotaLimitInMB = null) {
  630. $user = $this->newUserEntry();
  631. $user->login = $this->newLogin();
  632. $user->login->username = $username;
  633. $user->login->password = $password;
  634. $user->login->hashFunctionName = $passwordHashFunction;
  635. $user->name = $this->newName();
  636. $user->name->givenName = $givenName;
  637. $user->name->familyName = $familyName;
  638. if ($quotaLimitInMB !== null) {
  639. $user->quota = $this->newQuota();
  640. $user->quota->limit = $quotaLimitInMB;
  641. }
  642. return $this->insertUser($user);
  643. }
  644. /**
  645. * Retrieve a user based on their username.
  646. *
  647. * @param string $username The username to search for.
  648. * @return Zend_Gdata_Gapps_UserEntry The username to search for, or null
  649. * if no match found.
  650. * @throws Zend_Gdata_App_InvalidArgumentException
  651. * @throws Zend_Gdata_App_HttpException
  652. */
  653. public function retrieveUser ($username) {
  654. $query = $this->newUserQuery($username);
  655. try {
  656. $user = $this->getUserEntry($query);
  657. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  658. // Set the user to null if not found
  659. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  660. $user = null;
  661. } else {
  662. throw $e;
  663. }
  664. }
  665. return $user;
  666. }
  667. /**
  668. * Retrieve a page of users in alphabetical order, starting with the
  669. * provided username.
  670. *
  671. * @param string $startUsername (optional) The first username to retrieve.
  672. * If null or not declared, the page will begin with the first
  673. * user in the domain.
  674. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  675. * objects representing all users in the domain.
  676. * @throws Zend_Gdata_App_Exception
  677. * @throws Zend_Gdata_App_HttpException
  678. * @throws Zend_Gdata_Gapps_ServiceException
  679. */
  680. public function retrievePageOfUsers ($startUsername = null) {
  681. $query = $this->newUserQuery();
  682. $query->setStartUsername($startUsername);
  683. return $this->getUserFeed($query);
  684. }
  685. /**
  686. * Retrieve all users in the current domain. Be aware that
  687. * calling this function on a domain with many users will take a
  688. * signifigant amount of time to complete. On larger domains this may
  689. * may cause execution to timeout without proper precautions in place.
  690. *
  691. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  692. * objects representing all users in the domain.
  693. * @throws Zend_Gdata_App_Exception
  694. * @throws Zend_Gdata_App_HttpException
  695. * @throws Zend_Gdata_Gapps_ServiceException
  696. */
  697. public function retrieveAllUsers () {
  698. return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers());
  699. }
  700. /**
  701. * Overwrite a specified username with the provided UserEntry. The
  702. * UserEntry does not need to contain an edit link.
  703. *
  704. * This method is provided for compliance with the Google Apps
  705. * Provisioning API specification. Normally users will instead want to
  706. * call UserEntry::save() instead.
  707. *
  708. * @see Zend_Gdata_App_Entry::save
  709. * @param string $username The username whose data will be overwritten.
  710. * @param Zend_Gdata_Gapps_UserEntry $userEntry The user entry which
  711. * will be overwritten.
  712. * @return Zend_Gdata_Gapps_UserEntry The UserEntry returned by the
  713. * server.
  714. * @throws Zend_Gdata_App_Exception
  715. * @throws Zend_Gdata_App_HttpException
  716. * @throws Zend_Gdata_Gapps_ServiceException
  717. */
  718. public function updateUser($username, $userEntry) {
  719. return $this->updateEntry($userEntry, $this->getBaseUrl() .
  720. self::APPS_USER_PATH . '/' . $username);
  721. }
  722. /**
  723. * Mark a given user as suspended.
  724. *
  725. * @param string $username The username associated with the user who
  726. * should be suspended.
  727. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  728. * user.
  729. * @throws Zend_Gdata_App_Exception
  730. * @throws Zend_Gdata_App_HttpException
  731. * @throws Zend_Gdata_Gapps_ServiceException
  732. */
  733. public function suspendUser($username) {
  734. $user = $this->retrieveUser($username);
  735. $user->login->suspended = true;
  736. return $user->save();
  737. }
  738. /**
  739. * Mark a given user as not suspended.
  740. *
  741. * @param string $username The username associated with the user who
  742. * should be restored.
  743. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  744. * user.
  745. * @throws Zend_Gdata_App_Exception
  746. * @throws Zend_Gdata_App_HttpException
  747. * @throws Zend_Gdata_Gapps_ServiceException
  748. */
  749. public function restoreUser($username) {
  750. $user = $this->retrieveUser($username);
  751. $user->login->suspended = false;
  752. return $user->save();
  753. }
  754. /**
  755. * Delete a user by username.
  756. *
  757. * @param string $username The username associated with the user who
  758. * should be deleted.
  759. * @throws Zend_Gdata_App_Exception
  760. * @throws Zend_Gdata_App_HttpException
  761. * @throws Zend_Gdata_Gapps_ServiceException
  762. */
  763. public function deleteUser($username) {
  764. $this->delete($this->getBaseUrl() . self::APPS_USER_PATH . '/' .
  765. $username);
  766. }
  767. /**
  768. * Create a nickname for a given user.
  769. *
  770. * @param string $username The username to which the new nickname should
  771. * be associated.
  772. * @param string $nickname The new nickname to be created.
  773. * @return Zend_Gdata_Gapps_NicknameEntry The nickname entry which was
  774. * created by the server.
  775. * @throws Zend_Gdata_App_Exception
  776. * @throws Zend_Gdata_App_HttpException
  777. * @throws Zend_Gdata_Gapps_ServiceException
  778. */
  779. public function createNickname($username, $nickname) {
  780. $entry = $this->newNicknameEntry();
  781. $nickname = $this->newNickname($nickname);
  782. $login = $this->newLogin($username);
  783. $entry->nickname = $nickname;
  784. $entry->login = $login;
  785. return $this->insertNickname($entry);
  786. }
  787. /**
  788. * Retrieve the entry for a specified nickname.
  789. *
  790. * @param string $nickname The nickname to be retrieved.
  791. * @return Zend_Gdata_Gapps_NicknameEntry The requested nickname entry.
  792. * @throws Zend_Gdata_App_Exception
  793. * @throws Zend_Gdata_App_HttpException
  794. * @throws Zend_Gdata_Gapps_ServiceException
  795. */
  796. public function retrieveNickname($nickname) {
  797. $query = $this->newNicknameQuery();
  798. $query->setNickname($nickname);
  799. try {
  800. $nickname = $this->getNicknameEntry($query);
  801. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  802. // Set the nickname to null if not found
  803. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  804. $nickname = null;
  805. } else {
  806. throw $e;
  807. }
  808. }
  809. return $nickname;
  810. }
  811. /**
  812. * Retrieve all nicknames associated with a specific username.
  813. *
  814. * @param string $username The username whose nicknames should be
  815. * returned.
  816. * @return Zend_Gdata_Gapps_NicknameFeed A feed containing all nicknames
  817. * for the given user, or null if
  818. * @throws Zend_Gdata_App_Exception
  819. * @throws Zend_Gdata_App_HttpException
  820. * @throws Zend_Gdata_Gapps_ServiceException
  821. */
  822. public function retrieveNicknames($username) {
  823. $query = $this->newNicknameQuery();
  824. $query->setUsername($username);
  825. $nicknameFeed = $this->retrieveAllEntriesForFeed(
  826. $this->getNicknameFeed($query));
  827. return $nicknameFeed;
  828. }
  829. /**
  830. * Retrieve a page of nicknames in alphabetical order, starting with the
  831. * provided nickname.
  832. *
  833. * @param string $startNickname (optional) The first nickname to
  834. * retrieve. If null or not declared, the page will begin with
  835. * the first nickname in the domain.
  836. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  837. * objects representing all nicknames in the domain.
  838. * @throws Zend_Gdata_App_Exception
  839. * @throws Zend_Gdata_App_HttpException
  840. * @throws Zend_Gdata_Gapps_ServiceException
  841. */
  842. public function retrievePageOfNicknames ($startNickname = null) {
  843. $query = $this->newNicknameQuery();
  844. $query->setStartNickname($startNickname);
  845. return $this->getNicknameFeed($query);
  846. }
  847. /**
  848. * Retrieve all nicknames in the current domain. Be aware that
  849. * calling this function on a domain with many nicknames will take a
  850. * signifigant amount of time to complete. On larger domains this may
  851. * may cause execution to timeout without proper precautions in place.
  852. *
  853. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  854. * objects representing all nicknames in the domain.
  855. * @throws Zend_Gdata_App_Exception
  856. * @throws Zend_Gdata_App_HttpException
  857. * @throws Zend_Gdata_Gapps_ServiceException
  858. */
  859. public function retrieveAllNicknames () {
  860. return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames());
  861. }
  862. /**
  863. * Delete a specified nickname.
  864. *
  865. * @param string $nickname The name of the nickname to be deleted.
  866. * @throws Zend_Gdata_App_Exception
  867. * @throws Zend_Gdata_App_HttpException
  868. * @throws Zend_Gdata_Gapps_ServiceException
  869. */
  870. public function deleteNickname($nickname) {
  871. $this->delete($this->getBaseUrl() . self::APPS_NICKNAME_PATH . '/' . $nickname);
  872. }
  873. /**
  874. * Create a new email list.
  875. *
  876. * @param string $emailList The name of the email list to be created.
  877. * @return Zend_Gdata_Gapps_EmailListEntry The email list entry
  878. * as created on the server.
  879. * @throws Zend_Gdata_App_Exception
  880. * @throws Zend_Gdata_App_HttpException
  881. * @throws Zend_Gdata_Gapps_ServiceException
  882. */
  883. public function createEmailList($emailList) {
  884. $entry = $this->newEmailListEntry();
  885. $list = $this->newEmailList();
  886. $list->name = $emailList;
  887. $entry->emailList = $list;
  888. return $this->insertEmailList($entry);
  889. }
  890. /**
  891. * Retrieve all email lists associated with a recipient.
  892. *
  893. * @param string $username The recipient whose associated email lists
  894. * should be returned.
  895. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found as
  896. * Zend_Gdata_EmailListEntry objects.
  897. * @throws Zend_Gdata_App_Exception
  898. * @throws Zend_Gdata_App_HttpException
  899. * @throws Zend_Gdata_Gapps_ServiceException
  900. */
  901. public function retrieveEmailLists($recipient) {
  902. $query = $this->newEmailListQuery();
  903. $query->recipient = $recipient;
  904. return $this->getEmailListFeed($query);
  905. }
  906. /**
  907. * Retrieve a page of email lists in alphabetical order, starting with the
  908. * provided email list.
  909. *
  910. * @param string $startEmailListName (optional) The first list to
  911. * retrieve. If null or not defined, the page will begin
  912. * with the first email list in the domain.
  913. * @return Zend_Gdata_Gapps_EmailListFeed Collection of Zend_Gdata_EmailListEntry
  914. * objects representing all nicknames in the domain.
  915. * @throws Zend_Gdata_App_Exception
  916. * @throws Zend_Gdata_App_HttpException
  917. * @throws Zend_Gdata_Gapps_ServiceException
  918. */
  919. public function retrievePageOfEmailLists ($startNickname = null) {
  920. $query = $this->newEmailListQuery();
  921. $query->setStartEmailListName($startNickname);
  922. return $this->getEmailListFeed($query);
  923. }
  924. /**
  925. * Retrieve all email lists associated with the curent domain. Be aware that
  926. * calling this function on a domain with many email lists will take a
  927. * signifigant amount of time to complete. On larger domains this may
  928. * may cause execution to timeout without proper precautions in place.
  929. *
  930. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found
  931. * as Zend_Gdata_Gapps_EmailListEntry objects.
  932. * @throws Zend_Gdata_App_Exception
  933. * @throws Zend_Gdata_App_HttpException
  934. * @throws Zend_Gdata_Gapps_ServiceException
  935. */
  936. public function retrieveAllEmailLists() {
  937. return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists());
  938. }
  939. /**
  940. * Delete a specified email list.
  941. *
  942. * @param string $emailList The name of the emailList to be deleted.
  943. * @throws Zend_Gdata_App_Exception
  944. * @throws Zend_Gdata_App_HttpException
  945. * @throws Zend_Gdata_Gapps_ServiceException
  946. */
  947. public function deleteEmailList($emailList) {
  948. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  949. . $emailList);
  950. }
  951. /**
  952. * Add a specified recipient to an existing emailList.
  953. *
  954. * @param string $recipientAddress The address of the recipient to be
  955. * added to the email list.
  956. * @param string $emailList The name of the email address to which the
  957. * recipient should be added.
  958. * @return Zend_Gdata_Gapps_EmailListRecipientEntry The recipient entry
  959. * created by the server.
  960. * @throws Zend_Gdata_App_Exception
  961. * @throws Zend_Gdata_App_HttpException
  962. * @throws Zend_Gdata_Gapps_ServiceException
  963. */
  964. public function addRecipientToEmailList($recipientAddress, $emailList) {
  965. $entry = $this->newEmailListRecipientEntry();
  966. $who = $this->newWho();
  967. $who->email = $recipientAddress;
  968. $entry->who = $who;
  969. $address = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' .
  970. $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/';
  971. return $this->insertEmailListRecipient($entry, $address);
  972. }
  973. /**
  974. * Retrieve a page of email list recipients in alphabetical order,
  975. * starting with the provided email list recipient.
  976. *
  977. * @param string $emaiList The email list which should be searched.
  978. * @param string $startRecipient (optinal) The address of the first
  979. * recipient, or null to start with the first recipient in
  980. * the list.
  981. * @return Zend_Gdata_Gapps_EmailListRecipientFeed Collection of
  982. * Zend_Gdata_EmailListRecipientEntry objects representing all
  983. * recpients in the specified list.
  984. * @throws Zend_Gdata_App_Exception
  985. * @throws Zend_Gdata_App_HttpException
  986. * @throws Zend_Gdata_Gapps_ServiceException
  987. */
  988. public function retrievePageOfRecipients ($emailList,
  989. $startRecipient = null) {
  990. $query = $this->newEmailListRecipientQuery();
  991. $query->setEmailListName($emailList);
  992. $query->setStartRecipient($startRecipient);
  993. return $this->getEmailListRecipientFeed($query);
  994. }
  995. /**
  996. * Retrieve all recipients associated with an email list. Be aware that
  997. * calling this function on a domain with many email lists will take a
  998. * signifigant amount of time to complete. On larger domains this may
  999. * may cause execution to timeout without proper precautions in place.
  1000. *
  1001. * @param string $emaiList The email list which should be searched.
  1002. * @return Zend_Gdata_Gapps_EmailListRecipientFeed The list of email lists
  1003. * found as Zend_Gdata_Gapps_EmailListRecipientEntry objects.
  1004. * @throws Zend_Gdata_App_Exception
  1005. * @throws Zend_Gdata_App_HttpException
  1006. * @throws Zend_Gdata_Gapps_ServiceException
  1007. */
  1008. public function retrieveAllRecipients($emailList) {
  1009. return $this->retrieveAllEntriesForFeed(
  1010. $this->retrievePageOfRecipients($emailList));
  1011. }
  1012. /**
  1013. * Remove a specified recipient from an email list.
  1014. *
  1015. * @param string $recipientAddress The recipient to be removed.
  1016. * @param string $emailList The list from which the recipient should
  1017. * be removed.
  1018. * @throws Zend_Gdata_App_Exception
  1019. * @throws Zend_Gdata_App_HttpException
  1020. * @throws Zend_Gdata_Gapps_ServiceException
  1021. */
  1022. public function removeRecipientFromEmailList($recipientAddress, $emailList) {
  1023. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  1024. . $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/'
  1025. . $recipientAddress);
  1026. }
  1027. }