= 5.1.4 * * You can run this sample both from the command line (CLI) and also * from a web browser. Run this script without any command line options to * see usage, eg: * /usr/bin/env php Gapps.php * * More information on the Command Line Interface is available at: * http://www.php.net/features.commandline * * When running this code from a web browser, be sure to fill in your * Google Apps credentials below and choose a password for authentication * via the web browser. * * Since this is a demo, only minimal error handling and input validation * are performed. THIS CODE IS FOR DEMONSTRATION PURPOSES ONLY. NOT TO BE * USED IN A PRODUCTION ENVIRONMENT. * * NOTE: You must ensure that Zend Framework is in your PHP include * path. You can do this via php.ini settings, or by modifying the * argument to set_include_path in the code below. */ // ************************ BEGIN WWW CONFIGURATION ************************ /** * Google Apps username. This is the username (without domain) used * to administer your Google Apps account. This value is only * used when accessing this demo on a web server. * * For example, if you login to Google Apps as 'foo@bar.com.inavlid', * your username is 'foo'. */ define('GAPPS_USERNAME', 'username'); /** * Google Apps domain. This is the domain associated with your * Google Apps account. This value is only used when accessing this demo * on a web server. * * For example, if you login to Google Apps as foo@bar.com.inavlid, * your domain is 'bar.com.invalid'. */ define('GAPPS_DOMAIN', 'example.com.invalid'); /** * Google Apps password. This is the password associated with the above * username. This value is only used when accessing this demo on a * web server. */ define('GAPPS_PASSWORD', 'your password here'); /** * Login password. This password is used to protect your account from * unauthorized access when running this demo on a web server. * * If this field is blank, all access will be denied. A blank password * field is not the same as no password (which is disallowed for * security reasons). * * NOTE: While we could technically just ask the user for their Google Apps * credentials, the ClientLogin API is not intended for direct use by * web applications. If you are the only user of the application, this * is fine--- but you should not ask other users to enter their * credentials via your web application. */ define('LOGIN_PASSWORD', ''); // ************************* END WWW CONFIGURATION ************************* /** * @see Zend_Loader */ require_once 'Zend/Loader.php'; /** * @see Zend_Gdata */ Zend_Loader::loadClass('Zend_Gdata'); /** * @see Zend_Gdata_ClientLogin */ Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); /** * @see Zend_Gdata_Gapps */ Zend_Loader::loadClass('Zend_Gdata_Gapps'); /** * Returns a HTTP client object with the appropriate headers for communicating * with Google using the ClientLogin credentials supplied. * * @param string $user The username, in e-mail address format, to authenticate * @param string $pass The password for the user specified * @return Zend_Http_Client */ function getClientLoginHttpClient($user, $pass) { $service = Zend_Gdata_Gapps::AUTH_SERVICE_NAME; $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service); return $client; } /** * Creates a new user for the current domain. The user will be created * without admin privileges. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The desired username for the user. * @param string $givenName The given name for the user. * @param string $familyName The family name for the user. * @param string $password The plaintext password for the user. * @return void */ function createUser($gapps, $html, $username, $givenName, $familyName, $password) { if ($html) {echo "

Create User

\n";} $gapps->createUser($username, $givenName, $familyName, $password); if ($html) {echo "

Done.

\n";} } /** * Retrieves a user for the current domain by username. Information about * that user is then output. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The desired username for the user. * @return void */ function retrieveUser($gapps, $html, $username) { if ($html) {echo "

User Information

\n";} $user = $gapps->retrieveUser($username); if ($html) {echo '

';} if ($user !== null) { echo ' Username: ' . $user->login->username; if ($html) {echo '
';} echo "\n"; echo ' Given Name: '; if ($html) { echo htmlspecialchars($user->name->givenName); } else { echo $user->name->givenName; } if ($html) {echo '
';} echo "\n"; echo ' Family Name: '; if ($html) { echo htmlspecialchars($user->name->familyName); } else { echo $user->name->familyName; } if ($html) {echo '
';} echo "\n"; echo ' Suspended: ' . ($user->login->suspended ? 'Yes' : 'No'); if ($html) {echo '
';} echo "\n"; echo ' Admin: ' . ($user->login->admin ? 'Yes' : 'No'); if ($html) {echo '
';} echo "\n"; echo ' Must Change Password: ' . ($user->login->changePasswordAtNextLogin ? 'Yes' : 'No'); if ($html) {echo '
';} echo "\n"; echo ' Has Agreed To Terms: ' . ($user->login->agreedToTerms ? 'Yes' : 'No'); } else { echo 'Error: Specified user not found.'; } if ($html) {echo '

';} echo "\n"; } /** * Retrieves the list of users for the current domain and outputs * that list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @return void */ function retrieveAllUsers($gapps, $html) { if ($html) {echo "

Registered Users

\n";} $feed = $gapps->retrieveAllUsers(); if ($html) {echo "\n";} } /** * Change the name for an existing user. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated * @param string $newGivenName The new given name for the user. * @param string $newFamilyName The new family name for the user. * @return void */ function updateUserName($gapps, $html, $username, $newGivenName, $newFamilyName) { if ($html) {echo "

Update User Name

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->name->givenName = $newGivenName; $user->name->familyName = $newFamilyName; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Change the password for an existing user. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated * @param string $newPassword The new password for the user. * @return void */ function updateUserPassword($gapps, $html, $username, $newPassword) { if ($html) {echo "

Update User Password

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->password = $newPassword; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Suspend a given user. The user will not be able to login until restored. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function suspendUser($gapps, $html, $username) { if ($html) {echo "

Suspend User

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->suspended = true; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Restore a given user after being suspended. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function restoreUser($gapps, $html, $username) { if ($html) {echo "

Restore User

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->suspended = false; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Give a user admin rights. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function giveUserAdminRights($gapps, $html, $username) { if ($html) {echo "

Grant Administrative Rights

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->admin = true; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Revoke a user's admin rights. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function revokeUserAdminRights($gapps, $html, $username) { if ($html) {echo "

Revoke Administrative Rights

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->admin = false; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Force a user to change their password at next login. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function setUserMustChangePassword($gapps, $html, $username) { if ($html) {echo "

Force User To Change Password

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->changePasswordAtNextLogin = true; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Undo forcing a user to change their password at next login. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be updated. * @return void */ function clearUserMustChangePassword($gapps, $html, $username) { if ($html) {echo "

Undo Force User To Change Password

\n";} $user = $gapps->retrieveUser($username); if ($user !== null) { $user->login->changePasswordAtNextLogin = false; $user->save(); } else { if ($html) {echo '

';} echo 'Error: Specified user not found.'; if ($html) {echo '

';} echo "\n"; } if ($html) {echo "

Done.

\n";} } /** * Delete the user who owns a given username. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username which should be deleted. * @return void */ function deleteUser($gapps, $html, $username) { if ($html) {echo "

Delete User

\n";} $gapps->deleteUser($username); if ($html) {echo "

Done.

\n";} } /** * Create a new nickname. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username to which the nickname should be assigned. * @param string $nickname The name of the nickname to be created. * @return void */ function createNickname($gapps, $html, $username, $nickname) { if ($html) {echo "

Create Nickname

\n";} $gapps->createNickname($username, $nickname); if ($html) {echo "

Done.

\n";} } /** * Retrieve a specified nickname and output its ownership information. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $nickname The name of the nickname to be retrieved. * @return void */ function retrieveNickname($gapps, $html, $nickname) { if ($html) {echo "

Nickname Information

\n";} $nickname = $gapps->retrieveNickname($nickname); if ($html) {echo '

';} if ($nickname !== null) { echo ' Nickname: ' . $nickname->nickname->name; if ($html) {echo '
';} echo "\n"; echo ' Owner: ' . $nickname->login->username; } else { echo 'Error: Specified nickname not found.'; } if ($html) {echo '

';} echo "\n"; } /** * Outputs all nicknames owned by a specific username. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $username The username whose nicknames should be displayed. * @return void */ function retrieveNicknames($gapps, $html, $username) { if ($html) {echo "

Registered Nicknames For {$username}

\n";} $feed = $gapps->retrieveNicknames($username); if ($html) {echo "\n";} } /** * Retrieves the list of nicknames for the current domain and outputs * that list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @return void */ function retrieveAllNicknames($gapps, $html) { if ($html) {echo "

Registered Nicknames

\n";} $feed = $gapps->retrieveAllNicknames(); if ($html) {echo "\n";} } /** * Delete's a specific nickname from the current domain. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $nickname The nickname that should be deleted. * @return void */ function deleteNickname($gapps, $html, $nickname) { if ($html) {echo "

Delete Nickname

\n";} $gapps->deleteNickname($nickname); if ($html) {echo "

Done.

\n";} } /** * Create a new email list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $emailList The name of the email list to be created. * @return void */ function createEmailList($gapps, $html, $emailList) { if ($html) {echo "

Create Email List

\n";} $gapps->createEmailList($emailList); if ($html) {echo "

Done.

\n";} } /** * Outputs the list of email lists to which the specified address is * subscribed. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $recipient The email address of the recipient whose subscriptions should * be retrieved. Only a username is required if the recipient is a * member of the current domain. * @return void */ function retrieveEmailLists($gapps, $html, $recipient) { if ($html) {echo "

Email List Subscriptions For {$recipient}

\n";} $feed = $gapps->retrieveEmailLists($recipient); if ($html) {echo "\n";} } /** * Outputs the list of all email lists on the current domain. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @return void */ function retrieveAllEmailLists($gapps, $html) { if ($html) {echo "

Registered Email Lists

\n";} $feed = $gapps->retrieveAllEmailLists(); if ($html) {echo "\n";} } /** * Delete's a specific email list from the current domain. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $emailList The email list that should be deleted. * @return void */ function deleteEmailList($gapps, $html, $emailList) { if ($html) {echo "

Delete Email List

\n";} $gapps->deleteEmailList($emailList); if ($html) {echo "

Done.

\n";} } /** * Add a recipient to an existing email list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the * Google Apps server. * @param boolean $html True if output should be formatted for display in a * web browser. * @param string $recipientAddress The address of the recipient who should be added. * @param string $emailList The name of the email address the recipient be added to. * @return void */ function addRecipientToEmailList($gapps, $html, $recipientAddress, $emailList) { if ($html) {echo "

Subscribe Recipient

\n";} $gapps->addRecipientToEmailList($recipientAddress, $emailList); if ($html) {echo "

Done.

\n";} } /** * Outputs the list of all recipients for a given email list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google * Apps server. * @param boolean $html True if output should be formatted for display in a web browser. * @param string $emailList The email list whose recipients should be output. * @return void */ function retrieveAllRecipients($gapps, $html, $emailList) { if ($html) {echo "

Email List Recipients For {$emailList}

\n";} $feed = $gapps->retrieveAllRecipients($emailList); if ($html) {echo "\n";} } /** * Remove an existing recipient from an email list. * * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the * Google Apps server. * @param boolean $html True if output should be formatted for display in a * web browser. * @param string $recipientAddress The address of the recipient who should be removed. * @param string $emailList The email list from which the recipient should be removed. * @return void */ function removeRecipientFromEmailList($gapps, $html, $recipientAddress, $emailList) { if ($html) {echo "

Unsubscribe Recipient

\n";} $gapps->removeRecipientFromEmailList($recipientAddress, $emailList); if ($html) {echo "

Done.

\n";} } // ************************ BEGIN CLI SPECIFIC CODE ************************ /** * Display list of valid commands. * * @param string $executable The name of the current script. This is usually available as $argv[0]. * @return void */ function displayHelp($executable) { echo "Usage: php {$executable} [] [] " . "[ ...]\n\n"; echo "Possible action values include:\n" . "createUser\n" . "retrieveUser\n" . "retrieveAllUsers\n" . "updateUserName\n" . "updateUserPassword\n" . "suspendUser\n" . "restoreUser\n" . "giveUserAdminRights\n" . "revokeUserAdminRights\n" . "setUserMustChangePassword\n" . "clearUserMustChangePassword\n" . "deleteUser\n" . "createNickname\n" . "retrieveNickname\n" . "retrieveNicknames\n" . "retrieveAllNicknames\n" . "deleteNickname\n" . "createEmailList\n" . "retrieveEmailLists\n" . "retrieveAllEmailLists\n" . "deleteEmailList\n" . "addRecipientToEmailList\n" . "retrieveAllRecipients\n" . "removeRecipientFromEmailList\n"; } /** * Parse command line arguments and execute appropriate function when * running from the command line. * * If no arguments are provided, usage information will be provided. * * @param array $argv The array of command line arguments provided by PHP. * $argv[0] should be the current executable name or '-' if not available. * @param integer $argc The size of $argv. * @return void */ function runCLIVersion($argv, $argc) { if (isset($argc) && $argc >= 2) { # Prepare a server connection if ($argc >= 5) { try { $client = getClientLoginHttpClient($argv[2] . '@' . $argv[3], $argv[4]); $gapps = new Zend_Gdata_Gapps($client, $argv[3]); } catch (Zend_Gdata_App_AuthException $e) { echo "Error: Unable to authenticate. Please check your credentials.\n"; exit(1); } } # Dispatch arguments to the desired method switch ($argv[1]) { case 'createUser': if ($argc == 9) { createUser($gapps, false, $argv[5], $argv[6], $argv[7], $argv[8]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "This creates a new user with the given username.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe John Doe p4ssw0rd\n"; } break; case 'retrieveUser': if ($argc == 6) { retrieveUser($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "This retrieves the user with the specified " . "username and displays information about that user.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'retrieveAllUsers': if ($argc == 5) { retrieveAllUsers($gapps, false); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "This lists all users on the current domain.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} \n"; } break; case 'updateUserName': if ($argc == 8) { updateUserName($gapps, false, $argv[5], $argv[6], $argv[7]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "Renames an existing user.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe Jane Doe\n"; } break; case 'updateUserPassword': if ($argc == 7) { updateUserPassword($gapps, false, $argv[5], $argv[6]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "Changes the password for an existing user.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe password1\n"; } break; case 'suspendUser': if ($argc == 6) { suspendUser($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "This suspends the given user.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'restoreUser': if ($argc == 6) { restoreUser($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "This restores the given user after being suspended.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'giveUserAdminRights': if ($argc == 6) { giveUserAdminRights($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Give a user admin rights for this domain.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'revokeUserAdminRights': if ($argc == 6) { revokeUserAdminRights($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Remove a user's admin rights for this domain.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'setUserMustChangePassword': if ($argc == 6) { setUserMustChangePassword($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Force a user to change their password at next login.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'clearUserMustChangePassword': if ($argc == 6) { clearUserMustChangePassword($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Clear the flag indicating that a user must change " . "their password at next login.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'deleteUser': if ($argc == 6) { deleteUser($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Delete the user who owns a given username.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'createNickname': if ($argc == 7) { createNickname($gapps, false, $argv[5], $argv[6]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "Create a new nickname for the specified user.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe johnny\n"; } break; case 'retrieveNickname': if ($argc == 6) { retrieveNickname($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Retrieve a nickname and display its ownership " . "information.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "johnny\n"; } break; case 'retrieveNicknames': if ($argc == 6) { retrieveNicknames($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Output all nicknames owned by a specific username.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "jdoe\n"; } break; case 'retrieveAllNicknames': if ($argc == 5) { retrieveAllNicknames($gapps, false); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Output all registered nicknames on the system.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "\n"; } break; case 'deleteNickname': if ($argc == 6) { deleteNickname($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Delete a specific nickname.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "johnny\n"; } break; case 'createEmailList': if ($argc == 6) { createEmailList($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Create a new email list with the specified name.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "friends\n"; } break; case 'retrieveEmailLists': if ($argc == 6) { retrieveEmailLists($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Retrieve all email lists to which the specified " . "address is subscribed.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "johnny@somewhere.com.invalid\n"; } break; case 'retrieveAllEmailLists': if ($argc == 5) { retrieveAllEmailLists($gapps, false); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Retrieve a list of all email lists on the current " . "domain.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "\n"; } break; case 'deleteEmailList': if ($argc == 6) { deleteEmailList($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Delete a specified email list.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "friends\n"; } break; case 'addRecipientToEmailList': if ($argc == 7) { addRecipientToEmailList($gapps, false, $argv[5], $argv[6]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "Add a recipient to an existing email list.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "johnny@somewhere.com.invalid friends\n"; } break; case 'retrieveAllRecipients': if ($argc == 6) { retrieveAllRecipients($gapps, false, $argv[5]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . "\n\n"; echo "Retrieve all recipients for an existing email list.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "friends\n"; } break; case 'removeRecipientFromEmailList': if ($argc == 7) { removeRecipientFromEmailList($gapps, false, $argv[5], $argv[6]); } else { echo "Usage: php {$argv[0]} {$argv[1]} " . " \n\n"; echo "Remove an existing recipient from an email list.\n"; echo "EXAMPLE: php {$argv[0]} {$argv[1]} " . "johnny@somewhere.com.invalid friends\n"; } break; default: // Invalid action entered displayHelp($argv[0]); // End switch block } } else { // action left unspecified displayHelp($argv[0]); } } // ************************ BEGIN WWW SPECIFIC CODE ************************ /** * Writes the HTML prologue for this app. * * NOTE: We would normally keep the HTML/CSS markup separate from the business * logic above, but have decided to include it here for simplicity of * having a single-file sample. * * * @param boolean $displayMenu (optional) If set to true, a navigation * menu is displayed at the top of the page. Default is true. * @return void */ function startHTML($displayMenu = true) { ?> Google Apps Provisioning API Demo
← Back'; } ?>

Almost there...

Before using this demo, you must set an application password to protect your account. You will also need to set your Google Apps credentials in order to communicate with the Google Apps servers.

To continue, open this file in a text editor and fill out the information in the configuration section.

Google Apps Authentication Failed

Authentication with the Google Apps servers failed.

Please open this file in a text editor and make sure your credentials are correct.

Authentication Required

' . $errorText . "\n"; } ?>

Please enter your login password to continue.

Notice: This application is for demonstration purposes only. Not for use in a production environment.

Main Menu

Welcome to the Google Apps Provisioning API demo page. Please select from one of the following three options to see a list of commands.

Tip: You can also run this demo from the command line if your system has PHP CLI support enabled.

User Maintenance Menu

Create User

Create a new user with the given properties.




Retrieve User

Retrieve the information for an existing user.


Retrieve All Users

Retrieve the list of all users on the current domain.

Update Name

Update the name for an existing user.




Update Password

Update the password for an existing user.


Suspend/Restore User

Mark an existing user as suspended or restore a suspended user. While suspended, the user will be prohibited from logging into this domain.

User may log into this domain.
User may not log into this domain.

Issue/Revoke Admin Rights

Set whether an existing user has administrative rights for the current domain.

User may administer this domain.
User may not administer this domain.

Force User To Change Password

Set whether an existing user must change their password at their next login.

User is required to change their password at next login.
User is not required to change their password at next login.

Delete User

Delete an existing user on the current domain.


Nickname Maintenance Menu

Create Nickname

Create a nickname for an existing user.



Retrieve Nickname

Retrieve the information for an existing nickname.


Retrieve Nicknames

Retrieve the nicknames associated with an existing username.


Retrieve All Nicknames

Retrieve the nicknames on the current domain.

Delete Nickname

Delete an existing nickname from the current domain.


Email List Maintenance Menu

Create Email List

Create a new email list for the current domain.


Retrieve Email Lists

Retrieve all email lists to which a given email address is subscribed.


Retrieve All Email Lists

Retrieve all email lists on the current domain.

Delete Email List

Delete an existing email list from the current domain.


Add Recipient To Email List

Add or remove a recipient from an existing email list. A complete email address is required for recipients outside the current domain.



Subscribe recipient.
Unsubscribe recipient.

Retrieve All Recipients

Retrieve all recipients subscribed to an existing email list.


Logout

Logout successful.

Invalid mode.\n"; echo "

Please check your request and try again.

"; endHTML(true); } case 'setUserAdmin': if ($_POST['mode'] == 'issue') { startHTML(); giveUserAdminRights($gapps, true, $_POST['user']); endHTML(true); } elseif ($_POST['mode'] == 'revoke') { startHTML(); revokeUserAdminRights($gapps, true, $_POST['user']); endHTML(true); } else { header('HTTP/1.1 400 Bad Request'); startHTML(); echo "

Invalid mode.

\n"; echo "

Please check your request and try again.

"; endHTML(true); } case 'setForceChangePassword': if ($_POST['mode'] == 'set') { startHTML(); setUserMustChangePassword($gapps, true, $_POST['user']); endHTML(true); } elseif ($_POST['mode'] == 'clear') { startHTML(); clearUserMustChangePassword($gapps, true, $_POST['user']); endHTML(true); } else { header('HTTP/1.1 400 Bad Request'); startHTML(); echo "

Invalid mode.

\n"; echo "

Please check your request and try again.

"; endHTML(true); } case 'deleteUser': startHTML(); deleteUser($gapps, true, $_POST['user']); endHTML(true); case 'createNickname': startHTML(); createNickname($gapps, true, $_POST['user'], $_POST['nickname']); endHTML(true); case 'deleteNickname': startHTML(); deleteNickname($gapps, true, $_POST['nickname']); endHTML(true); case 'createEmailList': startHTML(); createEmailList($gapps, true, $_POST['emailList']); endHTML(true); case 'deleteEmailList': startHTML(); deleteEmailList($gapps, true, $_POST['emailList']); endHTML(true); case 'modifySubscription': if ($_POST['mode'] == 'subscribe') { startHTML(); addRecipientToEmailList($gapps, true, $_POST['recipient'], $_POST['emailList']); endHTML(true); } elseif ($_POST['mode'] == 'unsubscribe') { startHTML(); removeRecipientFromEmailList($gapps, true, $_POST['recipient'], $_POST['emailList']); endHTML(true); } else { header('HTTP/1.1 400 Bad Request'); startHTML(); echo "

Invalid mode.

\n"; echo "

Please check your request and try again.

"; endHTML(true); } } } // Check for an invalid command. If so, display an error and exit. if (!empty($_REQUEST['command'])) { header('HTTP/1.1 400 Bad Request'); startHTML(); echo "

Invalid command.

\n"; echo "

Please check your request and try again.

"; endHTML(true); } // If a menu parameter is available, display a submenu. if (!empty($_REQUEST['menu'])) { switch ($_REQUEST['menu']) { case 'user': startHTML(); displayUserMenu(); endHTML(); case 'nickname': startHTML(); displayNicknameMenu(); endHTML(); case 'emailList': startHTML(); displayEmailListMenu(); endHTML(); case 'logout': startHTML(false); logout(); endHTML(); default: header('HTTP/1.1 400 Bad Request'); startHTML(); echo "

Invalid menu selection.

\n"; echo "

Please check your request and try again.

"; endHTML(true); } } // If we get this far, that means there's nothing to do. Display // the main menu. // If no command was issued and no menu was selected, display the // main menu. startHTML(); displayMenu(); endHTML(); } // ************************** PROGRAM ENTRY POINT ************************** if (!isset($_SERVER["HTTP_HOST"])) { // running through command line runCLIVersion($argv, $argc); } else { // running through web server try { runWWWVersion(); } catch (Zend_Gdata_Gapps_ServiceException $e) { // Try to recover gracefully from a service exception. // The HTML prologue will have already been sent. echo "

Service Error Encountered

\n"; echo "
" . htmlspecialchars($e->__toString()) . "
"; endHTML(true); } }