]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/CasAuthentication/CasAuthenticationPlugin.php
Merge branch 'apinamespace' into 0.9.x
[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     public $server;
40     public $port = 443;
41     public $path = '';
42     public $takeOverLogin = false;
43
44     function checkPassword($username, $password)
45     {
46         global $casTempPassword;
47         return ($casTempPassword == $password);
48     }
49
50     function onAutoload($cls)
51     {
52         switch ($cls)
53         {
54          case 'phpCAS':
55             require_once(INSTALLDIR.'/plugins/CasAuthentication/extlib/CAS.php');
56             return false;
57          case 'CasloginAction':
58             require_once(INSTALLDIR.'/plugins/CasAuthentication/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
59             return false;
60         }
61     }
62
63     function onArgsInitialize(&$args)
64     {
65         if($this->takeOverLogin && $args['action'] == 'login')
66         {
67             $args['action'] = 'caslogin';
68         }
69     }
70
71     function onStartInitializeRouter($m)
72     {
73         $m->connect('main/cas', array('action' => 'caslogin'));
74         return true;
75     }
76
77     function onEndLoginGroupNav(&$action)
78     {
79         $action_name = $action->trimmed('action');
80
81         $action->menuItem(common_local_url('caslogin'),
82                           _m('CAS'),
83                           _m('Login or register with CAS'),
84                           $action_name === 'caslogin');
85
86         return true;
87     }
88
89     function onEndShowPageNotice($action)
90     {
91         $name = $action->trimmed('action');
92
93         switch ($name)
94         {
95          case 'login':
96             $instr = '(Have an account with CAS? ' .
97               'Try our [CAS login]'.
98               '(%%action.caslogin%%)!)';
99             break;
100          default:
101             return true;
102         }
103
104         $output = common_markup_to_html($instr);
105         $action->raw($output);
106         return true;
107     }
108
109     function onLoginAction($action, &$login)
110     {
111         switch ($action)
112         {
113          case 'caslogin':
114             $login = true;
115             return false;
116          default:
117             return true;
118         }
119     }
120
121     function onInitializePlugin(){
122         parent::onInitializePlugin();
123         if(!isset($this->server)){
124             throw new Exception("must specify a server");
125         }
126         if(!isset($this->port)){
127             throw new Exception("must specify a port");
128         }
129         if(!isset($this->path)){
130             throw new Exception("must specify a path");
131         }
132         //These values need to be accessible to a action object
133         //I can't think of any other way than global variables
134         //to allow the action instance to be able to see values :-(
135         global $casSettings;
136         $casSettings = array();
137         $casSettings['server']=$this->server;
138         $casSettings['port']=$this->port;
139         $casSettings['path']=$this->path;
140         $casSettings['takeOverLogin']=$this->takeOverLogin;
141     }
142
143     function onPluginVersion(&$versions)
144     {
145         $versions[] = array('name' => 'CAS Authentication',
146                             'version' => STATUSNET_VERSION,
147                             'author' => 'Craig Andrews',
148                             'homepage' => 'http://status.net/wiki/Plugin:CasAuthentication',
149                             'rawdescription' =>
150                             _m('The CAS Authentication plugin allows for StatusNet to handle authentication through CAS (Central Authentication Service).'));
151         return true;
152     }
153 }