user_add.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php // $Id: user_add.php 10204 2006-11-26 20:46:53Z pcool $
  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. include ('../inc/global.inc.php');
  30. include (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  31. include (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  32. include (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  33. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  34. // section for the tabs
  35. $this_section=SECTION_PLATFORM_ADMIN;
  36. // user permissions
  37. api_protect_admin_script();
  38. // Database table definitions
  39. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  40. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  41. $htmlHeadXtra[] = '
  42. <script language="JavaScript" type="text/JavaScript">
  43. <!--
  44. function enable_expiration_date() { //v2.0
  45. document.user_add.radio_expiration_date[0].checked=false;
  46. document.user_add.radio_expiration_date[1].checked=true;
  47. }
  48. //-->
  49. </script>';
  50. if(!empty($_GET['message'])){
  51. $message = urldecode($_GET['message']);
  52. }
  53. //$interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('PlatformAdmin'));
  54. $tool_name = get_lang('AddUsers');
  55. // Create the form
  56. $form = new FormValidator('user_add');
  57. // Lastname
  58. $form->addElement('text','lastname',get_lang('LastName'));
  59. $form->applyFilter('lastname','html_filter');
  60. $form->applyFilter('lastname','trim');
  61. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  62. // Firstname
  63. $form->addElement('text','firstname',get_lang('FirstName'));
  64. $form->applyFilter('firstname','html_filter');
  65. $form->applyFilter('firstname','trim');
  66. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  67. // Official code
  68. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  69. $form->applyFilter('official_code','html_filter');
  70. $form->applyFilter('official_code','trim');
  71. // Email
  72. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  73. $form->addRule('email', get_lang('EmailWrong'), 'email');
  74. $form->addRule('email', get_lang('EmailWrong'), 'required');
  75. // Phone
  76. $form->addElement('text','phone',get_lang('PhoneNumber'));
  77. // Picture
  78. $form->addElement('file', 'picture', get_lang('AddPicture'));
  79. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  80. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  81. // Username
  82. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  83. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  84. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  85. $form->addRule('username', '', 'maxlength',20);
  86. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  87. // Password
  88. $group = array();
  89. $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
  90. if(count($extAuthSource) > 0)
  91. {
  92. $group[] =& HTML_QuickForm::createElement('radio','password_auto',null,get_lang('ExternalAuthentication').' ',2);
  93. $auth_sources = array();
  94. foreach($extAuthSource as $key => $info)
  95. {
  96. $auth_sources[$key] = $key;
  97. }
  98. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  99. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  100. }
  101. $group[] =& HTML_QuickForm::createElement('radio','password_auto',get_lang('Password'),get_lang('AutoGeneratePassword').'<br />',1);
  102. $group[] =& HTML_QuickForm::createElement('radio', 'password_auto',null,null,0);
  103. $group[] =& HTML_QuickForm::createElement('password', 'password',null,null);
  104. $form->addGroup($group, 'password', get_lang('Password'), '');
  105. // Status
  106. $status = array();
  107. $status[COURSEMANAGER] = get_lang('CourseAdmin');
  108. $status[STUDENT] = get_lang('Student');
  109. $form->addElement('select','status',get_lang('Status'),$status);
  110. // Platform admin
  111. $group = array();
  112. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  113. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  114. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;');
  115. // Send email
  116. $group = array();
  117. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  118. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  119. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
  120. // Expiration Date
  121. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  122. $group = array ();
  123. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  124. $group[] = & $form->createElement('datepicker','expiration_date', null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  125. $form->addGroup($group, 'max_member_group', null, '', false);
  126. // Active account or inactive account
  127. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  128. $form->addElement('radio','active','',get_lang('Inactive'),0);
  129. // Set default values
  130. $defaults['admin']['platform_admin'] = 0;
  131. $defaults['mail']['send_mail'] = 1;
  132. $defaults['password']['password_auto'] = 1;
  133. $defaults['active'] = 1;
  134. $defaults['expiration_date']=array();
  135. $days = api_get_setting('account_valid_duration');
  136. $time = strtotime('+'.$days.' day');
  137. $defaults['expiration_date']['d']=date('d',$time);
  138. $defaults['expiration_date']['F']=date('m',$time);
  139. $defaults['expiration_date']['Y']=date('Y',$time);
  140. $defaults['radio_expiration_date'] = 0;
  141. $form->setDefaults($defaults);
  142. // Submit button
  143. $form->addElement('submit', 'submit', get_lang('Add'));
  144. $form->addElement('submit', 'submit_plus', get_lang('Add').'+');
  145. // Validate form
  146. if( $form->validate())
  147. {
  148. $user = $form->exportValues();
  149. $picture_element = & $form->getElement('picture');
  150. $picture = $picture_element->getValue();
  151. $picture_uri = '';
  152. if (strlen($picture['name']) > 0)
  153. {
  154. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  155. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  156. move_uploaded_file($picture['tmp_name'], $picture_location);
  157. }
  158. $lastname = $user['lastname'];
  159. $firstname = $user['firstname'];
  160. $official_code = $user['official_code'];
  161. $email = $user['email'];
  162. $phone = $user['phone'];
  163. $username = $user['username'];
  164. $status = intval($user['status']);
  165. $picture = $_FILES['picture'];
  166. $platform_admin = intval($user['platform_admin']);
  167. $send_mail = intval($user['mail']['send_mail']);
  168. if(count($extAuthSource) > 0 && $user['password']['password_auto'] == '2')
  169. {
  170. $auth_source = $user['password']['auth_source'];
  171. $password = 'PLACEHOLDER';
  172. }
  173. else
  174. {
  175. $auth_source = PLATFORM_AUTH_SOURCE;
  176. $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
  177. }
  178. if ($user['radio_expiration_date']=='1' )
  179. {
  180. $expiration_date=$user['expiration_date'];
  181. }
  182. else
  183. {
  184. $expiration_date='0000-00-00 00:00:00';
  185. }
  186. $active = intval($user['active']);
  187. $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);
  188. if ($platform_admin)
  189. {
  190. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  191. api_sql_query($sql,__FILE__,__LINE__);
  192. }
  193. if (!empty ($email) && $send_mail)
  194. {
  195. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  196. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  197. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  198. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  199. $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');
  200. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  201. }
  202. if(isset($user['submit_plus']))
  203. {
  204. //we want to add more. Prepare report message and redirect to the same page (to clean the form)
  205. header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')));
  206. exit ();
  207. }
  208. else
  209. {
  210. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')));
  211. exit ();
  212. }
  213. }
  214. // Display form
  215. Display::display_header($tool_name);
  216. //api_display_tool_title($tool_name);
  217. if(!empty($message)){
  218. Display::display_normal_message(stripslashes($message));
  219. }
  220. $form->display();
  221. /*
  222. ==============================================================================
  223. FOOTER
  224. ==============================================================================
  225. */
  226. Display::display_footer();
  227. ?>