X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=twitter%2Ftwitter.php;h=a460fd70f14b894f506805bcb45a7accec0ca4d1;hb=88ca2f5a1e83c2cd93c6b08863fdd361a3a9bd69;hp=807445b6de536fcb368760666d5f78017ea21c27;hpb=461b3db9f449fdb1ce780a337cf2f1bb7aa35ac5;p=friendica-addons.git diff --git a/twitter/twitter.php b/twitter/twitter.php index 807445b6..a460fd70 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1,13 +1,13 @@ * Author: Michael Vogel + * Maintainer: Hypolite Petovan * - * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel + * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel, Hypolite Petovan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,7 +33,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -/* Twitter Plugin for Friendica +/* Twitter Addon for Friendica * * Author: Tobias Diekershoff * tobias.diekershoff@gmx.net @@ -41,7 +41,7 @@ * License:3-clause BSD license * * Configuration: - * To use this plugin you need a OAuth Consumer key pair (key & secret) + * To use this addon you need a OAuth Consumer key pair (key & secret) * you can get it from Twitter at https://twitter.com/apps * * Register your Friendica site as "Client" application with "Read & Write" access @@ -53,63 +53,77 @@ * $a->config['twitter']['consumerkey'] = 'your consumer_key here'; * $a->config['twitter']['consumersecret'] = 'your consumer_secret here'; * - * To activate the plugin itself add it to the $a->config['system']['addon'] + * To activate the addon itself add it to the $a->config['system']['addon'] * setting. After this, your user can configure their Twitter account settings - * from "Settings -> Plugin Settings". + * from "Settings -> Addon Settings". * - * Requirements: PHP5, curl [Slinky library] + * Requirements: PHP5, curl */ +use Abraham\TwitterOAuth\TwitterOAuth; use Friendica\App; use Friendica\Content\OEmbed; +use Friendica\Content\Text\BBCode; +use Friendica\Content\Text\Plaintext; +use Friendica\Core\Addon; use Friendica\Core\Config; +use Friendica\Core\L10n; use Friendica\Core\PConfig; use Friendica\Core\Worker; use Friendica\Model\GContact; use Friendica\Model\Group; +use Friendica\Model\Item; use Friendica\Model\Photo; +use Friendica\Model\Queue; use Friendica\Model\User; use Friendica\Object\Image; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Network; +require_once 'boot.php'; +require_once 'include/dba.php'; require_once 'include/enotify.php'; +require_once 'include/text.php'; + +require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes function twitter_install() { // we need some hooks, for the configuration and for sending tweets - register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); - register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); - register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local'); - register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook'); - 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'); - register_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire'); - register_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body'); - register_hook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification'); + Addon::registerHook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); + Addon::registerHook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); + Addon::registerHook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local'); + Addon::registerHook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook'); + Addon::registerHook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); + Addon::registerHook('cron', 'addon/twitter/twitter.php', 'twitter_cron'); + Addon::registerHook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook'); + Addon::registerHook('follow', 'addon/twitter/twitter.php', 'twitter_follow'); + Addon::registerHook('expire', 'addon/twitter/twitter.php', 'twitter_expire'); + Addon::registerHook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body'); + Addon::registerHook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification'); logger("installed twitter"); } function twitter_uninstall() { - unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); - unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); - unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local'); - unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook'); - unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); - unregister_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron'); - unregister_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook'); - unregister_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow'); - unregister_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire'); - unregister_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body'); - unregister_hook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification'); + Addon::unregisterHook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); + Addon::unregisterHook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); + Addon::unregisterHook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local'); + Addon::unregisterHook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook'); + Addon::unregisterHook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); + Addon::unregisterHook('cron', 'addon/twitter/twitter.php', 'twitter_cron'); + Addon::unregisterHook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook'); + Addon::unregisterHook('follow', 'addon/twitter/twitter.php', 'twitter_follow'); + Addon::unregisterHook('expire', 'addon/twitter/twitter.php', 'twitter_expire'); + Addon::unregisterHook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body'); + Addon::unregisterHook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification'); // old setting - remove only - unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook'); - unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings'); - unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); + Addon::unregisterHook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook'); + Addon::unregisterHook('addon_settings', 'addon/twitter/twitter.php', 'twitter_settings'); + Addon::unregisterHook('addon_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post'); } function twitter_check_item_notification(App $a, &$notification_data) @@ -140,21 +154,19 @@ function twitter_follow(App $a, &$contact) $uid = $a->user["uid"]; - $ckey = Config::get('twitter', 'consumerkey'); + $ckey = Config::get('twitter', 'consumerkey'); $csecret = Config::get('twitter', 'consumersecret'); - $otoken = PConfig::get($uid, 'twitter', 'oauthtoken'); + $otoken = PConfig::get($uid, 'twitter', 'oauthtoken'); $osecret = PConfig::get($uid, 'twitter', 'oauthsecret'); - require_once "addon/twitter/codebird.php"; - - $cb = \Codebird\Codebird::getInstance(); - $cb->setConsumerKey($ckey, $csecret); - $cb->setToken($otoken, $osecret); - - $parameters = []; - $parameters["screen_name"] = $nickname; + // If the addon is not configured (general or for this user) quit here + if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) { + $contact = false; + return; + } - $user = $cb->friendships_create($parameters); + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $connection->post('friendships/create', ['screen_name' => $nickname]); twitter_fetchuser($a, $uid, $nickname); @@ -178,7 +190,7 @@ function twitter_jot_nets(App $a, &$b) $tw_defpost = PConfig::get(local_user(), 'twitter', 'post_by_default'); $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : ''); $b .= '
' - . t('Post to Twitter') . '
'; + . L10n::t('Post to Twitter') . ''; } } @@ -188,11 +200,11 @@ function twitter_settings_post(App $a, $post) return; } // don't check twitter settings if twitter submit button is not clicked - if (!x($_POST, 'twitter-submit')) { + if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) { return; } - if (isset($_POST['twitter-disconnect'])) { + if (!empty($_POST['twitter-disconnect'])) { /* * * * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair * from the user configuration @@ -212,18 +224,25 @@ function twitter_settings_post(App $a, $post) if (isset($_POST['twitter-pin'])) { // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen logger('got a Twitter PIN'); - require_once 'library/twitteroauth.php'; $ckey = Config::get('twitter', 'consumerkey'); $csecret = Config::get('twitter', 'consumersecret'); // the token and secret for which the PIN was generated were hidden in the settings // form as token and token2, we need a new connection to Twitter using these token // and secret to request a Access Token with the PIN - $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']); - $token = $connection->getAccessToken($_POST['twitter-pin']); - // ok, now that we have the Access Token, save them in the user config - PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']); - PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']); - PConfig::set(local_user(), 'twitter', 'post', 1); + try { + if (empty($_POST['twitter-pin'])) { + throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.')); + } + + $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']); + $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]); + // ok, now that we have the Access Token, save them in the user config + PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']); + PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']); + PConfig::set(local_user(), 'twitter', 'post', 1); + } catch(Exception $e) { + info($e->getMessage()); + } // reload the Addon Settings page, if we don't do it see Bug #42 goaway('settings/connectors'); } else { @@ -239,7 +258,7 @@ function twitter_settings_post(App $a, $post) PConfig::delete(local_user(), 'twitter', 'lastid'); } - info(t('Twitter settings updated.') . EOL); + info(L10n::t('Twitter settings updated.') . EOL); } } } @@ -255,10 +274,10 @@ function twitter_settings(App $a, &$s) * 2) If no OAuthtoken & stuff is present, generate button to get some * 3) Checkbox for "Send public notices (280 chars only) */ - $ckey = Config::get('twitter', 'consumerkey' ); - $csecret = Config::get('twitter', 'consumersecret' ); - $otoken = PConfig::get(local_user(), 'twitter', 'oauthtoken' ); - $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret' ); + $ckey = Config::get('twitter', 'consumerkey'); + $csecret = Config::get('twitter', 'consumersecret'); + $otoken = PConfig::get(local_user(), 'twitter', 'oauthtoken'); + $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret'); $enabled = intval(PConfig::get(local_user(), 'twitter', 'post')); $defenabled = intval(PConfig::get(local_user(), 'twitter', 'post_by_default')); @@ -269,60 +288,51 @@ function twitter_settings(App $a, &$s) $css = (($enabled) ? '' : '-disabled'); $s .= ''; - $s .= '

' . t('Twitter Import/Export/Mirror') . '

'; + $s .= '

' . L10n::t('Twitter Import/Export/Mirror') . '

'; $s .= '
'; $s .= '