]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidserver.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / OpenID / openidserver.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Settings for OpenID
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  Settings
23  * @package   StatusNet
24  * @author   Craig Andrews <candrews@integralblue.com>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/action.php';
36 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
37 require_once(INSTALLDIR.'/plugins/OpenID/User_openid_trustroot.php');
38
39 /**
40  * Settings for OpenID
41  *
42  * Lets users add, edit and delete OpenIDs from their account
43  *
44  * @category Settings
45  * @package  StatusNet
46  * @author   Craig Andrews <candrews@integralblue.com>
47  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
51 class OpenidserverAction extends Action
52 {
53     var $oserver;
54
55     function prepare($args)
56     {
57         parent::prepare($args);
58         $this->oserver = oid_server();
59         return true;
60     }
61
62     function handle($args)
63     {
64         parent::handle($args);
65         $request = $this->oserver->decodeRequest();
66         if (in_array($request->mode, array('checkid_immediate',
67             'checkid_setup'))) {
68             $user = common_current_user();
69             if(!$user){
70                 if($request->immediate){
71                     //cannot prompt the user to login in immediate mode, so answer false
72                     $response = $this->generateDenyResponse($request);
73                 }else{
74                     // Go log in, and then come back.
75                     //
76                     // Note: 303 redirect rather than 307 to avoid
77                     // prompting user for form resubmission if we
78                     // were POSTed here.
79                     common_set_returnto($_SERVER['REQUEST_URI']);
80                     common_redirect(common_local_url('login'), 303);
81                     return;
82                 }
83             }else if(common_profile_url($user->nickname) == $request->identity || $request->idSelect()){
84                 $user_openid_trustroot = User_openid_trustroot::pkeyGet(
85                                                 array('user_id'=>$user->id, 'trustroot'=>$request->trust_root));
86                 if(empty($user_openid_trustroot)){
87                     if($request->immediate){
88                         //cannot prompt the user to trust this trust root in immediate mode, so answer false
89                         $response = $this->generateDenyResponse($request);
90                     }else{
91                         common_ensure_session();
92                         $_SESSION['openid_trust_root'] = $request->trust_root;
93                         $allowResponse = $this->generateAllowResponse($request, $user);
94                         $this->oserver->encodeResponse($allowResponse); //sign the response
95                         $denyResponse = $this->generateDenyResponse($request);
96                         $this->oserver->encodeResponse($denyResponse); //sign the response
97                         $_SESSION['openid_allow_url'] = $allowResponse->encodeToUrl();
98                         $_SESSION['openid_deny_url'] = $denyResponse->encodeToUrl();
99
100                         // Ask the user to trust this trust root...
101                         //
102                         // Note: 303 redirect rather than 307 to avoid
103                         // prompting user for form resubmission if we
104                         // were POSTed here.
105                         common_redirect(common_local_url('openidtrust'), 303);
106                         return;
107                     }
108                 }else{
109                     //user has previously authorized this trust root
110                     $response = $this->generateAllowResponse($request, $user);
111                     //$response = $request->answer(true, null, common_profile_url($user->nickname));
112                 }
113             } else if ($request->immediate) {
114                 $response = $this->generateDenyResponse($request);
115             } else {
116                 //invalid
117                 // TRANS: OpenID plugin client error given trying to add an unauthorised OpenID to a user (403).
118                 $this->clientError(sprintf(_m('You are not authorized to use the identity %s.'),$request->identity),$code=403);
119             }
120         } else {
121             $response = $this->oserver->handleRequest($request);
122         }
123
124         if($response){
125             $response = $this->oserver->encodeResponse($response);
126             if ($response->code != AUTH_OPENID_HTTP_OK) {
127                 header(sprintf("HTTP/1.1 %d ", $response->code),
128                        true, $response->code);
129             }
130
131             if($response->headers){
132                 foreach ($response->headers as $k => $v) {
133                     header("$k: $v");
134                 }
135             }
136             $this->raw($response->body);
137         }else{
138             // TRANS: OpenID plugin client error given when not getting a response for a given OpenID provider (500).
139             $this->clientError(_m('Just an OpenID provider. Nothing to see here, move along...'),$code=500);
140         }
141     }
142
143     function generateAllowResponse($request, $user){
144         $response = $request->answer(true, null, common_profile_url($user->nickname));
145         
146         $profile = $user->getProfile();
147         $sreg_data = array(
148             'fullname' => $profile->fullname,
149             'nickname' => $user->nickname,
150             'email' => $user->email,
151             'language' => $user->language,
152             'timezone' => $user->timezone);
153         $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
154         $sreg_response = Auth_OpenID_SRegResponse::extractResponse(
155                               $sreg_request, $sreg_data);
156         $sreg_response->toMessage($response->fields);
157         return $response;
158     }
159
160     function generateDenyResponse($request){
161         $response = $request->answer(false);
162         return $response;
163     }
164 }