File: user.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (870 lines) | stat: -rw-r--r-- 21,971 bytes parent folder | download
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
<?php
/* 	OpenDb - Open Media Lending Database
	Copyright (C) 2001,2002 by Jason Pell

	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_once("./functions/database.php");
include_once("./functions/logging.php");
include_once("./functions/utils.php");
include_once("./functions/address_type.php");

function get_user_types_rs($user_type_r, $checked_user_type=NULL)
{
	for($i=0; $i<count($user_type_r); $i++)
	{
		$user_type_rs[] = 
					array(
						'value'=>$user_type_r[$i], 
						'display'=>get_usertype_description($user_type_r[$i]),
						'checked_ind'=>($user_type_r[$i] == $checked_user_type)?'Y':'N');
	}
	
	if(is_array($user_type_rs))
		return $user_type_rs;
	else// make sure we at least have an empty array.
		return array();
}

/**
	Return an array of owner types to be used as argument
	to fetch_user_rs(...) and fetch_user_cnt(...)
*/
function get_owner_user_types_r()
{
	return array('A','N');
}

/**
	Fetch users which can borrow items.
*/
function get_borrower_user_types_r()
{
	return array('A','N','B');
}

function get_changeuser_user_types_r()
{
	return array('N','B');
}

/**
	Fetch users which can borrow items.
*/
function get_review_user_types_r()
{
	return array('A','N','B');
}

function get_editinfo_user_types_r()
{
	return array('A', 'N', 'B');
}

/**
* An array of all user type's - for use in User Type radio button grid widget
*/
function get_user_types_r()
{
	return array('A', 'N', 'B', 'G');
}

/**
* Validates that the $uid is of a user, which an administrator
* can change to.
*/
function is_user_changeuser($uid, $type=NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	
	if(in_array($type, get_changeuser_user_types_r()))
		return TRUE;
	else
		return FALSE;
}

/**
*/
function is_user_allowed_to_own($uid, $type=NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	
	if(in_array($type, get_owner_user_types_r()))
		return TRUE;
	else
		return FALSE;
}

/**
*/
function is_user_allowed_to_borrow($uid, $type=NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	
	if(in_array($type, get_borrower_user_types_r()))
		return TRUE;
	else
		return FALSE;
}

function is_user_allowed_to_review($uid, $type=NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	
	if(in_array($type, get_review_user_types_r()))
		return TRUE;
	else
		return FALSE;
}

function is_user_allowed_to_edit_info($uid, $type=NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	
	if(in_array($type, get_editinfo_user_types_r()))
		return TRUE;
	else
		return FALSE;
}

/*
* 
*/
function is_usertype_valid($usertype)
{
	if($usertype == 'N' || $usertype == 'A' || $usertype == 'B' || $usertype == 'G')
		return TRUE;
	else
		return FALSE;
}

function get_usertype_description($usertype)
{
	global $LANG_VARS;
	
	if($usertype == 'N')
		return $LANG_VARS['normal'];
	else if($usertype == 'A')
		return $LANG_VARS['administrator'];
	else if($usertype == 'B')
		return $LANG_VARS['borrower'];
	else if($usertype == 'G')
		return $LANG_VARS['guest'];
	else
		return $LANG_VARS['unknown'];
}

/*
* Specify a IN clause, of user_type's that the current
* user_type is of at least that level.  For instance a
* normal user qualifies, if the minimum user type is
* G, B or N
*/
function get_min_user_type_r($user_type)
{
	switch($user_type)
	{
		case 'G':
			return array('G');
		case 'B':
			return array('G','B');
		case 'N':
			return array('G','B','N');
		case 'A':
			return array('G','B','N','A');
		default:
			return array(''); // invalid usertype.
	}
}

/*
* Returns 	-1 if new_user_type is lesser than old_user_type
* 			0 If no change
* 			1 Iff new_user_type is better than old_user_type
*/
function user_type_cmp($old_user_type, $new_user_type)
{
	if($old_user_type == $new_user_type)
		return 0;
	else if($old_user_type == 'G')
	{
	 	if($new_user_type == 'B' || $new_user_type == 'N' || $new_user_type == 'A')
			return 1;
		else
			return -1; // should never happen.
	}
	else if($old_user_type == 'B')
	{
		if($new_user_type == 'N' || $new_user_type == 'A')
			return 1;
		else
			return -1;
	}
	else if($old_user_type == 'N')
	{
		if($new_user_type == 'A')
			return 1;
		else
			return -1;
	}
	else
		return FALSE;
}

/*
* If a password is set for a user, they are
* considered active.
*/
function is_user_active($uid)
{
	$query = "SELECT active_ind FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);

		if($found['active_ind'] == 'Y')
		{
			return TRUE;
		}
	}
	
	//else
	return FALSE;
}

//
// Will return true if $uid is an admin user.
//
function is_user_admin($uid, $type = NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	if ($type == 'A')
		return TRUE;
	else
		return FALSE;
}

//
// Will return true if $uid is an admin user.
//
function is_user_borrower($uid, $type = NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	if ($type == 'B')
		return TRUE;
	else
		return FALSE;
}

//
// Will return true if $uid is a guest user.
//
function is_user_guest($uid, $type = NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	if ($type == 'G')
		return TRUE;
	else
		return FALSE;
}

//
// Will return true if $uid is normal user.
//
function is_user_normal($uid, $type = NULL)
{
	if(strlen($type)==0)
		$type = fetch_user_type($uid);
	if ($type == 'N')//relies on fetch_user_type returning 'N' if type is empty.
		return TRUE;
	else
		return FALSE;
}

/**
	Checks if uid actually exists.

	By default do not do case-insensitive check.  Generally $case=FALSE
	will be used in user_admin.php, to ensure two users with uid only
	differing in case are not created.  But because the rest of the logic
	in opendb does case-sensitive comparisons, we are going to do it here
	by default as well.
	
	$doUpperCheck	Do a case-sensitive comparison.  If false, the SQL query
					will have a UPPER added around user_id and $uid parameter.
*/
function is_user_valid($uid, $doUpperCheck=FALSE)
{
	// Do a pre-emptive check!
	if(strlen($uid)==0)
		return FALSE;
	
	// CASE SENSITIVE comparison by default.  This is the fastest
	// to execute as well.
	if($doUpperCheck === FALSE)
		$query = "SELECT 'x' FROM user WHERE user_id = '$uid'";
	else
		$query = "SELECT 'x' FROM user WHERE UPPER(user_id) = '".strtoupper($uid)."'";
		
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return TRUE;
	}
	//else
	return FALSE;
}

//
// This will return the FULL NAME of the matching user id,
// or if this is empty, the $uid will be returned instead.
// It does not check that the uid is valid, too bad if it is not.
//
function fetch_user_name($uid)
{
	$query = "SELECT IF(LENGTH(fullname)>0,fullname,user_id) as fullname FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return trim($found['fullname']);
	}
	//else
	return FALSE;
}

/**
* Since the EMAIL address is used throughout the system, a specialised
* abstraction of the user_address structure is required.  There is a general
* assumption that the EMAIL address will ALWAYS be present, and all current
* code generally assumes this.
*/
function fetch_user_email($uid)
{
	global $CONFIG_VARS;
	
	if(is_array($CONFIG_VARS['email.user_address_attribute']) && 
			isset($CONFIG_VARS['email.user_address_attribute']['s_attribute_type']))
	{
		$address_type_r = fetch_user_address_type_r($uid, 'EMAIL');
		if(is_not_empty_array($address_type_r))
		{
			$email_addr = fetch_user_address_attribute_val(
							$address_type_r['sequence_number'],
							$CONFIG_VARS['email.user_address_attribute']['s_attribute_type']);
		
			if($email_addr !== FALSE)
			{
				return $email_addr;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}
	else
	{
		return FALSE;
	}
}

// returns the user's specified language
function fetch_user_language($uid)
{
	// the name is the unique column in the user table.
	$query = "SELECT language FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found['language'];
	}
	//else
	return FALSE;
}

// returns the user's specified theme
function fetch_user_theme($uid)
{
	// the name is the unique column in the user table.
	$query = "SELECT theme FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found['theme'];
	}
	//else
	return FALSE;
}

function fetch_user_type($uid)
{
	// the name is the unique column in the user table.
	$query = "SELECT if(length(type)>0,type,'N') as type FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found['type'];
	}
	//else
	return FALSE;
}

//
// This returns the location of the user as a string.  Or FALSE if not found.
// 
function fetch_user_lastvisit($uid)
{
	// the name is the unique column in the user table.
	$query = "SELECT lastvisit FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found['lastvisit'];
	}
	//else
	return FALSE;
}

/**
	Return a resultset of all users, ordered by user_id
	user_id,fullname,type,lastvisit.  Will
	replace fullname with user_id if not defined. We only
	want to replace fullname with user_id if empty in this function
	because it is used for readonly operations.  The fetch_user_r
	is used to populate update forms, so the fullname must be 
	included with its current value, empty or not!

	@param $user_types		Specifies a list of usertypes to return in SQL statment.
							It is specified as an array.  Example: array('A','N','B','G')
							
							NOTE: If a set of user_types is specified, it is assumed that
							only ACTIVE user's should be returned, so a active_ind = 'Y'
							clause is appended to the WHERE.
							 
	@param $order_by		Specify an order by column. Options are: user_id, fullname,
							location, type, email, lastvisit
* 	@param $exclude_user	A neat way to exclude one user from the list. Does not 
* 							currently support excluding more than one user.
*/
function fetch_user_rs($user_types=NULL, $order_by=NULL, $sortorder="ASC", $include_deactivated_users=FALSE, $exclude_user=NULL, $start_index=NULL, $items_per_page=NULL)
{
	global $LANG_VARS;
	
	// Uses the special 'zero' value lastvisit = 0 to test for default date value.
	$query = "SELECT user_id, IF(LENGTH(fullname)>0,fullname,user_id) as fullname, IF(active_ind = 'Y', IF(LENGTH(type)>0,type,'N'),'D') as type, language, theme, IF(lastvisit <> 0,UNIX_TIMESTAMP(lastvisit),'') as lastvisit FROM user";

	// List all users who can borrow records.
	$user_type_clause = format_sql_in_clause($user_types);
	if($user_type_clause != NULL)
		$where_clause = "IF(LENGTH(type)>0,type,'N') IN($user_type_clause)";

	if(strlen($exclude_user)>0)
	{
		if(strlen($where_clause)>0)
			$where_clause .= " AND ";
		$where_clause .= "user_id NOT IN ('$exclude_user')";
	}
	
	if($include_deactivated_users !== TRUE)
	{
		if(strlen($where_clause)>0)
			$where_clause .= " AND ";
		$where_clause .= " active_ind = 'Y' ";
	}
	
	if(strlen($where_clause)>0)
		$query .= " WHERE $where_clause";
	
	// For simplicity sake!
	if(strlen($order_by)==0)
		$order_by = "fullname";

	if($order_by === "user_id")
		$query .= " ORDER BY user_id $sortorder";
	else if($order_by === "fullname")
		$query .= " ORDER BY fullname $sortorder, user_id";
	else if($order_by === "type")
		$query .= " ORDER BY type $sortorder, fullname, user_id";
	else if($order_by === "lastvisit")
		$query .= " ORDER BY lastvisit $sortorder, fullname, user_id";

	if(is_numeric($start_index) && is_numeric($items_per_page))
		$query .= ' LIMIT ' .$start_index. ', ' .$items_per_page;

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
		return $result;
	else
		return FALSE;
}

/**
	Returns a count of items owned by the specified owner_id, or FALSE if no records.
	
	@param $UserTypes = array('A','N','B','G')
			NOTE: If a set of user_types is specified, it is assumed that
			only ACTIVE user's should be returned, so a active_ind = 'Y'
			clause is appended to the WHERE.
*/
function fetch_user_cnt($user_types=NULL, $include_deactivated_users=FALSE, $exclude_user=NULL)
{
	$query = "SELECT count(user_id) as count FROM user";

	// List all users who can borrow records.
	$user_type_clause = format_sql_in_clause($user_types);
	if($user_type_clause != NULL)
		$where_clause = "IF(LENGTH(type)>0,type,'N') IN($user_type_clause)";

	if(strlen($exclude_user)>0)
	{
		if(strlen($where_clause)>0)
			$where_clause .= " AND ";
		$where_clause .= "user_id NOT IN ('$exclude_user')";
	}
	
	if($include_deactivated_users !== TRUE)
	{
		if(strlen($where_clause)>0)
			$where_clause .= " AND ";
		$where_clause .= " active_ind = 'Y' ";
	}
	
	if(strlen($where_clause)>0)
		$query .= " WHERE $where_clause";
	
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!==FALSE)
			return $found['count'];
	}
	
	//else
	return FALSE;
}

//
// Returns a single user record.
// user_id,fullname,location,type,email,lastvisit
//
function fetch_user_r($uid)
{
	$query = "SELECT user_id, fullname, if(length(type)>0,type,'N') as type, language, theme, lastvisit FROM user where user_id = '".$uid."'";
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found;
	}
	//else
	return FALSE;
}

//
// Checks password to see if it matches.
//
function validate_user_passwd($uid, $pwd)
{
	$query = "SELECT pwd FROM user WHERE user_id = '$uid'";
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);

		if ($found && md5($pwd)==$found['pwd'])
			return TRUE;
	}

	//else
	return FALSE;
}

/*
*/
function deactivate_user($uid)
{
	global $HTTP_SESSION_VARS;
	
	$query= "UPDATE user SET active_ind = 'N' WHERE user_id = '$uid'";
	$update = run_opendb_query($query);

	if(mysql_affected_rows()>0)
	{
		opendb_log("Deactivated user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to deactivate user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

/*
*/
function activate_user($uid)
{
	global $HTTP_SESSION_VARS;
	
	$query= "UPDATE user SET active_ind = 'Y' WHERE user_id = '$uid'";
	$update = run_opendb_query($query);

	if(mysql_affected_rows()>0)
	{
		opendb_log("Activated user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to activate user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//
// This function will not check if the original password matches before allowing a change, it simply
// makes the change.
//
function update_user_passwd($uid, $pwd)
{
	global $HTTP_SESSION_VARS;
	
	$query = "UPDATE user SET pwd = '".(strlen($pwd)>0?md5($pwd):"")."' WHERE user_id='$uid'";

	$update = run_opendb_query($query);
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if ($rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Updated user password (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update user password (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//
// Will update the last visit value of the specified user.
//
function update_user_lastvisit($uid)
{
	global $HTTP_SESSION_VARS;
	
	$query= "UPDATE user SET lastvisit=now() WHERE user_id = '$uid'";
	$update = run_opendb_query($query);

	// Any failure to update this one should be treated as that, this includes a lastvisit
	// being set to the same value, which should not happen.
	if (mysql_affected_rows()>0)
	{
		opendb_log("Updated user lastvisit (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update user lastvisit (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//
// Will update the User type.
//
function update_user_type($uid, $type)
{
	global $HTTP_SESSION_VARS;
	
	if(strlen($type)==0)
		$type = "N"; //default!
		
	$query="UPDATE user SET type='".$type."' WHERE user_id='$uid'";
	$update = run_opendb_query($query);
	
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if ($rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Updated user type (user_id=$uid, user_type=$type, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update user type (user_id=$uid, user_type=$type, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

/**
	Update User.  
	
		Specify FALSE for type to not attempt to update it.
		Specify FALSE to not update theme.
		Specify FALSE to not update language.
*/
function update_user($uid, $fullname, $language, $theme, $type)
{
	global $HTTP_SESSION_VARS;
	
	// Do not set to default if explicitly set to FALSE
	if($type!==FALSE && strlen($type)==0)
		$type = "N";

	$query = "UPDATE user SET ".
					"fullname='".addslashes($fullname)."'".
					($language!==FALSE?", language='".addslashes($language)."'":"").
					($theme!==FALSE?", theme='".addslashes($theme)."'":"").
					($type!==FALSE?", type='$type'":"").
				" WHERE user_id = '$uid'";

	$update = run_opendb_query($query);
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if ($rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Updated user details (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update user details (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//
// Insert a New user.  Does not check if $uid exists already, before doing so.
// This relies on the user_id UNIQUE constraint.  The $uid is the updating user.
// Will do md5($pwd) before inserting...
//
function insert_user($uid, $fullname, $pwd, $type, $language, $theme)
{
	global $HTTP_SESSION_VARS;
	
	if(strlen($type)==0)
		$type = "N"; //default!

	$query = "INSERT INTO user (user_id, fullname, pwd, type, language, theme, active_ind)".
				"VALUES('".$uid."',".
						"'".addslashes($fullname)."',".
						"'".md5($pwd)."',".
						"'".$type."',".
						"'".addslashes($language)."',".
						"'".addslashes($theme)."',".
						"'Y')";

	$insert = run_opendb_query($query);
	if(mysql_affected_rows()>0)
	{
		opendb_log("Inserted new user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to insert user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//
// Delete user.  Assumes validation has already been performed.
//
function delete_user($uid)
{
	global $HTTP_SESSION_VARS;
	
	$query= "DELETE FROM user WHERE user_id = '$uid'";
	$delete = run_opendb_query($query);
	if (mysql_affected_rows()>0)
	{
		opendb_log("Deleted user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to delete user (user_id=$uid, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

//---------------------------------------------------
// Utilities
//---------------------------------------------------

// Randomly generates a password of $length characters
function generate_password($length)
{
	// length should be at minimum 4 characters
	if($length<4) $length=5;

	// seed random generator with system time
	srand((double)microtime()*1000000); 

	// make a string containing acceptable characters 
	$symbols = "abcdefghijklmnopqrstuvwxyz" 
		  ."ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
                  ."0123456789"; 

	// loop for $length 
	for($ix = 0; $ix < $length; $ix++) 
	{ 
		// pick random symbol
		$randomNum   = rand(0,strlen($symbols)); 
		$randomChar  = substr($symbols,$randomNum,1); 

		// append random symbol to password
		$randomPass .= $randomChar; 
	} 

	// now returns our random password
	return $randomPass; 
}
?>