File indexing completed on 2024-12-22 05:36:46

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage Gapps
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 /**
0025  * @see Zend_Gdata
0026  */
0027 // require_once 'Zend/Gdata.php';
0028 
0029 /**
0030  * @see Zend_Gdata_Gapps_UserFeed
0031  */
0032 // require_once 'Zend/Gdata/Gapps/UserFeed.php';
0033 
0034 /**
0035  * @see Zend_Gdata_Gapps_NicknameFeed
0036  */
0037 // require_once 'Zend/Gdata/Gapps/NicknameFeed.php';
0038 
0039 /**
0040  * @see Zend_Gdata_Gapps_GroupFeed
0041  */
0042 // require_once 'Zend/Gdata/Gapps/GroupFeed.php';
0043 
0044 /**
0045  * @see Zend_Gdata_Gapps_MemberFeed
0046  */
0047 // require_once 'Zend/Gdata/Gapps/MemberFeed.php';
0048 
0049 /**
0050  * @see Zend_Gdata_Gapps_OwnerFeed
0051  */
0052 // require_once 'Zend/Gdata/Gapps/OwnerFeed.php';
0053 
0054 /**
0055  * @see Zend_Gdata_Gapps_EmailListFeed
0056  */
0057 // require_once 'Zend/Gdata/Gapps/EmailListFeed.php';
0058 
0059 /**
0060  * @see Zend_Gdata_Gapps_EmailListRecipientFeed
0061  */
0062 // require_once 'Zend/Gdata/Gapps/EmailListRecipientFeed.php';
0063 
0064 
0065 /**
0066  * Service class for interacting with the Google Apps Provisioning API.
0067  *
0068  * Like other service classes in this module, this class provides access via
0069  * an HTTP client to Google servers for working with entries and feeds.
0070  *
0071  * Because of the nature of this API, all access must occur over an
0072  * authenticated connection.
0073  *
0074  * @link http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html
0075  *
0076  * @category   Zend
0077  * @package    Zend_Gdata
0078  * @subpackage Gapps
0079  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0080  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0081  */
0082 class Zend_Gdata_Gapps extends Zend_Gdata
0083 {
0084 
0085     const APPS_BASE_FEED_URI = 'https://apps-apis.google.com/a/feeds';
0086     const AUTH_SERVICE_NAME = 'apps';
0087 
0088     /**
0089      * Path to user feeds on the Google Apps server.
0090      */
0091     const APPS_USER_PATH = '/user/2.0';
0092 
0093     /**
0094      * Path to nickname feeds on the Google Apps server.
0095      */
0096     const APPS_NICKNAME_PATH = '/nickname/2.0';
0097 
0098     /**
0099      * Path to group feeds on the Google Apps server.
0100      */
0101     const APPS_GROUP_PATH = '/group/2.0';
0102 
0103     /**
0104      * Path to email list feeds on the Google Apps server.
0105      */
0106     const APPS_EMAIL_LIST_PATH = '/emailList/2.0';
0107 
0108     /**
0109      * Path to email list recipient feeds on the Google Apps server.
0110      */
0111     const APPS_EMAIL_LIST_RECIPIENT_POSTFIX = '/recipient';
0112 
0113     /**
0114      * The domain which is being administered via the Provisioning API.
0115      *
0116      * @var string
0117      */
0118     protected $_domain = null;
0119 
0120     /**
0121      * Namespaces used for Zend_Gdata_Gapps
0122      *
0123      * @var array
0124      */
0125     public static $namespaces = array(
0126         array('apps', 'http://schemas.google.com/apps/2006', 1, 0)
0127     );
0128 
0129     /**
0130      * Create Gdata_Gapps object
0131      *
0132      * @param Zend_Http_Client $client (optional) The HTTP client to use when
0133      *          when communicating with the Google Apps servers.
0134      * @param string $domain (optional) The Google Apps domain which is to be
0135      *          accessed.
0136      * @param string $applicationId The identity of the app in the form of Company-AppName-Version
0137      */
0138     public function __construct($client = null, $domain = null, $applicationId = 'MyCompany-MyApp-1.0')
0139     {
0140         $this->registerPackage('Zend_Gdata_Gapps');
0141         $this->registerPackage('Zend_Gdata_Gapps_Extension');
0142         parent::__construct($client, $applicationId);
0143         $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
0144         $this->_domain = $domain;
0145     }
0146 
0147     /**
0148      * Convert an exception to an ServiceException if an AppsForYourDomain
0149      * XML document is contained within the original exception's HTTP
0150      * response. If conversion fails, throw the original error.
0151      *
0152      * @param Zend_Gdata_Exception $e The exception to convert.
0153      * @throws Zend_Gdata_Gapps_ServiceException
0154      * @throws mixed
0155      */
0156     public static function throwServiceExceptionIfDetected($e) {
0157         // Check to make sure that there actually response!
0158         // This can happen if the connection dies before the request
0159         // completes. (See ZF-5949)
0160         $response = $e->getResponse();
0161         if (!$response) {
0162           // require_once('Zend/Gdata/App/IOException.php');
0163           throw new Zend_Gdata_App_IOException('No HTTP response received (possible connection failure)');
0164         }
0165 
0166         try {
0167             // Check to see if there is an AppsForYourDomainErrors
0168             // datastructure in the response. If so, convert it to
0169             // an exception and throw it.
0170             // require_once 'Zend/Gdata/Gapps/ServiceException.php';
0171             $error = new Zend_Gdata_Gapps_ServiceException();
0172             $error->importFromString($response->getBody());
0173             throw $error;
0174         } catch (Zend_Gdata_App_Exception $e2) {
0175             // Unable to convert the response to a ServiceException,
0176             // most likely because the server didn't return an
0177             // AppsForYourDomainErrors document. Throw the original
0178             // exception.
0179             throw $e;
0180         }
0181     }
0182 
0183     /**
0184      * Imports a feed located at $uri.
0185      * This method overrides the default behavior of Zend_Gdata_App,
0186      * providing support for Zend_Gdata_Gapps_ServiceException.
0187      *
0188      * @param  string $uri
0189      * @param  Zend_Http_Client $client (optional) The client used for
0190      *          communication
0191      * @param  string $className (optional) The class which is used as the
0192      *          return type
0193      * @throws Zend_Gdata_App_Exception
0194      * @throws Zend_Gdata_App_HttpException
0195      * @throws Zend_Gdata_Gapps_ServiceException
0196      * @return Zend_Gdata_App_Feed
0197      */
0198     public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed', $useObjectMapping = true)
0199     {
0200         try {
0201             return parent::import($uri, $client, $className, $useObjectMapping);
0202         } catch (Zend_Gdata_App_HttpException $e) {
0203             self::throwServiceExceptionIfDetected($e);
0204         }
0205     }
0206 
0207     /**
0208      * GET a URI using client object.
0209      * This method overrides the default behavior of Zend_Gdata_App,
0210      * providing support for Zend_Gdata_Gapps_ServiceException.
0211      *
0212      * @param string $uri GET URI
0213      * @param array $extraHeaders Extra headers to add to the request, as an
0214      *        array of string-based key/value pairs.
0215      * @throws Zend_Gdata_App_HttpException
0216      * @throws Zend_Gdata_Gapps_ServiceException
0217      * @return Zend_Http_Response
0218      */
0219     public function get($uri, $extraHeaders = array())
0220     {
0221         try {
0222             return parent::get($uri, $extraHeaders);
0223         } catch (Zend_Gdata_App_HttpException $e) {
0224             self::throwServiceExceptionIfDetected($e);
0225         }
0226     }
0227 
0228     /**
0229      * POST data with client object.
0230      * This method overrides the default behavior of Zend_Gdata_App,
0231      * providing support for Zend_Gdata_Gapps_ServiceException.
0232      *
0233      * @param mixed $data The Zend_Gdata_App_Entry or XML to post
0234      * @param string $uri (optional) POST URI
0235      * @param integer $remainingRedirects (optional)
0236      * @param string $contentType Content-type of the data
0237      * @param array $extraHaders Extra headers to add tot he request
0238      * @return Zend_Http_Response
0239      * @throws Zend_Gdata_App_HttpException
0240      * @throws Zend_Gdata_App_InvalidArgumentException
0241      * @throws Zend_Gdata_Gapps_ServiceException
0242      */
0243     public function post($data, $uri = null, $remainingRedirects = null,
0244             $contentType = null, $extraHeaders = null)
0245     {
0246         try {
0247             return parent::post($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
0248         } catch (Zend_Gdata_App_HttpException $e) {
0249             self::throwServiceExceptionIfDetected($e);
0250         }
0251     }
0252 
0253     /**
0254      * PUT data with client object
0255      * This method overrides the default behavior of Zend_Gdata_App,
0256      * providing support for Zend_Gdata_Gapps_ServiceException.
0257      *
0258      * @param mixed $data The Zend_Gdata_App_Entry or XML to post
0259      * @param string $uri (optional) PUT URI
0260      * @param integer $remainingRedirects (optional)
0261      * @param string $contentType Content-type of the data
0262      * @param array $extraHaders Extra headers to add tot he request
0263      * @return Zend_Http_Response
0264      * @throws Zend_Gdata_App_HttpException
0265      * @throws Zend_Gdata_App_InvalidArgumentException
0266      * @throws Zend_Gdata_Gapps_ServiceException
0267      */
0268     public function put($data, $uri = null, $remainingRedirects = null,
0269             $contentType = null, $extraHeaders = null)
0270     {
0271         try {
0272             return parent::put($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
0273         } catch (Zend_Gdata_App_HttpException $e) {
0274             self::throwServiceExceptionIfDetected($e);
0275         }
0276     }
0277 
0278     /**
0279      * DELETE entry with client object
0280      * This method overrides the default behavior of Zend_Gdata_App,
0281      * providing support for Zend_Gdata_Gapps_ServiceException.
0282      *
0283      * @param mixed $data The Zend_Gdata_App_Entry or URL to delete
0284      * @param integer $remainingRedirects (optional)
0285      * @return void
0286      * @throws Zend_Gdata_App_HttpException
0287      * @throws Zend_Gdata_App_InvalidArgumentException
0288      * @throws Zend_Gdata_Gapps_ServiceException
0289      */
0290     public function delete($data, $remainingRedirects = null)
0291     {
0292         try {
0293             return parent::delete($data, $remainingRedirects);
0294         } catch (Zend_Gdata_App_HttpException $e) {
0295             self::throwServiceExceptionIfDetected($e);
0296         }
0297     }
0298 
0299     /**
0300      * Set domain for this service instance. This should be a fully qualified
0301      * domain, such as 'foo.example.com'.
0302      *
0303      * This value is used when calculating URLs for retrieving and posting
0304      * entries. If no value is specified, a URL will have to be manually
0305      * constructed prior to using any methods which interact with the Google
0306      * Apps provisioning service.
0307      *
0308      * @param string $value The domain to be used for this session.
0309      */
0310     public function setDomain($value)
0311     {
0312         $this->_domain = $value;
0313     }
0314 
0315     /**
0316      * Get domain for this service instance. This should be a fully qualified
0317      * domain, such as 'foo.example.com'. If no domain is set, null will be
0318      * returned.
0319      *
0320      * @return string The domain to be used for this session, or null if not
0321      *          set.
0322      */
0323     public function getDomain()
0324     {
0325         return $this->_domain;
0326     }
0327 
0328     /**
0329      * Returns the base URL used to access the Google Apps service, based
0330      * on the current domain. The current domain can be temporarily
0331      * overridden by providing a fully qualified domain as $domain.
0332      *
0333      * @param string $domain (optional) A fully-qualified domain to use
0334      *          instead of the default domain for this service instance.
0335      * @throws Zend_Gdata_App_InvalidArgumentException
0336      */
0337      public function getBaseUrl($domain = null)
0338      {
0339          if ($domain !== null) {
0340              return self::APPS_BASE_FEED_URI . '/' . $domain;
0341          } else if ($this->_domain !== null) {
0342              return self::APPS_BASE_FEED_URI . '/' . $this->_domain;
0343          } else {
0344              // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0345              throw new Zend_Gdata_App_InvalidArgumentException(
0346                      'Domain must be specified.');
0347          }
0348      }
0349 
0350     /**
0351      * Retrieve a UserFeed containing multiple UserEntry objects.
0352      *
0353      * @param mixed $location (optional) The location for the feed, as a URL
0354      *          or Query.
0355      * @return Zend_Gdata_Gapps_UserFeed
0356      * @throws Zend_Gdata_App_Exception
0357      * @throws Zend_Gdata_App_HttpException
0358      * @throws Zend_Gdata_Gapps_ServiceException
0359      */
0360     public function getUserFeed($location = null)
0361     {
0362         if ($location === null) {
0363             $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
0364         } else if ($location instanceof Zend_Gdata_Query) {
0365             $uri = $location->getQueryUrl();
0366         } else {
0367             $uri = $location;
0368         }
0369         return parent::getFeed($uri, 'Zend_Gdata_Gapps_UserFeed');
0370     }
0371 
0372     /**
0373      * Retreive NicknameFeed object containing multiple NicknameEntry objects.
0374      *
0375      * @param mixed $location (optional) The location for the feed, as a URL
0376      *          or Query.
0377      * @return Zend_Gdata_Gapps_NicknameFeed
0378      * @throws Zend_Gdata_App_Exception
0379      * @throws Zend_Gdata_App_HttpException
0380      * @throws Zend_Gdata_Gapps_ServiceException
0381      */
0382     public function getNicknameFeed($location = null)
0383     {
0384         if ($location === null) {
0385             $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
0386         } else if ($location instanceof Zend_Gdata_Query) {
0387             $uri = $location->getQueryUrl();
0388         } else {
0389             $uri = $location;
0390         }
0391         return parent::getFeed($uri, 'Zend_Gdata_Gapps_NicknameFeed');
0392     }
0393 
0394     /**
0395      * Retreive GroupFeed object containing multiple GroupEntry
0396      * objects.
0397      *
0398      * @param mixed $location (optional) The location for the feed, as a URL
0399      *          or Query.
0400      * @return Zend_Gdata_Gapps_GroupFeed
0401      * @throws Zend_Gdata_App_Exception
0402      * @throws Zend_Gdata_App_HttpException
0403      * @throws Zend_Gdata_Gapps_ServiceException
0404      */
0405     public function getGroupFeed($location = null)
0406     {
0407         if ($location === null) {
0408             $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
0409             $uri .= $this->getDomain();
0410         } else if ($location instanceof Zend_Gdata_Query) {
0411             $uri = $location->getQueryUrl();
0412         } else {
0413             $uri = $location;
0414         }
0415         return parent::getFeed($uri, 'Zend_Gdata_Gapps_GroupFeed');
0416     }
0417 
0418     /**
0419      * Retreive MemberFeed object containing multiple MemberEntry
0420      * objects.
0421      *
0422      * @param mixed $location (optional) The location for the feed, as a URL
0423      *          or Query.
0424      * @return Zend_Gdata_Gapps_MemberFeed
0425      * @throws Zend_Gdata_App_Exception
0426      * @throws Zend_Gdata_App_HttpException
0427      * @throws Zend_Gdata_Gapps_ServiceException
0428      */
0429     public function getMemberFeed($location = null)
0430     {
0431         if ($location === null) {
0432             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0433             throw new Zend_Gdata_App_InvalidArgumentException(
0434                     'Location must not be null');
0435         } else if ($location instanceof Zend_Gdata_Query) {
0436             $uri = $location->getQueryUrl();
0437         } else {
0438             $uri = $location;
0439         }
0440         return parent::getFeed($uri, 'Zend_Gdata_Gapps_MemberFeed');
0441     }
0442 
0443     /**
0444      * Retreive OwnerFeed object containing multiple OwnerEntry
0445      * objects.
0446      *
0447      * @param mixed $location (optional) The location for the feed, as a URL
0448      *          or Query.
0449      * @return Zend_Gdata_Gapps_OwnerFeed
0450      * @throws Zend_Gdata_App_Exception
0451      * @throws Zend_Gdata_App_HttpException
0452      * @throws Zend_Gdata_Gapps_ServiceException
0453      */
0454     public function getOwnerFeed($location = null)
0455     {
0456         if ($location === null) {
0457             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0458             throw new Zend_Gdata_App_InvalidArgumentException(
0459                     'Location must not be null');
0460         } else if ($location instanceof Zend_Gdata_Query) {
0461             $uri = $location->getQueryUrl();
0462         } else {
0463             $uri = $location;
0464         }
0465         return parent::getFeed($uri, 'Zend_Gdata_Gapps_OwnerFeed');
0466     }
0467 
0468     /**
0469      * Retreive EmailListFeed object containing multiple EmailListEntry
0470      * objects.
0471      *
0472      * @param mixed $location (optional) The location for the feed, as a URL
0473      *          or Query.
0474      * @return Zend_Gdata_Gapps_EmailListFeed
0475      * @throws Zend_Gdata_App_Exception
0476      * @throws Zend_Gdata_App_HttpException
0477      * @throws Zend_Gdata_Gapps_ServiceException
0478      */
0479     public function getEmailListFeed($location = null)
0480     {
0481         if ($location === null) {
0482             $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
0483         } else if ($location instanceof Zend_Gdata_Query) {
0484             $uri = $location->getQueryUrl();
0485         } else {
0486             $uri = $location;
0487         }
0488         return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListFeed');
0489     }
0490 
0491     /**
0492      * Retreive EmailListRecipientFeed object containing multiple
0493      * EmailListRecipientEntry objects.
0494      *
0495      * @param mixed $location The location for the feed, as a URL or Query.
0496      * @return Zend_Gdata_Gapps_EmailListRecipientFeed
0497      * @throws Zend_Gdata_App_Exception
0498      * @throws Zend_Gdata_App_HttpException
0499      * @throws Zend_Gdata_Gapps_ServiceException
0500      */
0501     public function getEmailListRecipientFeed($location)
0502     {
0503         if ($location === null) {
0504             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0505             throw new Zend_Gdata_App_InvalidArgumentException(
0506                     'Location must not be null');
0507         } else if ($location instanceof Zend_Gdata_Query) {
0508             $uri = $location->getQueryUrl();
0509         } else {
0510             $uri = $location;
0511         }
0512         return parent::getFeed($uri, 'Zend_Gdata_Gapps_EmailListRecipientFeed');
0513     }
0514 
0515     /**
0516      * Retreive a single UserEntry object.
0517      *
0518      * @param mixed $location The location for the feed, as a URL or Query.
0519      * @return Zend_Gdata_Gapps_UserEntry
0520      * @throws Zend_Gdata_App_Exception
0521      * @throws Zend_Gdata_App_HttpException
0522      * @throws Zend_Gdata_Gapps_ServiceException
0523      */
0524     public function getUserEntry($location)
0525     {
0526         if ($location === null) {
0527             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0528             throw new Zend_Gdata_App_InvalidArgumentException(
0529                     'Location must not be null');
0530         } else if ($location instanceof Zend_Gdata_Query) {
0531             $uri = $location->getQueryUrl();
0532         } else {
0533             $uri = $location;
0534         }
0535         return parent::getEntry($uri, 'Zend_Gdata_Gapps_UserEntry');
0536     }
0537 
0538     /**
0539      * Retreive a single NicknameEntry object.
0540      *
0541      * @param mixed $location The location for the feed, as a URL or Query.
0542      * @return Zend_Gdata_Gapps_NicknameEntry
0543      * @throws Zend_Gdata_App_Exception
0544      * @throws Zend_Gdata_App_HttpException
0545      * @throws Zend_Gdata_Gapps_ServiceException
0546      */
0547     public function getNicknameEntry($location)
0548     {
0549         if ($location === null) {
0550             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0551             throw new Zend_Gdata_App_InvalidArgumentException(
0552                     'Location must not be null');
0553         } else if ($location instanceof Zend_Gdata_Query) {
0554             $uri = $location->getQueryUrl();
0555         } else {
0556             $uri = $location;
0557         }
0558         return parent::getEntry($uri, 'Zend_Gdata_Gapps_NicknameEntry');
0559     }
0560 
0561     /**
0562      * Retreive a single GroupEntry object.
0563      *
0564      * @param mixed $location The location for the feed, as a URL or Query.
0565      * @return Zend_Gdata_Gapps_GroupEntry
0566      * @throws Zend_Gdata_App_Exception
0567      * @throws Zend_Gdata_App_HttpException
0568      * @throws Zend_Gdata_Gapps_ServiceException
0569      */
0570     public function getGroupEntry($location = null)
0571     {
0572         if ($location === null) {
0573             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0574             throw new Zend_Gdata_App_InvalidArgumentException(
0575                     'Location must not be null');
0576         } else if ($location instanceof Zend_Gdata_Query) {
0577             $uri = $location->getQueryUrl();
0578         } else {
0579             $uri = $location;
0580         }
0581         return parent::getEntry($uri, 'Zend_Gdata_Gapps_GroupEntry');
0582     }
0583 
0584     /**
0585      * Retreive a single MemberEntry object.
0586      *
0587      * @param mixed $location The location for the feed, as a URL or Query.
0588      * @return Zend_Gdata_Gapps_MemberEntry
0589      * @throws Zend_Gdata_App_Exception
0590      * @throws Zend_Gdata_App_HttpException
0591      * @throws Zend_Gdata_Gapps_ServiceException
0592      */
0593     public function getMemberEntry($location = null)
0594     {
0595         if ($location === null) {
0596             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0597             throw new Zend_Gdata_App_InvalidArgumentException(
0598                     'Location must not be null');
0599         } else if ($location instanceof Zend_Gdata_Query) {
0600             $uri = $location->getQueryUrl();
0601         } else {
0602             $uri = $location;
0603         }
0604         return parent::getEntry($uri, 'Zend_Gdata_Gapps_MemberEntry');
0605     }
0606 
0607     /**
0608      * Retreive a single OwnerEntry object.
0609      *
0610      * @param mixed $location The location for the feed, as a URL or Query.
0611      * @return Zend_Gdata_Gapps_OwnerEntry
0612      * @throws Zend_Gdata_App_Exception
0613      * @throws Zend_Gdata_App_HttpException
0614      * @throws Zend_Gdata_Gapps_ServiceException
0615      */
0616     public function getOwnerEntry($location = null)
0617     {
0618         if ($location === null) {
0619             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0620             throw new Zend_Gdata_App_InvalidArgumentException(
0621                     'Location must not be null');
0622         } else if ($location instanceof Zend_Gdata_Query) {
0623             $uri = $location->getQueryUrl();
0624         } else {
0625             $uri = $location;
0626         }
0627         return parent::getEntry($uri, 'Zend_Gdata_Gapps_OwnerEntry');
0628     }
0629 
0630     /**
0631      * Retreive a single EmailListEntry object.
0632      *
0633      * @param mixed $location The location for the feed, as a URL or Query.
0634      * @return Zend_Gdata_Gapps_EmailListEntry
0635      * @throws Zend_Gdata_App_Exception
0636      * @throws Zend_Gdata_App_HttpException
0637      * @throws Zend_Gdata_Gapps_ServiceException
0638      */
0639     public function getEmailListEntry($location)
0640     {
0641         if ($location === null) {
0642             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0643             throw new Zend_Gdata_App_InvalidArgumentException(
0644                     'Location must not be null');
0645         } else if ($location instanceof Zend_Gdata_Query) {
0646             $uri = $location->getQueryUrl();
0647         } else {
0648             $uri = $location;
0649         }
0650         return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListEntry');
0651     }
0652 
0653     /**
0654      * Retreive a single EmailListRecipientEntry object.
0655      *
0656      * @param mixed $location The location for the feed, as a URL or Query.
0657      * @return Zend_Gdata_Gapps_EmailListRecipientEntry
0658      * @throws Zend_Gdata_App_Exception
0659      * @throws Zend_Gdata_App_HttpException
0660      * @throws Zend_Gdata_Gapps_ServiceException
0661      */
0662     public function getEmailListRecipientEntry($location)
0663     {
0664         if ($location === null) {
0665             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0666             throw new Zend_Gdata_App_InvalidArgumentException(
0667                     'Location must not be null');
0668         } else if ($location instanceof Zend_Gdata_Query) {
0669             $uri = $location->getQueryUrl();
0670         } else {
0671             $uri = $location;
0672         }
0673         return parent::getEntry($uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
0674     }
0675 
0676     /**
0677      * Create a new user from a UserEntry.
0678      *
0679      * @param Zend_Gdata_Gapps_UserEntry $user The user entry to insert.
0680      * @param string $uri (optional) The URI where the user should be
0681      *          uploaded to. If null, the default user creation URI for
0682      *          this domain will be used.
0683      * @return Zend_Gdata_Gapps_UserEntry The inserted user entry as
0684      *          returned by the server.
0685      * @throws Zend_Gdata_App_Exception
0686      * @throws Zend_Gdata_App_HttpException
0687      * @throws Zend_Gdata_Gapps_ServiceException
0688      */
0689     public function insertUser($user, $uri = null)
0690     {
0691         if ($uri === null) {
0692             $uri = $this->getBaseUrl() . self::APPS_USER_PATH;
0693         }
0694         $newEntry = $this->insertEntry($user, $uri, 'Zend_Gdata_Gapps_UserEntry');
0695         return $newEntry;
0696     }
0697 
0698     /**
0699      * Create a new nickname from a NicknameEntry.
0700      *
0701      * @param Zend_Gdata_Gapps_NicknameEntry $nickname The nickname entry to
0702      *          insert.
0703      * @param string $uri (optional) The URI where the nickname should be
0704      *          uploaded to. If null, the default nickname creation URI for
0705      *          this domain will be used.
0706      * @return Zend_Gdata_Gapps_NicknameEntry The inserted nickname entry as
0707      *          returned by the server.
0708      * @throws Zend_Gdata_App_Exception
0709      * @throws Zend_Gdata_App_HttpException
0710      * @throws Zend_Gdata_Gapps_ServiceException
0711      */
0712     public function insertNickname($nickname, $uri = null)
0713     {
0714         if ($uri === null) {
0715             $uri = $this->getBaseUrl() . self::APPS_NICKNAME_PATH;
0716         }
0717         $newEntry = $this->insertEntry($nickname, $uri, 'Zend_Gdata_Gapps_NicknameEntry');
0718         return $newEntry;
0719     }
0720 
0721     /**
0722      * Create a new group from a GroupEntry.
0723      *
0724      * @param Zend_Gdata_Gapps_GroupEntry $group The group entry to insert.
0725      * @param string $uri (optional) The URI where the group should be
0726      *          uploaded to. If null, the default user creation URI for
0727      *          this domain will be used.
0728      * @return Zend_Gdata_Gapps_GroupEntry The inserted group entry as
0729      *          returned by the server.
0730      * @throws Zend_Gdata_App_Exception
0731      * @throws Zend_Gdata_App_HttpException
0732      * @throws Zend_Gdata_Gapps_ServiceException
0733      */
0734     public function insertGroup($group, $uri = null)
0735     {
0736         if ($uri === null) {
0737             $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
0738             $uri .= $this->getDomain();
0739         }
0740         $newEntry = $this->insertEntry($group, $uri, 'Zend_Gdata_Gapps_GroupEntry');
0741         return $newEntry;
0742     }
0743 
0744     /**
0745      * Create a new member from a MemberEntry.
0746      *
0747      * @param Zend_Gdata_Gapps_MemberEntry $member The member entry to insert.
0748      * @param string $uri (optional) The URI where the group should be
0749      *          uploaded to. If null, the default user creation URI for
0750      *          this domain will be used.
0751      * @return Zend_Gdata_Gapps_MemberEntry The inserted member entry as
0752      *          returned by the server.
0753      * @throws Zend_Gdata_App_Exception
0754      * @throws Zend_Gdata_App_HttpException
0755      * @throws Zend_Gdata_Gapps_ServiceException
0756      */
0757     public function insertMember($member, $uri = null)
0758     {
0759         if ($uri === null) {
0760             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0761             throw new Zend_Gdata_App_InvalidArgumentException(
0762                     'URI must not be null');
0763         }
0764         $newEntry = $this->insertEntry($member, $uri, 'Zend_Gdata_Gapps_MemberEntry');
0765         return $newEntry;
0766     }
0767 
0768     /**
0769      * Create a new group from a OwnerEntry.
0770      *
0771      * @param Zend_Gdata_Gapps_OwnerEntry $owner The owner entry to insert.
0772      * @param string $uri (optional) The URI where the owner should be
0773      *          uploaded to. If null, the default user creation URI for
0774      *          this domain will be used.
0775      * @return Zend_Gdata_Gapps_OwnerEntry The inserted owner entry as
0776      *          returned by the server.
0777      * @throws Zend_Gdata_App_Exception
0778      * @throws Zend_Gdata_App_HttpException
0779      * @throws Zend_Gdata_Gapps_ServiceException
0780      */
0781     public function insertOwner($owner, $uri = null)
0782     {
0783         if ($uri === null) {
0784             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0785             throw new Zend_Gdata_App_InvalidArgumentException(
0786                     'URI must not be null');
0787         }
0788         $newEntry = $this->insertEntry($owner, $uri, 'Zend_Gdata_Gapps_OwnerEntry');
0789         return $newEntry;
0790     }
0791 
0792     /**
0793      * Create a new email list from an EmailListEntry.
0794      *
0795      * @param Zend_Gdata_Gapps_EmailListEntry $emailList The email list entry
0796      *          to insert.
0797      * @param string $uri (optional) The URI where the email list should be
0798      *          uploaded to. If null, the default email list creation URI for
0799      *          this domain will be used.
0800      * @return Zend_Gdata_Gapps_EmailListEntry The inserted email list entry
0801      *          as returned by the server.
0802      * @throws Zend_Gdata_App_Exception
0803      * @throws Zend_Gdata_App_HttpException
0804      * @throws Zend_Gdata_Gapps_ServiceException
0805      */
0806     public function insertEmailList($emailList, $uri = null)
0807     {
0808         if ($uri === null) {
0809             $uri = $this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH;
0810         }
0811         $newEntry = $this->insertEntry($emailList, $uri, 'Zend_Gdata_Gapps_EmailListEntry');
0812         return $newEntry;
0813     }
0814 
0815     /**
0816      * Create a new email list recipient from an EmailListRecipientEntry.
0817      *
0818      * @param Zend_Gdata_Gapps_EmailListRecipientEntry $recipient The recipient
0819      *          entry to insert.
0820      * @param string $uri (optional) The URI where the recipient should be
0821      *          uploaded to. If null, the default recipient creation URI for
0822      *          this domain will be used.
0823      * @return Zend_Gdata_Gapps_EmailListRecipientEntry The inserted
0824      *          recipient entry as returned by the server.
0825      * @throws Zend_Gdata_App_Exception
0826      * @throws Zend_Gdata_App_HttpException
0827      * @throws Zend_Gdata_Gapps_ServiceException
0828      */
0829     public function insertEmailListRecipient($recipient, $uri = null)
0830     {
0831         if ($uri === null) {
0832             // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0833             throw new Zend_Gdata_App_InvalidArgumentException(
0834                     'URI must not be null');
0835         } elseif ($uri instanceof Zend_Gdata_Gapps_EmailListEntry) {
0836             $uri = $uri->getLink('edit')->href;
0837         }
0838         $newEntry = $this->insertEntry($recipient, $uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
0839         return $newEntry;
0840     }
0841 
0842     /**
0843      * Provides a magic factory method to instantiate new objects with
0844      * shorter syntax than would otherwise be required by the Zend Framework
0845      * naming conventions. For more information, see Zend_Gdata_App::__call().
0846      *
0847      * This overrides the default behavior of __call() so that query classes
0848      * do not need to have their domain manually set when created with
0849      * a magic factory method.
0850      *
0851      * @see Zend_Gdata_App::__call()
0852      * @param string $method The method name being called
0853      * @param array $args The arguments passed to the call
0854      * @throws Zend_Gdata_App_Exception
0855      */
0856     public function __call($method, $args) {
0857         if (preg_match('/^new(\w+Query)/', $method, $matches)) {
0858             $class = $matches[1];
0859             $foundClassName = null;
0860             foreach ($this->_registeredPackages as $name) {
0861                  try {
0862                      // Autoloading disabled on next line for compatibility
0863                      // with magic factories. See ZF-6660.
0864                      if (!class_exists($name . '_' . $class, false)) {
0865                         // require_once 'Zend/Loader.php';
0866                         @Zend_Loader::loadClass($name . '_' . $class);
0867                      }
0868                      $foundClassName = $name . '_' . $class;
0869                      break;
0870                  } catch (Zend_Exception $e) {
0871                      // package wasn't here- continue searching
0872                  }
0873             }
0874             if ($foundClassName != null) {
0875                 $reflectionObj = new ReflectionClass($foundClassName);
0876                 // Prepend the domain to the query
0877                 $args = array_merge(array($this->getDomain()), $args);
0878                 return $reflectionObj->newInstanceArgs($args);
0879             } else {
0880                 // require_once 'Zend/Gdata/App/Exception.php';
0881                 throw new Zend_Gdata_App_Exception(
0882                         "Unable to find '${class}' in registered packages");
0883             }
0884         } else {
0885             return parent::__call($method, $args);
0886         }
0887 
0888     }
0889 
0890     // Convenience methods
0891     // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e
0892 
0893     /**
0894      * Create a new user entry and send it to the Google Apps servers.
0895      *
0896      * @param string $username The username for the new user.
0897      * @param string $givenName The given name for the new user.
0898      * @param string $familyName The family name for the new user.
0899      * @param string $password The password for the new user as a plaintext string
0900      *                 (if $passwordHashFunction is null) or a SHA-1 hashed
0901      *                 value (if $passwordHashFunction = 'SHA-1').
0902      * @param string $quotaLimitInMB (optional) The quota limit for the new user in MB.
0903      * @return Zend_Gdata_Gapps_UserEntry (optional) The new user entry as returned by
0904      *                 server.
0905      * @throws Zend_Gdata_App_Exception
0906      * @throws Zend_Gdata_App_HttpException
0907      * @throws Zend_Gdata_Gapps_ServiceException
0908      */
0909     public function createUser ($username, $givenName, $familyName, $password,
0910             $passwordHashFunction = null, $quotaLimitInMB = null) {
0911         $user = $this->newUserEntry();
0912         $user->login = $this->newLogin();
0913         $user->login->username = $username;
0914         $user->login->password = $password;
0915         $user->login->hashFunctionName = $passwordHashFunction;
0916         $user->name = $this->newName();
0917         $user->name->givenName = $givenName;
0918         $user->name->familyName = $familyName;
0919         if ($quotaLimitInMB !== null) {
0920             $user->quota = $this->newQuota();
0921             $user->quota->limit = $quotaLimitInMB;
0922         }
0923         return $this->insertUser($user);
0924     }
0925 
0926     /**
0927      * Retrieve a user based on their username.
0928      *
0929      * @param string $username The username to search for.
0930      * @return Zend_Gdata_Gapps_UserEntry The username to search for, or null
0931      *              if no match found.
0932      * @throws Zend_Gdata_App_InvalidArgumentException
0933      * @throws Zend_Gdata_App_HttpException
0934      */
0935     public function retrieveUser ($username) {
0936         $query = $this->newUserQuery($username);
0937         try {
0938             $user = $this->getUserEntry($query);
0939         } catch (Zend_Gdata_Gapps_ServiceException $e) {
0940             // Set the user to null if not found
0941             if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
0942                 $user = null;
0943             } else {
0944                 throw $e;
0945             }
0946         }
0947         return $user;
0948     }
0949 
0950     /**
0951      * Retrieve a page of users in alphabetical order, starting with the
0952      * provided username.
0953      *
0954      * @param string $startUsername (optional) The first username to retrieve.
0955      *          If null or not declared, the page will begin with the first
0956      *          user in the domain.
0957      * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
0958      *              objects representing all users in the domain.
0959      * @throws Zend_Gdata_App_Exception
0960      * @throws Zend_Gdata_App_HttpException
0961      * @throws Zend_Gdata_Gapps_ServiceException
0962      */
0963     public function retrievePageOfUsers ($startUsername = null) {
0964         $query = $this->newUserQuery();
0965         $query->setStartUsername($startUsername);
0966         return $this->getUserFeed($query);
0967     }
0968 
0969     /**
0970      * Retrieve all users in the current domain. Be aware that
0971      * calling this function on a domain with many users will take a
0972      * signifigant amount of time to complete. On larger domains this may
0973      * may cause execution to timeout without proper precautions in place.
0974      *
0975      * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
0976      *              objects representing all users in the domain.
0977      * @throws Zend_Gdata_App_Exception
0978      * @throws Zend_Gdata_App_HttpException
0979      * @throws Zend_Gdata_Gapps_ServiceException
0980      */
0981     public function retrieveAllUsers () {
0982         return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers());
0983     }
0984 
0985     /**
0986      * Overwrite a specified username with the provided UserEntry.  The
0987      * UserEntry does not need to contain an edit link.
0988      *
0989      * This method is provided for compliance with the Google Apps
0990      * Provisioning API specification. Normally users will instead want to
0991      * call UserEntry::save() instead.
0992      *
0993      * @see Zend_Gdata_App_Entry::save
0994      * @param string $username The username whose data will be overwritten.
0995      * @param Zend_Gdata_Gapps_UserEntry $userEntry The user entry which
0996      *          will be overwritten.
0997      * @return Zend_Gdata_Gapps_UserEntry The UserEntry returned by the
0998      *          server.
0999      * @throws Zend_Gdata_App_Exception
1000      * @throws Zend_Gdata_App_HttpException
1001      * @throws Zend_Gdata_Gapps_ServiceException
1002      */
1003     public function updateUser($username, $userEntry) {
1004         return $this->updateEntry($userEntry, $this->getBaseUrl() .
1005             self::APPS_USER_PATH . '/' . $username);
1006     }
1007 
1008     /**
1009      * Mark a given user as suspended.
1010      *
1011      * @param string $username The username associated with the user who
1012      *          should be suspended.
1013      * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
1014      *          user.
1015      * @throws Zend_Gdata_App_Exception
1016      * @throws Zend_Gdata_App_HttpException
1017      * @throws Zend_Gdata_Gapps_ServiceException
1018      */
1019     public function suspendUser($username) {
1020         $user = $this->retrieveUser($username);
1021         $user->login->suspended = true;
1022         return $user->save();
1023     }
1024 
1025     /**
1026      * Mark a given user as not suspended.
1027      *
1028      * @param string $username The username associated with the user who
1029      *          should be restored.
1030      * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
1031      *          user.
1032      * @throws Zend_Gdata_App_Exception
1033      * @throws Zend_Gdata_App_HttpException
1034      * @throws Zend_Gdata_Gapps_ServiceException
1035      */
1036     public function restoreUser($username) {
1037         $user = $this->retrieveUser($username);
1038         $user->login->suspended = false;
1039         return $user->save();
1040     }
1041 
1042     /**
1043      * Delete a user by username.
1044      *
1045      * @param string $username The username associated with the user who
1046      *          should be deleted.
1047      * @throws Zend_Gdata_App_Exception
1048      * @throws Zend_Gdata_App_HttpException
1049      * @throws Zend_Gdata_Gapps_ServiceException
1050      */
1051     public function deleteUser($username) {
1052         $this->delete($this->getBaseUrl() . self::APPS_USER_PATH . '/' .
1053             $username);
1054     }
1055 
1056     /**
1057      * Create a nickname for a given user.
1058      *
1059      * @param string $username The username to which the new nickname should
1060      *          be associated.
1061      * @param string $nickname The new nickname to be created.
1062      * @return Zend_Gdata_Gapps_NicknameEntry The nickname entry which was
1063      *          created by the server.
1064      * @throws Zend_Gdata_App_Exception
1065      * @throws Zend_Gdata_App_HttpException
1066      * @throws Zend_Gdata_Gapps_ServiceException
1067      */
1068     public function createNickname($username, $nickname) {
1069         $entry = $this->newNicknameEntry();
1070         $nickname = $this->newNickname($nickname);
1071         $login = $this->newLogin($username);
1072         $entry->nickname = $nickname;
1073         $entry->login = $login;
1074         return $this->insertNickname($entry);
1075     }
1076 
1077     /**
1078      * Retrieve the entry for a specified nickname.
1079      *
1080      * @param string $nickname The nickname to be retrieved.
1081      * @return Zend_Gdata_Gapps_NicknameEntry The requested nickname entry.
1082      * @throws Zend_Gdata_App_Exception
1083      * @throws Zend_Gdata_App_HttpException
1084      * @throws Zend_Gdata_Gapps_ServiceException
1085      */
1086     public function retrieveNickname($nickname) {
1087         $query = $this->newNicknameQuery();
1088         $query->setNickname($nickname);
1089         try {
1090             $nickname = $this->getNicknameEntry($query);
1091         } catch (Zend_Gdata_Gapps_ServiceException $e) {
1092             // Set the nickname to null if not found
1093             if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
1094                 $nickname = null;
1095             } else {
1096                 throw $e;
1097             }
1098         }
1099         return $nickname;
1100     }
1101 
1102     /**
1103      * Retrieve all nicknames associated with a specific username.
1104      *
1105      * @param string $username The username whose nicknames should be
1106      *          returned.
1107      * @return Zend_Gdata_Gapps_NicknameFeed A feed containing all nicknames
1108      *          for the given user, or null if
1109      * @throws Zend_Gdata_App_Exception
1110      * @throws Zend_Gdata_App_HttpException
1111      * @throws Zend_Gdata_Gapps_ServiceException
1112      */
1113     public function retrieveNicknames($username) {
1114         $query = $this->newNicknameQuery();
1115         $query->setUsername($username);
1116         $nicknameFeed = $this->retrieveAllEntriesForFeed(
1117             $this->getNicknameFeed($query));
1118         return $nicknameFeed;
1119     }
1120 
1121     /**
1122      * Retrieve a page of nicknames in alphabetical order, starting with the
1123      * provided nickname.
1124      *
1125      * @param string $startNickname (optional) The first nickname to
1126      *          retrieve. If null or not declared, the page will begin with
1127      *          the first nickname in the domain.
1128      * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
1129      *              objects representing all nicknames in the domain.
1130      * @throws Zend_Gdata_App_Exception
1131      * @throws Zend_Gdata_App_HttpException
1132      * @throws Zend_Gdata_Gapps_ServiceException
1133      */
1134     public function retrievePageOfNicknames ($startNickname = null) {
1135         $query = $this->newNicknameQuery();
1136         $query->setStartNickname($startNickname);
1137         return $this->getNicknameFeed($query);
1138     }
1139 
1140     /**
1141      * Retrieve all nicknames in the current domain. Be aware that
1142      * calling this function on a domain with many nicknames will take a
1143      * signifigant amount of time to complete. On larger domains this may
1144      * may cause execution to timeout without proper precautions in place.
1145      *
1146      * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
1147      *              objects representing all nicknames in the domain.
1148      * @throws Zend_Gdata_App_Exception
1149      * @throws Zend_Gdata_App_HttpException
1150      * @throws Zend_Gdata_Gapps_ServiceException
1151      */
1152     public function retrieveAllNicknames () {
1153         return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames());
1154     }
1155 
1156     /**
1157      * Delete a specified nickname.
1158      *
1159      * @param string $nickname The name of the nickname to be deleted.
1160      * @throws Zend_Gdata_App_Exception
1161      * @throws Zend_Gdata_App_HttpException
1162      * @throws Zend_Gdata_Gapps_ServiceException
1163      */
1164     public function deleteNickname($nickname) {
1165         $this->delete($this->getBaseUrl() . self::APPS_NICKNAME_PATH . '/' . $nickname);
1166     }
1167 
1168     /**
1169      * Create a new group.
1170      *
1171      * @param string $groupId A unique identifier for the group
1172      * @param string $groupName The name of the group
1173      * @param string $description A description of the group
1174      * @param string $emailPermission The subscription permission of the group
1175      * @return Zend_Gdata_Gapps_GroupEntry The group entry as created on the server.
1176      */
1177     public function createGroup($groupId, $groupName, $description = null, $emailPermission = null)
1178     {
1179         $i = 0;
1180         $group = $this->newGroupEntry();
1181 
1182         $properties[$i] = $this->newProperty();
1183         $properties[$i]->name = 'groupId';
1184         $properties[$i]->value = $groupId;
1185         $i++;
1186         $properties[$i] = $this->newProperty();
1187         $properties[$i]->name = 'groupName';
1188         $properties[$i]->value = $groupName;
1189         $i++;
1190 
1191         if($description != null) {
1192             $properties[$i] = $this->newProperty();
1193             $properties[$i]->name = 'description';
1194             $properties[$i]->value = $description;
1195             $i++;
1196         }
1197 
1198         if($emailPermission != null) {
1199             $properties[$i] = $this->newProperty();
1200             $properties[$i]->name = 'emailPermission';
1201             $properties[$i]->value = $emailPermission;
1202             $i++;
1203         }
1204 
1205         $group->property = $properties;
1206 
1207         return $this->insertGroup($group);
1208     }
1209 
1210     /**
1211      * Retrieves a group based on group id
1212      *
1213      * @param string $groupId The unique identifier for the group
1214      * @return Zend_Gdata_Gapps_GroupEntry The group entry as returned by the server.
1215      */
1216     public function retrieveGroup($groupId)
1217     {
1218         $query = $this->newGroupQuery($groupId);
1219         //$query->setGroupId($groupId);
1220 
1221         try {
1222             $group = $this->getGroupEntry($query);
1223         } catch (Zend_Gdata_Gapps_ServiceException $e) {
1224             // Set the group to null if not found
1225             if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
1226                 $group = null;
1227             } else {
1228                 throw $e;
1229             }
1230         }
1231         return $group;
1232     }
1233 
1234     /**
1235      * Retrieve all groups in the current domain. Be aware that
1236      * calling this function on a domain with many groups will take a
1237      * signifigant amount of time to complete. On larger domains this may
1238      * may cause execution to timeout without proper precautions in place.
1239      *
1240      * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry objects
1241      *              representing all groups apart of the domain.
1242      */
1243     public function retrieveAllGroups()
1244     {
1245         return $this->retrieveAllEntriesForFeed($this->retrievePageOfGroups());
1246     }
1247 
1248     /**
1249      * Delete a group
1250      *
1251      * @param string $groupId The unique identifier for the group
1252      */
1253     public function deleteGroup($groupId)
1254     {
1255         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1256         $uri .= $this->getDomain() . '/' . $groupId;
1257 
1258         $this->delete($uri);
1259     }
1260 
1261     /**
1262      * Check to see if a member id or group id is a member of group
1263      *
1264      * @param string $memberId Member id or group group id
1265      * @param string $groupId Group to be checked for
1266      * @return bool True, if given entity is a member
1267      */
1268     public function isMember($memberId, $groupId)
1269     {
1270         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1271         $uri .= $this->getDomain() . '/' . $groupId . '/member/' . $memberId;
1272 
1273         //if the enitiy is not a member, an exception is thrown
1274         try {
1275             $results = $this->get($uri);
1276         } catch (Exception $e) {
1277             $results = false;
1278         }
1279 
1280         if($results) {
1281             return TRUE;
1282         } else {
1283             return FALSE;
1284         }
1285     }
1286 
1287     /**
1288      * Add an email address to a group as a member
1289      *
1290      * @param string $recipientAddress Email address, member id, or group id
1291      * @param string $groupId The unique id of the group
1292      * @return Zend_Gdata_Gapps_MemberEntry The member entry returned by the server
1293      */
1294     public function addMemberToGroup($recipientAddress, $groupId)
1295     {
1296         $member = $this->newMemberEntry();
1297 
1298         $properties[] = $this->newProperty();
1299         $properties[0]->name = 'memberId';
1300         $properties[0]->value = $recipientAddress;
1301 
1302         $member->property = $properties;
1303 
1304         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1305         $uri .= $this->getDomain() . '/' . $groupId . '/member';
1306 
1307         return $this->insertMember($member, $uri);
1308     }
1309 
1310     /**
1311      * Remove a member id from a group
1312      *
1313      * @param string $memberId Member id or group id
1314      * @param string $groupId The unique id of the group
1315      */
1316     public function removeMemberFromGroup($memberId, $groupId)
1317     {
1318         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1319         $uri .= $this->getDomain() . '/' . $groupId . '/member/' . $memberId;
1320 
1321         return $this->delete($uri);
1322     }
1323 
1324     /**
1325      * Retrieves all the members of a group
1326      *
1327      * @param string $groupId The unique id of the group
1328      * @return Zend_Gdata_Gapps_MemberFeed Collection of MemberEntry objects
1329      *              representing all members apart of the group.
1330      */
1331     public function retrieveAllMembers($groupId)
1332     {
1333         return $this->retrieveAllEntriesForFeed(
1334                 $this->retrievePageOfMembers($groupId));
1335     }
1336 
1337     /**
1338      * Add an email as an owner of a group
1339      *
1340      * @param string $email Owner's email
1341      * @param string $groupId Group ownership to be checked for
1342      * @return Zend_Gdata_Gapps_OwnerEntry The OwnerEntry returned by the server
1343      */
1344     public function addOwnerToGroup($email, $groupId)
1345     {
1346         $owner = $this->newOwnerEntry();
1347 
1348         $properties[] = $this->newProperty();
1349         $properties[0]->name = 'email';
1350         $properties[0]->value = $email;
1351 
1352         $owner->property = $properties;
1353 
1354         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1355         $uri .= $this->getDomain() . '/' . $groupId . '/owner';
1356 
1357         return $this->insertOwner($owner, $uri);
1358     }
1359 
1360     /**
1361      * Retrieves all the owners of a group
1362      *
1363      * @param string $groupId The unique identifier for the group
1364      * @return Zend_Gdata_Gapps_OwnerFeed Collection of Zend_Gdata_OwnerEntry
1365      *              objects representing all owners apart of the group.
1366      */
1367     public function retrieveGroupOwners($groupId)
1368     {
1369         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1370         $uri .= $this->getDomain() . '/' . $groupId . '/owner';
1371 
1372         return $this->getOwnerFeed($uri);
1373     }
1374 
1375     /**
1376      * Checks to see if an email is an owner of a group
1377      *
1378      * @param string $email Owner's email
1379      * @param string $groupId Group ownership to be checked for
1380      * @return bool True, if given entity is an owner
1381      */
1382     public function isOwner($email, $groupId)
1383     {
1384         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1385         $uri .= $this->getDomain() . '/' . $groupId . '/owner/' . $email;
1386 
1387         //if the enitiy is not an owner of the group, an exception is thrown
1388         try {
1389             $results = $this->get($uri);
1390         } catch (Exception $e) {
1391             $results = false;
1392         }
1393 
1394         if($results) {
1395             return TRUE;
1396         } else {
1397             return FALSE;
1398         }
1399     }
1400 
1401     /**
1402      * Remove email as an owner of a group
1403      *
1404      * @param string $email Owner's email
1405      * @param string $groupId The unique identifier for the group
1406      */
1407     public function removeOwnerFromGroup($email, $groupId)
1408     {
1409         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1410         $uri .= $this->getDomain() . '/' . $groupId . '/owner/' . $email;
1411 
1412         return $this->delete($uri);
1413     }
1414 
1415     /**
1416      * Update group properties with new values. any property not defined will not
1417      * be updated
1418      *
1419      * @param string $groupId A unique identifier for the group
1420      * @param string $groupName The name of the group
1421      * @param string $description A description of the group
1422      * @param string $emailPermission The subscription permission of the group
1423      * @return Zend_Gdata_Gapps_GroupEntry The group entry as updated on the server.
1424      */
1425     public function updateGroup($groupId, $groupName = null, $description = null,
1426             $emailPermission = null)
1427     {
1428         $i = 0;
1429         $group = $this->newGroupEntry();
1430 
1431         $properties[$i] = $this->newProperty();
1432         $properties[$i]->name = 'groupId';
1433         $properties[$i]->value = $groupId;
1434         $i++;
1435 
1436         if($groupName != null) {
1437             $properties[$i] = $this->newProperty();
1438             $properties[$i]->name = 'groupName';
1439             $properties[$i]->value = $groupName;
1440             $i++;
1441         }
1442 
1443         if($description != null) {
1444             $properties[$i] = $this->newProperty();
1445             $properties[$i]->name = 'description';
1446             $properties[$i]->value = $description;
1447             $i++;
1448         }
1449 
1450         if($emailPermission != null) {
1451             $properties[$i] = $this->newProperty();
1452             $properties[$i]->name = 'emailPermission';
1453             $properties[$i]->value = $emailPermission;
1454             $i++;
1455         }
1456 
1457         $group->property = $properties;
1458 
1459         $uri  = self::APPS_BASE_FEED_URI . self::APPS_GROUP_PATH . '/';
1460         $uri .= $this->getDomain() . '/' . $groupId;
1461 
1462         return $this->updateEntry($group, $uri, 'Zend_Gdata_Gapps_GroupEntry');
1463     }
1464 
1465     /**
1466      * Retrieve all of the groups that a user is a member of
1467      *
1468      * @param string $memberId Member username
1469      * @param bool $directOnly (Optional) If true, members with direct association
1470      *             only will be considered
1471      * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry
1472      *              objects representing all groups member is apart of in the domain.
1473      */
1474     public function retrieveGroups($memberId, $directOnly = null)
1475     {
1476         $query = $this->newGroupQuery();
1477         $query->setMember($memberId);
1478         if($directOnly != null) {
1479             $query->setDirectOnly($directOnly);
1480         }
1481         return $this->getGroupFeed($query);
1482     }
1483 
1484     /**
1485      * Retrieve a page of groups in alphabetical order, starting with the
1486      * provided group.
1487      *
1488      * @param string $startGroup (optional) The first group to
1489      *              retrieve. If null or not defined, the page will begin
1490      *              with the first group in the domain.
1491      * @return Zend_Gdata_Gapps_GroupFeed Collection of Zend_Gdata_GroupEntry
1492      *              objects representing the groups in the domain.
1493      * @throws Zend_Gdata_App_Exception
1494      * @throws Zend_Gdata_App_HttpException
1495      * @throws Zend_Gdata_Gapps_ServiceException
1496      */
1497     public function retrievePageOfGroups ($startGroup = null)
1498     {
1499         $query = $this->newGroupQuery();
1500         $query->setStartGroupId($startGroup);
1501         return $this->getGroupFeed($query);
1502     }
1503 
1504     /**
1505      * Gets page of Members
1506      *
1507      * @param string $groupId The group id which should be searched.
1508      * @param string $startMember (optinal) The address of the first member,
1509      *              or null to start with the first member in the list.
1510      * @return Zend_Gdata_Gapps_MemberFeed Collection of Zend_Gdata_MemberEntry
1511      *              objects
1512      */
1513     public function retrievePageOfMembers($groupId, $startMember = null)
1514     {
1515         $query = $this->newMemberQuery($groupId);
1516         $query->setStartMemberId($startMember);
1517         return $this->getMemberFeed($query);
1518     }
1519 
1520     /**
1521      * Create a new email list.
1522      *
1523      * @param string $emailList The name of the email list to be created.
1524      * @return Zend_Gdata_Gapps_EmailListEntry The email list entry
1525      *          as created on the server.
1526      * @throws Zend_Gdata_App_Exception
1527      * @throws Zend_Gdata_App_HttpException
1528      * @throws Zend_Gdata_Gapps_ServiceException
1529      */
1530     public function createEmailList($emailList) {
1531         $entry = $this->newEmailListEntry();
1532         $list = $this->newEmailList();
1533         $list->name = $emailList;
1534         $entry->emailList = $list;
1535         return $this->insertEmailList($entry);
1536     }
1537 
1538     /**
1539      * Retrieve all email lists associated with a recipient.
1540      *
1541      * @param string $username The recipient whose associated email lists
1542      *          should be returned.
1543      * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found as
1544      *          Zend_Gdata_EmailListEntry objects.
1545      * @throws Zend_Gdata_App_Exception
1546      * @throws Zend_Gdata_App_HttpException
1547      * @throws Zend_Gdata_Gapps_ServiceException
1548      */
1549     public function retrieveEmailLists($recipient) {
1550         $query = $this->newEmailListQuery();
1551         $query->recipient = $recipient;
1552         return $this->getEmailListFeed($query);
1553     }
1554 
1555     /**
1556      * Retrieve a page of email lists in alphabetical order, starting with the
1557      * provided email list.
1558      *
1559      * @param string $startEmailListName (optional) The first list to
1560      *              retrieve. If null or not defined, the page will begin
1561      *              with the first email list in the domain.
1562      * @return Zend_Gdata_Gapps_EmailListFeed Collection of Zend_Gdata_EmailListEntry
1563      *              objects representing all nicknames in the domain.
1564      * @throws Zend_Gdata_App_Exception
1565      * @throws Zend_Gdata_App_HttpException
1566      * @throws Zend_Gdata_Gapps_ServiceException
1567      */
1568     public function retrievePageOfEmailLists ($startNickname = null) {
1569         $query = $this->newEmailListQuery();
1570         $query->setStartEmailListName($startNickname);
1571         return $this->getEmailListFeed($query);
1572     }
1573 
1574     /**
1575      * Retrieve all email lists associated with the curent domain. Be aware that
1576      * calling this function on a domain with many email lists will take a
1577      * signifigant amount of time to complete. On larger domains this may
1578      * may cause execution to timeout without proper precautions in place.
1579      *
1580      * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found
1581      *              as Zend_Gdata_Gapps_EmailListEntry objects.
1582      * @throws Zend_Gdata_App_Exception
1583      * @throws Zend_Gdata_App_HttpException
1584      * @throws Zend_Gdata_Gapps_ServiceException
1585      */
1586     public function retrieveAllEmailLists() {
1587         return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists());
1588     }
1589 
1590     /**
1591      * Delete a specified email list.
1592      *
1593      * @param string $emailList The name of the emailList to be deleted.
1594      * @throws Zend_Gdata_App_Exception
1595      * @throws Zend_Gdata_App_HttpException
1596      * @throws Zend_Gdata_Gapps_ServiceException
1597      */
1598     public function deleteEmailList($emailList) {
1599         $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
1600             . $emailList);
1601     }
1602 
1603     /**
1604      * Add a specified recipient to an existing emailList.
1605      *
1606      * @param string $recipientAddress The address of the recipient to be
1607      *              added to the email list.
1608      * @param string $emailList The name of the email address to which the
1609      *              recipient should be added.
1610      * @return Zend_Gdata_Gapps_EmailListRecipientEntry The recipient entry
1611      *              created by the server.
1612      * @throws Zend_Gdata_App_Exception
1613      * @throws Zend_Gdata_App_HttpException
1614      * @throws Zend_Gdata_Gapps_ServiceException
1615      */
1616     public function addRecipientToEmailList($recipientAddress, $emailList) {
1617         $entry = $this->newEmailListRecipientEntry();
1618         $who = $this->newWho();
1619         $who->email = $recipientAddress;
1620         $entry->who = $who;
1621         $address = $this->getBaseUrl() .  self::APPS_EMAIL_LIST_PATH . '/' .
1622             $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/';
1623         return $this->insertEmailListRecipient($entry, $address);
1624     }
1625 
1626     /**
1627      * Retrieve a page of email list recipients in alphabetical order,
1628      * starting with the provided email list recipient.
1629      *
1630      * @param string $emaiList The email list which should be searched.
1631      * @param string $startRecipient (optinal) The address of the first
1632      *              recipient, or null to start with the first recipient in
1633      *              the list.
1634      * @return Zend_Gdata_Gapps_EmailListRecipientFeed Collection of
1635      *              Zend_Gdata_EmailListRecipientEntry objects representing all
1636      *              recpients in the specified list.
1637      * @throws Zend_Gdata_App_Exception
1638      * @throws Zend_Gdata_App_HttpException
1639      * @throws Zend_Gdata_Gapps_ServiceException
1640      */
1641     public function retrievePageOfRecipients ($emailList,
1642             $startRecipient = null) {
1643         $query = $this->newEmailListRecipientQuery();
1644         $query->setEmailListName($emailList);
1645         $query->setStartRecipient($startRecipient);
1646         return $this->getEmailListRecipientFeed($query);
1647     }
1648 
1649     /**
1650      * Retrieve all recipients associated with an email list. Be aware that
1651      * calling this function on a domain with many email lists will take a
1652      * signifigant amount of time to complete. On larger domains this may
1653      * may cause execution to timeout without proper precautions in place.
1654      *
1655      * @param string $emaiList The email list which should be searched.
1656      * @return Zend_Gdata_Gapps_EmailListRecipientFeed The list of email lists
1657      *              found as Zend_Gdata_Gapps_EmailListRecipientEntry objects.
1658      * @throws Zend_Gdata_App_Exception
1659      * @throws Zend_Gdata_App_HttpException
1660      * @throws Zend_Gdata_Gapps_ServiceException
1661      */
1662     public function retrieveAllRecipients($emailList) {
1663         return $this->retrieveAllEntriesForFeed(
1664                 $this->retrievePageOfRecipients($emailList));
1665     }
1666 
1667     /**
1668      * Remove a specified recipient from an email list.
1669      *
1670      * @param string $recipientAddress The recipient to be removed.
1671      * @param string $emailList The list from which the recipient should
1672      *              be removed.
1673      * @throws Zend_Gdata_App_Exception
1674      * @throws Zend_Gdata_App_HttpException
1675      * @throws Zend_Gdata_Gapps_ServiceException
1676      */
1677     public function removeRecipientFromEmailList($recipientAddress, $emailList) {
1678         $this->delete($this->getBaseUrl() . self::APPS_EMAIL_LIST_PATH . '/'
1679             . $emailList . self::APPS_EMAIL_LIST_RECIPIENT_POSTFIX . '/'
1680             . $recipientAddress);
1681     }
1682 
1683 }