/mod/oauth_api/vendors/oauth/library/store/OAuthStoreAbstract.class.php

https://github.com/wangaiying/elgg4ysu · PHP · 149 lines · 77 code · 18 blank · 54 comment · 5 complexity · 6dbe3011e61e021dd1a28aea091aafb4 MD5 · raw file

  1. <?php
  2. /**
  3. * Abstract base class for OAuthStore implementations
  4. *
  5. * @version $Id$
  6. * @author Marc Worrell <marcw@pobox.com>
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2007-2008 Mediamatic Lab
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. abstract class OAuthStoreAbstract
  31. {
  32. abstract public function getSecretsForVerify ( $consumer_key, $token, $token_type = 'access' );
  33. abstract public function getSecretsForSignature ( $uri, $user_id );
  34. abstract public function getServerTokenSecrets ( $consumer_key, $token, $token_type, $user_id, $name = '' );
  35. abstract public function addServerToken ( $consumer_key, $token_type, $token, $token_secret, $user_id, $options = array() );
  36. abstract public function deleteServer ( $consumer_key, $user_id, $user_is_admin = false );
  37. abstract public function getServer( $consumer_key, $user_id, $user_is_admin = false );
  38. abstract public function getServerForUri ( $uri, $user_id );
  39. abstract public function listServerTokens ( $user_id );
  40. abstract public function countServerTokens ( $consumer_key );
  41. abstract public function getServerToken ( $consumer_key, $token, $user_id );
  42. abstract public function deleteServerToken ( $consumer_key, $token, $user_id, $user_is_admin = false );
  43. abstract public function listServers ( $q = '', $user_id );
  44. abstract public function updateServer ( $server, $user_id, $user_is_admin = false );
  45. abstract public function updateConsumer ( $consumer, $user_id, $user_is_admin = false );
  46. abstract public function deleteConsumer ( $consumer_key, $user_id, $user_is_admin = false );
  47. abstract public function getConsumer ( $consumer_key, $user_id, $user_is_admin = false );
  48. abstract public function getConsumerStatic ();
  49. abstract public function addConsumerRequestToken ( $consumer_key, $options = array() );
  50. abstract public function getConsumerRequestToken ( $token );
  51. abstract public function deleteConsumerRequestToken ( $token );
  52. abstract public function authorizeConsumerRequestToken ( $token, $user_id, $referrer_host = '' );
  53. abstract public function countConsumerAccessTokens ( $consumer_key );
  54. abstract public function exchangeConsumerRequestForAccessToken ( $token, $options = array() );
  55. abstract public function getConsumerAccessToken ( $token, $user_id );
  56. abstract public function deleteConsumerAccessToken ( $token, $user_id, $user_is_admin = false );
  57. abstract public function setConsumerAccessTokenTtl ( $token, $ttl );
  58. abstract public function listConsumers ( $user_id );
  59. abstract public function listConsumerTokens ( $user_id );
  60. abstract public function checkServerNonce ( $consumer_key, $token, $timestamp, $nonce );
  61. abstract public function addLog ( $keys, $received, $sent, $base_string, $notes, $user_id = null );
  62. abstract public function listLog ( $options, $user_id );
  63. abstract public function install ();
  64. /**
  65. * Fetch the current static consumer key for this site, create it when it was not found.
  66. * The consumer secret for the consumer key is always empty.
  67. *
  68. * @return string consumer key
  69. */
  70. /* ** Some handy utility functions ** */
  71. /**
  72. * Generate a unique key
  73. *
  74. * @param boolean unique force the key to be unique
  75. * @return string
  76. */
  77. public function generateKey ( $unique = false )
  78. {
  79. $key = md5(uniqid(rand(), true));
  80. if ($unique)
  81. {
  82. list($usec,$sec) = explode(' ',microtime());
  83. $key .= dechex($usec).dechex($sec);
  84. }
  85. return $key;
  86. }
  87. /**
  88. * Check to see if a string is valid utf8
  89. *
  90. * @param string $s
  91. * @return boolean
  92. */
  93. protected function isUTF8 ( $s )
  94. {
  95. return preg_match('%(?:
  96. [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  97. |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  98. |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  99. |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  100. |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  101. |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  102. |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  103. )+%xs', $s);
  104. }
  105. /**
  106. * Make a string utf8, replacing all non-utf8 chars with a '.'
  107. *
  108. * @param string
  109. * @return string
  110. */
  111. protected function makeUTF8 ( $s )
  112. {
  113. if (function_exists('iconv'))
  114. {
  115. do
  116. {
  117. $ok = true;
  118. $text = @iconv('UTF-8', 'UTF-8//TRANSLIT', $s);
  119. if (strlen($text) != strlen($s))
  120. {
  121. // Remove the offending character...
  122. $s = $text . '.' . substr($s, strlen($text) + 1);
  123. $ok = false;
  124. }
  125. }
  126. while (!$ok);
  127. }
  128. return $s;
  129. }
  130. }
  131. ?>