/plugins/youtubesilo/tags/0.5-0.1.1/Zend/Gdata/AuthSub.php

https://github.com/somefool/habari-extras · PHP · 224 lines · 131 code · 19 blank · 74 comment · 17 complexity · 5454056c903ea7622fcc735261ab8874 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. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Http_Client
  22. */
  23. require_once 'Zend/Http/Client.php';
  24. /**
  25. * Zend_Http_Client_Exception
  26. */
  27. require_once 'Zend/Http/Client/Exception.php';
  28. /**
  29. * Zend_Version
  30. */
  31. require_once 'Zend/Version.php';
  32. /**
  33. * Wrapper around Zend_Http_Client to facilitate Google's "Account Authentication
  34. * Proxy for Web-Based Applications".
  35. *
  36. * @see http://code.google.com/apis/accounts/AuthForWebApps.html
  37. *
  38. * @category Zend
  39. * @package Zend_Gdata
  40. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Gdata_AuthSub
  44. {
  45. const AUTHSUB_REQUEST_URI = 'https://www.google.com/accounts/AuthSubRequest';
  46. const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
  47. const AUTHSUB_REVOKE_TOKEN_URI = 'https://www.google.com/accounts/AuthSubRevokeToken';
  48. const AUTHSUB_TOKEN_INFO_URI = 'https://www.google.com/accounts/AuthSubTokenInfo';
  49. /**
  50. * Creates a URI to request a single-use AuthSub token.
  51. *
  52. * @param string $next (required) URL identifying the service to be accessed.
  53. * The resulting token will enable access to the specified service only.
  54. * Some services may limit scope further, such as read-only access.
  55. * @param string $scope (required) URL identifying the service to be accessed.
  56. * The resulting token will enable access to the specified service only.
  57. * Some services may limit scope further, such as read-only access.
  58. * @param int $secure (optional) Boolean flag indicating whether the authentication
  59. * transaction should issue a secure token (1) or a non-secure token (0). Secure tokens
  60. * are available to registered applications only.
  61. * @param int $session (optional) Boolean flag indicating whether the one-time-use
  62. * token may be exchanged for a session token (1) or not (0).
  63. */
  64. public static function getAuthSubTokenUri($next, $scope, $secure =0, $session =0)
  65. {
  66. $querystring = '?next=' . urlencode($next)
  67. . '&scope=' . urldecode($scope)
  68. . '&secure=' . urlencode($secure)
  69. . '&session=' . urlencode($session);
  70. return self::AUTHSUB_REQUEST_URI.$querystring;
  71. }
  72. /**
  73. * Upgrades a single use token to a session token
  74. *
  75. * @param string $token
  76. * @throws Zend_Gdata_App_AuthException
  77. * @throws Zend_Gdata_App_HttpException
  78. */
  79. public static function getAuthSubSessionToken($token, $client = null)
  80. {
  81. if ($client == null) {
  82. $client = new Zend_Http_Client();
  83. }
  84. if (!$client instanceof Zend_Http_Client) {
  85. require_once 'Zend/Gdata/App/HttpException.php';
  86. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
  87. }
  88. $client->setUri(self::AUTHSUB_SESSION_TOKEN_URI);
  89. $headers['authorization'] = 'AuthSub token="' . $token . '"';
  90. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  91. $client->setConfig(array(
  92. 'strictredirects' => true,
  93. 'useragent' => $useragent
  94. )
  95. );
  96. $client->setHeaders($headers);
  97. try {
  98. $response = $client->request('GET');
  99. } catch (Zend_Http_Client_Exception $e) {
  100. require_once 'Zend/Gdata/App/HttpException.php';
  101. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  102. }
  103. // Parse Google's response
  104. if ($response->isSuccessful()) {
  105. $goog_resp = array();
  106. foreach (explode("\n", $response->getBody()) as $l) {
  107. $l = chop($l);
  108. if ($l) {
  109. list($key, $val) = explode('=', chop($l), 2);
  110. $goog_resp[$key] = $val;
  111. }
  112. }
  113. return $goog_resp['Token'];
  114. } else {
  115. require_once 'Zend/Gdata/App/AuthException.php';
  116. throw new Zend_Gdata_App_AuthException('Token upgrade failed. Reason: ' . $response->getBody());
  117. }
  118. }
  119. /**
  120. * Revoke a token
  121. *
  122. * @param string $token
  123. * @return boolean
  124. * @throws Zend_Gdata_App_HttpException
  125. */
  126. public static function AuthSubRevokeToken($token, $client = null)
  127. {
  128. if ($client == null) {
  129. $client = new Zend_Http_Client();
  130. }
  131. if (!$client instanceof Zend_Http_Client) {
  132. require_once 'Zend/Gdata/App/HttpException.php';
  133. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
  134. }
  135. $client->setUri(self::AUTHSUB_REVOKE_TOKEN_URI);
  136. $headers['authorization'] = 'AuthSub token="' . $token . '"';
  137. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  138. $client->setConfig(array(
  139. 'strictredirects' => true,
  140. 'useragent' => $useragent
  141. )
  142. );
  143. $client->setHeaders($headers);
  144. ob_start();
  145. try {
  146. $response = $client->request('GET');
  147. } catch (Zend_Http_Client_Exception $e) {
  148. require_once 'Zend/Gdata/App/HttpException.php';
  149. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  150. }
  151. ob_end_clean();
  152. // Parse Google's response
  153. if ($response->isSuccessful()) {
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. }
  159. /**
  160. * get token information
  161. *
  162. * @param string $token
  163. */
  164. public static function getAuthSubTokenInfo($token, $client = null)
  165. {
  166. if ($client == null) {
  167. $client = new Zend_Http_Client();
  168. }
  169. if (!$client instanceof Zend_Http_Client) {
  170. require_once 'Zend/Gdata/App/HttpException.php';
  171. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
  172. }
  173. $client->setUri(self::AUTHSUB_TOKEN_INFO_URI);
  174. $headers['authorization'] = 'AuthSub token="' . $token . '"';
  175. $client->setHeaders($headers);
  176. ob_start();
  177. try {
  178. $response = $client->request('GET');
  179. } catch (Zend_Http_Client_Exception $e) {
  180. require_once 'Zend/Gdata/App/HttpException.php';
  181. throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
  182. }
  183. ob_end_clean();
  184. return $response->getBody();
  185. }
  186. public static function getHttpClient($token, $client = null)
  187. {
  188. if ($client == null) {
  189. $client = new Zend_Http_Client();
  190. }
  191. if (!$client instanceof Zend_Http_Client) {
  192. require_once 'Zend/Gdata/App/HttpException.php';
  193. throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
  194. }
  195. $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
  196. $client->setConfig(array(
  197. 'strictredirects' => true,
  198. 'useragent' => $useragent
  199. )
  200. );
  201. $headers['authorization'] = 'AuthSub token="' . $token . '"';
  202. $client->setHeaders($headers);
  203. return $client;
  204. }
  205. }