Gapps.php 41 KB

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