forked from BellevueCollege/wordpress-cas-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress-cas-client.php
392 lines (326 loc) · 13.2 KB
/
wordpress-cas-client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/*
Plugin Name: WordPress CAS Client
Plugin URI: https://github.com/BellevueCollege/wordpress-cas-client
Description: Integrates WordPress with existing <a href="http://en.wikipedia.org/wiki/Central_Authentication_Service">CAS</a> single sign-on architectures. Additionally this plugin can use a LDAP server (such as Active Directory) for populating user information after the user has successfully logged on to WordPress. This plugin is a fork of the <a href="http://wordpress.org/extend/plugins/wpcas-w-ldap">wpCAS-w-LDAP</a> plugin.
Version: 1.2.0.0
Author: Bellevue College
Author URI: http://www.bellevuecollege.edu
License: GPL2
*/
/*
Copyright (C) 2009 Ioannis C. Yessios
This plugin owes a huge debt to
Casey Bisson's wpCAS, copyright (C) 2008
and released under GPL.
http://wordpress.org/extend/plugins/wpcasldap/
Casey Bisson's plugin owes a huge debt to Stephen Schwink's CAS Authentication plugin, copyright (C) 2008
and released under GPL.
http://wordpress.org/extend/plugins/cas-authentication/
It also borrowed a few lines of code from Jeff Johnson's SoJ CAS/LDAP Login plugin
http://wordpress.org/extend/plugins/soj-casldap/
This plugin honors and extends Bisson's and Schwink's work, and is licensed under the same terms.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// include common functions, etc.
include_once(dirname(__FILE__)."/cas-client-constants.php");
include_once(dirname(__FILE__)."/utilities.php");
// automatically include class files when encountered
spl_autoload_register('class_autoloader');
// Must explicitly include class file when referencing static members
include_once(dirname(__FILE__)."/ldapManager.php");
include_once(dirname(__FILE__)."/casManager.php");
// This global variable is set to either 'get_option' or 'get_site_option' depending on multisite option value
global $get_options_func ;
//This global variable is defaulted to 'options.php' , but for network setting we want the form to submit to itself, so we will leave it empty
global $form_action;
if (file_exists( dirname(__FILE__).'/config.php' ) )
/** @noinspection PhpIncludeInspection */
include_once( dirname(__FILE__).'/config.php' ); // attempt to fetch the optional config file
if (file_exists( dirname(__FILE__).'/cas-server-ui.php' ) )
include_once( dirname(__FILE__).'/cas-server-ui.php' ); // attempt to fetch the optional config file
if (file_exists( dirname(__FILE__).'/cas-password-encryption.php' ) )
include_once( dirname(__FILE__).'/cas-password-encryption.php' );
// helps separate debug output
debug_log("================= Executing wordpress-cas-client.php ===================\n");
if (file_exists( dirname(__FILE__).'/cas-client-ui.php' ) )
include_once( dirname(__FILE__).'/cas-client-ui.php' ); // attempt to fetch the optional config file
$get_options_func = "get_option";
updateSettings();
if(is_multisite())
{
add_action( 'network_admin_menu', 'cas_client_settings' );
debug_log("multisite true");
$get_options_func = "get_site_option";
}
debug_log("version :". $get_options_func('wpcasldap_cas_version'));
debug_log("version :". $get_options_func('wpcasldap_server_hostname'));
global $wpcasldap_options;
if($wpcasldap_options)
{
if (!is_array($wpcasldap_options))
$wpcasldap_optons = array();
}
$wpcasldap_use_options = wpcasldap_getoptions();
debug_log("(wordpress-cas-client) options: ".print_r($wpcasldap_use_options,true));
global $casManager;
$casManager = new casManager($wpcasldap_use_options);
// plugin hooks into authentication system
add_action('wp_authenticate', 'cas_client_authenticate', 10, 2);
add_action('wp_logout', 'cas_client_logout');
add_action('lost_password', 'cas_client_lost_password');
add_action('retrieve_password', 'cas_client_retrieve_password');
add_action('password_reset', 'cas_client_password_reset');
add_filter('show_password_fields', 'cas_client_show_password_fields');
if (is_admin() && !is_multisite()) {// Added condition not multisite because if multisite is true thn it should only show the settings in network admin menu.
add_action( 'admin_init', 'wpcasldap_register_settings' );
add_action( 'admin_menu', 'wpcasldap_options_page_add' );
}
function cas_client_authenticate()
{
global $casManager;
$casManager->authenticate();
}
function cas_client_logout()
{
global $casManager;
$casManager->logout();
}
function cas_client_lost_password()
{
global $casManager;
$casManager->disable_function();
}
function cas_client_retrieve_password()
{
global $casManager;
$casManager->disable_function();
}
function cas_client_password_reset()
{
global $casManager;
$casManager->disable_function();
}
function cas_client_show_password_fields($show_password_fields)
{
global $casManager;
return $casManager->show_password_fields($show_password_fields);
}
function sid2str($sid)
{
$srl = ord($sid[0]);
$number_sub_id = ord($sid[1]);
$x = substr($sid,2,6);
$h = unpack('N',"\x0\x0".substr($x,0,2));
$l = unpack('N',substr($x,2,6));
$iav = bcadd(bcmul($h[1],bcpow(2,32)),$l[1]);
for ($i=0; $i<$number_sub_id; $i++)
{
$sub_id = unpack('V', substr($sid, 8+4*$i, 4));
$sub_ids[] = $sub_id[1];
}
return sprintf('S-%d-%d-%s', $srl, $iav, implode('-',$sub_ids));
}
//----------------------------------------------------------------------------
// ADMIN OPTION PAGE FUNCTIONS
//----------------------------------------------------------------------------
function wpcasldap_register_settings() {
global $wpcasldap_options;
$options = array('email_suffix', 'casserver', 'cas_version', 'include_path', 'server_hostname', 'server_port', 'server_path', 'useradd', 'userrole', 'ldapuri', 'ldaphost',
'ldapport', 'ldapbasedn', 'useldap', 'ldapuser', 'ldappassword', 'casorldap_attribute', 'casatt_name', 'casatt_operator', 'casatt_user_value_to_compare', 'casatt_wp_role',
'casatt_wp_site', 'ldap_query', 'ldap_operator', 'ldap_user_value_to_compare', 'ldap_wp_role', 'ldap__wp_site');
foreach ($options as $o) {
if (!isset($wpcasldap_options[$o])) {
switch($o) {
case 'cas_verion':
$cleaner = 'wpcasldap_oneortwo';
break;
case 'useradd':
case 'useldap':
$cleaner = 'wpcasldap_yesorno';
break;
case 'email_suffix':
$cleaner = 'wpcasldap_strip_at';
break;
case 'userrole':
$cleaner = 'wpcasldap_fix_userrole';
break;
case 'ldapport':
case 'server_port':
$cleaner = 'intval';
break;
default:
$cleaner = 'wpcasldap_dummy';
}
register_setting( 'wpcasldap', 'wpcasldap_'.$o,$cleaner );
}
}
}
// TODO: The following 5 functions look like perhaps they should be moved into utilities.php
function wpcasldap_strip_at($in) {
return str_replace('@','',$in);
}
function wpcasldap_yesorno ($in) {
return (strtolower($in) == 'yes')?'yes':'no';
}
function wpcasldap_oneortwo($in) {
return ($in == '1.0')?'1.0':'2.0';
}
function wpcasldap_fix_userrole($in) {
$roles = array('subscriber','contributor','author','editor','administrator');
if (in_array($in,$roles))
return $in;
else
return 'subscriber';
}
function wpcasldap_dummy($in) {
return $in;
}
function cas_client_settings()
{
add_submenu_page("settings.php","CAS Client","CAS Client","manage_network","casclient",'wpcasldap_options_page');
}
function wpcasldap_options_page_add() {
if (function_exists('add_management_page'))
{
error_log("options general ----------------------------");
add_submenu_page('options-general.php', 'CAS Client', 'CAS Client', CAPABILITY, 'casclient', 'wpcasldap_options_page');
}
//add_submenu_page('options-general.php', 'wpCAS with LDAP', 'wpCAS with LDAP', CAPABILITY, 'wpcasldap', 'wpcasldap_options_page');
else
{
error_log("CAS Client for single site ----------------------------");
add_options_page( 'CAS Client','CAS Client',CAPABILITY, basename(__FILE__), 'wpcasldap_options_page');
}
//add_options_page( __( 'wpCAS with LDAP', 'wpcasldap' ), __( 'wpCAS with LDAP', 'wpcasldap' ),CAPABILITY, basename(__FILE__), 'wpcasldap_options_page');
}
function wpcasldap_getoptions() {
global $wpcasldap_options;
global $get_options_func;
//Parse the url to retrieve server_name, server_port and path
$cas_server = $get_options_func('wpcasldap_casserver');
$componentsOfUrl = parse_cas_url($cas_server);
error_log("url componenets :".print_r($componentsOfUrl,true));
$host = "";
$port = "";
$path = "";
if($componentsOfUrl)
{
if(isset($componentsOfUrl['host']))
{
$host = $componentsOfUrl['host'];
}
if(isset($componentsOfUrl['port']))
$port = $componentsOfUrl['port'];
else
$port = CAS_DEFAULT_PORT;
if(isset($componentsOfUrl['path']))
$path = $componentsOfUrl['path'];
else
$path = CAS_DEFAULT_PATH;
}
//error_log("hostname :".$host);
//error_log("port :".$port);
//error_log("path :".$path);
//Parse ldap URI to retrieve LDAP Host and LDAP Port
$ldap_uri = $get_options_func('wpcasldap_ldapuri');
debug_log("(wordpress-cas-client) Retrieved LDAP URI from db: '$ldap_uri'");
$ldap_host = "";
$ldap_port = "";
$ldap_uri_components = ldapManager::ParseUri($ldap_uri);
if(isset($ldap_uri_components))
{
if(isset($ldap_uri_components['host']))
{
$ldap_host = $ldap_uri_components['host'];
}
if(isset($ldap_uri_components['port']))
$ldap_port = $ldap_uri_components['port'];
else if(isset($ldap_uri_components['scheme']))
{
if(strtolower($ldap_uri_components['scheme']) == 'ldaps')
$ldap_port = ldapManager::SSL_DEFAULT_PORT;
else if(strtolower($ldap_uri_components['scheme']) == 'ldap')
$ldap_port = ldapManager::DEFAULT_PORT;
}
else
$ldap_port = ldapManager::DEFAULT_PORT;
}
//error_log("scheme :".$ldap_uri_components['scheme']);
//error_log("hostname :".$ldap_host);
//error_log("port :".$ldap_port);
//get ldap password and decrypt it
$ldapPassword = (string) $get_options_func('wpcasldap_ldappassword');
$ldapPassword = wpcasclient_decrypt($ldapPassword , $GLOBALS['ciphers']) ;
$ldapPassword = $ldapPassword ? $ldapPassword : ""; // if the decrypt function returns false thn set password to empty string
// TODO: Are all of these settings still being used? (e.g. ldap_host?)
$out = array (
'email_suffix' => $get_options_func('wpcasldap_email_suffix'),
'cas_version' => $get_options_func('wpcasldap_cas_version'),
'include_path' => $get_options_func('wpcasldap_include_path'),
'casserver' => $cas_server, //$get_options_func('wpcasldap_casserver'),
'server_hostname' => $host,//$get_options_func('wpcasldap_server_hostname'),
'server_port' => $port,//$get_options_func('wpcasldap_server_port'),
'server_path' => $path,//$get_options_func('wpcasldap_server_path'),
'useradd' => $get_options_func('wpcasldap_useradd'),
'userrole' => $get_options_func('wpcasldap_userrole'),
'ldapuri' => $ldap_uri,//$get_options_func('wpcasldap_ldapuri'),
'ldaphost' => $ldap_host, //$get_options_func('wpcasldap_ldaphost'),
'ldapport' => $ldap_port,// $get_options_func('wpcasldap_ldapport'),
'useldap' => $get_options_func('wpcasldap_useldap'),
'ldapbasedn' => $get_options_func('wpcasldap_ldapbasedn'),
'ldapuser' => $get_options_func('wpcasldap_ldapuser'),
'ldappassword' => $ldapPassword,
'casorldap_attribute' => $get_options_func('wpcasldap_casorldap_attribute'),
'casatt_name' => $get_options_func('wpcasldap_casatt_name'),
'casatt_operator' => $get_options_func('wpcasldap_casatt_operator'),
'casatt_user_value_to_compare' => $get_options_func('wpcasldap_casatt_user_value_to_compare'),
'casatt_wp_role' => $get_options_func('wpcasldap_casatt_wp_role'),
'casatt_wp_site' => $get_options_func('wpcasldap_casatt_wp_site'),
'ldap_query' => $get_options_func('wpcasldap_ldap_query'),
'ldap_operator' => $get_options_func('wpcasldap_ldap_operator'),
'ldap_user_value_to_compare' => $get_options_func('wpcasldap_ldap_user_value_to_compare'),
'ldap_wp_role' => $get_options_func('wpcasldap_ldap_wp_role'),
'ldap__wp_site' => $get_options_func('wpcasldap_ldap_wp_site'),
);
if (is_array($wpcasldap_options) && count($wpcasldap_options) > 0)
{
foreach ($wpcasldap_options as $key => $val) {
$out[$key] = $val;
}
}
//error_log("OUT :".print_r($out,true));
return $out;
}
function parse_cas_url(&$cas_server_url)
{
$components = parse_url($cas_server_url);
if($components)
{
if(empty($components['host']) && !empty($components['path']))
{
error_log("path :".$components['path']);
$cas_server_url = SCHEME.$cas_server_url;
error_log("cas url :".$cas_server_url);
$components = parse_url($cas_server_url);
error_log("componenets after editing url :".print_r($components,true));
}
}
return $components;
}
function get_option_wrapper($opt)
{
global $get_options_func;
return $get_options_func($opt);
}
?>