user_add.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php // $Id: user_add.php 9812 2006-10-26 08:57:18Z bmol $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. Copyright (c) Bart Mollet, Hogeschool Gent
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * @package dokeos.admin
  23. ==============================================================================
  24. */
  25. $langFile = array('admin','registration');
  26. $cidReset = true;
  27. include ('../inc/global.inc.php');
  28. $this_section=SECTION_PLATFORM_ADMIN;
  29. api_protect_admin_script();
  30. $table_admin = Database :: get_main_table(MAIN_ADMIN_TABLE);
  31. $table_user = Database :: get_main_table(MAIN_USER_TABLE);
  32. include (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  33. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  34. include (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  35. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  36. $htmlHeadXtra[] = '
  37. <script language="JavaScript" type="text/JavaScript">
  38. <!--
  39. function enable_expiration_date() { //v2.0
  40. document.user_add.radio_expiration_date[0].checked=false;
  41. document.user_add.radio_expiration_date[1].checked=true;
  42. }
  43. //-->
  44. </script>';
  45. if(!empty($_GET['message'])){
  46. $message = urldecode($_GET['message']);
  47. }
  48. //$interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('PlatformAdmin'));
  49. $tool_name = get_lang('AddUsers');
  50. // Create the form
  51. $form = new FormValidator('user_add');
  52. // Lastname
  53. $form->addElement('text','lastname',get_lang('LastName'));
  54. $form->applyFilter('lastname','html_filter');
  55. $form->applyFilter('lastname','trim');
  56. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  57. // Firstname
  58. $form->addElement('text','firstname',get_lang('FirstName'));
  59. $form->applyFilter('firstname','html_filter');
  60. $form->applyFilter('firstname','trim');
  61. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  62. // Official code
  63. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  64. $form->applyFilter('official_code','html_filter');
  65. $form->applyFilter('official_code','trim');
  66. // Email
  67. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  68. $form->addRule('email', get_lang('EmailWrong'), 'email');
  69. $form->addRule('email', get_lang('EmailWrong'), 'required');
  70. // Phone
  71. $form->addElement('text','phone',get_lang('PhoneNumber'));
  72. // Picture
  73. $form->addElement('file', 'picture', get_lang('AddPicture'));
  74. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  75. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  76. // Username
  77. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  78. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  79. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  80. $form->addRule('username', '', 'maxlength',20);
  81. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  82. // Password
  83. $group = array();
  84. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  85. if(count($extAuthSource) > 0)
  86. {
  87. $group[] =& HTML_QuickForm::createElement('radio','password_auto',null,get_lang('ExternalAuthentication').' ',2);
  88. $auth_sources = array();
  89. foreach($extAuthSource as $key => $info)
  90. {
  91. $auth_sources[$key] = $key;
  92. }
  93. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  94. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  95. }
  96. $group[] =& HTML_QuickForm::createElement('radio','password_auto',get_lang('Password'),get_lang('AutoGeneratePassword').'<br />',1);
  97. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto',null,null,0);
  98. $group[] =& HTML_QuickForm::createElement('password', 'password',null,null);
  99. $form->addGroup($group, 'password', get_lang('Password'), '');
  100. // Status
  101. $status = array();
  102. $status[COURSEMANAGER] = get_lang('CourseAdmin');
  103. $status[STUDENT] = get_lang('Student');
  104. $form->addElement('select','status',get_lang('Status'),$status);
  105. // Platform admin
  106. $group = array();
  107. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  108. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  109. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  110. // Send email
  111. $group = array();
  112. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  113. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  114. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  115. // Expiration Date
  116. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  117. $group = array ();
  118. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  119. $group[] = & $form->createElement('datepicker','expiration_date', null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  120. $form->addGroup($group, 'max_member_group', null, '', false);
  121. // Active account or inactive account
  122. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  123. $form->addElement('radio','active','',get_lang('Inactive'),0);
  124. // Set default values
  125. $defaults['admin']['platform_admin'] = 0;
  126. $defaults['mail']['send_mail'] = 1;
  127. $defaults['password']['password_auto'] = 1;
  128. $defaults['active'] = 1;
  129. $defaults['expiration_date']=array();
  130. $days = api_get_setting('account_valid_duration');
  131. $time = strtotime('+'.$days.' day');
  132. $defaults['expiration_date']['d']=date('d',$time);
  133. $defaults['expiration_date']['F']=date('m',$time);
  134. $defaults['expiration_date']['Y']=date('Y',$time);
  135. $defaults['radio_expiration_date'] = 0;
  136. $form->setDefaults($defaults);
  137. // Submit button
  138. $form->addElement('submit', 'submit', get_lang('Add'));
  139. $form->addElement('submit', 'submit_plus', get_lang('Add').'+');
  140. // Validate form
  141. if( $form->validate())
  142. {
  143. $user = $form->exportValues();
  144. $picture_element = & $form->getElement('picture');
  145. $picture = $picture_element->getValue();
  146. $picture_uri = '';
  147. if (strlen($picture['name']) > 0)
  148. {
  149. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  150. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  151. move_uploaded_file($picture['tmp_name'], $picture_location);
  152. }
  153. $lastname = $user['lastname'];
  154. $firstname = $user['firstname'];
  155. $official_code = $user['official_code'];
  156. $email = $user['email'];
  157. $phone = $user['phone'];
  158. $username = $user['username'];
  159. $status = intval($user['status']);
  160. $picture = $_FILES['picture'];
  161. $platform_admin = intval($user['platform_admin']);
  162. $send_mail = intval($user['mail']['send_mail']);
  163. if(count($extAuthSource) > 0 && $user['password']['password_auto'] == '2')
  164. {
  165. $auth_source = $user['password']['auth_source'];
  166. $password = 'PLACEHOLDER';
  167. }
  168. else
  169. {
  170. $auth_source = PLATFORM_AUTH_SOURCE;
  171. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  172. }
  173. if ($user['radio_expiration_date']=='1' )
  174. {
  175. $expiration_date=$user['expiration_date'];
  176. }
  177. else
  178. {
  179. $expiration_date='0000-00-00 00:00:00';
  180. }
  181. $active = intval($user['active']);
  182. $user_id = UserManager::create_user($firstname,$lastname,$status,$email,$username,$password,$official_code,api_get_setting('platformLanguage'),$phone,$picture_uri,$auth_source,$expiration_date,$active);
  183. if ($platform_admin)
  184. {
  185. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  186. api_sql_query($sql,__FILE__,__LINE__);
  187. }
  188. if (!empty ($email) && $send_mail)
  189. {
  190. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  191. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  192. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  193. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  194. $emailbody=get_lang('langDear')." ".stripslashes("$firstname $lastname").",\n\n".get_lang('langYouAreReg')." ". get_setting('siteName') ." ".get_lang('langSettings')." ". $username ."\n". get_lang('langPass')." : ".stripslashes($password)."\n\n" .get_lang('langAddress') ." ". get_setting('siteName') ." ". get_lang('langIs') ." : ". $rootWeb ."\n\n". get_lang('langProblem'). "\n\n". get_lang('langFormula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('langManager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('langEmail') ." : ".get_setting('emailAdministrator');
  195. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  196. }
  197. if(isset($user['submit_plus']))
  198. {
  199. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  200. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')));
  201. exit ();
  202. }
  203. else
  204. {
  205. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')));
  206. exit ();
  207. }
  208. }
  209. // Display form
  210. Display::display_header($tool_name);
  211. //api_display_tool_title($tool_name);
  212. if(!empty($message)){
  213. Display::display_normal_message(stripslashes($message));
  214. }
  215. $form->display();
  216. /*
  217. ==============================================================================
  218. FOOTER
  219. ==============================================================================
  220. */
  221. Display::display_footer();
  222. ?>