user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: user.php 1166 2014-11-03 01:49:32Z hypowang $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_USER_CHECK_USERNAME_FAILED', -1);
  9. define('UC_USER_USERNAME_BADWORD', -2);
  10. define('UC_USER_USERNAME_EXISTS', -3);
  11. define('UC_USER_EMAIL_FORMAT_ILLEGAL', -4);
  12. define('UC_USER_EMAIL_ACCESS_ILLEGAL', -5);
  13. define('UC_USER_EMAIL_EXISTS', -6);
  14. define('UC_LOGIN_SUCCEED', 0);
  15. define('UC_LOGIN_ERROR_FOUNDER_PW', -1);
  16. define('UC_LOGIN_ERROR_ADMIN_PW', -2);
  17. define('UC_LOGIN_ERROR_ADMIN_NOT_EXISTS', -3);
  18. define('UC_LOGIN_ERROR_SECCODE', -4);
  19. define('UC_LOGIN_ERROR_FAILEDLOGIN', -5);
  20. class control extends adminbase {
  21. function __construct() {
  22. $this->control();
  23. }
  24. function control() {
  25. parent::__construct();
  26. if(getgpc('a') != 'login' && getgpc('a') != 'logout') {
  27. if(!$this->user['isfounder'] && !$this->user['allowadminuser']) {
  28. $this->message('no_permission_for_this_module');
  29. }
  30. }
  31. $this->load('user');
  32. }
  33. function onlogin() {
  34. $authkey = md5(UC_KEY.$_SERVER['HTTP_USER_AGENT'].$this->onlineip);
  35. $this->load('user');
  36. $username = getgpc('username', 'P');
  37. $password = getgpc('password', 'P');
  38. $iframe = getgpc('iframe') ? 1 : 0;
  39. $isfounder = intval(getgpc('isfounder', 'P'));
  40. $rand = rand(100000, 999999);
  41. $seccodeinit = rawurlencode($this->authcode($rand, 'ENCODE', $authkey, 180));
  42. $errorcode = 0;
  43. if($this->submitcheck()) {
  44. if($isfounder == 1) {
  45. $username = 'UCenterAdministrator';
  46. }
  47. $can_do_login = $_ENV['user']->can_do_login($username, $this->onlineip);
  48. if(!$can_do_login) {
  49. $errorcode = UC_LOGIN_ERROR_FAILEDLOGIN;
  50. } else {
  51. $seccodehidden = urldecode(getgpc('seccodehidden', 'P'));
  52. $seccode = strtoupper(getgpc('seccode', 'P'));
  53. $seccodehidden = $this->authcode($seccodehidden, 'DECODE', $authkey);
  54. require UC_ROOT.'./lib/seccode.class.php';
  55. if(!seccode::seccode_check($seccodehidden, $seccode)) {
  56. $errorcode = UC_LOGIN_ERROR_SECCODE;
  57. } else {
  58. $errorcode = UC_LOGIN_SUCCEED;
  59. $this->user['username'] = $username;
  60. if($isfounder == 1) {
  61. $this->user['username'] = 'UCenterAdministrator';
  62. $md5password = md5(md5($password).UC_FOUNDERSALT);
  63. if($md5password == UC_FOUNDERPW) {
  64. $username = $this->user['username'];
  65. $this->view->sid = $this->sid_encode($this->user['username']);
  66. } else {
  67. $errorcode = UC_LOGIN_ERROR_FOUNDER_PW;
  68. }
  69. } else {
  70. $admin = $this->db->fetch_first("SELECT a.uid,m.username,m.salt,m.password FROM ".UC_DBTABLEPRE."admins a LEFT JOIN ".UC_DBTABLEPRE."members m USING(uid) WHERE a.username='$username'");
  71. if(!empty($admin)) {
  72. $md5password = md5(md5($password).$admin['salt']);
  73. if($admin['password'] == $md5password) {
  74. $this->view->sid = $this->sid_encode($admin['username']);
  75. } else {
  76. $errorcode = UC_LOGIN_ERROR_ADMIN_PW;
  77. }
  78. } else {
  79. $errorcode = UC_LOGIN_ERROR_ADMIN_NOT_EXISTS;
  80. }
  81. }
  82. if($errorcode == 0) {
  83. $this->setcookie('sid', $this->view->sid, 86400);
  84. $pwlen = strlen($password);
  85. $this->user['admin'] = 1;
  86. $this->writelog('login', 'succeed');
  87. if($iframe) {
  88. header('location: admin.php?m=frame&a=main&iframe=1'.($this->cookie_status ? '' : '&sid='.$this->view->sid));
  89. exit;
  90. } else {
  91. header('location: admin.php'.($this->cookie_status ? '' : '?sid='.$this->view->sid));
  92. exit;
  93. }
  94. } else {
  95. $this->writelog('login', 'error: user='.$this->user['username'].'; password='.($pwlen > 2 ? preg_replace("/^(.{".round($pwlen / 4)."})(.+?)(.{".round($pwlen / 6)."})$/s", "\\1***\\3", $password) : $password));
  96. $_ENV['user']->loginfailed($username, $this->onlineip);
  97. }
  98. }
  99. }
  100. }
  101. $username = dhtmlspecialchars($username);
  102. $password = dhtmlspecialchars($password);
  103. $this->view->assign('seccodeinit', $seccodeinit);
  104. $this->view->assign('username', $username);
  105. $this->view->assign('password', $password);
  106. $this->view->assign('isfounder', $isfounder);
  107. $this->view->assign('errorcode', $errorcode);
  108. $this->view->assign('iframe', $iframe);
  109. $this->view->display('admin_login');
  110. }
  111. function onlogout() {
  112. $this->writelog('logout');
  113. $this->setcookie('sid', '');
  114. header('location: admin.php');
  115. }
  116. function onadd() {
  117. if(!$this->submitcheck('submit')) {
  118. exit;
  119. }
  120. $username = getgpc('addname', 'P');
  121. $password = getgpc('addpassword', 'P');
  122. $email = getgpc('addemail', 'P');
  123. if(($status = $this->_check_username($username)) < 0) {
  124. if($status == UC_USER_CHECK_USERNAME_FAILED) {
  125. $this->message('user_add_username_ignore', 'BACK');
  126. } elseif($status == UC_USER_USERNAME_BADWORD) {
  127. $this->message('user_add_username_badwords', 'BACK');
  128. } elseif($status == UC_USER_USERNAME_EXISTS) {
  129. $this->message('user_add_username_exists', 'BACK');
  130. }
  131. }
  132. if(($status = $this->_check_email($email)) < 0) {
  133. if($status == UC_USER_EMAIL_FORMAT_ILLEGAL) {
  134. $this->message('user_add_email_formatinvalid', 'BACK');
  135. } elseif($status == UC_USER_EMAIL_ACCESS_ILLEGAL) {
  136. $this->message('user_add_email_ignore', 'BACK');
  137. } elseif($status == UC_USER_EMAIL_EXISTS) {
  138. $this->message('user_add_email_exists', 'BACK');
  139. }
  140. }
  141. $uid = $_ENV['user']->add_user($username, $password, $email);
  142. $this->message('user_add_succeed', 'admin.php?m=user&a=ls');
  143. }
  144. function onls() {
  145. include_once UC_ROOT.'view/default/admin.lang.php';
  146. $status = 0;
  147. if(!empty($_POST['addname']) && $this->submitcheck()) {
  148. $this->check_priv();
  149. $username = getgpc('addname', 'P');
  150. $password = getgpc('addpassword', 'P');
  151. $email = getgpc('addemail', 'P');
  152. if(($status = $this->_check_username($username)) >= 0) {
  153. if(($status = $this->_check_email($email)) >= 0) {
  154. $_ENV['user']->add_user($username, $password, $email);
  155. $status = 1;
  156. $this->writelog('user_add', "username=$username");
  157. }
  158. }
  159. }
  160. if($this->submitcheck() && !empty($_POST['delete'])) {
  161. $_ENV['user']->delete_user($_POST['delete']);
  162. $status = 2;
  163. $this->writelog('user_delete', "uid=".implode(',', $_POST['delete']));
  164. }
  165. $srchname = getgpc('srchname', 'R');
  166. $srchregdatestart = getgpc('srchregdatestart', 'R');
  167. $srchregdateend = getgpc('srchregdateend', 'R');
  168. $srchuid = intval(getgpc('srchuid', 'R'));
  169. $srchregip = trim(getgpc('srchregip', 'R'));
  170. $srchemail = trim(getgpc('srchemail', 'R'));
  171. $sqladd = $urladd = '';
  172. if($srchname) {
  173. $sqladd .= " AND username LIKE '$srchname%'";
  174. $this->view->assign('srchname', $srchname);
  175. }
  176. if($srchuid) {
  177. $sqladd .= " AND uid='$srchuid'";
  178. $this->view->assign('srchuid', $srchuid);
  179. }
  180. if($srchemail) {
  181. $sqladd .= " AND email='$srchemail'";
  182. $this->view->assign('srchemail', $srchemail);
  183. }
  184. if($srchregdatestart) {
  185. $urladd .= '&srchregdatestart='.$srchregdatestart;
  186. $sqladd .= " AND regdate>'".strtotime($srchregdatestart)."'";
  187. $this->view->assign('srchregdatestart', $srchregdatestart);
  188. }
  189. if($srchregdateend) {
  190. $urladd .= '&srchregdateend='.$srchregdateend;
  191. $sqladd .= " AND regdate<'".strtotime($srchregdateend)."'";
  192. $this->view->assign('srchregdateend', $srchregdateend);
  193. }
  194. if($srchregip) {
  195. $urladd .= '&srchregip='.$srchregip;
  196. $sqladd .= " AND regip='$srchregip'";
  197. $this->view->assign('srchregip', $srchregip);
  198. }
  199. $sqladd = $sqladd ? " WHERE 1 $sqladd" : '';
  200. $num = $_ENV['user']->get_total_num($sqladd);
  201. $userlist = $_ENV['user']->get_list($_GET['page'], UC_PPP, $num, $sqladd);
  202. foreach($userlist as $key => $user) {
  203. $user['smallavatar'] = '<img src="avatar.php?uid='.$user['uid'].'&size=small">';
  204. $userlist[$key] = $user;
  205. }
  206. $multipage = $this->page($num, UC_PPP, $_GET['page'], 'admin.php?m=user&a=ls&srchname='.$srchname.$urladd);
  207. $this->_format_userlist($userlist);
  208. $this->view->assign('userlist', $userlist);
  209. $adduser = getgpc('adduser');
  210. $a = getgpc('a');
  211. $this->view->assign('multipage', $multipage);
  212. $this->view->assign('adduser', $adduser);
  213. $this->view->assign('a', $a);
  214. $this->view->assign('status', $status);
  215. $this->view->display('admin_user');
  216. }
  217. function onedit() {
  218. $uid = getgpc('uid');
  219. $status = 0;
  220. if(!$this->user['isfounder']) {
  221. $isprotected = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."protectedmembers WHERE uid = '$uid'");
  222. if($isprotected) {
  223. $this->message('user_edit_noperm');
  224. }
  225. }
  226. if($this->submitcheck()) {
  227. $username = getgpc('username', 'P');
  228. $newusername = getgpc('newusername', 'P');
  229. $password = getgpc('password', 'P');
  230. $email = getgpc('email', 'P');
  231. $delavatar = getgpc('delavatar', 'P');
  232. $rmrecques = getgpc('rmrecques', 'P');
  233. $sqladd = '';
  234. if($username != $newusername) {
  235. if($_ENV['user']->get_user_by_username($newusername)) {
  236. $this->message('admin_user_exists');
  237. }
  238. $sqladd .= "username='$newusername', ";
  239. $this->load('note');
  240. $_ENV['note']->add('renameuser', 'uid='.$uid.'&oldusername='.urlencode($username).'&newusername='.urlencode($newusername));
  241. }
  242. if($password) {
  243. $salt = substr(uniqid(rand()), 0, 6);
  244. $orgpassword = $password;
  245. $password = md5(md5($password).$salt);
  246. $sqladd .= "password='$password', salt='$salt', ";
  247. $this->load('note');
  248. $_ENV['note']->add('updatepw', 'username='.urlencode($username).'&password=');
  249. }
  250. if($rmrecques) {
  251. $sqladd .= "secques='', ";
  252. }
  253. if(!empty($delavatar)) {
  254. $_ENV['user']->delete_useravatar($uid);
  255. }
  256. $this->db->query("UPDATE ".UC_DBTABLEPRE."members SET $sqladd email='$email' WHERE uid='$uid'");
  257. $status = $this->db->errno() ? -1 : 1;
  258. }
  259. $user = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."members WHERE uid='$uid'");
  260. $user['bigavatar'] = '<img src="avatar.php?uid='.$uid.'&size=big">';
  261. $user['bigavatarreal'] = '<img src="avatar.php?uid='.$uid.'&size=big&type=real">';
  262. $this->view->assign('uid', $uid);
  263. $this->view->assign('user', $user);
  264. $this->view->assign('status', $status);
  265. $this->view->display('admin_user');
  266. }
  267. function _check_username($username) {
  268. $username = addslashes(trim(stripslashes($username)));
  269. if(!$_ENV['user']->check_username($username)) {
  270. return UC_USER_CHECK_USERNAME_FAILED;
  271. } elseif($_ENV['user']->check_usernameexists($username)) {
  272. return UC_USER_USERNAME_EXISTS;
  273. }
  274. return 1;
  275. }
  276. function _check_email($email) {
  277. if(!$_ENV['user']->check_emailformat($email)) {
  278. return UC_USER_EMAIL_FORMAT_ILLEGAL;
  279. } elseif(!$_ENV['user']->check_emailaccess($email)) {
  280. return UC_USER_EMAIL_ACCESS_ILLEGAL;
  281. } elseif(!$this->settings['doublee'] && $_ENV['user']->check_emailexists($email)) {
  282. return UC_USER_EMAIL_EXISTS;
  283. } else {
  284. return 1;
  285. }
  286. }
  287. function _format_userlist(&$userlist) {
  288. if(is_array($userlist)) {
  289. foreach($userlist AS $key => $user) {
  290. $userlist[$key]['regdate'] = $this->date($user['regdate']);
  291. }
  292. }
  293. }
  294. }
  295. ?>