PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/public/zf-demos/Zend/Gdata/Gapps.php

https://bitbucket.org/hieronim1981/tunethemusic
PHP | 1992 lines | 1502 code | 121 blank | 369 comment | 183 complexity | 29cd901faa83329e30bd85e22be50995 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * PHP sample code for the Google Calendar data API. Utilizes the
  23. * Zend Framework Gdata components to communicate with the Google API.
  24. *
  25. * Requires the Zend Framework Gdata components and PHP >= 5.1.4
  26. *
  27. * You can run this sample both from the command line (CLI) and also
  28. * from a web browser. Run this script without any command line options to
  29. * see usage, eg:
  30. * /usr/bin/env php Gapps.php
  31. *
  32. * More information on the Command Line Interface is available at:
  33. * http://www.php.net/features.commandline
  34. *
  35. * When running this code from a web browser, be sure to fill in your
  36. * Google Apps credentials below and choose a password for authentication
  37. * via the web browser.
  38. *
  39. * Since this is a demo, only minimal error handling and input validation
  40. * are performed. THIS CODE IS FOR DEMONSTRATION PURPOSES ONLY. NOT TO BE
  41. * USED IN A PRODUCTION ENVIRONMENT.
  42. *
  43. * NOTE: You must ensure that Zend Framework is in your PHP include
  44. * path. You can do this via php.ini settings, or by modifying the
  45. * argument to set_include_path in the code below.
  46. */
  47. // ************************ BEGIN WWW CONFIGURATION ************************
  48. /**
  49. * Google Apps username. This is the username (without domain) used
  50. * to administer your Google Apps account. This value is only
  51. * used when accessing this demo on a web server.
  52. *
  53. * For example, if you login to Google Apps as 'foo@bar.com.inavlid',
  54. * your username is 'foo'.
  55. */
  56. define('GAPPS_USERNAME', 'username');
  57. /**
  58. * Google Apps domain. This is the domain associated with your
  59. * Google Apps account. This value is only used when accessing this demo
  60. * on a web server.
  61. *
  62. * For example, if you login to Google Apps as foo@bar.com.inavlid,
  63. * your domain is 'bar.com.invalid'.
  64. */
  65. define('GAPPS_DOMAIN', 'example.com.invalid');
  66. /**
  67. * Google Apps password. This is the password associated with the above
  68. * username. This value is only used when accessing this demo on a
  69. * web server.
  70. */
  71. define('GAPPS_PASSWORD', 'your password here');
  72. /**
  73. * Login password. This password is used to protect your account from
  74. * unauthorized access when running this demo on a web server.
  75. *
  76. * If this field is blank, all access will be denied. A blank password
  77. * field is not the same as no password (which is disallowed for
  78. * security reasons).
  79. *
  80. * NOTE: While we could technically just ask the user for their Google Apps
  81. * credentials, the ClientLogin API is not intended for direct use by
  82. * web applications. If you are the only user of the application, this
  83. * is fine--- but you should not ask other users to enter their
  84. * credentials via your web application.
  85. */
  86. define('LOGIN_PASSWORD', '');
  87. // ************************* END WWW CONFIGURATION *************************
  88. /**
  89. * @see Zend_Loader
  90. */
  91. require_once 'Zend/Loader.php';
  92. /**
  93. * @see Zend_Gdata
  94. */
  95. Zend_Loader::loadClass('Zend_Gdata');
  96. /**
  97. * @see Zend_Gdata_ClientLogin
  98. */
  99. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  100. /**
  101. * @see Zend_Gdata_Gapps
  102. */
  103. Zend_Loader::loadClass('Zend_Gdata_Gapps');
  104. /**
  105. * Returns a HTTP client object with the appropriate headers for communicating
  106. * with Google using the ClientLogin credentials supplied.
  107. *
  108. * @param string $user The username, in e-mail address format, to authenticate
  109. * @param string $pass The password for the user specified
  110. * @return Zend_Http_Client
  111. */
  112. function getClientLoginHttpClient($user, $pass)
  113. {
  114. $service = Zend_Gdata_Gapps::AUTH_SERVICE_NAME;
  115. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  116. return $client;
  117. }
  118. /**
  119. * Creates a new user for the current domain. The user will be created
  120. * without admin privileges.
  121. *
  122. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  123. * Apps server.
  124. * @param boolean $html True if output should be formatted for display in a web browser.
  125. * @param string $username The desired username for the user.
  126. * @param string $givenName The given name for the user.
  127. * @param string $familyName The family name for the user.
  128. * @param string $password The plaintext password for the user.
  129. * @return void
  130. */
  131. function createUser($gapps, $html, $username, $givenName, $familyName,
  132. $password)
  133. {
  134. if ($html) {echo "<h2>Create User</h2>\n";}
  135. $gapps->createUser($username, $givenName, $familyName,
  136. $password);
  137. if ($html) {echo "<p>Done.</p>\n";}
  138. }
  139. /**
  140. * Retrieves a user for the current domain by username. Information about
  141. * that user is then output.
  142. *
  143. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google Apps server.
  144. * @param boolean $html True if output should be formatted for display in a web browser.
  145. * @param string $username The desired username for the user.
  146. * @return void
  147. */
  148. function retrieveUser($gapps, $html, $username)
  149. {
  150. if ($html) {echo "<h2>User Information</h2>\n";}
  151. $user = $gapps->retrieveUser($username);
  152. if ($html) {echo '<p>';}
  153. if ($user !== null) {
  154. echo ' Username: ' . $user->login->username;
  155. if ($html) {echo '<br />';}
  156. echo "\n";
  157. echo ' Given Name: ';
  158. if ($html) {
  159. echo htmlspecialchars($user->name->givenName);
  160. } else {
  161. echo $user->name->givenName;
  162. }
  163. if ($html) {echo '<br />';}
  164. echo "\n";
  165. echo ' Family Name: ';
  166. if ($html) {
  167. echo htmlspecialchars($user->name->familyName);
  168. } else {
  169. echo $user->name->familyName;
  170. }
  171. if ($html) {echo '<br />';}
  172. echo "\n";
  173. echo ' Suspended: ' . ($user->login->suspended ? 'Yes' : 'No');
  174. if ($html) {echo '<br />';}
  175. echo "\n";
  176. echo ' Admin: ' . ($user->login->admin ? 'Yes' : 'No');
  177. if ($html) {echo '<br />';}
  178. echo "\n";
  179. echo ' Must Change Password: ' .
  180. ($user->login->changePasswordAtNextLogin ? 'Yes' : 'No');
  181. if ($html) {echo '<br />';}
  182. echo "\n";
  183. echo ' Has Agreed To Terms: ' .
  184. ($user->login->agreedToTerms ? 'Yes' : 'No');
  185. } else {
  186. echo 'Error: Specified user not found.';
  187. }
  188. if ($html) {echo '</p>';}
  189. echo "\n";
  190. }
  191. /**
  192. * Retrieves the list of users for the current domain and outputs
  193. * that list.
  194. *
  195. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google Apps server.
  196. * @param boolean $html True if output should be formatted for display in a web browser.
  197. * @return void
  198. */
  199. function retrieveAllUsers($gapps, $html)
  200. {
  201. if ($html) {echo "<h2>Registered Users</h2>\n";}
  202. $feed = $gapps->retrieveAllUsers();
  203. if ($html) {echo "<ul>\n";}
  204. foreach ($feed as $user) {
  205. if ($html) {
  206. echo " <li>";
  207. } else {
  208. echo " * ";
  209. }
  210. echo $user->login->username . ' (';
  211. if ($html) {
  212. echo htmlspecialchars($user->name->givenName . ' ' .
  213. $user->name->familyName);
  214. } else {
  215. echo $user->name->givenName . ' ' . $user->name->familyName;
  216. }
  217. echo ')';
  218. if ($html) {echo '</li>';}
  219. echo "\n";
  220. }
  221. if ($html) {echo "</ul>\n";}
  222. }
  223. /**
  224. * Change the name for an existing user.
  225. *
  226. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  227. * Apps server.
  228. * @param boolean $html True if output should be formatted for display in a web browser.
  229. * @param string $username The username which should be updated
  230. * @param string $newGivenName The new given name for the user.
  231. * @param string $newFamilyName The new family name for the user.
  232. * @return void
  233. */
  234. function updateUserName($gapps, $html, $username, $newGivenName, $newFamilyName)
  235. {
  236. if ($html) {echo "<h2>Update User Name</h2>\n";}
  237. $user = $gapps->retrieveUser($username);
  238. if ($user !== null) {
  239. $user->name->givenName = $newGivenName;
  240. $user->name->familyName = $newFamilyName;
  241. $user->save();
  242. } else {
  243. if ($html) {echo '<p>';}
  244. echo 'Error: Specified user not found.';
  245. if ($html) {echo '</p>';}
  246. echo "\n";
  247. }
  248. if ($html) {echo "<p>Done.</p>\n";}
  249. }
  250. /**
  251. * Change the password for an existing user.
  252. *
  253. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  254. * Apps server.
  255. * @param boolean $html True if output should be formatted for display in a web browser.
  256. * @param string $username The username which should be updated
  257. * @param string $newPassword The new password for the user.
  258. * @return void
  259. */
  260. function updateUserPassword($gapps, $html, $username, $newPassword)
  261. {
  262. if ($html) {echo "<h2>Update User Password</h2>\n";}
  263. $user = $gapps->retrieveUser($username);
  264. if ($user !== null) {
  265. $user->login->password = $newPassword;
  266. $user->save();
  267. } else {
  268. if ($html) {echo '<p>';}
  269. echo 'Error: Specified user not found.';
  270. if ($html) {echo '</p>';}
  271. echo "\n";
  272. }
  273. if ($html) {echo "<p>Done.</p>\n";}
  274. }
  275. /**
  276. * Suspend a given user. The user will not be able to login until restored.
  277. *
  278. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  279. * Apps server.
  280. * @param boolean $html True if output should be formatted for display in a web browser.
  281. * @param string $username The username which should be updated.
  282. * @return void
  283. */
  284. function suspendUser($gapps, $html, $username)
  285. {
  286. if ($html) {echo "<h2>Suspend User</h2>\n";}
  287. $user = $gapps->retrieveUser($username);
  288. if ($user !== null) {
  289. $user->login->suspended = true;
  290. $user->save();
  291. } else {
  292. if ($html) {echo '<p>';}
  293. echo 'Error: Specified user not found.';
  294. if ($html) {echo '</p>';}
  295. echo "\n";
  296. }
  297. if ($html) {echo "<p>Done.</p>\n";}
  298. }
  299. /**
  300. * Restore a given user after being suspended.
  301. *
  302. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  303. * Apps server.
  304. * @param boolean $html True if output should be formatted for display in a web browser.
  305. * @param string $username The username which should be updated.
  306. * @return void
  307. */
  308. function restoreUser($gapps, $html, $username)
  309. {
  310. if ($html) {echo "<h2>Restore User</h2>\n";}
  311. $user = $gapps->retrieveUser($username);
  312. if ($user !== null) {
  313. $user->login->suspended = false;
  314. $user->save();
  315. } else {
  316. if ($html) {echo '<p>';}
  317. echo 'Error: Specified user not found.';
  318. if ($html) {echo '</p>';}
  319. echo "\n";
  320. }
  321. if ($html) {echo "<p>Done.</p>\n";}
  322. }
  323. /**
  324. * Give a user admin rights.
  325. *
  326. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  327. * Apps server.
  328. * @param boolean $html True if output should be formatted for display in a web browser.
  329. * @param string $username The username which should be updated.
  330. * @return void
  331. */
  332. function giveUserAdminRights($gapps, $html, $username)
  333. {
  334. if ($html) {echo "<h2>Grant Administrative Rights</h2>\n";}
  335. $user = $gapps->retrieveUser($username);
  336. if ($user !== null) {
  337. $user->login->admin = true;
  338. $user->save();
  339. } else {
  340. if ($html) {echo '<p>';}
  341. echo 'Error: Specified user not found.';
  342. if ($html) {echo '</p>';}
  343. echo "\n";
  344. }
  345. if ($html) {echo "<p>Done.</p>\n";}
  346. }
  347. /**
  348. * Revoke a user's admin rights.
  349. *
  350. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  351. * Apps server.
  352. * @param boolean $html True if output should be formatted for display in a web browser.
  353. * @param string $username The username which should be updated.
  354. * @return void
  355. */
  356. function revokeUserAdminRights($gapps, $html, $username)
  357. {
  358. if ($html) {echo "<h2>Revoke Administrative Rights</h2>\n";}
  359. $user = $gapps->retrieveUser($username);
  360. if ($user !== null) {
  361. $user->login->admin = false;
  362. $user->save();
  363. } else {
  364. if ($html) {echo '<p>';}
  365. echo 'Error: Specified user not found.';
  366. if ($html) {echo '</p>';}
  367. echo "\n";
  368. }
  369. if ($html) {echo "<p>Done.</p>\n";}
  370. }
  371. /**
  372. * Force a user to change their password at next login.
  373. *
  374. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  375. * Apps server.
  376. * @param boolean $html True if output should be formatted for display in a web browser.
  377. * @param string $username The username which should be updated.
  378. * @return void
  379. */
  380. function setUserMustChangePassword($gapps, $html, $username)
  381. {
  382. if ($html) {echo "<h2>Force User To Change Password</h2>\n";}
  383. $user = $gapps->retrieveUser($username);
  384. if ($user !== null) {
  385. $user->login->changePasswordAtNextLogin = true;
  386. $user->save();
  387. } else {
  388. if ($html) {echo '<p>';}
  389. echo 'Error: Specified user not found.';
  390. if ($html) {echo '</p>';}
  391. echo "\n";
  392. }
  393. if ($html) {echo "<p>Done.</p>\n";}
  394. }
  395. /**
  396. * Undo forcing a user to change their password at next login.
  397. *
  398. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  399. * Apps server.
  400. * @param boolean $html True if output should be formatted for display in a web browser.
  401. * @param string $username The username which should be updated.
  402. * @return void
  403. */
  404. function clearUserMustChangePassword($gapps, $html, $username)
  405. {
  406. if ($html) {echo "<h2>Undo Force User To Change Password</h2>\n";}
  407. $user = $gapps->retrieveUser($username);
  408. if ($user !== null) {
  409. $user->login->changePasswordAtNextLogin = false;
  410. $user->save();
  411. } else {
  412. if ($html) {echo '<p>';}
  413. echo 'Error: Specified user not found.';
  414. if ($html) {echo '</p>';}
  415. echo "\n";
  416. }
  417. if ($html) {echo "<p>Done.</p>\n";}
  418. }
  419. /**
  420. * Delete the user who owns a given username.
  421. *
  422. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  423. * Apps server.
  424. * @param boolean $html True if output should be formatted for display in a web browser.
  425. * @param string $username The username which should be deleted.
  426. * @return void
  427. */
  428. function deleteUser($gapps, $html, $username)
  429. {
  430. if ($html) {echo "<h2>Delete User</h2>\n";}
  431. $gapps->deleteUser($username);
  432. if ($html) {echo "<p>Done.</p>\n";}
  433. }
  434. /**
  435. * Create a new nickname.
  436. *
  437. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  438. * Apps server.
  439. * @param boolean $html True if output should be formatted for display in a web browser.
  440. * @param string $username The username to which the nickname should be assigned.
  441. * @param string $nickname The name of the nickname to be created.
  442. * @return void
  443. */
  444. function createNickname($gapps, $html, $username, $nickname)
  445. {
  446. if ($html) {echo "<h2>Create Nickname</h2>\n";}
  447. $gapps->createNickname($username, $nickname);
  448. if ($html) {echo "<p>Done.</p>\n";}
  449. }
  450. /**
  451. * Retrieve a specified nickname and output its ownership information.
  452. *
  453. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  454. * Apps server.
  455. * @param boolean $html True if output should be formatted for display in a web browser.
  456. * @param string $nickname The name of the nickname to be retrieved.
  457. * @return void
  458. */
  459. function retrieveNickname($gapps, $html, $nickname)
  460. {
  461. if ($html) {echo "<h2>Nickname Information</h2>\n";}
  462. $nickname = $gapps->retrieveNickname($nickname);
  463. if ($html) {echo '<p>';}
  464. if ($nickname !== null) {
  465. echo ' Nickname: ' . $nickname->nickname->name;
  466. if ($html) {echo '<br />';}
  467. echo "\n";
  468. echo ' Owner: ' . $nickname->login->username;
  469. } else {
  470. echo 'Error: Specified nickname not found.';
  471. }
  472. if ($html) {echo '</p>';}
  473. echo "\n";
  474. }
  475. /**
  476. * Outputs all nicknames owned by a specific username.
  477. *
  478. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  479. * Apps server.
  480. * @param boolean $html True if output should be formatted for display in a web browser.
  481. * @param string $username The username whose nicknames should be displayed.
  482. * @return void
  483. */
  484. function retrieveNicknames($gapps, $html, $username)
  485. {
  486. if ($html) {echo "<h2>Registered Nicknames For {$username}</h2>\n";}
  487. $feed = $gapps->retrieveNicknames($username);
  488. if ($html) {echo "<ul>\n";}
  489. foreach ($feed as $nickname) {
  490. if ($html) {
  491. echo " <li>";
  492. } else {
  493. echo " * ";
  494. }
  495. echo $nickname->nickname->name;
  496. if ($html) {echo '</li>';}
  497. echo "\n";
  498. }
  499. if ($html) {echo "</ul>\n";}
  500. }
  501. /**
  502. * Retrieves the list of nicknames for the current domain and outputs
  503. * that list.
  504. *
  505. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  506. * Apps server.
  507. * @param boolean $html True if output should be formatted for display in a web browser.
  508. * @return void
  509. */
  510. function retrieveAllNicknames($gapps, $html)
  511. {
  512. if ($html) {echo "<h2>Registered Nicknames</h2>\n";}
  513. $feed = $gapps->retrieveAllNicknames();
  514. if ($html) {echo "<ul>\n";}
  515. foreach ($feed as $nickname) {
  516. if ($html) {
  517. echo " <li>";
  518. } else {
  519. echo " * ";
  520. }
  521. echo $nickname->nickname->name . ' => ' . $nickname->login->username;
  522. if ($html) {echo '</li>';}
  523. echo "\n";
  524. }
  525. if ($html) {echo "</ul>\n";}
  526. }
  527. /**
  528. * Delete's a specific nickname from the current domain.
  529. *
  530. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  531. * Apps server.
  532. * @param boolean $html True if output should be formatted for display in a web browser.
  533. * @param string $nickname The nickname that should be deleted.
  534. * @return void
  535. */
  536. function deleteNickname($gapps, $html, $nickname)
  537. {
  538. if ($html) {echo "<h2>Delete Nickname</h2>\n";}
  539. $gapps->deleteNickname($nickname);
  540. if ($html) {echo "<p>Done.</p>\n";}
  541. }
  542. /**
  543. * Create a new email list.
  544. *
  545. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  546. * Apps server.
  547. * @param boolean $html True if output should be formatted for display in a web browser.
  548. * @param string $emailList The name of the email list to be created.
  549. * @return void
  550. */
  551. function createEmailList($gapps, $html, $emailList)
  552. {
  553. if ($html) {echo "<h2>Create Email List</h2>\n";}
  554. $gapps->createEmailList($emailList);
  555. if ($html) {echo "<p>Done.</p>\n";}
  556. }
  557. /**
  558. * Outputs the list of email lists to which the specified address is
  559. * subscribed.
  560. *
  561. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  562. * Apps server.
  563. * @param boolean $html True if output should be formatted for display in a web browser.
  564. * @param string $recipient The email address of the recipient whose subscriptions should
  565. * be retrieved. Only a username is required if the recipient is a
  566. * member of the current domain.
  567. * @return void
  568. */
  569. function retrieveEmailLists($gapps, $html, $recipient)
  570. {
  571. if ($html) {echo "<h2>Email List Subscriptions For {$recipient}</h2>\n";}
  572. $feed = $gapps->retrieveEmailLists($recipient);
  573. if ($html) {echo "<ul>\n";}
  574. foreach ($feed as $list) {
  575. if ($html) {
  576. echo " <li>";
  577. } else {
  578. echo " * ";
  579. }
  580. echo $list->emailList->name;
  581. if ($html) {echo '</li>';}
  582. echo "\n";
  583. }
  584. if ($html) {echo "</ul>\n";}
  585. }
  586. /**
  587. * Outputs the list of all email lists on the current domain.
  588. *
  589. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  590. * Apps server.
  591. * @param boolean $html True if output should be formatted for display in a web browser.
  592. * @return void
  593. */
  594. function retrieveAllEmailLists($gapps, $html)
  595. {
  596. if ($html) {echo "<h2>Registered Email Lists</h2>\n";}
  597. $feed = $gapps->retrieveAllEmailLists();
  598. if ($html) {echo "<ul>\n";}
  599. foreach ($feed as $list) {
  600. if ($html) {
  601. echo " <li>";
  602. } else {
  603. echo " * ";
  604. }
  605. echo $list->emailList->name;
  606. if ($html) {echo '</li>';}
  607. echo "\n";
  608. }
  609. if ($html) {echo "</ul>\n";}
  610. }
  611. /**
  612. * Delete's a specific email list from the current domain.
  613. *
  614. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  615. * Apps server.
  616. * @param boolean $html True if output should be formatted for display in a web browser.
  617. * @param string $emailList The email list that should be deleted.
  618. * @return void
  619. */
  620. function deleteEmailList($gapps, $html, $emailList)
  621. {
  622. if ($html) {echo "<h2>Delete Email List</h2>\n";}
  623. $gapps->deleteEmailList($emailList);
  624. if ($html) {echo "<p>Done.</p>\n";}
  625. }
  626. /**
  627. * Add a recipient to an existing email list.
  628. *
  629. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the
  630. * Google Apps server.
  631. * @param boolean $html True if output should be formatted for display in a
  632. * web browser.
  633. * @param string $recipientAddress The address of the recipient who should be added.
  634. * @param string $emailList The name of the email address the recipient be added to.
  635. * @return void
  636. */
  637. function addRecipientToEmailList($gapps, $html, $recipientAddress,
  638. $emailList)
  639. {
  640. if ($html) {echo "<h2>Subscribe Recipient</h2>\n";}
  641. $gapps->addRecipientToEmailList($recipientAddress, $emailList);
  642. if ($html) {echo "<p>Done.</p>\n";}
  643. }
  644. /**
  645. * Outputs the list of all recipients for a given email list.
  646. *
  647. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
  648. * Apps server.
  649. * @param boolean $html True if output should be formatted for display in a web browser.
  650. * @param string $emailList The email list whose recipients should be output.
  651. * @return void
  652. */
  653. function retrieveAllRecipients($gapps, $html, $emailList)
  654. {
  655. if ($html) {echo "<h2>Email List Recipients For {$emailList}</h2>\n";}
  656. $feed = $gapps->retrieveAllRecipients($emailList);
  657. if ($html) {echo "<ul>\n";}
  658. foreach ($feed as $recipient) {
  659. if ($html) {
  660. echo " <li>";
  661. } else {
  662. echo " * ";
  663. }
  664. echo $recipient->who->email;
  665. if ($html) {echo '</li>';}
  666. echo "\n";
  667. }
  668. if ($html) {echo "</ul>\n";}
  669. }
  670. /**
  671. * Remove an existing recipient from an email list.
  672. *
  673. * @param Zend_Gdata_Gapps $gapps The service object to use for communicating with the
  674. * Google Apps server.
  675. * @param boolean $html True if output should be formatted for display in a
  676. * web browser.
  677. * @param string $recipientAddress The address of the recipient who should be removed.
  678. * @param string $emailList The email list from which the recipient should be removed.
  679. * @return void
  680. */
  681. function removeRecipientFromEmailList($gapps, $html, $recipientAddress,
  682. $emailList)
  683. {
  684. if ($html) {echo "<h2>Unsubscribe Recipient</h2>\n";}
  685. $gapps->removeRecipientFromEmailList($recipientAddress, $emailList);
  686. if ($html) {echo "<p>Done.</p>\n";}
  687. }
  688. // ************************ BEGIN CLI SPECIFIC CODE ************************
  689. /**
  690. * Display list of valid commands.
  691. *
  692. * @param string $executable The name of the current script. This is usually available as $argv[0].
  693. * @return void
  694. */
  695. function displayHelp($executable)
  696. {
  697. echo "Usage: php {$executable} <action> [<username>] [<password>] " .
  698. "[<arg1> <arg2> ...]\n\n";
  699. echo "Possible action values include:\n" .
  700. "createUser\n" .
  701. "retrieveUser\n" .
  702. "retrieveAllUsers\n" .
  703. "updateUserName\n" .
  704. "updateUserPassword\n" .
  705. "suspendUser\n" .
  706. "restoreUser\n" .
  707. "giveUserAdminRights\n" .
  708. "revokeUserAdminRights\n" .
  709. "setUserMustChangePassword\n" .
  710. "clearUserMustChangePassword\n" .
  711. "deleteUser\n" .
  712. "createNickname\n" .
  713. "retrieveNickname\n" .
  714. "retrieveNicknames\n" .
  715. "retrieveAllNicknames\n" .
  716. "deleteNickname\n" .
  717. "createEmailList\n" .
  718. "retrieveEmailLists\n" .
  719. "retrieveAllEmailLists\n" .
  720. "deleteEmailList\n" .
  721. "addRecipientToEmailList\n" .
  722. "retrieveAllRecipients\n" .
  723. "removeRecipientFromEmailList\n";
  724. }
  725. /**
  726. * Parse command line arguments and execute appropriate function when
  727. * running from the command line.
  728. *
  729. * If no arguments are provided, usage information will be provided.
  730. *
  731. * @param array $argv The array of command line arguments provided by PHP.
  732. * $argv[0] should be the current executable name or '-' if not available.
  733. * @param integer $argc The size of $argv.
  734. * @return void
  735. */
  736. function runCLIVersion($argv, $argc)
  737. {
  738. if (isset($argc) && $argc >= 2) {
  739. # Prepare a server connection
  740. if ($argc >= 5) {
  741. try {
  742. $client = getClientLoginHttpClient($argv[2] . '@' . $argv[3], $argv[4]);
  743. $gapps = new Zend_Gdata_Gapps($client, $argv[3]);
  744. } catch (Zend_Gdata_App_AuthException $e) {
  745. echo "Error: Unable to authenticate. Please check your credentials.\n";
  746. exit(1);
  747. }
  748. }
  749. # Dispatch arguments to the desired method
  750. switch ($argv[1]) {
  751. case 'createUser':
  752. if ($argc == 9) {
  753. createUser($gapps, false, $argv[5], $argv[6], $argv[7], $argv[8]);
  754. } else {
  755. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  756. "<user's username> <given name> <family name> <user's password>\n\n";
  757. echo "This creates a new user with the given username.\n";
  758. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  759. "jdoe John Doe p4ssw0rd\n";
  760. }
  761. break;
  762. case 'retrieveUser':
  763. if ($argc == 6) {
  764. retrieveUser($gapps, false, $argv[5]);
  765. } else {
  766. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  767. "<user's username>\n\n";
  768. echo "This retrieves the user with the specified " .
  769. "username and displays information about that user.\n";
  770. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  771. "jdoe\n";
  772. }
  773. break;
  774. case 'retrieveAllUsers':
  775. if ($argc == 5) {
  776. retrieveAllUsers($gapps, false);
  777. } else {
  778. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  779. "\n\n";
  780. echo "This lists all users on the current domain.\n";
  781. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password>\n";
  782. }
  783. break;
  784. case 'updateUserName':
  785. if ($argc == 8) {
  786. updateUserName($gapps, false, $argv[5], $argv[6], $argv[7]);
  787. } else {
  788. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  789. "<user's username> <new given name> <new family name>\n\n";
  790. echo "Renames an existing user.\n";
  791. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  792. "jdoe Jane Doe\n";
  793. }
  794. break;
  795. case 'updateUserPassword':
  796. if ($argc == 7) {
  797. updateUserPassword($gapps, false, $argv[5], $argv[6]);
  798. } else {
  799. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  800. "<user's username> <new user password>\n\n";
  801. echo "Changes the password for an existing user.\n";
  802. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  803. "jdoe password1\n";
  804. }
  805. break;
  806. case 'suspendUser':
  807. if ($argc == 6) {
  808. suspendUser($gapps, false, $argv[5]);
  809. } else {
  810. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  811. "<user's username>\n\n";
  812. echo "This suspends the given user.\n";
  813. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  814. "jdoe\n";
  815. }
  816. break;
  817. case 'restoreUser':
  818. if ($argc == 6) {
  819. restoreUser($gapps, false, $argv[5]);
  820. } else {
  821. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  822. "<user's username>\n\n";
  823. echo "This restores the given user after being suspended.\n";
  824. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  825. "jdoe\n";
  826. }
  827. break;
  828. case 'giveUserAdminRights':
  829. if ($argc == 6) {
  830. giveUserAdminRights($gapps, false, $argv[5]);
  831. } else {
  832. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  833. "<user's username>\n\n";
  834. echo "Give a user admin rights for this domain.\n";
  835. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  836. "jdoe\n";
  837. }
  838. break;
  839. case 'revokeUserAdminRights':
  840. if ($argc == 6) {
  841. revokeUserAdminRights($gapps, false, $argv[5]);
  842. } else {
  843. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  844. "<user's username>\n\n";
  845. echo "Remove a user's admin rights for this domain.\n";
  846. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  847. "jdoe\n";
  848. }
  849. break;
  850. case 'setUserMustChangePassword':
  851. if ($argc == 6) {
  852. setUserMustChangePassword($gapps, false, $argv[5]);
  853. } else {
  854. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  855. "<user's username>\n\n";
  856. echo "Force a user to change their password at next login.\n";
  857. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  858. "jdoe\n";
  859. }
  860. break;
  861. case 'clearUserMustChangePassword':
  862. if ($argc == 6) {
  863. clearUserMustChangePassword($gapps, false, $argv[5]);
  864. } else {
  865. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  866. "<user's username>\n\n";
  867. echo "Clear the flag indicating that a user must change " .
  868. "their password at next login.\n";
  869. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  870. "jdoe\n";
  871. }
  872. break;
  873. case 'deleteUser':
  874. if ($argc == 6) {
  875. deleteUser($gapps, false, $argv[5]);
  876. } else {
  877. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  878. "<user's username>\n\n";
  879. echo "Delete the user who owns a given username.\n";
  880. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  881. "jdoe\n";
  882. }
  883. break;
  884. case 'createNickname':
  885. if ($argc == 7) {
  886. createNickname($gapps, false, $argv[5], $argv[6]);
  887. } else {
  888. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  889. "<user's username> <nickname>\n\n";
  890. echo "Create a new nickname for the specified user.\n";
  891. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  892. "jdoe johnny\n";
  893. }
  894. break;
  895. case 'retrieveNickname':
  896. if ($argc == 6) {
  897. retrieveNickname($gapps, false, $argv[5]);
  898. } else {
  899. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  900. "<nickname>\n\n";
  901. echo "Retrieve a nickname and display its ownership " .
  902. "information.\n";
  903. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  904. "johnny\n";
  905. }
  906. break;
  907. case 'retrieveNicknames':
  908. if ($argc == 6) {
  909. retrieveNicknames($gapps, false, $argv[5]);
  910. } else {
  911. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  912. "<user's username>\n\n";
  913. echo "Output all nicknames owned by a specific username.\n";
  914. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  915. "jdoe\n";
  916. }
  917. break;
  918. case 'retrieveAllNicknames':
  919. if ($argc == 5) {
  920. retrieveAllNicknames($gapps, false);
  921. } else {
  922. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  923. "\n\n";
  924. echo "Output all registered nicknames on the system.\n";
  925. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  926. "\n";
  927. }
  928. break;
  929. case 'deleteNickname':
  930. if ($argc == 6) {
  931. deleteNickname($gapps, false, $argv[5]);
  932. } else {
  933. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  934. "<nickname>\n\n";
  935. echo "Delete a specific nickname.\n";
  936. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  937. "johnny\n";
  938. }
  939. break;
  940. case 'createEmailList':
  941. if ($argc == 6) {
  942. createEmailList($gapps, false, $argv[5]);
  943. } else {
  944. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  945. "<email list>\n\n";
  946. echo "Create a new email list with the specified name.\n";
  947. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  948. "friends\n";
  949. }
  950. break;
  951. case 'retrieveEmailLists':
  952. if ($argc == 6) {
  953. retrieveEmailLists($gapps, false, $argv[5]);
  954. } else {
  955. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  956. "<recipient>\n\n";
  957. echo "Retrieve all email lists to which the specified " .
  958. "address is subscribed.\n";
  959. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  960. "johnny@somewhere.com.invalid\n";
  961. }
  962. break;
  963. case 'retrieveAllEmailLists':
  964. if ($argc == 5) {
  965. retrieveAllEmailLists($gapps, false);
  966. } else {
  967. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  968. "\n\n";
  969. echo "Retrieve a list of all email lists on the current " .
  970. "domain.\n";
  971. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  972. "\n";
  973. }
  974. break;
  975. case 'deleteEmailList':
  976. if ($argc == 6) {
  977. deleteEmailList($gapps, false, $argv[5]);
  978. } else {
  979. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  980. "<email list>\n\n";
  981. echo "Delete a specified email list.\n";
  982. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  983. "friends\n";
  984. }
  985. break;
  986. case 'addRecipientToEmailList':
  987. if ($argc == 7) {
  988. addRecipientToEmailList($gapps, false, $argv[5], $argv[6]);
  989. } else {
  990. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  991. "<recipient> <email list>\n\n";
  992. echo "Add a recipient to an existing email list.\n";
  993. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  994. "johnny@somewhere.com.invalid friends\n";
  995. }
  996. break;
  997. case 'retrieveAllRecipients':
  998. if ($argc == 6) {
  999. retrieveAllRecipients($gapps, false, $argv[5]);
  1000. } else {
  1001. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  1002. "<email list>\n\n";
  1003. echo "Retrieve all recipients for an existing email list.\n";
  1004. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  1005. "friends\n";
  1006. }
  1007. break;
  1008. case 'removeRecipientFromEmailList':
  1009. if ($argc == 7) {
  1010. removeRecipientFromEmailList($gapps, false, $argv[5], $argv[6]);
  1011. } else {
  1012. echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  1013. "<recipient> <email list>\n\n";
  1014. echo "Remove an existing recipient from an email list.\n";
  1015. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
  1016. "johnny@somewhere.com.invalid friends\n";
  1017. }
  1018. break;
  1019. default:
  1020. // Invalid action entered
  1021. displayHelp($argv[0]);
  1022. // End switch block
  1023. }
  1024. } else {
  1025. // action left unspecified
  1026. displayHelp($argv[0]);
  1027. }
  1028. }
  1029. // ************************ BEGIN WWW SPECIFIC CODE ************************
  1030. /**
  1031. * Writes the HTML prologue for this app.
  1032. *
  1033. * NOTE: We would normally keep the HTML/CSS markup separate from the business
  1034. * logic above, but have decided to include it here for simplicity of
  1035. * having a single-file sample.
  1036. *
  1037. *
  1038. * @param boolean $displayMenu (optional) If set to true, a navigation
  1039. * menu is displayed at the top of the page. Default is true.
  1040. * @return void
  1041. */
  1042. function startHTML($displayMenu = true)
  1043. {
  1044. ?>
  1045. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  1046. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  1047. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  1048. <head>
  1049. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  1050. <title>Google Apps Provisioning API Demo</title>
  1051. <style type="text/css" media="screen">
  1052. body {
  1053. font-family: Arial, Helvetica, sans-serif;
  1054. font-size: small;
  1055. }
  1056. #header {
  1057. background-color: #9cF;
  1058. -moz-border-radius: 5px;
  1059. -webkit-border-radius: 5px;
  1060. padding-left: 5px;
  1061. height: 2.4em;
  1062. }
  1063. #header h1 {
  1064. width: 49%;
  1065. display: inline;
  1066. float: left;
  1067. margin: 0;
  1068. padding: 0;
  1069. font-size: 2em;
  1070. }
  1071. #header p {
  1072. width: 49%;
  1073. margin: 0;
  1074. padding-right: 15px;
  1075. float: right;
  1076. line-height: 2.4em;
  1077. text-align: right;
  1078. }
  1079. .clear {
  1080. clear:both;
  1081. }
  1082. h2 {
  1083. background-color: #ccc;
  1084. -moz-border-radius: 5px;
  1085. -webkit-border-radius: 5px;
  1086. margin-top: 1em;
  1087. padding-left: 5px;
  1088. }
  1089. .error {
  1090. color: red;
  1091. }
  1092. form {
  1093. width: 500px;
  1094. background: #ddf8cc;
  1095. border: 1px solid #80c605;
  1096. padding: 0 1em;
  1097. margin: 1em auto;
  1098. }
  1099. .warning {
  1100. width: 500px;
  1101. background: #F4B5B4;
  1102. border: 1px solid #900;
  1103. padding: 0 1em;
  1104. margin: 1em auto;
  1105. }
  1106. label {
  1107. display: block;
  1108. width: 130px;
  1109. float: left;
  1110. text-align: right;
  1111. padding-top: 0.3em;
  1112. padding-right: 3px;
  1113. }
  1114. .radio {
  1115. margin: 0;
  1116. padding-left: 130px;
  1117. }
  1118. #menuSelect {
  1119. padding: 0;
  1120. }
  1121. #menuSelect li {
  1122. display: block;
  1123. width: 500px;
  1124. background: #ddf8cc;
  1125. border: 1px solid #80c605;
  1126. margin: 1em auto;
  1127. padding: 0;
  1128. font-size: 1.3em;
  1129. text-align: center;
  1130. list-style-type: none;
  1131. }
  1132. #menuSelect li:hover {
  1133. background: #c4faa2;
  1134. }
  1135. #menuSelect a {
  1136. display: block;
  1137. height: 2em;
  1138. margin: 0px;
  1139. padding-top: 0.75em;
  1140. padding-bottom: -0.25em;
  1141. text-decoration: none;
  1142. }
  1143. #content {
  1144. width: 600px;
  1145. margin: 0 auto;
  1146. padding: 0;
  1147. text-align: left;
  1148. }
  1149. </style>
  1150. </head>
  1151. <body>
  1152. <div id="header">
  1153. <h1>Google Apps API Demo</h1>
  1154. <?php if ($displayMenu === true) { ?>
  1155. <p><?php echo GAPPS_DOMAIN ?> | <a href="?">Main</a> | <a href="?menu=logout">Logout</a></p>
  1156. <?php } ?>
  1157. <div class="clear"></div>
  1158. </div>
  1159. <div id="content">
  1160. <?php
  1161. }
  1162. /**
  1163. * Writes the HTML epilogue for this app and exit.
  1164. *
  1165. * @param boolean $displayBackButton (optional) If true, displays a
  1166. * link to go back at the bottom of the page. Defaults to false.
  1167. * @return void
  1168. */
  1169. function endHTML($displayBackButton = false)
  1170. {
  1171. if ($displayBackButton === true) {
  1172. echo '<a href="javascript:history.go(-1)">&larr; Back</a>';
  1173. }
  1174. ?>
  1175. </div>
  1176. </body>
  1177. </html>
  1178. <?php
  1179. exit();
  1180. }
  1181. /**
  1182. * Displays a notice indicating that a login password needs to be
  1183. * set before continuing.
  1184. *
  1185. * @return void
  1186. */
  1187. function displayPasswordNotSetNotice()
  1188. {
  1189. ?>
  1190. <div class="warning">
  1191. <h3>Almost there...</h3>
  1192. <p>Before using this demo, you must set an application password
  1193. to protect your account. You will also need to set your
  1194. Google Apps credentials in order to communicate with the Google
  1195. Apps servers.</p>
  1196. <p>To continue, open this file in a text editor and fill
  1197. out the information in the configuration section.</p>
  1198. </div>
  1199. <?php
  1200. }
  1201. /**
  1202. * Displays a notice indicating that authentication to Google Apps failed.
  1203. *
  1204. * @return void
  1205. */
  1206. function displayAuthenticationFailedNotice()
  1207. {
  1208. ?>
  1209. <div class="warning">
  1210. <h3>Google Apps Authentication Failed</h3>
  1211. <p>Authentication with the Google Apps servers failed.</p>
  1212. <p>Please open this file in a text editor and make
  1213. sure your credentials are correct.</p>
  1214. </div>
  1215. <?php
  1216. }
  1217. /**
  1218. * Outputs a request to the user to enter their login password.
  1219. *
  1220. * @param string $errorText (optional) Error text to be displayed next to the login form.
  1221. * @return void
  1222. */
  1223. function requestUserLogin($errorText = null)
  1224. {
  1225. ?>
  1226. <form method="post" accept-charset="utf-8">
  1227. <h3>Authentication Required</h3>
  1228. <?php
  1229. if ($errorText !== null) {
  1230. echo '<span class="error">' . $errorText . "</span>\n";
  1231. }
  1232. ?>
  1233. <p>Please enter your login password to continue.</p>
  1234. <p><label for="password">Password: </label>
  1235. <input type="password" name="password" value="" /></p>
  1236. <p><strong>Notice:</strong> This application is for demonstration
  1237. purposes only. Not for use in a production environment.</p>
  1238. <p><input type="submit" value="Continue &rarr;"></p>
  1239. </form>
  1240. <?php
  1241. }
  1242. /**
  1243. * Display the main menu for running in a web browser.
  1244. *
  1245. * @return void
  1246. */
  1247. function displayMenu()
  1248. {
  1249. ?>
  1250. <h2>Main Menu</h2>
  1251. <p>Welcome to the Google Apps Provisioning API demo page. Please select
  1252. from one of the following three options to see a list of commands.</p>
  1253. <ul id="menuSelect">
  1254. <li><a class="menuSelect" href="?menu=user">User Maintenance Menu</a></li>
  1255. <li><a class="menuSelect" href="?menu=nickname">Nickname Maintenance Menu</a></li>
  1256. <li><a class="menuSelect" href="?menu=emailList">Email List Maintenance Menu</a></li>
  1257. </ul>
  1258. <p>Tip: You can also run this demo from the command line if your system
  1259. has PHP CLI support enabled.</p>
  1260. <?php
  1261. }
  1262. /**
  1263. * Display the user maintenance menu for running in a web browser.
  1264. *
  1265. * @return void
  1266. */
  1267. function displayUserMenu()
  1268. {
  1269. ?>
  1270. <h2>User Maintenance Menu</h2>
  1271. <form method="post" accept-charset="utf-8">
  1272. <h3>Create User</h3>
  1273. <p>Create a new user with the given properties.</p>
  1274. <p>
  1275. <input type="hidden" name="command" value="createUser" />
  1276. <label for="user">Username: </label>
  1277. <input type="text" name="user" value="" /><br />
  1278. <label for="givenName">Given Name: </label>
  1279. <input type="text" name="givenName" value="" /><br />
  1280. <label for="familyName">Family Name: </label>
  1281. <input type="text" name="familyName" value="" /><br />
  1282. <label for="pass">Password: </label>
  1283. <input type="password" name="pass" value="" />
  1284. </p>
  1285. <p><input type="submit" value="Create User &rarr;"></p>
  1286. </form>
  1287. <form method="get" accept-charset="utf-8">
  1288. <h3>Retrieve User</h3>
  1289. <p>Retrieve the information for an existing user.</p>
  1290. <p>
  1291. <input type="hidden" name="command" value="retrieveUser" />
  1292. <label for="user">Username: </label>
  1293. <input type="text" name="user" value="" /><br />
  1294. </p>
  1295. <p><input type="submit" value="Retrieve User &rarr;"></p>
  1296. </form>
  1297. <form method="get" accept-charset="utf-8">
  1298. <h3>Retrieve All Users</h3>
  1299. <p>Retrieve the list of all users on the current domain.</p>
  1300. <p>
  1301. <input type="hidden" name="command" value="retrieveAllUsers" />
  1302. </p>
  1303. <p><input type="submit" value="Retrieve Users &rarr;"></p>
  1304. </form>
  1305. <form method="post" accept-charset="utf-8">
  1306. <h3>Update Name</h3>
  1307. <p>Update the name for an existing user.</p>
  1308. <p>
  1309. <input type="hidden" name="command" value="updateUserName" />
  1310. <label for="user">Username: </label>
  1311. <input type="text" name="user" value="" /><br />
  1312. <label for="givenName">New Given Name: </label>
  1313. <input type="text" name="givenName" value="" /><br />
  1314. <label for="familyName">New Family Name: </label>
  1315. <input type="text" name="familyName" value="" /><br />
  1316. </p>
  1317. <p><input type="submit" value="Update User &rarr;"></p>
  1318. </form>
  1319. <form method="post" accept-charset="utf-8">
  1320. <h3>Update Password</h3>
  1321. <p>Update the password for an existing user.</p>
  1322. <p>
  1323. <input type="hidden" name="command" value="updateUserPassword" />
  1324. <label for="user">Username: </label>
  1325. <input type="text" name="user" value="" /><br />
  1326. <label for="pass">New Password: </label>
  1327. <input type="password" name="pass" value="" /></p>
  1328. </p>
  1329. <p><input type="submit" value="Update User &rarr;"></p>
  1330. </form>
  1331. <form method="post" accept-charset="utf-8">
  1332. <h3>Suspend/Restore User</h3>
  1333. <p>Mark an existing user as suspended or restore a suspended user.
  1334. While suspended, the user will be prohibited from logging into
  1335. this domain.</p>
  1336. <p>
  1337. <input type="hidden" name="command" value="setUserSuspended" />
  1338. <label for="user">Username: </label>
  1339. <input type="text" name="user" value="" />
  1340. </p>
  1341. <div class="radio">
  1342. <input type="radio" name="mode" value="restore">User may log into
  1343. this domain.</input><br />
  1344. <input type="radio" name="mode" value="suspend" checked="true">User
  1345. may <strong>not</strong> log into this domain.</input>
  1346. </div>
  1347. <p><input type="submit" value="Update User &rarr;"></p>
  1348. </form>
  1349. <form method="post" accept-charset="utf-8">
  1350. <h3>Issue/Revoke Admin Rights</h3>
  1351. <p>Set whether an existing user has administrative rights for the current
  1352. domain.</p>
  1353. <p>
  1354. <input type="hidden" name="command" value="setUserAdmin" />
  1355. <label for="user">Username: </label>
  1356. <input type="text" name="user" value="" />
  1357. </p>
  1358. <div class="radio">
  1359. <input type="radio" name="mode" value="issue">User
  1360. may administer this domain.</input><br />
  1361. <input type="radio" name="mode" value="revoke" checked="true">User
  1362. may <strong>not</strong> administer this domain.</input>
  1363. </div>
  1364. <p><input type="submit" value="Update User &rarr;"></p>
  1365. </form>
  1366. <form method="post" accept-charset="utf-8">
  1367. <h3>Force User To Change Password</h3>
  1368. <p>Set whether an existing user must change their password at
  1369. their next login.</p>
  1370. <p>
  1371. <input type="hidden" name="command" value="setForceChangePassword" />
  1372. <label for="user">Username: </label>
  1373. <input type="text" name="user" value="" />
  1374. </p>
  1375. <div class="radio">
  1376. <input type="radio" name="mode" value="set">User is required to
  1377. change their password at next login.</input><br />
  1378. <input type="radio" name="mode" value="clear" checked="true">User is
  1379. <strong>not</strong> required to change their password at next
  1380. login.</input>
  1381. </div>
  1382. <p><input type="submit" value="Update User &rarr;"></p>
  1383. </form>
  1384. <form method="post" accept-charset="utf-8">
  1385. <h3>Delete User</h3>
  1386. <p>Delete an existing user on the current domain.</p>
  1387. <p>
  1388. <input type="hidden" name="command" value="deleteUser" />
  1389. <label for="user">Username: </label>
  1390. <input type="text" name="user" value="" /><br />
  1391. </p>
  1392. <p><input type="submit" value="Delete User &rarr;"></p>
  1393. </form>
  1394. <?php
  1395. }
  1396. /**
  1397. * Display the nickname maintenance menu for running in a web browser.
  1398. *
  1399. * @return void
  1400. */
  1401. function displayNicknameMenu()
  1402. {
  1403. ?>
  1404. <h2>Nickname Maintenance Menu</h2>
  1405. <form method="post" accept-charset="utf-8">
  1406. <h3>Create Nickname</h3>
  1407. <p>Create a nickname for an existing user.</p>
  1408. <p>
  1409. <input type="hidden" name="command" value="createNickname" />
  1410. <label for="user">Username: </label>
  1411. <input type="text" name="user" value="" /><br />
  1412. <label for="nickname">Nickname: </label>
  1413. <input type="text" name="nickname" value="" /><br />
  1414. </p>
  1415. <p><input type="submit" value="Create Nickname &rarr;"></p>
  1416. </form>
  1417. <form method="get" accept-charset="utf-8">
  1418. <h3>Retrieve Nickname</h3>
  1419. <p>Retrieve the information for an existing nickname.</p>
  1420. <p>
  1421. <input type="hidden" name="command" value="retrieveNickname" />
  1422. <label for="nickname">Nickname: </label>
  1423. <input type="text" name="nickname" value="" /><br />
  1424. </p>
  1425. <p><input type="submit" value="Retrieve Nickname &rarr;"></p>
  1426. </form>
  1427. <form method="get" accept-charset="utf-8">
  1428. <h3>Retrieve Nicknames</h3>
  1429. <p>Retrieve the nicknames associated with an existing username.</p>
  1430. <p>
  1431. <input type="hidden" name="command" value="retrieveNicknames" />
  1432. <label for="user">Username: </label>
  1433. <input type="text" name="user" value="" /><br />
  1434. </p>
  1435. <p><input type="submit" value="Retrieve Nicknames &rarr;"></p>
  1436. </form>
  1437. <form method="get" accept-charset="utf-8">
  1438. <h3>Retrieve All Nicknames</h3>
  1439. <p>Retrieve the nicknames on the current domain.</p>
  1440. <p>
  1441. <input type="hidden" name="command" value="retrieveAllNicknames" />
  1442. </p>
  1443. <p><input type="submit" value="Retrieve Nicknames &rarr;"></p>
  1444. </form>
  1445. <form method="post" accept-charset="utf-8">
  1446. <h3>Delete Nickname</h3>
  1447. <p>Delete an existing nickname from the current domain.</p>
  1448. <p>
  1449. <input type="hidden" name="command" value="deleteNickname" />
  1450. <label for="nickname">Nickname: </label>
  1451. <input type="text" name="nickname" value="" /><br />
  1452. </p>
  1453. <p><input type="submit" value="Delete Nickname &rarr;"></p>
  1454. </form>
  1455. <?php
  1456. }
  1457. /**
  1458. * Display the email list maintenance menu for running in a web browser.
  1459. *
  1460. * @return void
  1461. */
  1462. function displayEmailListMenu()
  1463. {
  1464. ?>
  1465. <h2>Email List Maintenance Menu</h2>
  1466. <form method="post" accept-charset="utf-8">
  1467. <h3>Create Email List</h3>
  1468. <p>Create a new email list for the current domain.</p>
  1469. <p>
  1470. <input type="hidden" name="command" value="createEmailList" />
  1471. <label for="emailList">List Name: </label>
  1472. <input type="text" name="emailList" value="" /><br />
  1473. </p>
  1474. <p><input type="submit" value="Create List &rarr;"></p>
  1475. </form>
  1476. <form method="get" accept-charset="utf-8">
  1477. <h3>Retrieve Email Lists</h3>
  1478. <p>Retrieve all email lists to which a given email address is
  1479. subscribed.</p>
  1480. <p>
  1481. <input type="hidden" name="command" value="retrieveEmailLists" />
  1482. <label for="recipient">Recipient Address: </label>
  1483. <input type="text" name="recipient" value="" /><br />
  1484. </p>
  1485. <p><input type="submit" value="Retrieve Lists &rarr;"></p>
  1486. </form>
  1487. <form method="get" accept-charset="utf-8">
  1488. <h3>Retrieve All Email Lists</h3>
  1489. <p>Retrieve all email lists on the current domain.</p>
  1490. <p>
  1491. <input type="hidden" name="command" value="retrieveAllEmailLists" />
  1492. </p>
  1493. <p><input type="submit" value="Retrieve Lists &rarr;"></p>
  1494. </form>
  1495. <form method="post" accept-charset="utf-8">
  1496. <h3>Delete Email List</h3>
  1497. <p>Delete an existing email list from the current domain.</p>
  1498. <p>
  1499. <input type="hidden" name="command" value="deleteEmailList" />
  1500. <label for="emailList">List Name: </label>
  1501. <input type="text" name="emailList" value="" /><br />
  1502. </p>
  1503. <p><input type="submit" value="Delete List &rarr;"></p>
  1504. </form>
  1505. <form method="post" accept-charset="utf-8">
  1506. <h3>Add Recipient To Email List</h3>
  1507. <p>Add or remove a recipient from an existing email list. A complete
  1508. email address is required for recipients outside the current
  1509. domain.</p>
  1510. <p>
  1511. <input type="hidden" name="command" value="modifySubscription" />
  1512. <label for="emailList">List Name: </label>
  1513. <input type="text" name="emailList" value="" /><br />
  1514. <label for="recipient">Recipient Address: </label>
  1515. <input type="text" name="recipient" value="" /><br />
  1516. <div class="radio">
  1517. <input type="radio" name="mode" value="subscribe">Subscribe
  1518. recipient.</input><br />
  1519. <input type="radio" name="mode" value="unsubscribe"
  1520. checked="true">Unsubscribe recipient.</input>
  1521. </div>
  1522. </p>
  1523. <p><input type="submit" value="Update Subscription &rarr;"></p>
  1524. </form>
  1525. <form method="get" accept-charset="utf-8">
  1526. <h3>Retrieve All Recipients</h3>
  1527. <p>Retrieve all recipients subscribed to an existing email list.</p>
  1528. <p>
  1529. <input type="hidden" name="command" value="retrieveAllRecipients" />
  1530. <label for="emailList">List Name: </label>
  1531. <input type="text" name="emailList" value="" /><br />
  1532. </p>
  1533. <p><input type="submit" value="Retrieve Recipients &rarr;"></p>
  1534. </form>
  1535. <?php
  1536. }
  1537. /**
  1538. * Log the current user out of the application.
  1539. *
  1540. * @return void
  1541. */
  1542. function logout()
  1543. {
  1544. session_destroy();
  1545. ?>
  1546. <h2>Logout</h2>
  1547. <p>Logout successful.</p>
  1548. <ul id="menuSelect">
  1549. <li><a class="menuSelect" href="?">Login</a></li>
  1550. </ul>
  1551. <?php
  1552. }
  1553. /**
  1554. * Processes loading of this sample code through a web browser.
  1555. *
  1556. * @return void
  1557. */
  1558. function runWWWVersion()
  1559. {
  1560. session_start();
  1561. // Note that all calls to endHTML() below end script execution!
  1562. // Check to make sure that the user has set a password.
  1563. $p = LOGIN_PASSWORD;
  1564. if (empty($p)) {
  1565. startHTML(false);
  1566. displayPasswordNotSetNotice();
  1567. endHTML();
  1568. }
  1569. // Grab any login credentials that might be waiting in the request
  1570. if (!empty($_POST['password'])) {
  1571. if ($_POST['password'] == LOGIN_PASSWORD) {
  1572. $_SESSION['authenticated'] = 'true';
  1573. } else {
  1574. // Invalid password. Stop and display a login screen.
  1575. startHTML(false);
  1576. requestUserLogin("Incorrect password.");
  1577. endHTML();
  1578. }
  1579. }
  1580. // If the user isn't authenticated, display a login screen
  1581. if (!isset($_SESSION['authenticated'])) {
  1582. startHTML(false);
  1583. requestUserLogin();
  1584. endHTML();
  1585. }
  1586. // Try to login. If login fails, log the user out and display an
  1587. // error message.
  1588. try {
  1589. $client = getClientLoginHttpClient(GAPPS_USERNAME . '@' .
  1590. GAPPS_DOMAIN, GAPPS_PASSWORD);
  1591. $gapps = new Zend_Gdata_Gapps($client, GAPPS_DOMAIN);
  1592. } catch (Zend_Gdata_App_AuthException $e) {
  1593. session_destroy();
  1594. startHTML(false);
  1595. displayAuthenticationFailedNotice();
  1596. endHTML();
  1597. }
  1598. // Success! We're logged in.
  1599. // First we check for commands that can be submitted either though
  1600. // POST or GET (they don't make any changes).
  1601. if (!empty($_REQUEST['command'])) {
  1602. switch ($_REQUEST['command']) {
  1603. case 'retrieveUser':
  1604. startHTML();
  1605. retrieveUser($gapps, true, $_REQUEST['user']);
  1606. endHTML(true);
  1607. case 'retrieveAllUsers':
  1608. startHTML();
  1609. retrieveAllUsers($gapps, true);
  1610. endHTML(true);
  1611. case 'retrieveNickname':
  1612. startHTML();
  1613. retrieveNickname($gapps, true, $_REQUEST['nickname']);
  1614. endHTML(true);
  1615. case 'retrieveNicknames':
  1616. startHTML();
  1617. retrieveNicknames($gapps, true, $_REQUEST['user']);
  1618. endHTML(true);
  1619. case 'retrieveAllNicknames':
  1620. startHTML();
  1621. retrieveAllNicknames($gapps, true);
  1622. endHTML(true);
  1623. case 'retrieveEmailLists':
  1624. startHTML();
  1625. retrieveEmailLists($gapps, true, $_REQUEST['recipient']);
  1626. endHTML(true);
  1627. case 'retrieveAllEmailLists':
  1628. startHTML();
  1629. retrieveAllEmailLists($gapps, true);
  1630. endHTML(true);
  1631. case 'retrieveAllRecipients':
  1632. startHTML();
  1633. retrieveAllRecipients($gapps, true, $_REQUEST['emailList']);
  1634. endHTML(true);
  1635. }
  1636. }
  1637. // Now we handle the potentially destructive commands, which have to
  1638. // be submitted by POST only.
  1639. if (!empty($_POST['command'])) {
  1640. switch ($_POST['command']) {
  1641. case 'createUser':
  1642. startHTML();
  1643. createUser($gapps, true, $_POST['user'],
  1644. $_POST['givenName'], $_POST['familyName'],
  1645. $_POST['pass']);
  1646. endHTML(true);
  1647. case 'updateUserName':
  1648. startHTML();
  1649. updateUserName($gapps, true, $_POST['user'],
  1650. $_POST['givenName'], $_POST['familyName']);
  1651. endHTML(true);
  1652. case 'updateUserPassword':
  1653. startHTML();
  1654. updateUserPassword($gapps, true, $_POST['user'],
  1655. $_POST['pass']);
  1656. endHTML(true);
  1657. case 'setUserSuspended':
  1658. if ($_POST['mode'] == 'suspend') {
  1659. startHTML();
  1660. suspendUser($gapps, true, $_POST['user']);
  1661. endHTML(true);
  1662. } elseif ($_POST['mode'] == 'restore') {
  1663. startHTML();
  1664. restoreUser($gapps, true, $_POST['user']);
  1665. endHTML(true);
  1666. } else {
  1667. header('HTTP/1.1 400 Bad Request');
  1668. startHTML();
  1669. echo "<h2>Invalid mode.</h2>\n";
  1670. echo "<p>Please check your request and try again.</p>";
  1671. endHTML(true);
  1672. }
  1673. case 'setUserAdmin':
  1674. if ($_POST['mode'] == 'issue') {
  1675. startHTML();
  1676. giveUserAdminRights($gapps, true, $_POST['user']);
  1677. endHTML(true);
  1678. } elseif ($_POST['mode'] == 'revoke') {
  1679. startHTML();
  1680. revokeUserAdminRights($gapps, true, $_POST['user']);
  1681. endHTML(true);
  1682. } else {
  1683. header('HTTP/1.1 400 Bad Request');
  1684. startHTML();
  1685. echo "<h2>Invalid mode.</h2>\n";
  1686. echo "<p>Please check your request and try again.</p>";
  1687. endHTML(true);
  1688. }
  1689. case 'setForceChangePassword':
  1690. if ($_POST['mode'] == 'set') {
  1691. startHTML();
  1692. setUserMustChangePassword($gapps, true, $_POST['user']);
  1693. endHTML(true);
  1694. } elseif ($_POST['mode'] == 'clear') {
  1695. startHTML();
  1696. clearUserMustChangePassword($gapps, true, $_POST['user']);
  1697. endHTML(true);
  1698. } else {
  1699. header('HTTP/1.1 400 Bad Request');
  1700. startHTML();
  1701. echo "<h2>Invalid mode.</h2>\n";
  1702. echo "<p>Please check your request and try again.</p>";
  1703. endHTML(true);
  1704. }
  1705. case 'deleteUser':
  1706. startHTML();
  1707. deleteUser($gapps, true, $_POST['user']);
  1708. endHTML(true);
  1709. case 'createNickname':
  1710. startHTML();
  1711. createNickname($gapps, true, $_POST['user'],
  1712. $_POST['nickname']);
  1713. endHTML(true);
  1714. case 'deleteNickname':
  1715. startHTML();
  1716. deleteNickname($gapps, true, $_POST['nickname']);
  1717. endHTML(true);
  1718. case 'createEmailList':
  1719. startHTML();
  1720. createEmailList($gapps, true, $_POST['emailList']);
  1721. endHTML(true);
  1722. case 'deleteEmailList':
  1723. startHTML();
  1724. deleteEmailList($gapps, true, $_POST['emailList']);
  1725. endHTML(true);
  1726. case 'modifySubscription':
  1727. if ($_POST['mode'] == 'subscribe') {
  1728. startHTML();
  1729. addRecipientToEmailList($gapps, true, $_POST['recipient'],
  1730. $_POST['emailList']);
  1731. endHTML(true);
  1732. } elseif ($_POST['mode'] == 'unsubscribe') {
  1733. startHTML();
  1734. removeRecipientFromEmailList($gapps, true,
  1735. $_POST['recipient'], $_POST['emailList']);
  1736. endHTML(true);
  1737. } else {
  1738. header('HTTP/1.1 400 Bad Request');
  1739. startHTML();
  1740. echo "<h2>Invalid mode.</h2>\n";
  1741. echo "<p>Please check your request and try again.</p>";
  1742. endHTML(true);
  1743. }
  1744. }
  1745. }
  1746. // Check for an invalid command. If so, display an error and exit.
  1747. if (!empty($_REQUEST['command'])) {
  1748. header('HTTP/1.1 400 Bad Request');
  1749. startHTML();
  1750. echo "<h2>Invalid command.</h2>\n";
  1751. echo "<p>Please check your request and try again.</p>";
  1752. endHTML(true);
  1753. }
  1754. // If a menu parameter is available, display a submenu.
  1755. if (!empty($_REQUEST['menu'])) {
  1756. switch ($_REQUEST['menu']) {
  1757. case 'user':
  1758. startHTML();
  1759. displayUserMenu();
  1760. endHTML();
  1761. case 'nickname':
  1762. startHTML();
  1763. displayNicknameMenu();
  1764. endHTML();
  1765. case 'emailList':
  1766. startHTML();
  1767. displayEmailListMenu();
  1768. endHTML();
  1769. case 'logout':
  1770. startHTML(false);
  1771. logout();
  1772. endHTML();
  1773. default:
  1774. header('HTTP/1.1 400 Bad Request');
  1775. startHTML();
  1776. echo "<h2>Invalid menu selection.</h2>\n";
  1777. echo "<p>Please check your request and try again.</p>";
  1778. endHTML(true);
  1779. }
  1780. }
  1781. // If we get this far, that means there's nothing to do. Display
  1782. // the main menu.
  1783. // If no command was issued and no menu was selected, display the
  1784. // main menu.
  1785. startHTML();
  1786. displayMenu();
  1787. endHTML();
  1788. }
  1789. // ************************** PROGRAM ENTRY POINT **************************
  1790. if (!isset($_SERVER["HTTP_HOST"])) {
  1791. // running through command line
  1792. runCLIVersion($argv, $argc);
  1793. } else {
  1794. // running through web server
  1795. try {
  1796. runWWWVersion();
  1797. } catch (Zend_Gdata_Gapps_ServiceException $e) {
  1798. // Try to recover gracefully from a service exception.
  1799. // The HTML prologue will have already been sent.
  1800. echo "<p><strong>Service Error Encountered</strong></p>\n";
  1801. echo "<pre>" . htmlspecialchars($e->__toString()) . "</pre>";
  1802. endHTML(true);
  1803. }
  1804. }