123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class SmssettingsAction extends SettingsAction
- {
- protected function doPreparation()
- {
- if (!common_config('sms', 'enabled')) {
-
- throw new ServerException(_('SMS is not available.'));
- }
- }
-
- function title()
- {
-
- return _('SMS settings');
- }
-
- function getInstructions()
- {
-
-
-
-
- return _('You can receive SMS messages through email from %%site.name%%.');
- }
- function showScripts()
- {
- parent::showScripts();
- $this->autofocus('sms');
- }
-
- function showContent()
- {
- $user = $this->scoped->getUser();
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_settings_sms',
- 'class' => 'form_settings',
- 'action' =>
- common_local_url('smssettings')));
- $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
-
- $this->element('legend', null, _('SMS address'));
- $this->hidden('token', common_session_token());
- if ($user->sms) {
- $carrier = $user->getCarrier();
- $this->element('p', 'form_confirmed',
- $user->sms . ' (' . $carrier->name . ')');
- $this->element('p', 'form_guide',
-
- _('Current confirmed SMS-enabled phone number.'));
- $this->hidden('sms', $user->sms);
- $this->hidden('carrier', $user->carrier);
-
- $this->submit('remove', _m('BUTTON','Remove'));
- } else {
- try {
- $confirm = $this->getConfirmation();
- $carrier = Sms_carrier::getKV($confirm->address_extra);
- $this->element('p', 'form_unconfirmed',
- $confirm->address . ' (' . $carrier->name . ')');
- $this->element('p', 'form_guide',
-
- _('Awaiting confirmation on this phone number.'));
- $this->hidden('sms', $confirm->address);
- $this->hidden('carrier', $confirm->address_extra);
-
- $this->submit('cancel', _m('BUTTON','Cancel'));
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
-
- $this->input('code', _('Confirmation code'), null,
-
- _('Enter the code you received on your phone.'));
- $this->elementEnd('li');
- $this->elementEnd('ul');
-
- $this->submit('confirm', _m('BUTTON','Confirm'));
- } catch (NoResultException $e) {
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
-
- $this->input('sms', _('SMS phone number'),
- ($this->arg('sms')) ? $this->arg('sms') : null,
-
- _('Phone number, no punctuation or spaces, '.
- 'with area code.'));
- $this->elementEnd('li');
- $this->elementEnd('ul');
- $this->carrierSelect();
-
- $this->submit('add', _m('BUTTON','Add'));
- }
- }
- $this->elementEnd('fieldset');
- if ($user->sms) {
- $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
-
-
- $this->element('legend', null, _('Incoming email'));
- if ($user->incomingemail) {
- $this->element('p', 'form_unconfirmed', $user->incomingemail);
- $this->element('p', 'form_note',
-
-
- _('Send email to this address to post new notices.'));
-
- $this->submit('removeincoming', _m('BUTTON','Remove'));
- }
- $this->element('p', 'form_guide',
-
-
- _('Make a new email address for posting to; '.
- 'cancels the old one.'));
-
- $this->submit('newincoming', _m('BUTTON','New'));
- $this->elementEnd('fieldset');
- }
- $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
-
- $this->element('legend', null, _('SMS preferences'));
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
- $this->checkbox('smsnotify',
-
- _('Send me notices through SMS; '.
- 'I understand I may incur '.
- 'exorbitant charges from my carrier.'),
- $user->smsnotify);
- $this->elementEnd('li');
- $this->elementEnd('ul');
-
- $this->submit('save', _m('BUTTON','Save'));
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- }
-
- function getConfirmation()
- {
- $confirm = new Confirm_address();
- $confirm->user_id = $this->scoped->getID();
- $confirm->address_type = 'sms';
- if ($confirm->find(true)) {
- return $confirm;
- }
- throw new NoResultException($confirm);
- }
- protected function doPost()
- {
- if ($this->arg('save')) {
- return $this->savePreferences();
- } else if ($this->arg('add')) {
- return $this->addAddress();
- } else if ($this->arg('cancel')) {
- return $this->cancelConfirmation();
- } else if ($this->arg('remove')) {
- return $this->removeAddress();
- } else if ($this->arg('removeincoming')) {
- return $this->removeIncoming();
- } else if ($this->arg('newincoming')) {
- return $this->newIncoming();
- } else if ($this->arg('confirm')) {
- return $this->confirmCode();
- }
-
- throw new ClientException(_('Unexpected form submission.'));
- }
-
- function savePreferences()
- {
- $user = $this->scoped->getUser();
- $user->query('BEGIN');
- $original = clone($user);
- $user->smsnotify = $this->boolean('smsnotify');
- $result = $user->update($original);
- if ($result === false) {
- common_log_db_error($user, 'UPDATE', __FILE__);
-
- throw new ServerException(_('Could not update user.'));
- }
- $user->query('COMMIT');
-
- return _('SMS preferences saved.');
- }
-
- function addAddress()
- {
- $user = common_current_user();
- $sms = $this->trimmed('sms');
- $carrier_id = $this->trimmed('carrier');
-
- if (empty($sms)) {
-
- throw new ClientException(_('No phone number.'));
- }
- if (empty($carrier_id)) {
-
- throw new ClientException(_('No carrier selected.'));
- }
- $sms = common_canonical_sms($sms);
- if ($user->sms === $sms) {
-
- throw new AlreadyFulfilledException(_('That is already your phone number.'));
- } else if ($this->smsExists($sms)) {
-
- throw new ClientException(_('That phone number already belongs to another user.'));
- }
- $confirm = new Confirm_address();
- $confirm->address = $sms;
- $confirm->address_extra = $carrier_id;
- $confirm->address_type = 'sms';
- $confirm->user_id = $this->scoped->getID();
- $confirm->code = common_confirmation_code(40);
- $result = $confirm->insert();
- if ($result === false) {
- common_log_db_error($confirm, 'INSERT', __FILE__);
-
- $this->serverError(_('Could not insert confirmation code.'));
- }
- $carrier = Sms_carrier::getKV($carrier_id);
- mail_confirm_sms($confirm->code,
- $user->nickname,
- $carrier->toEmailAddress($sms));
-
- return _('A confirmation code was sent to the phone number you added. '.
- 'Check your phone for the code and instructions '.
- 'on how to use it.');
- }
-
- function cancelConfirmation()
- {
- $sms = $this->trimmed('sms');
- $carrier = $this->trimmed('carrier');
- try {
- $confirm = $this->getConfirmation();
- if ($confirm->address != $sms) {
-
- throw new ClientException(_('That is the wrong confirmation number.'));
- }
- } catch (NoResultException $e) {
-
- throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
- }
- $confirm->delete();
-
- return _('SMS confirmation cancelled.');
- }
-
- function removeAddress()
- {
- $user = $this->scoped->getUser();
- $sms = $this->arg('sms');
- $carrier = $this->arg('carrier');
-
- if ($user->sms != $sms) {
-
-
- throw new ClientException(_('That is not your phone number.'));
- }
- $original = clone($user);
- $user->sms = null;
- $user->carrier = null;
- $user->smsemail = null;
-
- $user->updateWithKeys($original);
-
- return _('The SMS phone number was removed.');
- }
-
- function smsExists($sms)
- {
- $other = User::getKV('sms', $sms);
- if (!$other instanceof User) {
- return false;
- }
- return !$this->scoped->sameAs($other->getProfile());
- }
-
- function carrierSelect()
- {
- $carrier = new Sms_carrier();
- $cnt = $carrier->find();
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
-
- $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
- $this->elementStart('select', array('name' => 'carrier',
- 'id' => 'carrier'));
- $this->element('option', array('value' => 0),
-
- _('Select a carrier'));
- while ($carrier->fetch()) {
- $this->element('option', array('value' => $carrier->id),
- $carrier->name);
- }
- $this->elementEnd('select');
- $this->element('p', 'form_guide',
-
-
- sprintf(_('Mobile carrier for your phone. '.
- 'If you know a carrier that accepts ' .
- 'SMS over email but isn\'t listed here, ' .
- 'send email to let us know at %s.'),
- common_config('site', 'email')));
- $this->elementEnd('li');
- $this->elementEnd('ul');
- }
-
- function confirmCode()
- {
- $code = $this->trimmed('code');
- if (empty($code)) {
-
- throw new ClientException(_('No code entered.'));
- }
- common_redirect(common_local_url('confirmaddress', array('code' => $code)), 303);
- }
-
- function removeIncoming()
- {
- $user = common_current_user();
- if (!$user->incomingemail) {
-
- throw new ClientException(_('No incoming email address.'));
- }
- $orig = clone($user);
- $user->incomingemail = null;
-
- $user->updateWithKeys($orig);
-
- return _('Incoming email address removed.');
- }
-
- function newIncoming()
- {
- $user = $this->scoped->getUser();
- $orig = clone($user);
- $user->incomingemail = mail_new_incoming_address();
-
- $user->updateWithKeys($orig);
-
- return _('New incoming email address added.');
- }
- }
|