imsettings.php 18 KB

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