PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/_core/users/edit_user.php

https://github.com/harriswong/ATutor
PHP | 265 lines | 184 code | 41 blank | 40 comment | 64 complexity | 04d6607ec7a233282c2273c3f1114dc9 MD5 | raw file
  1. <?php
  2. /************************************************************************/
  3. /* ATutor */
  4. /************************************************************************/
  5. /* Copyright (c) 2002-2010 */
  6. /* Inclusive Design Institute */
  7. /* http://atutor.ca */
  8. /* */
  9. /* This program is free software. You can redistribute it and/or */
  10. /* modify it under the terms of the GNU General Public License */
  11. /* as published by the Free Software Foundation. */
  12. /************************************************************************/
  13. // $Id$
  14. define('AT_INCLUDE_PATH', '../../../include/');
  15. require(AT_INCLUDE_PATH.'vitals.inc.php');
  16. admin_authenticate(AT_ADMIN_PRIV_USERS);
  17. if (isset($_POST['cancel'])) {
  18. if (isset($_POST['ml']) && $_REQUEST['ml']) {
  19. header('Location: '.AT_BASE_HREF.'mods/_core/users/master_list.php');
  20. } else {
  21. header('Location: '.AT_BASE_HREF.'mods/_core/users/users.php');
  22. }
  23. exit;
  24. }
  25. if (isset($_POST['submit'])) {
  26. $missing_fields = array();
  27. $id = intval($_POST['id']);
  28. //$_POST['password'] = $addslashes($_POST['password']);
  29. $_POST['website'] = $addslashes($_POST['website']);
  30. $_POST['first_name'] = $addslashes($_POST['first_name']);
  31. $_POST['second_name'] = $addslashes($_POST['second_name']);
  32. $_POST['last_name'] = $addslashes($_POST['last_name']);
  33. $_POST['address'] = $addslashes($_POST['address']);
  34. $_POST['postal'] = $addslashes($_POST['postal']);
  35. $_POST['city'] = $addslashes($_POST['city']);
  36. $_POST['province'] = $addslashes($_POST['province']);
  37. $_POST['country'] = $addslashes($_POST['country']);
  38. $_POST['phone'] = $addslashes($_POST['phone']);
  39. $_POST['status'] = intval($_POST['status']);
  40. $_POST['old_status'] = intval($_POST['old_status']);
  41. $_POST['gender'] = $addslashes($_POST['gender']);
  42. $_POST['student_id'] = intval($_POST['student_id']);
  43. $_POST['email'] = $addslashes($_POST['email']);
  44. //check if student id (public field) is already being used
  45. if (!$_POST['overwrite'] && !empty($_POST['student_id'])) {
  46. $result = mysql_query("SELECT public_field FROM ".TABLE_PREFIX."master_list WHERE public_field='$_POST[student_id]' AND member_id<>0 AND member_id<>$id",$db);
  47. if (mysql_num_rows($result) != 0) {
  48. $msg->addError('CREATE_MASTER_USED');
  49. }
  50. }
  51. /* email check */
  52. if ($_POST['email'] == '') {
  53. $missing_fields[] = _AT('email');
  54. } else if (!preg_match("/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/i", $_POST['email'])) {
  55. $msg->addError('EMAIL_INVALID');
  56. }
  57. $result = mysql_query("SELECT * FROM ".TABLE_PREFIX."members WHERE email LIKE '$_POST[email]' AND member_id <> $id",$db);
  58. if (mysql_num_rows($result) != 0) {
  59. $valid = 'no';
  60. $msg->addError('EMAIL_EXISTS');
  61. }
  62. if (!$_POST['first_name']) {
  63. $missing_fields[] = _AT('first_name');
  64. }
  65. if (!$_POST['last_name']) {
  66. $missing_fields[] = _AT('last_name');
  67. }
  68. $_POST['first_name'] = str_replace('<', '', $_POST['first_name']);
  69. $_POST['second_name'] = str_replace('<', '', $_POST['second_name']);
  70. $_POST['last_name'] = str_replace('<', '', $_POST['last_name']);
  71. // check if first+last is unique
  72. /*
  73. * http://www.atutor.ca/atutor/mantis/view.php?id=3760
  74. if ($_POST['first_name'] && $_POST['last_name']) {
  75. $first_name_sql = $addslashes($_POST['first_name']);
  76. $last_name_sql = $addslashes($_POST['last_name']);
  77. $second_name_sql = $addslashes($_POST['second_name']);
  78. $sql = "SELECT member_id FROM ".TABLE_PREFIX."members WHERE first_name='$first_name_sql' AND second_name='$second_name_sql' AND last_name='$last_name_sql' AND member_id<>$id LIMIT 1";
  79. $result = mysql_query($sql, $db);
  80. if (mysql_fetch_assoc($result)) {
  81. $msg->addError('FIRST_LAST_NAME_UNIQUE');
  82. }
  83. }
  84. */
  85. //check date of birth
  86. $mo = intval($_POST['month']);
  87. $day = intval($_POST['day']);
  88. $yr = intval($_POST['year']);
  89. /* let's us take (one or) two digit years (ex. 78 = 1978, 3 = 2003) */
  90. if ($yr < date('y')) {
  91. $yr += 2000;
  92. } else if ($yr < 1900) {
  93. $yr += 1900;
  94. }
  95. $dob = $yr.'-'.$mo.'-'.$day;
  96. if ($mo && $day && $yr && !checkdate($mo, $day, $yr)) {
  97. $msg->addError('DOB_INVALID');
  98. } else if (!$mo || !$day || !$yr) {
  99. $dob = '0000-00-00';
  100. $yr = $mo = $day = 0;
  101. }
  102. if ($missing_fields) {
  103. $missing_fields = implode(', ', $missing_fields);
  104. $msg->addError(array('EMPTY_FIELDS', $missing_fields));
  105. }
  106. if (!$msg->containsErrors()) {
  107. if (isset($_POST['profile_pic_delete'])) {
  108. profile_image_delete($id);
  109. }
  110. if (($_POST['website']) && (!strstr($_POST['website'], "://"))) {
  111. $_POST['website'] = "http://".$_POST['website'];
  112. }
  113. if ($_POST['website'] == 'http://') {
  114. $_POST['website'] = '';
  115. }
  116. $_POST['postal'] = strtoupper(trim($_POST['postal']));
  117. if (isset($_POST['private_email'])) {
  118. $_POST['private_email'] = 1;
  119. } else {
  120. $_POST['private_email'] = 0;
  121. }
  122. /* insert into the db. (the last 0 for status) */
  123. $sql = "UPDATE ".TABLE_PREFIX."members SET email = '$_POST[email]',
  124. website = '$_POST[website]',
  125. first_name = '$_POST[first_name]',
  126. second_name= '$_POST[second_name]',
  127. last_name = '$_POST[last_name]',
  128. dob = '$dob',
  129. gender = '$_POST[gender]',
  130. address = '$_POST[address]',
  131. postal = '$_POST[postal]',
  132. city = '$_POST[city]',
  133. province = '$_POST[province]',
  134. country = '$_POST[country]',
  135. phone = '$_POST[phone]',
  136. status = $_POST[status],
  137. language = '$_SESSION[lang]',
  138. private_email = $_POST[private_email],
  139. creation_date=creation_date,
  140. last_login=last_login
  141. WHERE member_id = $id";
  142. $result = mysql_query($sql, $db);
  143. if (!$result) {
  144. require(AT_INCLUDE_PATH.'header.inc.php');
  145. $msg->addError('DB_NOT_UPDATED');
  146. $msg->printAll();
  147. require(AT_INCLUDE_PATH.'footer.inc.php');
  148. exit;
  149. }
  150. if (defined('AT_MASTER_LIST') && AT_MASTER_LIST) {
  151. $_POST['student_id'] = $addslashes($_POST['student_id']);
  152. $student_pin = sha1($addslashes($_POST['student_pin']));
  153. //if changed, delete old stud id
  154. if (!empty($_POST['old_student_id']) && $_POST['old_student_id'] != $_POST['student_id']) {
  155. $sql = "DELETE FROM ".TABLE_PREFIX."master_list WHERE public_field=".$_POST['old_student_id']." AND member_id=$id";
  156. $result = mysql_query($sql, $db);
  157. }
  158. //if new is set
  159. if (!empty($_POST['student_id']) && $_POST['old_student_id'] != $_POST['student_id']) {
  160. $sql = "REPLACE INTO ".TABLE_PREFIX."master_list VALUES ('$_POST[student_id]', '', $id)";
  161. $result = mysql_query($sql, $db);
  162. }
  163. }
  164. if (defined('AT_EMAIL_CONFIRMATION') && AT_EMAIL_CONFIRMATION && ($_POST['status'] == AT_STATUS_UNCONFIRMED) && ($_POST['old_status'] != AT_STATUS_UNCONFIRMED)) {
  165. $sql = "SELECT email, creation_date FROM ".TABLE_PREFIX."members WHERE member_id=$id";
  166. $result = mysql_query($sql, $db);
  167. $row = mysql_fetch_assoc($result);
  168. $code = substr(md5($row['email'] . $row['creation_date']. $id), 0, 10);
  169. $confirmation_link = AT_BASE_HREF . 'confirm.php?id='.$id.SEP.'m='.$code;
  170. /* send the email confirmation message: */
  171. require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
  172. $mail = new ATutorMailer();
  173. $mail->AddAddress($row['email']);
  174. $mail->From = $_config['contact_email'];
  175. $mail->Subject = $_config['site_name'] . ' - ' . _AT('email_confirmation_subject');
  176. $mail->Body = _AT('email_confirmation_message', $_config['site_name'], $confirmation_link);
  177. $mail->Send();
  178. }
  179. $msg->addFeedback('PROFILE_UPDATED_ADMIN');
  180. if (isset($_POST['ml']) && $_REQUEST['ml']) {
  181. header('Location: '.AT_BASE_HREF.'mods/_core/users/master_list.php');
  182. } else {
  183. header('Location: '.AT_BASE_HREF.'mods/_core/users/users.php');
  184. }
  185. exit;
  186. }
  187. }
  188. $id = intval($_REQUEST['id']);
  189. if (empty($_POST)) {
  190. $sql = "SELECT * FROM ".TABLE_PREFIX."members WHERE member_id = $id";
  191. $result = mysql_query($sql, $db);
  192. if (!($row = mysql_fetch_assoc($result))) {
  193. require(AT_INCLUDE_PATH.'header.inc.php');
  194. $msg->addError('USER_NOT_FOUND');
  195. $msg->printAll();
  196. require(AT_INCLUDE_PATH.'footer.inc.php');
  197. exit;
  198. }
  199. $_POST = $row;
  200. list($_POST['year'],$_POST['month'],$_POST['day']) = explode('-', $row['dob']);
  201. //$_POST['password2'] = $_POST['password'];
  202. $_POST['old_status'] = $_POST['status'];
  203. if (admin_authenticate(AT_ADMIN_PRIV_USERS, TRUE) && defined('AT_MASTER_LIST') && AT_MASTER_LIST) {
  204. $sql = "SELECT public_field FROM ".TABLE_PREFIX."master_list WHERE member_id=$id";
  205. $result = mysql_query($sql, $db);
  206. if ($row = mysql_fetch_assoc($result)) {
  207. $_POST['old_student_id'] = $row['public_field'];
  208. $_POST['student_id'] = $row['public_field'];
  209. }
  210. }
  211. }
  212. $savant->assign('languageManager', $languageManager);
  213. if (isset($_REQUEST['ml']) && $_REQUEST['ml']) {
  214. // redirect back to the master list
  215. $savant->assign('ml', 1);
  216. } else {
  217. $savant->assign('ml', 0);
  218. }
  219. /* HAVE TO SEND MEMBER_ID THROUGH FORM AS A HIDDEN POST VARIABLE!!! */
  220. /* PUT IN IF LOOP THAT LETS YOU SEE STATUS RADIO BUTTONS */
  221. $savant->assign('no_captcha', true);
  222. $savant->display('registration.tmpl.php');
  223. ?>