Gapps.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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-2015 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_GroupFeed
  36. */
  37. require_once 'Zend/Gdata/Gapps/GroupFeed.php';
  38. /**
  39. * @see Zend_Gdata_Gapps_MemberFeed
  40. */
  41. require_once 'Zend/Gdata/Gapps/MemberFeed.php';
  42. /**
  43. * @see Zend_Gdata_Gapps_OwnerFeed
  44. */
  45. require_once 'Zend/Gdata/Gapps/OwnerFeed.php';
  46. /**
  47. * @see Zend_Gdata_Gapps_EmailListFeed
  48. */
  49. require_once 'Zend/Gdata/Gapps/EmailListFeed.php';
  50. /**
  51. * @see Zend_Gdata_Gapps_EmailListRecipientFeed
  52. */
  53. require_once 'Zend/Gdata/Gapps/EmailListRecipientFeed.php';
  54. /**
  55. * Service class for interacting with the Google Apps Provisioning API.
  56. *
  57. * Like other service classes in this module, this class provides access via
  58. * an HTTP client to Google servers for working with entries and feeds.
  59. *
  60. * Because of the nature of this API, all access must occur over an
  61. * authenticated connection.
  62. *
  63. * @link http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html
  64. *
  65. * @category Zend
  66. * @package Zend_Gdata
  67. * @subpackage Gapps
  68. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  69. * @license http://framework.zend.com/license/new-bsd New BSD License
  70. */
  71. class Zend_Gdata_Gapps extends Zend_Gdata
  72. {
  73. const APPS_BASE_FEED_URI = 'https://apps-apis.google.com/a/feeds';
  74. const AUTH_SERVICE_NAME = 'apps';
  75. /**
  76. * Path to user feeds on the Google Apps server.
  77. */
  78. const APPS_USER_PATH = '/user/2.0';
  79. /**
  80. * Path to nickname feeds on the Google Apps server.
  81. */
  82. const APPS_NICKNAME_PATH = '/nickname/2.0';
  83. /**
  84. * Path to group feeds on the Google Apps server.
  85. */
  86. const APPS_GROUP_PATH = '/group/2.0';
  87. /**
  88. * Path to email list feeds on the Google Apps server.
  89. */
  90. const APPS_EMAIL_LIST_PATH = '/emailList/2.0';
  91. /**
  92. * Path to email list recipient feeds on the Google Apps server.
  93. */
  94. const APPS_EMAIL_LIST_RECIPIENT_POSTFIX = '/recipient';
  95. /**
  96. * The domain which is being administered via the Provisioning API.
  97. *
  98. * @var string
  99. */
  100. protected $_domain = null;
  101. /**
  102. * Namespaces used for Zend_Gdata_Gapps
  103. *
  104. * @var array
  105. */
  106. public static $namespaces = array(
  107. array('apps', 'http://schemas.google.com/apps/2006', 1, 0)
  108. );
  109. /**
  110. * Create Gdata_Gapps object
  111. *
  112. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  113. * when communicating with the Google Apps servers.
  114. * @param string $domain (optional) The Google Apps domain which is to be
  115. * accessed.
  116. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  117. */
  118. public function __construct($client = null, $domain = null, $applicationId = 'MyCompany-MyApp-1.0')
  119. {
  120. $this->registerPackage('Zend_Gdata_Gapps');
  121. $this->registerPackage('Zend_Gdata_Gapps_Extension');
  122. parent::__construct($client, $applicationId);
  123. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  124. $this->_domain = $domain;
  125. }
  126. /**
  127. * Convert an exception to an ServiceException if an AppsForYourDomain
  128. * XML document is contained within the original exception's HTTP
  129. * response. If conversion fails, throw the original error.
  130. *
  131. * @param Zend_Gdata_Exception $e The exception to convert.
  132. * @throws Zend_Gdata_Gapps_ServiceException
  133. * @throws mixed
  134. */
  135. public static function throwServiceExceptionIfDetected($e) {
  136. // Check to make sure that there actually response!
  137. // This can happen if the connection dies before the request
  138. // completes. (See ZF-5949)
  139. $response = $e->getResponse();
  140. if (!$response) {
  141. require_once('Zend/Gdata/App/IOException.php');
  142. throw new Zend_Gdata_App_IOException('No HTTP response received (possible connection failure)');
  143. }
  144. try {
  145. // Check to see if there is an AppsForYourDomainErrors
  146. // datastructure in the response. If so, convert it to
  147. // an exception and throw it.
  148. require_once 'Zend/Gdata/Gapps/ServiceException.php';
  149. $error = new Zend_Gdata_Gapps_ServiceException();
  150. $error->importFromString($response->getBody());
  151. throw $error;
  152. } catch (Zend_Gdata_App_Exception $e2) {
  153. // Unable to convert the response to a ServiceException,
  154. // most likely because the server didn't return an
  155. // AppsForYourDomainErrors document. Throw the original
  156. // exception.
  157. throw $e;
  158. }
  159. }
  160. /**
  161. * Imports a feed located at $uri.
  162. * This method overrides the default behavior of Zend_Gdata_App,
  163. * providing support for Zend_Gdata_Gapps_ServiceException.
  164. *
  165. * @param string $uri
  166. * @param Zend_Http_Client $client (optional) The client used for
  167. * communication
  168. * @param string $className (optional) The class which is used as the
  169. * return type
  170. * @throws Zend_Gdata_App_Exception
  171. * @throws Zend_Gdata_App_HttpException
  172. * @throws Zend_Gdata_Gapps_ServiceException
  173. * @return Zend_Gdata_App_Feed
  174. */
  175. public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed', $useObjectMapping = true)
  176. {
  177. try {
  178. return parent::import($uri, $client, $className, $useObjectMapping);
  179. } catch (Zend_Gdata_App_HttpException $e) {
  180. self::throwServiceExceptionIfDetected($e);
  181. }
  182. }
  183. /**
  184. * GET a URI using client object.
  185. * This method overrides the default behavior of Zend_Gdata_App,
  186. * providing support for Zend_Gdata_Gapps_ServiceException.
  187. *
  188. * @param string $uri GET URI
  189. * @param array $extraHeaders Extra headers to add to the request, as an
  190. * array of string-based key/value pairs.
  191. * @throws Zend_Gdata_App_HttpException
  192. * @throws Zend_Gdata_Gapps_ServiceException
  193. * @return Zend_Http_Response
  194. */
  195. public function get($uri, $extraHeaders = array())
  196. {
  197. try {
  198. return parent::get($uri, $extraHeaders);
  199. } catch (Zend_Gdata_App_HttpException $e) {
  200. self::throwServiceExceptionIfDetected($e);
  201. }
  202. }
  203. /**
  204. * POST data with client object.
  205. * This method overrides the default behavior of Zend_Gdata_App,
  206. * providing support for Zend_Gdata_Gapps_ServiceException.
  207. *
  208. * @param mixed $data The Zend_Gdata_App_Entry or XML to post
  209. * @param string $uri (optional) POST URI
  210. * @param integer $remainingRedirects (optional)
  211. * @param string $contentType Content-type of the data
  212. * @param array $extraHaders Extra headers to add tot he request
  213. * @return Zend_Http_Response
  214. * @throws Zend_Gdata_App_HttpException
  215. * @throws Zend_Gdata_App_InvalidArgumentException
  216. * @throws Zend_Gdata_Gapps_ServiceException
  217. */
  218. public function post($data, $uri = null, $remainingRedirects = null,
  219. $contentType = null, $extraHeaders = null)
  220. {
  221. try {
  222. return parent::post($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
  223. } catch (Zend_Gdata_App_HttpException $e) {
  224. self::throwServiceExceptionIfDetected($e);
  225. }
  226. }
  227. /**
  228. * PUT data with client object
  229. * This method overrides the default behavior of Zend_Gdata_App,
  230. * providing support for Zend_Gdata_Gapps_ServiceException.
  231. *
  232. * @param mixed $data The Zend_Gdata_App_Entry or XML to post
  233. * @param string $uri (optional) PUT URI
  234. * @param integer $remainingRedirects (optional)
  235. * @param string $contentType Content-type of the data
  236. * @param array $extraHaders Extra headers to add tot he request
  237. * @return Zend_Http_Response
  238. * @throws Zend_Gdata_App_HttpException
  239. * @throws Zend_Gdata_App_InvalidArgumentException
  240. * @throws Zend_Gdata_Gapps_ServiceException
  241. */
  242. public function put($data, $uri = null, $remainingRedirects = null,
  243. $contentType = null, $extraHeaders = null)
  244. {
  245. try {
  246. return parent::put($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
  247. } catch (Zend_Gdata_App_HttpException $e) {
  248. self::throwServiceExceptionIfDetected($e);
  249. }
  250. }
  251. /**
  252. * DELETE entry with client object
  253. * This method overrides the default behavior of Zend_Gdata_App,
  254. * providing support for Zend_Gdata_Gapps_ServiceException.
  255. *
  256. * @param mixed $data The Zend_Gdata_App_Entry or URL to delete
  257. * @param integer $remainingRedirects (optional)
  258. * @return void
  259. * @throws Zend_Gdata_App_HttpException
  260. * @throws Zend_Gdata_App_InvalidArgumentException
  261. * @throws Zend_Gdata_Gapps_ServiceException
  262. */
  263. public function delete($data, $remainingRedirects = null)
  264. {
  265. try {
  266. return parent::delete($data, $remainingRedirects);
  267. } catch (Zend_Gdata_App_HttpException $e) {
  268. self::throwServiceExceptionIfDetected($e);
  269. }
  270. }
  271. /**
  272. * Set domain for this service instance. This should be a fully qualified
  273. * domain, such as 'foo.example.com'.
  274. *
  275. * This value is used when calculating URLs for retrieving and posting
  276. * entries. If no value is specified, a URL will have to be manually
  277. * constructed prior to using any methods which interact with the Google
  278. * Apps provisioning service.
  279. *
  280. * @param string $value The domain to be used for this session.
  281. */
  282. public function setDomain($value)
  283. {
  284. $this->_domain = $value;
  285. }
  286. /**
  287. * Get domain for this service instance. This should be a fully qualified
  288. * domain, such as 'foo.example.com'. If no domain is set, null will be
  289. * returned.
  290. *
  291. * @return string The domain to be used for this session, or null if not
  292. * set.
  293. */
  294. public function getDomain()
  295. {
  296. return $this->_domain;
  297. }
  298. /**
  299. * Returns the base URL used to access the Google Apps service, based
  300. * on the current domain. The current domain can be temporarily
  301. * overridden by providing a fully qualified domain as $domain.
  302. *
  303. * @param string $domain (optional) A fully-qualified domain to use
  304. * instead of the default domain for this service instance.
  305. * @throws Zend_Gdata_App_InvalidArgumentException
  306. */
  307. public function getBaseUrl($domain = null)
  308. {
  309. if ($domain !== null) {
  310. return self::APPS_BASE_FEED_URI . '/' . $domain;
  311. } else if ($this->_domain !== null) {
  312. return self::APPS_BASE_FEED_URI . '/' . $this->_domain;
  313. } else {
  314. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  315. throw new Zend_Gdata_App_InvalidArgumentException(
  316. 'Domain must be specified.');
  317. }
  318. }
  319. /**
  320. * Retrieve a UserFeed containing multiple UserEntry objects.
  321. *
  322. * @param mixed $location (optional) The location for the feed, as a URL
  323. * or Query.
  324. * @return Zend_Gdata_Gapps_UserFeed
  325. * @throws Zend_Gdata_App_Exception
  326. * @throws Zend_Gdata_App_HttpException
  327. * @throws Zend_Gdata_Gapps_ServiceException
  328. */
  329. public function getUserFeed($location = null)
  330. {
  331. if ($location === null) {
  332. $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
  333. } else if ($location instanceof Zend_Gdata_Query) {
  334. $uri = $location->getQueryUrl();
  335. } else {
  336. $uri = $location;
  337. }
  338. return parent::getFeed($uri, 'Zend_Gdata_Gapps_UserFeed');
  339. }
  340. /**
  341. * Retreive NicknameFeed object containing multiple NicknameEntry objects.
  342. *
  343. * @param mixed $location (optional) The location for the feed, as a URL
  344. * or Query.
  345. * @return Zend_Gdata_Gapps_NicknameFeed
  346. * @throws Zend_Gdata_App_Exception
  347. * @throws Zend_Gdata_App_HttpException
  348. * @throws Zend_Gdata_Gapps_ServiceException
  349. */
  350. public function getNicknameFeed($location = null)
  351. {
  352. if ($location === null) {
  353. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  354. } else if ($location instanceof Zend_Gdata_Query) {
  355. $uri = $location->getQueryUrl();
  356. } else {
  357. $uri = $location;
  358. }
  359. return parent::getFeed($uri, 'Zend_Gdata_Gapps_NicknameFeed');
  360. }
  361. /**
  362. * Retreive GroupFeed object containing multiple GroupEntry
  363. * objects.
  364. *
  365. * @param mixed $location (optional) The location for the feed, as a URL
  366. * or Query.
  367. * @return Zend_Gdata_Gapps_GroupFeed
  368. * @throws Zend_Gdata_App_Exception
  369. * @throws Zend_Gdata_App_HttpException
  370. * @throws Zend_Gdata_Gapps_ServiceException
  371. */
  372. public function getGroupFeed($location = null)
  373. {
  374. if ($location === null) {
  375. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  376. $uri .= $this->getDomain();
  377. } else if ($location instanceof Zend_Gdata_Query) {
  378. $uri = $location->getQueryUrl();
  379. } else {
  380. $uri = $location;
  381. }
  382. return parent::getFeed($uri, 'Zend_Gdata_Gapps_GroupFeed');
  383. }
  384. /**
  385. * Retreive MemberFeed object containing multiple MemberEntry
  386. * objects.
  387. *
  388. * @param mixed $location (optional) The location for the feed, as a URL
  389. * or Query.
  390. * @return Zend_Gdata_Gapps_MemberFeed
  391. * @throws Zend_Gdata_App_Exception
  392. * @throws Zend_Gdata_App_HttpException
  393. * @throws Zend_Gdata_Gapps_ServiceException
  394. */
  395. public function getMemberFeed($location = null)
  396. {
  397. if ($location === null) {
  398. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  399. throw new Zend_Gdata_App_InvalidArgumentException(
  400. 'Location must not be null');
  401. } else if ($location instanceof Zend_Gdata_Query) {
  402. $uri = $location->getQueryUrl();
  403. } else {
  404. $uri = $location;
  405. }
  406. return parent::getFeed($uri, 'Zend_Gdata_Gapps_MemberFeed');
  407. }
  408. /**
  409. * Retreive OwnerFeed object containing multiple OwnerEntry
  410. * objects.
  411. *
  412. * @param mixed $location (optional) The location for the feed, as a URL
  413. * or Query.
  414. * @return Zend_Gdata_Gapps_OwnerFeed
  415. * @throws Zend_Gdata_App_Exception
  416. * @throws Zend_Gdata_App_HttpException
  417. * @throws Zend_Gdata_Gapps_ServiceException
  418. */
  419. public function getOwnerFeed($location = null)
  420. {
  421. if ($location === null) {
  422. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  423. throw new Zend_Gdata_App_InvalidArgumentException(
  424. 'Location must not be null');
  425. } else if ($location instanceof Zend_Gdata_Query) {
  426. $uri = $location->getQueryUrl();
  427. } else {
  428. $uri = $location;
  429. }
  430. return parent::getFeed($uri, 'Zend_Gdata_Gapps_OwnerFeed');
  431. }
  432. /**
  433. * Retreive EmailListFeed object containing multiple EmailListEntry
  434. * objects.
  435. *
  436. * @param mixed $location (optional) The location for the feed, as a URL
  437. * or Query.
  438. * @return Zend_Gdata_Gapps_EmailListFeed
  439. * @throws Zend_Gdata_App_Exception
  440. * @throws Zend_Gdata_App_HttpException
  441. * @throws Zend_Gdata_Gapps_ServiceException
  442. */
  443. public function getEmailListFeed($location = null)
  444. {
  445. if ($location === null) {
  446. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  447. } else if ($location instanceof Zend_Gdata_Query) {
  448. $uri = $location->getQueryUrl();
  449. } else {
  450. $uri = $location;
  451. }
  452. return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListFeed');
  453. }
  454. /**
  455. * Retreive EmailListRecipientFeed object containing multiple
  456. * EmailListRecipientEntry objects.
  457. *
  458. * @param mixed $location The location for the feed, as a URL or Query.
  459. * @return Zend_Gdata_Gapps_EmailListRecipientFeed
  460. * @throws Zend_Gdata_App_Exception
  461. * @throws Zend_Gdata_App_HttpException
  462. * @throws Zend_Gdata_Gapps_ServiceException
  463. */
  464. public function getEmailListRecipientFeed($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::getFeed($uri, 'Zend_Gdata_Gapps_EmailListRecipientFeed');
  476. }
  477. /**
  478. * Retreive a single UserEntry object.
  479. *
  480. * @param mixed $location The location for the feed, as a URL or Query.
  481. * @return Zend_Gdata_Gapps_UserEntry
  482. * @throws Zend_Gdata_App_Exception
  483. * @throws Zend_Gdata_App_HttpException
  484. * @throws Zend_Gdata_Gapps_ServiceException
  485. */
  486. public function getUserEntry($location)
  487. {
  488. if ($location === null) {
  489. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  490. throw new Zend_Gdata_App_InvalidArgumentException(
  491. 'Location must not be null');
  492. } else if ($location instanceof Zend_Gdata_Query) {
  493. $uri = $location->getQueryUrl();
  494. } else {
  495. $uri = $location;
  496. }
  497. return parent::getEntry($uri, 'Zend_Gdata_Gapps_UserEntry');
  498. }
  499. /**
  500. * Retreive a single NicknameEntry object.
  501. *
  502. * @param mixed $location The location for the feed, as a URL or Query.
  503. * @return Zend_Gdata_Gapps_NicknameEntry
  504. * @throws Zend_Gdata_App_Exception
  505. * @throws Zend_Gdata_App_HttpException
  506. * @throws Zend_Gdata_Gapps_ServiceException
  507. */
  508. public function getNicknameEntry($location)
  509. {
  510. if ($location === null) {
  511. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  512. throw new Zend_Gdata_App_InvalidArgumentException(
  513. 'Location must not be null');
  514. } else if ($location instanceof Zend_Gdata_Query) {
  515. $uri = $location->getQueryUrl();
  516. } else {
  517. $uri = $location;
  518. }
  519. return parent::getEntry($uri, 'Zend_Gdata_Gapps_NicknameEntry');
  520. }
  521. /**
  522. * Retreive a single GroupEntry object.
  523. *
  524. * @param mixed $location The location for the feed, as a URL or Query.
  525. * @return Zend_Gdata_Gapps_GroupEntry
  526. * @throws Zend_Gdata_App_Exception
  527. * @throws Zend_Gdata_App_HttpException
  528. * @throws Zend_Gdata_Gapps_ServiceException
  529. */
  530. public function getGroupEntry($location = null)
  531. {
  532. if ($location === null) {
  533. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  534. throw new Zend_Gdata_App_InvalidArgumentException(
  535. 'Location must not be null');
  536. } else if ($location instanceof Zend_Gdata_Query) {
  537. $uri = $location->getQueryUrl();
  538. } else {
  539. $uri = $location;
  540. }
  541. return parent::getEntry($uri, 'Zend_Gdata_Gapps_GroupEntry');
  542. }
  543. /**
  544. * Retreive a single MemberEntry object.
  545. *
  546. * @param mixed $location The location for the feed, as a URL or Query.
  547. * @return Zend_Gdata_Gapps_MemberEntry
  548. * @throws Zend_Gdata_App_Exception
  549. * @throws Zend_Gdata_App_HttpException
  550. * @throws Zend_Gdata_Gapps_ServiceException
  551. */
  552. public function getMemberEntry($location = null)
  553. {
  554. if ($location === null) {
  555. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  556. throw new Zend_Gdata_App_InvalidArgumentException(
  557. 'Location must not be null');
  558. } else if ($location instanceof Zend_Gdata_Query) {
  559. $uri = $location->getQueryUrl();
  560. } else {
  561. $uri = $location;
  562. }
  563. return parent::getEntry($uri, 'Zend_Gdata_Gapps_MemberEntry');
  564. }
  565. /**
  566. * Retreive a single OwnerEntry object.
  567. *
  568. * @param mixed $location The location for the feed, as a URL or Query.
  569. * @return Zend_Gdata_Gapps_OwnerEntry
  570. * @throws Zend_Gdata_App_Exception
  571. * @throws Zend_Gdata_App_HttpException
  572. * @throws Zend_Gdata_Gapps_ServiceException
  573. */
  574. public function getOwnerEntry($location = null)
  575. {
  576. if ($location === null) {
  577. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  578. throw new Zend_Gdata_App_InvalidArgumentException(
  579. 'Location must not be null');
  580. } else if ($location instanceof Zend_Gdata_Query) {
  581. $uri = $location->getQueryUrl();
  582. } else {
  583. $uri = $location;
  584. }
  585. return parent::getEntry($uri, 'Zend_Gdata_Gapps_OwnerEntry');
  586. }
  587. /**
  588. * Retreive a single EmailListEntry object.
  589. *
  590. * @param mixed $location The location for the feed, as a URL or Query.
  591. * @return Zend_Gdata_Gapps_EmailListEntry
  592. * @throws Zend_Gdata_App_Exception
  593. * @throws Zend_Gdata_App_HttpException
  594. * @throws Zend_Gdata_Gapps_ServiceException
  595. */
  596. public function getEmailListEntry($location)
  597. {
  598. if ($location === null) {
  599. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  600. throw new Zend_Gdata_App_InvalidArgumentException(
  601. 'Location must not be null');
  602. } else if ($location instanceof Zend_Gdata_Query) {
  603. $uri = $location->getQueryUrl();
  604. } else {
  605. $uri = $location;
  606. }
  607. return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListEntry');
  608. }
  609. /**
  610. * Retreive a single EmailListRecipientEntry object.
  611. *
  612. * @param mixed $location The location for the feed, as a URL or Query.
  613. * @return Zend_Gdata_Gapps_EmailListRecipientEntry
  614. * @throws Zend_Gdata_App_Exception
  615. * @throws Zend_Gdata_App_HttpException
  616. * @throws Zend_Gdata_Gapps_ServiceException
  617. */
  618. public function getEmailListRecipientEntry($location)
  619. {
  620. if ($location === null) {
  621. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  622. throw new Zend_Gdata_App_InvalidArgumentException(
  623. 'Location must not be null');
  624. } else if ($location instanceof Zend_Gdata_Query) {
  625. $uri = $location->getQueryUrl();
  626. } else {
  627. $uri = $location;
  628. }
  629. return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
  630. }
  631. /**
  632. * Create a new user from a UserEntry.
  633. *
  634. * @param Zend_Gdata_Gapps_UserEntry $user The user entry to insert.
  635. * @param string $uri (optional) The URI where the user should be
  636. * uploaded to. If null, the default user creation URI for
  637. * this domain will be used.
  638. * @return Zend_Gdata_Gapps_UserEntry The inserted user entry as
  639. * returned by the server.
  640. * @throws Zend_Gdata_App_Exception
  641. * @throws Zend_Gdata_App_HttpException
  642. * @throws Zend_Gdata_Gapps_ServiceException
  643. */
  644. public function insertUser($user, $uri = null)
  645. {
  646. if ($uri === null) {
  647. $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
  648. }
  649. $newEntry = $this->insertEntry($user, $uri, 'Zend_Gdata_Gapps_UserEntry');
  650. return $newEntry;
  651. }
  652. /**
  653. * Create a new nickname from a NicknameEntry.
  654. *
  655. * @param Zend_Gdata_Gapps_NicknameEntry $nickname The nickname entry to
  656. * insert.
  657. * @param string $uri (optional) The URI where the nickname should be
  658. * uploaded to. If null, the default nickname creation URI for
  659. * this domain will be used.
  660. * @return Zend_Gdata_Gapps_NicknameEntry The inserted nickname entry as
  661. * returned by the server.
  662. * @throws Zend_Gdata_App_Exception
  663. * @throws Zend_Gdata_App_HttpException
  664. * @throws Zend_Gdata_Gapps_ServiceException
  665. */
  666. public function insertNickname($nickname, $uri = null)
  667. {
  668. if ($uri === null) {
  669. $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
  670. }
  671. $newEntry = $this->insertEntry($nickname, $uri, 'Zend_Gdata_Gapps_NicknameEntry');
  672. return $newEntry;
  673. }
  674. /**
  675. * Create a new group from a GroupEntry.
  676. *
  677. * @param Zend_Gdata_Gapps_GroupEntry $group The group entry to insert.
  678. * @param string $uri (optional) The URI where the group should be
  679. * uploaded to. If null, the default user creation URI for
  680. * this domain will be used.
  681. * @return Zend_Gdata_Gapps_GroupEntry The inserted group entry as
  682. * returned by the server.
  683. * @throws Zend_Gdata_App_Exception
  684. * @throws Zend_Gdata_App_HttpException
  685. * @throws Zend_Gdata_Gapps_ServiceException
  686. */
  687. public function insertGroup($group, $uri = null)
  688. {
  689. if ($uri === null) {
  690. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  691. $uri .= $this->getDomain();
  692. }
  693. $newEntry = $this->insertEntry($group, $uri, 'Zend_Gdata_Gapps_GroupEntry');
  694. return $newEntry;
  695. }
  696. /**
  697. * Create a new member from a MemberEntry.
  698. *
  699. * @param Zend_Gdata_Gapps_MemberEntry $member The member entry to insert.
  700. * @param string $uri (optional) The URI where the group should be
  701. * uploaded to. If null, the default user creation URI for
  702. * this domain will be used.
  703. * @return Zend_Gdata_Gapps_MemberEntry The inserted member entry as
  704. * returned by the server.
  705. * @throws Zend_Gdata_App_Exception
  706. * @throws Zend_Gdata_App_HttpException
  707. * @throws Zend_Gdata_Gapps_ServiceException
  708. */
  709. public function insertMember($member, $uri = null)
  710. {
  711. if ($uri === null) {
  712. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  713. throw new Zend_Gdata_App_InvalidArgumentException(
  714. 'URI must not be null');
  715. }
  716. $newEntry = $this->insertEntry($member, $uri, 'Zend_Gdata_Gapps_MemberEntry');
  717. return $newEntry;
  718. }
  719. /**
  720. * Create a new group from a OwnerEntry.
  721. *
  722. * @param Zend_Gdata_Gapps_OwnerEntry $owner The owner entry to insert.
  723. * @param string $uri (optional) The URI where the owner should be
  724. * uploaded to. If null, the default user creation URI for
  725. * this domain will be used.
  726. * @return Zend_Gdata_Gapps_OwnerEntry The inserted owner entry as
  727. * returned by the server.
  728. * @throws Zend_Gdata_App_Exception
  729. * @throws Zend_Gdata_App_HttpException
  730. * @throws Zend_Gdata_Gapps_ServiceException
  731. */
  732. public function insertOwner($owner, $uri = null)
  733. {
  734. if ($uri === null) {
  735. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  736. throw new Zend_Gdata_App_InvalidArgumentException(
  737. 'URI must not be null');
  738. }
  739. $newEntry = $this->insertEntry($owner, $uri, 'Zend_Gdata_Gapps_OwnerEntry');
  740. return $newEntry;
  741. }
  742. /**
  743. * Create a new email list from an EmailListEntry.
  744. *
  745. * @param Zend_Gdata_Gapps_EmailListEntry $emailList The email list entry
  746. * to insert.
  747. * @param string $uri (optional) The URI where the email list should be
  748. * uploaded to. If null, the default email list creation URI for
  749. * this domain will be used.
  750. * @return Zend_Gdata_Gapps_EmailListEntry The inserted email list entry
  751. * as returned by the server.
  752. * @throws Zend_Gdata_App_Exception
  753. * @throws Zend_Gdata_App_HttpException
  754. * @throws Zend_Gdata_Gapps_ServiceException
  755. */
  756. public function insertEmailList($emailList, $uri = null)
  757. {
  758. if ($uri === null) {
  759. $uri = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH;
  760. }
  761. $newEntry = $this->insertEntry($emailList, $uri, 'Zend_Gdata_Gapps_EmailListEntry');
  762. return $newEntry;
  763. }
  764. /**
  765. * Create a new email list recipient from an EmailListRecipientEntry.
  766. *
  767. * @param Zend_Gdata_Gapps_EmailListRecipientEntry $recipient The recipient
  768. * entry to insert.
  769. * @param string $uri (optional) The URI where the recipient should be
  770. * uploaded to. If null, the default recipient creation URI for
  771. * this domain will be used.
  772. * @return Zend_Gdata_Gapps_EmailListRecipientEntry The inserted
  773. * recipient entry as returned by the server.
  774. * @throws Zend_Gdata_App_Exception
  775. * @throws Zend_Gdata_App_HttpException
  776. * @throws Zend_Gdata_Gapps_ServiceException
  777. */
  778. public function insertEmailListRecipient($recipient, $uri = null)
  779. {
  780. if ($uri === null) {
  781. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  782. throw new Zend_Gdata_App_InvalidArgumentException(
  783. 'URI must not be null');
  784. } elseif ($uri instanceof Zend_Gdata_Gapps_EmailListEntry) {
  785. $uri = $uri->getLink('edit')->href;
  786. }
  787. $newEntry = $this->insertEntry($recipient, $uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
  788. return $newEntry;
  789. }
  790. /**
  791. * Provides a magic factory method to instantiate new objects with
  792. * shorter syntax than would otherwise be required by the Zend Framework
  793. * naming conventions. For more information, see Zend_Gdata_App::__call().
  794. *
  795. * This overrides the default behavior of __call() so that query classes
  796. * do not need to have their domain manually set when created with
  797. * a magic factory method.
  798. *
  799. * @see Zend_Gdata_App::__call()
  800. * @param string $method The method name being called
  801. * @param array $args The arguments passed to the call
  802. * @throws Zend_Gdata_App_Exception
  803. */
  804. public function __call($method, $args) {
  805. if (preg_match('/^new(\w+Query)/', $method, $matches)) {
  806. $class = $matches[1];
  807. $foundClassName = null;
  808. foreach ($this->_registeredPackages as $name) {
  809. try {
  810. // Autoloading disabled on next line for compatibility
  811. // with magic factories. See ZF-6660.
  812. if (!class_exists($name . '_' . $class, false)) {
  813. require_once 'Zend/Loader.php';
  814. @Zend_Loader::loadClass($name . '_' . $class);
  815. }
  816. $foundClassName = $name . '_' . $class;
  817. break;
  818. } catch (Zend_Exception $e) {
  819. // package wasn't here- continue searching
  820. }
  821. }
  822. if ($foundClassName != null) {
  823. $reflectionObj = new ReflectionClass($foundClassName);
  824. // Prepend the domain to the query
  825. $args = array_merge(array($this->getDomain()), $args);
  826. return $reflectionObj->newInstanceArgs($args);
  827. } else {
  828. require_once 'Zend/Gdata/App/Exception.php';
  829. throw new Zend_Gdata_App_Exception(
  830. "Unable to find '${class}' in registered packages");
  831. }
  832. } else {
  833. return parent::__call($method, $args);
  834. }
  835. }
  836. // Convenience methods
  837. // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e
  838. /**
  839. * Create a new user entry and send it to the Google Apps servers.
  840. *
  841. * @param string $username The username for the new user.
  842. * @param string $givenName The given name for the new user.
  843. * @param string $familyName The family name for the new user.
  844. * @param string $password The password for the new user as a plaintext string
  845. * (if $passwordHashFunction is null) or a SHA-1 hashed
  846. * value (if $passwordHashFunction = 'SHA-1').
  847. * @param string $quotaLimitInMB (optional) The quota limit for the new user in MB.
  848. * @return Zend_Gdata_Gapps_UserEntry (optional) The new user entry as returned by
  849. * server.
  850. * @throws Zend_Gdata_App_Exception
  851. * @throws Zend_Gdata_App_HttpException
  852. * @throws Zend_Gdata_Gapps_ServiceException
  853. */
  854. public function createUser ($username, $givenName, $familyName, $password,
  855. $passwordHashFunction = null, $quotaLimitInMB = null) {
  856. $user = $this->newUserEntry();
  857. $user->login = $this->newLogin();
  858. $user->login->username = $username;
  859. $user->login->password = $password;
  860. $user->login->hashFunctionName = $passwordHashFunction;
  861. $user->name = $this->newName();
  862. $user->name->givenName = $givenName;
  863. $user->name->familyName = $familyName;
  864. if ($quotaLimitInMB !== null) {
  865. $user->quota = $this->newQuota();
  866. $user->quota->limit = $quotaLimitInMB;
  867. }
  868. return $this->insertUser($user);
  869. }
  870. /**
  871. * Retrieve a user based on their username.
  872. *
  873. * @param string $username The username to search for.
  874. * @return Zend_Gdata_Gapps_UserEntry The username to search for, or null
  875. * if no match found.
  876. * @throws Zend_Gdata_App_InvalidArgumentException
  877. * @throws Zend_Gdata_App_HttpException
  878. */
  879. public function retrieveUser ($username) {
  880. $query = $this->newUserQuery($username);
  881. try {
  882. $user = $this->getUserEntry($query);
  883. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  884. // Set the user to null if not found
  885. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  886. $user = null;
  887. } else {
  888. throw $e;
  889. }
  890. }
  891. return $user;
  892. }
  893. /**
  894. * Retrieve a page of users in alphabetical order, starting with the
  895. * provided username.
  896. *
  897. * @param string $startUsername (optional) The first username to retrieve.
  898. * If null or not declared, the page will begin with the first
  899. * user in the domain.
  900. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  901. * objects representing all users in the domain.
  902. * @throws Zend_Gdata_App_Exception
  903. * @throws Zend_Gdata_App_HttpException
  904. * @throws Zend_Gdata_Gapps_ServiceException
  905. */
  906. public function retrievePageOfUsers ($startUsername = null) {
  907. $query = $this->newUserQuery();
  908. $query->setStartUsername($startUsername);
  909. return $this->getUserFeed($query);
  910. }
  911. /**
  912. * Retrieve all users in the current domain. Be aware that
  913. * calling this function on a domain with many users will take a
  914. * signifigant amount of time to complete. On larger domains this may
  915. * may cause execution to timeout without proper precautions in place.
  916. *
  917. * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
  918. * objects representing all users 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 retrieveAllUsers () {
  924. return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers());
  925. }
  926. /**
  927. * Overwrite a specified username with the provided UserEntry. The
  928. * UserEntry does not need to contain an edit link.
  929. *
  930. * This method is provided for compliance with the Google Apps
  931. * Provisioning API specification. Normally users will instead want to
  932. * call UserEntry::save() instead.
  933. *
  934. * @see Zend_Gdata_App_Entry::save
  935. * @param string $username The username whose data will be overwritten.
  936. * @param Zend_Gdata_Gapps_UserEntry $userEntry The user entry which
  937. * will be overwritten.
  938. * @return Zend_Gdata_Gapps_UserEntry The UserEntry returned by the
  939. * server.
  940. * @throws Zend_Gdata_App_Exception
  941. * @throws Zend_Gdata_App_HttpException
  942. * @throws Zend_Gdata_Gapps_ServiceException
  943. */
  944. public function updateUser($username, $userEntry) {
  945. return $this->updateEntry($userEntry, $this->getBaseUrl() .
  946. self::APPS_USER_PATH . '/' . $username);
  947. }
  948. /**
  949. * Mark a given user as suspended.
  950. *
  951. * @param string $username The username associated with the user who
  952. * should be suspended.
  953. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  954. * user.
  955. * @throws Zend_Gdata_App_Exception
  956. * @throws Zend_Gdata_App_HttpException
  957. * @throws Zend_Gdata_Gapps_ServiceException
  958. */
  959. public function suspendUser($username) {
  960. $user = $this->retrieveUser($username);
  961. $user->login->suspended = true;
  962. return $user->save();
  963. }
  964. /**
  965. * Mark a given user as not suspended.
  966. *
  967. * @param string $username The username associated with the user who
  968. * should be restored.
  969. * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
  970. * user.
  971. * @throws Zend_Gdata_App_Exception
  972. * @throws Zend_Gdata_App_HttpException
  973. * @throws Zend_Gdata_Gapps_ServiceException
  974. */
  975. public function restoreUser($username) {
  976. $user = $this->retrieveUser($username);
  977. $user->login->suspended = false;
  978. return $user->save();
  979. }
  980. /**
  981. * Delete a user by username.
  982. *
  983. * @param string $username The username associated with the user who
  984. * should be deleted.
  985. * @throws Zend_Gdata_App_Exception
  986. * @throws Zend_Gdata_App_HttpException
  987. * @throws Zend_Gdata_Gapps_ServiceException
  988. */
  989. public function deleteUser($username) {
  990. $this->delete($this->getBaseUrl() . self::APPS_USER_PATH . '/' .
  991. $username);
  992. }
  993. /**
  994. * Create a nickname for a given user.
  995. *
  996. * @param string $username The username to which the new nickname should
  997. * be associated.
  998. * @param string $nickname The new nickname to be created.
  999. * @return Zend_Gdata_Gapps_NicknameEntry The nickname entry which was
  1000. * created by the server.
  1001. * @throws Zend_Gdata_App_Exception
  1002. * @throws Zend_Gdata_App_HttpException
  1003. * @throws Zend_Gdata_Gapps_ServiceException
  1004. */
  1005. public function createNickname($username, $nickname) {
  1006. $entry = $this->newNicknameEntry();
  1007. $nickname = $this->newNickname($nickname);
  1008. $login = $this->newLogin($username);
  1009. $entry->nickname = $nickname;
  1010. $entry->login = $login;
  1011. return $this->insertNickname($entry);
  1012. }
  1013. /**
  1014. * Retrieve the entry for a specified nickname.
  1015. *
  1016. * @param string $nickname The nickname to be retrieved.
  1017. * @return Zend_Gdata_Gapps_NicknameEntry The requested nickname entry.
  1018. * @throws Zend_Gdata_App_Exception
  1019. * @throws Zend_Gdata_App_HttpException
  1020. * @throws Zend_Gdata_Gapps_ServiceException
  1021. */
  1022. public function retrieveNickname($nickname) {
  1023. $query = $this->newNicknameQuery();
  1024. $query->setNickname($nickname);
  1025. try {
  1026. $nickname = $this->getNicknameEntry($query);
  1027. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  1028. // Set the nickname to null if not found
  1029. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  1030. $nickname = null;
  1031. } else {
  1032. throw $e;
  1033. }
  1034. }
  1035. return $nickname;
  1036. }
  1037. /**
  1038. * Retrieve all nicknames associated with a specific username.
  1039. *
  1040. * @param string $username The username whose nicknames should be
  1041. * returned.
  1042. * @return Zend_Gdata_Gapps_NicknameFeed A feed containing all nicknames
  1043. * for the given user, or null if
  1044. * @throws Zend_Gdata_App_Exception
  1045. * @throws Zend_Gdata_App_HttpException
  1046. * @throws Zend_Gdata_Gapps_ServiceException
  1047. */
  1048. public function retrieveNicknames($username) {
  1049. $query = $this->newNicknameQuery();
  1050. $query->setUsername($username);
  1051. $nicknameFeed = $this->retrieveAllEntriesForFeed(
  1052. $this->getNicknameFeed($query));
  1053. return $nicknameFeed;
  1054. }
  1055. /**
  1056. * Retrieve a page of nicknames in alphabetical order, starting with the
  1057. * provided nickname.
  1058. *
  1059. * @param string $startNickname (optional) The first nickname to
  1060. * retrieve. If null or not declared, the page will begin with
  1061. * the first nickname in the domain.
  1062. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  1063. * objects representing all nicknames in the domain.
  1064. * @throws Zend_Gdata_App_Exception
  1065. * @throws Zend_Gdata_App_HttpException
  1066. * @throws Zend_Gdata_Gapps_ServiceException
  1067. */
  1068. public function retrievePageOfNicknames ($startNickname = null) {
  1069. $query = $this->newNicknameQuery();
  1070. $query->setStartNickname($startNickname);
  1071. return $this->getNicknameFeed($query);
  1072. }
  1073. /**
  1074. * Retrieve all nicknames in the current domain. Be aware that
  1075. * calling this function on a domain with many nicknames will take a
  1076. * signifigant amount of time to complete. On larger domains this may
  1077. * may cause execution to timeout without proper precautions in place.
  1078. *
  1079. * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
  1080. * objects representing all nicknames in the domain.
  1081. * @throws Zend_Gdata_App_Exception
  1082. * @throws Zend_Gdata_App_HttpException
  1083. * @throws Zend_Gdata_Gapps_ServiceException
  1084. */
  1085. public function retrieveAllNicknames () {
  1086. return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames());
  1087. }
  1088. /**
  1089. * Delete a specified nickname.
  1090. *
  1091. * @param string $nickname The name of the nickname to be deleted.
  1092. * @throws Zend_Gdata_App_Exception
  1093. * @throws Zend_Gdata_App_HttpException
  1094. * @throws Zend_Gdata_Gapps_ServiceException
  1095. */
  1096. public function deleteNickname($nickname) {
  1097. $this->delete($this->getBaseUrl() . self::APPS_NICKNAME_PATH . '/' . $nickname);
  1098. }
  1099. /**
  1100. * Create a new group.
  1101. *
  1102. * @param string $groupId A unique identifier for the group
  1103. * @param string $groupName The name of the group
  1104. * @param string $description A description of the group
  1105. * @param string $emailPermission The subscription permission of the group
  1106. * @return Zend_Gdata_Gapps_GroupEntry The group entry as created on the server.
  1107. */
  1108. public function createGroup($groupId, $groupName, $description = null, $emailPermission = null)
  1109. {
  1110. $i = 0;
  1111. $group = $this->newGroupEntry();
  1112. $properties[$i] = $this->newProperty();
  1113. $properties[$i]->name = 'groupId';
  1114. $properties[$i]->value = $groupId;
  1115. $i++;
  1116. $properties[$i] = $this->newProperty();
  1117. $properties[$i]->name = 'groupName';
  1118. $properties[$i]->value = $groupName;
  1119. $i++;
  1120. if($description != null) {
  1121. $properties[$i] = $this->newProperty();
  1122. $properties[$i]->name = 'description';
  1123. $properties[$i]->value = $description;
  1124. $i++;
  1125. }
  1126. if($emailPermission != null) {
  1127. $properties[$i] = $this->newProperty();
  1128. $properties[$i]->name = 'emailPermission';
  1129. $properties[$i]->value = $emailPermission;
  1130. $i++;
  1131. }
  1132. $group->property = $properties;
  1133. return $this->insertGroup($group);
  1134. }
  1135. /**
  1136. * Retrieves a group based on group id
  1137. *
  1138. * @param string $groupId The unique identifier for the group
  1139. * @return Zend_Gdata_Gapps_GroupEntry The group entry as returned by the server.
  1140. */
  1141. public function retrieveGroup($groupId)
  1142. {
  1143. $query = $this->newGroupQuery($groupId);
  1144. //$query->setGroupId($groupId);
  1145. try {
  1146. $group = $this->getGroupEntry($query);
  1147. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  1148. // Set the group to null if not found
  1149. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
  1150. $group = null;
  1151. } else {
  1152. throw $e;
  1153. }
  1154. }
  1155. return $group;
  1156. }
  1157. /**
  1158. * Retrieve all groups in the current domain. Be aware that
  1159. * calling this function on a domain with many groups will take a
  1160. * signifigant amount of time to complete. On larger domains this may
  1161. * may cause execution to timeout without proper precautions in place.
  1162. *
  1163. * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry objects
  1164. * representing all groups apart of the domain.
  1165. */
  1166. public function retrieveAllGroups()
  1167. {
  1168. return $this->retrieveAllEntriesForFeed($this->retrievePageOfGroups());
  1169. }
  1170. /**
  1171. * Delete a group
  1172. *
  1173. * @param string $groupId The unique identifier for the group
  1174. */
  1175. public function deleteGroup($groupId)
  1176. {
  1177. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1178. $uri .= $this->getDomain() . '/' . $groupId;
  1179. $this->delete($uri);
  1180. }
  1181. /**
  1182. * Check to see if a member id or group id is a member of group
  1183. *
  1184. * @param string $memberId Member id or group group id
  1185. * @param string $groupId Group to be checked for
  1186. * @return bool True, if given entity is a member
  1187. */
  1188. public function isMember($memberId, $groupId)
  1189. {
  1190. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1191. $uri .= $this->getDomain() . '/' . $groupId . '/member/' . $memberId;
  1192. //if the enitiy is not a member, an exception is thrown
  1193. try {
  1194. $results = $this->get($uri);
  1195. } catch (Exception $e) {
  1196. $results = false;
  1197. }
  1198. if($results) {
  1199. return TRUE;
  1200. } else {
  1201. return FALSE;
  1202. }
  1203. }
  1204. /**
  1205. * Add an email address to a group as a member
  1206. *
  1207. * @param string $recipientAddress Email address, member id, or group id
  1208. * @param string $groupId The unique id of the group
  1209. * @return Zend_Gdata_Gapps_MemberEntry The member entry returned by the server
  1210. */
  1211. public function addMemberToGroup($recipientAddress, $groupId)
  1212. {
  1213. $member = $this->newMemberEntry();
  1214. $properties[] = $this->newProperty();
  1215. $properties[0]->name = 'memberId';
  1216. $properties[0]->value = $recipientAddress;
  1217. $member->property = $properties;
  1218. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1219. $uri .= $this->getDomain() . '/' . $groupId . '/member';
  1220. return $this->insertMember($member, $uri);
  1221. }
  1222. /**
  1223. * Remove a member id from a group
  1224. *
  1225. * @param string $memberId Member id or group id
  1226. * @param string $groupId The unique id of the group
  1227. */
  1228. public function removeMemberFromGroup($memberId, $groupId)
  1229. {
  1230. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1231. $uri .= $this->getDomain() . '/' . $groupId . '/member/' . $memberId;
  1232. return $this->delete($uri);
  1233. }
  1234. /**
  1235. * Retrieves all the members of a group
  1236. *
  1237. * @param string $groupId The unique id of the group
  1238. * @return Zend_Gdata_Gapps_MemberFeed Collection of MemberEntry objects
  1239. * representing all members apart of the group.
  1240. */
  1241. public function retrieveAllMembers($groupId)
  1242. {
  1243. return $this->retrieveAllEntriesForFeed(
  1244. $this->retrievePageOfMembers($groupId));
  1245. }
  1246. /**
  1247. * Add an email as an owner of a group
  1248. *
  1249. * @param string $email Owner's email
  1250. * @param string $groupId Group ownership to be checked for
  1251. * @return Zend_Gdata_Gapps_OwnerEntry The OwnerEntry returned by the server
  1252. */
  1253. public function addOwnerToGroup($email, $groupId)
  1254. {
  1255. $owner = $this->newOwnerEntry();
  1256. $properties[] = $this->newProperty();
  1257. $properties[0]->name = 'email';
  1258. $properties[0]->value = $email;
  1259. $owner->property = $properties;
  1260. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1261. $uri .= $this->getDomain() . '/' . $groupId . '/owner';
  1262. return $this->insertOwner($owner, $uri);
  1263. }
  1264. /**
  1265. * Retrieves all the owners of a group
  1266. *
  1267. * @param string $groupId The unique identifier for the group
  1268. * @return Zend_Gdata_Gapps_OwnerFeed Collection of Zend_Gdata_OwnerEntry
  1269. * objects representing all owners apart of the group.
  1270. */
  1271. public function retrieveGroupOwners($groupId)
  1272. {
  1273. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1274. $uri .= $this->getDomain() . '/' . $groupId . '/owner';
  1275. return $this->getOwnerFeed($uri);
  1276. }
  1277. /**
  1278. * Checks to see if an email is an owner of a group
  1279. *
  1280. * @param string $email Owner's email
  1281. * @param string $groupId Group ownership to be checked for
  1282. * @return bool True, if given entity is an owner
  1283. */
  1284. public function isOwner($email, $groupId)
  1285. {
  1286. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1287. $uri .= $this->getDomain() . '/' . $groupId . '/owner/' . $email;
  1288. //if the enitiy is not an owner of the group, an exception is thrown
  1289. try {
  1290. $results = $this->get($uri);
  1291. } catch (Exception $e) {
  1292. $results = false;
  1293. }
  1294. if($results) {
  1295. return TRUE;
  1296. } else {
  1297. return FALSE;
  1298. }
  1299. }
  1300. /**
  1301. * Remove email as an owner of a group
  1302. *
  1303. * @param string $email Owner's email
  1304. * @param string $groupId The unique identifier for the group
  1305. */
  1306. public function removeOwnerFromGroup($email, $groupId)
  1307. {
  1308. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1309. $uri .= $this->getDomain() . '/' . $groupId . '/owner/' . $email;
  1310. return $this->delete($uri);
  1311. }
  1312. /**
  1313. * Update group properties with new values. any property not defined will not
  1314. * be updated
  1315. *
  1316. * @param string $groupId A unique identifier for the group
  1317. * @param string $groupName The name of the group
  1318. * @param string $description A description of the group
  1319. * @param string $emailPermission The subscription permission of the group
  1320. * @return Zend_Gdata_Gapps_GroupEntry The group entry as updated on the server.
  1321. */
  1322. public function updateGroup($groupId, $groupName = null, $description = null,
  1323. $emailPermission = null)
  1324. {
  1325. $i = 0;
  1326. $group = $this->newGroupEntry();
  1327. $properties[$i] = $this->newProperty();
  1328. $properties[$i]->name = 'groupId';
  1329. $properties[$i]->value = $groupId;
  1330. $i++;
  1331. if($groupName != null) {
  1332. $properties[$i] = $this->newProperty();
  1333. $properties[$i]->name = 'groupName';
  1334. $properties[$i]->value = $groupName;
  1335. $i++;
  1336. }
  1337. if($description != null) {
  1338. $properties[$i] = $this->newProperty();
  1339. $properties[$i]->name = 'description';
  1340. $properties[$i]->value = $description;
  1341. $i++;
  1342. }
  1343. if($emailPermission != null) {
  1344. $properties[$i] = $this->newProperty();
  1345. $properties[$i]->name = 'emailPermission';
  1346. $properties[$i]->value = $emailPermission;
  1347. $i++;
  1348. }
  1349. $group->property = $properties;
  1350. $uri = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
  1351. $uri .= $this->getDomain() . '/' . $groupId;
  1352. return $this->updateEntry($group, $uri, 'Zend_Gdata_Gapps_GroupEntry');
  1353. }
  1354. /**
  1355. * Retrieve all of the groups that a user is a member of
  1356. *
  1357. * @param string $memberId Member username
  1358. * @param bool $directOnly (Optional) If true, members with direct association
  1359. * only will be considered
  1360. * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry
  1361. * objects representing all groups member is apart of in the domain.
  1362. */
  1363. public function retrieveGroups($memberId, $directOnly = null)
  1364. {
  1365. $query = $this->newGroupQuery();
  1366. $query->setMember($memberId);
  1367. if($directOnly != null) {
  1368. $query->setDirectOnly($directOnly);
  1369. }
  1370. return $this->getGroupFeed($query);
  1371. }
  1372. /**
  1373. * Retrieve a page of groups in alphabetical order, starting with the
  1374. * provided group.
  1375. *
  1376. * @param string $startGroup (optional) The first group to
  1377. * retrieve. If null or not defined, the page will begin
  1378. * with the first group in the domain.
  1379. * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry
  1380. * objects representing the groups in the domain.
  1381. * @throws Zend_Gdata_App_Exception
  1382. * @throws Zend_Gdata_App_HttpException
  1383. * @throws Zend_Gdata_Gapps_ServiceException
  1384. */
  1385. public function retrievePageOfGroups ($startGroup = null)
  1386. {
  1387. $query = $this->newGroupQuery();
  1388. $query->setStartGroupId($startGroup);
  1389. return $this->getGroupFeed($query);
  1390. }
  1391. /**
  1392. * Gets page of Members
  1393. *
  1394. * @param string $groupId The group id which should be searched.
  1395. * @param string $startMember (optinal) The address of the first member,
  1396. * or null to start with the first member in the list.
  1397. * @return Zend_Gdata_Gapps_MemberFeed Collection of Zend_Gdata_MemberEntry
  1398. * objects
  1399. */
  1400. public function retrievePageOfMembers($groupId, $startMember = null)
  1401. {
  1402. $query = $this->newMemberQuery($groupId);
  1403. $query->setStartMemberId($startMember);
  1404. return $this->getMemberFeed($query);
  1405. }
  1406. /**
  1407. * Create a new email list.
  1408. *
  1409. * @param string $emailList The name of the email list to be created.
  1410. * @return Zend_Gdata_Gapps_EmailListEntry The email list entry
  1411. * as created on the server.
  1412. * @throws Zend_Gdata_App_Exception
  1413. * @throws Zend_Gdata_App_HttpException
  1414. * @throws Zend_Gdata_Gapps_ServiceException
  1415. */
  1416. public function createEmailList($emailList) {
  1417. $entry = $this->newEmailListEntry();
  1418. $list = $this->newEmailList();
  1419. $list->name = $emailList;
  1420. $entry->emailList = $list;
  1421. return $this->insertEmailList($entry);
  1422. }
  1423. /**
  1424. * Retrieve all email lists associated with a recipient.
  1425. *
  1426. * @param string $username The recipient whose associated email lists
  1427. * should be returned.
  1428. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found as
  1429. * Zend_Gdata_EmailListEntry objects.
  1430. * @throws Zend_Gdata_App_Exception
  1431. * @throws Zend_Gdata_App_HttpException
  1432. * @throws Zend_Gdata_Gapps_ServiceException
  1433. */
  1434. public function retrieveEmailLists($recipient) {
  1435. $query = $this->newEmailListQuery();
  1436. $query->recipient = $recipient;
  1437. return $this->getEmailListFeed($query);
  1438. }
  1439. /**
  1440. * Retrieve a page of email lists in alphabetical order, starting with the
  1441. * provided email list.
  1442. *
  1443. * @param string $startEmailListName (optional) The first list to
  1444. * retrieve. If null or not defined, the page will begin
  1445. * with the first email list in the domain.
  1446. * @return Zend_Gdata_Gapps_EmailListFeed Collection of Zend_Gdata_EmailListEntry
  1447. * objects representing all nicknames in the domain.
  1448. * @throws Zend_Gdata_App_Exception
  1449. * @throws Zend_Gdata_App_HttpException
  1450. * @throws Zend_Gdata_Gapps_ServiceException
  1451. */
  1452. public function retrievePageOfEmailLists ($startNickname = null) {
  1453. $query = $this->newEmailListQuery();
  1454. $query->setStartEmailListName($startNickname);
  1455. return $this->getEmailListFeed($query);
  1456. }
  1457. /**
  1458. * Retrieve all email lists associated with the curent domain. Be aware that
  1459. * calling this function on a domain with many email lists will take a
  1460. * signifigant amount of time to complete. On larger domains this may
  1461. * may cause execution to timeout without proper precautions in place.
  1462. *
  1463. * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found
  1464. * as Zend_Gdata_Gapps_EmailListEntry objects.
  1465. * @throws Zend_Gdata_App_Exception
  1466. * @throws Zend_Gdata_App_HttpException
  1467. * @throws Zend_Gdata_Gapps_ServiceException
  1468. */
  1469. public function retrieveAllEmailLists() {
  1470. return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists());
  1471. }
  1472. /**
  1473. * Delete a specified email list.
  1474. *
  1475. * @param string $emailList The name of the emailList to be deleted.
  1476. * @throws Zend_Gdata_App_Exception
  1477. * @throws Zend_Gdata_App_HttpException
  1478. * @throws Zend_Gdata_Gapps_ServiceException
  1479. */
  1480. public function deleteEmailList($emailList) {
  1481. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  1482. . $emailList);
  1483. }
  1484. /**
  1485. * Add a specified recipient to an existing emailList.
  1486. *
  1487. * @param string $recipientAddress The address of the recipient to be
  1488. * added to the email list.
  1489. * @param string $emailList The name of the email address to which the
  1490. * recipient should be added.
  1491. * @return Zend_Gdata_Gapps_EmailListRecipientEntry The recipient entry
  1492. * created by the server.
  1493. * @throws Zend_Gdata_App_Exception
  1494. * @throws Zend_Gdata_App_HttpException
  1495. * @throws Zend_Gdata_Gapps_ServiceException
  1496. */
  1497. public function addRecipientToEmailList($recipientAddress, $emailList) {
  1498. $entry = $this->newEmailListRecipientEntry();
  1499. $who = $this->newWho();
  1500. $who->email = $recipientAddress;
  1501. $entry->who = $who;
  1502. $address = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/' .
  1503. $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/';
  1504. return $this->insertEmailListRecipient($entry, $address);
  1505. }
  1506. /**
  1507. * Retrieve a page of email list recipients in alphabetical order,
  1508. * starting with the provided email list recipient.
  1509. *
  1510. * @param string $emaiList The email list which should be searched.
  1511. * @param string $startRecipient (optinal) The address of the first
  1512. * recipient, or null to start with the first recipient in
  1513. * the list.
  1514. * @return Zend_Gdata_Gapps_EmailListRecipientFeed Collection of
  1515. * Zend_Gdata_EmailListRecipientEntry objects representing all
  1516. * recpients in the specified list.
  1517. * @throws Zend_Gdata_App_Exception
  1518. * @throws Zend_Gdata_App_HttpException
  1519. * @throws Zend_Gdata_Gapps_ServiceException
  1520. */
  1521. public function retrievePageOfRecipients ($emailList,
  1522. $startRecipient = null) {
  1523. $query = $this->newEmailListRecipientQuery();
  1524. $query->setEmailListName($emailList);
  1525. $query->setStartRecipient($startRecipient);
  1526. return $this->getEmailListRecipientFeed($query);
  1527. }
  1528. /**
  1529. * Retrieve all recipients associated with an email list. Be aware that
  1530. * calling this function on a domain with many email lists will take a
  1531. * signifigant amount of time to complete. On larger domains this may
  1532. * may cause execution to timeout without proper precautions in place.
  1533. *
  1534. * @param string $emaiList The email list which should be searched.
  1535. * @return Zend_Gdata_Gapps_EmailListRecipientFeed The list of email lists
  1536. * found as Zend_Gdata_Gapps_EmailListRecipientEntry objects.
  1537. * @throws Zend_Gdata_App_Exception
  1538. * @throws Zend_Gdata_App_HttpException
  1539. * @throws Zend_Gdata_Gapps_ServiceException
  1540. */
  1541. public function retrieveAllRecipients($emailList) {
  1542. return $this->retrieveAllEntriesForFeed(
  1543. $this->retrievePageOfRecipients($emailList));
  1544. }
  1545. /**
  1546. * Remove a specified recipient from an email list.
  1547. *
  1548. * @param string $recipientAddress The recipient to be removed.
  1549. * @param string $emailList The list from which the recipient should
  1550. * be removed.
  1551. * @throws Zend_Gdata_App_Exception
  1552. * @throws Zend_Gdata_App_HttpException
  1553. * @throws Zend_Gdata_Gapps_ServiceException
  1554. */
  1555. public function removeRecipientFromEmailList($recipientAddress, $emailList) {
  1556. $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
  1557. . $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/'
  1558. . $recipientAddress);
  1559. }
  1560. }