Twitterアカウントでログインするアプリのためのフレームワーク

オレオレフレームワーク。
説明が少ないので、意味がよくわかんないと思います。
気が向いたら更新します。


```php:App.php

<?php

require_once dirname(__FILE__).'/smarty/Smarty/Smarty.class.php';
require_once dirname(__FILE__).'/twitteroauth/twitteroauth.php';

Class App{

	protected $username;

	// アプリに応じて書き換える
	public $consumer_key = 'AAAAAAAAAAAAA1234567890';
	public $consumer_secret = 'AAAAAAAAAAAAA1234567890';
	public $access_token = 'AAAAAAAAAAAAA1234567890';
	public $access_token_secret = 'AAAAAAAAAAAAA1234567890';

	protected $_db;
	protected $_smarty;

	protected $request;

	public function __construct(){
		session_start();
		$this->setRequest();
		$this->_db = $this->getPDO();
		$this->_smarty = $this->getSmarty();

		$this->run();
	}

	protected function run(){
		// まずはログイン
		$this->login();

		// 以下、ログイン後の処理
	}

	// ログインしてない場合はここを実行
	protected function unloginIndex(){
	}


	protected function login(){
		// ログインしようとしているなら、手続きを開始させる
		if(isset($this->request['mode']) && $this->request['mode'] == 'login'){
			$this->gotoTwitter();
		}
		// Twitterで認証を受けて返ってきたなら、
		elseif(isset($_SESSION['request_token']) && isset($_SESSION['request_token_secret']) &&
		isset($this->request['oauth_token']) && isset($this->request['oauth_verifier'])){

			// もっかいリクエスト
			$client = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $_SESSION['request_token'], $_SESSION['request_token_secret']);
			$token = $client->getAccessToken($this->request['oauth_verifier']);
			// ちゃんとトークンがあれば
			if(isset($token['oauth_token']) && isset($token['oauth_token_secret']) && isset($token['screen_name'])){
				// 存在しなければ
				if(false === $this->db_user_exist($token['screen_name'])){
					$this->db_user_add($token['oauth_token'], $token['oauth_token_secret'], $token['screen_name']);
				}
				// DBと違っていれば、まあ認証の方が正しいだろうからupdate
				elseif(false === $this->db_user_auth($token['oauth_token'], $token['oauth_token_secret'], $token['screen_name'])){
					$this->db_user_update($token['oauth_token'], $this->request['oauth_token_secret'], $this->request['screen_name']);
				}
				unset($_SESSION['request_token']);
				unset($_SESSION['request_token_secret']);
				$_SESSION['access_token'] = $token['oauth_token'];
				$_SESSION['access_token_secret'] = $token['oauth_token_secret'];
				$_SESSION['username'] = $token['screen_name'];
			}
		}
		// ログインできるかどうかに必要なアクセストークンを持っているなら、それを照合
		elseif(isset($_SESSION['access_token']) && isset($_SESSION['access_token_secret']) && isset($_SESSION['username'])){
			if(false === $this->db_user_auth($_SESSION['access_token'], $_SESSION['access_token_secret'], $_SESSION['username'])){
				$this->gotoTwitter();
			}
			else {
				unset($_SESSION['request_token']);
				unset($_SESSION['request_token_secret']);
			}
		}
		else {
			$this->unloginIndex();
			unset($_SESSION['request_token']);
			unset($_SESSION['request_token_secret']);
			exit();
		}

		// セッションは絶対あるはずだから、ユーザ名を格納
		$this->username = $_SESSION['username'];
	}


	protected function gotoTwitter(){
		$to = new TwitterOAuth($this->consumer_key,$this->consumer_secret);
		$request_token = $to->getRequestToken();
		$_SESSION['request_token'] = $request_token['oauth_token'];
		$_SESSION['request_token_secret'] = $request_token['oauth_token_secret'];
		$url = $to->getAuthorizeURL($request_token);
		header('Location: '.$url);
	}

	protected function db_user_add($access_token, $access_token_secret, $username){
		$pdo = $this->_db;
		$sql = <<<EOM
insert into user
(username, access_token, access_token_secret) values
(:username, :access_token, :access_token_secret)
EOM;
		$sth = $pdo->prepare($sql);
		$sth->bindValue(':username', $username, PDO::PARAM_STR);
		$sth->bindValue(':access_token', $access_token, PDO::PARAM_STR);
		$sth->bindValue(':access_token_secret', $access_token_secret, PDO::PARAM_STR);
		$sth->execute();

		return true;
	}

	protected function db_user_update($access_token, $access_token_secret, $username){
		$pdo = $this->_db;
		$sql = <<<EOM
update user
set
access_token = :access_token,
access_token_secret = :access_token_secret
where username = :username
EOM;
		$sth = $pdo->prepare($sql);
		$sth->bindValue(':username', $username, PDO::PARAM_STR);
		$sth->bindValue(':access_token', $access_token, PDO::PARAM_STR);
		$sth->bindValue(':access_token_secret', $access_token_secret, PDO::PARAM_STR);
		$sth->execute();

		return true;
	}


	protected function db_user_auth($access_token, $access_token_secret, $username){
		$pdo = $this->_db;
		$sql = <<<EOM
select * from user
where access_token = :access_token and
access_token_secret = :access_token_secret and
username = :username
EOM;
		$sth = $pdo->prepare($sql);
		$sth->bindValue(':access_token',  $access_token,  PDO::PARAM_STR);
		$sth->bindValue(':access_token_secret',  $access_token_secret,  PDO::PARAM_STR);
		$sth->bindValue(':username',  $username,  PDO::PARAM_STR);
		$sth->execute();
		$result = $sth->fetch(PDO::FETCH_ASSOC);
		$sth->closeCursor();

		if($result){
			return $result;
		}
		else {
			return false;
		}
	}

	protected function db_user_exist($username){
		$pdo = $this->_db;
		$sql = <<<EOM
select * from user
where username = :username
EOM;
		$sth = $pdo->prepare($sql);
		$sth->bindValue(':username',  $username,  PDO::PARAM_STR);
		$sth->execute();
		$result = $sth->fetch(PDO::FETCH_ASSOC);
		$sth->closeCursor();

		if($result){
			return $result;
		}
		else {
			return false;
		}
	}

	protected function getSmarty(){

		// オブジェクトの生成
		$smarty = new Smarty;

		// Smarty の設定
		$smarty->caching = false;
		$smarty->compile_check = true;
		$smarty->template_dir = dirname(__FILE__).'/smarty/templates/';
		$smarty->compile_dir  = dirname(__FILE__).'/smarty/templates_c/';
		$smarty->config_dir   = dirname(__FILE__).'/smarty/configs/';
		$smarty->cache_dir    = dirname(__FILE__).'/smarty/cache/';

		return $smarty;
	}

	protected function getPDO(){
		// MySQL connection
		$db = new PDO('mysql:host=localhost;dbname=dbname',
		'username', 'password');
		$db->query("SET NAMES utf8;");

		return $db;
	}

	protected function setRequest(){
		$this->request = $_GET+$_POST;

		return true;
	}
}

?>

```