twittersettings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Settings for Twitter integration
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. require_once dirname(__DIR__) . '/twitter.php';
  33. /**
  34. * Settings for Twitter integration
  35. *
  36. * @category Settings
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. *
  42. * @see SettingsAction
  43. */
  44. class TwittersettingsAction extends ProfileSettingsAction
  45. {
  46. /**
  47. * Title of the page
  48. *
  49. * @return string Title of the page
  50. */
  51. function title()
  52. {
  53. // TRANS: Title for page with Twitter integration settings.
  54. return _m('Twitter settings');
  55. }
  56. /**
  57. * Instructions for use
  58. *
  59. * @return instructions for use
  60. */
  61. function getInstructions()
  62. {
  63. // TRANS: Instructions for page with Twitter integration settings.
  64. return _m('Connect your Twitter account to share your updates ' .
  65. 'with your Twitter friends and vice-versa.');
  66. }
  67. /**
  68. * Content area of the page
  69. *
  70. * Shows a form for associating a Twitter account with this
  71. * StatusNet account. Also lets the user set preferences.
  72. *
  73. * @return void
  74. */
  75. function showContent()
  76. {
  77. $user = common_current_user();
  78. $profile = $user->getProfile();
  79. $fuser = null;
  80. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  81. if (!empty($flink)) {
  82. $fuser = $flink->getForeignUser();
  83. }
  84. $this->elementStart('form', array('method' => 'post',
  85. 'id' => 'form_settings_twitter',
  86. 'class' => 'form_settings',
  87. 'action' =>
  88. common_local_url('twittersettings')));
  89. $this->hidden('token', common_session_token());
  90. $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
  91. if (empty($fuser)) {
  92. $this->elementStart('ul', 'form_data');
  93. $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
  94. $this->element('a', array('href' => common_local_url('twitterauthorization')),
  95. // TRANS: Link description to connect to a Twitter account.
  96. 'Connect my Twitter account');
  97. $this->elementEnd('li');
  98. $this->elementEnd('ul');
  99. $this->elementEnd('fieldset');
  100. } else {
  101. // TRANS: Fieldset legend.
  102. $this->element('legend', null, _m('Twitter account'));
  103. $this->elementStart('p', array('id' => 'form_confirmed'));
  104. $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
  105. $this->elementEnd('p');
  106. $this->element('p', 'form_note',
  107. // TRANS: Form note when a Twitter account has been connected.
  108. _m('Connected Twitter account'));
  109. $this->elementEnd('fieldset');
  110. $this->elementStart('fieldset');
  111. // TRANS: Fieldset legend.
  112. $this->element('legend', null, _m('Disconnect my account from Twitter'));
  113. if (!$user->password) {
  114. $this->elementStart('p', array('class' => 'form_guide'));
  115. // TRANS: Form guide. %s is a URL to the password settings.
  116. // TRANS: This message contains a Markdown link in the form [description](link).
  117. $message = sprintf(_m('Disconnecting your Twitter account ' .
  118. 'could make it impossible to log in! Please ' .
  119. '[set a password](%s) first.'),
  120. common_local_url('passwordsettings'));
  121. $message = common_markup_to_html($message);
  122. $this->text($message);
  123. $this->elementEnd('p');
  124. } else {
  125. // TRANS: Form instructions. %1$s is the StatusNet sitename.
  126. $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
  127. 'You can use your %1$s password to log in.');
  128. $site = common_config('site', 'name');
  129. $this->element('p', 'instructions',
  130. sprintf($note, $site));
  131. // TRANS: Button text for disconnecting a Twitter account.
  132. $this->submit('disconnect', _m('BUTTON','Disconnect'));
  133. }
  134. $this->elementEnd('fieldset');
  135. $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
  136. // TRANS: Fieldset legend.
  137. $this->element('legend', null, _m('Preferences'));
  138. $this->elementStart('ul', 'form_data');
  139. $this->elementStart('li');
  140. $this->checkbox('noticesend',
  141. // TRANS: Checkbox label.
  142. _m('Automatically send my notices to Twitter.'),
  143. ($flink) ?
  144. ($flink->noticesync & FOREIGN_NOTICE_SEND) :
  145. true);
  146. $this->elementEnd('li');
  147. $this->elementStart('li');
  148. $this->checkbox('replysync',
  149. // TRANS: Checkbox label.
  150. _m('Send local "@" replies to Twitter.'),
  151. ($flink) ?
  152. ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
  153. true);
  154. $this->elementEnd('li');
  155. $this->elementStart('li');
  156. $this->checkbox('friendsync',
  157. // TRANS: Checkbox label.
  158. _m('Subscribe to my Twitter friends here.'),
  159. ($flink) ?
  160. ($flink->friendsync & FOREIGN_FRIEND_RECV) :
  161. false);
  162. $this->elementEnd('li');
  163. if (common_config('twitterimport','enabled')) {
  164. $this->elementStart('li');
  165. $this->checkbox('noticerecv',
  166. // TRANS: Checkbox label.
  167. _m('Import my friends timeline.'),
  168. ($flink) ?
  169. ($flink->noticesync & FOREIGN_NOTICE_RECV) :
  170. false);
  171. $this->elementEnd('li');
  172. } else {
  173. // preserve setting even if bidrection bridge toggled off
  174. if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
  175. $this->hidden('noticerecv', true, 'noticerecv');
  176. }
  177. }
  178. $this->elementEnd('ul');
  179. if ($flink) {
  180. // TRANS: Button text for saving Twitter integration settings.
  181. $this->submit('save', _m('BUTTON','Save'));
  182. } else {
  183. // TRANS: Button text for adding Twitter integration.
  184. $this->submit('add', _m('BUTTON','Add'));
  185. }
  186. $this->elementEnd('fieldset');
  187. }
  188. $this->elementEnd('form');
  189. }
  190. /**
  191. * Handle posts to this form
  192. *
  193. * Based on the button that was pressed, muxes out to other functions
  194. * to do the actual task requested.
  195. *
  196. * All sub-functions reload the form with a message -- success or failure.
  197. *
  198. * @return void
  199. */
  200. function handlePost()
  201. {
  202. // CSRF protection
  203. $token = $this->trimmed('token');
  204. if (!$token || $token != common_session_token()) {
  205. // TRANS: Client error displayed when the session token does not match or is not given.
  206. $this->showForm(_m('There was a problem with your session token. '.
  207. 'Try again, please.'));
  208. return;
  209. }
  210. if ($this->arg('save')) {
  211. $this->savePreferences();
  212. } else if ($this->arg('disconnect')) {
  213. $this->removeTwitterAccount();
  214. } else {
  215. // TRANS: Client error displayed when the submitted form contains unexpected data.
  216. $this->showForm(_m('Unexpected form submission.'));
  217. }
  218. }
  219. /**
  220. * Disassociate an existing Twitter account from this account
  221. *
  222. * @return void
  223. */
  224. function removeTwitterAccount()
  225. {
  226. $user = common_current_user();
  227. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  228. if (empty($flink)) {
  229. // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
  230. $this->clientError(_m('No Twitter connection to remove.'));
  231. }
  232. $result = $flink->safeDelete();
  233. if (empty($result)) {
  234. common_log_db_error($flink, 'DELETE', __FILE__);
  235. // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
  236. $this->serverError(_m('Could not remove Twitter user.'));
  237. }
  238. // TRANS: Success message displayed after disconnecting a Twitter account.
  239. $this->showForm(_m('Twitter account disconnected.'), true);
  240. }
  241. /**
  242. * Save user's Twitter-bridging preferences
  243. *
  244. * @return void
  245. */
  246. function savePreferences()
  247. {
  248. $noticesend = $this->boolean('noticesend');
  249. $noticerecv = $this->boolean('noticerecv');
  250. $friendsync = $this->boolean('friendsync');
  251. $replysync = $this->boolean('replysync');
  252. $user = common_current_user();
  253. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  254. if (empty($flink)) {
  255. common_log_db_error($flink, 'SELECT', __FILE__);
  256. // @todo FIXME: Shouldn't this be a serverError()?
  257. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  258. $this->showForm(_m('Could not save Twitter preferences.'));
  259. return;
  260. }
  261. $original = clone($flink);
  262. $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
  263. $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
  264. $result = $flink->update($original);
  265. if ($result === false) {
  266. common_log_db_error($flink, 'UPDATE', __FILE__);
  267. // @todo FIXME: Shouldn't this be a serverError()?
  268. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  269. $this->showForm(_m('Could not save Twitter preferences.'));
  270. return;
  271. }
  272. if ($wasReceiving xor $noticerecv) {
  273. $this->notifyDaemon($flink->foreign_id, $noticerecv);
  274. }
  275. // TRANS: Success message after saving Twitter integration preferences.
  276. $this->showForm(_m('Twitter preferences saved.'), true);
  277. }
  278. /**
  279. * Tell the import daemon that we've updated a user's receive status.
  280. */
  281. function notifyDaemon($twitterUserId, $receiving)
  282. {
  283. // @todo... should use control signals rather than queues
  284. }
  285. }