confirmaddress.php 7.1 KB

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