Gapps.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. if (!class_exists($name . '_' . $class)) {
  589. require_once 'Zend/Loader.php';
  590. @Zend_Loader::loadClass($name . '_' . $class);
  591. }
  592. $foundClassName = $name . '_' . $class;
  593. break;
  594. } catch (Zend_Exception $e) {
  595. // package wasn't here- continue searching
  596. }
  597. }
  598. if ($foundClassName != null) {
  599. $reflectionObj = new ReflectionClass($foundClassName);
  600. // Prepend the domain to the query
  601. $args = array_merge(array($this->getDomain()), $args);
  602. return $reflectionObj->newInstanceArgs($args);
  603. } else {
  604. require_once 'Zend/Gdata/App/Exception.php';
  605. throw new Zend_Gdata_App_Exception(
  606. "Unable to find '${class}' in registered packages");
  607. }
  608. } else {
  609. return parent::__call($method, $args);
  610. }
  611. }
  612. // Convenience methods
  613. // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e
  614. /**
  615. * Create a new user entry and send it to the Google Apps servers.
  616. *
  617. * @param string $username The username for the new user.
  618. * @param string $givenName The given name for the new user.
  619. * @param string $familyName The family name for the new user.
  620. * @param string $password The password for the new user as a plaintext string
  621. * (if $passwordHashFunction is null) or a SHA-1 hashed
  622. * value (if $passwordHashFunction = 'SHA-1').
  623. * @param string $quotaLimitInMB (optional) The quota limit for the new user in MB.
  624. * @return Zend_Gdata_Gapps_UserEntry (optional) The new user entry as returned by
  625. * server.
  626. * @throws Zend_Gdata_App_Exception
  627. * @throws Zend_Gdata_App_HttpException
  628. * @throws Zend_Gdata_Gapps_ServiceException
  629. */
  630. public function createUser ($username, $givenName, $familyName, $password,
  631. $passwordHashFunction = null, $quotaLimitInMB = null) {
  632. $user = $this->newUserEntry();
  633. $user->login = $this->newLogin();
  634. $user->login->username = $username;
  635. $user->login->password = $password;
  636. $user->login->hashFunctionName = $passwordHashFunction;
  637. $user->name = $this->newName();
  638. $user->name->givenName = $givenName;
  639. $user->name->familyName = $familyName;
  640. if ($quotaLimitInMB !== null) {
  641. $user->quota = $this->newQuota();
  642. $user->quota->limit = $quotaLimitInMB;
  643. }
  644. return $this->insertUser($user);
  645. }
  646. /**
  647. * Retrieve a user based on their username.
  648. *
  649. * @param string $username The username to search for.
  650. * @return Zend_Gdata_Gapps_UserEntry The username to search for, or null
  651. * if no match found.
  652. * @throws Zend_Gdata_App_InvalidArgumentException
  653. * @throws Zend_Gdata_App_HttpException
  654. */
  655. public function retrieveUser ($username) {
  656. $query = $this->newUserQuery($username);
  657. try {
  658. $user = $this->getUserEntry($query);
  659. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  660. // Set the user to null if not found
  661. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  662. $user = null;
  663. } else {
  664. throw $e;
  665. }
  666. }
  667. return $user;
  668. }
  669. /**
  670. * Retrieve a page of users in alphabetical order, starting with the
  671. * provided username.
  672. *
  673. * @param string $startUsername (optional) The first username to retrieve.
  674. * If null or not declared, the page will begin with the first
  675. * user in the domain.
  676. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  677. * objects representing all users in the domain.
  678. * @throws Zend_Gdata_App_Exception
  679. * @throws Zend_Gdata_App_HttpException
  680. * @throws Zend_Gdata_Gapps_ServiceException
  681. */
  682. public function retrievePageOfUsers ($startUsername = null) {
  683. $query = $this->newUserQuery();
  684. $query->setStartUsername($startUsername);
  685. return $this->getUserFeed($query);
  686. }
  687. /**
  688. * Retrieve all users in the current domain. Be aware that
  689. * calling this function on a domain with many users will take a
  690. * signifigant amount of time to complete. On larger domains this may
  691. * may cause execution to timeout without proper precautions in place.
  692. *
  693. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  694. * objects representing all users in the domain.
  695. * @throws Zend_Gdata_App_Exception
  696. * @throws Zend_Gdata_App_HttpException
  697. * @throws Zend_Gdata_Gapps_ServiceException
  698. */
  699. public function retrieveAllUsers () {
  700. return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers());
  701. }
  702. /**
  703. * Overwrite a specified username with the provided UserEntry. The
  704. * UserEntry does not need to contain an edit link.
  705. *
  706. * This method is provided for compliance with the Google Apps
  707. * Provisioning API specification. Normally users will instead want to
  708. * call UserEntry::save() instead.
  709. *
  710. * @see Zend_Gdata_App_Entry::save
  711. * @param string $username The username whose data will be overwritten.
  712. * @param Zend_Gdata_Gapps_UserEntry $userEntry The user entry which
  713. * will be overwritten.
  714. * @return Zend_Gdata_Gapps_UserEntry The UserEntry returned by the
  715. * server.
  716. * @throws Zend_Gdata_App_Exception
  717. * @throws Zend_Gdata_App_HttpException
  718. * @throws Zend_Gdata_Gapps_ServiceException
  719. */
  720. public function updateUser($username, $userEntry) {
  721. return $this->updateEntry($userEntry, $this->getBaseUrl() .
  722. self::APPS_USER_PATH . '/' . $username);
  723. }
  724. /**
  725. * Mark a given user as suspended.
  726. *
  727. * @param string $username The username associated with the user who
  728. * should be suspended.
  729. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  730. * user.
  731. * @throws Zend_Gdata_App_Exception
  732. * @throws Zend_Gdata_App_HttpException
  733. * @throws Zend_Gdata_Gapps_ServiceException
  734. */
  735. public function suspendUser($username) {
  736. $user = $this->retrieveUser($username);
  737. $user->login->suspended = true;
  738. return $user->save();
  739. }
  740. /**
  741. * Mark a given user as not suspended.
  742. *
  743. * @param string $username The username associated with the user who
  744. * should be restored.
  745. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  746. * user.
  747. * @throws Zend_Gdata_App_Exception
  748. * @throws Zend_Gdata_App_HttpException
  749. * @throws Zend_Gdata_Gapps_ServiceException
  750. */
  751. public function restoreUser($username) {
  752. $user = $this->retrieveUser($username);
  753. $user->login->suspended = false;
  754. return $user->save();
  755. }
  756. /**
  757. * Delete a user by username.
  758. *
  759. * @param string $username The username associated with the user who
  760. * should be deleted.
  761. * @throws Zend_Gdata_App_Exception
  762. * @throws Zend_Gdata_App_HttpException
  763. * @throws Zend_Gdata_Gapps_ServiceException
  764. */
  765. public function deleteUser($username) {
  766. $this->delete($this->getBaseUrl() . self::APPS_USER_PATH . '/' .
  767. $username);
  768. }
  769. /**
  770. * Create a nickname for a given user.
  771. *
  772. * @param string $username The username to which the new nickname should
  773. * be associated.
  774. * @param string $nickname The new nickname to be created.
  775. * @return Zend_Gdata_Gapps_NicknameEntry The nickname entry which was
  776. * created by the server.
  777. * @throws Zend_Gdata_App_Exception
  778. * @throws Zend_Gdata_App_HttpException
  779. * @throws Zend_Gdata_Gapps_ServiceException
  780. */
  781. public function createNickname($username, $nickname) {
  782. $entry = $this->newNicknameEntry();
  783. $nickname = $this->newNickname($nickname);
  784. $login = $this->newLogin($username);
  785. $entry->nickname = $nickname;
  786. $entry->login = $login;
  787. return $this->insertNickname($entry);
  788. }
  789. /**
  790. * Retrieve the entry for a specified nickname.
  791. *
  792. * @param string $nickname The nickname to be retrieved.
  793. * @return Zend_Gdata_Gapps_NicknameEntry The requested nickname entry.
  794. * @throws Zend_Gdata_App_Exception
  795. * @throws Zend_Gdata_App_HttpException
  796. * @throws Zend_Gdata_Gapps_ServiceException
  797. */
  798. public function retrieveNickname($nickname) {
  799. $query = $this->newNicknameQuery();
  800. $query->setNickname($nickname);
  801. try {
  802. $nickname = $this->getNicknameEntry($query);
  803. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  804. // Set the nickname to null if not found
  805. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  806. $nickname = null;
  807. } else {
  808. throw $e;
  809. }
  810. }
  811. return $nickname;
  812. }
  813. /**
  814. * Retrieve all nicknames associated with a specific username.
  815. *
  816. * @param string $username The username whose nicknames should be
  817. * returned.
  818. * @return Zend_Gdata_Gapps_NicknameFeed A feed containing all nicknames
  819. * for the given user, or null if
  820. * @throws Zend_Gdata_App_Exception
  821. * @throws Zend_Gdata_App_HttpException
  822. * @throws Zend_Gdata_Gapps_ServiceException
  823. */
  824. public function retrieveNicknames($username) {
  825. $query = $this->newNicknameQuery();
  826. $query->setUsername($username);
  827. $nicknameFeed = $this->retrieveAllEntriesForFeed(
  828. $this->getNicknameFeed($query));
  829. return $nicknameFeed;
  830. }
  831. /**
  832. * Retrieve a page of nicknames in alphabetical order, starting with the
  833. * provided nickname.
  834. *
  835. * @param string $startNickname (optional) The first nickname to
  836. * retrieve. If null or not declared, the page will begin with
  837. * the first nickname in the domain.
  838. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  839. * objects representing all nicknames in the domain.
  840. * @throws Zend_Gdata_App_Exception
  841. * @throws Zend_Gdata_App_HttpException
  842. * @throws Zend_Gdata_Gapps_ServiceException
  843. */
  844. public function retrievePageOfNicknames ($startNickname = null) {
  845. $query = $this->newNicknameQuery();
  846. $query->setStartNickname($startNickname);
  847. return $this->getNicknameFeed($query);
  848. }
  849. /**
  850. * Retrieve all nicknames in the current domain. Be aware that
  851. * calling this function on a domain with many nicknames will take a
  852. * signifigant amount of time to complete. On larger domains this may
  853. * may cause execution to timeout without proper precautions in place.
  854. *
  855. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  856. * objects representing all nicknames in the domain.
  857. * @throws Zend_Gdata_App_Exception
  858. * @throws Zend_Gdata_App_HttpException
  859. * @throws Zend_Gdata_Gapps_ServiceException
  860. */
  861. public function retrieveAllNicknames () {
  862. return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames());
  863. }
  864. /**
  865. * Delete a specified nickname.
  866. *
  867. * @param string $nickname The name of the nickname to be deleted.
  868. * @throws Zend_Gdata_App_Exception
  869. * @throws Zend_Gdata_App_HttpException
  870. * @throws Zend_Gdata_Gapps_ServiceException
  871. */
  872. public function deleteNickname($nickname) {
  873. $this->delete($this->getBaseUrl() . self::APPS_NICKNAME_PATH . '/' . $nickname);
  874. }
  875. /**
  876. * Create a new email list.
  877. *
  878. * @param string $emailList The name of the email list to be created.
  879. * @return Zend_Gdata_Gapps_EmailListEntry The email list entry
  880. * as created on the server.
  881. * @throws Zend_Gdata_App_Exception
  882. * @throws Zend_Gdata_App_HttpException
  883. * @throws Zend_Gdata_Gapps_ServiceException
  884. */
  885. public function createEmailList($emailList) {
  886. $entry = $this->newEmailListEntry();
  887. $list = $this->newEmailList();
  888. $list->name = $emailList;
  889. $entry->emailList = $list;
  890. return $this->insertEmailList($entry);
  891. }
  892. /**
  893. * Retrieve all email lists associated with a recipient.
  894. *
  895. * @param string $username The recipient whose associated email lists
  896. * should be returned.
  897. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found as
  898. * Zend_Gdata_EmailListEntry objects.
  899. * @throws Zend_Gdata_App_Exception
  900. * @throws Zend_Gdata_App_HttpException
  901. * @throws Zend_Gdata_Gapps_ServiceException
  902. */
  903. public function retrieveEmailLists($recipient) {
  904. $query = $this->newEmailListQuery();
  905. $query->recipient = $recipient;
  906. return $this->getEmailListFeed($query);
  907. }
  908. /**
  909. * Retrieve a page of email lists in alphabetical order, starting with the
  910. * provided email list.
  911. *
  912. * @param string $startEmailListName (optional) The first list to
  913. * retrieve. If null or not defined, the page will begin
  914. * with the first email list in the domain.
  915. * @return Zend_Gdata_Gapps_EmailListFeed Collection of Zend_Gdata_EmailListEntry
  916. * objects representing all nicknames in the domain.
  917. * @throws Zend_Gdata_App_Exception
  918. * @throws Zend_Gdata_App_HttpException
  919. * @throws Zend_Gdata_Gapps_ServiceException
  920. */
  921. public function retrievePageOfEmailLists ($startNickname = null) {
  922. $query = $this->newEmailListQuery();
  923. $query->setStartEmailListName($startNickname);
  924. return $this->getEmailListFeed($query);
  925. }
  926. /**
  927. * Retrieve all email lists associated with the curent domain. Be aware that
  928. * calling this function on a domain with many email lists will take a
  929. * signifigant amount of time to complete. On larger domains this may
  930. * may cause execution to timeout without proper precautions in place.
  931. *
  932. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found
  933. * as Zend_Gdata_Gapps_EmailListEntry objects.
  934. * @throws Zend_Gdata_App_Exception
  935. * @throws Zend_Gdata_App_HttpException
  936. * @throws Zend_Gdata_Gapps_ServiceException
  937. */
  938. public function retrieveAllEmailLists() {
  939. return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists());
  940. }
  941. /**
  942. * Delete a specified email list.
  943. *
  944. * @param string $emailList The name of the emailList to be deleted.
  945. * @throws Zend_Gdata_App_Exception
  946. * @throws Zend_Gdata_App_HttpException
  947. * @throws Zend_Gdata_Gapps_ServiceException
  948. */
  949. public function deleteEmailList($emailList) {
  950. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  951. . $emailList);
  952. }
  953. /**
  954. * Add a specified recipient to an existing emailList.
  955. *
  956. * @param string $recipientAddress The address of the recipient to be
  957. * added to the email list.
  958. * @param string $emailList The name of the email address to which the
  959. * recipient should be added.
  960. * @return Zend_Gdata_Gapps_EmailListRecipientEntry The recipient entry
  961. * created by the server.
  962. * @throws Zend_Gdata_App_Exception
  963. * @throws Zend_Gdata_App_HttpException
  964. * @throws Zend_Gdata_Gapps_ServiceException
  965. */
  966. public function addRecipientToEmailList($recipientAddress, $emailList) {
  967. $entry = $this->newEmailListRecipientEntry();
  968. $who = $this->newWho();
  969. $who->email = $recipientAddress;
  970. $entry->who = $who;
  971. $address = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' .
  972. $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/';
  973. return $this->insertEmailListRecipient($entry, $address);
  974. }
  975. /**
  976. * Retrieve a page of email list recipients in alphabetical order,
  977. * starting with the provided email list recipient.
  978. *
  979. * @param string $emaiList The email list which should be searched.
  980. * @param string $startRecipient (optinal) The address of the first
  981. * recipient, or null to start with the first recipient in
  982. * the list.
  983. * @return Zend_Gdata_Gapps_EmailListRecipientFeed Collection of
  984. * Zend_Gdata_EmailListRecipientEntry objects representing all
  985. * recpients in the specified list.
  986. * @throws Zend_Gdata_App_Exception
  987. * @throws Zend_Gdata_App_HttpException
  988. * @throws Zend_Gdata_Gapps_ServiceException
  989. */
  990. public function retrievePageOfRecipients ($emailList,
  991. $startRecipient = null) {
  992. $query = $this->newEmailListRecipientQuery();
  993. $query->setEmailListName($emailList);
  994. $query->setStartRecipient($startRecipient);
  995. return $this->getEmailListRecipientFeed($query);
  996. }
  997. /**
  998. * Retrieve all recipients associated with an email list. Be aware that
  999. * calling this function on a domain with many email lists will take a
  1000. * signifigant amount of time to complete. On larger domains this may
  1001. * may cause execution to timeout without proper precautions in place.
  1002. *
  1003. * @param string $emaiList The email list which should be searched.
  1004. * @return Zend_Gdata_Gapps_EmailListRecipientFeed The list of email lists
  1005. * found as Zend_Gdata_Gapps_EmailListRecipientEntry objects.
  1006. * @throws Zend_Gdata_App_Exception
  1007. * @throws Zend_Gdata_App_HttpException
  1008. * @throws Zend_Gdata_Gapps_ServiceException
  1009. */
  1010. public function retrieveAllRecipients($emailList) {
  1011. return $this->retrieveAllEntriesForFeed(
  1012. $this->retrievePageOfRecipients($emailList));
  1013. }
  1014. /**
  1015. * Remove a specified recipient from an email list.
  1016. *
  1017. * @param string $recipientAddress The recipient to be removed.
  1018. * @param string $emailList The list from which the recipient should
  1019. * be removed.
  1020. * @throws Zend_Gdata_App_Exception
  1021. * @throws Zend_Gdata_App_HttpException
  1022. * @throws Zend_Gdata_Gapps_ServiceException
  1023. */
  1024. public function removeRecipientFromEmailList($recipientAddress, $emailList) {
  1025. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  1026. . $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/'
  1027. . $recipientAddress);
  1028. }
  1029. }