]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/confirmaddress.php
9ac6848d7a09f55ceaefb7dd8b05d0a2d423de98
[quix0rs-gnu-social.git] / actions / confirmaddress.php
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
30 if (!defined('GNUSOCIAL')) { 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 ManagedAction
46 {
47     /** type of confirmation. */
48
49     protected $address;
50
51     protected function doPreparation()
52     {
53         if (!common_logged_in()) {
54             common_set_returnto($this->selfUrl());
55             common_redirect(common_local_url('login'));
56         }
57         $code = $this->trimmed('code');
58         if (!$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.'));
61         }
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);
66         }
67
68         try {
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]));
72             $confirm->delete();
73             throw $e;
74         }
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!'));
78         }
79
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));
86         }
87         $this->address = $confirm->address;
88
89         $cur = $this->scoped->getUser();
90
91         $cur->query('BEGIN');
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
96                 $confirm->delete();
97                 // TRANS: Client error for an already confirmed email/jabber/sms address.
98                 throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
99             }
100
101             $orig_user = clone($cur);
102
103             $cur->$type = $confirm->address;
104
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);
109             }
110
111             // Throws exception on failure.
112             $cur->updateWithKeys($orig_user);
113
114             if ($type == 'email') {
115                 $cur->emailChanged();
116             }
117
118         } else {
119
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
126                     $confirm->delete();
127                     // TRANS: Client error for an already confirmed IM address.
128                     throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
129                 }
130                 $user_im_prefs->screenname = $confirm->address;
131                 $result = $user_im_prefs->update();
132
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.'));
137                 }
138             }else{
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();
144
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.'));
149                 }
150             }
151
152         }
153
154         $confirm->delete();
155
156         $cur->query('COMMIT');
157     }
158
159     /**
160      * Title of the page
161      *
162      * @return string title
163      */
164     function title()
165     {
166         // TRANS: Title for the contact address confirmation action.
167         return _('Confirm address');
168     }
169
170     /**
171      * Show a confirmation message.
172      *
173      * @return void
174      */
175     function showContent()
176     {
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.'),
182                                $this->address));
183     }
184 }