Changeset 81826 in spip-zone
- Timestamp:
- Apr 11, 2014, 2:56:54 PM (7 years ago)
- Location:
- _plugins_/twitter/trunk
- Files:
-
- 4 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
_plugins_/twitter/trunk/action/twitter_oauth_authorize.php
r73938 r81826 18 18 function action_twitter_oauth_authorize_dist(){ 19 19 20 include_spip('inc/twitteroauth ');20 include_spip('inc/twitteroauthspip'); 21 21 include_spip('inc/session'); 22 22 … … 42 42 $consumer_secret = $cfg['twitter_consumer_secret']; 43 43 44 $connection = new TwitterOAuth ($consumer_key, $consumer_secret, $GLOBALS['visiteur_session']['oauth_token'], $GLOBALS['visiteur_session']['oauth_token_secret']);44 $connection = new TwitterOAuthSPIP($consumer_key, $consumer_secret, $GLOBALS['visiteur_session']['oauth_token'], $GLOBALS['visiteur_session']['oauth_token_secret']); 45 45 $access_token = $connection->getAccessToken(_request('oauth_verifier')); 46 46 session_set('access_token',$access_token); … … 86 86 87 87 include_spip('inc/filtres'); 88 include_spip('inc/twitteroauth ');88 include_spip('inc/twitteroauthspip'); 89 89 include_spip('inc/session'); 90 90 … … 101 101 */ 102 102 try { 103 $connection = new TwitterOAuth ($cfg['twitter_consumer_key'], $cfg['twitter_consumer_secret']);103 $connection = new TwitterOAuthSPIP($cfg['twitter_consumer_key'], $cfg['twitter_consumer_secret']); 104 104 $request_token = $connection->getRequestToken($oauth_callback); 105 105 $token = $request_token['oauth_token']; -
_plugins_/twitter/trunk/inc/twitter.php
r77627 r81826 86 86 * 87 87 * 88 * @return bool|TwitterOAuth 88 * @return bool|TwitterOAuthSPIP 89 89 */ 90 90 function twitter_connect($tokens=null){ … … 97 97 // Cas de twitter et oAuth 98 98 $t2 = md5(serialize($tokens)); 99 include_spip('inc/twitteroauth ');100 $connection[$t] = $connection[$t2] = new TwitterOAuth (99 include_spip('inc/twitteroauthspip'); 100 $connection[$t] = $connection[$t2] = new TwitterOAuthSPIP( 101 101 $tokens['twitter_consumer_key'], 102 102 $tokens['twitter_consumer_secret'], -
_plugins_/twitter/trunk/inc/twitteroauth.php
r73676 r81826 206 206 */ 207 207 function http($url, $method, $postfields = NULL) { 208 209 210 208 211 $this->http_info = array(); 209 212 if (!function_exists("curl_init") -
_plugins_/twitter/trunk/inc/twitteroauthspip.php
r77626 r81826 11 11 if (!defined("_ECRIRE_INC_VERSION")) return; 12 12 13 /* 14 * Abraham Williams (abraham@abrah.am) http://abrah.am 15 * 16 * The first PHP Library to support OAuth for Twitter's REST API. 17 */ 18 19 /* Load OAuth lib. You can find it at http://oauth.net */ 20 require_once('OAuth.php'); 13 include_spip('inc/twitteroauth'); 21 14 22 15 /** 23 16 * Twitter OAuth class 24 17 */ 25 class TwitterOAuth { 26 /* Contains the last HTTP status code returned. */ 27 public $http_code; 28 /* Contains the last API call. */ 29 public $url; 30 /* Set up the API root URL. */ 31 public $host = "https://api.twitter.com/1.1/"; 32 /* Set timeout default. */ 33 public $timeout = 30; 34 /* Set connect timeout. */ 35 public $connecttimeout = 30; 36 /* Verify SSL Cert. */ 37 public $ssl_verifypeer = FALSE; 38 /* Respons format. */ 39 public $format = 'json'; 40 /* Decode returned json data. */ 41 public $decode_json = TRUE; 42 /* Contains the last HTTP headers returned. */ 43 public $http_info; 44 /* Set the useragnet. */ 45 public $useragent = 'TwitterOAuth v0.2.0-beta2'; 46 /* Immediately retry the API call if the response was not successful. */ 47 //public $retry = TRUE; 48 49 50 51 52 /** 53 * Set API URLS 54 */ 55 function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; } 56 function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; } 57 function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; } 58 function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; } 59 60 /** 61 * Debug helpers 62 */ 63 function lastStatusCode() { return $this->http_status; } 64 function lastAPICall() { return $this->last_api_call; } 65 66 /** 67 * construct TwitterOAuth object 68 */ 69 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) { 70 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1(); 71 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret); 72 if (!empty($oauth_token) && !empty($oauth_token_secret)) { 73 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret); 74 } else { 75 $this->token = NULL; 76 } 77 } 78 79 80 /** 81 * Get a request_token from Twitter 82 * 83 * @returns a key/value array containing oauth_token and oauth_token_secret 84 */ 85 function getRequestToken($oauth_callback = NULL) { 86 $parameters = array(); 87 if (!empty($oauth_callback)) { 88 $parameters['oauth_callback'] = $oauth_callback; 89 } 90 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters); 91 $token = OAuthUtil::parse_parameters($request); 92 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 93 return $token; 94 } 95 96 /** 97 * Get the authorize URL 98 * 99 * @returns a string 100 */ 101 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) { 102 if (is_array($token)) { 103 $token = $token['oauth_token']; 104 } 105 if (empty($sign_in_with_twitter)) { 106 return $this->authorizeURL() . "?oauth_token={$token}"; 107 } else { 108 return $this->authenticateURL() . "?oauth_token={$token}"; 109 } 110 } 111 112 /** 113 * Exchange request token and secret for an access token and 114 * secret, to sign API calls. 115 * 116 * @returns array("oauth_token" => "the-access-token", 117 * "oauth_token_secret" => "the-access-secret", 118 * "user_id" => "9436992", 119 * "screen_name" => "abraham") 120 */ 121 function getAccessToken($oauth_verifier = FALSE) { 122 $parameters = array(); 123 if (!empty($oauth_verifier)) { 124 $parameters['oauth_verifier'] = $oauth_verifier; 125 } 126 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters); 127 $token = OAuthUtil::parse_parameters($request); 128 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 129 return $token; 130 } 131 132 /** 133 * One time exchange of username and password for access token and secret. 134 * 135 * @returns array("oauth_token" => "the-access-token", 136 * "oauth_token_secret" => "the-access-secret", 137 * "user_id" => "9436992", 138 * "screen_name" => "abraham", 139 * "x_auth_expires" => "0") 140 */ 141 function getXAuthToken($username, $password) { 142 $parameters = array(); 143 $parameters['x_auth_username'] = $username; 144 $parameters['x_auth_password'] = $password; 145 $parameters['x_auth_mode'] = 'client_auth'; 146 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters); 147 $token = OAuthUtil::parse_parameters($request); 148 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); 149 return $token; 150 } 151 152 /** 153 * GET wrapper for oAuthRequest. 154 */ 155 function get($url, $parameters = array()) { 156 $response = $this->oAuthRequest($url, 'GET', $parameters); 157 if ($this->format === 'json' && $this->decode_json) { 158 return json_decode($response,true); 159 } 160 return $response; 161 } 162 163 /** 164 * POST wrapper for oAuthRequest. 165 */ 166 function post($url, $parameters = array()) { 167 $response = $this->oAuthRequest($url, 'POST', $parameters); 168 if ($this->format === 'json' && $this->decode_json) { 169 return json_decode($response); 170 } 171 return $response; 172 } 173 174 /** 175 * DELETE wrapper for oAuthReqeust. 176 */ 177 function delete($url, $parameters = array()) { 178 $response = $this->oAuthRequest($url, 'DELETE', $parameters); 179 if ($this->format === 'json' && $this->decode_json) { 180 return json_decode($response); 181 } 182 return $response; 183 } 184 185 /** 186 * Format and sign an OAuth / API request 187 */ 188 function oAuthRequest($url, $method, $parameters) { 189 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { 190 $url = "{$this->host}{$url}.{$this->format}"; 191 } 192 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); 193 $request->sign_request($this->sha1_method, $this->consumer, $this->token); 194 switch ($method) { 195 case 'GET': 196 return $this->http($request->to_url(), 'GET'); 197 default: 198 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); 199 } 200 } 18 class TwitterOAuthSPIP extends TwitterOAuth { 201 19 202 20 /** … … 206 24 */ 207 25 function http($url, $method, $postfields = NULL) { 208 $this->http_info = array(); 209 if (!function_exists("curl_init") 210 OR !function_exists("curl_setopt") 211 OR !function_exists("curl_exec") 212 ) 213 throw new OAuthException('cURL is missing'); 26 // var_dump(parent::http($url, $method, $postfields)); 27 include_spip("inc/distant"); 214 28 215 $ci = curl_init(); 216 /* Curl settings */ 217 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); 218 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); 219 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); 220 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); 221 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); 222 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); 223 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); 224 curl_setopt($ci, CURLOPT_HEADER, FALSE); 29 $taille_max = _INC_DISTANT_MAX_SIZE; 30 $trans = false; 31 $refuser_gz = false; 32 $date_verif = ''; 33 $uri_referer = ''; 34 $datas = ''; 35 $boundary = ''; 36 $current = $url; 225 37 226 38 switch ($method) { 227 39 case 'POST': 228 curl_setopt($ci, CURLOPT_POST, TRUE); 229 if (!empty($postfields)) { 230 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); 231 } 232 break; 40 if (!empty($postfields)) { 41 if (is_string($postfields)){ 42 parse_str($postfields,$datas); 43 } 44 else 45 $datas = $postfields; 46 47 list($type, $postdata) = prepare_donnees_post($datas, $boundary); 48 $datas = $type . 'Content-Length: ' . strlen($postdata) . "\r\n\r\n" . $postdata; 49 } 50 break; 233 51 case 'DELETE': 234 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); 235 if (!empty($postfields)) { 236 $url = "{$url}?{$postfields}"; 237 } 238 } 52 if (!empty($postfields)) { 53 $current = "{$current}?{$postfields}"; 54 } 55 } 239 56 240 curl_setopt($ci, CURLOPT_URL, $url); 241 $response = curl_exec($ci); 242 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); 243 $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); 244 $this->url = $url; 245 curl_close ($ci); 57 $this->http_info = array( 58 'url' => $current, 59 'http_code' => 0, 60 'content_type' => '', 61 'header_size' => 0, 62 'request_size' => 0, 63 'redirect_count' => 0, 64 ); 65 $this->url = $current; 66 $response = ''; 67 68 // dix tentatives maximum en cas d'entetes 301... 69 for ($i = 0; $i<10; $i++){ 70 $current = recuperer_lapage($current, $trans, $method, $taille_max, $datas, $refuser_gz, $date_verif, $uri_referer); 71 if (!$current) break; 72 if (is_array($current)){ 73 break; 74 } 75 else 76 spip_log("recuperer page recommence sur $current"); 77 } 78 79 $this->info['redirect_count'] = $i; 80 if (!$current){ 81 $this->http_code = 500; 82 $this->info['http_code'] = 500; 83 if ($GLOBALS['meta']["http_proxy"]) 84 return $response; 85 else 86 return parent::http($url, $method, $postfields); 87 } 88 if (!is_array($current)){ 89 $this->http_code = 301; 90 $this->http_info['http_code'] = 301; 91 return $response; 92 } 93 94 $this->http_code = 200; 95 $this->http_info['http_code'] = 200; 96 list($headers, $response) = $current; 97 98 $this->http_info['header_size'] = strlen($headers); 99 $this->http_info['request_size'] = strlen($response); 100 101 246 102 return $response; 247 103 } 248 104 249 /**250 * Get the header info to store.251 */252 function getHeader($ch, $header) {253 $i = strpos($header, ':');254 if (!empty($i)) {255 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));256 $value = trim(substr($header, $i + 2));257 $this->http_header[$key] = $value;258 }259 return strlen($header);260 }261 105 } -
_plugins_/twitter/trunk/paquet.xml
r80123 r81826 2 2 prefix="twitter" 3 3 categorie="communication" 4 version="1. 1.3"4 version="1.2.0" 5 5 etat="stable" 6 6 compatibilite="[3.0.0;3.0.*]"
Note: See TracChangeset
for help on using the changeset viewer.