]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/CasAuthentication/CasAuthenticationPlugin.php
[VersionBump] 1.19.0, fairly late
[quix0rs-gnu-social.git] / plugins / CasAuthentication / CasAuthenticationPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to enable Single Sign On via CAS (Central Authentication Service)
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  Plugin
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
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 // We bundle the phpCAS library...
35 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/CAS');
36
37 class CasAuthenticationPlugin extends AuthenticationPlugin
38 {
39     const PLUGIN_VERSION = '2.0.0';
40
41     public $server;
42     public $port = 443;
43     public $path = '';
44     public $takeOverLogin = false;
45     public $user_whitelist = null;
46
47     function checkPassword($username, $password)
48     {
49         global $casTempPassword;
50         return ($casTempPassword == $password);
51     }
52
53     function onAutoload($cls)
54     {
55         switch ($cls)
56         {
57          case 'phpCAS':
58             require_once(INSTALLDIR.'/plugins/CasAuthentication/extlib/CAS.php');
59             return false;
60         }
61
62         // if it's not our exception, try standard places
63         return parent::onAutoload($cls);
64     }
65
66     function onArgsInitialize(&$args)
67     {
68         if($this->takeOverLogin && $args['action'] == 'login')
69         {
70             $args['action'] = 'caslogin';
71         }
72     }
73
74     function onStartInitializeRouter($m)
75     {
76         $m->connect('main/cas', array('action' => 'caslogin'));
77         return true;
78     }
79
80     function onEndLoginGroupNav($action)
81     {
82         $action_name = $action->trimmed('action');
83
84         $action->menuItem(common_local_url('caslogin'),
85                           // TRANS: Menu item. CAS is Central Authentication Service.
86                           _m('CAS'),
87                           // TRANS: Tooltip for menu item. CAS is Central Authentication Service.
88                           _m('Login or register with CAS.'),
89                           $action_name === 'caslogin');
90
91         return true;
92     }
93
94     function onEndShowPageNotice($action)
95     {
96         $name = $action->trimmed('action');
97
98         switch ($name)
99         {
100          case 'login':
101             // TRANS: Invitation to users with a CAS account to log in using the service.
102             // TRANS: "[CAS login]" is a link description. (%%action.caslogin%%) is the URL.
103             // TRANS: These two elements may not be separated.
104             $instr = _m('(Have an account with CAS? ' .
105               'Try our [CAS login](%%action.caslogin%%)!)');
106             break;
107          default:
108             return true;
109         }
110
111         $output = common_markup_to_html($instr);
112         $action->raw($output);
113         return true;
114     }
115
116     function onLoginAction($action, &$login)
117     {
118         switch ($action)
119         {
120          case 'caslogin':
121             $login = true;
122             return false;
123          default:
124             return true;
125         }
126     }
127
128     function onInitializePlugin(){
129         parent::onInitializePlugin();
130         if(!isset($this->server)){
131             // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
132             throw new Exception(_m("Specifying a server is required."));
133         }
134         if(!isset($this->port)){
135             // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
136             throw new Exception(_m("Specifying a port is required."));
137         }
138         if(!isset($this->path)){
139             // TRANS: Exception thrown when the CAS Authentication plugin has been configured incorrectly.
140             throw new Exception(_m("Specifying a path is required."));
141         }
142         //These values need to be accessible to a action object
143         //I can't think of any other way than global variables
144         //to allow the action instance to be able to see values :-(
145         global $casSettings;
146         $casSettings = array();
147         $casSettings['server']=$this->server;
148         $casSettings['port']=$this->port;
149         $casSettings['path']=$this->path;
150         $casSettings['takeOverLogin']=$this->takeOverLogin;
151         $casSettings['user_whitelist']=$this->user_whitelist;
152     }
153
154     function onPluginVersion(array &$versions)
155     {
156         $versions[] = array('name' => 'CAS Authentication',
157                             'version' => self::PLUGIN_VERSION,
158                             'author' => 'Craig Andrews',
159                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/CasAuthentication',
160                             // TRANS: Plugin description. CAS is Central Authentication Service.
161                             'rawdescription' => _m('The CAS Authentication plugin allows for StatusNet to handle authentication through CAS (Central Authentication Service).'));
162         return true;
163     }
164 }