smssettings.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Settings for SMS
  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 SMS
  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 SmssettingsAction 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 SMS settings.
  53. return _('SMS settings');
  54. }
  55. /**
  56. * Instructions for use
  57. *
  58. * @return instructions for use
  59. */
  60. function getInstructions()
  61. {
  62. // XXX: For consistency of parameters in messages, this should be a
  63. // regular parameters, replaced with sprintf().
  64. // TRANS: SMS settings page instructions.
  65. // TRANS: %%site.name%% is the name of the site.
  66. return _('You can receive SMS messages through email from %%site.name%%.');
  67. }
  68. function showScripts()
  69. {
  70. parent::showScripts();
  71. $this->autofocus('sms');
  72. }
  73. /**
  74. * Content area of the page
  75. *
  76. * Shows a form for adding and removing SMS phone numbers and setting
  77. * SMS preferences.
  78. *
  79. * @return void
  80. */
  81. function showContent()
  82. {
  83. if (!common_config('sms', 'enabled')) {
  84. $this->element('div', array('class' => 'error'),
  85. // TRANS: Message given in the SMS settings if SMS is not enabled on the site.
  86. _('SMS is not available.'));
  87. return;
  88. }
  89. $user = common_current_user();
  90. $this->elementStart('form', array('method' => 'post',
  91. 'id' => 'form_settings_sms',
  92. 'class' => 'form_settings',
  93. 'action' =>
  94. common_local_url('smssettings')));
  95. $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
  96. // TRANS: Form legend for SMS settings form.
  97. $this->element('legend', null, _('SMS address'));
  98. $this->hidden('token', common_session_token());
  99. if ($user->sms) {
  100. $carrier = $user->getCarrier();
  101. $this->element('p', 'form_confirmed',
  102. $user->sms . ' (' . $carrier->name . ')');
  103. $this->element('p', 'form_guide',
  104. // TRANS: Form guide in SMS settings form.
  105. _('Current confirmed SMS-enabled phone number.'));
  106. $this->hidden('sms', $user->sms);
  107. $this->hidden('carrier', $user->carrier);
  108. // TRANS: Button label to remove a confirmed SMS address.
  109. $this->submit('remove', _m('BUTTON','Remove'));
  110. } else {
  111. $confirm = $this->getConfirmation();
  112. if ($confirm) {
  113. $carrier = Sms_carrier::getKV($confirm->address_extra);
  114. $this->element('p', 'form_unconfirmed',
  115. $confirm->address . ' (' . $carrier->name . ')');
  116. $this->element('p', 'form_guide',
  117. // TRANS: Form guide in IM settings form.
  118. _('Awaiting confirmation on this phone number.'));
  119. $this->hidden('sms', $confirm->address);
  120. $this->hidden('carrier', $confirm->address_extra);
  121. // TRANS: Button label to cancel a SMS address confirmation procedure.
  122. $this->submit('cancel', _m('BUTTON','Cancel'));
  123. $this->elementStart('ul', 'form_data');
  124. $this->elementStart('li');
  125. // TRANS: Field label for SMS address input in SMS settings form.
  126. $this->input('code', _('Confirmation code'), null,
  127. // TRANS: Form field instructions in SMS settings form.
  128. _('Enter the code you received on your phone.'));
  129. $this->elementEnd('li');
  130. $this->elementEnd('ul');
  131. // TRANS: Button label to confirm SMS confirmation code in SMS settings.
  132. $this->submit('confirm', _m('BUTTON','Confirm'));
  133. } else {
  134. $this->elementStart('ul', 'form_data');
  135. $this->elementStart('li');
  136. // TRANS: Field label for SMS phone number input in SMS settings form.
  137. $this->input('sms', _('SMS phone number'),
  138. ($this->arg('sms')) ? $this->arg('sms') : null,
  139. // TRANS: SMS phone number input field instructions in SMS settings form.
  140. _('Phone number, no punctuation or spaces, '.
  141. 'with area code.'));
  142. $this->elementEnd('li');
  143. $this->elementEnd('ul');
  144. $this->carrierSelect();
  145. // TRANS: Button label for adding a SMS phone number in SMS settings form.
  146. $this->submit('add', _m('BUTTON','Add'));
  147. }
  148. }
  149. $this->elementEnd('fieldset');
  150. if ($user->sms) {
  151. $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
  152. // XXX: Confused! This is about SMS. Should this message be updated?
  153. // TRANS: Form legend for incoming SMS settings form.
  154. $this->element('legend', null, _('Incoming email'));
  155. if ($user->incomingemail) {
  156. $this->element('p', 'form_unconfirmed', $user->incomingemail);
  157. $this->element('p', 'form_note',
  158. // XXX: Confused! This is about SMS. Should this message be updated?
  159. // TRANS: Form instructions for incoming SMS e-mail address form in SMS settings.
  160. _('Send email to this address to post new notices.'));
  161. // TRANS: Button label for removing a set sender SMS e-mail address to post notices from.
  162. $this->submit('removeincoming', _m('BUTTON','Remove'));
  163. }
  164. $this->element('p', 'form_guide',
  165. // XXX: Confused! This is about SMS. Should this message be updated?
  166. // TRANS: Instructions for incoming SMS e-mail address input form.
  167. _('Make a new email address for posting to; '.
  168. 'cancels the old one.'));
  169. // TRANS: Button label for adding an SMS e-mail address to send notices from.
  170. $this->submit('newincoming', _m('BUTTON','New'));
  171. $this->elementEnd('fieldset');
  172. }
  173. $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
  174. // TRANS: Form legend for SMS preferences form.
  175. $this->element('legend', null, _('SMS preferences'));
  176. $this->elementStart('ul', 'form_data');
  177. $this->elementStart('li');
  178. $this->checkbox('smsnotify',
  179. // TRANS: Checkbox label in SMS preferences form.
  180. _('Send me notices through SMS; '.
  181. 'I understand I may incur '.
  182. 'exorbitant charges from my carrier.'),
  183. $user->smsnotify);
  184. $this->elementEnd('li');
  185. $this->elementEnd('ul');
  186. // TRANS: Button label to save SMS preferences.
  187. $this->submit('save', _m('BUTTON','Save'));
  188. $this->elementEnd('fieldset');
  189. $this->elementEnd('form');
  190. }
  191. /**
  192. * Get a pending confirmation, if any, for this user
  193. *
  194. * @return void
  195. *
  196. * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
  197. */
  198. function getConfirmation()
  199. {
  200. $user = common_current_user();
  201. $confirm = new Confirm_address();
  202. $confirm->user_id = $user->id;
  203. $confirm->address_type = 'sms';
  204. if ($confirm->find(true)) {
  205. return $confirm;
  206. } else {
  207. return null;
  208. }
  209. }
  210. /**
  211. * Handle posts to this form
  212. *
  213. * Based on the button that was pressed, muxes out to other functions
  214. * to do the actual task requested.
  215. *
  216. * All sub-functions reload the form with a message -- success or failure.
  217. *
  218. * @return void
  219. */
  220. function handlePost()
  221. {
  222. // CSRF protection
  223. $token = $this->trimmed('token');
  224. if (!$token || $token != common_session_token()) {
  225. // TRANS: Client error displayed when the session token does not match or is not given.
  226. $this->showForm(_('There was a problem with your session token. '.
  227. 'Try again, please.'));
  228. return;
  229. }
  230. if ($this->arg('save')) {
  231. $this->savePreferences();
  232. } else if ($this->arg('add')) {
  233. $this->addAddress();
  234. } else if ($this->arg('cancel')) {
  235. $this->cancelConfirmation();
  236. } else if ($this->arg('remove')) {
  237. $this->removeAddress();
  238. } else if ($this->arg('removeincoming')) {
  239. $this->removeIncoming();
  240. } else if ($this->arg('newincoming')) {
  241. $this->newIncoming();
  242. } else if ($this->arg('confirm')) {
  243. $this->confirmCode();
  244. } else {
  245. // TRANS: Message given submitting a form with an unknown action in SMS settings.
  246. $this->showForm(_('Unexpected form submission.'));
  247. }
  248. }
  249. /**
  250. * Handle a request to save preferences
  251. *
  252. * Sets the user's SMS preferences in the DB.
  253. *
  254. * @return void
  255. */
  256. function savePreferences()
  257. {
  258. $smsnotify = $this->boolean('smsnotify');
  259. $user = common_current_user();
  260. assert(!is_null($user)); // should already be checked
  261. $user->query('BEGIN');
  262. $original = clone($user);
  263. $user->smsnotify = $smsnotify;
  264. $result = $user->update($original);
  265. if ($result === false) {
  266. common_log_db_error($user, 'UPDATE', __FILE__);
  267. // TRANS: Server error thrown on database error updating SMS preferences.
  268. $this->serverError(_('Could not update user.'));
  269. }
  270. $user->query('COMMIT');
  271. // TRANS: Confirmation message for successful SMS preferences save.
  272. $this->showForm(_('SMS preferences saved.'), true);
  273. }
  274. /**
  275. * Add a new SMS number for confirmation
  276. *
  277. * When the user requests a new SMS number, sends a confirmation
  278. * message.
  279. *
  280. * @return void
  281. */
  282. function addAddress()
  283. {
  284. $user = common_current_user();
  285. $sms = $this->trimmed('sms');
  286. $carrier_id = $this->trimmed('carrier');
  287. // Some validation
  288. if (!$sms) {
  289. // TRANS: Message given saving SMS phone number without having provided one.
  290. $this->showForm(_('No phone number.'));
  291. return;
  292. }
  293. if (!$carrier_id) {
  294. // TRANS: Message given saving SMS phone number without having selected a carrier.
  295. $this->showForm(_('No carrier selected.'));
  296. return;
  297. }
  298. $sms = common_canonical_sms($sms);
  299. if ($user->sms == $sms) {
  300. // TRANS: Message given saving SMS phone number that is already set.
  301. $this->showForm(_('That is already your phone number.'));
  302. return;
  303. } else if ($this->smsExists($sms)) {
  304. // TRANS: Message given saving SMS phone number that is already set for another user.
  305. $this->showForm(_('That phone number already belongs to another user.'));
  306. return;
  307. }
  308. $confirm = new Confirm_address();
  309. $confirm->address = $sms;
  310. $confirm->address_extra = $carrier_id;
  311. $confirm->address_type = 'sms';
  312. $confirm->user_id = $user->id;
  313. $confirm->code = common_confirmation_code(40);
  314. $result = $confirm->insert();
  315. if ($result === false) {
  316. common_log_db_error($confirm, 'INSERT', __FILE__);
  317. // TRANS: Server error thrown on database error adding SMS confirmation code.
  318. $this->serverError(_('Could not insert confirmation code.'));
  319. }
  320. $carrier = Sms_carrier::getKV($carrier_id);
  321. mail_confirm_sms($confirm->code,
  322. $user->nickname,
  323. $carrier->toEmailAddress($sms));
  324. // TRANS: Message given saving valid SMS phone number that is to be confirmed.
  325. $msg = _('A confirmation code was sent to the phone number you added. '.
  326. 'Check your phone for the code and instructions '.
  327. 'on how to use it.');
  328. $this->showForm($msg, true);
  329. }
  330. /**
  331. * Cancel a pending confirmation
  332. *
  333. * Cancels the confirmation.
  334. *
  335. * @return void
  336. */
  337. function cancelConfirmation()
  338. {
  339. $sms = $this->trimmed('sms');
  340. $carrier = $this->trimmed('carrier');
  341. $confirm = $this->getConfirmation();
  342. if (!$confirm) {
  343. // TRANS: Message given canceling SMS phone number confirmation that is not pending.
  344. $this->showForm(_('No pending confirmation to cancel.'));
  345. return;
  346. }
  347. if ($confirm->address != $sms) {
  348. // TRANS: Message given canceling SMS phone number confirmation for the wrong phone number.
  349. $this->showForm(_('That is the wrong confirmation number.'));
  350. return;
  351. }
  352. $result = $confirm->delete();
  353. if (!$result) {
  354. common_log_db_error($confirm, 'DELETE', __FILE__);
  355. // TRANS: Server error thrown on database error canceling SMS phone number confirmation.
  356. $this->serverError(_('Could not delete SMS confirmation.'));
  357. }
  358. // TRANS: Message given after successfully canceling SMS phone number confirmation.
  359. $this->showForm(_('SMS confirmation cancelled.'), true);
  360. }
  361. /**
  362. * Remove a phone number from the user's account
  363. *
  364. * @return void
  365. */
  366. function removeAddress()
  367. {
  368. $user = common_current_user();
  369. $sms = $this->arg('sms');
  370. $carrier = $this->arg('carrier');
  371. // Maybe an old tab open...?
  372. if ($user->sms != $sms) {
  373. // TRANS: Message given trying to remove an SMS phone number that is not
  374. // TRANS: registered for the active user.
  375. $this->showForm(_('That is not your phone number.'));
  376. return;
  377. }
  378. $original = clone($user);
  379. $user->sms = null;
  380. $user->carrier = null;
  381. $user->smsemail = null;
  382. // Throws exception on failure. Also performs it within a transaction.
  383. $user->updateWithKeys($original);
  384. // TRANS: Message given after successfully removing a registered SMS phone number.
  385. $this->showForm(_('The SMS phone number was removed.'), true);
  386. }
  387. /**
  388. * Does this sms number exist in our database?
  389. *
  390. * Also checks if it belongs to someone else
  391. *
  392. * @param string $sms phone number to check
  393. *
  394. * @return boolean does the number exist
  395. */
  396. function smsExists($sms)
  397. {
  398. $user = common_current_user();
  399. $other = User::getKV('sms', $sms);
  400. if (!$other) {
  401. return false;
  402. } else {
  403. return $other->id != $user->id;
  404. }
  405. }
  406. /**
  407. * Show a drop-down box with all the SMS carriers in the DB
  408. *
  409. * @return void
  410. */
  411. function carrierSelect()
  412. {
  413. $carrier = new Sms_carrier();
  414. $cnt = $carrier->find();
  415. $this->elementStart('ul', 'form_data');
  416. $this->elementStart('li');
  417. // TRANS: Label for mobile carrier dropdown menu in SMS settings.
  418. $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
  419. $this->elementStart('select', array('name' => 'carrier',
  420. 'id' => 'carrier'));
  421. $this->element('option', array('value' => 0),
  422. // TRANS: Default option for mobile carrier dropdown menu in SMS settings.
  423. _('Select a carrier'));
  424. while ($carrier->fetch()) {
  425. $this->element('option', array('value' => $carrier->id),
  426. $carrier->name);
  427. }
  428. $this->elementEnd('select');
  429. $this->element('p', 'form_guide',
  430. // TRANS: Form instructions for mobile carrier dropdown menu in SMS settings.
  431. // TRANS: %s is an administrative contact's e-mail address.
  432. sprintf(_('Mobile carrier for your phone. '.
  433. 'If you know a carrier that accepts ' .
  434. 'SMS over email but isn\'t listed here, ' .
  435. 'send email to let us know at %s.'),
  436. common_config('site', 'email')));
  437. $this->elementEnd('li');
  438. $this->elementEnd('ul');
  439. }
  440. /**
  441. * Confirm an SMS confirmation code
  442. *
  443. * Redirects to the confirmaddress page for this code
  444. *
  445. * @return void
  446. */
  447. function confirmCode()
  448. {
  449. $code = $this->trimmed('code');
  450. if (!$code) {
  451. // TRANS: Message given saving SMS phone number confirmation code without having provided one.
  452. $this->showForm(_('No code entered.'));
  453. return;
  454. }
  455. common_redirect(common_local_url('confirmaddress',
  456. array('code' => $code)),
  457. 303);
  458. }
  459. /**
  460. * Handle a request to remove an incoming email address
  461. *
  462. * @return void
  463. */
  464. function removeIncoming()
  465. {
  466. $user = common_current_user();
  467. if (!$user->incomingemail) {
  468. // TRANS: Form validation error displayed when trying to remove an incoming e-mail address while no address has been set.
  469. $this->showForm(_('No incoming email address.'));
  470. return;
  471. }
  472. $orig = clone($user);
  473. $user->incomingemail = null;
  474. // Throws exception on failure. Also performs it within a transaction.
  475. $user->updateWithKeys($orig);
  476. // TRANS: Confirmation text after updating SMS settings.
  477. $this->showForm(_('Incoming email address removed.'), true);
  478. }
  479. /**
  480. * Generate a new incoming email address
  481. *
  482. * @return void
  483. *
  484. * @see Emailsettings::newIncoming
  485. */
  486. function newIncoming()
  487. {
  488. $user = common_current_user();
  489. $orig = clone($user);
  490. $user->incomingemail = mail_new_incoming_address();
  491. // Throws exception on failure. Also performs it within a transaction.
  492. $user->updateWithKeys($orig);
  493. // TRANS: Confirmation text after updating SMS settings.
  494. $this->showForm(_('New incoming email address added.'), true);
  495. }
  496. }