user_edit.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php // $Id: user_edit.php 10215 2006-11-27 13:57:17Z 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. include('../inc/global.inc.php');
  29. $this_section=SECTION_PLATFORM_ADMIN;
  30. api_protect_admin_script();
  31. $htmlHeadXtra[] = '
  32. <script language="JavaScript" type="text/JavaScript">
  33. <!--
  34. function enable_expiration_date() { //v2.0
  35. document.user_add.radio_expiration_date[0].checked=false;
  36. document.user_add.radio_expiration_date[1].checked=true;
  37. }
  38. //-->
  39. </script>';
  40. include(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  41. include(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  42. include(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  43. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  44. $user_id=isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
  45. $noPHP_SELF=true;
  46. $tool_name=get_lang('ModifyUserInfo');
  47. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  48. $interbreadcrumb[]=array('url' => "user_list.php","name" => get_lang('UserList'));
  49. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  50. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  51. $sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u LEFT JOIN $table_admin a ON a.user_id = u.user_id WHERE u.user_id = '".$user_id."'";
  52. $res = api_sql_query($sql,__FILE__,__LINE__);
  53. if(mysql_num_rows($res) != 1)
  54. {
  55. header('Location: user_list.php');
  56. exit;
  57. }
  58. $user_data = mysql_fetch_array($res,MYSQL_ASSOC);
  59. $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
  60. $user_data['send_mail'] = 0;
  61. $user_data['old_password'] = $user_data['password'];
  62. unset($user_data['password']);
  63. // Create the form
  64. $form = new FormValidator('user_add','post','','',array('style' => 'width: 60%; float: '.($text_dir=='rtl'?'right;':'left;')));
  65. $form->addElement('hidden','user_id',$user_id);
  66. // Lastname
  67. $form->addElement('text','lastname',get_lang('LastName'));
  68. $form->applyFilter('lastname','html_filter');
  69. $form->applyFilter('lastname','trim');
  70. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  71. // Firstname
  72. $form->addElement('text','firstname',get_lang('FirstName'));
  73. $form->applyFilter('firstname','html_filter');
  74. $form->applyFilter('firstname','trim');
  75. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  76. // Official code
  77. $form->addElement('text', 'official_code', get_lang('OfficialCode'),array('size' => '40'));
  78. $form->applyFilter('official_code','html_filter');
  79. $form->applyFilter('official_code','trim');
  80. // Email
  81. $form->addElement('text', 'email', get_lang('Email'),array('size' => '40'));
  82. $form->addRule('email', get_lang('EmailWrong'), 'email');
  83. $form->addRule('email', get_lang('EmailWrong'), 'required');
  84. // Phone
  85. $form->addElement('text','phone',get_lang('PhoneNumber'));
  86. // Picture
  87. $form->addElement('file', 'picture', get_lang('AddPicture'));
  88. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  89. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  90. if (strlen($user_data['picture_uri']) > 0 )
  91. {
  92. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  93. }
  94. // Username
  95. $form->addElement('text', 'username', get_lang('LoginName'),array('maxlength'=>20));
  96. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  97. $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
  98. $form->addRule('username', '', 'maxlength',20);
  99. $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
  100. // Password
  101. $form->addElement('radio','reset_password',get_lang('Password'),get_lang('DontResetPassword'),0);
  102. if(count($extAuthSource) > 0)
  103. {
  104. $group[] =& HTML_QuickForm::createElement('radio','reset_password',null,get_lang('ExternalAuthentication').' ',3);
  105. $auth_sources = array();
  106. foreach($extAuthSource as $key => $info)
  107. {
  108. $auth_sources[$key] = $key;
  109. }
  110. $group[] =& HTML_QuickForm::createElement('select','auth_source',null,$auth_sources);
  111. $group[] =& HTML_QuickForm::createElement('static','','','<br />');
  112. $form->addGroup($group, 'password', null, '',false);
  113. }
  114. $form->addElement('radio','reset_password',null,get_lang('AutoGeneratePassword'),1);
  115. $group = array();
  116. $group[] =& HTML_QuickForm::createElement('radio', 'reset_password',null,null,2);
  117. $group[] =& HTML_QuickForm::createElement('password', 'password',null,null);
  118. $form->addGroup($group, 'password', null, '',false);
  119. // Status
  120. $status = array();
  121. $status[COURSEMANAGER] = get_lang('CourseAdmin');
  122. $status[STUDENT] = get_lang('Student');
  123. $form->addElement('select','status',get_lang('Status'),$status);
  124. // Platform admin
  125. // Only when changing another user!
  126. if($user_id != $_SESSION['_uid'])
  127. {
  128. $group = array();
  129. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('Yes'),1);
  130. $group[] =& HTML_QuickForm::createElement('radio', 'platform_admin',null,get_lang('No'),0);
  131. $form->addGroup($group, 'admin', get_lang('PlatformAdmin'), '&nbsp;',false);
  132. }
  133. // Send email
  134. $group = array();
  135. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'),1);
  136. $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'),0);
  137. $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;',false);
  138. // Registration Date
  139. $form->addElement('static','registration_date', get_lang('RegistrationDate'), $user_data['registration_date']);
  140. if(! $user_data['platform_admin'] )
  141. {
  142. // Expiration Date
  143. $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
  144. $group = array ();
  145. $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
  146. $group[] = & $form->createElement('datepicker', 'expiration_date',null, array ('form_name' => $form->getAttribute('name'), 'onChange'=>'enable_expiration_date()'));
  147. $form->addGroup($group, 'max_member_group', null, '', false);
  148. // Active account or inactive account
  149. $form->addElement('radio','active',get_lang('ActiveAccount'),get_lang('Active'),1);
  150. $form->addElement('radio','active','',get_lang('Inactive'),0);
  151. }
  152. // Submit button
  153. $form->addElement('submit', 'submit', 'OK');
  154. // Set default values
  155. $expiration_date=$user_data['expiration_date'];
  156. if ($expiration_date=='0000-00-00 00:00:00')
  157. {
  158. $user_data['radio_expiration_date']=0;
  159. $user_data['expiration_date']=array();
  160. $user_data['expiration_date']['d']=date('d');
  161. $user_data['expiration_date']['F']=date('m');
  162. $user_data['expiration_date']['Y']=date('Y');
  163. }
  164. else
  165. {
  166. $user_data['radio_expiration_date']=1;
  167. $user_data['expiration_date']=array();
  168. $user_data['expiration_date']['d']=substr($expiration_date,8,2);
  169. $user_data['expiration_date']['F']=substr($expiration_date,5,2);
  170. $user_data['expiration_date']['Y']=substr($expiration_date,0,4);
  171. }
  172. $form->setDefaults($user_data);
  173. // Validate form
  174. if( $form->validate())
  175. {
  176. $user = $form->exportValues();
  177. $picture_element = & $form->getElement('picture');
  178. $picture = $picture_element->getValue();
  179. $picture_uri = '';
  180. if (strlen($picture['name']) > 0)
  181. {
  182. $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
  183. $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
  184. move_uploaded_file($picture['tmp_name'], $picture_location);
  185. }
  186. elseif(isset($user['delete_picture']))
  187. {
  188. @unlink('../upload/users/'.$user_data['picture_uri']);
  189. }
  190. $lastname = $user['lastname'];
  191. $firstname = $user['firstname'];
  192. $official_code = $user['official_code'];
  193. $email = $user['email'];
  194. $phone = $user['phone'];
  195. $username = $user['username'];
  196. $status = intval($user['status']);
  197. $picture = $_FILES['picture'];
  198. $platform_admin = intval($user['platform_admin']);
  199. $send_mail = intval($user['send_mail']);
  200. $reset_password = intval($user['reset_password']);
  201. if ($user['radio_expiration_date']=='1' && ! $user_data['platform_admin'] )
  202. {
  203. $expiration_date=$user['expiration_date'];
  204. }
  205. else
  206. {
  207. $expiration_date='0000-00-00 00:00:00';
  208. }
  209. $active = $user_data['platform_admin'] ? 1 : intval($user['active']);
  210. if( $reset_password == 0)
  211. {
  212. $password = null;
  213. $auth_source = $user_data['auth_source'];
  214. }
  215. elseif($reset_password == 1)
  216. {
  217. $password = api_generate_password();
  218. $auth_source = PLATFORM_AUTH_SOURCE;
  219. }
  220. else
  221. {
  222. $password = $user['password'];
  223. $auth_source = PLATFORM_AUTH_SOURCE;
  224. }
  225. UserManager::update_user($user_id,$firstname,$lastname,$username,$password,$auth_source,$email,$status,$official_code,$phone,$picture_uri,$expiration_date, $active);
  226. if($user_id != $_SESSION['_uid'])
  227. {
  228. if($platform_admin == 1)
  229. {
  230. $sql = "INSERT IGNORE INTO $table_admin SET user_id = '".$user_id."'";
  231. api_sql_query($sql,__FILE__,__LINE__);
  232. }
  233. else
  234. {
  235. $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'";
  236. api_sql_query($sql,__FILE__,__LINE__);
  237. }
  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("$form_firstname $lastname").",\n\n".get_lang('YouAreReg')." ". get_setting('siteName') ." ".get_lang('Settings')." ". $username;
  246. if($reset_password != 0 || !$userPasswordCrypted )
  247. {
  248. $emaibody .= "\n".get_lang('Pass')." : ".stripslashes($password);
  249. }
  250. $emailbody .= "\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');
  251. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  252. }
  253. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('UserUpdated')));
  254. exit();
  255. }
  256. Display::display_header($tool_name);
  257. //api_display_tool_title($tool_name);
  258. // Show the users picture
  259. if (strlen($user_data['picture_uri']) > 0)
  260. {
  261. $picture_url = api_get_path(WEB_CODE_PATH).'upload/users/'.$user_data['picture_uri'];
  262. }
  263. else
  264. {
  265. $picture_url = api_get_path(WEB_CODE_PATH)."img/unknown.jpg";
  266. }
  267. $image_size = @getimagesize($picture_url);
  268. $img_attributes = 'src="'.$picture_url.'?rand='.time().'" '
  269. .'alt="'.$user_data['lastname'].' '.$user_data['firstname'].'" '
  270. .'style="float:'.($text_dir == 'rtl' ? 'left' : 'right').'; padding:5px;" ';
  271. if ($image_size[0] > 200) //limit display width to 300px
  272. $img_attributes .= 'width="200" ';
  273. echo '<img '.$img_attributes.'/>';
  274. // Display form
  275. $form->display();
  276. /*
  277. ==============================================================================
  278. FOOTER
  279. ==============================================================================
  280. */
  281. Display::display_footer();
  282. ?>