PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/statusnet/index.php

https://github.com/whale2/users
PHP | 119 lines | 97 code | 17 blank | 5 comment | 8 complexity | c5d9ce83cf51033ab31b361231242771 MD5 | raw file
  1. <?php
  2. require_once(dirname(dirname(dirname(__FILE__))).'/OAuthModule.php');
  3. class StatusNetAuthenticationModule extends OAuthAuthenticationModule
  4. {
  5. private $title;
  6. private $rootURL;
  7. private $APIRootURL;
  8. protected $userCredentialsClass = 'StatusNetUserCredentials';
  9. public function __construct($oAuthConsumerKey, $oAuthConsumerSecret, $title = 'Status.Net', $rootURL = 'https://identi.ca/', $APIrootURL = null)
  10. {
  11. /** Support auth through multiple statusnet services in the same instance */
  12. $this->title = $title;
  13. $this->rootURL = $rootURL;
  14. if (is_null($APIrootURL)) {
  15. $this->APIRootURL = $rootURL.'api/';
  16. } else {
  17. $this->APIRootURL = $APIrootURL;
  18. }
  19. parent::__construct(
  20. $this->title,
  21. $this->APIRootURL,
  22. $oAuthConsumerKey,
  23. $oAuthConsumerSecret,
  24. $this->APIRootURL.'oauth/request_token',
  25. $this->APIRootURL.'oauth/access_token',
  26. $this->APIRootURL.'oauth/authorize',
  27. array('HMAC-SHA1'),
  28. $this->APIRootURL,
  29. UserConfig::$USERSROOTURL.'/modules/statusnet/StatusNet_badge_green.png',
  30. UserConfig::$USERSROOTURL.'/modules/statusnet/StatusNet_badge_green.png',
  31. UserConfig::$USERSROOTURL.'/modules/statusnet/StatusNet_badge_green.png',
  32. array(
  33. array(4501, 'Logged in using '.$this->title.' account', 1),
  34. array(4502, 'Added '.$this->title.' account', 1),
  35. array(4503, 'Removed '.$this->title.' account', 0),
  36. array(4504, 'Registered using '.$this->title.' account', 1),
  37. )
  38. );
  39. }
  40. public function getID()
  41. {
  42. return "statusnet";
  43. }
  44. public function getLegendColor()
  45. {
  46. return "91a93b";
  47. }
  48. public function getTitle()
  49. {
  50. return $this->title;
  51. }
  52. public function getIdentity($oauth_user_id) {
  53. // get twitter handle
  54. $request = new OAuthRequester($this->APIRootURL.'account/verify_credentials.json', 'GET');
  55. $result = $request->doRequest($oauth_user_id);
  56. if ($result['code'] == 200) {
  57. $data = json_decode($result['body'], true);
  58. if (is_null($data)) {
  59. switch(json_last_error())
  60. {
  61. case JSON_ERROR_DEPTH:
  62. error_log('JSON Error: Maximum stack depth exceeded');
  63. break;
  64. case JSON_ERROR_CTRL_CHAR:
  65. error_log('JSON Error: Unexpected control character found');
  66. break;
  67. case JSON_ERROR_SYNTAX:
  68. error_log('JSON Error: Syntax error, malformed JSON');
  69. break;
  70. case JSON_ERROR_NONE:
  71. error_log('JSON Error: No errors');
  72. break;
  73. }
  74. return null;
  75. }
  76. if (!is_null($data) && array_key_exists('id', $data) && array_key_exists('name', $data)) {
  77. return $data;
  78. }
  79. }
  80. return null;
  81. }
  82. protected function renderUserInfo($serialized_userinfo) {
  83. $user_info = unserialize($serialized_userinfo);
  84. ?>@<a href="<?php echo $this->rootURL . UserTools::escape($user_info['screen_name']); ?>" target="_blank"><?php echo UserTools::escape($user_info['screen_name']); ?></a><br/><?php
  85. }
  86. /**
  87. * Call to Status.Net Twitter API using OAuth
  88. */
  89. protected function api_call($path, $method = "GET", $params = null, $body = null, $files = null) {
  90. return makeOAuthRequest(
  91. $this->$APIRootURL.$path,
  92. $method,
  93. $params,
  94. $body,
  95. $files);
  96. }
  97. }
  98. class StatusNetUserCredentials extends OAuthUserCredentials {
  99. public function getHTML() {
  100. return '@<a href="'.$this->rootURL . UserTools::escape($this->userinfo['screen_name']).'" target="_blank">'.$this->userinfo['screen_name'].'</a>';
  101. }
  102. }