ApiController_old.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. <?php
  2. /**
  3. * MainController
  4. * Feel free to delete the methods and replace them with your own code.
  5. *
  6. * @author darkredz
  7. */
  8. class ApiController extends DooController {
  9. private $username, $config, $data;
  10. public function __construct() {
  11. // parent::__construct();
  12. // $this->config = pc_base::load_config('system');
  13. // /* 判断应用字符集和phpsso字符集是否相同,如果不相同,转换用户名为phpsso所用字符集 */
  14. // $this->username = isset($this->data['username']) ? $this->data['username'] : '';
  15. //
  16. // if ($this->username && CHARSET != $this->applist[$this->appid]['charset']) {
  17. // if ($this->applist[$this->appid]['charset'] == 'utf-8') { //判断应用字符集是否为utf-8编码
  18. // //应用字符集如果是utf-8,并且用户名是utf-8编码,转换用户名为phpsso字符集,如果为英文,is_utf8返回false,不进行转换
  19. // if (is_utf8($this->username)) {
  20. // $this->username = iconv($this->applist[$this->appid]['charset'], CHARSET, $this->username);
  21. // }
  22. // } else {
  23. // if (!is_utf8($this->username)) {
  24. // $this->username = iconv($this->applist[$this->appid]['charset'], CHARSET, $this->username);
  25. // }
  26. // }
  27. // }
  28. // $authstr = $this->authcode($_POST['data']);
  29. //// $authstr = $this->authcode($this->params['authstr']);
  30. // $ddddd = explode('&', urldecode($authstr));
  31. // foreach ($ddddd as $k => $v) {
  32. // list($key, $val) = explode('=', $v);
  33. // $this->$key = $val;
  34. // }
  35. }
  36. /**
  37. * 用户注册
  38. * @param string $username 用户名
  39. * @param string $password 密码
  40. * @param string $email email
  41. * @return int {-1:用户名已经存在 ;-2:email已存在;-4:用户名禁止注册;-5:邮箱禁止注册;-6:uc注册失败;int(uid):成功}
  42. */
  43. public function register() {
  44. $this->random = isset($this->data['random']) && !empty($this->data['random']) ? $this->data['random'] : create_randomstr(6);
  45. $this->password = isset($this->data['password']) ? create_password($this->data['password'], $this->random) : '';
  46. $this->email = isset($this->data['email']) ? $this->data['email'] : '';
  47. $this->type = isset($this->appid) ? 'app' : 'connect';
  48. $this->regip = isset($this->data['regip']) ? $this->data['regip'] : '';
  49. $this->appid = isset($this->appid) ? $this->appid : '';
  50. $this->appname = $this->applist[$this->appid]['name'];
  51. $checkname = $this->checkname(1);
  52. if ($checkname == -1) {
  53. exit('-1');
  54. } elseif ($checkname == -4) {
  55. exit('-4');
  56. }
  57. $checkemail = $this->checkemail(1);
  58. if ($checkemail == -1) {
  59. exit('-2');
  60. } elseif ($checkemail == -5) {
  61. exit('-5');
  62. }
  63. //UCenter会员注册
  64. $ucuserid = 0;
  65. if ($this->config['ucuse']) {
  66. pc_base::load_config('uc_config');
  67. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  68. $uid = uc_user_register($this->username, $this->data['password'], $this->email, $this->random);
  69. if (is_numeric($uid)) {
  70. switch ($uid) {
  71. case '-3':
  72. exit('-1');
  73. break;
  74. case '-6':
  75. exit('-2');
  76. break;
  77. case '-2':
  78. exit('-4');
  79. break;
  80. case '-5':
  81. exit('-5');
  82. break;
  83. case '-1':
  84. exit('-4');
  85. break;
  86. case '-4':
  87. exit('-5');
  88. break;
  89. default :
  90. $ucuserid = $uid;
  91. break;
  92. }
  93. } else {
  94. exit('-6');
  95. }
  96. }
  97. $data = array(
  98. 'username' => $this->username,
  99. 'password' => $this->password,
  100. 'email' => $this->email,
  101. 'regip' => $this->regip,
  102. 'regdate' => SYS_TIME,
  103. 'lastdate' => SYS_TIME,
  104. 'appname' => $this->appname,
  105. 'type' => $this->type,
  106. 'random' => $this->random,
  107. 'ucuserid' => $ucuserid
  108. );
  109. $uid = $this->db->insert($data, 1);
  110. /* 插入消息队列 */
  111. $noticedata = $data;
  112. $noticedata['uid'] = $uid;
  113. messagequeue::add('member_add', $noticedata);
  114. exit("$uid"); //exit($uid) 不可以If status is an integer, that value will also be used as the exit status.
  115. }
  116. /**
  117. * 编辑用户,可以不传入旧密码和新密码
  118. * 如果传入新密码,则修改密码为新密码
  119. * @param string $username 用户名
  120. * @param string $password 旧密码
  121. * @param string $newpassword 新密码
  122. * @param string $email email
  123. * @param string $random 密码随机数
  124. * @return int {-1:用户不存在;-2:旧密码错误;-3:email已经存在 ;1:成功;0:未作修改}
  125. */
  126. public function edit() {
  127. header("Content-type: text/html; charset=utf-8");
  128. $authstr = $this->authcode($_POST['data']);
  129. // var_dump($authstr);
  130. $ddddd = explode('&', urldecode($authstr));
  131. // var_dump($ddddd);
  132. foreach ($ddddd as $k => $v) {
  133. list($key, $val) = explode('=', $v);
  134. $TmpArray[$key] = $val;
  135. }
  136. // debug_zval_dump($TmpArray);
  137. // 比对 旧密码 回答问题
  138. // getuserinfo
  139. // $this->email = isset($this->data['email']) ? $this->data['email'] : '';
  140. // $this->uid = isset($this->data['uid']) ? $this->data['uid'] : '';
  141. //
  142. $userinfo = $this->__getuserinfo($TmpArray['uid']);
  143. // var_dump($userinfo);
  144. // // 不为空
  145. // string(1) "3" refcount(1)
  146. // ["password"] =>
  147. // string(6) "dddddd" refcount(1)
  148. // ["newpassword"] =>
  149. // string(6) "wwwwww" refcount(1)
  150. // ["useranswer"] =>
  151. // if (isset($TmpArray['password']) && !empty($TmpArray['password'])) {
  152. //
  153. // }
  154. // if (isset($TmpArray['newpassword']) && !empty($TmpArray['newpassword'])) {
  155. // $this->data['randomstr'] = $this->create_randomstr();
  156. // $this->data['newpasswd'] = $this->create_password($TmpArray['newpassword'], $this->data['randomstr']);
  157. // }
  158. //
  159. // $this->random = !empty($this->data['random']) ? $this->data['random'] : $userinfo['random'];
  160. // if (isset($this->data['newpassword']) && !empty($this->data['newpassword'])) {
  161. // $this->newpassword = create_password($this->data['newpassword'], $this->random);
  162. // }
  163. //
  164. if (!$userinfo) {
  165. exit('-1');
  166. }
  167. $passworld = $this->create_password($TmpArray['password'], $userinfo[0]['salt']);
  168. if (isset($this->password) && !empty($this->password) && ($passworld != $userinfo[0]['userpasswd'])) {
  169. exit('-2');
  170. }
  171. // echo urldecode($TmpArray['useranswer']) . '|' . $userinfo[0]['useranswer'];
  172. if ($TmpArray['useranswer'] && ($userinfo[0]['useranswer'] != $TmpArray['useranswer'])) {
  173. exit('-3');
  174. }
  175. $this->data['randomstr'] = $this->create_randomstr();
  176. $this->data['newpasswd'] = $this->create_password($TmpArray['newpassword'], $this->data['randomstr']);
  177. Doo::loadModel('users');
  178. $objuser = new Users();
  179. $objuser->userpasswd = $this->data['newpasswd'];
  180. $objuser->salt = $this->data['randomstr'];
  181. $RetValue = $objuser->update(array('where' => 'id=' . $TmpArray['uid'] . ' AND useranswer=\'' . $TmpArray['useranswer'] . '\''));
  182. return $RetValue;
  183. //
  184. // $data = array();
  185. // $data['appname'] = $this->applist[$this->appid]['name'];
  186. //
  187. // if (!empty($this->email) && $userinfo['email'] != $this->email) {
  188. // $data['email'] = $this->email;
  189. // }
  190. //
  191. // if (isset($this->newpassword) && $userinfo['password'] != $this->newpassword) {
  192. // $data['password'] = $this->newpassword;
  193. // $data['random'] = $this->random;
  194. // }
  195. }
  196. /**
  197. * 生成随机字符串
  198. * @param string $lenth 长度
  199. * @return string 字符串
  200. */
  201. function create_randomstr($lenth = 6) {
  202. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  203. }
  204. /**
  205. *
  206. * @param $password 密码
  207. * @param $random 随机数
  208. */
  209. function create_password($password = '', $random = '') {
  210. if (empty($random)) {
  211. $array['random'] = $this->create_randomstr();
  212. $array['password'] = md5(md5($password) . $array['random']);
  213. return $array;
  214. }
  215. return md5(md5($password) . $random);
  216. }
  217. /**
  218. * 随机字符串函数
  219. * @param $password 密码
  220. * @param $random 随机数
  221. */
  222. function random($length, $chars = '0123456789') {
  223. $hash = '';
  224. $max = strlen($chars) - 1;
  225. for ($i = 0; $i < $length; $i++) {
  226. $hash .= $chars[mt_rand(0, $max)];
  227. }
  228. return $hash;
  229. }
  230. /**
  231. * 删除用户
  232. * @param string {$uid:用户id;$username:用户名;$email:email}
  233. * @return array {-1:删除失败;>0:删除成功}
  234. */
  235. public function delete() {
  236. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : '';
  237. $this->email = isset($this->data['email']) ? $this->data['email'] : '';
  238. if ($this->uid > 0 || is_array($this->uid)) {
  239. $where = to_sqls($this->uid, '', 'uid');
  240. //ucenter部份
  241. if ($this->config['ucuse']) {
  242. pc_base::load_config('uc_config');
  243. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  244. $s = $this->db->select($where, 'ucuserid');
  245. if ($s) {
  246. $uc_data = array();
  247. foreach ($s as $k => $v) {
  248. $uc_data[$k] = $v['ucuserid'];
  249. }
  250. if (!empty($uc_data))
  251. $r = uc_user_delete($uc_data);
  252. if (!$r) {
  253. exit('-1');
  254. }
  255. } else {
  256. exit('-1');
  257. }
  258. }
  259. /* 插入消息队列 */
  260. $noticedata['uids'] = $this->uid;
  261. messagequeue::add('member_delete', $noticedata);
  262. $this->db->delete($where);
  263. exit('1');
  264. } elseif (!empty($this->username)) {
  265. $this->db->delete(array('username' => $this->username));
  266. exit('2');
  267. } elseif (!empty($this->email)) {
  268. $this->db->delete(array('email' => $this->email));
  269. exit('3');
  270. } else {
  271. exit('-1');
  272. }
  273. }
  274. /**
  275. * 获取用户信息
  276. * @param string {$uid:用户id;$username:用户名;$email:email}
  277. * @return array {-1:用户不存在;array(userinfo):用户信息}
  278. */
  279. private function __getuserinfo($uid) {
  280. if ($uid > 0) {
  281. $r = Doo::db()->find('users', array(
  282. 'where' => 'id=?',
  283. 'param' => array($uid),
  284. 'asArray' => TRUE
  285. )
  286. );
  287. } elseif (!empty($this->username)) {
  288. $r = $this->db->get_one(array('username' => $this->username));
  289. } elseif (!empty($this->email)) {
  290. $r = $this->db->get_one(array('email' => $this->email));
  291. } else {
  292. return false;
  293. }
  294. return $r;
  295. }
  296. function getuserlist(){
  297. $authstr = $this->authcode($_POST['data']);
  298. $ddddd = explode('&', urldecode($authstr));
  299. foreach ($ddddd as $k => $v) {
  300. list($key, $val) = explode('=', $v);
  301. $this->$key = $val;
  302. }
  303. $r = Doo::db()->find('users', array(
  304. 'select'=>'id,username',
  305. 'where' => 'id in ( '.$this->uid.' )',
  306. 'param' => array($this->uid),
  307. 'asArray' => TRUE
  308. )
  309. );
  310. exit(json_encode($r));
  311. }
  312. /**
  313. * 获取用户信息
  314. * @param string {$uid:用户id;$username:用户名;$email:email}
  315. * @return array {-1:用户不存在;array(userinfo):用户信息}
  316. */
  317. public function getuserinfo() {
  318. // TODO:远程字符串解析提取出来做成函数
  319. $authstr = $this->authcode($_POST['data']);
  320. $ddddd = explode('&', urldecode($authstr));
  321. foreach ($ddddd as $k => $v) {
  322. list($key, $val) = explode('=', $v);
  323. $TmpArray[$key] = $val;
  324. $this->$key = $val;
  325. }
  326. $uid = $TmpArray['uid'];
  327. // var_dump($uid);
  328. if ($uid > 0) {
  329. $r = Doo::db()->find('users', array(
  330. 'where' => 'id=?',
  331. 'param' => array($uid),
  332. 'asArray' => TRUE
  333. )
  334. );
  335. } elseif (!empty($this->username)) {
  336. $r = Doo::db()->find('users', array(
  337. 'where' => 'username=?',
  338. 'param' => array($this->username),
  339. 'asArray' => TRUE
  340. )
  341. );
  342. } elseif (!empty($this->email)) {
  343. $r = $this->db->get_one(array('email' => $this->email));
  344. } else {
  345. return false;
  346. }
  347. if ($this->username) {
  348. exit(json_encode($r));
  349. } else {
  350. exit(json_encode($r));
  351. }
  352. }
  353. function isMail($mail) {
  354. // if (preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9\-\.])+/", $mail))
  355. if (filter_var($mail, FILTER_VALIDATE_EMAIL)){
  356. return true;
  357. }else{
  358. return false;
  359. }
  360. }
  361. public function checkauth(){
  362. $authstr = $this->authcode($_POST['data']);
  363. $ddddd = explode('&', urldecode($authstr));
  364. foreach ($ddddd as $k => $v) {
  365. list($key, $val) = explode('=', $v);
  366. $this->$key = $val;
  367. }
  368. $k=explode('_', $this->auth);
  369. $userinfo = Doo::db()->find('users', array(
  370. 'select' => 'id,keys_auth',
  371. 'where' => 'id=?',
  372. 'param' => array($k[0]),
  373. ));
  374. if(empty($userinfo[0]->keys_auth)){
  375. echo '2';die;
  376. }
  377. $dd = explode('_', $userinfo[0]->keys_auth);
  378. Doo::loadModel('users');
  379. $users = new Users();
  380. if(!isset($dd[1])){
  381. $users->keys_auth=$k[1]."_1";
  382. $users->update(array('where' => 'id=?', 'param' => array($k[0])));
  383. }else{
  384. //过程认证完成
  385. if(isset($k[2])){
  386. $users->keys_auth="";
  387. $users->update(array('where' => 'id=?', 'param' => array($k[0])));
  388. }
  389. }
  390. if($dd[0]==$k[1]){
  391. echo '1';die;
  392. }else{
  393. echo '2';die;
  394. }
  395. }
  396. /**
  397. * 用户登录
  398. * @param string $username 用户名
  399. * @param string $password 密码
  400. * @return array {-2;密码错误;-1:用户不存在;array(userinfo):用户信息}
  401. */
  402. public function login() {
  403. $authstr = $this->authcode($_POST['data']);
  404. $ddddd = explode('&', urldecode($authstr));
  405. foreach ($ddddd as $k => $v) {
  406. list($key, $val) = explode('=', $v);
  407. $this->$key = $val;
  408. }
  409. if ($this->isMail($this->email)) {
  410. $userinfo = Doo::db()->find('users', array(
  411. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth',
  412. 'where' => 'useremail=?',
  413. 'param' => array($this->email),
  414. ));
  415. }
  416. if ($this->username) {
  417. $userinfo = Doo::db()->find('users', array(
  418. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth',
  419. 'where' => 'username=?',
  420. 'param' => array($this->username),
  421. ));
  422. }
  423. if (empty($userinfo))
  424. exit('-22');
  425. if (!empty($userinfo) && $userinfo[0]->userpasswd == $this->create_password($this->userpasswd, $userinfo[0]->salt)) {
  426. //登录成功更新用户最近登录时间和ip
  427. Doo::loadModel('users');
  428. $users = new Users();
  429. $key=$this->random_k(8);
  430. $users->keys_auth=$key;
  431. $users->lastloginip = ip2long($this->clientIP());
  432. $users->lastlogintime = time();
  433. $users->update(array('where' => 'id=?', 'param' => array($userinfo[0]->id)));
  434. $userinfo[0]->keys_auth=$key;
  435. // $this->LoginSetCookie($userinfo);
  436. // $res = '<script type="text/javascript" src="http://192.168.1.140:8012/zhsso/api/synlogin/' . $userinfo[0]->username . '"></script>';\
  437. Doo::loadModel('userswitch');
  438. $userswitch = new UserSwitch();
  439. $usArray = $userswitch->getOne(array('select' => 'actime', 'where' => 'uid=?', 'param' => array($userinfo[0]->id), 'asArray' => TRUE));
  440. if (!$usArray['actime'])
  441. exit('-3');
  442. echo json_encode($userinfo);
  443. die();
  444. } else {
  445. exit('-2');
  446. }
  447. }
  448. public function scLogin(){
  449. $authstr = $this->authcode($_POST['data']);
  450. $ddddd = explode('&', urldecode($authstr));
  451. foreach ($ddddd as $k => $v) {
  452. list($key, $val) = explode('=', $v);
  453. $this->$key = $val;
  454. }
  455. if ($this->isMail($this->email)) {
  456. $userinfo = Doo::db()->find('users', array(
  457. 'select' => 'id,username,userpasswd,useremail,salt,actstate,keys_auth',
  458. 'where' => 'useremail=?',
  459. 'param' => array($this->email),
  460. ));
  461. }
  462. if ($this->username) {
  463. $userinfo = Doo::db()->find('users', array(
  464. 'select' => 'id,username,userpasswd,useremail,salt,actstate,keys_auth',
  465. 'where' => 'username=?',
  466. 'param' => array($this->username),
  467. ));
  468. }
  469. if (empty($userinfo))
  470. exit('-22');
  471. if (!empty($userinfo) && $userinfo[0]->userpasswd == $this->create_password($this->userpasswd, $userinfo[0]->salt)) {
  472. //登录成功更新用户最近登录时间和ip
  473. Doo::loadModel('users');
  474. $users = new Users();
  475. $key=$this->random_k(8);
  476. $users->keys_auth=$key;
  477. $users->lastloginip = ip2long($this->clientIP());
  478. $users->lastlogintime = time();
  479. $users->update(array('where' => 'id=?', 'param' => array($userinfo[0]->id)));
  480. $userinfo[0]->keys_auth=$key;
  481. // $this->LoginSetCookie($userinfo);
  482. // $res = '<script type="text/javascript" src="http://192.168.1.140:8012/zhsso/api/synlogin/' . $userinfo[0]->username . '"></script>';\
  483. Doo::loadModel('userswitch');
  484. Doo::loadModel('scUser');
  485. $scUser=new scUser();
  486. $userswitch = new UserSwitch();
  487. $usArray = $userswitch->getOne(array('select' => 'actime', 'where' => 'uid=?', 'param' => array($userinfo[0]->id), 'asArray' => TRUE));
  488. $scU=$scUser->find(array('where' => 'uid='.$userinfo[0]->id, 'asArray' => TRUE));
  489. $scU[0]['userName']=$userinfo[0]->username;$scU[0]['userpasswd']=$userinfo[0]->userpasswd;$scU[0]['useremail']=$userinfo[0]->useremail;
  490. if (!$usArray['actime'])
  491. exit('-3');
  492. echo json_encode($scU);
  493. die();
  494. } else {
  495. exit('-2');
  496. }
  497. }
  498. /**
  499. * 同步登陆
  500. * @param string $uid 用户id
  501. * @return string javascript用户同步登陆js
  502. */
  503. public function synlogin() {
  504. //判断本应用是否开启同步登陆
  505. if ($this->applist[$this->appid]['synlogin']) {
  506. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : '';
  507. $this->password = isset($this->data['password']) ? $this->data['password'] : '';
  508. $res = '';
  509. //ucenter登陆部份
  510. if ($this->config['ucuse']) {
  511. pc_base::load_config('uc_config');
  512. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  513. $r = $this->db->get_one(array('uid' => $this->uid), "ucuserid");
  514. if ($r['ucuserid'])
  515. $res .= uc_user_synlogin($r['ucuserid']);
  516. }
  517. foreach ($this->applist as $v) {
  518. if (!$v['synlogin'])
  519. continue;
  520. if ($v['appid'] != $this->appid) {
  521. $tmp_s = strstr($v['url'] . $v['apifilename'], '?') ? '&' : '?';
  522. $res .= '<script type="text/javascript" src="' . $v['url'] . $v['apifilename'] . $tmp_s . 'time=' . SYS_TIME . '&code=' . urlencode(sys_auth('action=synlogin&username=' . $this->username . '&uid=' . $this->uid . '&password=' . $this->password . "&time=" . SYS_TIME, 'ENCODE', $v['authkey'])) . '" reload="1"></script>';
  523. }
  524. }
  525. exit($res);
  526. } else {
  527. exit('0');
  528. }
  529. }
  530. /**
  531. * 同步退出
  532. * @return string javascript用户同步退出js
  533. */
  534. public function synlogout() {
  535. if ($this->applist[$this->appid]['synlogin']) {
  536. $res = '';
  537. //ucenter登陆部份
  538. if ($this->config['ucuse']) {
  539. pc_base::load_config('uc_config');
  540. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  541. $res .= uc_user_synlogout();
  542. }
  543. foreach ($this->applist as $v) {
  544. if (!$v['synlogin'])
  545. continue;
  546. if ($v['appid'] != $this->appid) {
  547. $tmp_s = strstr($v['url'] . $v['apifilename'], '?') ? '&' : '?';
  548. $res .= '<script type="text/javascript" src="' . $v['url'] . $v['apifilename'] . $tmp_s . 'time=' . SYS_TIME . '&code=' . urlencode(sys_auth('action=synlogout&time=' . SYS_TIME, 'ENCODE', $v['authkey'])) . '" reload="1"></script>';
  549. }
  550. }
  551. exit($res);
  552. } else {
  553. exit;
  554. }
  555. }
  556. /**
  557. * 获取应用列表
  558. */
  559. public function getapplist() {
  560. $applist = getcache('applist', 'admin');
  561. exit(serialize($applist));
  562. }
  563. /**
  564. * 获取积分兑换规则
  565. */
  566. public function getcredit($return = '') {
  567. $creditcache = getcache('creditlist', 'admin');
  568. foreach ($creditcache as $v) {
  569. if ($v['fromid'] == $this->appid) {
  570. $creditlist[$v['from'] . '_' . $v['to']] = $v;
  571. }
  572. }
  573. if ($return) {
  574. return $creditlist;
  575. } else {
  576. exit(serialize($creditlist));
  577. }
  578. }
  579. /**
  580. * 兑换积分
  581. * @param int $uid phpssouid
  582. * @param int $from 本系统积分类型id
  583. * @param int $toappid 目标系统应用appid
  584. * @param int $to 目标系统积分类型id
  585. * @param int $credit 本系统扣除积分数
  586. * @return bool {1:成功;0:失败}
  587. */
  588. public function changecredit() {
  589. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : exit('0');
  590. $this->toappid = isset($this->data['toappid']) ? $this->data['toappid'] : exit('0');
  591. $this->from = isset($this->data['from']) ? $this->data['from'] : exit('0');
  592. $this->to = isset($this->data['to']) ? $this->data['to'] : exit('0');
  593. $this->credit = isset($this->data['credit']) ? $this->data['credit'] : exit('0');
  594. $this->appname = $this->applist[$this->appid]['name'];
  595. $outcredit = $this->getcredit(1);
  596. //目标系统积分增加数
  597. $this->credit = floor($this->credit * $outcredit[$this->from . '_' . $this->to]['torate'] / $outcredit[$this->from . '_' . $this->to]['fromrate']);
  598. /* 插入消息队列 */
  599. $noticedata['appname'] = $this->appname;
  600. $noticedata['uid'] = $this->uid;
  601. $noticedata['toappid'] = $this->toappid;
  602. $noticedata['totypeid'] = $this->to;
  603. $noticedata['credit'] = $this->credit;
  604. messagequeue::add('change_credit', $noticedata);
  605. exit('1');
  606. }
  607. /**
  608. * 检查用户名
  609. * @param string $username 用户名
  610. * @return int {-4:用户名禁止注册;-1:用户名已经存在 ;1:成功}
  611. */
  612. public function checkname($is_return = 0) {
  613. if (empty($this->username)) {
  614. if ($is_return) {
  615. return -1;
  616. } else {
  617. exit('-1');
  618. }
  619. }
  620. //非法关键词判断
  621. $denyusername = $this->settings['denyusername'];
  622. if (is_array($denyusername)) {
  623. $denyusername = implode("|", $denyusername);
  624. $pattern = '/^(' . str_replace(array('\\*', ' ', "\|"), array('.*', '', '|'), preg_quote($denyusername, '/')) . ')$/i';
  625. if (preg_match($pattern, $this->username)) {
  626. if ($is_return) {
  627. return -4;
  628. } else {
  629. exit('-4');
  630. }
  631. }
  632. }
  633. //UCenter部分
  634. if ($this->config['ucuse']) {
  635. pc_base::load_config('uc_config');
  636. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  637. $rs = uc_user_checkname($this->username);
  638. if ($rs < 1) {
  639. exit('-4');
  640. }
  641. }
  642. $r = $this->db->get_one(array('username' => $this->username));
  643. if ($is_return) {
  644. return !empty($r) ? -1 : 1;
  645. } else {
  646. echo!empty($r) ? -1 : 1;
  647. exit;
  648. }
  649. }
  650. /**
  651. * 检查email
  652. * @param string $email email
  653. * @return int {-1:email已经存在 ;-5:邮箱禁止注册;1:成功}
  654. */
  655. public function checkemail($is_return = 0) {
  656. $this->email = isset($this->email) ? $this->email : isset($this->data['email']) ? $this->data['email'] : '';
  657. if (empty($this->email)) {
  658. if ($is_return) {
  659. return -1;
  660. } else {
  661. exit('-1');
  662. }
  663. }
  664. //非法关键词判断
  665. $denyemail = $this->settings['denyemail'];
  666. if (is_array($denyemail)) {
  667. $denyemail = implode("|", $denyemail);
  668. $pattern = '/^(' . str_replace(array('\\*', ' ', "\|"), array('.*', '', '|'), preg_quote($denyemail, '/')) . ')$/i';
  669. if (preg_match($pattern, $this->email)) {
  670. if ($is_return) {
  671. return -5;
  672. } else {
  673. exit('-5');
  674. }
  675. }
  676. }
  677. //UCenter部分
  678. if ($this->config['ucuse']) {
  679. pc_base::load_config('uc_config');
  680. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  681. $rs = uc_user_checkemail($this->email);
  682. if ($rs < 1) {
  683. exit('-5');
  684. }
  685. }
  686. $r = $this->db->get_one(array('email' => $this->email));
  687. if ($is_return) {
  688. return !empty($r) ? -1 : 1;
  689. } else {
  690. !empty($r) ? exit('-1') : exit('1');
  691. }
  692. }
  693. /**
  694. * 上传头像处理
  695. * 传入头像压缩包,解压到指定文件夹后删除非图片文件
  696. */
  697. public function uploadavatar() {
  698. //根据用户id创建文件夹
  699. $encodestr = $this->authcode(rawurldecode($this->params['authstr']));
  700. if (isset($encodestr)) {
  701. $this->uid = $encodestr;
  702. } else {
  703. exit('0');
  704. }
  705. $dir1 = ceil($this->uid / 10000);
  706. $dir2 = ceil($this->uid % 10000 / 1000);
  707. //创建图片存储文件夹
  708. $avatarfile = 'data/avatar/';
  709. $dir = $avatarfile . $dir1 . '/' . $dir2 . '/' . $this->uid . '/';
  710. if (!file_exists($dir)) {
  711. mkdir($dir, 0777, true);
  712. }
  713. //存储flashpost图片
  714. $this->data['avatardata'] = $GLOBALS['HTTP_RAW_POST_DATA'];
  715. $filename = Doo::conf()->SITE_PATH . $dir . $this->uid . '.zip';
  716. file_put_contents($filename, $this->data['avatardata']);
  717. //解压缩文件
  718. Doo::loadClass('pclzip.lib');
  719. // pc_base::load_app_class('pclzip', 'phpsso', 0);
  720. $archive = new PclZip($filename);
  721. if ($archive->extract(PCLZIP_OPT_PATH, $dir) == 0) {
  722. die("Error : " . $archive->errorInfo(true));
  723. }
  724. // 判断文件安全,删除压缩包和非jpg图片
  725. $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
  726. if ($handle = opendir($dir)) {
  727. while (false !== ($file = readdir($handle))) {
  728. if ($file !== '.' && $file !== '..') {
  729. if (!in_array($file, $avatararr)) {
  730. @unlink($dir . $file);
  731. } else {
  732. $info = @getimagesize($dir . $file);
  733. if (!$info || $info[2] != 2) {
  734. @unlink($dir . $file);
  735. }
  736. }
  737. }
  738. }
  739. closedir($handle);
  740. }
  741. // $this->db->update(array('avatar' => 1), array('uid' => $this->uid));
  742. exit('1');
  743. }
  744. /**
  745. * 存储支付方式
  746. *
  747. */
  748. public function setPayWay() {
  749. $authstr = $this->authcode($_POST['data']);
  750. // var_dump($authstr);
  751. $ddddd = explode('&', urldecode($authstr));
  752. // var_dump($ddddd);
  753. foreach ($ddddd as $k => $v) {
  754. list($key, $val) = explode(' = ', $v);
  755. $TmpArray[$key] = $val;
  756. }
  757. // var_dump($TmpArray);
  758. Doo::loadModel('users_payway');
  759. $upayway = new UsersPayway();
  760. $upayway->uid = $TmpArray['uid'];
  761. $upayway->idbank = $TmpArray['idbank'];
  762. $upayway->idalipay = $TmpArray['alipay'];
  763. $upayway->idtenpay = $TmpArray['tenpay'];
  764. if ($upayway->insert())
  765. exit('1');
  766. }
  767. /**
  768. * 实名认证
  769. *
  770. */
  771. public function setVerify() {
  772. Doo::loadModel('users_verify');
  773. }
  774. /**
  775. * 删除用户头像
  776. * @return {0:失败;1:成功}
  777. */
  778. public function deleteavatar() {
  779. //根据用户id创建文件夹
  780. if (isset($this->data['uid'])) {
  781. $this->uid = $this->data['uid'];
  782. } else {
  783. exit('0');
  784. }
  785. $dir1 = ceil($this->uid / 10000);
  786. $dir2 = ceil($this->uid % 10000 / 1000);
  787. //图片存储文件夹
  788. $avatarfile = pc_base::load_config('system', 'upload_path') . 'avatar/';
  789. $dir = $avatarfile . $dir1 . '/' . $dir2 . '/' . $this->uid . '/';
  790. $this->db->update(array('avatar' => 0), array('uid' => $this->uid));
  791. if (!file_exists($dir)) {
  792. exit('1');
  793. } else {
  794. if ($handle = opendir($dir)) {
  795. while (false !== ($file = readdir($handle))) {
  796. if ($file !== '.' && $file !== '..') {
  797. @unlink($dir . $file);
  798. }
  799. }
  800. closedir($handle);
  801. @rmdir($dir);
  802. exit('1');
  803. }
  804. }
  805. }
  806. /**
  807. *
  808. * @param type $string
  809. * @param type $operation
  810. * @param type $key
  811. * @param type $expiry
  812. * @return string
  813. */
  814. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  815. $ckey_length = 4;
  816. $key = md5($key != '' ? $key : Doo::conf()->AUTHKEY);
  817. $keya = md5(substr($key, 0, 16));
  818. $keyb = md5(substr($key, 16, 16));
  819. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  820. $cryptkey = $keya . md5($keya . $keyc);
  821. $key_length = strlen($cryptkey);
  822. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  823. $string_length = strlen($string);
  824. $result = '';
  825. $box = range(0, 255);
  826. $rndkey = array();
  827. for ($i = 0; $i <= 255; $i++) {
  828. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  829. }
  830. for ($j = $i = 0; $i < 256; $i++) {
  831. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  832. $tmp = $box[$i];
  833. $box[$i] = $box[$j];
  834. $box[$j] = $tmp;
  835. }
  836. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  837. $a = ($a + 1) % 256;
  838. $j = ($j + $box[$a]) % 256;
  839. $tmp = $box[$a];
  840. $box[$a] = $box[$j];
  841. $box[$j] = $tmp;
  842. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  843. }
  844. if ($operation == 'DECODE') {
  845. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  846. return substr($result, 26);
  847. } else {
  848. return '';
  849. }
  850. } else {
  851. return $keyc . str_replace(' = ', '', base64_encode($result));
  852. }
  853. }
  854. public function LoginSetCookie() {
  855. // if (empty($this->params['username']))
  856. // return FALSE;
  857. $user = Doo::db()->find('users', array(
  858. 'where' => 'username = ?',
  859. 'param' => array($this->params['username']),
  860. ));
  861. // if (empty($user))
  862. // return FALSE;
  863. header('P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"');
  864. // header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
  865. // setcookie(Doo::conf()->COOKIEPRE . 'auth', '', 0, '/', '192.168.1.140', 0);
  866. // setcookie(Doo::conf()->COOKIEPRE . '_userid', '', 0, '/', '192.168.1.140', 0);
  867. // setcookie(Doo::conf()->COOKIEPRE . '_username', '', 0, '/', '192.168.1.140', 0);
  868. // setcookie(Doo::conf()->COOKIEPRE_WK . 'auth', '', 0, '/', '192.168.1.140', 0);
  869. // setcookie(Doo::conf()->COOKIEPRE_WK . '_userid', '', 0, '/', '192.168.1.140', 0);
  870. // setcookie(Doo::conf()->COOKIEPRE_WK . '_username', '', 0, '/', '192.168.1.140', 0);
  871. // setcookie(Doo::conf()->COOKIEPRE_WD . 'auth', '', 0, '/', '192.168.1.106', 0);
  872. // setcookie(Doo::conf()->COOKIEPRE_WD . '_userid', '', 0, '/', '192.168.1.106', 0);
  873. // setcookie(Doo::conf()->COOKIEPRE_WD . '_username', '', 0, '/', '192.168.1.106', 0);
  874. // 通行证
  875. setcookie(Doo::conf()->COOKIEPRE . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', '192.168.1.140', 0);
  876. setcookie(Doo::conf()->COOKIEPRE . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  877. setcookie(Doo::conf()->COOKIEPRE . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  878. // 文库
  879. // setcookie(Doo::conf()->COOKIEPRE_WK . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', '192.168.1.140', 0);
  880. // setcookie(Doo::conf()->COOKIEPRE_WK . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  881. // setcookie(Doo::conf()->COOKIEPRE_WK . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  882. // 问答
  883. // setcookie(Doo::conf()->COOKIEPRE_WD . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  884. // setcookie(Doo::conf()->COOKIEPRE_WD . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  885. // setcookie(Doo::conf()->COOKIEPRE_WD . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  886. // setcookie("test", 'sdfsdfsdfsdf', time() + 3600, '/', '192.168.1.106');
  887. }
  888. // public function nsetcookie($name) {
  889. // $user = Doo::db()->find('users', array(
  890. // 'where' => 'username = ?',
  891. // 'param' => array($name),
  892. // ));
  893. // setcookie(Doo::conf()->COOKIEPRE . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', '192.168.1.140', 0);
  894. // setcookie(Doo::conf()->COOKIEPRE . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  895. // setcookie(Doo::conf()->COOKIEPRE . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  896. // // 文库
  897. // setcookie(Doo::conf()->COOKIEPRE_WK . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', '192.168.1.140', 0);
  898. // setcookie(Doo::conf()->COOKIEPRE_WK . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  899. // setcookie(Doo::conf()->COOKIEPRE_WK . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  900. // // 问答
  901. // setcookie(Doo::conf()->COOKIEPRE_WD . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  902. // setcookie(Doo::conf()->COOKIEPRE_WD . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  903. // setcookie(Doo::conf()->COOKIEPRE_WD . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', 'local_zhask.com', 0);
  904. // }
  905. public function getBank() {
  906. // TODO:远程字符串解析提取出来做成函数
  907. $authstr = $this->authcode($_POST['data']);
  908. $ddddd = explode('&', urldecode($authstr));
  909. foreach ($ddddd as $k => $v) {
  910. list($key, $val) = explode('=', $v);
  911. $TmpArray[$key] = $val;
  912. $this->$key = $val;
  913. }
  914. $uid = $TmpArray['uid'];
  915. if ($uid > 0) {
  916. $r = Doo::db()->find('users', array(
  917. 'select' => 'bank',
  918. 'where' => 'id=?',
  919. 'param' => array($uid),
  920. 'asArray' => TRUE
  921. )
  922. );
  923. }
  924. exit(json_encode(array($r[0]['bank'])));
  925. }
  926. public function getPayOrder() {
  927. // TODO:远程字符串解析提取出来做成函数
  928. $authstr = $this->authcode($_POST['data']);
  929. $ddddd = explode('&', urldecode($authstr));
  930. foreach ($ddddd as $k => $v) {
  931. list($key, $val) = explode('=', $v);
  932. $TmpArray[$key] = $val;
  933. $this->$key = $val;
  934. }
  935. $uid = $TmpArray['uid'];
  936. if ($uid > 0) {
  937. $r = Doo::db()->find('pay', array(
  938. 'where' => 'userid=?',
  939. 'param' => array($uid),
  940. 'asArray' => TRUE
  941. )
  942. );
  943. }
  944. exit(json_encode($r));
  945. }
  946. public function madd() {
  947. // TODO:认证,变量判断
  948. $authstr = $this->authcode($_POST['data']);
  949. $ddddd = explode('&', urldecode($authstr));
  950. foreach ($ddddd as $k => $v) {
  951. list($key, $val) = explode('=', $v);
  952. $TmpArray[$key] = $val;
  953. $this->$key = $val;
  954. }
  955. $uid = $TmpArray['uid'];
  956. if ($uid > 0) {
  957. $usersArray = Doo::db()->find('users', array(
  958. 'where' => 'id=?',
  959. 'param' => array($uid),
  960. 'asArray' => TRUE
  961. )
  962. );
  963. if (empty($usersArray)) {
  964. exit('0');
  965. } else {
  966. $usersObject = Doo::loadModel('users', TRUE);
  967. $usersObject->id = $usersArray[0]['id'];
  968. $usersObject->bank = new DooDbExpression('bank+' . $this->bank);
  969. if ($usersObject->update()) {
  970. //TODO:写入日志
  971. exit('1');
  972. } else {
  973. exit('0');
  974. }
  975. }
  976. } else {
  977. exit('0');
  978. }
  979. }
  980. public function msub() {
  981. // TODO:远程字符串解析提取出来做成函数
  982. $authstr = $this->authcode($_POST['data']);
  983. $ddddd = explode('&', urldecode($authstr));
  984. foreach ($ddddd as $k => $v) {
  985. list($key, $val) = explode('=', $v);
  986. $TmpArray[$key] = $val;
  987. $this->$key = $val;
  988. }
  989. $uid = $TmpArray['uid'];
  990. if ($uid > 0) {
  991. $usersArray = Doo::db()->find('users', array(
  992. 'where' => 'id=?',
  993. 'param' => array($uid),
  994. 'asArray' => TRUE
  995. )
  996. );
  997. if (empty($usersArray)) {
  998. exit('0');
  999. } else {
  1000. if ($usersArray[0]['bank'] < $this->bank) {
  1001. exit('-1');
  1002. }
  1003. $usersObject = Doo::loadModel('users', TRUE);
  1004. $usersObject->id = $usersArray[0]['id'];
  1005. $usersObject->bank = new DooDbExpression('bank-' . $this->bank);
  1006. if ($usersObject->update()) {
  1007. //TODO:写入日志
  1008. exit('1');
  1009. } else {
  1010. exit('0');
  1011. }
  1012. }
  1013. } else {
  1014. exit('0');
  1015. }
  1016. }
  1017. /**
  1018. * random 获取字符串
  1019. * @param int $length
  1020. * @return string $hash
  1021. */
  1022. public function random_k($length = 6, $type = 0) {
  1023. $hash = '';
  1024. $chararr = array ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' );
  1025. $chars = $chararr [$type];
  1026. $max = strlen ( $chars ) - 1;
  1027. PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
  1028. for($i = 0; $i < $length; $i ++) {
  1029. $hash .= $chars [mt_rand ( 0, $max )];
  1030. }
  1031. return $hash;
  1032. }
  1033. }
  1034. ?>