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