Gapps.php 41 KB

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