confirmaddress.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * Confirm an address
  18. *
  19. * @category Confirm
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2008-2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Confirm an address
  28. *
  29. * When users change their SMS, email, Jabber, or other addresses, we send out
  30. * a confirmation code to make sure the owner of that address approves. This class
  31. * accepts those codes.
  32. *
  33. * @category Confirm
  34. * @package GNUsocial
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class ConfirmaddressAction extends ManagedAction
  39. {
  40. /** type of confirmation. */
  41. protected $address;
  42. protected function doPreparation()
  43. {
  44. if (!common_logged_in()) {
  45. common_set_returnto($this->selfUrl());
  46. common_redirect(common_local_url('login'));
  47. }
  48. $code = $this->trimmed('code');
  49. if (!$code) {
  50. // TRANS: Client error displayed when not providing a confirmation code in the contact address confirmation action.
  51. throw new ClientException(_('No confirmation code.'));
  52. }
  53. $confirm = Confirm_address::getKV('code', $code);
  54. if (!$confirm instanceof Confirm_address) {
  55. // TRANS: Client error displayed when providing a non-existing confirmation code in the contact address confirmation action.
  56. throw new ClientException(_('Confirmation code not found.'), 404);
  57. }
  58. try {
  59. $profile = Profile::getByID($confirm->user_id);
  60. } catch (NoResultException $e) {
  61. common_log(LOG_INFO, 'Tried to confirm the email for a deleted profile: '._ve(['id'=>$confirm->user_id, 'email'=>$confirm->address]));
  62. $confirm->delete();
  63. throw $e;
  64. }
  65. if (!$profile->sameAs($this->scoped)) {
  66. // TRANS: Client error displayed when not providing a confirmation code for another user in the contact address confirmation action.
  67. throw new AuthorizationException(_('That confirmation code is not for you!'));
  68. }
  69. $type = $confirm->address_type;
  70. $transports = array();
  71. Event::handle('GetImTransports', array(&$transports));
  72. if (!in_array($type, array('email', 'sms')) && !in_array($type, array_keys($transports))) {
  73. // TRANS: Server error for an unknown address type, which can be 'email', 'sms', or the name of an IM network (such as 'xmpp' or 'aim')
  74. throw new ServerException(sprintf(_('Unrecognized address type %s'), $type));
  75. }
  76. $this->address = $confirm->address;
  77. $cur = $this->scoped->getUser();
  78. $cur->query('START TRANSACTION');
  79. if (in_array($type, array('email', 'sms'))) {
  80. common_debug("Confirming {$type} address for user {$this->scoped->getID()}");
  81. if ($cur->$type == $confirm->address) {
  82. // Already verified, so delete the confirm_address entry
  83. $confirm->delete();
  84. // TRANS: Client error for an already confirmed email/jabber/sms address.
  85. throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
  86. }
  87. $orig_user = clone($cur);
  88. $cur->$type = $confirm->address;
  89. if ($type == 'sms') {
  90. $cur->carrier = ($confirm->address_extra)+0;
  91. $carrier = Sms_carrier::getKV($cur->carrier);
  92. $cur->smsemail = $carrier->toEmailAddress($cur->sms);
  93. }
  94. // Throws exception on failure.
  95. $cur->updateWithKeys($orig_user);
  96. if ($type == 'email') {
  97. $cur->emailChanged();
  98. }
  99. } else {
  100. $user_im_prefs = new User_im_prefs();
  101. $user_im_prefs->transport = $confirm->address_type;
  102. $user_im_prefs->user_id = $cur->id;
  103. if ($user_im_prefs->find() && $user_im_prefs->fetch()) {
  104. if ($user_im_prefs->screenname === $confirm->address) {
  105. // Already verified, so delete the confirm_address entry
  106. $confirm->delete();
  107. // TRANS: Client error for an already confirmed IM address.
  108. throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
  109. }
  110. $user_im_prefs->screenname = $confirm->address;
  111. $result = $user_im_prefs->update();
  112. if ($result === false) {
  113. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  114. // TRANS: Server error displayed when updating IM preferences fails.
  115. throw new ServerException(_('Could not update user IM preferences.'));
  116. }
  117. } else {
  118. $user_im_prefs = new User_im_prefs();
  119. $user_im_prefs->screenname = $confirm->address;
  120. $user_im_prefs->transport = $confirm->address_type;
  121. $user_im_prefs->user_id = $cur->id;
  122. $user_im_prefs->created = common_sql_now();
  123. $result = $user_im_prefs->insert();
  124. if ($result === false) {
  125. common_log_db_error($user_im_prefs, 'INSERT', __FILE__);
  126. // TRANS: Server error displayed when adding IM preferences fails.
  127. throw new ServerException(_('Could not insert user IM preferences.'));
  128. }
  129. }
  130. }
  131. $confirm->delete();
  132. $cur->query('COMMIT');
  133. }
  134. /**
  135. * Title of the page
  136. *
  137. * @return string title
  138. */
  139. public function title()
  140. {
  141. // TRANS: Title for the contact address confirmation action.
  142. return _('Confirm address');
  143. }
  144. /**
  145. * Show a confirmation message.
  146. *
  147. * @return void
  148. */
  149. public function showContent()
  150. {
  151. $this->element(
  152. 'p',
  153. null,
  154. // TRANS: Success message for the contact address confirmation action.
  155. // TRANS: %s can be 'email', 'jabber', or 'sms'.
  156. sprintf(
  157. _('The address "%s" has been confirmed for your account.'),
  158. $this->address
  159. )
  160. );
  161. }
  162. }