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/enotify.php';
require_once 'include/text.php';
-require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
+require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
$otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
$osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
+ // 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;
+ }
+
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
$connection->post('friendships/create', ['screen_name' => $nickname]);
// 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']);
* account at Twitter.
*/
$connection = new TwitterOAuth($ckey, $csecret);
- $request_token = $connection->getRequestToken();
- $token = $request_token['oauth_token'];
+ $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
/* * *
* make some nice form
*/
$s .= '<p>' . L10n::t('At this Friendica instance the Twitter addon was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') . '</p>';
- $s .= '<a href="' . $connection->getAuthorizeURL($token) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . L10n::t('Log in with Twitter') . '"></a>';
+ $s .= '<a href="' . $connection->url('oauth/authorize', ['oauth_token' => $result->oauth_token]) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . L10n::t('Log in with Twitter') . '"></a>';
$s .= '<div id="twitter-pin-wrapper">';
$s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
$s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
- $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $token . '" />';
- $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $request_token['oauth_token_secret'] . '" />';
+ $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result->oauth_token . '" />';
+ $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result->oauth_token_secret . '" />';
$s .= '</div><div class="clear"></div>';
$s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
} else {
return;
}
- $tweet = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
+ $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
$max_char = 280;
$msgarr = BBCode::toPlaintext($b, $max_char, true, 8);
$image = "";
} elseif ($iscomment) {
logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
- q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
- dbesc("twitter::" . $result->id_str),
- dbesc($result->text),
- intval($b['id'])
- );
+ Item::update(['extid' => "twitter::" . $result->id_str, 'body' => $result->text], ['id' => $b['id']]);
}
}
$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) {
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",
- dbesc("twitter::" . $result->id_str),
- intval($b['id'])
- );
+ Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
}
}
}
$abandon_days = 0;
}
- $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
+ $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
if (count($r)) {
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);
}
}
`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(DateTimeFormat::utcNow()),
dbesc("https://twitter.com/" . $contact->screen_name),
dbesc(normalise_link("https://twitter.com/" . $contact->screen_name)),
dbesc($contact->screen_name."@twitter.com"),
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
+ dbesc(DateTimeFormat::utcNow()),
+ dbesc(DateTimeFormat::utcNow()),
+ dbesc(DateTimeFormat::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'] < DateTimeFormat::convert('now -2 days', '', '', )) ? true : false);
+ $update_photo = ($r[0]['avatar-date'] < DateTimeFormat::utc('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)) {
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
+ dbesc(DateTimeFormat::utcNow()),
+ dbesc(DateTimeFormat::utcNow()),
+ dbesc(DateTimeFormat::utcNow()),
dbesc("https://twitter.com/".$contact->screen_name),
dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
dbesc($contact->screen_name."@twitter.com"),
return $contact_id;
}
-function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture)
+function twitter_expand_entities(App $a, $body, $item, $picture)
{
- $tags = "";
-
$plain = $body;
if (isset($item->entities->urls)) {
$body = add_page_info_to_body($body);
}
- if ($no_tags) {
- return ["body" => $body, "tags" => "", "plain" => $plain];
- }
-
$tags_arr = [];
foreach ($item->entities->hashtags AS $hashtag) {
* @param object $post Twitter object with the post
* @param array $postarray Array of the item that is about to be posted
*
- * @return $picture string Returns a a single picture string if it isn't a media post
+ * @return $picture string Image URL or empty string
*/
function twitter_media_entities($post, &$postarray)
{
// Search for media links
$picture = twitter_media_entities($post, $postarray);
- $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture);
+ $converted = twitter_expand_entities($a, $postarray['body'], $post, $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'] = DateTimeFormat::utc($post->created_at);
+ $postarray['edited'] = DateTimeFormat::utc($post->created_at);
$statustext = $converted["plain"];
'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'],
continue;
}
- $item = item_store($postarray);
+ $item = Item::insert($postarray);
$postarray["id"] = $item;
logger('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
continue;
}
- $item = item_store($postarray);
+ $item = Item::insert($postarray);
$postarray["id"] = $item;
logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted home timeline item ' . $item);
continue;
}
- $item = item_store($postarray);
+ $item = Item::insert($postarray);
$postarray["id"] = $item;
if ($item && function_exists("check_item_notification")) {
'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'],