forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-app-joomla.php
More file actions
449 lines (374 loc) · 15.1 KB
/
user-app-joomla.php
File metadata and controls
449 lines (374 loc) · 15.1 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
defined ( '_ISVALID' ) or die ( 'You cannot access this file directly!' );
// This file contains all the functions for getting information
// about users from Joomla 1.0.8 and logging in/out
// ** also compatible with Mambo 4.5.3
// Reference to the application means the external application (joomla)
// user-app-*.php auth files assume the following:
// - login ids are unique within the application
// - user administration is done through the application
// The following functions had to be configured to work with the application:
// - app_active_session
// - app_update_session
// - user_logged_in (returns login id if true)
// - get_admins (returns an array of admin login ids)
// - user_get_users (returns array of users)
// - user_load_variables (loads info about a user)
include_once 'auth-settings.php';
// User administration should be done through the application's interface
$user_can_update_password = false;
$admin_can_add_user = false;
// For Joomla, we can automatically fetch the values we need from the
// configuration.php file
$app_config = '';
$config_lines = file( $app_path . "configuration.php" );
foreach ( $config_lines as $line ) {
preg_match ( "/mosConfig_([\w]+) = '([^']+)'/", $line, $match);
if ( isset ( $match[1] ) && isset ( $match[2] ) )
$app_config[$match[1]] = $match[2];
}
unset ( $config_lines );
// Joomla 1.0.8 introduced session types
$app_session_type = ( isset ( $app_config['session_type'] ) )
? $app_config['session_type'] : '';
$app_secret = $app_config['secret'];
// Session id cookie name
$app_sid = app_cookie_name( $app_config['live_site'] );
// Session lifetime before required to login again
$app_sid_lifetime = $app_config['lifetime'];
// Name of table containing users
$app_user_table = $app_config['dbprefix'].'users';
// Name of table containing sessions
$app_session_table = $app_config['dbprefix'].'session';
// Application login form parameters
$app_login_page['action'] = $app_config['live_site'].'/index.php?option=login';
$app_login_page['username'] = 'username';
$app_login_page['password'] = 'passwd';
$app_login_page['remember'] = 'remember';
$app_login_page['submit'] = 'submit';
$app_login_page['return'] = 'return';
// hidden params
$app_login_page['hidden']['op2'] = 'login';
// What is the full URL to the logout page (including http:// or https://)
$app_logout_page = $app_config['live_site'].'/index.php?option=logout&op2=logout';
// Name of database containing the app's tables
$app_db = $app_config['db'];
// Host that the app's db is on
$app_host = $app_config['host'];
// Login/Password to access the app's database
$app_login = $app_config['user'];
$app_pass = $app_config['password'];
// Debug
//var_dump($app_config);exit;
// Cleanup stuff we don't need anymore
unset ( $app_config );
/********************************************************************/
// Checks to see if the session has a user associated with it and
// if the session is timed out
// returns: login id
function app_active_session ($sid) {
global $app_db, $app_host, $app_login, $app_pass, $app_same_db,
$app_session_table, $app_settings_table, $app_sid_lifetime,
$app_user_table, $c, $db_database, $db_host, $db_login, $db_password;
// if application is in a separate db, we have to connect to it
if ($app_same_db != '1') $c = dbi_connect($app_host, $app_login, $app_pass, $app_db);
// get login and last access time
$sql = "SELECT username, time FROM $app_session_table WHERE session_id = '$sid' ".
"AND guest = 0 AND userid > 0 ";
$res = dbi_query ( $sql );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$login = $row[0];
$last = $row[1];
}
dbi_free_result ( $res );
}
// Did we pass inactive session time limit
if ( ( $app_sid_lifetime > 0 ) && ( $last < ( time () - $app_sid_lifetime ) ) ) $login = false;
// if application is in a separate db, we have to connect back to the webcal db
if ($app_same_db != '1') $c = dbi_connect($db_host, $db_login, $db_password, $db_database);
return $login;
}
// Joomla 1.0.8 introduced the option of three different session types
function app_get_sid( $id ) {
global $app_secret, $app_session_type;
$browser = @$_SERVER['HTTP_USER_AGENT'];
switch ( $app_session_type ) {
case 2:
// 1.0.0 to 1.0.7 Compatibility
// lowest level security
$value = md5 ( $id . $_SERVER['REMOTE_ADDR'] );
break;
case 1:
// slightly reduced security - 3rd level IP authentication for those behind IP Proxy
$remote_addr = explode ( '.', $_SERVER['REMOTE_ADDR'] );
$ip = $remote_addr[0] .'.'. $remote_addr[1] .'.'. $remote_addr[2];
$value = md5 ( $app_secret . md5 ( $id . $ip . $browser ) );
break;
default:
// Highest security level - new default for 1.0.8 and beyond
$ip = $_SERVER['REMOTE_ADDR'];
$value = md5 ( $app_secret . md5 ( $id . $ip . $browser ) );
break;
}
return $value;
}
// Updates the session table to set the last access time to now
function app_update_session($sid) {
global $app_db, $app_host, $app_login, $app_pass, $app_same_db,
$app_session_table, $c, $db_database, $db_host, $db_login, $db_password;
// if application is in a separate db, we have to connect to it
if ($app_same_db != '1') $c = dbi_connect($app_host, $app_login, $app_pass, $app_db);
// get login and last access time
$sql = "UPDATE $app_session_table SET time = '".time ()."' WHERE session_id = '$sid'";
dbi_query ( $sql );
// if application is in a separate db, we have to connect back to the webcal db
if ($app_same_db != '1') $c = dbi_connect($db_host, $db_login, $db_password, $db_database);
return true;
}
function app_cookie_name( $live_site='' ) {
if( substr( $live_site, 0, 7 ) == 'http://' ) {
$hash = md5( 'site' . substr( $live_site, 7 ) );
} elseif( substr( $live_site, 0, 8 ) == 'https://' ) {
$hash = md5( 'site' . substr( $live_site, 8 ) );
} else {
$hash = md5( 'site' . $live_site );
}
return $hash;
}
// Searches application database for $app_admin_gid and returns an array of the group members.
// Do this search only once per request.
// returns: array of admin ids
function get_admins () {
global $app_admin_gid, $app_db, $app_host, $app_login,
$app_pass, $app_same_db, $app_user_table, $c, $cached_admins,
$db_database, $db_host, $db_login, $db_password;
if ( ! empty ( $cached_admins ) ) return $cached_admins;
$cached_admins = [];
// if application is in a separate db, we have to connect to it
if ($app_same_db != '1') $c = dbi_connect($app_host, $app_login, $app_pass, $app_db);
// what are the gid's of the admin group
$where = '(';
$num = count ( $app_admin_gid );
for ($i=0; $i<$num; $i++) {
$where .= "gid = $app_admin_gid[$i]";
if ( ( $i + 1 ) < $num ) $where .= ' OR ';
}
$where .= ')';
$sql = "SELECT id FROM $app_user_table WHERE $where AND id > 0";
$res = dbi_query ( $sql );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$cached_admins[] = $row[0];
}
}
// if application is in a separate db, we have to connect back to the webcal db
if ($app_same_db != '1') $c = dbi_connect($db_host, $db_login, $db_password, $db_database);
return array_unique($cached_admins);
}
/// Get a list of users and return info in an array.
// returns: array of users
function user_get_users ( $publicOnly=false ) {
global $app_db, $app_host, $app_login, $app_pass, $app_same_db,
$app_user_table, $c, $db_database, $db_host, $db_login,
$db_password, $PUBLIC_ACCESS_FULLNAME, $PUBLIC_ACCESS;
$Admins = get_admins ();
$count = 0;
$ret = [];
if ( $PUBLIC_ACCESS == 'Y' )
$ret[$count++] = [
'cal_login' => '__public__',
'cal_lastname' => '',
'cal_firstname' => '',
'cal_is_admin' => 'N',
'cal_email' => '',
'cal_password' => '',
'cal_fullname' => $PUBLIC_ACCESS_FULLNAME];
if ( $publicOnly ) return $ret;
// if application is in a separate db, we have to connect to it
if ($app_same_db != '1') $c = dbi_connect($app_host, $app_login, $app_pass, $app_db);
$sql = "SELECT id, name, username, email FROM $app_user_table WHERE id > 0 AND block = 0 ORDER BY name";
$res = dbi_query ( $sql );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$flname = explode (' ',$row[1]);
$fname = ( isset ( $flname[1] ) ? $flname[0] : $row[1] );
$lname = ( isset ( $flname[1] ) ? $flname[1] : '' );
$ret[$count++] = [
'cal_login' => $row[2],
'cal_lastname' => $lname,
'cal_firstname' => $fname,
'cal_is_admin' => user_is_admin ($row[0],$Admins),
'cal_email' => $row[3],
'cal_fullname' => $row[1]];
}
dbi_free_result ( $res );
}
// if application is in a separate db, we have to connect back to the webcal db
if ($app_same_db != '1') $c = dbi_connect($db_host, $db_login, $db_password, $db_database);
usort ( $ret, 'sort_users');
return $ret;
}
// Load info about a user (first name, last name, admin) and set globally.
// params:
// $user - user login
// $prefix - variable prefix to use
function user_load_variables ( $login, $prefix ) {
global $app_db, $app_host, $app_login, $app_pass, $app_same_db,
$app_user_table, $c, $cached_user_var, $db_database, $db_host,
$db_login, $db_password, $NONUSER_PREFIX, $PUBLIC_ACCESS_FULLNAME;
if ( ! empty ( $cached_user_var[$login][$prefix] ) )
return $cached_user_var[$login][$prefix];
$cached_user_var = [];
if ($NONUSER_PREFIX && substr ($login, 0, strlen ($NONUSER_PREFIX) ) == $NONUSER_PREFIX) {
nonuser_load_variables ( $login, $prefix );
return true;
}
if ( $login == '__public__' ) {
$GLOBALS[$prefix . 'login'] = $login;
$GLOBALS[$prefix . 'firstname'] = '';
$GLOBALS[$prefix . 'lastname'] = '';
$GLOBALS[$prefix . 'is_admin'] = 'N';
$GLOBALS[$prefix . 'email'] = '';
$GLOBALS[$prefix . 'fullname'] = $PUBLIC_ACCESS_FULLNAME;
$GLOBALS[$prefix . 'password'] = '';
return true;
}
// if application is in a separate db, we have to connect to it
if ($app_same_db != '1') $c = dbi_connect($app_host, $app_login, $app_pass, $app_db);
$sql = "SELECT id, name, username, email FROM $app_user_table WHERE username = '$login'";
$res = dbi_query ( $sql );
if ( $res ) {
if ( $row = dbi_fetch_row ( $res ) ) {
$flname = explode (' ',$row[1]);
$fname = ( isset ( $flname[1] ) ? $flname[0] : $row[1] );
$lname = ( isset ( $flname[1] ) ? $flname[1] : '' );
$GLOBALS[$prefix . 'login'] = $login;
$GLOBALS[$prefix . 'firstname'] = $fname;
$GLOBALS[$prefix . 'lastname'] = $lname;
$GLOBALS[$prefix . 'is_admin'] = user_is_admin ($row[0],get_admins ());
$GLOBALS[$prefix . 'email'] = $row[3];
$GLOBALS[$prefix . 'fullname'] = $row[1];
}
dbi_free_result ( $res );
} else {
$error = db_error ();
return false;
}
// if application is in a separate db, we have to connect back to the webcal db
if ($app_same_db != '1') $c = dbi_connect($db_host, $db_login, $db_password, $db_database);
//save these results
$cached_user_var[$login][$prefix] = true;
return true;
}
// Checks to see if the user is logged into the application
// returns: login id
function user_logged_in () {
global $app_sid, $_COOKIE;
// First check to see if the user even has a session cookie
if (empty ($_COOKIE[$app_sid])) return false;
// Generate session id
$sid = app_get_sid( $_COOKIE[$app_sid] );
// Check to see if the session is still valid
if (! $login = app_active_session ($sid) ) return false;
// Update the session last access time
app_update_session($sid);
return $login;
}
/*********************************************************************
*
* Stuff that should stay the same for all user-app files
*
********************************************************************/
// Are the application's tables in the same database as webcalendar's?
$app_same_db = (($db_database == $app_db) && ($app_host == $db_host)) ? '1' : '0';
//echo "Same DB:$app_same_db";exit;
// Redirect the user to the login-app.php page
function app_login_screen( $return ) {
header ( "Location: " . getServerUrl() . "login-app.php?return_path={$return}");
exit;
}
// Test if a user is an admin, that is: if the user is a member of a special
// group in the application database
// params:
// $values - the login name
// returns: Y if user is admin, N if not
function user_is_admin ($uid,$Admins) {
if ( ! $Admins ) {
return 'N';
} else if (in_array ($uid, $Admins)) {
return 'Y';
} else {
return 'N';
}
}
// Delete a user from the webcalendar tables. (NOT from the application)
// We assume that we've already checked to make sure this user doesn't
// have events still in the database.
// params:
// $user - user to delete
function user_delete_user ( $user ) {
// Get event ids for all events this user is a participant
$events = get_users_event_ids ( $user );
// Now count number of participants in each event...
// If just 1, then save id to be deleted
$delete_em = [];
for ( $i = 0; $i < count ( $events ); $i++ ) {
$res = dbi_query ( "SELECT COUNT(*) FROM webcal_entry_user " .
"WHERE cal_id = " . $events[$i] );
if ( $res ) {
if ( $row = dbi_fetch_row ( $res ) ) {
if ( $row[0] == 1 )
$delete_em[] = $events[$i];
}
dbi_free_result ( $res );
}
}
// Now delete events that were just for this user
for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
dbi_query ( "DELETE FROM webcal_entry WHERE cal_id = " . $delete_em[$i] );
}
// Delete user participation from events
dbi_query ( "DELETE FROM webcal_entry_user WHERE cal_login = '$user'" );
// Delete preferences
dbi_query ( "DELETE FROM webcal_user_pref WHERE cal_login = '$user'" );
// Delete from groups
dbi_query ( "DELETE FROM webcal_group_user WHERE cal_login = '$user'" );
// Delete bosses & assistants
dbi_query ( "DELETE FROM webcal_asst WHERE cal_boss = '$user'" );
dbi_query ( "DELETE FROM webcal_asst WHERE cal_assistant = '$user'" );
// Delete user's views
$delete_em = [];
$res = dbi_query ( "SELECT cal_view_id FROM webcal_view " .
"WHERE cal_owner = '$user'" );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$delete_em[] = $row[0];
}
dbi_free_result ( $res );
}
for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
dbi_query ( "DELETE FROM webcal_view_user WHERE cal_view_id = " .
$delete_em[$i] );
}
dbi_query ( "DELETE FROM webcal_view WHERE cal_owner = '$user'" );
// Delete layers
dbi_query ( "DELETE FROM webcal_user_layers WHERE cal_login = '$user'" );
// Delete any layers other users may have that point to this user.
dbi_query ( "DELETE FROM webcal_user_layers WHERE cal_layeruser = '$user'" );
}
// Functions we don't use with this file:
function user_update_user ( $user, $firstname, $lastname, $email, $admin,
$enabled = 'Y' ) {
global $error;
$error = 'User admin not supported.'; return false;
}
function user_update_user_password ( $user, $password ) {
global $error;
$error = 'User admin not supported.'; return false;
}
function user_add_user ( $user, $password, $firstname, $lastname, $email, $admin, $enabled = 'Y' ) {
global $error;
$error = 'User admin not supported.'; return false;
}
?>