Skip to content

Instantly share code, notes, and snippets.

@dogmatic69
Created March 8, 2011 02:24
Show Gist options
  • Save dogmatic69/859741 to your computer and use it in GitHub Desktop.
Save dogmatic69/859741 to your computer and use it in GitHub Desktop.
setting up the basics
<?php
class Connect extends TwitterAppModel{
/**
*
* @var string
*/
public $name = 'Connect';
public $schema = array(
'title' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'link' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'description' => array(
'type' => 'text',
'null' => true,
'key' => 'primary',
'length' => null
),
'publisher' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'creator' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
),
'date' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 255
)
);
public $request = array(
'uri' => array(
'host' => 'api.twitter.com',
'path' => '',
),
'method' => 'GET',
'auth' => array(
'method' => 'OAuth',
'oauth_callback' => true,
'oauth_consumer_key' => true,
'oauth_consumer_secret' => true,
'oauth_token' => null,
'oauth_verifier' => null
)
);
/**
* twitter oauth url
*
* @var string
*/
public $authorizeUrl = 'http://api.twitter.com/oauth/authorize?oauth_token=%s';
public $map = array(
'count' => 'Rss.Channel.totalResults',
'limit' => 'Rss.Channel.itemsPerPage',
'page' => 'Rss.Channel.startIndex',
);
public $requestTypes = array(
'request' => '/oauth/request_token',
'access' => '/oauth/access_token'
);
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
Configure::load('twitter.config');
$config = Configure::read('Twitter');
if(isset($this->request['auth']['oauth_callback'])){
$this->request['auth']['oauth_callback'] = Router::url($config['callback_url'], true);
}
$this->request['auth']['oauth_consumer_key'] = $config['consumer_key'];
$this->request['auth']['oauth_consumer_secret'] = $config['consumer_secret'];
}
/**
* set the query up as per the find conditions.
*/
public function beforeFind($queryData){
$this->request['uri']['path'] = $this->requestTypes['request'];
if(isset($this->requestTypes[$queryData['conditions']['Connect.type']])){
$this->request['uri']['path'] = $this->requestTypes[$queryData['conditions']['Connect.type']];
}
if(isset($queryData['conditions']['Connect.oauth_token']) && isset($queryData['conditions']['Connect.oauth_verifier'])){
$this->request['auth']['oauth_token'] = $queryData['conditions']['Connect.oauth_token'];
$this->request['auth']['oauth_verifier'] = $queryData['conditions']['Connect.oauth_verifier'];
}
$queryData['conditions'] = 'raw';
return $queryData;
}
/**
* does not want to work ffs
*/
public function afterFind($results, $primary){
return $results;
if(is_string($results[0]['Connect'])){
return $this->formatQueryString($results[0]['Connect']);
}
}
/**
* does not want to work in afterSave()
*/
public function formatQueryString($results){
$results = current($results);
parse_str($results, $results);
$return[$this->name] = $results;
return $return;
}
}
<?php
class TwitterAppModel extends Model{
/**
* Model name
*
* @var string
*/
public $name = 'TwitterAppModel';
/**
* database configuration to use
*
* @var string
*/
public $useDbConfig = 'twitter';
/**
* Behaviors to attach
*
* @var mixed
*/
public $actsAs = false;
/**
* database table to use
*
* @var string
*/
public $useTable = false;
}
<?php
class ConnectsController extends TwitterAppController {
/**
* class name
*
* @var string
*/
public $name = 'Connects';
/**
* use the model for conecting and authenticating
*/
public $uses = array(
'Twitter.Connect'
);
/**
* Components to load
*
* @var array
*/
public $components = array(
'Session'
);
/**
* need to figure out what this is about.
*/
public function beforeFilter(){
parent::beforeFilter();
$this->Connect->enabled = null;
}
/**
* connect to twitter
*/
public function connect() {
if($this->Session->read('Twitter')){
$this->Session->setFlash(__('Your twitter account is already linked', true));
$this->redirect();
}
$this->Session->write('Twitter.referer', $this->referer());
$connection = $this->Connect->find(
'first',
array(
'conditions' => array(
'Connect.type' => 'request'
)
)
);
$connection = $this->Connect->formatQueryString($connection);
$url = sprintf($this->Connect->authorizeUrl, $connection['Connect']['oauth_token']);
$this->redirect($url);
}
/**
* callback for twitter to give the app a key
*/
public function callback() {
if(!$this->Session->read('Twitter.referer')){
$this->Session->setFlash(__('Something went wrong, please try again', true));
$this->redirect('/');
}
$connection = $this->Connect->find(
'first',
array(
'conditions' => array(
'Connect.type' => 'access',
'Connect.oauth_token' => $this->params['url']['oauth_token'],
'Connect.oauth_verifier' => $this->params['url']['oauth_verifier']
)
)
);
$connection = $this->Connect->formatQueryString($connection);
if(!isset($connection['Connect']) || empty($connection['Connect'])){
$this->Session->setFlash(__('There was an error authenticating you, please try agian', true));
$this->redirect('/');
}
$this->Session->write('Twitter', $connection['Connect']);
$this->__linkOrCreateAccount($connection['Connect']);
}
/**
* log them out the twitter profile by deleting the session and then
* head to infinitas logout
*
* @param array $options options for the logout
* redirect - where to send the user
*
* http://dev.twitter.com/doc/post/account/end_session
*/
public function logout(){
$_options['redirect'] = $this->referer();
$options = array_merge($_options, (array)Configure::read('Twitter.logout'));
$this->Session->delete('Twitter');
$this->redirect($options['redirect']);
}
/**
* when someone wants to remove their account
*/
public function unlink(){
$id = $this->Session->read('Auth.User.id');
if(!$id){
$this->Session->setFlash(__('You are not allowed to do that', true));
$this->redirect('/');
}
ClassRegistry::init('Users.User')->save(array('User' => array('User.id' => $id, 'User.twitter_id' => 0)));
}
/**
* figure out what someone needs when they log in.
*/
private function __linkOrCreateAccount($user = null){
$redirect = $this->Session->read('Twitter.redirect');
$User = ClassRegistry::init('Users.User');
$id = $this->Session->read('Auth.User.id');
$linked = $User->find(
'first',
array(
'conditions' => array(
'User.twitter_id' => $user['user_id']
)
)
);
var_dump(!$linked && $id);
// not linked and logged in
if(!$linked && $id){
$User->id = $id;
$User->saveField('twitter_id', $user['user_id']);
$this->Session->write('Twitter.new_user', true);
$this->redirect($redirect);
}
// linked but not logged in
else if(!empty($linked) && !$id){
$this->Session->write('Auth.User', $existing['User']);
$this->redirect($redirect);
}
// completely new user
else{
$User->create();
$User->save(
array(
'User' => array(
'User.username' => $user['screen_name'],
'User.twitter_id' => $user['user_id'],
'User.password' => sha1($user['user_id'])
)
),
array(
'validate' => false
)
);
$this->Session->write('Twitter.new_user', true);
$this->redirect($redirect);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment