user_add.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php // $Id: user_add.php 14632 2008-03-17 17:49:21Z elixir_inter $
  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. // name of the language file that needs to be included
  26. $language_file = array('admin','registration');
  27. $cidReset = true;
  28. // including necessary libraries
  29. require ('../inc/global.inc.php');
  30. $libpath = api_get_path(LIBRARY_PATH);
  31. include_once ($libpath.'fileManage.lib.php');
  32. include_once ($libpath.'fileUpload.lib.php');
  33. include_once ($libpath.'usermanager.lib.php');
  34. require_once ($libpath.'formvalidator/FormValidator.class.php');
  35. require_once ($libpath.'security.lib.php');
  36. // section for the tabs
  37. $this_section=SECTION_PLATFORM_ADMIN;
  38. // user permissions
  39. api_protect_admin_script();
  40. // Database table definitions
  41. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  42. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  43. $htmlHeadXtra[] = '
  44. <script language="JavaScript" type="text/JavaScript">
  45. <!--
  46. function enable_expiration_date() { //v2.0
  47. document.user_add.radio_expiration_date[0].checked=false;
  48. document.user_add.radio_expiration_date[1].checked=true;
  49. }
  50. function password_switch_radio_button(form, input){
  51. var NodeList = document.getElementsByTagName("input");
  52. for(var i=0; i< NodeList.length; i++){
  53. if(NodeList.item(i).name=="password[password_auto]" && NodeList.item(i).value=="0"){
  54. NodeList.item(i).checked=true;
  55. }
  56. }
  57. }
  58. function display_drh_list(){
  59. if(document.getElementById("status_select").value=='.STUDENT.')
  60. {
  61. document.getElementById("drh_list").style.display="block";
  62. }
  63. else
  64. {
  65. document.getElementById("drh_list").style.display="none";
  66. document.getElementById("drh_select").options[0].selected="selected";
  67. }
  68. }
  69. //-->
  70. </script>';
  71. if(!empty($_GET['message'])){
  72. $message = urldecode($_GET['message']);
  73. }
  74. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  75. $tool_name = get_lang('AddUsers');
  76. // Create the form
  77. $form = new FormValidator('user_add');
  78. // Lastname
  79. $form->addElement('text','lastname',get_lang('LastName'));
  80. $form->applyFilter('lastname','html_filter');
  81. $form->applyFilter('lastname','trim');
  82. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  83. // Firstname
  84. $form->addElement('text','firstname',get_lang('FirstName'));
  85. $form->applyFilter('firstname','html_filter');
  86. $form->applyFilter('firstname','trim');
  87. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  88. // Official code
  89. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  90. $form->applyFilter('official_code','html_filter');
  91. $form->applyFilter('official_code','trim');
  92. // Email
  93. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  94. $form->addRule('email', get_lang('EmailWrong'), 'email');
  95. $form->addRule('email', get_lang('EmailWrong'), 'required');
  96. // Phone
  97. $form->addElement('text','phone',get_lang('PhoneNumber'));
  98. // Picture
  99. $form->addElement('file', 'picture', get_lang('AddPicture'));
  100. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  101. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  102. // Username
  103. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  104. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  105. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  106. $form->addRule('username', '', 'maxlength',20);
  107. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  108. // Password
  109. $group = array();
  110. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  111. if(count($extAuthSource) > 0)
  112. {
  113. $group[] =& HTML_QuickForm::createElement('radio','password_auto',null,get_lang('ExternalAuthentication').' ',2);
  114. $auth_sources = array();
  115. foreach($extAuthSource as $key => $info)
  116. {
  117. $auth_sources[$key] = $key;
  118. }
  119. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  120. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  121. }
  122. $group[] =& HTML_QuickForm::createElement('radio','password_auto',get_lang('Password'),get_lang('AutoGeneratePassword').'<br />',1);
  123. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto','id="radio_user_password"',null,0);
  124. $group[] =& HTML_QuickForm::createElement('password', 'password',null,'onkeydown=password_switch_radio_button(document.user_add,"password[password_auto]")');
  125. $form->addGroup($group, 'password', get_lang('Password'), '');
  126. // Status
  127. $status = array();
  128. $status[COURSEMANAGER] = get_lang('Teacher');
  129. $status[STUDENT] = get_lang('Learner');
  130. $status[DRH] = get_lang('Drh');
  131. //$status[ADMINCRFP] = get_lang('AdminCrfp');
  132. $form->addElement('select','status',get_lang('Status'),$status,'id="status_select" onchange="display_drh_list()"');
  133. //drh list (display only if student)
  134. $display = $_POST['status'] == STUDENT ? 'block' : 'none';
  135. $form->addElement('html','<div id="drh_list" style="display:'.$display.';">');
  136. $drh_select = $form->addElement('select','hr_dept_id',get_lang('Drh'),array(),'id="drh_select"');
  137. $drh_list = UserManager :: get_user_list(array('status'=>DRH),array('lastname','firstname'));
  138. $drh_select->addOption('---',0);
  139. foreach($drh_list as $drh)
  140. {
  141. $drh_select->addOption($drh['lastname'].' '.$drh['firstname'],$drh['user_id']);
  142. }
  143. $form->addElement('html', '</div>');
  144. // Platform admin
  145. $group = array();
  146. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  147. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  148. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  149. // Send email
  150. $group = array();
  151. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  152. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  153. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  154. // Expiration Date
  155. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  156. $group = array ();
  157. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  158. $group[] = & $form->createElement('datepicker','expiration_date', null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  159. $form->addGroup($group, 'max_member_group', null, '', false);
  160. // Active account or inactive account
  161. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  162. $form->addElement('radio','active','',get_lang('Inactive'),0);
  163. // Set default values
  164. $defaults['admin']['platform_admin'] = 0;
  165. $defaults['mail']['send_mail'] = 1;
  166. $defaults['password']['password_auto'] = 1;
  167. $defaults['active'] = 1;
  168. $defaults['expiration_date']=array();
  169. $days = api_get_setting('account_valid_duration');
  170. $time = strtotime('+'.$days.' day');
  171. $defaults['expiration_date']['d']=date('d',$time);
  172. $defaults['expiration_date']['F']=date('m',$time);
  173. $defaults['expiration_date']['Y']=date('Y',$time);
  174. $defaults['radio_expiration_date'] = 0;
  175. $form->setDefaults($defaults);
  176. // Submit button
  177. $form->addElement('submit', 'submit', get_lang('Add'));
  178. $form->addElement('submit', 'submit_plus', get_lang('Add').'+');
  179. // Validate form
  180. if( $form->validate())
  181. {
  182. $check = Security::check_token('post');
  183. if($check)
  184. {
  185. $user = $form->exportValues();
  186. $picture_element = & $form->getElement('picture');
  187. $picture = $picture_element->getValue();
  188. $picture_uri = '';
  189. if (strlen($picture['name']) > 0)
  190. {
  191. if(!is_dir(api_get_path(SYS_CODE_PATH).'upload/users/')){
  192. if(mkdir(api_get_path(SYS_CODE_PATH).'upload/users/'))
  193. {
  194. $perm = api_get_setting('permissions_for_new_directories');
  195. $perm = octdec(!empty($perm)?$perm:'0770');
  196. chmod(api_get_path(SYS_CODE_PATH).'upload/users/');
  197. }
  198. }
  199. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  200. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  201. move_uploaded_file($picture['tmp_name'], $picture_location);
  202. }
  203. $lastname = $user['lastname'];
  204. $firstname = $user['firstname'];
  205. $official_code = $user['official_code'];
  206. $email = $user['email'];
  207. $phone = $user['phone'];
  208. $username = $user['username'];
  209. $status = intval($user['status']);
  210. $picture = $_FILES['picture'];
  211. $platform_admin = intval($user['admin']['platform_admin']);
  212. $send_mail = intval($user['mail']['send_mail']);
  213. $hr_dept_id = intval($user['hr_dept_id']);
  214. if(count($extAuthSource) > 0 && $user['password']['password_auto'] == '2')
  215. {
  216. $auth_source = $user['password']['auth_source'];
  217. $password = 'PLACEHOLDER';
  218. }
  219. else
  220. {
  221. $auth_source = PLATFORM_AUTH_SOURCE;
  222. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  223. }
  224. if ($user['radio_expiration_date']=='1' )
  225. {
  226. $expiration_date=$user['expiration_date'];
  227. }
  228. else
  229. {
  230. $expiration_date='0000-00-00 00:00:00';
  231. }
  232. $active = intval($user['active']);
  233. $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, $hr_dept_id);
  234. if ($platform_admin)
  235. {
  236. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  237. api_sql_query($sql,__FILE__,__LINE__);
  238. }
  239. if (!empty ($email) && $send_mail)
  240. {
  241. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  242. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  243. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  244. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  245. $emailbody=get_lang('Dear')." ".stripslashes("$firstname $lastname").",\n\n".get_lang('YouAreReg')." ". get_setting('siteName') ." ".get_lang('Settings')." ". $username ."\n". get_lang('Pass')." : ".stripslashes($password)."\n\n" .get_lang('Address') ." ". get_setting('siteName') ." ". get_lang('Is') ." : ". $_configuration['root_web'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  246. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  247. }
  248. Security::clear_token();
  249. if(isset($user['submit_plus']))
  250. {
  251. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  252. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')));
  253. exit ();
  254. }
  255. else
  256. {
  257. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')));
  258. exit ();
  259. }
  260. }
  261. }else{
  262. if(isset($_POST['submit'])){
  263. Security::clear_token();
  264. }
  265. $token = Security::get_token();
  266. $form->addElement('hidden','sec_token');
  267. $form->setConstants(array('sec_token' => $token));
  268. }
  269. // Display form
  270. Display::display_header($tool_name);
  271. //api_display_tool_title($tool_name);
  272. if(!empty($message)){
  273. Display::display_normal_message(stripslashes($message));
  274. }
  275. $form->display();
  276. /*
  277. ==============================================================================
  278. FOOTER
  279. ==============================================================================
  280. */
  281. Display::display_footer();
  282. ?>