TwitterBridgePlugin.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Plugin for sending and importing Twitter statuses
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @author Julien C <chaumond@gmail.com>
  23. * @copyright 2009-2010 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. require_once __DIR__ . '/twitter.php';
  28. /**
  29. * Plugin for sending and importing Twitter statuses
  30. *
  31. * This class allows users to link their Twitter accounts
  32. *
  33. * Depends on Favorite plugin.
  34. *
  35. * @category Plugin
  36. * @package GNUsocial
  37. * @author Zach Copley <zach@status.net>
  38. * @author Julien C <chaumond@gmail.com>
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. */
  41. class TwitterBridgePlugin extends Plugin
  42. {
  43. const PLUGIN_VERSION = '2.0.0';
  44. public $adminImportControl = false; // Should the 'import' checkbox be exposed in the admin panel?
  45. /**
  46. * Initializer for the plugin.
  47. */
  48. public function initialize()
  49. {
  50. // Allow the key and secret to be passed in
  51. // Control panel will override
  52. if (isset($this->consumer_key)) {
  53. $key = common_config('twitter', 'consumer_key');
  54. if (empty($key)) {
  55. Config::save('twitter', 'consumer_key', $this->consumer_key);
  56. }
  57. }
  58. if (isset($this->consumer_secret)) {
  59. $secret = common_config('twitter', 'consumer_secret');
  60. if (empty($secret)) {
  61. Config::save(
  62. 'twitter',
  63. 'consumer_secret',
  64. $this->consumer_secret
  65. );
  66. }
  67. }
  68. }
  69. /**
  70. * Check to see if there is a consumer key and secret defined
  71. * for Twitter integration.
  72. *
  73. * @return boolean result
  74. */
  75. public static function hasKeys()
  76. {
  77. $ckey = common_config('twitter', 'consumer_key');
  78. $csecret = common_config('twitter', 'consumer_secret');
  79. if (empty($ckey) && empty($csecret)) {
  80. $ckey = common_config('twitter', 'global_consumer_key');
  81. $csecret = common_config('twitter', 'global_consumer_secret');
  82. }
  83. if (!empty($ckey) && !empty($csecret)) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Add Twitter-related paths to the router table
  90. *
  91. * Hook for RouterInitialized event.
  92. *
  93. * @param URLMapper $m path-to-action mapper
  94. *
  95. * @return boolean hook return
  96. */
  97. public function onRouterInitialized(URLMapper $m)
  98. {
  99. $m->connect('panel/twitter', ['action' => 'twitteradminpanel']);
  100. if (self::hasKeys()) {
  101. $m->connect(
  102. 'twitter/authorization',
  103. ['action' => 'twitterauthorization']
  104. );
  105. $m->connect(
  106. 'settings/twitter',
  107. ['action' => 'twittersettings']
  108. );
  109. if (common_config('twitter', 'signin')) {
  110. $m->connect(
  111. 'main/twitterlogin',
  112. ['action' => 'twitterlogin']
  113. );
  114. }
  115. }
  116. return true;
  117. }
  118. /*
  119. * Add a login tab for 'Sign in with Twitter'
  120. *
  121. * @param Action $action the current action
  122. *
  123. * @return void
  124. */
  125. public function onEndLoginGroupNav($action)
  126. {
  127. $action_name = $action->trimmed('action');
  128. if (self::hasKeys() && common_config('twitter', 'signin')) {
  129. $action->menuItem(
  130. common_local_url('twitterlogin'),
  131. // TRANS: Menu item in login navigation.
  132. _m('MENU', 'Twitter'),
  133. // TRANS: Title for menu item in login navigation.
  134. _m('Login or register using Twitter.'),
  135. 'twitterlogin' === $action_name
  136. );
  137. }
  138. return true;
  139. }
  140. /**
  141. * Add the Twitter Settings page to the Connect Settings menu
  142. *
  143. * @param Action $action The calling page
  144. *
  145. * @return boolean hook return
  146. */
  147. public function onEndConnectSettingsNav($action)
  148. {
  149. if (self::hasKeys()) {
  150. $action_name = $action->trimmed('action');
  151. $action->menuItem(
  152. common_local_url('twittersettings'),
  153. // TRANS: Menu item in connection settings navigation.
  154. _m('MENU', 'Twitter'),
  155. // TRANS: Title for menu item in connection settings navigation.
  156. _m('Twitter integration options'),
  157. $action_name === 'twittersettings'
  158. );
  159. }
  160. return true;
  161. }
  162. /**
  163. * Add a Twitter queue item for each notice
  164. *
  165. * @param Notice $notice the notice
  166. * @param array &$transports the list of transports (queues)
  167. *
  168. * @return boolean hook return
  169. */
  170. public function onStartEnqueueNotice($notice, &$transports)
  171. {
  172. if (self::hasKeys() && $notice->isLocal() && $notice->inScope(null)) {
  173. // Avoid a possible loop
  174. if ($notice->source != 'twitter') {
  175. array_push($transports, 'twitter');
  176. }
  177. }
  178. return true;
  179. }
  180. /**
  181. * Add Twitter bridge daemons to the list of daemons to start
  182. *
  183. * @param array $daemons the list fo daemons to run
  184. *
  185. * @return boolean hook return
  186. */
  187. public function onGetValidDaemons(&$daemons)
  188. {
  189. if (self::hasKeys()) {
  190. array_push(
  191. $daemons,
  192. INSTALLDIR
  193. . '/plugins/TwitterBridge/daemons/synctwitterfriends.php'
  194. );
  195. if (common_config('twitterimport', 'enabled')) {
  196. array_push(
  197. $daemons,
  198. INSTALLDIR
  199. . '/plugins/TwitterBridge/daemons/twitterstatusfetcher.php'
  200. );
  201. }
  202. }
  203. return true;
  204. }
  205. /**
  206. * Register Twitter notice queue handler
  207. *
  208. * @param QueueManager $manager
  209. *
  210. * @return boolean hook return
  211. */
  212. public function onEndInitializeQueueManager($manager)
  213. {
  214. if (self::hasKeys()) {
  215. // Outgoing notices -> twitter
  216. $manager->connect('twitter', 'TwitterQueueHandler');
  217. // Incoming statuses <- twitter
  218. $manager->connect('tweetin', 'TweetInQueueHandler');
  219. }
  220. return true;
  221. }
  222. /**
  223. * If the plugin's installed, this should be accessible to admins
  224. */
  225. public function onAdminPanelCheck($name, &$isOK)
  226. {
  227. if ($name == 'twitter') {
  228. $isOK = true;
  229. return false;
  230. }
  231. return true;
  232. }
  233. /**
  234. * Add a Twitter tab to the admin panel
  235. *
  236. * @param Widget $nav Admin panel nav
  237. *
  238. * @return boolean hook value
  239. */
  240. public function onEndAdminPanelNav($nav)
  241. {
  242. if (AdminPanelAction::canAdmin('twitter')) {
  243. $action_name = $nav->action->trimmed('action');
  244. $nav->out->menuItem(
  245. common_local_url('twitteradminpanel'),
  246. // TRANS: Menu item in administrative panel that leads to the Twitter bridge configuration.
  247. _m('Twitter'),
  248. // TRANS: Menu item title in administrative panel that leads to the Twitter bridge configuration.
  249. _m('Twitter bridge configuration page.'),
  250. $action_name == 'twitteradminpanel',
  251. 'nav_twitter_admin_panel'
  252. );
  253. }
  254. return true;
  255. }
  256. /**
  257. * Plugin version data
  258. *
  259. * @param array &$versions array of version blocks
  260. *
  261. * @return boolean hook value
  262. */
  263. public function onPluginVersion(array &$versions): bool
  264. {
  265. $versions[] = array(
  266. 'name' => 'TwitterBridge',
  267. 'version' => self::PLUGIN_VERSION,
  268. 'author' => 'Zach Copley, Julien C, Jean Baptiste Favre',
  269. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/TwitterBridge',
  270. // TRANS: Plugin description.
  271. 'rawdescription' => _m(
  272. 'The Twitter "bridge" plugin allows integration ' .
  273. 'of a StatusNet instance with ' .
  274. '<a href="http://twitter.com/">Twitter</a>.'
  275. )
  276. );
  277. return true;
  278. }
  279. /**
  280. * Expose the adminImportControl setting to the administration panel code.
  281. * This allows us to disable the import bridge enabling checkbox for administrators,
  282. * since on a bulk farm site we can't yet automate the import daemon setup.
  283. *
  284. * @return boolean hook value;
  285. */
  286. public function onTwitterBridgeAdminImportControl()
  287. {
  288. return (bool)$this->adminImportControl;
  289. }
  290. /**
  291. * Database schema setup
  292. *
  293. * We maintain a table mapping StatusNet notices to Twitter statuses
  294. *
  295. * @see Schema
  296. * @see ColumnDef
  297. *
  298. * @return boolean hook value; true means continue processing, false means stop.
  299. */
  300. public function onCheckSchema()
  301. {
  302. $schema = Schema::get();
  303. // For saving the last-synched status of various timelines
  304. // home_timeline, messages (in), messages (out), ...
  305. $schema->ensureTable('twitter_synch_status', Twitter_synch_status::schemaDef());
  306. // For storing user-submitted flags on profiles
  307. $schema->ensureTable('notice_to_status', Notice_to_status::schemaDef());
  308. return true;
  309. }
  310. /**
  311. * If a notice gets deleted, remove the Notice_to_status mapping and
  312. * delete the status on Twitter.
  313. *
  314. * @param User $user The user doing the deleting
  315. * @param Notice $notice The notice getting deleted
  316. *
  317. * @return boolean hook value
  318. */
  319. public function onStartDeleteOwnNotice(User $user, Notice $notice)
  320. {
  321. $n2s = Notice_to_status::getKV('notice_id', $notice->id);
  322. if ($n2s instanceof Notice_to_status) {
  323. try {
  324. $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE); // twitter service
  325. } catch (NoResultException $e) {
  326. return true;
  327. }
  328. if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
  329. $this->log(LOG_INFO, "Skipping deleting notice for {$notice->id} since link is not OAuth.");
  330. return true;
  331. }
  332. try {
  333. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  334. $client = new TwitterOAuthClient($token->key, $token->secret);
  335. $client->statusesDestroy($n2s->status_id);
  336. } catch (Exception $e) {
  337. common_log(LOG_ERR, "Error attempting to delete bridged notice from Twitter: " . $e->getMessage());
  338. }
  339. $n2s->delete();
  340. }
  341. return true;
  342. }
  343. /**
  344. * Notify remote users when their notices get favorited.
  345. *
  346. * @param Profile or User $profile of local user doing the faving
  347. * @param Notice $notice being favored
  348. * @return hook return value
  349. */
  350. public function onEndFavorNotice(Profile $profile, Notice $notice)
  351. {
  352. try {
  353. $flink = Foreign_link::getByUserID($profile->getID(), TWITTER_SERVICE); // twitter service
  354. } catch (NoResultException $e) {
  355. return true;
  356. }
  357. if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
  358. $this->log(LOG_INFO, "Skipping fave processing for {$profile->getID()} since link is not OAuth.");
  359. return true;
  360. }
  361. $status_id = twitter_status_id($notice);
  362. if (empty($status_id)) {
  363. return true;
  364. }
  365. try {
  366. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  367. $client = new TwitterOAuthClient($token->key, $token->secret);
  368. $client->favoritesCreate($status_id);
  369. } catch (Exception $e) {
  370. common_log(LOG_ERR, "Error attempting to favorite bridged notice on Twitter: " . $e->getMessage());
  371. }
  372. return true;
  373. }
  374. /**
  375. * Notify remote users when their notices get de-favorited.
  376. *
  377. * @param Profile $profile Profile person doing the de-faving
  378. * @param Notice $notice Notice being favored
  379. *
  380. * @return hook return value
  381. */
  382. public function onEndDisfavorNotice(Profile $profile, Notice $notice)
  383. {
  384. try {
  385. $flink = Foreign_link::getByUserID($profile->getID(), TWITTER_SERVICE); // twitter service
  386. } catch (NoResultException $e) {
  387. return true;
  388. }
  389. if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
  390. $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
  391. return true;
  392. }
  393. $status_id = twitter_status_id($notice);
  394. if (empty($status_id)) {
  395. return true;
  396. }
  397. try {
  398. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  399. $client = new TwitterOAuthClient($token->key, $token->secret);
  400. $client->favoritesDestroy($status_id);
  401. } catch (Exception $e) {
  402. common_log(LOG_ERR, "Error attempting to unfavorite bridged notice on Twitter: " . $e->getMessage());
  403. }
  404. return true;
  405. }
  406. public function onStartGetProfileUri($profile, &$uri)
  407. {
  408. if (preg_match('!^https?://twitter.com/!', $profile->profileurl)) {
  409. $uri = $profile->profileurl;
  410. return false;
  411. }
  412. return true;
  413. }
  414. /**
  415. * Add links in the user's profile block to their Twitter profile URL.
  416. *
  417. * @param Profile $profile The profile being shown
  418. * @param Array &$links Writeable array of arrays (href, text, image).
  419. *
  420. * @return boolean hook value (true)
  421. */
  422. public function onOtherAccountProfiles($profile, &$links)
  423. {
  424. $fuser = null;
  425. try {
  426. $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
  427. $fuser = $flink->getForeignUser();
  428. $links[] = array("href" => $fuser->uri,
  429. "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
  430. "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
  431. } catch (NoResultException $e) {
  432. // no foreign link and/or user for Twitter on this profile ID
  433. }
  434. return true;
  435. }
  436. public function onEndShowHeadElements(Action $action)
  437. {
  438. // Showing a notice
  439. if ($action instanceof ShowNoticeAction) {
  440. $notice = Notice::getKV('id', $action->arg('notice'));
  441. if (is_null($notice)) {
  442. return true;
  443. }
  444. try {
  445. $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
  446. $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
  447. } catch (NoResultException $e) {
  448. return true;
  449. }
  450. $statusId = twitter_status_id($notice);
  451. if ($notice instanceof Notice && $notice->isLocal() && $statusId) {
  452. $tweetUrl = 'https://twitter.com/' . $fuser->nickname . '/status/' . $statusId;
  453. $action->element('link', array('rel' => 'syndication', 'href' => $tweetUrl));
  454. }
  455. }
  456. if (!($action instanceof AttachmentAction)) {
  457. return true;
  458. }
  459. /* Twitter card support. See https://dev.twitter.com/docs/cards */
  460. /* @fixme: should we display twitter cards only for attachments posted
  461. * by local users ? Seems mandatory to display twitter:creator
  462. *
  463. * Author: jbfavre
  464. */
  465. switch ($action->attachment->mimetype) {
  466. case 'image/pjpeg':
  467. case 'image/jpeg':
  468. case 'image/jpg':
  469. case 'image/png':
  470. case 'image/gif':
  471. $action->element(
  472. 'meta',
  473. [
  474. 'name' => 'twitter:card',
  475. 'content' => 'photo',
  476. ],
  477. null
  478. );
  479. $action->element(
  480. 'meta',
  481. [
  482. 'name' => 'twitter:url',
  483. 'content' => common_local_url(
  484. 'attachment',
  485. ['attachment' => $action->attachment->id]
  486. )
  487. ],
  488. null
  489. );
  490. $action->element(
  491. 'meta',
  492. [
  493. 'name' => 'twitter:image',
  494. 'content' => $action->attachment->url,
  495. ]
  496. );
  497. $action->element(
  498. 'meta',
  499. [
  500. 'name' => 'twitter:title',
  501. 'content' => $action->attachment->title,
  502. ]
  503. );
  504. $ns = new AttachmentNoticeSection($action);
  505. $notices = $ns->getNotices();
  506. $noticeArray = $notices->fetchAll();
  507. // Should not have more than 1 notice for this attachment.
  508. if (count($noticeArray) != 1) {
  509. break;
  510. }
  511. $post = $noticeArray[0];
  512. try {
  513. $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
  514. $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
  515. $action->element('meta', array('name' => 'twitter:creator',
  516. 'content' => '@'.$fuser->nickname));
  517. } catch (NoResultException $e) {
  518. // no foreign link and/or user for Twitter on this profile ID
  519. }
  520. break;
  521. default:
  522. break;
  523. }
  524. return true;
  525. }
  526. /**
  527. * Set the object_type field of previously imported Twitter notices to
  528. * ActivityObject::NOTE if they are unset. Null object_type caused a notice
  529. * not to show on the timeline.
  530. */
  531. public function onEndUpgrade()
  532. {
  533. printfnq('Ensuring all Twitter notices have an object_type...');
  534. $notice = new Notice();
  535. $notice->whereAdd("source = 'twitter'");
  536. $notice->whereAdd('object_type IS NULL');
  537. if ($notice->find()) {
  538. while ($notice->fetch()) {
  539. $orig = Notice::getKV('id', $notice->id);
  540. $notice->object_type = ActivityObject::NOTE;
  541. $notice->update($orig);
  542. }
  543. }
  544. printfnq("DONE.\n");
  545. }
  546. }