diff --git a/twitter/twitter.css b/twitter/twitter.css index 3ff37cda4..99ee0bef3 100755 --- a/twitter/twitter.css +++ b/twitter/twitter.css @@ -24,6 +24,8 @@ #twitter-enable-label, #twitter-shortening-label, #twitter-mirror-label, +#twitter-import-label, +#twitter-create_user-label, #twitter-pin-label { float: left; width: 250px; diff --git a/twitter/twitter.php b/twitter/twitter.php index 1bee1bdf0..83368f905 100755 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -71,6 +71,7 @@ function twitter_install() { register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); register_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron'); register_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook'); + register_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow'); logger("installed twitter"); } @@ -91,6 +92,45 @@ function twitter_uninstall() { } +function twitter_follow($a, &$contact) { + + logger("twitter_follow: Check if contact is twitter contact. ".$contact["url"], LOGGER_DEBUG); + + if (!strstr($contact["url"], "://twitter.com") AND !strstr($contact["url"], "@twitter.com")) + return; + + // contact seems to be a twitter contact, so continue + $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]); + $nickname = str_replace("@twitter.com", "", $nickname); + + $uid = $a->user["uid"]; + + $ckey = get_config('twitter', 'consumerkey'); + $csecret = get_config('twitter', 'consumersecret'); + $otoken = get_pconfig($uid, 'twitter', 'oauthtoken'); + $osecret = get_pconfig($uid, 'twitter', 'oauthsecret'); + + require_once("addon/twitter/codebird.php"); + + $cb = \Codebird\Codebird::getInstance(); + $cb->setConsumerKey($ckey, $csecret); + $cb->setToken($otoken, $osecret); + + $parameters = array(); + $parameters["screen_name"] = $nickname; + + $user = $cb->friendships_create($parameters); + + twitter_fetchuser($a, $uid, $nickname); + + $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey + FROM `contact` WHERE `uid` = %d AND `nick` = '%s'", + intval($uid), + dbesc($nickname)); + if (count($r)) + $contact["contact"] = $r[0]; +} + function twitter_jot_nets(&$a,&$b) { if(! local_user()) return; @@ -107,9 +147,10 @@ function twitter_jot_nets(&$a,&$b) { function twitter_settings_post ($a,$post) { if(! local_user()) return; - // don't check twitter settings if twitter submit button is not clicked - if (!x($_POST,'twitter-submit')) return; - + // don't check twitter settings if twitter submit button is not clicked + if (!x($_POST,'twitter-submit')) + return; + if (isset($_POST['twitter-disconnect'])) { /*** * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair @@ -125,6 +166,8 @@ function twitter_settings_post ($a,$post) { del_pconfig(local_user(), 'twitter', 'lastid'); del_pconfig(local_user(), 'twitter', 'mirror_posts'); del_pconfig(local_user(), 'twitter', 'intelligent_shortening'); + del_pconfig(local_user(), 'twitter', 'import'); + del_pconfig(local_user(), 'twitter', 'create_user'); } else { if (isset($_POST['twitter-pin'])) { // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen @@ -152,6 +195,8 @@ function twitter_settings_post ($a,$post) { set_pconfig(local_user(),'twitter','post_taglinks',intval($_POST['twitter-sendtaglinks'])); set_pconfig(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror'])); set_pconfig(local_user(), 'twitter', 'intelligent_shortening', intval($_POST['twitter-shortening'])); + set_pconfig(local_user(), 'twitter', 'import', intval($_POST['twitter-import'])); + set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user'])); info( t('Twitter settings updated.') . EOL); }} } @@ -178,6 +223,10 @@ function twitter_settings(&$a,&$s) { $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : ''); $shorteningenabled = get_pconfig(local_user(),'twitter','intelligent_shortening'); $shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : ''); + $importenabled = get_pconfig(local_user(),'twitter','import'); + $importchecked = (($importenabled) ? ' checked="checked" ' : ''); + $create_userenabled = get_pconfig(local_user(),'twitter','create_user'); + $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : ''); $s .= '