Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Gdata / Docs.php
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-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /**
23 * PHP sample code for the Google Documents List data API. Utilizes the
24 * Zend Framework Gdata components to communicate with the Google API.
25 *
26 * Requires the Zend Framework Gdata components and PHP >= 5.1.4
27 *
28 * You can run this sample both from the command line (CLI) and also
29 * from a web browser. When running through a web browser, only
30 * AuthSub and outputting a list of documents is demonstrated. When
31 * running via CLI, all functionality except AuthSub is available and dependent
32 * upon the command line options passed. Run this script without any
33 * command line options to see usage, eg:
34 * /usr/local/bin/php -f Docs.php
35 *
36 * More information on the Command Line Interface is available at:
37 * http://www.php.net/features.commandline
38 *
39 * NOTE: You must ensure that Zend Framework is in your PHP include
40 * path. You can do this via php.ini settings, or by modifying the
41 * argument to set_include_path in the code below.
42 *
43 * NOTE: As this is sample code, not all of the functions do full error
44 * handling.
45 */
46
47 /**
48 * @see Zend_Loader
49 */
50 require_once 'Zend/Loader.php';
51
52 /**
53 * @see Zend_Gdata
54 */
55 Zend_Loader::loadClass('Zend_Gdata');
56
57 /**
58 * @see Zend_Gdata_AuthSub
59 */
60 Zend_Loader::loadClass('Zend_Gdata_AuthSub');
61
62 /**
63 * @see Zend_Gdata_ClientLogin
64 */
65 Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
66
67 /**
68 * @see Zend_Gdata_Docs
69 */
70 Zend_Loader::loadClass('Zend_Gdata_Docs');
71
72 /**
73 * Returns a HTTP client object with the appropriate headers for communicating
74 * with Google using the ClientLogin credentials supplied.
75 *
76 * @param string $user The username, in e-mail address format, to authenticate
77 * @param string $pass The password for the user specified
78 * @return Zend_Http_Client
79 */
80 function getClientLoginHttpClient($user, $pass)
81 {
82 $service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
83 $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
84 return $client;
85 }
86
87 // ************************ BEGIN CLI SPECIFIC CODE ************************
88
89 /**
90 * Display list of valid commands.
91 *
92 * @param string $executable The name of the current script. This is usually available as $argv[0].
93 * @return void
94 */
95 function displayHelp($executable)
96 {
97 echo "Usage: php {$executable} <action> [<username>] [<password>] " .
98 "[<arg1> <arg2> ...]\n\n";
99 echo "Possible action values include:\n" .
100 "retrieveAllDocuments\n" .
101 "retrieveWPDocs\n" .
102 "retrieveSpreadsheets\n" .
103 "fullTextSearch\n" .
104 "uploadDocument\n";
105 }
106
107 /**
108 * Parse command line arguments and execute appropriate function when
109 * running from the command line.
110 *
111 * If no arguments are provided, usage information will be provided.
112 *
113 * @param array $argv The array of command line arguments provided by PHP.
114 * $argv[0] should be the current executable name or '-' if not available.
115 * @param integer $argc The size of $argv.
116 * @return void
117 */
118 function runCLIVersion($argv, $argc)
119 {
120 if (isset($argc) && $argc >= 2) {
121 # Prepare a server connection
122 if ($argc >= 4) {
123 try {
124 $client = getClientLoginHttpClient($argv[2], $argv[3]);
125 $docs = new Zend_Gdata_Docs($client);
126 } catch (Zend_Gdata_App_AuthException $e) {
127 echo "Error: Unable to authenticate. Please check your";
128 echo " credentials.\n";
129 exit(1);
130 }
131 }
132
133 # Dispatch arguments to the desired method
134 switch ($argv[1]) {
135 case 'retrieveAllDocuments':
136 if ($argc >= 4) {
137 retrieveAllDocuments($docs, false);
138 } else {
139 echo "Usage: php {$argv[0]} {$argv[1]} <username>";
140 echo " <password>\n\n";
141 echo "This lists all of the documents in the user's";
142 echo " account.\n";
143 }
144 break;
145 case 'retrieveWPDocs':
146 if ($argc >= 4) {
147 //echo "!WP Docs:";
148 //var_dump($docs);
149 retrieveWPDocs($docs, false);
150 } else {
151 echo "Usage: php {$argv[0]} {$argv[1]} <username>";
152 echo " <password>\n\n";
153 echo "This lists all of the word processing documents in";
154 echo " the user's account.\n";
155 }
156 break;
157 case 'retrieveSpreadsheets':
158 if ($argc >= 4) {
159 retrieveAllDocuments($docs, false);
160 } else {
161 echo "Usage: php {$argv[0]} {$argv[1]} <username>";
162 echo " <password>\n\n";
163 echo "This lists all of the spreadsheets in the user's";
164 echo " account.\n";
165 }
166 break;
167 case 'fullTextSearch':
168 if ($argc >= 4) {
169 // Combine all of the query args into one query string.
170 // The command line split the query string on space
171 // characters.
172 $queryString = implode(' ', array_slice($argv, 4));
173 fullTextSearch($docs, false, $queryString);
174 } else {
175 echo "Usage: php {$argv[0]} {$argv[1]} <username>";
176 echo " <password> <query string>\n\n";
177 echo "This lists all of the documents which contain the";
178 echo " query string.\n";
179 }
180 break;
181 case 'uploadDocument':
182 if ($argc >= 5) {
183 // Pass in the file name of the document to be uploaded.
184 // Since the document is on this machine, we do not need
185 // to set the temporary file name. The temp file name is
186 // used only when uploading to a webserver.
187 uploadDocument($docs, false, $argv[4], null);
188 } else {
189 echo "Usage: php {$argv[0]} {$argv[1]} <username>";
190 echo " <password> <file_with_path>\n\n";
191 echo "This lists all of the documents which contain the";
192 echo " query string.\n";
193 echo "\nExample: php {$argv[0]} {$argv[1]} <username>";
194 echo " <password> /tmp/testSpreadsheet.ods\n";
195 }
196 break;
197 default:
198 // Invalid action entered
199 displayHelp($argv[0]);
200 // End switch block
201 }
202 } else {
203 // action left unspecified
204 displayHelp($argv[0]);
205 }
206 }
207
208 /**
209 * Displays the titles for the Google Documents entries in the feed. In HTML
210 * mode, the titles are links which point to the HTML version of the document.
211 *
212 * @param Zend_Gdata_Docs_DocumentListFeed $feed
213 * @param boolean $html True if output should be formatted for display in
214 * a web browser
215 * @return void
216 */
217 function printDocumentsFeed($feed, $html)
218 {
219 if ($html) {echo "<ul>\n";}
220
221 // Iterate over the document entries in the feed and display each document's
222 // title.
223 foreach ($feed->entries as $entry) {
224
225 if ($html) {
226 // Find the URL of the HTML view of the document.
227 $alternateLink = '';
228 foreach ($entry->link as $link) {
229 if ($link->getRel() === 'alternate') {
230 $alternateLink = $link->getHref();
231 }
232 }
233 // Make the title link to the document on docs.google.com.
234 echo "<li><a href=\"$alternateLink\">\n";
235 }
236
237 echo "$entry->title\n";
238
239 if ($html) {echo "</a></li>\n";}
240 }
241
242 if ($html) {echo "</ul>\n";}
243 }
244
245 /**
246 * Obtain a list of all of a user's docs.google.com documents and print the
247 * titles to the command line.
248 *
249 * @param Zend_Gdata_Docs $client The service object to use for communicating with the Google
250 * Documents server.
251 * @param boolean $html True if output should be formatted for display in a web browser.
252 * @return void
253 */
254 function retrieveAllDocuments($client, $html)
255 {
256 if ($html) {echo "<h2>Your documents</h2>\n";}
257
258 $feed = $client->getDocumentListFeed();
259
260 printDocumentsFeed($feed, $html);
261 }
262
263 /**
264 * Obtain a list of all of a user's docs.google.com word processing
265 * documents and print the titles to the command line.
266 *
267 * @param Zend_Gdata_Docs $client The service object to use for communicating with the Google
268 * Documents server.
269 * @param boolean $html True if output should be formatted for display in a web browser.
270 * @return void
271 */
272 function retrieveWPDocs($client, $html)
273 {
274 if ($html) {echo "<h2>Your word processing documents</h2>\n";}
275
276 $feed = $client->getDocumentListFeed(
277 'http://docs.google.com/feeds/documents/private/full/-/document');
278
279 printDocumentsFeed($feed, $html);
280 }
281
282 /**
283 * Obtain a list of all of a user's docs.google.com spreadsheets
284 * documents and print the titles to the command line.
285 *
286 * @param Zend_Gdata_Docs $client The service object to use for communicating with the Google
287 * Documents server.
288 * @param boolean $html True if output should be formatted for display in a web browser.
289 * @return void
290 */
291 function retrieveSpreadsheets($client, $html)
292 {
293 if ($html) {echo "<h2>Your spreadsheets</h2>\n";}
294
295 $feed = $client->getDocumentListFeed(
296 'http://docs.google.com/feeds/documents/private/full/-/spreadsheet');
297
298 printDocumentsFeed($feed, $html);
299 }
300
301 /**
302 * Obtain a list of all of a user's docs.google.com documents
303 * which match the specified search criteria and print the titles to the
304 * command line.
305 *
306 * @param Zend_Gdata_Docs $client The service object to use for communicating with the Google
307 * Documents server.
308 * @param boolean $html True if output should be formatted for display in a web browser.
309 * @param string $query The search query to use
310 * @return void
311 */
312 function fullTextSearch($client, $html, $query)
313 {
314 if ($html) {echo "<h2>Documents containing $query</h2>\n";}
315
316 $feed = $client->getDocumentListFeed(
317 'http://docs.google.com/feeds/documents/private/full?q=' . $query);
318
319 printDocumentsFeed($feed, $html);
320 }
321
322 /**
323 * Upload the specified document
324 *
325 * @param Zend_Gdata_Docs $docs The service object to use for communicating with
326 * the Google Documents server.
327 * @param boolean $html True if output should be formatted for display in
328 * a web browser.
329 * @param string $originalFileName The name of the file to be uploaded. The mime type
330 * of the file is determined from the extension on
331 * this file name. For example, test.csv is uploaded
332 * as a comma seperated volume and converted into a
333 * spreadsheet.
334 * @param string $temporaryFileLocation (optional) The file in which the data for the
335 * document is stored. This is used when the file has
336 * been uploaded from the client's machine to the
337 * server and is stored in a temporary file which
338 * does not have an extension. If this parameter is
339 * null, the file is read from the originalFileName.
340 * @return void
341 */
342 function uploadDocument($docs, $html, $originalFileName,
343 $temporaryFileLocation) {
344 $fileToUpload = $originalFileName;
345 if ($temporaryFileLocation) {
346 $fileToUpload = $temporaryFileLocation;
347 }
348
349 // Upload the file and convert it into a Google Document. The original
350 // file name is used as the title of the document and the mime type
351 // is determined based on the extension on the original file name.
352 $newDocumentEntry = $docs->uploadFile($fileToUpload, $originalFileName,
353 null, Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);
354
355 echo "New Document Title: ";
356
357 if ($html) {
358 // Find the URL of the HTML view of this document.
359 $alternateLink = '';
360 foreach ($newDocumentEntry->link as $link) {
361 if ($link->getRel() === 'alternate') {
362 $alternateLink = $link->getHref();
363 }
364 }
365 // Make the title link to the document on docs.google.com.
366 echo "<a href=\"$alternateLink\">\n";
367 }
368 echo $newDocumentEntry->title."\n";
369 if ($html) {echo "</a>\n";}
370 }
371
372 // ************************ BEGIN WWW SPECIFIC CODE ************************
373
374 /**
375 * Writes the HTML prologue for this app.
376 *
377 * NOTE: We would normally keep the HTML/CSS markup separate from the business
378 * logic above, but have decided to include it here for simplicity of
379 * having a single-file sample.
380 *
381 *
382 * @param boolean $displayMenu (optional) If set to true, a navigation menu is displayed at the top
383 * of the page. Default is true.
384 * @return void
385 */
386 function startHTML($displayMenu = true)
387 {
388 ?>
389 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
390 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
391
392 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
393 <head>
394 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
395
396 <title>Documents List API Demo</title>
397
398 <style type="text/css" media="screen">
399 body {
400 font-family: Arial, Helvetica, sans-serif;
401 font-size: small;
402 }
403
404 #header {
405 background-color: #9cF;
406 -moz-border-radius: 5px;
407 -webkit-border-radius: 5px;
408 padding-left: 5px;
409 height: 2.4em;
410 }
411
412 #header h1 {
413 width: 49%;
414 display: inline;
415 float: left;
416 margin: 0;
417 padding: 0;
418 font-size: 2em;
419 }
420
421 #header p {
422 width: 49%;
423 margin: 0;
424 padding-right: 15px;
425 float: right;
426 line-height: 2.4em;
427 text-align: right;
428 }
429
430 .clear {
431 clear:both;
432 }
433
434 h2 {
435 background-color: #ccc;
436 -moz-border-radius: 5px;
437 -webkit-border-radius: 5px;
438 margin-top: 1em;
439 padding-left: 5px;
440 }
441
442 .error {
443 color: red;
444 }
445
446 form {
447 width: 500px;
448 background: #ddf8cc;
449 border: 1px solid #80c605;
450 padding: 0 1em;
451 margin: 1em auto;
452 }
453
454 .warning {
455 width: 500px;
456 background: #F4B5B4;
457 border: 1px solid #900;
458 padding: 0 1em;
459 margin: 1em auto;
460 }
461
462 label {
463 display: block;
464 width: 130px;
465 float: left;
466 text-align: right;
467 padding-top: 0.3em;
468 padding-right: 3px;
469 }
470
471 .radio {
472 margin: 0;
473 padding-left: 130px;
474 }
475
476 #menuSelect {
477 padding: 0;
478 }
479
480 #menuSelect li {
481 display: block;
482 width: 500px;
483 background: #ddf8cc;
484 border: 1px solid #80c605;
485 margin: 1em auto;
486 padding: 0;
487 font-size: 1.3em;
488 text-align: center;
489 list-style-type: none;
490 }
491
492 #menuSelect li:hover {
493 background: #c4faa2;
494 }
495
496 #menuSelect a {
497 display: block;
498 height: 2em;
499 margin: 0px;
500 padding-top: 0.75em;
501 padding-bottom: -0.25em;
502 text-decoration: none;
503 }
504 #content {
505 width: 600px;
506 margin: 0 auto;
507 padding: 0;
508 text-align: left;
509 }
510 </style>
511
512 </head>
513
514 <body>
515
516 <div id="header">
517 <h1>Documents List API Demo</h1>
518 <?php if ($displayMenu === true) { ?>
519 <p><a href="?">Main</a> | <a href="?menu=logout">Logout</a></p>
520 <?php } ?>
521 <div class="clear"></div>
522 </div>
523
524 <div id="content">
525 <?php
526 }
527
528 /**
529 * Writes the HTML epilogue for this app and exit.
530 *
531 * @param boolean $displayBackButton (optional) If true, displays a link to go back at the bottom
532 * of the page. Defaults to false.
533 * @return void
534 */
535 function endHTML($displayBackButton = false)
536 {
537 if ($displayBackButton === true) {
538 echo '<div style="clear: both;">';
539 echo '<a href="javascript:history.go(-1)">&larr; Back</a></div>';
540 }
541 ?>
542 </div>
543 </body>
544 </html>
545 <?php
546 exit();
547 }
548
549 /**
550 * Displays a notice indicating that a login password needs to be
551 * set before continuing.
552 *
553 * @return void
554 */
555 function displayPasswordNotSetNotice()
556 {
557 ?>
558 <div class="warning">
559 <h3>Almost there...</h3>
560 <p>Before using this demo, you must set an application password
561 to protect your account. You will also need to set your
562 Google Apps credentials in order to communicate with the Google
563 Apps servers.</p>
564 <p>To continue, open this file in a text editor and fill
565 out the information in the configuration section.</p>
566 </div>
567 <?php
568 }
569
570 /**
571 * Displays a notice indicating that authentication to Google Apps failed.
572 *
573 * @return void
574 */
575 function displayAuthenticationFailedNotice()
576 {
577 ?>
578 <div class="warning">
579 <h3>Google Docs Authentication Failed</h3>
580 <p>Authentication with the Google Apps servers failed.</p>
581 <p>Please open this file in a text editor and make
582 sure your credentials are correct.</p>
583 </div>
584 <?php
585 }
586
587 /**
588 * Outputs a request to the user to login to their Google account, including
589 * a link to the AuthSub URL.
590 *
591 * Uses getAuthSubUrl() to get the URL which the user must visit to authenticate
592 *
593 * @param string $linkText
594 * @return void
595 */
596 function requestUserLogin($linkText)
597 {
598 $authSubUrl = getAuthSubUrl();
599 echo "<a href=\"{$authSubUrl}\">{$linkText}</a>";
600 }
601
602 /**
603 * Returns the AuthSub URL which the user must visit to authenticate requests
604 * from this application.
605 *
606 * Uses getCurrentUrl() to get the next URL which the user will be redirected
607 * to after successfully authenticating with the Google service.
608 *
609 * @return string AuthSub URL
610 */
611 function getAuthSubUrl()
612 {
613 $next = getCurrentUrl();
614 $scope = 'http://docs.google.com/feeds/documents';
615 $secure = false;
616 $session = true;
617 return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,
618 $session);
619 }
620
621 /**
622 * Returns a HTTP client object with the appropriate headers for communicating
623 * with Google using AuthSub authentication.
624 *
625 * Uses the $_SESSION['sessionToken'] to store the AuthSub session token after
626 * it is obtained. The single use token supplied in the URL when redirected
627 * after the user succesfully authenticated to Google is retrieved from the
628 * $_GET['token'] variable.
629 *
630 * @return Zend_Http_Client
631 */
632 function getAuthSubHttpClient()
633 {
634 global $_SESSION, $_GET;
635 if (!isset($_SESSION['docsSampleSessionToken']) && isset($_GET['token'])) {
636 $_SESSION['docsSampleSessionToken'] =
637 Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
638 }
639 $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['docsSampleSessionToken']);
640 return $client;
641 }
642
643 /**
644 * Returns the full URL of the current page, based upon env variables
645 *
646 * Env variables used:
647 * $_SERVER['HTTPS'] = (on|off|)
648 * $_SERVER['HTTP_HOST'] = value of the Host: header
649 * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
650 * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
651 *
652 * @return string Current URL
653 */
654 function getCurrentUrl()
655 {
656 global $_SERVER;
657
658 /**
659 * Filter php_self to avoid a security vulnerability.
660 */
661 $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
662 strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
663
664 if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
665 $protocol = 'https://';
666 } else {
667 $protocol = 'http://';
668 }
669 $host = $_SERVER['HTTP_HOST'];
670 if ($_SERVER['SERVER_PORT'] != '' &&
671 (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
672 ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
673 $port = ':' . $_SERVER['SERVER_PORT'];
674 } else {
675 $port = '';
676 }
677 return $protocol . $host . $port . $php_request_uri;
678 }
679
680 /**
681 * Display the main menu for running in a web browser.
682 *
683 * @return void
684 */
685 function displayMenu()
686 {
687 ?>
688 <h2>Main Menu</h2>
689
690 <p>Welcome to the Google Documents List API demo page. Please select
691 from one of the following three options to see a list of commands.</p>
692
693 <ul id="menuSelect">
694 <li><a class="menuSelect" href="?menu=list">List Documents</a></li>
695 <li><a class="menuSelect" href="?menu=query">Query your Documents</a></li>
696 <li><a class="menuSelect" href="?menu=upload">Upload a new document</a></li>
697 </ul>
698
699 <p>Tip: You can also run this demo from the command line if your system
700 has PHP CLI support enabled.</p>
701 <?php
702 }
703
704 /**
705 * Log the current user out of the application.
706 *
707 * @return void
708 */
709 function logout()
710 {
711 session_destroy();
712 ?>
713 <h2>Logout</h2>
714
715 <p>Logout successful.</p>
716
717 <ul id="menuSelect">
718 <li><a class="menuSelect" href="?">Login</a></li>
719 </ul>
720 <?php
721 }
722
723
724 /**
725 * Processes loading of this sample code through a web browser.
726 *
727 * @return void
728 */
729 function runWWWVersion()
730 {
731 session_start();
732
733 // Note that all calls to endHTML() below end script execution!
734
735 global $_SESSION, $_GET;
736 if (!isset($_SESSION['docsSampleSessionToken']) && !isset($_GET['token'])) {
737 requestUserLogin('Please login to your Google Account.');
738 } else {
739 $client = getAuthSubHttpClient();
740 $docs = new Zend_Gdata_Docs($client);
741
742 // First we check for commands that can be submitted either though
743 // POST or GET (they don't make any changes).
744 if (!empty($_REQUEST['command'])) {
745 switch ($_REQUEST['command']) {
746 case 'retrieveAllDocuments':
747 startHTML();
748 retrieveAllDocuments($docs, true);
749 endHTML(true);
750 case 'retrieveWPDocs':
751 startHTML();
752 retrieveWPDocs($docs, true);
753 endHTML(true);
754 case 'retrieveSpreadsheets':
755 startHTML();
756 retrieveSpreadsheets($docs, true);
757 endHTML(true);
758 case 'fullTextSearch':
759 startHTML();
760 fullTextSearch($docs, true, $_REQUEST['query']);
761 endHTML(true);
762
763 }
764 }
765
766 // Now we handle the potentially destructive commands, which have to
767 // be submitted by POST only.
768 if (!empty($_POST['command'])) {
769 switch ($_POST['command']) {
770 case 'uploadDocument':
771 startHTML();
772 uploadDocument($docs, true,
773 $_FILES['uploadedFile']['name'],
774 $_FILES['uploadedFile']['tmp_name']);
775 endHTML(true);
776 case 'modifySubscription':
777 if ($_POST['mode'] == 'subscribe') {
778 startHTML();
779 endHTML(true);
780 } elseif ($_POST['mode'] == 'unsubscribe') {
781 startHTML();
782 endHTML(true);
783 } else {
784 header('HTTP/1.1 400 Bad Request');
785 startHTML();
786 echo "<h2>Invalid mode.</h2>\n";
787 echo "<p>Please check your request and try again.</p>";
788 endHTML(true);
789 }
790 }
791 }
792
793 // Check for an invalid command. If so, display an error and exit.
794 if (!empty($_REQUEST['command'])) {
795 header('HTTP/1.1 400 Bad Request');
796 startHTML();
797 echo "<h2>Invalid command.</h2>\n";
798 echo "<p>Please check your request and try again.</p>";
799 endHTML(true);
800 }
801 // If a menu parameter is available, display a submenu.
802
803 if (!empty($_REQUEST['menu'])) {
804 switch ($_REQUEST['menu']) {
805 case 'list':
806 startHTML();
807 displayListMenu();
808 endHTML();
809 case 'query':
810 startHTML();
811 displayQueryMenu();
812 endHTML();
813 case 'upload':
814 startHTML();
815 displayUploadMenu();
816 endHTML();
817 case 'logout':
818 startHTML(false);
819 logout();
820 endHTML();
821 default:
822 header('HTTP/1.1 400 Bad Request');
823 startHTML();
824 echo "<h2>Invalid menu selection.</h2>\n";
825 echo "<p>Please check your request and try again.</p>";
826 endHTML(true);
827 }
828 }
829 // If we get this far, that means there's nothing to do. Display
830 // the main menu.
831 // If no command was issued and no menu was selected, display the
832 // main menu.
833 startHTML();
834 displayMenu();
835 endHTML();
836 }
837 }
838
839 /**
840 * Display the menu for running in a web browser.
841 *
842 * @return void
843 */
844 function displayListMenu()
845 {
846 ?>
847 <h2>List Documents Menu</h2>
848
849 <form method="get" accept-charset="utf-8">
850 <h3>Retrieve Google Documents Feed</h3>
851 <p>Retrieve the feed for all of your documents.</p>
852 <p>
853 <input type="hidden" name="command" value="retrieveAllDocuments" />
854 </p>
855 <p><input type="submit" value="Retrieve Documents Feed &rarr;"></p>
856 </form>
857
858 <form method="get" accept-charset="utf-8">
859 <h3>Retrieve Google Word Processing Documents</h3>
860 <p>Query the documents list feed for all word processing documents.</p>
861 <p>
862 <input type="hidden" name="command" value="retrieveWPDocs" />
863 </p>
864 <p><input type="submit" value="Retrieve Word Processing Documents &rarr;"></p>
865 </form>
866
867 <form method="get" accept-charset="utf-8">
868 <h3>Retrieve Google Spreadsheets</h3>
869 <p>Query the documents list feed for all spreadsheets.</p>
870 <p>
871 <input type="hidden" name="command" value="retrieveSpreadsheets" />
872 </p>
873 <p><input type="submit" value="Retrieve Spreadsheets &rarr;"></p>
874 </form>
875 <?php
876 }
877
878 /**
879 * Display the menu for running in a web browser.
880 *
881 * @return void
882 */
883 function displayQueryMenu()
884 {
885 ?>
886 <h2>Query the Documents List Feed</h2>
887
888 <form method="get" accept-charset="utf-8">
889 <h3>Search the Documents List Feed</h3>
890 <p>Find documents which contain the desired text.</p>
891 <p>
892 <input type="hidden" name="command" value="fullTextSearch" />
893 <input type="text" name="query" />
894 </p>
895 <p><input type="submit" value="Search Documents Feed &rarr;"></p>
896 </form>
897
898 <?php
899 }
900
901 /**
902 * Display the menu for running in a web browser.
903 *
904 * @return void
905 */
906 function displayUploadMenu()
907 {
908 ?>
909 <h2>Upload a document</h2>
910
911 <form method="post" enctype="multipart/form-data">
912 <h3>Select a Document to Upload</h3>
913 <p>Upload a file from your computer to <a href="http://docs.google.com">Google Documents</a>.</p>
914 <p>
915 <input type="hidden" name="command" value="uploadDocument" />
916 <input name="uploadedFile" type="file" />
917 </p>
918 <p><input type="submit" value="Upload the Document &rarr;"></p>
919 </form>
920
921 <?php
922 }
923
924 // ************************** PROGRAM ENTRY POINT **************************
925
926 if (!isset($_SERVER["HTTP_HOST"])) {
927 // running through command line
928 runCLIVersion($argv, $argc);
929 } else {
930 // running through web server
931 try {
932 runWWWVersion();
933 } catch (Zend_Gdata_Gapps_ServiceException $e) {
934 // Try to recover gracefully from a service exception.
935 // The HTML prologue will have already been sent.
936 echo "<p><strong>Service Error Encountered</strong></p>\n";
937 echo "<pre>" . htmlspecialchars($e->__toString()) . "</pre>";
938 endHTML(true);
939 }
940 }