= 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 "
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 "';}
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 '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 "';} 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 "';} 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 "';} 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 "';} 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 "';} 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 "';} 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 "';} 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 "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 "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 "';}
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 '
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 "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 "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 "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 "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}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.
Authentication with the Google Apps servers failed.
Please open this file in a text editor and make sure your credentials are correct.
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.
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 "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 "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 "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 "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 "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); } }