ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

user_algorithms.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class user_algorithms_1_0_0{
27
38 var $User = false;
39
50 var $Security = false;
51 var $UserAccess = false;
52
67 function __construct()
68 {
69 try
70 {
71 $this->Security = get_package( 'security' , 'last' , __FILE__ );
72 $this->UserAccess = get_package( 'user::user_access' , 'last' , __FILE__ );
73 }
74 catch( Exception $e )
75 {
76 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
77 }
78 }
79
102 function get_by_id( $id )
103 {
104 try
105 {
106 $id = $this->Security->get( $id , 'integer' );
107
108 $Users = $this->UserAccess->unsafe_select( $this->UserAccess->NativeTable.".id = $id" );
109
110 if( count( $Users ) === 0 || count( $Users ) > 1 )
111 {
112 throw( new Exception( 'User with id '.$id.' was not found' ) );
113 }
114 else
115 {
116 return( $Users[ 0 ] );
117 }
118 }
119 catch( Exception $e )
120 {
121 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
122 }
123 }
124
135 function session_start()
136 {
137 if( session_id() == '' )
138 {
139 session_start();
140 }
141 }
142
161 function logged_in()
162 {
163 try
164 {
165 $this->session_start();
166
167 if( isset( $_SESSION[ 'login' ] ) )
168 {
169 return( true );
170 }
171
172 return( false );
173 }
174 catch( Exception $e )
175 {
176 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
177 }
178 }
179
199 function get_login()
200 {
201 try
202 {
203 $this->session_start();
204
205 if( $this->logged_in() )
206 {
207 /* залогинен, значит опознаем его */
208 return( $_SESSION[ 'login' ] );
209 }
210 else
211 {
212 /* не залогинен значит гость */
213 return( 'guest' );
214 }
215
216 return( false );
217 }
218 catch( Exception $e )
219 {
220 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
221 }
222 }
223
242 function get_id()
243 {
244 try
245 {
246 $this->session_start();
247
248 if( $this->logged_in() )
249 {
250 return( $_SESSION[ 'user_id' ] );
251 }
252 else
253 {
254 /* guest id will be returned */
255 return( $this->UserAccess->GuestUserId );
256 }
257
258 return( false );
259 }
260 catch( Exception $e )
261 {
262 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
263 }
264 }
265
284 function get_user()
285 {
286 try
287 {
288 if( $this->User === false )
289 {
290 $this->User = $this->get_by_id( $this->get_id() );
291 }
292
293 return( $this->User );
294 }
295 catch( Exception $e )
296 {
297 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
298 }
299 }
300
323 function login( $Login , $id )
324 {
325 try
326 {
327 if( $this->user_active( $Login ) )
328 {
329 $this->session_start();
330
331 $_SESSION[ 'login' ] = $Login;
332 $_SESSION[ 'user_id' ] = $id;
333
334 $this->User = false;
335 }
336 else
337 {
338 return( false );
339 }
340 }
341 catch( Exception $e )
342 {
343 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
344 }
345 }
346
361 function logout()
362 {
363 try
364 {
365 $this->session_start();
366
367 unset( $_SESSION[ 'login' ] );
368 unset( $_SESSION[ 'id' ] );
369
370 $this->User = false;
371 }
372 catch( Exception $e )
373 {
374 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
375 }
376 }
377
400 function user_active( $Login )
401 {
402 try
403 {
404 $User = $this->UserAccess->get_user( $Login );
405
406 return( $User->active == 'active' );
407 }
408 catch( Exception $e )
409 {
410 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
411 }
412 }
413
436 function user_banned( $Login )
437 {
438 try
439 {
440 $User = $this->UserAccess->get_user( $Login );
441
442 return( $User->banned );
443 }
444 catch( Exception $e )
445 {
446 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
447 }
448 }
449
472 function get_password_hash( $Login = false )
473 {
474 try
475 {
476 if( $Login === false )
477 {
478 $Login = $this->get_login();
479 }
480
481 $User = $this->UserAccess->get_user( $Login );
482
483 return( $User->password );
484 }
485 catch( Exception $e )
486 {
487 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
488 }
489 }
490
513 function user_exists( $Login )
514 {
515 try
516 {
517 $Login = $this->Security->get( $Login , 'string' );
518
519 $this->UserAccess->get_user( $Login );
520
521 return( true );
522 }
523 catch( Exception $e )
524 {
525 return( false );
526 }
527 }
528
547 function delete( $ids )
548 {
549 try
550 {
551 $ids = $this->Security->get( $ids , 'integer_list' );
552
553 $Users = $this->UserAccess->unsafe_select( $this->UserAccess->NativeTable.
554 ".id IN( $ids ) and `system` = 0" );
555
556 $this->UserAccess->delete( implode( ',' , get_field_ex( $Users , 'id' ) ) );
557 }
558 catch( Exception $e )
559 {
560 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
561 }
562 }
563
586 function email_exists( $Email )
587 {
588 try
589 {
590 $Email = $this->Security->get( $Email , 'email' );
591
592 $Records = $this->UserAccess->unsafe_select( "email LIKE '$Email'" );
593
594 return( count( $Records ) == 1 );
595 }
596 catch( Exception $e )
597 {
598 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
599 }
600 }
601
632 function validate_auth( $Login , $Password , $HashPassed = false )
633 {
634 try
635 {
636 $Login = $this->Security->get( $Login , 'string' );
637 $Password = $this->Security->get( $Password , 'string' );
638
639 $User = $this->UserAccess->get_user( $Login );
640
641 if( $HashPassed )
642 {
643 return( $Password == get_field( $User , 'password' ) );
644 }
645 else
646 {
647 return( md5( $Password ) == get_field( $User , 'password' ) );
648 }
649 }
650 catch( Exception $e )
651 {
652 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
653 }
654 }
655
678 function generate_password( $Length = 10 )
679 {
680 try
681 {
682 $Letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
683 $c = strlen( $Letters );
684
685 $Password = '';
686
687 for( $i = 0 ; $i < $Length ; $i++ )
688 {
689 $Password .= $Letters[ rand( 0 , $c - 1 ) ];
690 }
691
692 return( $Password );
693 }
694 catch( Exception $e )
695 {
696 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
697 }
698 }
699 }
700
701 ?>