Changeset 1291
- Timestamp:
- 02/13/11 17:53:44 (2 years ago)
- Location:
- Files:
-
- 6 modified
- 1 moved
-
files/lib/data/twitter/Twitter.class.php (modified) (2 diffs)
-
files/lib/page/TwitterPage.class.php (modified) (5 diffs)
-
files/lib/system/event/listener/UserLoginTwitterListener.class.php (moved) (moved from twitter/files/lib/system/event/listener/UserLoginTwitter.class.php) (2 diffs)
-
files/lib/util/TwitterUtil.class.php (modified) (8 diffs)
-
install.sql (modified) (2 diffs)
-
package.xml (modified) (1 diff)
-
templates/twitterLogin.tpl (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
twitter/files/lib/data/twitter/Twitter.class.php
r1290 r1291 1 1 <?php 2 require_once(dirname(__FILE__).'/EpiCurl.php'); 3 require_once(dirname(__FILE__).'/EpiOAuth.php'); 4 require_once(dirname(__FILE__).'/EpiSequence.php'); 5 require_once(dirname(__FILE__).'/EpiTwitter.php'); 2 require_once(dirname(__FILE__).'/EpiTwitter/EpiCurl.php'); 3 require_once(dirname(__FILE__).'/EpiTwitter/EpiOAuth.php'); 4 require_once(dirname(__FILE__).'/EpiTwitter/EpiTwitter.php'); 6 5 7 6 /** 8 7 * 8 * @author Torben Brodt 9 * @copyright 2010 easy-coding.de 10 * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> 11 * @package de.easy-coding.wcf.twitter 9 12 */ 10 13 class Twitter extends EpiTwitter { 11 14 12 15 /** 16 * @var string 17 */ 18 protected $userAgent = 'TwitterWCF (http://trac.easy-coding.de/trac/wcf/wiki/Twitter)'; 19 20 /** 21 * remember these requests in session 22 * @var array<string> 23 */ 24 protected $cache = array( /*'/account/verify_credentials.json'*/); 25 26 /** 13 27 * @var Twitter 14 28 */ 15 protected $instance = null;29 protected static $instance = null; 16 30 17 31 /** … … 24 38 return self::$instance; 25 39 } 40 41 public function getToken() { 42 return array( 43 'oauth_token' => $this->token, 44 'oauth_token_secret' => $this->tokenSecret 45 ); 46 } 47 48 /** 49 * @return boolean 50 */ 51 public function startSession() { 52 $return = false; 53 54 if(isset($_GET['oauth_token'])) { 55 $this->setToken($_GET['oauth_token']); 56 $token = $this->getAccessToken(); 57 $this->setToken($token->oauth_token, $token->oauth_token_secret); 58 $return = true; 59 } 60 61 else if(WCF::getUser()->userID) { 62 $me = TwitterUtil::getTwitterAccount(WCF::getUser()->userID); 63 if($me) { 64 $this->setToken($me['userToken'], $me['userSecret']); 65 $return = true; 66 } 67 } 68 return $return; 69 } 70 71 /** 72 * wraps some method calls with session based cache 73 * @override 74 */ 75 public function get($endpoint, $params = null) { 76 if(in_array($endpoint, $this->cache)) { 77 $return = WCF::getSession()->getVar($endpoint); 78 if(!empty($return)) { 79 return $return; 80 } 81 } 82 $return = parent::get($endpoint, $params); 83 84 if(in_array($endpoint, $this->cache)) { 85 WCF::getSession()->register($endpoint, $return); 86 } 87 return $return; 88 } 26 89 } -
twitter/files/lib/page/TwitterPage.class.php
r1179 r1291 1 1 <?php 2 2 require_once(WCF_DIR.'lib/page/AbstractPage.class.php'); 3 require_once(WCF_DIR.'lib/data/twitter/Twitter.class.php'); 4 require_once(WCF_DIR.'lib/util/TwitterUtil.class.php'); 3 5 4 6 /** … … 11 13 class TwitterPage extends AbstractPage { 12 14 public $templateName = 'twitter'; 13 public $twitters = array();14 15 15 16 /** … … 19 20 parent::readData(); 20 21 21 $minLifetime = 0; 22 $maxLifetime = 900; // 0:15h 22 if(isset($_GET['oauth_token'])) { 23 $twitterObj = Twitter::getInstance(); 24 $twitterObj->setToken($_GET['oauth_token']); 25 $token = $twitterObj->getAccessToken(); 26 $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret); 23 27 24 WCF::getCache()->addResource( 25 'twitter.wcf', 26 WCF_DIR.'cache/cache.twitter.wcf.php', 27 WCF_DIR.'lib/system/cache/CacheBuilderTwitter.class.php', 28 $minLifetime, 29 $maxLifetime 30 ); 31 32 $this->twitters = WCF::getCache()->get('twitter.wcf'); 28 // save to cookies 29 setcookie('oauth_token', $token->oauth_token); 30 setcookie('oauth_token_secret', $token->oauth_token_secret); 31 32 $twitterInfo= $twitterObj->get_accountVerify_credentials(); 33 echo "<h1>Your twitter username is {$twitterInfo->screen_name} and your profile picture is <img src=\"{$twitterInfo->profile_image_url}\"></h1> 34 <p><a href=\"random.php\">Go to another page and load your friends list from your cookie</p>"; 35 } 33 36 } 34 37 … … 40 43 41 44 WCF::getTPL()->assign(array( 42 'twitters' => $this->twitters,43 45 'allowSpidersToIndexThisPage' => true 44 46 )); … … 52 54 require_once(WCF_DIR.'lib/page/util/menu/PageMenu.class.php'); 53 55 PageMenu::setActiveMenuItem('wcf.twitter.title'); 54 55 // check permission56 WCF::getUser()->checkPermission('user.twitter.canViewTwitterPage');57 56 58 57 // check module options -
twitter/files/lib/system/event/listener/UserLoginTwitterListener.class.php
r1286 r1291 20 20 21 21 switch($className) { 22 // login or register with twitter 23 case 'UserLoginForm': 24 // readData 25 TwitterUtil::loginOrRegister($eventObj); 26 break; 27 22 28 // registered user links with twitter 23 29 case 'UserProfileEditForm': … … 27 33 } 28 34 break; 35 36 // password validations can be skipped if twitter auth is successfully 37 case 'AccountManagementForm': 38 // validate 39 if($eventName == 'validate' && TwitterUtil::isValidTwitterUser()) { 40 41 // bypass password query with a little hack 42 $password = UserRegistrationUtil::getNewPassword((REGISTER_PASSWORD_MIN_LENGTH > 9 ? REGISTER_PASSWORD_MIN_LENGTH : 9)); 43 WCF::getUser()->password = StringUtil::getDoubleSaltedHash($password, WCF::getUser()->salt); 44 $eventObj->password = $password; 45 } 46 47 // hide password input 48 else if($eventName == 'assignVariables' && TwitterUtil::hasTwitterAccount(WCF::getUser()->userID)) { 49 WCF::getTPL()->append('additionalFields', '<script type="text/javascript"> 50 $($("password").parentNode.parentNode).hide(); 51 </script>'); 52 } 53 break; 29 54 } 30 55 } -
twitter/files/lib/util/TwitterUtil.class.php
r1290 r1291 46 46 */ 47 47 public static function hasTwitterAccount($userID) { 48 $sql = "SELECT utt.twitterID 48 $row = self::getTwitterAccount($userID); 49 return $row && $row['userToken'] && $row['userSecret']; 50 } 51 52 /** 53 * is there an existing user with given twitter id? 54 * 55 * @param integer $userID 56 * @return boolean 57 */ 58 public static function getTwitterAccount($userID) { 59 $sql = "SELECT utt.userToken, 60 utt.userSecret 49 61 FROM wcf".WCF_N."_user_to_twitter utt 50 62 WHERE utt.userID = ".intval($userID); … … 61 73 * @return boolean 62 74 */ 63 protected static function addTwitterUser($ me, $user) {75 protected static function addTwitterUser($user, $me, $token) { 64 76 $sql = "REPLACE INTO wcf".WCF_N."_user_to_twitter 65 77 (userID, userToken, userSecret) 66 78 VALUES (".intval($user->userID).", 67 '".escapeString($ me['oauth_token'])."',68 '".escapeString($ me['oauth_token_secret'])."')";79 '".escapeString($token['oauth_token'])."', 80 '".escapeString($token['oauth_token_secret'])."')"; 69 81 70 82 return WCF::getDB()->sendQuery($sql); … … 84 96 85 97 $me = null; 86 $uid = array();87 98 88 99 // API call. 89 try { 90 $me = $twitterObj->get('/account/verify_credentials.json'); 91 } catch (Exception $e) { 92 error_log($e); 100 if($twitterObj->startSession()) { 101 try { 102 $me = $twitterObj->get('/account/verify_credentials.json'); 103 } catch (Exception $e) { 104 error_log($e); 105 } 93 106 } 94 107 95 108 // no permissions granted, ask for login 96 109 if(!$me) { 97 $loginUrl = $twitterObj->getAuthenticateUrl(); 110 $proto = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https://' : 'http://'; 111 $loginUrl = $twitterObj->getAuthenticateUrl(null, array( 112 'oauth_callback' => $proto.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 113 )); 98 114 99 115 WCF::getTPL()->assign(array( … … 117 133 public static function updateCurrentUser() { 118 134 $me = null; 119 $uid = array();120 135 121 136 $twitterObj = Twitter::getInstance(); 122 137 123 138 // API call. 124 try { 125 $me = $twitterObj->get('/account/verify_credentials.json'); 126 } catch (TwitterApiException $e) { 127 error_log($e); 139 if($twitterObj->startSession()) { 140 try { 141 $me = $twitterObj->get('/account/verify_credentials.json'); 142 } catch (Exception $e) { 143 error_log($e); 144 } 128 145 } 129 146 130 147 // no permissions granted, ask for login 131 148 if(!$me) { 132 $loginUrl = $twitterObj->getAuthenticateUrl(); 149 $proto = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https://' : 'http://'; 150 $loginUrl = $twitterObj->getAuthenticateUrl(null, array( 151 'oauth_callback' => $proto.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 152 )); 133 153 134 154 WCF::getTPL()->assign(array( … … 154 174 155 175 // either user is new, oder just got a link, but add a twitter link 156 self::addTwitterUser($ me, $user);176 self::addTwitterUser($user, $me, Twitter::getInstance()->getToken()); 157 177 158 178 return true; … … 163 183 WCF::getTPL()->append('additionalFields', '<fieldset> 164 184 <legend>'.WCF::getLanguage()->get('wcf.twitter.title').'</legend> 165 '.WCF::getLanguage()->get('wcf.twitter.status.connected').' 185 <div class="formElement"> 186 <div class="formFieldLabel"> 187 <img src="'.RELATIVE_WCF_DIR.'icon/twitterM.png" alt="" /> 188 </div> 189 <div class="formField"> 190 '.WCF::getLanguage()->get('wcf.twitter.status.connected').' 191 </div> 192 </div> 166 193 </fieldset>'); 167 194 } … … 185 212 186 213 $me = null; 187 $uid = array();188 214 189 215 // API call. 190 try { 191 $me = $twitterObj->get('/account/verify_credentials.json'); 192 } catch (TwitterApiException $e) { 193 error_log($e); 216 if($twitterObj->startSession()) { 217 try { 218 $me = $twitterObj->get('/account/verify_credentials.json'); 219 } catch (Exception $e) { 220 error_log($e); 221 } 194 222 } 195 223 196 224 // no permissions granted, ask for login 197 225 if(!$me) { 198 $loginUrl = $twitterObj->getAuthenticateUrl(); 226 $proto = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https://' : 'http://'; 227 $loginUrl = $twitterObj->getAuthenticateUrl(null, array( 228 'oauth_callback' => $proto.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 229 )); 199 230 200 231 WCF::getTPL()->assign(array( … … 234 265 235 266 // either user is new, oder just got a link, but add a twitter link 236 self::addTwitterUser($ me, $user);267 self::addTwitterUser($user, $me, Twitter::getInstance()->getToken()); 237 268 } 238 269 -
twitter/install.sql
r1290 r1291 1 DROP TABLE IF EXISTS wcf1_twitter_account; 2 CREATE TABLE wcf1_twitter_account ( 3 accountID BIGINT unsigned NOT NULL PRIMARY KEY, 4 name VARCHAR(255) NULL, 5 screen_name VARCHAR(255) NULL, 6 profile_image_url VARCHAR(255) NULL 7 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 8 1 9 DROP TABLE IF EXISTS wcf1_user_to_twitter; 2 10 CREATE TABLE wcf1_user_to_twitter ( 3 userID INT(10) NOT NULL PRIMARY KEY, 11 userID INT(10) unsigned NOT NULL PRIMARY KEY, 12 accountID BIGINT unsigned NOT NULL, 4 13 userToken VARCHAR(64) NOT NULL, 5 14 userSecret VARCHAR(64) NOT NULL … … 8 17 DROP TABLE IF EXISTS wcf1_twitter_message; 9 18 CREATE TABLE wcf1_twitter_message ( 10 messageID INT(10) NOT NULL PRIMARY KEY, 11 userID INT(10) NOT NULL, 12 groupID INT(10) NOT NULL DEFAULT 0, 13 message TEXT NULL, 14 time INT(10) NOT NULL DE 19 messageID BIGINT unsigned NOT NULL PRIMARY KEY, 20 accountID BIGINT unsigned NOT NULL, 21 message VARCHAR(255) NULL, 22 time INT(10) NOT NULL DEFAULT 0, 15 23 FULLTEXT KEY (message), 16 KEY ( userID),24 KEY (accountID) 17 25 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -
twitter/package.xml
r1290 r1291 24 24 <files>files.tar</files> 25 25 <templates>templates.tar</templates> 26 <sql>install.sql</sql> 26 27 27 28 <!-- config --> 28 29 <options>options.xml</options> 30 <eventlistener>eventlistener.xml</eventlistener> 29 31 <pagemenu>pagemenu.xml</pagemenu> 30 <groupoptions>groupoptions.xml</groupoptions>31 32 <searchablemessagetypes>smt.xml</searchablemessagetypes> 32 33 -
twitter/templates/twitterLogin.tpl
r1290 r1291 1 1 <div class="formElement"> 2 2 <div class="formField"> 3 <a href="{$loginUrl}"> login to twitter</a>3 <a href="{$loginUrl}"><img src="http://si0.twimg.com/images/dev/buttons/sign-in-with-twitter-l.png" alt="" /></a> 4 4 </div> 5 5 </div>
