ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAccountRegistrationGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
18 require_once './Services/Registration/classes/class.ilRegistrationSettings.php';
19 require_once "./Services/User/classes/class.ilUserAgreement.php";
20 
22 {
23  var $ctrl;
24  var $tpl;
26 
27  protected $code_was_used = false;
28 
29  public function __construct()
30  {
31  global $ilCtrl,$tpl,$lng;
32 
33  $this->tpl =& $tpl;
34 
35  $this->ctrl =& $ilCtrl;
36  $this->ctrl->saveParameter($this,'lang');
37 
38  $this->lng =& $lng;
39  $this->lng->loadLanguageModule('registration');
40 
41  $this->registration_settings = new ilRegistrationSettings();
42  }
43 
44  public function executeCommand()
45  {
46  global $ilErr, $tpl;
47 
48  if($this->registration_settings->getRegistrationType() == IL_REG_DISABLED)
49  {
50  $ilErr->raiseError($this->lng->txt('reg_disabled'),$ilErr->FATAL);
51  }
52 
53  $next_class = $this->ctrl->getNextClass($this);
54  $cmd = $this->ctrl->getCmd();
55 
56  switch($next_class)
57  {
58  default:
59  if($cmd)
60  {
61  $this->$cmd();
62  }
63  else
64  {
65  $this->displayForm();
66  }
67  break;
68  }
69  $tpl->show();
70  return true;
71  }
72 
73  public function displayForm()
74  {
75  global $lng;
76 
77  $this->tpl->addBlockFile("CONTENT", "content", "tpl.usr_registration.html");
78  $this->tpl->addBlockFile("STATUSLINE", "statusline", "tpl.statusline.html");
79 
80  $this->tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("registration"));
81 
82  // language selection
83  $this->tpl->setVariable("FORMACTION",$this->ctrl->getFormAction($this));
84  $this->tpl->setVariable("TXT_OK",$lng->txt("ok"));
85  $this->tpl->setVariable("TXT_CHOOSE_LANGUAGE", $lng->txt("choose_language"));
86  $this->ctrl->getFormAction($this);
87  foreach ($lng->getInstalledLanguages() as $lang_key)
88  {
89  $this->tpl->setCurrentBlock("languages");
90  $this->tpl->setVariable("LINK_LANG",$this->ctrl->getLinkTarget($this,'displayForm'));
91  $this->tpl->setVariable("LANG_NAME",
92  ilLanguage::_lookupEntry($lang_key, "meta", "meta_l_".$lang_key));
93  $this->tpl->setVariable("LANG_ICON", $lang_key);
94  $this->tpl->setVariable("BORDER", 0);
95  $this->tpl->setVariable("VSPACE", 0);
96  $this->tpl->parseCurrentBlock();
97  }
98 
99  if(!$this->form)
100  {
101  $this->__initForm();
102  }
103  $this->tpl->setVariable("FORM", $this->form->getHTML());
104  }
105 
106  protected function __initForm()
107  {
108  global $lng, $ilUser;
109 
110  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
111  $this->form = new ilPropertyFormGUI();
112  $this->form->setFormAction($this->ctrl->getFormAction($this));
113 
114 
115  // user defined fields
116 
117  $user_defined_data = $ilUser->getUserDefinedData();
118 
119  include_once './Services/User/classes/class.ilUserDefinedFields.php';
120  $user_defined_fields =& ilUserDefinedFields::_getInstance();
121  $custom_fields = array();
122  foreach($user_defined_fields->getRegistrationDefinitions() as $field_id => $definition)
123  {
124  if($definition['field_type'] == UDF_TYPE_TEXT)
125  {
126  $custom_fields["udf_".$definition['field_id']] =
127  new ilTextInputGUI($definition['field_name'], "udf_".$definition['field_id']);
128  $custom_fields["udf_".$definition['field_id']]->setValue($user_defined_data["f_".$field_id]);
129  $custom_fields["udf_".$definition['field_id']]->setMaxLength(255);
130  $custom_fields["udf_".$definition['field_id']]->setSize(40);
131  }
132  else if($definition['field_type'] == UDF_TYPE_WYSIWYG)
133  {
134  $custom_fields["udf_".$definition['field_id']] =
135  new ilTextAreaInputGUI($definition['field_name'], "udf_".$definition['field_id']);
136  $custom_fields["udf_".$definition['field_id']]->setValue($user_defined_data["f_".$field_id]);
137  $custom_fields["udf_".$definition['field_id']]->setUseRte(true);
138  }
139  else
140  {
141  $custom_fields["udf_".$definition['field_id']] =
142  new ilSelectInputGUI($definition['field_name'], "udf_".$definition['field_id']);
143  $custom_fields["udf_".$definition['field_id']]->setValue($user_defined_data["f_".$field_id]);
144  $custom_fields["udf_".$definition['field_id']]->setOptions(
145  $user_defined_fields->fieldValuesToSelectArray($definition['field_values']));
146  }
147  if($definition['required'])
148  {
149  $custom_fields["udf_".$definition['field_id']]->setRequired(true);
150  }
151  }
152 
153  // standard fields
154  include_once("./Services/User/classes/class.ilUserProfile.php");
155  $up = new ilUserProfile();
156  $up->setMode(ilUserProfile::MODE_REGISTRATION);
157  $up->skipGroup("preferences");
158 
159  // add fields to form
160  $up->addStandardFieldsToForm($this->form, NULL, $custom_fields);
161  unset($custom_fields);
162 
163 
164  // user agreement
165 
166  $field = new ilFormSectionHeaderGUI();
167  $field->setTitle($lng->txt("usr_agreement"));
168  $this->form->addItem($field);
169 
170  $field = new ilCustomInputGUI();
171  $field->setHTML('<div id="agreement">'.ilUserAgreement::_getText().'</div>');
172  $this->form->addItem($field);
173 
174  $field = new ilCheckboxInputGUI($lng->txt("accept_usr_agreement"), "usr_agreement");
175  $field->setRequired(true);
176  $field->setValue(1);
177  $this->form->addItem($field);
178 
179  $this->form->addCommandButton("saveForm", $lng->txt("register"));
180  }
181 
182  public function saveForm()
183  {
184  global $ilias, $lng, $rbacadmin, $ilDB, $ilErr, $ilSetting;
185 
186  $this->__initForm();
187  if($this->form->checkInput())
188  {
189  require_once 'Services/User/classes/class.ilObjUser.php';
190 
191  // custom validation
192 
193  $form_valid = true;
194 
195  if(!$this->form->getInput("usr_agreement"))
196  {
197  ilUtil::sendInfo($lng->txt("force_accept_usr_agreement"), true);
198  $form_valid = false;
199  }
200 
201  // validate registration code
202  if($this->registration_settings->registrationCodeRequired() ||
203  $this->registration_settings->getAllowCodes())
204  {
205  $code = $this->form->getInput("usr_registration_code");
206  if($code || $this->registration_settings->registrationCodeRequired())
207  {
208  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
210  {
211  $code_obj = $this->form->getItemByPostVar('usr_registration_code');
212  $code_obj->setAlert($lng->txt('registration_code_not_valid'));
213  $form_valid = false;
214  }
215  }
216  }
217 
218  // validate username
219  $login_obj = $this->form->getItemByPostVar('username');
220  $login = $this->form->getInput("username");
221  if (!ilUtil::isLogin($login))
222  {
223  $login_obj->setAlert($lng->txt("login_invalid"));
224  $form_valid = false;
225  }
226  else if (ilObjUser::_loginExists($login))
227  {
228  $login_obj->setAlert($lng->txt("login_exists"));
229  $form_valid = false;
230  }
231  else if ((int)$ilSetting->get('allow_change_loginname') &&
232  (int)$ilSetting->get('prevent_reuse_of_loginnames') &&
234  {
235  $login_obj->setAlert($lng->txt('login_exists'));
236  $form_valid = false;
237  }
238 
239  // Do some Radius checks
240  $this->__validateRole($this->form->getInput("usr_roles"));
241 
242  if(!$form_valid)
243  {
244  ilUtil::sendFailure($lng->txt('form_input_not_valid'));
245  }
246  else
247  {
248  $password = $this->__createUser();
249  $this->__distributeMails($password);
250  $this->login($password);
251  return true;
252  }
253  }
254 
255  $this->form->setValuesByPost();
256  $this->displayForm();
257  return false;
258  }
259 
260  protected function __createUser()
261  {
262  global $ilSetting, $rbacadmin;
263 
264  $this->userObj = new ilObjUser();
265 
266  include_once("./Services/User/classes/class.ilUserProfile.php");
267  $up = new ilUserProfile();
268  $up->setMode(ilUserProfile::MODE_REGISTRATION);
269 
270  $map = array();
271  $up->skipGroup("preferences");
272  $up->skipGroup("settings");
273  $up->skipGroup("instant_messengers");
274  $up->skipField("password");
275  $up->skipField("birthday");
276  $up->skipField("upload");
277  foreach ($up->getStandardFields() as $k => $v)
278  {
279  if($v["method"])
280  {
281  $method = "set".substr($v["method"], 3);
282  if(method_exists($this->userObj, $method))
283  {
284  if ($k != "username")
285  {
286  $k = "usr_".$k;
287  }
288  $field_obj = $this->form->getItemByPostVar($k);
289  if($field_obj)
290  {
291  $this->userObj->$method($this->form->getInput($k));
292  }
293  }
294  }
295  }
296 
297  $this->userObj->setFullName();
298 
299  $birthday_obj = $this->form->getItemByPostVar("usr_birthday");
300  if ($birthday_obj)
301  {
302  $birthday = $this->form->getInput("usr_birthday");
303  $birthday = $birthday["date"];
304 
305  // when birthday was not set, array will not be substituted with string by ilBirthdayInputGui
306  if(!is_array($birthday))
307  {
308  $this->userObj->setBirthday($birthday);
309  }
310  }
311 
312  // messenger
313  $map = array("icq", "yahoo", "msn", "aim", "skype", "jabber", "voip");
314  foreach($map as $client)
315  {
316  $field = "usr_im_".$client;
317  $field_obj = $this->form->getItemByPostVar($field);
318  if($field_obj)
319  {
320  $this->userObj->setInstantMessengerId($client, $this->form->getInput($field));
321  }
322  }
323 
324  $this->userObj->setTitle($this->userObj->getFullname());
325  $this->userObj->setDescription($this->userObj->getEmail());
326 
327  if ($this->registration_settings->passwordGenerationEnabled())
328  {
329  $password = ilUtil::generatePasswords(1);
330  $password = $password[0];
331  }
332  else
333  {
334  $password = $this->form->getInput("usr_password");
335  }
336  $this->userObj->setPasswd($password);
337 
338 
339  // Set user defined data
340  include_once './Services/User/classes/class.ilUserDefinedFields.php';
341  $user_defined_fields =& ilUserDefinedFields::_getInstance();
342  $defs = $user_defined_fields->getRegistrationDefinitions();
343  $udf = array();
344  foreach ($_POST as $k => $v)
345  {
346  if (substr($k, 0, 4) == "udf_")
347  {
348  $f = substr($k, 4);
349  $udf[$f] = $v;
350  }
351  }
352  $this->userObj->setUserDefinedData($udf);
353 
354  $this->userObj->setTimeLimitOwner(7);
355 
356 
357  $default_role = false;
358  if ($this->registration_settings->roleSelectionEnabled())
359  {
360  $default_role = $this->form->getInput('usr_roles');
361  }
362  else
363  {
364  // Assign by email
365  include_once 'Services/Registration/classes/class.ilRegistrationEmailRoleAssignments.php';
366  $registration_role_assignments = new ilRegistrationRoleAssignments();
367  $default_role = $registration_role_assignments->getRoleByEmail($this->userObj->getEmail());
368  }
369 
370  // get role from code / set code to used
371  $code = $this->form->getInput('usr_registration_code');
372  $this->code_was_used = false;
373  if($this->registration_settings->getRegistrationType() == IL_REG_CODES ||
374  ($code && $this->registration_settings->getAllowCodes()))
375  {
376  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
378  $default_role = ilRegistrationCode::getCodeRole($code);
379  $this->code_was_used = true;
380  }
381 
382 
383  if ($this->registration_settings->getAccessLimitation())
384  {
385  include_once 'Services/Registration/classes/class.ilRegistrationRoleAccessLimitations.php';
386  $access_limitations_obj = new ilRegistrationRoleAccessLimitations();
387 
388  $access_limit_mode = $access_limitations_obj->getMode($default_role);
389  if ($access_limit_mode == 'absolute')
390  {
391  $access_limit = $access_limitations_obj->getAbsolute($default_role);
392  $this->userObj->setTimeLimitUnlimited(0);
393  $this->userObj->setTimeLimitUntil($access_limit);
394  }
395  elseif ($access_limit_mode == 'relative')
396  {
397  $rel_d = (int) $access_limitations_obj->getRelative($default_role,'d');
398  $rel_m = (int) $access_limitations_obj->getRelative($default_role,'m');
399  $rel_y = (int) $access_limitations_obj->getRelative($default_role,'y');
400 
401  $access_limit = $rel_d * 86400 + $rel_m * 2592000 + $rel_y * 31536000 + time();
402  $this->userObj->setTimeLimitUnlimited(0);
403  $this->userObj->setTimeLimitUntil($access_limit);
404  }
405  else
406  {
407  $this->userObj->setTimeLimitUnlimited(1);
408  $this->userObj->setTimeLimitUntil(time());
409  }
410  }
411  else
412  {
413  $this->userObj->setTimeLimitUnlimited(1);
414  $this->userObj->setTimeLimitUntil(time());
415  }
416 
417  $this->userObj->setTimeLimitFrom(time());
418 
419  $this->userObj->create();
420 
421 
422  if($this->registration_settings->getRegistrationType() == IL_REG_DIRECT ||
423  $this->registration_settings->getRegistrationType() == IL_REG_CODES ||
425  {
426  $this->userObj->setActive(1,0);
427  }
428  else if($this->registration_settings->getRegistrationType() == IL_REG_ACTIVATION)
429  {
430  $this->userObj->setActive(0,0);
431  }
432  else
433  {
434  $this->userObj->setActive(0,0);
435  }
436 
437  $this->userObj->updateOwner();
438 
439  // set a timestamp for last_password_change
440  // this ts is needed by the ACCOUNT_SECURITY_MODE_CUSTOMIZED
441  // in ilSecuritySettings
442  $this->userObj->setLastPasswordChangeTS( time() );
443 
444  //insert user data in table user_data
445  $this->userObj->saveAsNew();
446 
447  // store acceptance of user agreement
448  $this->userObj->writeAccepted();
449 
450  // setup user preferences
451  $this->userObj->setLanguage($this->form->getInput('usr_language'));
452  $hits_per_page = $ilSetting->get("hits_per_page");
453  if ($hits_per_page < 10)
454  {
455  $hits_per_page = 10;
456  }
457  $this->userObj->setPref("hits_per_page", $hits_per_page);
458  $show_online = $ilSetting->get("show_users_online");
459  if ($show_online == "")
460  {
461  $show_online = "y";
462  }
463  $this->userObj->setPref("show_users_online", $show_online);
464  $this->userObj->writePrefs();
465 
466  $rbacadmin->assignUser((int)$default_role, $this->userObj->getId(), true);
467 
468  return $password;
469  }
470 
471  protected function __validateRole($role)
472  {
473  global $ilDB,$ilias,$ilErr,$lng;
474 
475  // validate role
476  include_once("./Services/AccessControl/classes/class.ilObjRole.php");
477  if ($this->registration_settings->roleSelectionEnabled() and
479  {
480  $ilias->raiseError("Invalid role selection in registration: ".
481  ilObject::_lookupTitle($role)." [".$role."]".
482  ", IP: ".$_SERVER["REMOTE_ADDR"],$ilias->error_obj->FATAL);
483  }
484  return true;
485  }
486 
487  protected function __distributeMails($password)
488  {
489  global $ilSetting;
490 
491  include_once './Services/Language/classes/class.ilLanguage.php';
492  include_once './Services/User/classes/class.ilObjUser.php';
493  include_once "Services/Mail/classes/class.ilFormatMail.php";
494  include_once './Services/Registration/classes/class.ilRegistrationMailNotification.php';
495 
496  // Always send mail to approvers
497  if($this->registration_settings->getRegistrationType() == IL_REG_APPROVE && !$this->code_was_used)
498  {
499  $mail = new ilRegistrationMailNotification();
501  $mail->setRecipients($this->registration_settings->getApproveRecipients());
502  $mail->setAdditionalInformation(array('usr' => $this->userObj));
503  $mail->send();
504  }
505  else
506  {
507  $mail = new ilRegistrationMailNotification();
509  $mail->setRecipients($this->registration_settings->getApproveRecipients());
510  $mail->setAdditionalInformation(array('usr' => $this->userObj));
511  $mail->send();
512 
513  }
514  // Send mail to new user
515 
516  // Registration with confirmation link ist enabled
517  if($this->registration_settings->getRegistrationType() == IL_REG_ACTIVATION && !$this->code_was_used)
518  {
519  include_once 'Services/Mail/classes/class.ilMail.php';
520  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
521 
522  // mail subject
523  $subject = $this->lng->txt("reg_mail_subject_confirmation");
524 
525  // mail body
526  $hashcode = ilObjUser::_generateRegistrationHash($this->userObj->getId());
527  $body = $this->lng->txt("reg_mail_body_salutation")." ".$this->userObj->getFullname().",\n\n";
528  $body .= $this->lng->txt('reg_mail_body_confirmation')."\n".
529  ILIAS_HTTP_PATH.'/confirmReg.php?client_id='.CLIENT_ID."&rh=".$hashcode."\n\n";
530 
531  $body .= sprintf($this->lng->txt('reg_mail_body_2_confirmation'),
532  ilFormat::_secondsToString($this->registration_settings->getRegistrationHashLifetime()))."\n\n";
533 
534  $body .= $this->lng->txt('reg_mail_body_3_confirmation');
535 
536  $mail_obj->enableSoap(false);
537  $mail_obj->appendInstallationSignature(true);
538  $mail_obj->sendMail($this->userObj->getEmail(), '', '',
539  $subject,
540  $body,
541  array(), array('normal'));
542  }
543  else
544  {
545  // try individual account mail in user administration
546  include_once("Services/Mail/classes/class.ilAccountMail.php");
547  include_once './Services/User/classes/class.ilObjUserFolder.php';
548  $amail = ilObjUserFolder::_lookupNewAccountMail($GLOBALS["lng"]->getDefaultLanguage());
549  if (trim($amail["body"]) != "" && trim($amail["subject"]) != "")
550  {
551  $acc_mail = new ilAccountMail();
552  $acc_mail->setUser($this->userObj);
553  if ($this->registration_settings->passwordGenerationEnabled())
554  {
555  $acc_mail->setUserPassword($password);
556  }
557  $acc_mail->send();
558  }
559  else // do default mail
560  {
561  include_once "Services/Mail/classes/class.ilMimeMail.php";
562 
563  $mmail = new ilMimeMail();
564  $mmail->autoCheck(false);
565  $mmail->From($ilSetting->get("admin_email"));
566  $mmail->To($this->userObj->getEmail());
567 
568  // mail subject
569  $subject = $this->lng->txt("reg_mail_subject");
570 
571  // mail body
572  $body = $this->lng->txt("reg_mail_body_salutation")." ".$this->userObj->getFullname().",\n\n".
573  $this->lng->txt("reg_mail_body_text1")."\n\n".
574  $this->lng->txt("reg_mail_body_text2")."\n".
575  ILIAS_HTTP_PATH."/login.php?client_id=".$ilias->client_id."\n";
576  $body .= $this->lng->txt("login").": ".$this->userObj->getLogin()."\n";
577 
578  if ($this->registration_settings->passwordGenerationEnabled())
579  {
580  $body.= $this->lng->txt("passwd").": ".$password."\n";
581  }
582  $body.= "\n";
583 
584  // Info about necessary approvement
585  if($this->registration_settings->getRegistrationType() == IL_REG_APPROVE && !$this->code_was_used)
586  {
587  $body .= ($this->lng->txt('reg_mail_body_pwd_generation')."\n\n");
588  }
589 
590  $body .= ($this->lng->txt("reg_mail_body_text3")."\n\r");
591  $body .= $this->userObj->getProfileAsString($this->lng);
592  $mmail->Subject($subject);
593  $mmail->Body($body);
594  $mmail->Send();
595  }
596  }
597  }
598 
599  public function login($password)
600  {
601  global $ilias,$lng,$ilLog;
602 
603  $ilLog->write("Entered login");
604 
605  $this->tpl->addBlockFile("CONTENT", "content", "tpl.usr_registered.html");
606 
607  $this->tpl->setVariable("IMG_USER",
608  ilUtil::getImagePath("icon_usr_b.gif"));
609  $this->tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("registration"));
610  $this->tpl->setVariable("TXT_WELCOME", $lng->txt("welcome").", ".$this->userObj->getTitle()."!");
611 
612  if (($this->registration_settings->getRegistrationType() == IL_REG_DIRECT or
613  $this->registration_settings->getRegistrationType() == IL_REG_CODES or
615  !$this->registration_settings->passwordGenerationEnabled())
616  {
617  $this->tpl->setCurrentBlock("activation");
618  $this->tpl->setVariable("TXT_REGISTERED", $lng->txt("txt_registered"));
619  $this->tpl->setVariable("FORMACTION", "login.php?cmd=post&target=".$_GET["target"]);
620  $this->tpl->setVariable("TARGET","target=\"_parent\"");
621  $this->tpl->setVariable("TXT_LOGIN", $lng->txt("login_to_ilias"));
622  $this->tpl->setVariable("USERNAME",$this->userObj->getLogin());
623  $this->tpl->setVariable("PASSWORD",$password);
624  $this->tpl->parseCurrentBlock();
625  }
626  else if ($this->registration_settings->getRegistrationType() == IL_REG_APPROVE)
627  {
628  $this->tpl->setVariable("TXT_REGISTERED", $lng->txt("txt_submitted"));
629  }
630  else if($this->registration_settings->getRegistrationType() == IL_REG_ACTIVATION)
631  {
632  $this->tpl->setVariable("TXT_REGISTERED", sprintf($lng->txt("reg_confirmation_link_successful"), './login.php'));
633  $this->tpl->setVariable("REDIRECT_URL", './login.php');
634  }
635  else
636  {
637  $this->tpl->setVariable("TXT_REGISTERED", $lng->txt("txt_registered_passw_gen"));
638  }
639  }
640 }
641 ?>