index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Sample code to demonstrate accessing a Google Data feed using OAuth for
  23. * authorization. Utilizes the Zend Framework Zend_OAuth components to
  24. * communicate with the API(s).
  25. *
  26. * NOTE: As this is sample code, not all of the functions do full error
  27. * handling.
  28. */
  29. require_once 'Zend/Gdata/Docs.php';
  30. require_once 'Zend/Gdata/Spreadsheets.php';
  31. require_once 'Gdata_OAuth_Helper.php';
  32. session_start();
  33. // Application constants. Replace these values with your own.
  34. $APP_NAME = 'google-ZendGData3LOSample-1.0';
  35. $APP_URL = getAppURL();
  36. $scopes = array(
  37. 'https://docs.google.com/feeds/',
  38. 'http://spreadsheets.google.com/feeds/'
  39. );
  40. // Setup OAuth consumer. Thes values should be replaced with your registered
  41. // app's consumer key/secret.
  42. $CONSUMER_KEY = 'anonymous';
  43. $CONSUMER_SECRET = 'anonymous';
  44. $consumer = new Gdata_OAuth_Helper($CONSUMER_KEY, $CONSUMER_SECRET);
  45. // Main controller logic.
  46. switch (@$_REQUEST['action']) {
  47. case 'logout':
  48. logout($APP_URL);
  49. break;
  50. case 'request_token':
  51. $_SESSION['REQUEST_TOKEN'] = serialize($consumer->fetchRequestToken(
  52. implode(' ', $scopes), $APP_URL . '?action=access_token'));
  53. $consumer->authorizeRequestToken();
  54. break;
  55. case 'access_token':
  56. $_SESSION['ACCESS_TOKEN'] = serialize($consumer->fetchAccessToken());
  57. header('Location: ' . $APP_URL);
  58. break;
  59. default:
  60. if (isset($_SESSION['ACCESS_TOKEN'])) {
  61. $accessToken = unserialize($_SESSION['ACCESS_TOKEN']);
  62. $httpClient = $accessToken->getHttpClient(
  63. $consumer->getOauthOptions());
  64. $docsService = new Zend_Gdata_Docs($httpClient, $APP_NAME);
  65. $spreadsheetsService = new Zend_Gdata_Spreadsheets($httpClient,
  66. $APP_NAME);
  67. // Retrieve user's list of Google Docs and spreadsheet list.
  68. $docsFeed = $docsService->getDocumentListFeed();
  69. $spreadsheetFeed = $spreadsheetsService->getSpreadsheetFeed(
  70. 'http://spreadsheets.google.com/feeds/spreadsheets/private/full?max-results=100');
  71. renderHTML($accessToken, array($docsFeed, $spreadsheetFeed));
  72. } else {
  73. renderHTML();
  74. }
  75. }
  76. /**
  77. * Returns a the base URL of the current running web app.
  78. *
  79. * @return string
  80. */
  81. function getAppURL() {
  82. $pageURL = 'http';
  83. if ($_SERVER['HTTPS'] == 'on') {
  84. $pageURL .= 's';
  85. }
  86. $pageURL .= '://';
  87. if ($_SERVER['SERVER_PORT'] != '80') {
  88. $pageURL .= $_SERVER['SERVER_NAME'] . ':' .
  89. $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'];
  90. } else {
  91. $pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
  92. }
  93. return $pageURL;
  94. }
  95. /**
  96. * Removes session data and redirects the user to a URL.
  97. *
  98. * @param string $redirectUrl The URL to direct the user to after session data
  99. * is destroyed.
  100. * @return void
  101. */
  102. function logout($redirectUrl) {
  103. session_destroy();
  104. header('Location: ' . $redirectUrl);
  105. exit;
  106. }
  107. /**
  108. * Prints the token string and secret of the token passed in.
  109. *
  110. * @param Zend_OAuth_Token $token An access or request token object to print.
  111. * @return void
  112. */
  113. function printToken($token) {
  114. echo '<b>Token:</b>' . $token->getToken() . '<br>';
  115. echo '<b>Token secret:</b>' . $token->getTokenSecret() . '<br>';
  116. }
  117. /**
  118. * Prints basic properties of a Google Data feed.
  119. *
  120. * @param Zend_Gdata_Feed $feed A feed object to print.
  121. * @return void
  122. */
  123. function printFeed($feed) {
  124. echo '<ol>';
  125. foreach ($feed->entries as $entry) {
  126. $alternateLink = '';
  127. foreach ($entry->link as $link) {
  128. if ($link->getRel() == 'alternate') {
  129. $alternateLink = $link->getHref();
  130. }
  131. }
  132. echo "<li><a href=\"$alternateLink\" target=\"_new\">$entry->title</a></li>";
  133. }
  134. echo '</ol>';
  135. }
  136. /**
  137. * Renders the page's HTML.
  138. *
  139. * @param Zend_OAuth_Token $token (optional) The user's current OAuth token.
  140. * @param array $feeds (optional) An array of Zend_Gdata_Feed to print
  141. * information for.
  142. * @return void
  143. */
  144. function renderHTML($token=null, $feeds=null) {
  145. ?>
  146. <!DOCTYPE html>
  147. <html lang="en">
  148. <head>
  149. <meta charset=utf-8 />
  150. <link href="style.css" type="text/css" rel="stylesheet"/>
  151. </head>
  152. <body>
  153. <?php if (!isset($_SESSION['ACCESS_TOKEN'])) { ?>
  154. <button onclick="location.href='<?php echo "$APP_URL?action=request_token" ?>';">Grant Access to this app!</button>
  155. <?php } else { ?>
  156. <div id="token_info">
  157. <span style="float:left;"><img src="http://code.google.com/apis/accounts/images/oauth_icon.png"></span>
  158. <div id="token"><?php printToken($token); ?></div>
  159. </div>
  160. <div id="logout"><a href="<?php echo "$APP_URL?action=logout"; ?>">Logout</a></div>
  161. <div style="clear:both;">
  162. <div id="doclist">
  163. <h4>First 100 documents from the <a href="http://code.google.com/apis/documents/" target="_new">Documents List Data API</a>:</h4>
  164. <div class="feed"><?php printFeed($feeds[0]); ?></div>
  165. </div>
  166. <div id="spreadsheets">
  167. <h4>First 100 spreadsheets from the <a href="http://code.google.com/apis/spreadsheets/" target="_new">Spreadsheets Data API</a>:</h4>
  168. <div class="feed"><?php printFeed($feeds[1]); ?></div>
  169. </div>
  170. </div>
  171. <?php } ?>
  172. </body>
  173. </html>
  174. <?php
  175. }