PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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