ultimix
user_controller.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 
27 
38  var $ContextSet = false;
39  var $EventManager = false;
40  var $Messages = false;
42  var $Security = false;
43  var $UserAccess = false;
44  var $UserAlgorithms = false;
46 
58 
70 
82 
93  var $EmailSender = 'System';
94 
105  var $SystemEmail = 'ultimix@localhost';
106 
121  function load_settings()
122  {
123  try
124  {
125  $Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
126  $Settings->load_package_settings( 'page::page_composer' , 'last' , 'cf_site' );
127  $this->EnableRegistration = intval( $Settings->get_setting( 'enable_registration' , 1 ) );
128  $this->RegistrationConfirm = intval( $Settings->get_setting( 'registration_confirm' , 1 ) );
129  $this->EmailSender = $Settings->get_setting( 'email_sender' , $this->EmailSender );
130  $this->SystemEmail = $Settings->get_setting( 'system_email' , $this->SystemEmail );
131  }
132  catch( Exception $e )
133  {
134  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
135  }
136  }
137 
149  {
150  try
151  {
152  $this->ContextSet = get_package( 'gui::context_set' , 'last' , __FILE__ );
153  $this->EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
154  $this->Messages = get_package( 'page::messages' , 'last' , __FILE__ );
155  $this->PageComposerUtilities = get_package( 'page::page_composer_utilities' , 'last' , __FILE__ );
156  $this->Security = get_package( 'security' , 'last' , __FILE__ );
157  }
158  catch( Exception $e )
159  {
160  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
161  }
162  }
163 
174  function __construct()
175  {
176  try
177  {
178  $this->load_common_packages();
179 
180  $this->UserAccess = get_package( 'user::user_access' , 'last' , __FILE__ );
181  $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
182  $this->UserControllerUtilities = get_package(
183  'user::user_controller::user_controller_utilities' , 'last' , __FILE__
184  );
185  }
186  catch( Exception $e )
187  {
188  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
189  }
190  }
191 
214  function do_login( $Login , &$Options )
215  {
216  try
217  {
218  $User = $this->UserAccess->get_user( $Login );
219 
220  if( $this->UserAlgorithms->user_banned( $Login ) )
221  {
222  $this->Messages->add_error_message( 'user_is_banned_to '.$User->banned_to );
223  return;
224  }
225 
226  $id = get_field( $User , 'id' );
227  $this->EventManager->trigger_event( 'on_before_login' , array( 'id' => $id ) );
228  $this->UserAlgorithms->login( $Login , $id );
229  $this->EventManager->trigger_event( 'on_after_login' , array( 'id' => $id ) );
230 
231  $this->PageComposerUtilities->redirect_using_map( $Options );
232  }
233  catch( Exception $e )
234  {
235  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
236  }
237  }
238 
257  function login( &$Options )
258  {
259  try
260  {
261  $Login = $this->Security->get_gp( 'login' , 'string' );
262  $UserPassword = $this->Security->get_gp( 'password' , 'string' );
263 
264  $UserExists = $this->UserAlgorithms->user_exists( $Login );
265  $UserActive = $AuthValid = true;
266 
267  if( $UserExists )
268  {
269  if( $UserActive = $this->UserAlgorithms->user_active( $Login ) )
270  {
271  if( $AuthValid = $this->UserAlgorithms->validate_auth( $Login , $UserPassword ) )
272  {
273  $this->do_login( $Login , $Options );
274  }
275  }
276  }
277 
278  $this->UserControllerUtilities->handle_login_errors( $UserExists , $UserActive , $AuthValid );
279  }
280  catch( Exception $e )
281  {
282  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
283  }
284  }
285 
304  function logout( &$Options )
305  {
306  try
307  {
308  $this->UserAlgorithms->logout();
309  }
310  catch( Exception $e )
311  {
312  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
313  }
314  }
315 
334  function activate_user( &$Options )
335  {
336  try
337  {
338  if( $this->EnableRegistration === 1 )
339  {
340  $Hash = $this->Security->get_gp( 'hash' , 'command' , false );
341  if( $Hash !== false )
342  {
343  $this->UserAccess->activate_user( $Hash );
344  $this->Messages->add_success_message( 'user_was_activated' );
345  }
346  }
347  }
348  catch( Exception $e )
349  {
350  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
351  }
352  }
353 
372  function admin_activate_user( &$Options )
373  {
374  try
375  {
376  $Ids = $this->Security->get_gp( 'ids' , 'integer' );
377 
378  if( is_array( $Ids ) === false )
379  {
380  $Ids = array( $Ids );
381  }
382 
383  $this->UserAccess->activate_users( $Ids );
384  }
385  catch( Exception $e )
386  {
387  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
388  }
389  }
390 
409  function admin_deactivate_user( &$Options )
410  {
411  try
412  {
413  $Ids = $this->Security->get_gp( 'ids' , 'integer' );
414 
415  if( is_array( $Ids ) === false )
416  {
417  $Ids = array( $Ids );
418  }
419 
420  $this->UserAccess->deactivate_users( $Ids );
421  }
422  catch( Exception $e )
423  {
424  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
425  }
426  }
427 
446  function restore_password( &$Options )
447  {
448  try
449  {
450  $Login = $this->Security->get_gp( 'rlogin' , 'string' );
451  $NewPassword = $this->UserAlgorithms->generate_password();
452 
453  $CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
454  $Message = str_replace(
455  '{new_password}' , $NewPassword ,
456  $CachedMultyFS->get_template( __FILE__ , 'password_restoration_email.tpl' )
457  );
458 
459  $this->UserControllerUtilities->send_email(
460  $this->SystemEmail , $this->EmailSender , $Message ,
461  '{lang:password_restoration}' , get_field( $this->UserAccess->get_user( $Login ) , 'email' )
462  );
463 
464  $this->UserAccess->reset_password( $Login , $NewPassword );
465  }
466  catch( Exception $e )
467  {
468  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
469  }
470  }
471 
487  {
488  try
489  {
490  $User = $this->UserAlgorithms->get_user();
491  $Login = get_field( $User , 'login' );
492  $Password = $this->Security->get_gp( 'password' , 'string' );
493  $PasswordConfirmation = $this->Security->get_gp( 'password_confirmation' , 'string' );
494 
495  if( $Password == $PasswordConfirmation )
496  {
497  $this->UserAccess->reset_password( $Login , $Password );
498  }
499  }
500  catch( Exception $e )
501  {
502  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
503  }
504  }
505 
524  private function get_update_data()
525  {
526  try
527  {
528  $UserEmail = $this->Security->get_gp( 'email' , 'email' );
529 
530  $Site = $this->Security->get_gp( 'site' , 'string' , '' );
531 
532  $About = $this->Security->get_gp( 'about' , 'string' , '' );
533 
534  return( array( $UserEmail , $Site , $About ) );
535  }
536  catch( Exception $e )
537  {
538  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
539  }
540  }
541 
564  private function need_reset_password( $Login )
565  {
566  try
567  {
568  $PermitAlgorithms = get_package( 'permit::permit_algorithms' , 'last' , __FILE__ );
569  $HasPermit = $PermitAlgorithms->object_has_permit( false , 'user' , 'user_manager' );
570  $ChangePassword = $HasPermit || ( $this->Security->get_gp( 'current_password' , 'set' ) &&
571  strlen( $CurrentPassword = $this->Security->get_gp( 'current_password' , 'string' ) ) &&
572  $this->UserAlgorithms->validate_auth( $Login , $CurrentPassword ) );
573 
574  return( $ChangePassword );
575  }
576  catch( Exception $e )
577  {
578  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
579  }
580  }
581 
600  function update_user( &$Options )
601  {
602  try
603  {
604  $User = $this->UserAlgorithms->get_user();
605 
606  list( $UserEmail , $Site , $About ) = $this->get_update_data();
607 
608  $Record = array( 'email' => $UserEmail , 'site' => $Site , 'about' => $About );
609  $this->update( get_field( $User , 'id' ) , $Record );
610 
611  $ChangePassword = $this->need_reset_password( $Login );
612 
613  if( $ChangePassword )
614  {
615  $this->try_reset_password();
616  }
617 
618  $this->Messages->add_success_message( 'user_update_was_completed' );
619  }
620  catch( Exception $e )
621  {
622  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
623  }
624  }
625 
644  private function send_confirmation( $ActivationHash )
645  {
646  try
647  {
648  $CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
649  $Message = $CachedMultyFS->get_template( __FILE__ , 'confirm_registration_email.tpl' );
650  $Message = str_replace( '{hash}' , $ActivationHash , $Message );
651 
652  $this->send_email( $Message , '{lang:registration_confirm}' );
653  }
654  catch( Exception $e )
655  {
656  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
657  }
658  }
659 
678  private function create_user_object_and_permits()
679  {
680  try
681  {
682  $SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
683 
684  $Record = $SecurityParser->parse_http_parameters(
685  'login:string;password:string;email:email;name:string;'.
686  'sex:integer;site:string,allow_not_set;about:string,allow_not_set'
687  );
688 
689  list( $id , $Hash ) = $this->UserAccess->create( $Record );
690 
691  $this->UserControllerUtilities->add_default_permits( $id );
692 
693  return( $Hash );
694  }
695  catch( Exception $e )
696  {
697  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
698  }
699  }
700 
719  private function send_confirmation_if_necessary( $ActivationHash )
720  {
721  try
722  {
723  if( $this->RegistrationConfirm &&
724  $this->Security->get_p( 'active_permanently' , 'command' , false ) !== 'on' )
725  {
726  $this->send_confirmation( $ActivationHash );
727  }
728  else
729  {
730  $this->UserAccess->activate_user( $ActivationHash );
731  $this->RegistrationConfirm = false;
732  }
733  }
734  catch( Exception $e )
735  {
736  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
737  }
738  }
739 
755  private function register_do()
756  {
757  try
758  {
759  $Hash = $this->create_user_object_and_permits();
760 
761  $this->send_confirmation_if_necessary( $Hash );
762 
763  $this->RegistrationWasPassed = true;
764 
765  $Login = $this->Security->get_gp( 'login' , 'string' );
766 
767  $this->EventManager->trigger_event( 'on_after_registration' , array( 'login' => $Login ) );
768  }
769  catch( Exception $e )
770  {
771  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
772  }
773  }
774 
793  function registration( &$Options )
794  {
795  try
796  {
797  $PermitAlgorithms = get_package( 'permit::permit_algorithms' , 'last' , __FILE__ );
798  $HasPermit = $PermitAlgorithms->object_has_permit( false , 'user' , 'user_manager' );
799 
800  if( $this->EnableRegistration === 1 || $HasPermit )
801  {
802  if( $this->UserControllerUtilities->handle_register_errors() )
803  {
804  return;
805  }
806 
807  $this->register_do();
808  }
809  }
810  catch( Exception $e )
811  {
812  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
813  }
814  }
815 
834  function set_avatar( &$Options )
835  {
836  try
837  {
838  $FileInputController = get_package( 'file_input::file_input_controller' , 'last' , __FILE__ );
839 
840  if( $FileInputController->UploadedFile )
841  {
842  $User = $this->UserAlgorithms->get_user();
843 
844  $Avatar = get_field( $User , 'avatar' );
845  if( $Avatar > 0 )
846  {
847  $FileInputAccess = get_package( 'file_input::file_input_access' , 'last' , __FILE__ );
848  $FileInputAccess->delete( $Avatar );
849  }
850 
851  $Login = get_field( $User , 'login' );
852  $FileId = get_field( $FileInputController->UploadedFile , 'id' );
853  $this->UserAccess->set_avatar( $Login , $FileId );
854  }
855  }
856  catch( Exception $e )
857  {
858  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
859  }
860  }
861 
880  function controller( &$Options )
881  {
882  try
883  {
884  $this->ContextSet->add_contexts(
885  $Options , dirname( __FILE__ ) , $this->UserControllerUtilities->get_configs()
886  );
887 
888  if( $this->ContextSet->execute( $Options , $this , __FILE__ ) )
889  {
890  return;
891  }
892  }
893  catch( Exception $e )
894  {
895  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
896  }
897  }
898  }
899 ?>