emailsettings.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 email
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @copyright 2008-2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Settings for email
  29. *
  30. * @copyright 2008-2009 StatusNet, Inc.
  31. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  32. *
  33. * @see Widget
  34. */
  35. class EmailsettingsAction extends SettingsAction
  36. {
  37. /**
  38. * Title of the page
  39. *
  40. * @return string Title of the page
  41. */
  42. public function title()
  43. {
  44. // TRANS: Title for e-mail settings.
  45. return _('Email settings');
  46. }
  47. /**
  48. * Instructions for use
  49. *
  50. * @return instructions for use
  51. */
  52. public function getInstructions()
  53. {
  54. // XXX: For consistency of parameters in messages, this should be a
  55. // regular parameters, replaced with sprintf().
  56. // TRANS: E-mail settings page instructions.
  57. // TRANS: %%site.name%% is the name of the site.
  58. return _('Manage how you get email from %%site.name%%.');
  59. }
  60. public function showScripts()
  61. {
  62. parent::showScripts();
  63. $this->script('emailsettings.js');
  64. $this->autofocus('email');
  65. }
  66. /**
  67. * Content area of the page
  68. *
  69. * Shows a form for adding and removing email addresses and setting
  70. * email preferences.
  71. *
  72. * @return void
  73. */
  74. public function showContent()
  75. {
  76. $user = $this->scoped->getUser();
  77. $this->elementStart('form', array('method' => 'post',
  78. 'id' => 'form_settings_email',
  79. 'class' => 'form_settings',
  80. 'action' =>
  81. common_local_url('emailsettings')));
  82. $this->elementStart('fieldset');
  83. $this->elementStart('fieldset', array('id' => 'settings_email_address'));
  84. // TRANS: Form legend for e-mail settings form.
  85. $this->element('legend', null, _('Email address'));
  86. $this->hidden('token', common_session_token());
  87. if ($user->email) {
  88. $this->element('p', array('id' => 'form_confirmed'), $user->email);
  89. // TRANS: Form note in e-mail settings form.
  90. $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
  91. $this->hidden('email', $user->email);
  92. // TRANS: Button label to remove a confirmed e-mail address.
  93. $this->submit('remove', _m('BUTTON', 'Remove'));
  94. } else {
  95. try {
  96. $confirm = $this->getConfirmation();
  97. $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
  98. $this->element(
  99. 'p',
  100. ['class' => 'form_note'],
  101. // TRANS: Form note in e-mail settings form.
  102. _('Awaiting confirmation on this address. '.
  103. 'Check your inbox (and spam box!) for a message '.
  104. 'with further instructions.')
  105. );
  106. $this->hidden('email', $confirm->address);
  107. // TRANS: Button label to cancel an e-mail address confirmation procedure.
  108. $this->submit('cancel', _m('BUTTON', 'Cancel'));
  109. } catch (NoResultException $e) {
  110. $this->elementStart('ul', 'form_data');
  111. $this->elementStart('li');
  112. // TRANS: Field label for e-mail address input in e-mail settings form.
  113. $this->input(
  114. 'email',
  115. _('Email address'),
  116. $this->trimmed('email') ?: null,
  117. // TRANS: Instructions for e-mail address input form. Do not translate
  118. // TRANS: "example.org". It is one of the domain names reserved for
  119. // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
  120. // TRANS: Any other domain may be owned by a legitimate person or
  121. // TRANS: organization.
  122. _('Email address, like "UserName@example.org"')
  123. );
  124. $this->elementEnd('li');
  125. $this->elementEnd('ul');
  126. // TRANS: Button label for adding an e-mail address in e-mail settings form.
  127. $this->submit('add', _m('BUTTON', 'Add'));
  128. }
  129. }
  130. $this->elementEnd('fieldset');
  131. if (common_config('emailpost', 'enabled') && $user->email) {
  132. $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
  133. // TRANS: Form legend for incoming e-mail settings form.
  134. $this->element('legend', null, _('Incoming email'));
  135. $this->elementStart('ul', 'form_data');
  136. $this->elementStart('li');
  137. $this->checkbox(
  138. 'emailpost',
  139. // TRANS: Checkbox label in e-mail preferences form.
  140. _('I want to post notices by email.'),
  141. $user->emailpost
  142. );
  143. $this->elementEnd('li');
  144. $this->elementEnd('ul');
  145. // Our stylesheets make the form_data list items all floats, which
  146. // creates lots of problems with trying to wrap divs around things.
  147. // This should force a break before the next section, which needs
  148. // to be separate so we can disable the things in it when the
  149. // checkbox is off.
  150. $this->elementStart('div', array('style' => 'clear: both'));
  151. $this->elementEnd('div');
  152. $this->elementStart('div', array('id' => 'emailincoming'));
  153. if ($user->incomingemail) {
  154. $this->elementStart('p');
  155. $this->element('span', 'address', $user->incomingemail);
  156. // @todo XXX: Looks a little awkward in the UI.
  157. // Something like "xxxx@identi.ca Send email ..". Needs improvement.
  158. $this->element(
  159. 'span',
  160. 'input_instructions',
  161. // TRANS: Form instructions for incoming e-mail form in e-mail settings.
  162. _('Send email to this address to post new notices.')
  163. );
  164. $this->elementEnd('p');
  165. // TRANS: Button label for removing a set sender e-mail address to post notices from.
  166. $this->submit('removeincoming', _m('BUTTON', 'Remove'));
  167. }
  168. $this->elementStart('p');
  169. if ($user->incomingemail) {
  170. // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
  171. $msg = _('Make a new email address for posting to; '.
  172. 'cancels the old one.');
  173. } else {
  174. // TRANS: Instructions for incoming e-mail address input form.
  175. $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
  176. }
  177. $this->element('span', 'input_instructions', $msg);
  178. $this->elementEnd('p');
  179. // TRANS: Button label for adding an e-mail address to send notices from.
  180. $this->submit('newincoming', _m('BUTTON', 'New'));
  181. $this->elementEnd('div'); // div#emailincoming
  182. $this->elementEnd('fieldset');
  183. }
  184. $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
  185. // TRANS: Form legend for e-mail preferences form.
  186. $this->element('legend', null, _('Email preferences'));
  187. $this->elementStart('ul', 'form_data');
  188. if (Event::handle('StartEmailFormData', array($this, $this->scoped))) {
  189. $this->elementStart('li');
  190. $this->checkbox(
  191. 'emailnotifysub',
  192. // TRANS: Checkbox label in e-mail preferences form.
  193. _('Send me notices of new subscriptions through email.'),
  194. $user->emailnotifysub
  195. );
  196. $this->elementEnd('li');
  197. $this->elementStart('li');
  198. $this->checkbox(
  199. 'emailnotifymsg',
  200. // TRANS: Checkbox label in e-mail preferences form.
  201. _('Send me email when someone sends me a private message.'),
  202. $user->emailnotifymsg
  203. );
  204. $this->elementEnd('li');
  205. $this->elementStart('li');
  206. $this->checkbox(
  207. 'emailnotifyattn',
  208. // TRANS: Checkbox label in e-mail preferences form.
  209. _('Send me email when someone sends me an "@-reply".'),
  210. $user->emailnotifyattn
  211. );
  212. $this->elementEnd('li');
  213. $this->elementStart('li');
  214. $this->checkbox(
  215. 'emailnotifynudge',
  216. // TRANS: Checkbox label in e-mail preferences form.
  217. _('Allow friends to nudge me and send me an email.'),
  218. $user->emailnotifynudge
  219. );
  220. $this->elementEnd('li');
  221. Event::handle('EndEmailFormData', array($this, $this->scoped));
  222. }
  223. $this->elementEnd('ul');
  224. // TRANS: Button label to save e-mail preferences.
  225. $this->submit('save', _m('BUTTON', 'Save'));
  226. $this->elementEnd('fieldset');
  227. $this->elementEnd('fieldset');
  228. $this->elementEnd('form');
  229. }
  230. /**
  231. * Gets any existing email address confirmations we're waiting for
  232. *
  233. * @return Confirm_address Email address confirmation for user, or null
  234. */
  235. public function getConfirmation()
  236. {
  237. $confirm = new Confirm_address();
  238. $confirm->user_id = $this->scoped->getID();
  239. $confirm->address_type = 'email';
  240. if ($confirm->find(true)) {
  241. return $confirm;
  242. }
  243. throw new NoResultException($confirm);
  244. }
  245. protected function doPost()
  246. {
  247. if ($this->arg('save')) {
  248. return $this->savePreferences();
  249. } elseif ($this->arg('add')) {
  250. return $this->addAddress();
  251. } elseif ($this->arg('cancel')) {
  252. return $this->cancelConfirmation();
  253. } elseif ($this->arg('remove')) {
  254. return $this->removeAddress();
  255. } elseif ($this->arg('removeincoming')) {
  256. return $this->removeIncoming();
  257. } elseif ($this->arg('newincoming')) {
  258. return $this->newIncoming();
  259. }
  260. // TRANS: Message given submitting a form with an unknown action in e-mail settings.
  261. throw new ClientException(_('Unexpected form submission.'));
  262. }
  263. /**
  264. * Save email preferences
  265. *
  266. * @return void
  267. */
  268. public function savePreferences()
  269. {
  270. if (Event::handle('StartEmailSaveForm', array($this, $this->scoped))) {
  271. $emailnotifysub = $this->boolean('emailnotifysub');
  272. $emailnotifymsg = $this->boolean('emailnotifymsg');
  273. $emailnotifynudge = $this->boolean('emailnotifynudge');
  274. $emailnotifyattn = $this->boolean('emailnotifyattn');
  275. $emailpost = $this->boolean('emailpost');
  276. $user = $this->scoped->getUser();
  277. $user->query('BEGIN');
  278. $original = clone($user);
  279. $user->emailnotifysub = $emailnotifysub;
  280. $user->emailnotifymsg = $emailnotifymsg;
  281. $user->emailnotifynudge = $emailnotifynudge;
  282. $user->emailnotifyattn = $emailnotifyattn;
  283. $user->emailpost = $emailpost;
  284. $result = $user->update($original);
  285. if ($result === false) {
  286. common_log_db_error($user, 'UPDATE', __FILE__);
  287. $user->query('ROLLBACK');
  288. // TRANS: Server error thrown on database error updating e-mail preferences.
  289. throw new ServerException(_('Could not update user.'));
  290. }
  291. $user->query('COMMIT');
  292. Event::handle('EndEmailSaveForm', array($this, $this->scoped));
  293. }
  294. // TRANS: Confirmation message for successful e-mail preferences save.
  295. return _('Email preferences saved.');
  296. }
  297. /**
  298. * Add the address passed in by the user
  299. *
  300. * @return void
  301. */
  302. public function addAddress()
  303. {
  304. $user = $this->scoped->getUser();
  305. $email = $this->trimmed('email');
  306. // Some validation
  307. if (empty($email)) {
  308. // TRANS: Message given saving e-mail address without having provided one.
  309. throw new ClientException(_('No email address.'));
  310. }
  311. $email = common_canonical_email($email);
  312. if (empty($email)) {
  313. // TRANS: Message given saving e-mail address that cannot be normalised.
  314. throw new ClientException(_('Cannot normalize that email address.'));
  315. }
  316. if (!Validate::email($email, common_config('email', 'check_domain'))) {
  317. // TRANS: Message given saving e-mail address that not valid.
  318. throw new ClientException(_('Not a valid email address.'));
  319. } elseif ($user->email === $email) {
  320. // TRANS: Message given saving e-mail address that is already set.
  321. throw new ClientException(_('That is already your email address.'));
  322. } elseif ($this->emailExists($email)) {
  323. // TRANS: Message given saving e-mail address that is already set for another user.
  324. throw new ClientException(_('That email address already belongs to another user.'));
  325. }
  326. if (Event::handle('StartAddEmailAddress', array($user, $email))) {
  327. $confirm = new Confirm_address();
  328. $confirm->address = $email;
  329. $confirm->address_type = 'email';
  330. $confirm->user_id = $user->getID();
  331. $confirm->code = common_confirmation_code(64);
  332. $result = $confirm->insert();
  333. if ($result === false) {
  334. common_log_db_error($confirm, 'INSERT', __FILE__);
  335. // TRANS: Server error thrown on database error adding e-mail confirmation code.
  336. throw new ServerException(_('Could not insert confirmation code.'));
  337. }
  338. $confirm->sendConfirmation();
  339. Event::handle('EndAddEmailAddress', array($user, $email));
  340. }
  341. // TRANS: Message given saving valid e-mail address that is to be confirmed.
  342. return _('A confirmation code was sent to the email address you added. '.
  343. 'Check your inbox (and spam box!) for the code and instructions '.
  344. 'on how to use it.');
  345. }
  346. /**
  347. * Handle a request to cancel email confirmation
  348. *
  349. * @return void
  350. */
  351. public function cancelConfirmation()
  352. {
  353. $email = $this->trimmed('email');
  354. try {
  355. $confirm = $this->getConfirmation();
  356. if ($confirm->address !== $email) {
  357. // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
  358. throw new ClientException(_('That is the wrong email address.'));
  359. }
  360. } catch (NoResultException $e) {
  361. // TRANS: Message given canceling e-mail address confirmation that is not pending.
  362. throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
  363. }
  364. $confirm->delete();
  365. // TRANS: Message given after successfully canceling e-mail address confirmation.
  366. return _('Email confirmation cancelled.');
  367. }
  368. /**
  369. * Handle a request to remove an address from the user's account
  370. *
  371. * @return void
  372. */
  373. public function removeAddress()
  374. {
  375. $user = common_current_user();
  376. $email = $this->trimmed('email');
  377. // Maybe an old tab open...?
  378. if ($user->email !== $email) {
  379. // TRANS: Message given trying to remove an e-mail address that is not
  380. // TRANS: registered for the active user.
  381. throw new ClientException(_('That is not your email address.'));
  382. }
  383. $original = clone($user);
  384. $user->email = DB_DataObject_Cast::sql('NULL');
  385. // Throws exception on failure. Also performs it within a transaction.
  386. $user->updateWithKeys($original);
  387. // TRANS: Message given after successfully removing a registered e-mail address.
  388. return _('The email address was removed.');
  389. }
  390. /**
  391. * Handle a request to remove an incoming email address
  392. *
  393. * @return void
  394. */
  395. public function removeIncoming()
  396. {
  397. $user = common_current_user();
  398. if (empty($user->incomingemail)) {
  399. // TRANS: Form validation error displayed when trying to remove an incoming e-mail address while no address has been set.
  400. throw new AlreadyFulfilledException(_('No incoming email address.'));
  401. }
  402. $orig = clone($user);
  403. $user->incomingemail = DB_DataObject_Cast::sql('NULL');
  404. $user->emailpost = false;
  405. // Throws exception on failure. Also performs it within a transaction.
  406. $user->updateWithKeys($orig);
  407. // TRANS: Message given after successfully removing an incoming e-mail address.
  408. return _('Incoming email address removed.');
  409. }
  410. /**
  411. * Generate a new incoming email address
  412. *
  413. * @return void
  414. */
  415. public function newIncoming()
  416. {
  417. $user = common_current_user();
  418. $orig = clone($user);
  419. $user->incomingemail = mail_new_incoming_address();
  420. $user->emailpost = true;
  421. // Throws exception on failure. Also performs it within a transaction.
  422. $user->updateWithKeys($orig);
  423. // TRANS: Message given after successfully adding an incoming e-mail address.
  424. return _('New incoming email address added.');
  425. }
  426. /**
  427. * Does another user already have this email address?
  428. *
  429. * Email addresses are unique for users.
  430. *
  431. * @param string $email Address to check
  432. *
  433. * @return boolean Whether the email already exists.
  434. */
  435. public function emailExists($email)
  436. {
  437. $user = common_current_user();
  438. $other = User::getKV('email', $email);
  439. if (!$other instanceof User) {
  440. return false;
  441. }
  442. return $other->id != $user->id;
  443. }
  444. }