3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
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.
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/
45 class ConfirmaddressAction extends ManagedAction
47 /** type of confirmation. */
51 protected function doPreparation()
53 if (!common_logged_in()) {
54 common_set_returnto($this->selfUrl());
55 common_redirect(common_local_url('login'));
57 $code = $this->trimmed('code');
59 // TRANS: Client error displayed when not providing a confirmation code in the contact address confirmation action.
60 throw new ClientException(_('No confirmation code.'));
62 $confirm = Confirm_address::getKV('code', $code);
63 if (!$confirm instanceof Confirm_address) {
64 // TRANS: Client error displayed when providing a non-existing confirmation code in the contact address confirmation action.
65 throw new ClientException(_('Confirmation code not found.'), 404);
69 $profile = Profile::getByID($confirm->user_id);
70 } catch (NoResultException $e) {
71 common_log(LOG_INFO, 'Tried to confirm the email for a deleted profile: '._ve(['id'=>$confirm->user_id, 'email'=>$confirm->address]));
75 if (!$profile->sameAs($this->scoped)) {
76 // TRANS: Client error displayed when not providing a confirmation code for another user in the contact address confirmation action.
77 throw new AuthorizationException(_('That confirmation code is not for you!'));
80 $type = $confirm->address_type;
81 $transports = array();
82 Event::handle('GetImTransports', array(&$transports));
83 if (!in_array($type, array('email', 'sms')) && !in_array($type, array_keys($transports))) {
84 // 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')
85 throw new ServerException(sprintf(_('Unrecognized address type %s'), $type));
87 $this->address = $confirm->address;
89 $cur = $this->scoped->getUser();
92 if (in_array($type, array('email', 'sms'))) {
93 common_debug("Confirming {$type} address for user {$this->scoped->getID()}");
94 if ($cur->$type == $confirm->address) {
95 // Already verified, so delete the confirm_address entry
97 // TRANS: Client error for an already confirmed email/jabber/sms address.
98 throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
101 $orig_user = clone($cur);
103 $cur->$type = $confirm->address;
105 if ($type == 'sms') {
106 $cur->carrier = ($confirm->address_extra)+0;
107 $carrier = Sms_carrier::getKV($cur->carrier);
108 $cur->smsemail = $carrier->toEmailAddress($cur->sms);
111 // Throws exception on failure.
112 $cur->updateWithKeys($orig_user);
114 if ($type == 'email') {
115 $cur->emailChanged();
120 $user_im_prefs = new User_im_prefs();
121 $user_im_prefs->transport = $confirm->address_type;
122 $user_im_prefs->user_id = $cur->id;
123 if ($user_im_prefs->find() && $user_im_prefs->fetch()) {
124 if($user_im_prefs->screenname == $confirm->address){
125 // Already verified, so delete the confirm_address entry
127 // TRANS: Client error for an already confirmed IM address.
128 throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
130 $user_im_prefs->screenname = $confirm->address;
131 $result = $user_im_prefs->update();
133 if ($result === false) {
134 common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
135 // TRANS: Server error displayed when updating IM preferences fails.
136 throw new ServerException(_('Could not update user IM preferences.'));
139 $user_im_prefs = new User_im_prefs();
140 $user_im_prefs->screenname = $confirm->address;
141 $user_im_prefs->transport = $confirm->address_type;
142 $user_im_prefs->user_id = $cur->id;
143 $result = $user_im_prefs->insert();
145 if ($result === false) {
146 common_log_db_error($user_im_prefs, 'INSERT', __FILE__);
147 // TRANS: Server error displayed when adding IM preferences fails.
148 throw new ServerException(_('Could not insert user IM preferences.'));
156 $cur->query('COMMIT');
162 * @return string title
166 // TRANS: Title for the contact address confirmation action.
167 return _('Confirm address');
171 * Show a confirmation message.
175 function showContent()
177 $this->element('p', null,
178 // TRANS: Success message for the contact address confirmation action.
179 // TRANS: %s can be 'email', 'jabber', or 'sms'.
180 sprintf(_('The address "%s" has been '.
181 'confirmed for your account.'),