user_edit.php 12 KB

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