PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/uc_server/control/user.php

https://github.com/jinbo51/DiscuzX
PHP | 395 lines | 341 code | 46 blank | 8 comment | 77 complexity | 7a7b9460521ff70e24d70ce4546cea12 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 1059 2011-03-01 07:25:09Z monkey $
  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. class usercontrol extends base {
  15. function __construct() {
  16. $this->usercontrol();
  17. }
  18. function usercontrol() {
  19. parent::__construct();
  20. $this->load('user');
  21. }
  22. // -1 未开启
  23. function onsynlogin() {
  24. $this->init_input();
  25. $uid = $this->input('uid');
  26. if($this->app['synlogin']) {
  27. if($this->user = $_ENV['user']->get_user_by_uid($uid)) {
  28. $synstr = '';
  29. foreach($this->cache['apps'] as $appid => $app) {
  30. if($app['synlogin']) {
  31. $synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogin&username='.$this->user['username'].'&uid='.$this->user['uid'].'&password='.$this->user['password']."&time=".$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  32. if(is_array($app['extra']['extraurl'])) foreach($app['extra']['extraurl'] as $extraurl) {
  33. $synstr .= '<script type="text/javascript" src="'.$extraurl.'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogin&username='.$this->user['username'].'&uid='.$this->user['uid'].'&password='.$this->user['password']."&time=".$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  34. }
  35. }
  36. }
  37. return $synstr;
  38. }
  39. }
  40. return '';
  41. }
  42. function onsynlogout() {
  43. $this->init_input();
  44. if($this->app['synlogin']) {
  45. $synstr = '';
  46. foreach($this->cache['apps'] as $appid => $app) {
  47. if($app['synlogin']) {
  48. $synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogout&time='.$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  49. if(is_array($app['extra']['extraurl'])) foreach($app['extra']['extraurl'] as $extraurl) {
  50. $synstr .= '<script type="text/javascript" src="'.$extraurl.'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogout&time='.$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  51. }
  52. }
  53. }
  54. return $synstr;
  55. }
  56. return '';
  57. }
  58. function onregister() {
  59. $this->init_input();
  60. $username = $this->input('username');
  61. $password = $this->input('password');
  62. $email = $this->input('email');
  63. $questionid = $this->input('questionid');
  64. $answer = $this->input('answer');
  65. $regip = $this->input('regip');
  66. if(($status = $this->_check_username($username)) < 0) {
  67. return $status;
  68. }
  69. if(($status = $this->_check_email($email)) < 0) {
  70. return $status;
  71. }
  72. $uid = $_ENV['user']->add_user($username, $password, $email, 0, $questionid, $answer, $regip);
  73. return $uid;
  74. }
  75. function onedit() {
  76. $this->init_input();
  77. $username = $this->input('username');
  78. $oldpw = $this->input('oldpw');
  79. $newpw = $this->input('newpw');
  80. $email = $this->input('email');
  81. $ignoreoldpw = $this->input('ignoreoldpw');
  82. $questionid = $this->input('questionid');
  83. $answer = $this->input('answer');
  84. if(!$ignoreoldpw && $email && ($status = $this->_check_email($email, $username)) < 0) {
  85. return $status;
  86. }
  87. $status = $_ENV['user']->edit_user($username, $oldpw, $newpw, $email, $ignoreoldpw, $questionid, $answer);
  88. if($newpw && $status > 0) {
  89. $this->load('note');
  90. $_ENV['note']->add('updatepw', 'username='.urlencode($username).'&password=');
  91. $_ENV['note']->send();
  92. }
  93. return $status;
  94. }
  95. function onlogin() {
  96. $this->init_input();
  97. $isuid = $this->input('isuid');
  98. $username = $this->input('username');
  99. $password = $this->input('password');
  100. $checkques = $this->input('checkques');
  101. $questionid = $this->input('questionid');
  102. $answer = $this->input('answer');
  103. if($isuid == 1) {
  104. $user = $_ENV['user']->get_user_by_uid($username);
  105. } elseif($isuid == 2) {
  106. $user = $_ENV['user']->get_user_by_email($username);
  107. } else {
  108. $user = $_ENV['user']->get_user_by_username($username);
  109. }
  110. $passwordmd5 = preg_match('/^\w{32}$/', $password) ? $password : md5($password);
  111. if(empty($user)) {
  112. $status = -1;
  113. } elseif($user['password'] != md5($passwordmd5.$user['salt'])) {
  114. $status = -2;
  115. } elseif($checkques && $user['secques'] != '' && $user['secques'] != $_ENV['user']->quescrypt($questionid, $answer)) {
  116. $status = -3;
  117. } else {
  118. $status = $user['uid'];
  119. }
  120. $merge = $status != -1 && !$isuid && $_ENV['user']->check_mergeuser($username) ? 1 : 0;
  121. return array($status, $user['username'], $password, $user['email'], $merge);
  122. }
  123. function oncheck_email() {
  124. $this->init_input();
  125. $email = $this->input('email');
  126. return $this->_check_email($email);
  127. }
  128. function oncheck_username() {
  129. $this->init_input();
  130. $username = $this->input('username');
  131. if(($status = $this->_check_username($username)) < 0) {
  132. return $status;
  133. } else {
  134. return 1;
  135. }
  136. }
  137. function onget_user() {
  138. $this->init_input();
  139. $username = $this->input('username');
  140. if(!$this->input('isuid')) {
  141. $status = $_ENV['user']->get_user_by_username($username);
  142. } else {
  143. $status = $_ENV['user']->get_user_by_uid($username);
  144. }
  145. if($status) {
  146. return array($status['uid'],$status['username'],$status['email']);
  147. } else {
  148. return 0;
  149. }
  150. }
  151. function ongetprotected() {
  152. $protectedmembers = $this->db->fetch_all("SELECT uid,username FROM ".UC_DBTABLEPRE."protectedmembers GROUP BY username");
  153. return $protectedmembers;
  154. }
  155. function ondelete() {
  156. $this->init_input();
  157. $uid = $this->input('uid');
  158. return $_ENV['user']->delete_user($uid);
  159. }
  160. function ondeleteavatar() {
  161. $this->init_input();
  162. $uid = $this->input('uid');
  163. $_ENV['user']->delete_useravatar($uid);
  164. }
  165. function onaddprotected() {
  166. $this->init_input();
  167. $username = $this->input('username');
  168. $admin = $this->input('admin');
  169. $appid = $this->app['appid'];
  170. $usernames = (array)$username;
  171. foreach($usernames as $username) {
  172. $user = $_ENV['user']->get_user_by_username($username);
  173. $uid = $user['uid'];
  174. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."protectedmembers SET uid='$uid', username='$username', appid='$appid', dateline='{$this->time}', admin='$admin'", 'SILENT');
  175. }
  176. return $this->db->errno() ? -1 : 1;
  177. }
  178. function ondeleteprotected() {
  179. $this->init_input();
  180. $username = $this->input('username');
  181. $appid = $this->app['appid'];
  182. $usernames = (array)$username;
  183. foreach($usernames as $username) {
  184. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."protectedmembers WHERE username='$username' AND appid='$appid'");
  185. }
  186. return $this->db->errno() ? -1 : 1;
  187. }
  188. function onmerge() {
  189. $this->init_input();
  190. $oldusername = $this->input('oldusername');
  191. $newusername = $this->input('newusername');
  192. $uid = $this->input('uid');
  193. $password = $this->input('password');
  194. $email = $this->input('email');
  195. if(($status = $this->_check_username($newusername)) < 0) {
  196. return $status;
  197. }
  198. $uid = $_ENV['user']->add_user($newusername, $password, $email, $uid);
  199. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mergemembers WHERE appid='".$this->app['appid']."' AND username='$oldusername'");
  200. return $uid;
  201. }
  202. function onmerge_remove() {
  203. $this->init_input();
  204. $username = $this->input('username');
  205. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mergemembers WHERE appid='".$this->app['appid']."' AND username='$username'");
  206. return NULL;
  207. }
  208. function _check_username($username) {
  209. $username = addslashes(trim(stripslashes($username)));
  210. if(!$_ENV['user']->check_username($username)) {
  211. return UC_USER_CHECK_USERNAME_FAILED;
  212. } elseif(!$_ENV['user']->check_usernamecensor($username)) {
  213. return UC_USER_USERNAME_BADWORD;
  214. } elseif($_ENV['user']->check_usernameexists($username)) {
  215. return UC_USER_USERNAME_EXISTS;
  216. }
  217. return 1;
  218. }
  219. function _check_email($email, $username = '') {
  220. if(!$_ENV['user']->check_emailformat($email)) {
  221. return UC_USER_EMAIL_FORMAT_ILLEGAL;
  222. } elseif(!$_ENV['user']->check_emailaccess($email)) {
  223. return UC_USER_EMAIL_ACCESS_ILLEGAL;
  224. } elseif(!$this->settings['doublee'] && $_ENV['user']->check_emailexists($email, $username)) {
  225. return UC_USER_EMAIL_EXISTS;
  226. } else {
  227. return 1;
  228. }
  229. }
  230. function ongetcredit($arr) {
  231. $this->init_input();
  232. $appid = $this->input('appid');
  233. $uid = $this->input('uid');
  234. $credit = $this->input('credit');
  235. $this->load('note');
  236. $this->load('misc');
  237. $app = $this->cache['apps'][$appid];
  238. $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  239. if($app['extra']['apppath'] && @include $app['extra']['apppath'].'./api/'.$apifilename) {
  240. $uc_note = new uc_note();
  241. return $uc_note->getcredit(array('uid' => $uid, 'credit' => $credit), '');
  242. } else {
  243. $url = $_ENV['note']->get_url_code('getcredit', "uid=$uid&credit=$credit", $appid);
  244. return $_ENV['misc']->dfopen($url, 0, '', '', 1, $app['ip'], UC_NOTE_TIMEOUT);
  245. }
  246. }
  247. function onuploadavatar() {
  248. @header("Expires: 0");
  249. @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  250. @header("Pragma: no-cache");
  251. //header("Content-type: application/xml; charset=utf-8");
  252. $this->init_input(getgpc('agent', 'G'));
  253. $uid = $this->input('uid');
  254. if(empty($uid)) {
  255. return -1;
  256. }
  257. if(empty($_FILES['Filedata'])) {
  258. return -3;
  259. }
  260. list($width, $height, $type, $attr) = getimagesize($_FILES['Filedata']['tmp_name']);
  261. if(!in_array($type, array(1,2,3,6))) {
  262. @unlink($_FILES['Filedata']['tmp_name']);
  263. return -4;
  264. }
  265. $imgtype = array(1 => '.gif', 2 => '.jpg', 3 => '.png');
  266. $filetype = $imgtype[$type];
  267. if(!$filetype) $filetype = '.jpg';
  268. $tmpavatar = UC_DATADIR.'./tmp/upload'.$uid.$filetype;
  269. file_exists($tmpavatar) && @unlink($tmpavatar);
  270. if(@copy($_FILES['Filedata']['tmp_name'], $tmpavatar) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpavatar)) {
  271. @unlink($_FILES['Filedata']['tmp_name']);
  272. list($width, $height, $type, $attr) = getimagesize($tmpavatar);
  273. if($width < 10 || $height < 10 || $type == 4) {
  274. @unlink($tmpavatar);
  275. return -2;
  276. }
  277. } else {
  278. @unlink($_FILES['Filedata']['tmp_name']);
  279. return -4;
  280. }
  281. $avatarurl = UC_DATAURL.'/tmp/upload'.$uid.$filetype;
  282. return $avatarurl;
  283. }
  284. function onrectavatar() {
  285. @header("Expires: 0");
  286. @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  287. @header("Pragma: no-cache");
  288. header("Content-type: application/xml; charset=utf-8");
  289. $this->init_input(getgpc('agent'));
  290. $uid = $this->input('uid');
  291. if(empty($uid)) {
  292. return '<root><message type="error" value="-1" /></root>';
  293. }
  294. $home = $this->get_home($uid);
  295. if(!is_dir(UC_DATADIR.'./avatar/'.$home)) {
  296. $this->set_home($uid, UC_DATADIR.'./avatar/');
  297. }
  298. $avatartype = getgpc('avatartype', 'G') == 'real' ? 'real' : 'virtual';
  299. $bigavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'big', $avatartype);
  300. $middleavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'middle', $avatartype);
  301. $smallavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'small', $avatartype);
  302. $bigavatar = $this->flashdata_decode(getgpc('avatar1', 'P'));
  303. $middleavatar = $this->flashdata_decode(getgpc('avatar2', 'P'));
  304. $smallavatar = $this->flashdata_decode(getgpc('avatar3', 'P'));
  305. if(!$bigavatar || !$middleavatar || !$smallavatar) {
  306. return '<root><message type="error" value="-2" /></root>';
  307. }
  308. $success = 1;
  309. $fp = @fopen($bigavatarfile, 'wb');
  310. @fwrite($fp, $bigavatar);
  311. @fclose($fp);
  312. $fp = @fopen($middleavatarfile, 'wb');
  313. @fwrite($fp, $middleavatar);
  314. @fclose($fp);
  315. $fp = @fopen($smallavatarfile, 'wb');
  316. @fwrite($fp, $smallavatar);
  317. @fclose($fp);
  318. $biginfo = @getimagesize($bigavatarfile);
  319. $middleinfo = @getimagesize($middleavatarfile);
  320. $smallinfo = @getimagesize($smallavatarfile);
  321. if(!$biginfo || !$middleinfo || !$smallinfo || $biginfo[2] == 4 || $middleinfo[2] == 4 || $smallinfo[2] == 4) {
  322. file_exists($bigavatarfile) && unlink($bigavatarfile);
  323. file_exists($middleavatarfile) && unlink($middleavatarfile);
  324. file_exists($smallavatarfile) && unlink($smallavatarfile);
  325. $success = 0;
  326. }
  327. $filetype = '.jpg';
  328. @unlink(UC_DATADIR.'./tmp/upload'.$uid.$filetype);
  329. if($success) {
  330. return '<?xml version="1.0" ?><root><face success="1"/></root>';
  331. } else {
  332. return '<?xml version="1.0" ?><root><face success="0"/></root>';
  333. }
  334. }
  335. function flashdata_decode($s) {
  336. $r = '';
  337. $l = strlen($s);
  338. for($i=0; $i<$l; $i=$i+2) {
  339. $k1 = ord($s[$i]) - 48;
  340. $k1 -= $k1 > 9 ? 7 : 0;
  341. $k2 = ord($s[$i+1]) - 48;
  342. $k2 -= $k2 > 9 ? 7 : 0;
  343. $r .= chr($k1 << 4 | $k2);
  344. }
  345. return $r;
  346. }
  347. }
  348. ?>