smssettings.php 19 KB

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