twittersettings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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('GNUSOCIAL')) { exit(1); }
  30. require_once dirname(__DIR__) . '/twitter.php';
  31. /**
  32. * Settings for Twitter integration
  33. *
  34. * @category Settings
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://status.net/
  39. *
  40. * @see SettingsAction
  41. */
  42. class TwittersettingsAction extends ProfileSettingsAction
  43. {
  44. protected $flink = null;
  45. protected $fuser = null;
  46. protected function doPreparation()
  47. {
  48. try {
  49. $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
  50. $this->fuser = $this->flink->getForeignUser();
  51. } catch (NoResultException $e) {
  52. // No foreign link found for this user!
  53. }
  54. }
  55. /**
  56. * Title of the page
  57. *
  58. * @return string Title of the page
  59. */
  60. function title()
  61. {
  62. // TRANS: Title for page with Twitter integration settings.
  63. return _m('Twitter settings');
  64. }
  65. /**
  66. * Instructions for use
  67. *
  68. * @return instructions for use
  69. */
  70. function getInstructions()
  71. {
  72. // TRANS: Instructions for page with Twitter integration settings.
  73. return _m('Connect your Twitter account to share your updates ' .
  74. 'with your Twitter friends and vice-versa.');
  75. }
  76. /**
  77. * Content area of the page
  78. *
  79. * Shows a form for associating a Twitter account with this
  80. * StatusNet account. Also lets the user set preferences.
  81. *
  82. * @return void
  83. */
  84. function showContent()
  85. {
  86. $this->elementStart('form', array('method' => 'post',
  87. 'id' => 'form_settings_twitter',
  88. 'class' => 'form_settings',
  89. 'action' =>
  90. common_local_url('twittersettings')));
  91. $this->hidden('token', common_session_token());
  92. $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
  93. if ($this->fuser instanceof Foreign_user) {
  94. // TRANS: Fieldset legend.
  95. $this->element('legend', null, _m('Twitter account'));
  96. $this->elementStart('p', array('id' => 'form_confirmed'));
  97. $this->element('a', array('href' => $this->fuser->uri), $this->fuser->nickname);
  98. $this->elementEnd('p');
  99. $this->element('p', 'form_note',
  100. // TRANS: Form note when a Twitter account has been connected.
  101. _m('Connected Twitter account'));
  102. $this->elementEnd('fieldset');
  103. $this->elementStart('fieldset');
  104. // TRANS: Fieldset legend.
  105. $this->element('legend', null, _m('Disconnect my account from Twitter'));
  106. if (!$this->scoped->hasPassword()) {
  107. $this->elementStart('p', array('class' => 'form_guide'));
  108. // TRANS: Form guide. %s is a URL to the password settings.
  109. // TRANS: This message contains a Markdown link in the form [description](link).
  110. $message = sprintf(_m('Disconnecting your Twitter account ' .
  111. 'could make it impossible to log in! Please ' .
  112. '[set a password](%s) first.'),
  113. common_local_url('passwordsettings'));
  114. $message = common_markup_to_html($message);
  115. $this->text($message);
  116. $this->elementEnd('p');
  117. } else {
  118. // TRANS: Form instructions. %1$s is the StatusNet sitename.
  119. $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
  120. 'You can use your %1$s password to log in.');
  121. $site = common_config('site', 'name');
  122. $this->element('p', 'instructions',
  123. sprintf($note, $site));
  124. // TRANS: Button text for disconnecting a Twitter account.
  125. $this->submit('disconnect', _m('BUTTON','Disconnect'));
  126. }
  127. $this->elementEnd('fieldset');
  128. $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
  129. // TRANS: Fieldset legend.
  130. $this->element('legend', null, _m('Preferences'));
  131. $this->elementStart('ul', 'form_data');
  132. $this->elementStart('li');
  133. $this->checkbox('noticesend',
  134. // TRANS: Checkbox label.
  135. _m('Automatically send my notices to Twitter.'),
  136. $this->flink->noticesync & FOREIGN_NOTICE_SEND);
  137. $this->elementEnd('li');
  138. $this->elementStart('li');
  139. $this->checkbox('replysync',
  140. // TRANS: Checkbox label.
  141. _m('Send local "@" replies to Twitter.'),
  142. $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY);
  143. $this->elementEnd('li');
  144. $this->elementStart('li');
  145. $this->checkbox('repeatsync',
  146. // TRANS: Checkbox label.
  147. _m('Send local repeats to Twitter.'),
  148. $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPEAT);
  149. $this->elementEnd('li');
  150. $this->elementStart('li');
  151. $this->checkbox('friendsync',
  152. // TRANS: Checkbox label.
  153. _m('Subscribe to my Twitter friends here.'),
  154. $this->flink->friendsync & FOREIGN_FRIEND_RECV);
  155. $this->elementEnd('li');
  156. if (common_config('twitterimport','enabled')) {
  157. $this->elementStart('li');
  158. $this->checkbox('noticerecv',
  159. // TRANS: Checkbox label.
  160. _m('Import my friends timeline.'),
  161. $this->flink->noticesync & FOREIGN_NOTICE_RECV);
  162. $this->elementEnd('li');
  163. } else {
  164. // preserve setting even if bidrection bridge toggled off
  165. if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
  166. $this->hidden('noticerecv', true, 'noticerecv');
  167. }
  168. }
  169. $this->elementEnd('ul');
  170. if ($this->flink instanceof Foreign_link) {
  171. // TRANS: Button text for saving Twitter integration settings.
  172. $this->submit('save', _m('BUTTON','Save'));
  173. } else {
  174. // TRANS: Button text for adding Twitter integration.
  175. $this->submit('add', _m('BUTTON','Add'));
  176. }
  177. } else {
  178. $this->elementStart('ul', 'form_data');
  179. $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
  180. $this->element('a', array('href' => common_local_url('twitterauthorization')),
  181. // TRANS: Link description to connect to a Twitter account.
  182. 'Connect my Twitter account');
  183. $this->elementEnd('li');
  184. $this->elementEnd('ul');
  185. }
  186. $this->elementEnd('fieldset');
  187. $this->elementEnd('form');
  188. }
  189. /**
  190. * Handle posts to this form
  191. *
  192. * Based on the button that was pressed, muxes out to other functions
  193. * to do the actual task requested.
  194. *
  195. * All sub-functions reload the form with a message -- success or failure.
  196. *
  197. * @return void
  198. */
  199. protected function doPost()
  200. {
  201. if ($this->arg('save')) {
  202. return $this->savePreferences();
  203. } else if ($this->arg('disconnect')) {
  204. return $this->removeTwitterAccount();
  205. }
  206. // TRANS: Client error displayed when the submitted form contains unexpected data.
  207. throw new ClientException(_m('Unexpected form submission.'));
  208. }
  209. /**
  210. * Disassociate an existing Twitter account from this account
  211. *
  212. * @return void
  213. */
  214. protected function removeTwitterAccount()
  215. {
  216. if (!$this->flink instanceof Foreign_link) {
  217. // TRANS: Error message possibly displayed when trying to remove a connected Twitter account when there isn't one connected.
  218. throw new AlreadyFulfilledException(_m('No Twitter connection to remove.'));
  219. }
  220. $result = $this->flink->safeDelete();
  221. if ($result === false) {
  222. common_log_db_error($this->flink, 'DELETE', __FILE__);
  223. // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
  224. throw new ServerException(_m('Could not remove Twitter user.'));
  225. }
  226. $this->flink = null;
  227. $this->fuser = null;
  228. // TRANS: Success message displayed after disconnecting a Twitter account.
  229. return _m('Twitter account disconnected.');
  230. }
  231. /**
  232. * Save user's Twitter-bridging preferences
  233. *
  234. * @return void
  235. */
  236. protected function savePreferences()
  237. {
  238. $noticesend = $this->boolean('noticesend');
  239. $noticerecv = $this->boolean('noticerecv');
  240. $friendsync = $this->boolean('friendsync');
  241. $replysync = $this->boolean('replysync');
  242. $repeatsync = $this->boolean('repeatsync');
  243. if (!$this->flink instanceof Foreign_link) {
  244. common_log_db_error($this->flink, 'SELECT', __FILE__);
  245. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  246. throw new ServerException(_m('Your account is not linked to Twitter.'));
  247. }
  248. $original = clone($this->flink);
  249. $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
  250. $this->flink->set_flags($noticesend, $noticerecv, $replysync, $repeatsync, $friendsync);
  251. $result = $this->flink->update($original);
  252. if ($result === false) {
  253. common_log_db_error($this->flink, 'UPDATE', __FILE__);
  254. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  255. throw new ServerException(_m('Could not save Twitter preferences.'));
  256. }
  257. if ($wasReceiving xor $noticerecv) {
  258. $this->notifyDaemon($this->flink->foreign_id, $noticerecv);
  259. }
  260. // TRANS: Success message after saving Twitter integration preferences.
  261. return _m('Twitter preferences saved.');
  262. }
  263. /**
  264. * Tell the import daemon that we've updated a user's receive status.
  265. */
  266. function notifyDaemon($twitterUserId, $receiving)
  267. {
  268. // @todo... should use control signals rather than queues
  269. }
  270. }