]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/OpenIDPlugin.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / plugins / OpenID / OpenIDPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
7  * LICENCE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Evan Prodromou <evan@status.net>
23  * @copyright 2009 StatusNet, Inc.
24  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25  * @link      http://status.net/
26  */
27
28 if (!defined('STATUSNET')) {
29     exit(1);
30 }
31
32 /**
33  * Plugin for OpenID authentication and identity
34  *
35  * This class enables consumer support for OpenID, the distributed authentication
36  * and identity system.
37  *
38  * @category Plugin
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://status.net/
43  * @link     http://openid.net/
44  */
45
46 class OpenIDPlugin extends Plugin
47 {
48     /**
49      * Initializer for the plugin.
50      */
51
52     function __construct()
53     {
54         parent::__construct();
55     }
56
57     /**
58      * Add OpenID-related paths to the router table
59      *
60      * Hook for RouterInitialized event.
61      *
62      * @return boolean hook return
63      */
64
65     function onRouterInitialized(&$m)
66     {
67         $m->connect('main/openid', array('action' => 'openidlogin'));
68         $m->connect('settings/openid', array('action' => 'openidsettings'));
69         $m->connect('xrds', array('action' => 'publicxrds'));
70         $m->connect('index.php?action=finishopenidlogin', array('action' => 'finishopenidlogin'));
71         $m->connect('index.php?action=finishaddopenid', array('action' => 'finishaddopenid'));
72
73         return true;
74     }
75
76     function onEndLoginGroupNav(&$action)
77     {
78         $action_name = $action->trimmed('action');
79
80         $action->menuItem(common_local_url('openidlogin'),
81                           _('OpenID'),
82                           _('Login or register with OpenID'),
83                           $action_name === 'openidlogin');
84
85         return true;
86     }
87
88     function onEndAccountSettingsNav(&$action)
89     {
90         $action_name = $action->trimmed('action');
91
92         $action->menuItem(common_local_url('openidsettings'),
93                           _('OpenID'),
94                           _('Add or remove OpenIDs'),
95                           $action_name === 'openidsettings');
96
97         return true;
98     }
99
100     function onAutoload($cls)
101     {
102         switch ($cls)
103         {
104          case 'OpenidloginAction':
105          case 'FinishopenidloginAction':
106          case 'FinishaddopenidAction':
107          case 'XrdsAction':
108          case 'PublicxrdsAction':
109          case 'OpenidsettingsAction':
110             require_once(INSTALLDIR.'/plugins/OpenID/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
111             return false;
112          case 'User_openid':
113             require_once(INSTALLDIR.'/plugins/OpenID/User_openid.php');
114             return false;
115          default:
116             return true;
117         }
118     }
119
120     function onSensitiveAction($action, &$ssl)
121     {
122         switch ($action)
123         {
124          case 'finishopenidlogin':
125          case 'finishaddopenid':
126             $ssl = true;
127             return false;
128          default:
129             return true;
130         }
131     }
132
133     function onLoginAction($action, &$login)
134     {
135         switch ($action)
136         {
137          case 'openidlogin':
138          case 'finishopenidlogin':
139             $login = true;
140             return false;
141          default:
142             return true;
143         }
144     }
145
146     /**
147      * We include a <meta> element linking to the publicxrds page, for OpenID
148      * client-side authentication.
149      *
150      * @return void
151      */
152
153     function onEndHeadChildren($action)
154     {
155         // for client side of OpenID authentication
156         $action->element('meta', array('http-equiv' => 'X-XRDS-Location',
157                                        'content' => common_local_url('publicxrds')));
158     }
159
160     /**
161      * Redirect to OpenID login if they have an OpenID
162      *
163      * @return boolean whether to continue
164      */
165
166     function onRedirectToLogin($action, $user)
167     {
168         if (!empty($user) && User_openid::hasOpenID($user->id)) {
169             common_redirect(common_local_url('openidlogin'), 303);
170             return false;
171         }
172         return true;
173     }
174
175     function onEndShowPageNotice($action)
176     {
177         $name = $action->trimmed('action');
178
179         switch ($name)
180         {
181          case 'register':
182             $instr = '(Have an [OpenID](http://openid.net/)? ' .
183               'Try our [OpenID registration]'.
184               '(%%action.openidlogin%%)!)';
185             break;
186          case 'login':
187             $instr = '(Have an [OpenID](http://openid.net/)? ' .
188               'Try our [OpenID login]'.
189               '(%%action.openidlogin%%)!)';
190             break;
191          default:
192             return true;
193         }
194
195         $output = common_markup_to_html($instr);
196         $action->raw($output);
197         return true;
198     }
199
200     function onStartLoadDoc(&$title, &$output)
201     {
202         if ($title == 'openid')
203         {
204             $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
205
206             $c = file_get_contents($filename);
207             $output = common_markup_to_html($c);
208             return false; // success!
209         }
210
211         return true;
212     }
213
214     function onEndLoadDoc($title, &$output)
215     {
216         if ($title == 'help')
217         {
218             $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
219
220             $output .= common_markup_to_html($menuitem);
221         }
222
223         return true;
224     }
225 }