imsettings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. * Settings for Jabber/XMPP integration
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2008-2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Settings for Jabber/XMPP integration
  28. *
  29. * @category Settings
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. *
  34. * @see SettingsAction
  35. */
  36. class ImsettingsAction extends SettingsAction
  37. {
  38. /**
  39. * Title of the page
  40. *
  41. * @return string Title of the page
  42. */
  43. public function title()
  44. {
  45. // TRANS: Title for Instant Messaging settings.
  46. return _('IM settings');
  47. }
  48. /**
  49. * Instructions for use
  50. *
  51. * @return instructions for use
  52. */
  53. public function getInstructions()
  54. {
  55. // TRANS: Instant messaging settings page instructions.
  56. // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
  57. // TRANS: the order and formatting of link text and link should remain unchanged.
  58. return _('You can send and receive notices through '.
  59. '[instant messaging](%%doc.im%%). '.
  60. 'Configure your addresses and settings below.');
  61. }
  62. /**
  63. * Content area of the page
  64. *
  65. * We make different sections of the form for the different kinds of
  66. * functions, and have submit buttons with different names. These
  67. * are muxed by handlePost() to see what the user really wants to do.
  68. *
  69. * @return void
  70. */
  71. public function showContent()
  72. {
  73. $transports = array();
  74. Event::handle('GetImTransports', array(&$transports));
  75. if (! $transports) {
  76. $this->element(
  77. 'div',
  78. ['class' => 'error'],
  79. // TRANS: Message given in the IM settings if IM is not enabled on the site.
  80. _('IM is not available.')
  81. );
  82. return;
  83. }
  84. $user = common_current_user();
  85. $user_im_prefs_by_transport = array();
  86. foreach ($transports as $transport => $transport_info) {
  87. $this->elementStart(
  88. 'form',
  89. [
  90. 'method' => 'post',
  91. 'id' => 'form_settings_im',
  92. 'class' => 'form_settings',
  93. 'action' => common_local_url('imsettings'),
  94. ]
  95. );
  96. $this->elementStart('fieldset', array('id' => 'settings_im_address'));
  97. // TRANS: Form legend for IM settings form.
  98. $this->element('legend', null, $transport_info['display']);
  99. $this->hidden('token', common_session_token());
  100. $this->hidden('transport', $transport);
  101. if (!empty($user_im_prefs = User_im_prefs::pkeyGet([
  102. 'transport' => $transport,
  103. 'user_id' => $user->id,
  104. ]))) {
  105. $user_im_prefs_by_transport[$transport] = $user_im_prefs;
  106. $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
  107. $this->element(
  108. 'p',
  109. 'form_note',
  110. // TRANS: Form note in IM settings form. %s is the type of IM address that was confirmed.
  111. sprintf(_('Current confirmed %s address.'), $transport_info['display'])
  112. );
  113. $this->hidden('screenname', $user_im_prefs->screenname);
  114. // TRANS: Button label to remove a confirmed IM address.
  115. $this->submit('remove', _m('BUTTON', 'Remove'));
  116. } else {
  117. try {
  118. $confirm = $this->getConfirmation($transport);
  119. $this->element('p', 'form_unconfirmed', $confirm->address);
  120. // TRANS: Form note in IM settings form.
  121. $this->element(
  122. 'p',
  123. 'form_note',
  124. // TRANS: Form note in IM settings form.
  125. // TRANS: %s is the IM service name, %2$s is the IM address set.
  126. sprintf(
  127. _('Awaiting confirmation on this address. '
  128. . 'Check your %1$s account for a '
  129. . 'message with further instructions. '
  130. . '(Did you add %2$s to your buddy list?)'),
  131. $transport_info['display'],
  132. $transport_info['daemonScreenname']
  133. )
  134. );
  135. $this->hidden('screenname', $confirm->address);
  136. // TRANS: Button label to cancel an IM address confirmation procedure.
  137. $this->submit('cancel', _m('BUTTON', 'Cancel'));
  138. } catch (NoResultException $e) {
  139. $this->elementStart('ul', 'form_data');
  140. $this->elementStart('li');
  141. // TRANS: Field label for IM address.
  142. $this->input(
  143. 'screenname',
  144. _('IM address'),
  145. ($this->arg('screenname')) ? $this->arg('screenname') : null,
  146. // TRANS: Field title for IM address. %s is the IM service name.
  147. sprintf(_('%s screenname.'), $transport_info['display'])
  148. );
  149. $this->elementEnd('li');
  150. $this->elementEnd('ul');
  151. // TRANS: Button label for adding an IM address in IM settings form.
  152. $this->submit('add', _m('BUTTON', 'Add'));
  153. }
  154. }
  155. $this->elementEnd('fieldset');
  156. $this->elementEnd('form');
  157. }
  158. if ($user_im_prefs_by_transport) {
  159. $this->elementStart(
  160. 'form',
  161. [
  162. 'method' => 'post',
  163. 'id' => 'form_settings_im',
  164. 'class' => 'form_settings',
  165. 'action' => common_local_url('imsettings'),
  166. ]
  167. );
  168. $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
  169. // TRANS: Header for IM preferences form.
  170. $this->element('legend', null, _('IM Preferences'));
  171. $this->hidden('token', common_session_token());
  172. $this->elementStart('table');
  173. $this->elementStart('tr');
  174. foreach ($user_im_prefs_by_transport as $transport => $user_im_prefs) {
  175. $this->element('th', null, $transports[$transport]['display']);
  176. }
  177. $this->elementEnd('tr');
  178. $preferences = array(
  179. // TRANS: Checkbox label in IM preferences form.
  180. array('name'=>'notify', 'description'=>_('Send me notices')),
  181. // TRANS: Checkbox label in IM preferences form.
  182. array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
  183. // TRANS: Checkbox label in IM preferences form.
  184. array('name'=>'replies', 'description'=>_('Send me replies '.
  185. 'from people I\'m not subscribed to.')),
  186. );
  187. foreach ($preferences as $preference) {
  188. $this->elementStart('tr');
  189. foreach ($user_im_prefs_by_transport as $transport => $user_im_prefs) {
  190. $preference_name = $preference['name'];
  191. $this->elementStart('td');
  192. $this->checkbox(
  193. $transport . '_' . $preference['name'],
  194. $preference['description'],
  195. $user_im_prefs->$preference_name
  196. );
  197. $this->elementEnd('td');
  198. }
  199. $this->elementEnd('tr');
  200. }
  201. $this->elementEnd('table');
  202. // TRANS: Button label to save IM preferences.
  203. $this->submit('save', _m('BUTTON', 'Save'));
  204. $this->elementEnd('fieldset');
  205. $this->elementEnd('form');
  206. }
  207. }
  208. /**
  209. * Get a confirmation code for this user
  210. *
  211. * @return Confirm_address address object for this user
  212. */
  213. public function getConfirmation($transport)
  214. {
  215. $confirm = new Confirm_address();
  216. $confirm->user_id = $this->scoped->getID();
  217. $confirm->address_type = $transport;
  218. if ($confirm->find(true)) {
  219. return $confirm;
  220. }
  221. throw new NoResultException($confirm);
  222. }
  223. protected function doPost()
  224. {
  225. if ($this->arg('save')) {
  226. return $this->savePreferences();
  227. } elseif ($this->arg('add')) {
  228. return $this->addAddress();
  229. } elseif ($this->arg('cancel')) {
  230. return $this->cancelConfirmation();
  231. } elseif ($this->arg('remove')) {
  232. return $this->removeAddress();
  233. }
  234. // TRANS: Message given submitting a form with an unknown action in Instant Messaging settings.
  235. throw new ClientException(_('Unexpected form submission.'));
  236. }
  237. /**
  238. * Save user's XMPP preferences
  239. *
  240. * These are the checkboxes at the bottom of the page. They're used to
  241. * set different settings
  242. *
  243. * @return void
  244. */
  245. public function savePreferences()
  246. {
  247. $user_im_prefs = new User_im_prefs();
  248. $user_im_prefs->query('START TRANSACTION');
  249. $user_im_prefs->user_id = $this->scoped->getID();
  250. if ($user_im_prefs->find() && $user_im_prefs->fetch()) {
  251. $preferences = array('notify', 'updatefrompresence', 'replies');
  252. do {
  253. $original = clone($user_im_prefs);
  254. $new = clone($user_im_prefs);
  255. foreach ($preferences as $preference) {
  256. $new->$preference = $this->boolean($new->transport . '_' . $preference);
  257. }
  258. $result = $new->update($original);
  259. if ($result === false) {
  260. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  261. // TRANS: Server error thrown on database error updating IM preferences.
  262. throw new ServerException(_('Could not update IM preferences.'));
  263. }
  264. } while ($user_im_prefs->fetch());
  265. }
  266. $user_im_prefs->query('COMMIT');
  267. // TRANS: Confirmation message for successful IM preferences save.
  268. return _('Preferences saved.');
  269. }
  270. /**
  271. * Sends a confirmation to the address given
  272. *
  273. * Stores a confirmation record and sends out a
  274. * message with the confirmation info.
  275. *
  276. * @return void
  277. */
  278. public function addAddress()
  279. {
  280. $screenname = $this->trimmed('screenname');
  281. $transport = $this->trimmed('transport');
  282. // Some validation
  283. if (empty($screenname)) {
  284. // TRANS: Message given saving IM address without having provided one.
  285. throw new ClientException(_('No screenname.'));
  286. }
  287. if (empty($transport)) {
  288. // TRANS: Form validation error when no transport is available setting an IM address.
  289. throw new ClientException(_('No transport.'));
  290. }
  291. Event::handle('NormalizeImScreenname', array($transport, &$screenname));
  292. if (empty($screenname)) {
  293. // TRANS: Message given saving IM address that cannot be normalised.
  294. throw new ClientException(_('Cannot normalize that screenname.'));
  295. }
  296. $valid = false;
  297. Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
  298. if (!$valid) {
  299. // TRANS: Message given saving IM address that not valid.
  300. throw new ClientException(_('Not a valid screenname.'));
  301. } elseif ($this->screennameExists($transport, $screenname)) {
  302. // TRANS: Message given saving IM address that is already set for another user.
  303. throw new ClientException(_('Screenname already belongs to another user.'));
  304. }
  305. $confirm = new Confirm_address();
  306. $confirm->address = $screenname;
  307. $confirm->address_type = $transport;
  308. $confirm->user_id = $this->scoped->getID();
  309. $confirm->code = common_confirmation_code(64);
  310. $confirm->sent = common_sql_now();
  311. $confirm->claimed = common_sql_now();
  312. $result = $confirm->insert();
  313. if ($result === false) {
  314. common_log_db_error($confirm, 'INSERT', __FILE__);
  315. // TRANS: Server error thrown on database error adding Instant Messaging confirmation code.
  316. $this->serverError(_('Could not insert confirmation code.'));
  317. }
  318. Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $this->scoped));
  319. // TRANS: Message given saving valid IM address that is to be confirmed.
  320. return _('A confirmation code was sent to the IM address you added.');
  321. }
  322. /**
  323. * Cancel a confirmation
  324. *
  325. * If a confirmation exists, cancel it.
  326. *
  327. * @return void
  328. */
  329. public function cancelConfirmation()
  330. {
  331. $screenname = $this->trimmed('screenname');
  332. $transport = $this->trimmed('transport');
  333. try {
  334. $confirm = $this->getConfirmation($transport);
  335. if ($confirm->address != $screenname) {
  336. // TRANS: Message given canceling IM address confirmation for the wrong IM address.
  337. throw new ClientException(_('That is the wrong IM address.'));
  338. }
  339. } catch (NoResultException $e) {
  340. // TRANS: Message given canceling Instant Messaging address confirmation that is not pending.
  341. throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
  342. }
  343. $confirm->delete();
  344. // TRANS: Message given after successfully canceling IM address confirmation.
  345. return _('IM confirmation cancelled.');
  346. }
  347. /**
  348. * Remove an address
  349. *
  350. * If the user has a confirmed address, remove it.
  351. *
  352. * @return void
  353. */
  354. public function removeAddress()
  355. {
  356. $screenname = $this->trimmed('screenname');
  357. $transport = $this->trimmed('transport');
  358. // Maybe an old tab open...?
  359. $user_im_prefs = new User_im_prefs();
  360. $user_im_prefs->user_id = $this->scoped->getID();
  361. $user_im_prefs->transport = $transport;
  362. if (!$user_im_prefs->find(true)) {
  363. // TRANS: Message given trying to remove an IM address that is not
  364. // TRANS: registered for the active user.
  365. throw new AlreadyFulfilledException(_('There were no preferences stored for this transport.'));
  366. }
  367. $result = $user_im_prefs->delete();
  368. if ($result === false) {
  369. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  370. // TRANS: Server error thrown on database error removing a registered IM address.
  371. throw new ServerException(_('Could not update user IM preferences.'));
  372. }
  373. // XXX: unsubscribe to the old address
  374. // TRANS: Message given after successfully removing a registered Instant Messaging address.
  375. return _('The IM address was removed.');
  376. }
  377. /**
  378. * Does this screenname exist?
  379. *
  380. * Checks if we already have another user with this address.
  381. *
  382. * @param string $transport Transport to check
  383. * @param string $screenname Screenname to check
  384. *
  385. * @return boolean whether the screenname exists
  386. */
  387. public function screennameExists($transport, $screenname)
  388. {
  389. $user_im_prefs = new User_im_prefs();
  390. $user_im_prefs->transport = $transport;
  391. $user_im_prefs->screenname = $screenname;
  392. return $user_im_prefs->find(true) ? true : false;
  393. }
  394. }