emailsettings.php 21 KB

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