user.php 13 KB

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