twittersettings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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('friendsync',
  146. // TRANS: Checkbox label.
  147. _m('Subscribe to my Twitter friends here.'),
  148. $this->flink->friendsync & FOREIGN_FRIEND_RECV);
  149. $this->elementEnd('li');
  150. if (common_config('twitterimport','enabled')) {
  151. $this->elementStart('li');
  152. $this->checkbox('noticerecv',
  153. // TRANS: Checkbox label.
  154. _m('Import my friends timeline.'),
  155. $this->flink->noticesync & FOREIGN_NOTICE_RECV);
  156. $this->elementEnd('li');
  157. } else {
  158. // preserve setting even if bidrection bridge toggled off
  159. if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
  160. $this->hidden('noticerecv', true, 'noticerecv');
  161. }
  162. }
  163. $this->elementEnd('ul');
  164. if ($this->flink instanceof Foreign_link) {
  165. // TRANS: Button text for saving Twitter integration settings.
  166. $this->submit('save', _m('BUTTON','Save'));
  167. } else {
  168. // TRANS: Button text for adding Twitter integration.
  169. $this->submit('add', _m('BUTTON','Add'));
  170. }
  171. } else {
  172. $this->elementStart('ul', 'form_data');
  173. $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
  174. $this->element('a', array('href' => common_local_url('twitterauthorization')),
  175. // TRANS: Link description to connect to a Twitter account.
  176. 'Connect my Twitter account');
  177. $this->elementEnd('li');
  178. $this->elementEnd('ul');
  179. }
  180. $this->elementEnd('fieldset');
  181. $this->elementEnd('form');
  182. }
  183. /**
  184. * Handle posts to this form
  185. *
  186. * Based on the button that was pressed, muxes out to other functions
  187. * to do the actual task requested.
  188. *
  189. * All sub-functions reload the form with a message -- success or failure.
  190. *
  191. * @return void
  192. */
  193. protected function doPost()
  194. {
  195. if ($this->arg('save')) {
  196. return $this->savePreferences();
  197. } else if ($this->arg('disconnect')) {
  198. return $this->removeTwitterAccount();
  199. }
  200. // TRANS: Client error displayed when the submitted form contains unexpected data.
  201. throw new ClientException(_m('Unexpected form submission.'));
  202. }
  203. /**
  204. * Disassociate an existing Twitter account from this account
  205. *
  206. * @return void
  207. */
  208. protected function removeTwitterAccount()
  209. {
  210. if (!$this->flink instanceof Foreign_link) {
  211. // TRANS: Error message possibly displayed when trying to remove a connected Twitter account when there isn't one connected.
  212. throw new AlreadyFulfilledException(_m('No Twitter connection to remove.'));
  213. }
  214. $result = $this->flink->safeDelete();
  215. if ($result === false) {
  216. common_log_db_error($this->flink, 'DELETE', __FILE__);
  217. // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
  218. throw new ServerException(_m('Could not remove Twitter user.'));
  219. }
  220. $this->flink = null;
  221. $this->fuser = null;
  222. // TRANS: Success message displayed after disconnecting a Twitter account.
  223. return _m('Twitter account disconnected.');
  224. }
  225. /**
  226. * Save user's Twitter-bridging preferences
  227. *
  228. * @return void
  229. */
  230. protected function savePreferences()
  231. {
  232. $noticesend = $this->boolean('noticesend');
  233. $noticerecv = $this->boolean('noticerecv');
  234. $friendsync = $this->boolean('friendsync');
  235. $replysync = $this->boolean('replysync');
  236. if (!$this->flink instanceof Foreign_link) {
  237. common_log_db_error($this->flink, 'SELECT', __FILE__);
  238. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  239. throw new ServerException(_m('Your account is not linked to Twitter.'));
  240. }
  241. $original = clone($this->flink);
  242. $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
  243. $this->flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
  244. $result = $this->flink->update($original);
  245. if ($result === false) {
  246. common_log_db_error($this->flink, 'UPDATE', __FILE__);
  247. // TRANS: Server error displayed when saving Twitter integration preferences fails.
  248. throw new ServerException(_m('Could not save Twitter preferences.'));
  249. }
  250. if ($wasReceiving xor $noticerecv) {
  251. $this->notifyDaemon($this->flink->foreign_id, $noticerecv);
  252. }
  253. // TRANS: Success message after saving Twitter integration preferences.
  254. return _m('Twitter preferences saved.');
  255. }
  256. /**
  257. * Tell the import daemon that we've updated a user's receive status.
  258. */
  259. function notifyDaemon($twitterUserId, $receiving)
  260. {
  261. // @todo... should use control signals rather than queues
  262. }
  263. }