Skip to content

Commit

Permalink
Twitter xAuth Support
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Apr 29, 2010
1 parent 78b240e commit ea57ced
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 9 deletions.
6 changes: 3 additions & 3 deletions libs/TW2MV/Client.php
Expand Up @@ -5,14 +5,14 @@
*
* PHP versions 5
*
* Copyright 2009, nojimage (http://php-tips.com/)
* Copyright 2010, nojimage (http://php-tips.com/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @version 1.1
* @author nojimage <nojimage at gmail.com>
* @copyright 2009 nojimage
* @copyright 2010 nojimage
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://php-tips.com/
* @package tw2mv
Expand Down Expand Up @@ -59,7 +59,7 @@ public function __construct($config)
try {
$this->http = new HTTP_Request2();
$this->http->setConfig('ssl_verify_peer', false);

} catch (Exception $e) {
debug($e->getMessage());

Expand Down
88 changes: 88 additions & 0 deletions libs/TW2MV/Configure.php
Expand Up @@ -24,6 +24,13 @@
*/
class TW2MV_Configure
{
/**
*
* @var string
* @since version 2.1.0
*/
public $config_file = '';

/**
*
* @var array
Expand Down Expand Up @@ -118,6 +125,34 @@ class TW2MV_Configure
*/
public $twitter_filter_allows = array();

/**
* OAuth Consumer Key
* @var string
* @since version 2.1.0
*/
public $twitter_oauth_consumer_key = 'sKLeVx7IpK3rKByLn218w';

/**
* OAuth Consumer Secret Key
* @var string
* @since version 2.1.0
*/
public $twitter_oauth_consumer_secret = 'DSwg6gZyPoh3frNBXGY7lzuxU2X8DYh9nrj0';

/**
* OAuth Access Token
* @var string
* @since version 2.1.0
*/
public $twitter_oauth_access_token = '';

/**
* OAuth Access Token Secret
* @var string
* @since version 2.1.0
*/
public $twitter_oauth_access_token_secret = '';

/**
* パスワードが暗号化されている場合に付与される文字列
* @var string
Expand Down Expand Up @@ -202,6 +237,8 @@ function load($config_file, $options = null)

$this->config = $config;

$this->config_file = $config_file;

return get_object_vars($this);
}

Expand Down Expand Up @@ -347,11 +384,62 @@ static function decrypt($cipher, $key = null)

/**
* 暗号化用のキーを取得する
*
* @return string
*/
static function get_secure_key()
{
$key_file = CONFIG_DIR . 'secret_key.php';
return (is_file($key_file)) ? sha1(file_get_contents($key_file)) : 'gre#jTG%EihogNu04t6uXewR@lglew';
}

/**
* Access Tokenを保存
*
* @param $oauth_token
* @param $oauth_token_secret
* @since version 2.1.0
*/
function saveTwitterAccessToken($oauth_token, $oauth_token_secret)
{

$this->twitter_oauth_access_token = $oauth_token;
$this->twitter_oauth_access_token_secret = $oauth_token_secret;

// 設定ファイル読み込み
$file_contents = file_get_contents($this->config_file);

if (preg_match('/^(twitter.oauth_access_token)\s*=\s*".*?"/i', $file_contents)) {

$file_contents = preg_replace('/^(twitter.oauth_access_token)\s*=\s*".*?"/i', '$1 = "'. $oauth_token . '"', $file_contents);

} else {

$file_contents .= "\n";
$file_contents .= 'twitter.oauth_access_token = "' . $oauth_token . '"';

}

if (preg_match('/^(twitter.oauth_access_token_secret)\s*=\s*".*?"/i', $file_contents)) {

$file_contents = preg_replace('/^(twitter.oauth_access_token_secret)\s*=\s*".*?"/i', '$1 = "'. $oauth_token_secret . '"', $file_contents);

} else {

$file_contents .= "\n";
$file_contents .= 'twitter.oauth_access_token_secret = "' . $oauth_token_secret . '"';

}

// 書き出し
if ($fh = fopen($this->config_file, 'a')) {
if (flock($fh, LOCK_EX)) {
ftruncate($fh, 0);
fwrite($fh, $file_contents);
flock($fh, LOCK_UN);
}
fclose($fh);
}

}
}
127 changes: 121 additions & 6 deletions libs/TW2MV/Twitter.php
@@ -1,20 +1,21 @@
<?php
require_once 'Client.php';
require_once 'HTTP' . DS . 'OAuth' . DS . 'Consumer.php';
/**
* TW2MV_Twitter
*
* Twitterの処理
*
* PHP versions 5
*
* Copyright 2009, nojimage (http://php-tips.com/)
* Copyright 2010, nojimage (http://php-tips.com/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @version 1.1
* @version 1.2
* @author nojimage <nojimage at gmail.com>
* @copyright 2009 nojimage
* @copyright 2010 nojimage
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://php-tips.com/
* @package tw2mv
Expand All @@ -31,6 +32,59 @@ class TW2MV_Twitter extends TW2MV_Client
*/
static $HTTP_URI = 'http://api.twitter.com/1/';

/**
* HTTP_OAuth_Consumer
*
* @var HTTP_OAuth_Consumer
* @since version 2.1.0
*/
protected $oauth;

/**
* xAuth request url
*
* @var string
* @since version 2.1.0
*/
static $XAUTH_ACCESS_TOKEN_REQUEST_URL = 'https://api.twitter.com/oauth/access_token';

/**
*
* @param TW2MV_Configure
* @since version 2.1.0
*/
public function __construct($config)
{
parent::__construct($config);

try {

// OAuthリクエスト
$consumer_request = new HTTP_OAuth_Consumer_Request();
$consumer_request->accept($this->http);

$this->oauth = new HTTP_OAuth_Consumer($this->config->twitter_oauth_consumer_key, $this->config->twitter_oauth_consumer_secret);
$this->oauth->accept($consumer_request);

if (empty($this->config->twitter_oauth_access_token) || empty($this->config->twitter_oauth_access_token_secret)) {

// Access Tokenを取得
$this->_getAccessToken();

}

// トークンをセット
$this->oauth->setToken($this->config->twitter_oauth_access_token);
$this->oauth->setTokenSecret($this->config->twitter_oauth_access_token_secret);

} catch (Exception $e) {

debug($e->getMessage());

}

}

/**
* Twitterから発言を取得
* @return array
Expand Down Expand Up @@ -83,15 +137,20 @@ function post($message)
$datas = array('status' => $message->make_message(140, $this->config->twitter_message_suffix));

if ($this->config->core_fetch_only) {

debug($datas);

} else {

// 投稿
$this->http->setAuth($this->config->twitter_username, TW2MV_Configure::decrypt($this->config->twitter_password));
$result = $this->_json_decode($this->post_request(self::$HTTP_URI . 'statuses/update.json', $datas, true));

}

if (!empty($result->error)) {

debug($result->error);

}

return empty($result->error);
Expand All @@ -110,10 +169,13 @@ function direct_message($message, $to)

// 投稿
if ($this->config->core_fetch_only) {

debug($datas);

} else {
$this->http->setAuth($this->config->twitter_username, TW2MV_Configure::decrypt($this->config->twitter_password));

$result = $this->_json_decode($this->post_request(self::$HTTP_URI . 'direct_messages/new.json', $datas, true));

}

return empty($result->error);
Expand Down Expand Up @@ -208,4 +270,57 @@ protected function _filter($messages)

return $passed;
}

/**
* OAuth Access Tokenの取得
*
* @since version 2.1.0
*/
protected function _getAccessToken()
{
$params = array(
'x_auth_mode' => 'client_auth',
'x_auth_username' => $this->config->twitter_username,
'x_auth_password' => TW2MV_Configure::decrypt($this->config->twitter_password));

$response = $this->oauth->sendRequest(self::$XAUTH_ACCESS_TOKEN_REQUEST_URL, $params, HTTP_Request2::METHOD_POST);

if ($response->getStatus() !== 200) {
throw new Exception($response->getBody(), $response->getStatus());
}

// レスポンスデータを解析
$access_token_info = array();
parse_str($response->getBody(), $access_token_info);

// 設定ファイルへ保存
$this->config->saveTwitterAccessToken($access_token_info['oauth_token'], $access_token_info['oauth_token_secret']);
}

/**
* POST Request
*
* @param $url
* @param $datas
* @return string
* @since version 2.1.0
*/
public function post_request($url, $datas = array())
{

$body = '';

try {

$response = $this->oauth->sendRequest($url, $datas, HTTP_Request2::METHOD_POST);
$body = $response->getBody();

} catch (Exception $e) {

debug($e->getMessage());

}

return $body;
}
}

0 comments on commit ea57ced

Please sign in to comment.