imsettings.php 16 KB

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