confirmaddress.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Confirm an address
  34. *
  35. * When users change their SMS, email, Jabber, or other addresses, we send out
  36. * a confirmation code to make sure the owner of that address approves. This class
  37. * accepts those codes.
  38. *
  39. * @category Confirm
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class ConfirmaddressAction extends Action
  46. {
  47. /** type of confirmation. */
  48. var $address;
  49. /**
  50. * Accept a confirmation code
  51. *
  52. * Checks the code and confirms the address in the
  53. * user record
  54. *
  55. * @param args $args $_REQUEST array
  56. *
  57. * @return void
  58. */
  59. function handle($args)
  60. {
  61. parent::handle($args);
  62. if (!common_logged_in()) {
  63. common_set_returnto($this->selfUrl());
  64. common_redirect(common_local_url('login'));
  65. }
  66. $code = $this->trimmed('code');
  67. if (!$code) {
  68. // TRANS: Client error displayed when not providing a confirmation code in the contact address confirmation action.
  69. $this->clientError(_('No confirmation code.'));
  70. }
  71. $confirm = Confirm_address::getKV('code', $code);
  72. if (!$confirm) {
  73. // TRANS: Client error displayed when providing a non-existing confirmation code in the contact address confirmation action.
  74. $this->clientError(_('Confirmation code not found.'));
  75. }
  76. $cur = common_current_user();
  77. if ($cur->id != $confirm->user_id) {
  78. // TRANS: Client error displayed when not providing a confirmation code for another user in the contact address confirmation action.
  79. $this->clientError(_('That confirmation code is not for you!'));
  80. }
  81. $type = $confirm->address_type;
  82. $transports = array();
  83. Event::handle('GetImTransports', array(&$transports));
  84. if (!in_array($type, array('email', 'sms')) && !in_array($type, array_keys($transports))) {
  85. // 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')
  86. $this->serverError(sprintf(_('Unrecognized address type %s'), $type));
  87. }
  88. $this->address = $confirm->address;
  89. $cur->query('BEGIN');
  90. if (in_array($type, array('email', 'sms')))
  91. {
  92. if ($cur->$type == $confirm->address) {
  93. // TRANS: Client error for an already confirmed email/jabber/sms address.
  94. $this->clientError(_('That address has already been confirmed.'));
  95. }
  96. $orig_user = clone($cur);
  97. $cur->$type = $confirm->address;
  98. if ($type == 'sms') {
  99. $cur->carrier = ($confirm->address_extra)+0;
  100. $carrier = Sms_carrier::getKV($cur->carrier);
  101. $cur->smsemail = $carrier->toEmailAddress($cur->sms);
  102. }
  103. // Throws exception on failure.
  104. $cur->updateWithKeys($orig_user);
  105. if ($type == 'email') {
  106. $cur->emailChanged();
  107. }
  108. } else {
  109. $user_im_prefs = new User_im_prefs();
  110. $user_im_prefs->transport = $confirm->address_type;
  111. $user_im_prefs->user_id = $cur->id;
  112. if ($user_im_prefs->find() && $user_im_prefs->fetch()) {
  113. if($user_im_prefs->screenname == $confirm->address){
  114. // TRANS: Client error for an already confirmed IM address.
  115. $this->clientError(_('That address has already been confirmed.'));
  116. }
  117. $user_im_prefs->screenname = $confirm->address;
  118. $result = $user_im_prefs->update();
  119. if (!$result) {
  120. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  121. // TRANS: Server error displayed when updating IM preferences fails.
  122. $this->serverError(_('Could not update user IM preferences.'));
  123. }
  124. }else{
  125. $user_im_prefs = new User_im_prefs();
  126. $user_im_prefs->screenname = $confirm->address;
  127. $user_im_prefs->transport = $confirm->address_type;
  128. $user_im_prefs->user_id = $cur->id;
  129. $result = $user_im_prefs->insert();
  130. if (!$result) {
  131. common_log_db_error($user_im_prefs, 'INSERT', __FILE__);
  132. // TRANS: Server error displayed when adding IM preferences fails.
  133. $this->serverError(_('Could not insert user IM preferences.'));
  134. }
  135. }
  136. }
  137. $result = $confirm->delete();
  138. if (!$result) {
  139. common_log_db_error($confirm, 'DELETE', __FILE__);
  140. // TRANS: Server error displayed when an address confirmation code deletion from the
  141. // TRANS: database fails in the contact address confirmation action.
  142. $this->serverError(_('Could not delete address confirmation.'));
  143. }
  144. $cur->query('COMMIT');
  145. $this->showPage();
  146. }
  147. /**
  148. * Title of the page
  149. *
  150. * @return string title
  151. */
  152. function title()
  153. {
  154. // TRANS: Title for the contact address confirmation action.
  155. return _('Confirm address');
  156. }
  157. /**
  158. * Show a confirmation message.
  159. *
  160. * @return void
  161. */
  162. function showContent()
  163. {
  164. $cur = common_current_user();
  165. $this->element('p', null,
  166. // TRANS: Success message for the contact address confirmation action.
  167. // TRANS: %s can be 'email', 'jabber', or 'sms'.
  168. sprintf(_('The address "%s" has been '.
  169. 'confirmed for your account.'),
  170. $this->address));
  171. }
  172. }