user_add.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php // $Id: user_add.php 13273 2007-09-26 09:50:11Z elixir_julian $
  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. //-->
  59. </script>';
  60. if(!empty($_GET['message'])){
  61. $message = urldecode($_GET['message']);
  62. }
  63. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  64. $tool_name = get_lang('AddUsers');
  65. // Create the form
  66. $form = new FormValidator('user_add');
  67. // Lastname
  68. $form->addElement('text','lastname',get_lang('LastName'));
  69. $form->applyFilter('lastname','html_filter');
  70. $form->applyFilter('lastname','trim');
  71. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  72. // Firstname
  73. $form->addElement('text','firstname',get_lang('FirstName'));
  74. $form->applyFilter('firstname','html_filter');
  75. $form->applyFilter('firstname','trim');
  76. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  77. // Official code
  78. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  79. $form->applyFilter('official_code','html_filter');
  80. $form->applyFilter('official_code','trim');
  81. // Email
  82. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  83. $form->addRule('email', get_lang('EmailWrong'), 'email');
  84. $form->addRule('email', get_lang('EmailWrong'), 'required');
  85. // Phone
  86. $form->addElement('text','phone',get_lang('PhoneNumber'));
  87. // Picture
  88. $form->addElement('file', 'picture', get_lang('AddPicture'));
  89. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  90. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  91. // Username
  92. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  93. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  94. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  95. $form->addRule('username', '', 'maxlength',20);
  96. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  97. // Password
  98. $group = array();
  99. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  100. if(count($extAuthSource) > 0)
  101. {
  102. $group[] =& HTML_QuickForm::createElement('radio','password_auto',null,get_lang('ExternalAuthentication').' ',2);
  103. $auth_sources = array();
  104. foreach($extAuthSource as $key => $info)
  105. {
  106. $auth_sources[$key] = $key;
  107. }
  108. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  109. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  110. }
  111. $group[] =& HTML_QuickForm::createElement('radio','password_auto',get_lang('Password'),get_lang('AutoGeneratePassword').'<br />',1);
  112. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto','id="radio_user_password"',null,0);
  113. $group[] =& HTML_QuickForm::createElement('password', 'password',null,'onkeydown=password_switch_radio_button(document.user_add,"password[password_auto]")');
  114. $form->addGroup($group, 'password', get_lang('Password'), '');
  115. // Status
  116. $status = array();
  117. $status[COURSEMANAGER] = get_lang('CourseAdmin');
  118. $status[STUDENT] = get_lang('Student');
  119. $form->addElement('select','status',get_lang('Status'),$status);
  120. // Platform admin
  121. $group = array();
  122. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  123. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  124. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  125. // Send email
  126. $group = array();
  127. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  128. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  129. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  130. // Expiration Date
  131. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  132. $group = array ();
  133. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  134. $group[] = & $form->createElement('datepicker','expiration_date', null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  135. $form->addGroup($group, 'max_member_group', null, '', false);
  136. // Active account or inactive account
  137. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  138. $form->addElement('radio','active','',get_lang('Inactive'),0);
  139. // Set default values
  140. $defaults['admin']['platform_admin'] = 0;
  141. $defaults['mail']['send_mail'] = 1;
  142. $defaults['password']['password_auto'] = 1;
  143. $defaults['active'] = 1;
  144. $defaults['expiration_date']=array();
  145. $days = api_get_setting('account_valid_duration');
  146. $time = strtotime('+'.$days.' day');
  147. $defaults['expiration_date']['d']=date('d',$time);
  148. $defaults['expiration_date']['F']=date('m',$time);
  149. $defaults['expiration_date']['Y']=date('Y',$time);
  150. $defaults['radio_expiration_date'] = 0;
  151. $form->setDefaults($defaults);
  152. // Submit button
  153. $form->addElement('submit', 'submit', get_lang('Add'));
  154. $form->addElement('submit', 'submit_plus', get_lang('Add').'+');
  155. // Validate form
  156. if( $form->validate())
  157. {
  158. $check = Security::check_token('post');
  159. if($check)
  160. {
  161. $user = $form->exportValues();
  162. $picture_element = & $form->getElement('picture');
  163. $picture = $picture_element->getValue();
  164. $picture_uri = '';
  165. if (strlen($picture['name']) > 0)
  166. {
  167. if(!is_dir(api_get_path(SYS_CODE_PATH).'upload/users/')){
  168. if(mkdir(api_get_path(SYS_CODE_PATH).'upload/users/'))
  169. {
  170. $perm = api_get_setting('permissions_for_new_directories');
  171. $perm = octdec(!empty($perm)?$perm:'0770');
  172. chmod(api_get_path(SYS_CODE_PATH).'upload/users/');
  173. }
  174. }
  175. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  176. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  177. move_uploaded_file($picture['tmp_name'], $picture_location);
  178. }
  179. $lastname = $user['lastname'];
  180. $firstname = $user['firstname'];
  181. $official_code = $user['official_code'];
  182. $email = $user['email'];
  183. $phone = $user['phone'];
  184. $username = $user['username'];
  185. $status = intval($user['status']);
  186. $picture = $_FILES['picture'];
  187. $platform_admin = intval($user['admin']['platform_admin']);
  188. $send_mail = intval($user['mail']['send_mail']);
  189. if(count($extAuthSource) > 0 && $user['password']['password_auto'] == '2')
  190. {
  191. $auth_source = $user['password']['auth_source'];
  192. $password = 'PLACEHOLDER';
  193. }
  194. else
  195. {
  196. $auth_source = PLATFORM_AUTH_SOURCE;
  197. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  198. }
  199. if ($user['radio_expiration_date']=='1' )
  200. {
  201. $expiration_date=$user['expiration_date'];
  202. }
  203. else
  204. {
  205. $expiration_date='0000-00-00 00:00:00';
  206. }
  207. $active = intval($user['active']);
  208. $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);
  209. if ($platform_admin)
  210. {
  211. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  212. api_sql_query($sql,__FILE__,__LINE__);
  213. }
  214. if (!empty ($email) && $send_mail)
  215. {
  216. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  217. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  218. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  219. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  220. $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');
  221. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  222. }
  223. Security::clear_token();
  224. if(isset($user['submit_plus']))
  225. {
  226. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  227. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')));
  228. exit ();
  229. }
  230. else
  231. {
  232. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')));
  233. exit ();
  234. }
  235. }
  236. }else{
  237. if(isset($_POST['submit'])){
  238. Security::clear_token();
  239. }
  240. $token = Security::get_token();
  241. $form->addElement('hidden','sec_token');
  242. $form->setConstants(array('sec_token' => $token));
  243. }
  244. // Display form
  245. Display::display_header($tool_name);
  246. //api_display_tool_title($tool_name);
  247. if(!empty($message)){
  248. Display::display_normal_message(stripslashes($message));
  249. }
  250. $form->display();
  251. /*
  252. ==============================================================================
  253. FOOTER
  254. ==============================================================================
  255. */
  256. Display::display_footer();
  257. ?>