X-Git-Url: https://git.mxchange.org/?p=friendica-addons.git;a=blobdiff_plain;f=twitter%2Ftwitter.php;h=cc12e8de8448dad7a223358b27fde337e35e6400;hp=f88d4542b41a0c1a9f695216b3f9badbb7615b33;hb=e4f32f8ac8dbee577dc31195b1104add371f77eb;hpb=6ca14b467806f95a843184eafeed9225da0364aa diff --git a/twitter/twitter.php b/twitter/twitter.php index f88d4542..cc12e8de 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,60 +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\Network; +use Friendica\Util\Temporal; +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) @@ -137,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 = array(); - $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); @@ -175,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') . ''; } } @@ -209,20 +224,19 @@ 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']); + $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); // reload the Addon Settings page, if we don't do it see Bug #42 - goaway($a->get_baseurl() . '/settings/connectors'); + goaway('settings/connectors'); } else { // if no PIN is supplied in the POST variables, the user has changed the setting // to post a tweet for every new __public__ posting to the wall @@ -236,7 +250,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); } } } @@ -252,29 +266,25 @@ 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' ); - $enabled = PConfig::get(local_user(), 'twitter', 'post'); - $checked = (($enabled) ? ' checked="checked" ' : ''); - $defenabled = PConfig::get(local_user(), 'twitter', 'post_by_default'); - $defchecked = (($defenabled) ? ' checked="checked" ' : ''); - $mirrorenabled = PConfig::get(local_user(), 'twitter', 'mirror_posts'); - $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : ''); - $importenabled = PConfig::get(local_user(), 'twitter', 'import'); - $importchecked = (($importenabled) ? ' checked="checked" ' : ''); - $create_userenabled = PConfig::get(local_user(), 'twitter', 'create_user'); - $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : ''); + $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')); + $mirrorenabled = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts')); + $importenabled = intval(PConfig::get(local_user(), 'twitter', 'import')); + $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user')); $css = (($enabled) ? '' : '-disabled'); $s .= ''; - $s .= '

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

'; + $s .= '

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

'; $s .= '
'; $s .= '
'; @@ -390,25 +401,21 @@ function twitter_action(App $a, $uid, $pid, $action) $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); + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); - $post = array('id' => $pid); + $post = ['id' => $pid]; logger("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), LOGGER_DATA); switch ($action) { case "delete": - // To-Do: $result = $cb->statuses_destroy($post); + // To-Do: $result = $connection->post('statuses/destroy', $post); break; case "like": - $result = $cb->favorites_create($post); + $result = $connection->post('favorites/create', $post); break; case "unlike": - $result = $cb->favorites_destroy($post); + $result = $connection->post('favorites/destroy', $post); break; } logger("twitter_action '" . $action . "' send, result: " . print_r($result, true), LOGGER_DEBUG); @@ -417,8 +424,6 @@ function twitter_action(App $a, $uid, $pid, $action) function twitter_post_hook(App $a, &$b) { // Post to Twitter - require_once "include/network.php"; - if (!PConfig::get($b["uid"], 'twitter', 'import') && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) { return; @@ -428,7 +433,10 @@ function twitter_post_hook(App $a, &$b) logger("twitter_post_hook: parameter " . print_r($b, true), LOGGER_DATA); // Looking if its a reply to a twitter post - if ((substr($b["parent-uri"], 0, 9) != "twitter::") && (substr($b["extid"], 0, 9) != "twitter::") && (substr($b["thr-parent"], 0, 9) != "twitter::")) { + if ((substr($b["parent-uri"], 0, 9) != "twitter::") + && (substr($b["extid"], 0, 9) != "twitter::") + && (substr($b["thr-parent"], 0, 9) != "twitter::")) + { logger("twitter_post_hook: no twitter post " . $b["parent"]); return; } @@ -465,7 +473,7 @@ function twitter_post_hook(App $a, &$b) // Dont't post if the post doesn't belong to us. // This is a check for forum postings - $self = dba::select('contact', array('id'), array('uid' => $b['uid'], 'self' => true), array('limit' => 1)); + $self = dba::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]); if ($b['contact-id'] != $self['id']) { return; } @@ -477,10 +485,12 @@ function twitter_post_hook(App $a, &$b) if ($b['verb'] == ACTIVITY_LIKE) { logger("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), LOGGER_DEBUG); - if ($b['deleted']) + if ($b['deleted']) { twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike"); - else + } else { twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like"); + } + return; } @@ -514,17 +524,14 @@ function twitter_post_hook(App $a, &$b) return; } - require_once 'library/twitteroauth.php'; - require_once 'include/bbcode.php'; - $tweet = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); $max_char = 280; - require_once "include/plaintext.php"; - $msgarr = plaintext($a, $b, $max_char, true, 8); + $msgarr = BBCode::toPlaintext($b, $max_char, true, 8); $msg = $msgarr["text"]; if (($msg == "") && isset($msgarr["title"])) { - $msg = shortenmsg($msgarr["title"], $max_char - 50); + $msg = Plaintext::shorten($msgarr["title"], $max_char - 50); } $image = ""; @@ -539,29 +546,16 @@ function twitter_post_hook(App $a, &$b) // and now tweet it :-) if (strlen($msg) && ($image != "")) { - $img_str = fetch_url($image); - - $tempfile = tempnam(get_temppath(), "cache"); - file_put_contents($tempfile, $img_str); - - // Twitter had changed something so that the old library doesn't work anymore - // so we are using a new library for twitter - // To-Do: - // Switching completely to this library with all functions - require_once "addon/twitter/codebird.php"; - - $cb = \Codebird\Codebird::getInstance(); - $cb->setConsumerKey($ckey, $csecret); - $cb->setToken($otoken, $osecret); + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $media = $connection->upload('media/upload', ['media' => $image]); - $post = array('status' => $msg, 'media[]' => $tempfile); + $post = ['status' => $msg, 'media_ids' => $media->media_id_string]; if ($iscomment) { $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9); } - $result = $cb->statuses_updateWithMedia($post); - unlink($tempfile); + $result = $connection->post('statuses/update', $post); logger('twitter_post_with_media send, result: ' . print_r($result, true), LOGGER_DEBUG); @@ -588,12 +582,11 @@ function twitter_post_hook(App $a, &$b) if (strlen($msg) && ($image == "")) { // ----------------- $max_char = 280; - require_once "include/plaintext.php"; - $msgarr = plaintext($a, $b, $max_char, true, 8); + $msgarr = BBCode::toPlaintext($b, $max_char, true, 8); $msg = $msgarr["text"]; if (($msg == "") && isset($msgarr["title"])) { - $msg = shortenmsg($msgarr["title"], $max_char - 50); + $msg = Plaintext::shorten($msgarr["title"], $max_char - 50); } if (isset($msgarr["url"])) { @@ -601,13 +594,13 @@ function twitter_post_hook(App $a, &$b) } // ----------------- $url = 'statuses/update'; - $post = array('status' => $msg, 'weighted_character_count' => 'true'); + $post = ['status' => $msg, 'weighted_character_count' => 'true']; if ($iscomment) { $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9); } - $result = $tweet->post($url, $post); + $result = $connection->post($url, $post); logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG); if ($result->source) { @@ -622,10 +615,10 @@ function twitter_post_hook(App $a, &$b) $a->contact = $r[0]["id"]; } - $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $post)); - require_once 'include/queue_fn.php'; - add_to_queue($a->contact, NETWORK_TWITTER, $s); - notice(t('Twitter post failed. Queued for retry.') . EOL); + $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]); + + Queue::add($a->contact, NETWORK_TWITTER, $s); + notice(L10n::t('Twitter post failed. Queued for retry.') . EOL); } elseif ($iscomment) { logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']); q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d", @@ -637,25 +630,25 @@ function twitter_post_hook(App $a, &$b) } } -function twitter_plugin_admin_post(App $a) +function twitter_addon_admin_post(App $a) { - $consumerkey = x($_POST, 'consumerkey') ? notags(trim($_POST['consumerkey'])) : ''; - $consumersecret = x($_POST, 'consumersecret') ? notags(trim($_POST['consumersecret'])) : ''; + $consumerkey = x($_POST, 'consumerkey') ? notags(trim($_POST['consumerkey'])) : ''; + $consumersecret = x($_POST, 'consumersecret') ? notags(trim($_POST['consumersecret'])) : ''; Config::set('twitter', 'consumerkey', $consumerkey); Config::set('twitter', 'consumersecret', $consumersecret); - info(t('Settings updated.') . EOL); + info(L10n::t('Settings updated.') . EOL); } -function twitter_plugin_admin(App $a, &$o) +function twitter_addon_admin(App $a, &$o) { $t = get_markup_template("admin.tpl", "addon/twitter/"); - $o = replace_macros($t, array( - '$submit' => t('Save Settings'), + $o = replace_macros($t, [ + '$submit' => L10n::t('Save Settings'), // name, label, value, help, [extra values] - '$consumerkey' => array('consumerkey', t('Consumer key'), Config::get('twitter', 'consumerkey'), ''), - '$consumersecret' => array('consumersecret', t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''), - )); + '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''], + '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''], + ]); } function twitter_cron(App $a, $b) @@ -685,10 +678,11 @@ function twitter_cron(App $a, $b) } $abandon_days = intval(Config::get('system', 'account_abandon_days')); - if ($abandon_days < 1) + if ($abandon_days < 1) { $abandon_days = 0; + } - $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400); + $abandon_limit = date(Temporal::MYSQL, time() - $abandon_days * 86400); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'"); if (count($r)) { @@ -734,9 +728,9 @@ function twitter_expire(App $a, $b) } if (method_exists('dba', 'delete')) { - $r = dba::select('item', array('id'), array('deleted' => true, 'network' => NETWORK_TWITTER)); + $r = dba::select('item', ['id'], ['deleted' => true, 'network' => NETWORK_TWITTER]); while ($row = dba::fetch($r)) { - dba::delete('item', array('id' => $row['id'])); + dba::delete('item', ['id' => $row['id']]); } dba::close($r); } else { @@ -751,7 +745,7 @@ function twitter_expire(App $a, $b) if (count($r)) { foreach ($r as $rr) { logger('twitter_expire: user ' . $rr['uid']); - item_expire($rr['uid'], $days, NETWORK_TWITTER, true); + Item::expire($rr['uid'], $days, NETWORK_TWITTER, true); } } @@ -766,7 +760,6 @@ function twitter_prepare_body(App $a, &$b) if ($b["preview"]) { $max_char = 280; - require_once "include/plaintext.php"; $item = $b["item"]; $item["plink"] = $a->get_baseurl() . "/display/" . $a->user["nickname"] . "/" . $item["parent"]; @@ -786,7 +779,7 @@ function twitter_prepare_body(App $a, &$b) } } - $msgarr = plaintext($a, $item, $max_char, true, 8); + $msgarr = BBCode::toPlaintext($item, $max_char, true, 8); $msg = $msgarr["text"]; if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) { @@ -822,13 +815,20 @@ function twitter_do_mirrorpost(App $a, $uid, $post) if (is_object($post->retweeted_status)) { // We don't support nested shares, so we mustn't show quotes as shares on retweets - $item = twitter_createpost($a, $uid, $post->retweeted_status, array('id' => 0), false, false, true); - - $datarray['body'] = "\n" . share_header($item['author-name'], $item['author-link'], $item['author-avatar'], "", $item['created'], $item['plink']); + $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true); + + $datarray['body'] = "\n" . share_header( + $item['author-name'], + $item['author-link'], + $item['author-avatar'], + "", + $item['created'], + $item['plink'] + ); $datarray['body'] .= $item['body'] . '[/share]'; } else { - $item = twitter_createpost($a, $uid, $post, array('id' => 0), false, false, false); + $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false); $datarray['body'] = $item['body']; } @@ -867,10 +867,9 @@ function twitter_fetchtimeline(App $a, $uid) require_once 'include/items.php'; require_once 'mod/share.php'; - require_once 'library/twitteroauth.php'; $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); - $parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"); + $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"]; $first_time = ($lastid == ""); @@ -921,8 +920,6 @@ function twitter_queue_hook(App $a, &$b) return; } - require_once 'include/queue_fn.php'; - foreach ($qi as $x) { if ($x['network'] !== NETWORK_TWITTER) { continue; @@ -952,15 +949,8 @@ function twitter_queue_hook(App $a, &$b) $z = unserialize($x['content']); - require_once "addon/twitter/codebird.php"; - - $cb = \Codebird\Codebird::getInstance(); - $cb->setConsumerKey($ckey, $csecret); - $cb->setToken($otoken, $osecret); - - if ($z['url'] == "statuses/update") { - $result = $cb->statuses_update($z['post']); - } + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $result = $connection->post($z['url'], $z['post']); logger('twitter_queue: post result: ' . print_r($result, true), LOGGER_DEBUG); @@ -968,7 +958,7 @@ function twitter_queue_hook(App $a, &$b) logger('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"'); } else { $success = true; - remove_queue_item($x['id']); + Queue::removeItem($x['id']); } } else { logger("twitter_queue: Error getting tokens for user " . $user['uid']); @@ -976,7 +966,7 @@ function twitter_queue_hook(App $a, &$b) if (!$success) { logger('twitter_queue: delayed'); - update_queue_time($x['id']); + Queue::updateTime($x['id']); } } } @@ -1001,14 +991,15 @@ function twitter_fetch_contact($uid, $contact, $create_user) $avatar = twitter_fix_avatar($contact->profile_image_url_https); - GContact::update(array("url" => "https://twitter.com/" . $contact->screen_name, + GContact::update(["url" => "https://twitter.com/" . $contact->screen_name, "network" => NETWORK_TWITTER, "photo" => $avatar, "hide" => true, "name" => $contact->name, "nick" => $contact->screen_name, "location" => $contact->location, "about" => $contact->description, - "addr" => $contact->screen_name . "@twitter.com", "generation" => 2)); + "addr" => $contact->screen_name . "@twitter.com", "generation" => 2]); $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", - intval($uid), dbesc("twitter::" . $contact->id_str)); + intval($uid), + dbesc("twitter::" . $contact->id_str)); if (!count($r) && !$create_user) { return 0; @@ -1026,7 +1017,7 @@ function twitter_fetch_contact($uid, $contact, $create_user) `location`, `about`, `writable`, `blocked`, `readonly`, `pending`) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, 0, 0, 0)", intval($uid), - dbesc(datetime_convert()), + dbesc(Temporal::utcNow()), dbesc("https://twitter.com/" . $contact->screen_name), dbesc(normalise_link("https://twitter.com/" . $contact->screen_name)), dbesc($contact->screen_name."@twitter.com"), @@ -1055,14 +1046,7 @@ function twitter_fetch_contact($uid, $contact, $create_user) $contact_id = $r[0]['id']; - $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1", - intval($uid) - ); - - if ($g && intval($g[0]['def_gid'])) { - require_once 'include/group.php'; - group_add_member($uid, '', $contact_id, $g[0]['def_gid']); - } + Group::addMember(User::getDefaultGroup($uid), $contact_id); $photos = Photo::importProfilePhoto($avatar, $uid, $contact_id, true); @@ -1077,16 +1061,16 @@ function twitter_fetch_contact($uid, $contact, $create_user) dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc(datetime_convert()), + dbesc(Temporal::utcNow()), + dbesc(Temporal::utcNow()), + dbesc(Temporal::utcNow()), intval($contact_id) ); } } else { // update profile photos once every two weeks as we have no notification of when they change. - //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -2 days')) ? true : false); - $update_photo = ($r[0]['avatar-date'] < datetime_convert('', '', 'now -12 hours')); + //$update_photo = (($r[0]['avatar-date'] < Temporal::convert('now -2 days', '', '', )) ? true : false); + $update_photo = ($r[0]['avatar-date'] < Temporal::convert('now -12 hours')); // check that we have all the photos, this has been known to fail on occasion if ((!$r[0]['photo']) || (!$r[0]['thumb']) || (!$r[0]['micro']) || ($update_photo)) { @@ -1112,9 +1096,9 @@ function twitter_fetch_contact($uid, $contact, $create_user) dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc(datetime_convert()), + dbesc(Temporal::utcNow()), + dbesc(Temporal::utcNow()), + dbesc(Temporal::utcNow()), dbesc("https://twitter.com/".$contact->screen_name), dbesc(normalise_link("https://twitter.com/".$contact->screen_name)), dbesc($contact->screen_name."@twitter.com"), @@ -1133,17 +1117,11 @@ function twitter_fetch_contact($uid, $contact, $create_user) function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") { - $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); - $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", intval($uid)); @@ -1153,7 +1131,7 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") return; } - $parameters = array(); + $parameters = []; if ($screen_name != "") { $parameters["screen_name"] = $screen_name; @@ -1164,7 +1142,8 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") } // Fetching user data - $user = $cb->users_show($parameters); + $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + $user = $connection->get('users/show', $parameters); if (!is_object($user)) { return; @@ -1177,9 +1156,6 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture) { - require_once "include/oembed.php"; - require_once "include/network.php"; - $tags = ""; $plain = $body; @@ -1190,13 +1166,13 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur $footerlink = ""; $footer = ""; - foreach ($item->entities->urls AS $url) { + foreach ($item->entities->urls as $url) { $plain = str_replace($url->url, '', $plain); if ($url->url && $url->expanded_url && $url->display_url) { - $expanded_url = original_url($url->expanded_url); + $expanded_url = Network::finalUrl($url->expanded_url); - $oembed_data = oembed_fetch_url($expanded_url); + $oembed_data = OEmbed::fetchURL($expanded_url); // Quickfix: Workaround for URL with "[" and "]" in it if (strpos($expanded_url, "[") || strpos($expanded_url, "]")) { @@ -1223,7 +1199,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur } elseif ($oembed_data->type != "link") { $body = str_replace($url->url, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body); } else { - $img_str = fetch_url($expanded_url, true, $redirects, 4); + $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4); $tempfile = tempnam(get_temppath(), "cache"); file_put_contents($tempfile, $img_str); @@ -1266,10 +1242,10 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur } if ($no_tags) { - return array("body" => $body, "tags" => "", "plain" => $plain); + return ["body" => $body, "tags" => "", "plain" => $plain]; } - $tags_arr = array(); + $tags_arr = []; foreach ($item->entities->hashtags AS $hashtag) { $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]"; @@ -1324,7 +1300,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur $tags = implode($tags_arr, ","); } - return array("body" => $body, "tags" => $tags, "plain" => $plain); + return ["body" => $body, "tags" => $tags, "plain" => $plain]; } /** @@ -1356,7 +1332,7 @@ function twitter_media_entities($post, &$postarray) } // This is a pure media post, first search for all media urls - $media = array(); + $media = []; foreach ($post->extended_entities->media AS $medium) { switch ($medium->type) { case 'photo': @@ -1393,7 +1369,7 @@ function twitter_media_entities($post, &$postarray) function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_existing_contact, $noquote) { - $postarray = array(); + $postarray = []; $postarray['network'] = NETWORK_TWITTER; $postarray['gravity'] = 0; $postarray['uid'] = $uid; @@ -1403,13 +1379,13 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis // Don't import our own comments $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1", - dbesc($postarray['uri']), - intval($uid) + dbesc($postarray['uri']), + intval($uid) ); if (count($r)) { logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG); - return array(); + return []; } $contactid = 0; @@ -1418,8 +1394,8 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $parent = "twitter::" . $post->in_reply_to_status_id_str; $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", - dbesc($parent), - intval($uid) + dbesc($parent), + intval($uid) ); if (count($r)) { $postarray['thr-parent'] = $r[0]["uri"]; @@ -1428,8 +1404,8 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $postarray['object-type'] = ACTIVITY_OBJ_COMMENT; } else { $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1", - dbesc($parent), - intval($uid) + dbesc($parent), + intval($uid) ); if (count($r)) { $postarray['thr-parent'] = $r[0]['uri']; @@ -1458,7 +1434,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $postarray['owner-avatar'] = $r[0]["photo"]; } else { logger("No self contact for user " . $uid, LOGGER_DEBUG); - return array(); + return []; } } // Don't create accounts of people who just comment something @@ -1480,7 +1456,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $contactid = $self['id']; } elseif ($contactid <= 0) { logger("Contact ID is zero or less than zero.", LOGGER_DEBUG); - return array(); + return []; } $postarray['contact-id'] = $contactid; @@ -1514,8 +1490,8 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture); $postarray['body'] = $converted["body"]; $postarray['tag'] = $converted["tags"]; - $postarray['created'] = datetime_convert('UTC', 'UTC', $post->created_at); - $postarray['edited'] = datetime_convert('UTC', 'UTC', $post->created_at); + $postarray['created'] = Temporal::convert($post->created_at); + $postarray['edited'] = Temporal::convert($post->created_at); $statustext = $converted["plain"]; @@ -1550,7 +1526,14 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis $postarray['body'] = $statustext; - $postarray['body'] .= "\n" . share_header($quoted['author-name'], $quoted['author-link'], $quoted['author-avatar'], "", $quoted['created'], $quoted['plink']); + $postarray['body'] .= "\n" . share_header( + $quoted['author-name'], + $quoted['author-link'], + $quoted['author-avatar'], + "", + $quoted['created'], + $quoted['plink'] + ); $postarray['body'] .= $quoted['body'] . '[/share]'; } @@ -1562,7 +1545,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray) { /// TODO: this whole function doesn't seem to work. Needs complete check $user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", - intval($uid) + intval($uid) ); if (!count($user)) { @@ -1575,8 +1558,8 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray) } $own_user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", - intval($uid), - dbesc("twitter::".$own_id) + intval($uid), + dbesc("twitter::".$own_id) ); if (!count($own_user)) { @@ -1589,8 +1572,8 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray) } $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0", - dbesc($postarray['parent-uri']), - intval($uid) + dbesc($postarray['parent-uri']), + intval($uid) ); if (count($myconv)) { @@ -1604,7 +1587,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray) $conv_parent = $conv['parent']; - notification(array( + notification([ 'type' => NOTIFY_COMMENT, 'notify_flags' => $user[0]['notify-flags'], 'language' => $user[0]['language'], @@ -1612,14 +1595,14 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray) 'to_email' => $user[0]['email'], 'uid' => $user[0]['uid'], 'item' => $postarray, - 'link' => $a->get_baseurl() . '/display/' . urlencode(get_item_guid($top_item)), + 'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($top_item)), 'source_name' => $postarray['author-name'], 'source_link' => $postarray['author-link'], 'source_photo' => $postarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $conv_parent, - )); + ]); // only send one notification break; @@ -1631,10 +1614,10 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_ { logger("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, LOGGER_DEBUG); - $posts = array(); + $posts = []; while ($post->in_reply_to_status_id_str != "") { - $parameters = array("trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str); + $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str]; $post = $connection->get('statuses/show', $parameters); @@ -1644,8 +1627,8 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_ } $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", - dbesc("twitter::".$post->id_str), - intval($uid) + dbesc("twitter::".$post->id_str), + intval($uid) ); if (count($r)) { @@ -1663,10 +1646,11 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_ foreach ($posts as $post) { $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false); - if (trim($postarray['body']) == "") + if (trim($postarray['body']) == "") { continue; + } - $item = item_store($postarray); + $item = Item::insert($postarray); $postarray["id"] = $item; logger('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item); @@ -1695,7 +1679,6 @@ function twitter_fetchhometimeline(App $a, $uid) $application_name = $a->get_hostname(); } - require_once 'library/twitteroauth.php'; require_once 'include/items.php'; $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); @@ -1730,7 +1713,7 @@ function twitter_fetchhometimeline(App $a, $uid) return; } - $parameters = array("exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"); + $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"]; //$parameters["count"] = 200; // Fetching timeline $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid'); @@ -1783,7 +1766,7 @@ function twitter_fetchhometimeline(App $a, $uid) continue; } - $item = item_store($postarray); + $item = Item::insert($postarray); $postarray["id"] = $item; logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted home timeline item ' . $item); @@ -1835,7 +1818,7 @@ function twitter_fetchhometimeline(App $a, $uid) continue; } - $item = item_store($postarray); + $item = Item::insert($postarray); $postarray["id"] = $item; if ($item && function_exists("check_item_notification")) { @@ -1857,12 +1840,13 @@ function twitter_fetchhometimeline(App $a, $uid) $item = $r[0]['id']; $parent_id = $r[0]['parent']; } - } else + } else { $parent_id = $postarray['parent']; + } if (($item != 0) && !function_exists("check_item_notification")) { require_once 'include/enotify.php'; - notification(array( + notification([ 'type' => NOTIFY_TAGSELF, 'notify_flags' => $u[0]['notify-flags'], 'language' => $u[0]['language'], @@ -1870,14 +1854,14 @@ function twitter_fetchhometimeline(App $a, $uid) 'to_email' => $u[0]['email'], 'uid' => $u[0]['uid'], 'item' => $postarray, - 'link' => $a->get_baseurl() . '/display/' . urlencode(get_item_guid($item)), + 'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($item)), 'source_name' => $postarray['author-name'], 'source_link' => $postarray['author-link'], 'source_photo' => $postarray['author-avatar'], 'verb' => ACTIVITY_TAG, 'otype' => 'item', 'parent' => $parent_id - )); + ]); } } } @@ -1897,8 +1881,6 @@ function twitter_fetch_own_contact(App $a, $uid) $contact_id = 0; if ($own_id == "") { - require_once 'library/twitteroauth.php'; - $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); // Fetching user data @@ -1965,9 +1947,7 @@ function twitter_is_retweet(App $a, $uid, $body) $otoken = PConfig::get($uid, 'twitter', 'oauthtoken'); $osecret = PConfig::get($uid, 'twitter', 'oauthsecret'); - require_once 'library/twitteroauth.php'; $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); - $result = $connection->post('statuses/retweet/' . $id); logger('twitter_is_retweet: result ' . print_r($result, true), LOGGER_DEBUG);