ApiController.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. /**
  11. * 用户注册
  12. * @param string $username 用户名
  13. * @param string $password 密码
  14. * @param string $email email
  15. * @return int {-1:用户名已经存在 ;-2:email已存在;-2:mobile已存在;-4:用户名禁止注册;-5:邮箱禁止注册;-6:uc注册失败;int(uid):成功}
  16. */
  17. public function register() {
  18. $this->random = isset($this->data['random']) && !empty($this->data['random']) ? $this->data['random'] : create_randomstr(6);
  19. $this->password = isset($this->data['password']) ? create_password($this->data['password'], $this->random) : '';
  20. $this->email = isset($this->data['email']) ? $this->data['email'] : '';
  21. $this->type = isset($this->appid) ? 'app' : 'connect';
  22. $this->regip = isset($this->data['regip']) ? $this->data['regip'] : '';
  23. $this->appid = isset($this->appid) ? $this->appid : '';
  24. $this->appname = $this->applist[$this->appid]['name'];
  25. $checkname = $this->checkname(1);
  26. if ($checkname == -1) {
  27. exit('-1');
  28. } elseif ($checkname == -4) {
  29. exit('-4');
  30. }
  31. $checkemail = $this->checkemail(1);
  32. if ($checkemail == -1) {
  33. exit('-2');
  34. } elseif ($checkemail == -5) {
  35. exit('-5');
  36. }
  37. //UCenter会员注册
  38. $ucuserid = 0;
  39. if ($this->config['ucuse']) {
  40. pc_base::load_config('uc_config');
  41. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  42. $uid = uc_user_register($this->username, $this->data['password'], $this->email, $this->random);
  43. if (is_numeric($uid)) {
  44. switch ($uid) {
  45. case '-3':
  46. exit('-1');
  47. break;
  48. case '-6':
  49. exit('-2');
  50. break;
  51. case '-2':
  52. exit('-4');
  53. break;
  54. case '-5':
  55. exit('-5');
  56. break;
  57. case '-1':
  58. exit('-4');
  59. break;
  60. case '-4':
  61. exit('-5');
  62. break;
  63. default :
  64. $ucuserid = $uid;
  65. break;
  66. }
  67. } else {
  68. exit('-6');
  69. }
  70. }
  71. $data = array(
  72. 'username' => $this->username,
  73. 'password' => $this->password,
  74. 'email' => $this->email,
  75. 'regip' => $this->regip,
  76. 'regdate' => SYS_TIME,
  77. 'lastdate' => SYS_TIME,
  78. 'appname' => $this->appname,
  79. 'type' => $this->type,
  80. 'random' => $this->random,
  81. 'ucuserid' => $ucuserid
  82. );
  83. $uid = $this->db->insert($data, 1);
  84. /* 插入消息队列 */
  85. $noticedata = $data;
  86. $noticedata['uid'] = $uid;
  87. messagequeue::add('member_add', $noticedata);
  88. exit("$uid"); //exit($uid) 不可以If status is an integer, that value will also be used as the exit status.
  89. }
  90. /**
  91. * 移动端用户注册
  92. * @param string $username 用户名
  93. * @param string $password 密码
  94. * @param string $email email
  95. * @return int {-1:用户名已经存在 ;-2:mobile已存在;-3:mobile格式错误;-4:用户名禁止注册;-5:手机禁止注册;-6:验证码错误或过期;array(userinfo):用户信息}
  96. */
  97. public function MobileRegister(){
  98. $authstr = $this->authcode($_POST['data']);
  99. $ddddd = explode('&', urldecode($authstr));
  100. foreach ($ddddd as $k => $v) {
  101. list($key, $val) = explode('=', $v);
  102. $this->data[$key] = $val;
  103. }
  104. $this->username = isset($this->data['username']) ? $this->data['username'] : '';
  105. $this->random = isset($this->data['random']) && !empty($this->data['random']) ? $this->data['random'] : $this->create_randomstr(6);
  106. $this->password = isset($this->data['password']) ? $this->create_password($this->data['password'], $this->random) : '';
  107. $this->mobile = isset($this->data['mobile']) ? $this->data['mobile'] : '';
  108. $this->regip = isset($this->data['regip']) ? $this->data['regip'] : '';
  109. $this->code = isset($this->data['code']) ? $this->data['code'] : '';
  110. $checkmobile = $this->checkmobile(1);
  111. if ($checkmobile == -2) {
  112. exit('-2');
  113. } elseif ($checkmobile == -3) {
  114. exit('-3');
  115. } elseif ($checkmobile == -5) {
  116. exit('-5');
  117. }
  118. $checkname = $this->checknamebymobile(1);
  119. if ($checkname == -1) {
  120. exit('-1');
  121. } elseif ($checkname == -4) {
  122. exit('-4');
  123. }
  124. $checkcode = $this->checkcode(1);
  125. if ($checkcode == -6) {
  126. exit('-6');
  127. }
  128. $data = array(
  129. 'username' => $this->username,
  130. 'userpasswd' => $this->password,
  131. 'mobile' => $this->mobile,
  132. 'regip' => $this->regip,
  133. 'salt' => $this->random,
  134. 'intime' => time(),
  135. 'regdate' => time(),
  136. );
  137. doo::loadModel('users');
  138. $users = new Users();
  139. $RetValue = $users->_insertAttributes('users', $data);
  140. $userinfo = Doo::db()->find('users', array(
  141. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  142. 'where' => 'id=?',
  143. 'param' => array($RetValue),
  144. ));
  145. echo json_encode($userinfo);
  146. die();
  147. // exit("$RetValue"); //exit($uid) 不可以If status is an integer, that value will also be used as the exit status.
  148. }
  149. /**
  150. * 用户获取手机验证码,type=1表示获取注册,type=2表示找回密码
  151. * @param string $mobile mobile
  152. * @param string $type type
  153. * @return int {-2:mobile已存在;-3:mobile格式错误;-5:手机禁止注册;-7:验证码获取有误;-8:mobile未注册;array(code):验证码信息}
  154. */
  155. public function MobileCode(){
  156. $authstr = $this->authcode($_POST['data']);
  157. $ddddd = explode('&', urldecode($authstr));
  158. foreach ($ddddd as $k => $v) {
  159. list($key, $val) = explode('=', $v);
  160. $this->data[$key] = $val;
  161. }
  162. $this->mobile = isset($this->data['mobile']) ? $this->data['mobile'] : '';
  163. $type = isset($this->data['type']) ? $this->data['type'] : '';
  164. $checkmobile = $this->checkmobile(1);
  165. if($type == 1){
  166. if ($checkmobile == -2) {
  167. exit('-2');
  168. } elseif ($checkmobile == -3) {
  169. exit('-3');
  170. } elseif ($checkmobile == -5) {
  171. exit('-5');
  172. }
  173. }else{
  174. if ($checkmobile == 1) {
  175. exit('-8');
  176. } elseif ($checkmobile == -3) {
  177. exit('-3');
  178. }
  179. }
  180. Doo::loadModel('mobilecode');
  181. $mobilecode = new mobilecode();
  182. $code = $this->getRandomCheckCode();
  183. $now = time();
  184. $checkresult = $mobilecode->find(array('where' => 'mobile='.$this->mobile, 'desc' => 'id', 'limit' => 1,'asArray' => true));
  185. if(!empty($checkresult)){
  186. $old = $checkresult['time'];
  187. if($now-$old >= 60){
  188. $sendresult = $this->sendSms($this->mobile,$code);
  189. // $sendresult = true;
  190. if($sendresult){
  191. $mobilecode->mobile = $this->mobile;
  192. $mobilecode->code = $code;
  193. $mobilecode->time = $now;
  194. $mobilecode->insert();
  195. echo json_encode(array('code' => $code));die;
  196. }else{
  197. exit('-7');
  198. }
  199. }else{
  200. exit('-7');
  201. }
  202. }else {
  203. $sendresult = $this->sendSms($this->mobile,$code);
  204. // $sendresult = true;
  205. if($sendresult){
  206. $mobilecode->mobile = $this->mobile;
  207. $mobilecode->code = $code;
  208. $mobilecode->time = $now;
  209. $mobilecode->insert();
  210. echo json_encode(array('code' => $code));die;
  211. }else{
  212. exit('-7');
  213. }
  214. }
  215. }
  216. /**
  217. * 移动端找回密码
  218. * @param string $mobile 手机号
  219. * @param string $password 密码
  220. * @param string $code 手机验证码
  221. * @return int {-3:mobile格式错误;-6:验证码错误;-8:mobile未注册;-9:用户不存在;array(userinfo):用户信息}
  222. */
  223. public function ForgetPW(){
  224. $authstr = $this->authcode($_POST['data']);
  225. $ddddd = explode('&', urldecode($authstr));
  226. foreach ($ddddd as $k => $v) {
  227. list($key, $val) = explode('=', $v);
  228. $this->data[$key] = $val;
  229. }
  230. $this->password = isset($this->data['password']) ? $this->data['password'] : '';
  231. $this->mobile = isset($this->data['mobile']) ? $this->data['mobile'] : '';
  232. $this->code = isset($this->data['code']) ? $this->data['code'] : '';
  233. $checkmobile = $this->checkmobile(1);
  234. if ($checkmobile == 1) {
  235. exit('-8');
  236. } elseif ($checkmobile == -3) {
  237. exit('-3');
  238. }
  239. $checkcode = $this->checkcode(1);
  240. if ($checkcode == -6) {
  241. exit('-6');
  242. }
  243. Doo::loadModel('users');
  244. $users = new Users();
  245. $userinfo = $users->getRowByMobile($this->mobile);
  246. if(empty($userinfo)){
  247. exit('-9');
  248. }
  249. $newpasswd = $this->create_password($this->password, $userinfo['salt']);
  250. $users->id = $userinfo['id'];
  251. $users->userpasswd = $newpasswd;
  252. $users->update();
  253. $userinfo = Doo::db()->find('users', array(
  254. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  255. 'where' => 'id=?',
  256. 'param' => array($userinfo['id']),
  257. ));
  258. echo json_encode($userinfo);
  259. die();
  260. }
  261. /**
  262. * 获取所有用户在某段时间总充值的数目
  263. * @param string $startday 开始时间
  264. * @param string $endday 结束时间
  265. * @return int {-1;时间有错误;floor result:数目结果}
  266. */
  267. public function Deposit(){
  268. $authstr = $this->authcode($_POST['data']);
  269. $ddddd = explode('&', urldecode($authstr));
  270. foreach ($ddddd as $k => $v) {
  271. list($key, $val) = explode('=', $v);
  272. $this->data[$key] = $val;
  273. }
  274. $startday = isset($this->data['startday']) ? $this->data['startday'] : '';
  275. $endday = isset($this->data['endday']) ? $this->data['endday'] : '';
  276. if(empty($startday) || empty($endday)){
  277. exit('-1');
  278. }
  279. // $pay = new Pay();
  280. // $depositlist = $pay->find(array('select' => 'sum(money) as sum', 'where' => 'status="succ" and addtime between '.$startday.' and '.$endday, 'group' => 'status', 'asArray' => TRUE));
  281. $sql = 'SELECT sum(money) as sum FROM `wk_pay_account` WHERE `status` = "succ" and addtime between '.$startday.' and '.$endday.' group by status';
  282. $query = Doo::db ()->query ( $sql );
  283. $depositlist = $query->fetch ();
  284. echo round($depositlist['sum'],2);
  285. exit;
  286. }
  287. /**
  288. * 编辑用户,可以不传入旧密码和新密码
  289. * 如果传入新密码,则修改密码为新密码
  290. * @param string $username 用户名
  291. * @param string $password 旧密码
  292. * @param string $newpassword 新密码
  293. * @param string $email email
  294. * @param string $random 密码随机数
  295. * @return int {-1:用户不存在;-2:旧密码错误;-3:email已经存在 ;1:成功;0:未作修改}
  296. */
  297. public function edit() {
  298. header("Content-type: text/html; charset=utf-8");
  299. $authstr = $this->authcode($_POST['data']);
  300. $ddddd = explode('&', urldecode($authstr));
  301. foreach ($ddddd as $k => $v) {
  302. list($key, $val) = explode('=', $v);
  303. $TmpArray[$key] = $val;
  304. }
  305. $userinfo = $this->__getuserinfo($TmpArray['uid']);
  306. if (!$userinfo) {
  307. exit('-1');
  308. }
  309. $passworld = $this->create_password($TmpArray['password'], $userinfo[0]['salt']);
  310. if (isset($this->password) && !empty($this->password) && ($passworld != $userinfo[0]['userpasswd'])) {
  311. exit('-2');
  312. }
  313. if ($TmpArray['useranswer'] && ($userinfo[0]['useranswer'] != $TmpArray['useranswer'])) {
  314. exit('-3');
  315. }
  316. $this->data['randomstr'] = $this->create_randomstr();
  317. $this->data['newpasswd'] = $this->create_password($TmpArray['newpassword'], $this->data['randomstr']);
  318. Doo::loadModel('users');
  319. $objuser = new Users();
  320. $objuser->userpasswd = $this->data['newpasswd'];
  321. $objuser->salt = $this->data['randomstr'];
  322. $RetValue = $objuser->update(array('where' => 'id=' . $TmpArray['uid'] . ' AND useranswer=\'' . $TmpArray['useranswer'] . '\''));
  323. return $RetValue;
  324. }
  325. /**
  326. * 生成随机字符串
  327. * @param string $lenth 长度
  328. * @return string 字符串
  329. */
  330. function create_randomstr($lenth = 6) {
  331. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  332. }
  333. /**
  334. *
  335. * @param $password 密码
  336. * @param $random 随机数
  337. */
  338. function create_password($password = '', $random = '') {
  339. if (empty($random)) {
  340. $array['random'] = $this->create_randomstr();
  341. $array['password'] = md5(md5($password) . $array['random']);
  342. return $array;
  343. }
  344. return md5(md5($password) . $random);
  345. }
  346. /**
  347. * 随机字符串函数
  348. * @param $password 密码
  349. * @param $random 随机数
  350. */
  351. function random($length, $chars = '0123456789') {
  352. $hash = '';
  353. $max = strlen($chars) - 1;
  354. for ($i = 0; $i < $length; $i++) {
  355. $hash .= $chars[mt_rand(0, $max)];
  356. }
  357. return $hash;
  358. }
  359. /**
  360. * 删除用户
  361. * @param string {$uid:用户id;$username:用户名;$email:email;$mobile:mobile}
  362. * @return array {-1:删除失败;>0:删除成功}
  363. */
  364. public function delete() {
  365. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : '';
  366. $this->email = isset($this->data['email']) ? $this->data['email'] : '';
  367. $this->mobile = isset($this->data['mobile']) ? $this->data['mobile'] : '';
  368. if ($this->uid > 0 || is_array($this->uid)) {
  369. $where = to_sqls($this->uid, '', 'uid');
  370. //ucenter部份
  371. if ($this->config['ucuse']) {
  372. pc_base::load_config('uc_config');
  373. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  374. $s = $this->db->select($where, 'ucuserid');
  375. if ($s) {
  376. $uc_data = array();
  377. foreach ($s as $k => $v) {
  378. $uc_data[$k] = $v['ucuserid'];
  379. }
  380. if (!empty($uc_data))
  381. $r = uc_user_delete($uc_data);
  382. if (!$r) {
  383. exit('-1');
  384. }
  385. } else {
  386. exit('-1');
  387. }
  388. }
  389. /* 插入消息队列 */
  390. $noticedata['uids'] = $this->uid;
  391. messagequeue::add('member_delete', $noticedata);
  392. $this->db->delete($where);
  393. exit('1');
  394. } elseif (!empty($this->username)) {
  395. $this->db->delete(array('username' => $this->username));
  396. exit('2');
  397. } elseif (!empty($this->email)) {
  398. $this->db->delete(array('email' => $this->email));
  399. exit('3');
  400. } elseif (!empty($this->mobile)) {
  401. $this->db->delete(array('mobile' => $this->mobile));
  402. exit('3');
  403. } else {
  404. exit('-1');
  405. }
  406. }
  407. /**
  408. * 获取用户信息
  409. * @param string {$uid:用户id;$username:用户名;$email:email;$mobile:mobile}
  410. * @return array {-1:用户不存在;array(userinfo):用户信息}
  411. */
  412. private function __getuserinfo($uid) {
  413. if ($uid > 0) {
  414. $r = Doo::db()->find('users', array(
  415. 'where' => 'id=?',
  416. 'param' => array($uid),
  417. 'asArray' => TRUE
  418. )
  419. );
  420. } elseif (!empty($this->username)) {
  421. $r = $this->db->get_one(array('username' => $this->username));
  422. } elseif (!empty($this->email)) {
  423. $r = $this->db->get_one(array('email' => $this->email));
  424. } elseif (!empty($this->mobile)) {
  425. $r = $this->db->get_one(array('mobile' => $this->mobile));
  426. } else {
  427. return false;
  428. }
  429. return $r;
  430. }
  431. function getuserlist(){
  432. $authstr = $this->authcode($_POST['data']);
  433. $ddddd = explode('&', urldecode($authstr));
  434. foreach ($ddddd as $k => $v) {
  435. list($key, $val) = explode('=', $v);
  436. $this->$key = $val;
  437. }
  438. $r = Doo::db()->find('users', array(
  439. 'select'=>'id,username',
  440. 'where' => 'id in ( '.$this->uid.' )',
  441. 'param' => array($this->uid),
  442. 'asArray' => TRUE
  443. )
  444. );
  445. exit(json_encode($r));
  446. }
  447. /**
  448. * 获取用户信息
  449. * @param string {$uid:用户id;$username:用户名;$email:email;$mobile:mobile}
  450. * @return array {-1:用户不存在;array(userinfo):用户信息}
  451. */
  452. public function getuserinfo() {
  453. // TODO:远程字符串解析提取出来做成函数
  454. $authstr = $this->authcode($_POST['data']);
  455. $ddddd = explode('&', urldecode($authstr));
  456. foreach ($ddddd as $k => $v) {
  457. list($key, $val) = explode('=', $v);
  458. $TmpArray[$key] = $val;
  459. $this->$key = $val;
  460. }
  461. $uid = isset($TmpArray['uid']) ? $TmpArray['uid'] : 0;
  462. if ($uid > 0) {
  463. $r = Doo::db()->find('users', array(
  464. 'where' => 'id=?',
  465. 'param' => array($uid),
  466. 'asArray' => TRUE
  467. )
  468. );
  469. } elseif (!empty($this->username)) {
  470. $r = Doo::db()->find('users', array(
  471. 'where' => 'username=?',
  472. 'param' => array($this->username),
  473. 'asArray' => TRUE
  474. )
  475. );
  476. } elseif (!empty($this->email)) {
  477. $r = Doo::db()->find('users', array(
  478. 'where' => 'useremail=?',
  479. 'param' => array($this->email),
  480. 'asArray' => TRUE
  481. )
  482. );
  483. } elseif(!empty($this->mobile)) {
  484. $r = Doo::db()->find('users', array(
  485. 'where' => 'mobile=?',
  486. 'param' => array($this->mobile),
  487. 'asArray' => TRUE
  488. )
  489. );
  490. } else {
  491. return false;
  492. }
  493. if ($this->username) {
  494. exit(json_encode($r));
  495. } else {
  496. exit(json_encode($r));
  497. }
  498. }
  499. function isMail($mail) {
  500. // if (preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9\-\.])+/", $mail))
  501. if (filter_var($mail, FILTER_VALIDATE_EMAIL)){
  502. return true;
  503. }else{
  504. return false;
  505. }
  506. }
  507. // 检测手机格式
  508. function isMobile($mobile) {
  509. if(preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
  510. return TRUE;
  511. } else {
  512. return FALSE;
  513. }
  514. }
  515. public function checkauth(){
  516. $authstr = $this->authcode($_POST['data']);
  517. $ddddd = explode('&', urldecode($authstr));
  518. foreach ($ddddd as $k => $v) {
  519. list($key, $val) = explode('=', $v);
  520. $this->$key = $val;
  521. }
  522. $k=explode('_', $this->auth);
  523. $userinfo = Doo::db()->find('users', array(
  524. 'select' => 'id,keys_auth',
  525. 'where' => 'id=?',
  526. 'param' => array($k[0]),
  527. ));
  528. if(empty($userinfo[0]->keys_auth)){
  529. echo '2';die;
  530. }
  531. $dd = explode('_', $userinfo[0]->keys_auth);
  532. Doo::loadModel('users');
  533. $users = new Users();
  534. if(!isset($dd[1])){
  535. $users->keys_auth=$k[1]."_1";
  536. $users->update(array('where' => 'id=?', 'param' => array($k[0])));
  537. }else{
  538. //过程认证完成
  539. if(isset($k[2])){
  540. $users->keys_auth="";
  541. $users->update(array('where' => 'id=?', 'param' => array($k[0])));
  542. }
  543. }
  544. if($dd[0]==$k[1]){
  545. echo '1';die;
  546. }else{
  547. echo '2';die;
  548. }
  549. }
  550. /**
  551. * 用户登录
  552. * @param string $username 用户名
  553. * @param string $password 密码
  554. * @return array {-2;密码错误;-1:用户不存在;array(userinfo):用户信息}
  555. */
  556. public function login() {
  557. $authstr = $this->authcode($_POST['data']);
  558. $ddddd = explode('&', urldecode($authstr));
  559. foreach ($ddddd as $k => $v) {
  560. list($key, $val) = explode('=', $v);
  561. $this->$key = $val;
  562. }
  563. if (isset($this->email) && $this->isMail($this->email)) {
  564. $userinfo = Doo::db()->find('users', array(
  565. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  566. 'where' => 'useremail=?',
  567. 'param' => array($this->email),
  568. ));
  569. }
  570. if (isset($this->mobile) && $this->isMobile($this->mobile)) {
  571. $userinfo = Doo::db()->find('users', array(
  572. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  573. 'where' => 'mobile=?',
  574. 'param' => array($this->mobile),
  575. ));
  576. }
  577. if ($this->username) {
  578. $userinfo = Doo::db()->find('users', array(
  579. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  580. 'where' => 'username=?',
  581. 'param' => array($this->username),
  582. ));
  583. }
  584. if (empty($userinfo))
  585. exit('-22');
  586. if (!empty($userinfo) && $userinfo[0]->userpasswd == $this->create_password($this->userpasswd, $userinfo[0]->salt)) {
  587. //登录成功更新用户最近登录时间和ip
  588. Doo::loadModel('users');
  589. $users = new Users();
  590. $key=$this->random_k(8);
  591. $users->keys_auth=$key;
  592. $users->lastloginip = ip2long($this->clientIP());
  593. $users->lastlogintime = time();
  594. $users->update(array('where' => 'id=?', 'param' => array($userinfo[0]->id)));
  595. $userinfo[0]->keys_auth=$key;
  596. Doo::loadModel('userswitch');
  597. $userswitch = new UserSwitch();
  598. $usArray = $userswitch->getOne(array('select' => 'actime', 'where' => 'uid=?', 'param' => array($userinfo[0]->id), 'asArray' => TRUE));
  599. if (!$usArray['actime'] && empty($userinfo[0]->mobile))
  600. exit('-3');
  601. echo json_encode($userinfo);
  602. die();
  603. } else {
  604. exit('-2');
  605. }
  606. }
  607. /**
  608. * 用户登录
  609. * @param string $username 用户名
  610. * @param string $password 密码
  611. * @return array {-2;密码错误;-22:用户不存在;array(userinfo):用户信息}
  612. */
  613. public function login2() {
  614. // $authstr = $this->authcode($_POST['data']);
  615. // $ddddd = explode('&', urldecode($authstr));
  616. // foreach ($ddddd as $k => $v) {
  617. // list($key, $val) = explode('=', $v);
  618. // $this->$key = $val;
  619. // }
  620. if ($this->isMail($_POST['username'])) {
  621. $userinfo = Doo::db()->find('users', array(
  622. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  623. 'where' => 'useremail=?',
  624. 'param' => array($_POST['username']),
  625. ));
  626. }else if ($this->isMobile($_POST['username'])) {
  627. $userinfo = Doo::db()->find('users', array(
  628. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  629. 'where' => 'mobile=?',
  630. 'param' => array($_POST['username']),
  631. ));
  632. }
  633. if (empty($userinfo))
  634. exit('-22');
  635. if (!empty($userinfo) && $userinfo[0]->userpasswd == $this->create_password($_POST['userpasswd'], $userinfo[0]->salt)) {
  636. //登录成功更新用户最近登录时间和ip
  637. Doo::loadModel('users');
  638. $users = new Users();
  639. $key=$this->random_k(8);
  640. $users->keys_auth=$key;
  641. $users->lastloginip = ip2long($this->clientIP());
  642. $users->lastlogintime = time();
  643. $users->update(array('where' => 'id=?', 'param' => array($userinfo[0]->id)));
  644. $userinfo[0]->keys_auth=$key;
  645. Doo::loadModel('userswitch');
  646. $userswitch = new UserSwitch();
  647. $usArray = $userswitch->getOne(array('select' => 'actime', 'where' => 'uid=?', 'param' => array($userinfo[0]->id), 'asArray' => TRUE));
  648. if (!$usArray['actime'] && empty($userinfo[0]->mobile))
  649. exit('-3');
  650. echo json_encode($userinfo);
  651. die();
  652. } else {
  653. exit('-2');
  654. }
  655. }
  656. public function scLogin(){
  657. $authstr = $this->authcode($_POST['data']);
  658. $ddddd = explode('&', urldecode($authstr));
  659. foreach ($ddddd as $k => $v) {
  660. list($key, $val) = explode('=', $v);
  661. $this->$key = $val;
  662. }
  663. if (isset($this->email) && $this->isMail($this->email)) {
  664. $userinfo = Doo::db()->find('users', array(
  665. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  666. 'where' => 'useremail=?',
  667. 'param' => array($this->email),
  668. ));
  669. }
  670. if (isset($this->mobile) && $this->isMobile($this->mobile)) {
  671. $userinfo = Doo::db()->find('users', array(
  672. 'select' => 'id,username,userpasswd,useremail,salt,actstate,bank,keys_auth,mobile',
  673. 'where' => 'mobile=?',
  674. 'param' => array($this->mobile),
  675. ));
  676. }
  677. if ($this->username) {
  678. $userinfo = Doo::db()->find('users', array(
  679. 'select' => 'id,username,userpasswd,useremail,salt,actstate,keys_auth,mobile',
  680. 'where' => 'username=?',
  681. 'param' => array($this->username),
  682. ));
  683. }
  684. if (empty($userinfo))
  685. exit('-22');
  686. if (!empty($userinfo) && $userinfo[0]->userpasswd == $this->create_password($this->userpasswd, $userinfo[0]->salt)) {
  687. //登录成功更新用户最近登录时间和ip
  688. Doo::loadModel('users');
  689. $users = new Users();
  690. $key=$this->random_k(8);
  691. $users->keys_auth=$key;
  692. $users->lastloginip = ip2long($this->clientIP());
  693. $users->lastlogintime = time();
  694. $users->update(array('where' => 'id=?', 'param' => array($userinfo[0]->id)));
  695. $userinfo[0]->keys_auth=$key;
  696. Doo::loadModel('userswitch');
  697. Doo::loadModel('scUser');
  698. $scUser=new scUser();
  699. $userswitch = new UserSwitch();
  700. $usArray = $userswitch->getOne(array('select' => 'actime', 'where' => 'uid=?', 'param' => array($userinfo[0]->id), 'asArray' => TRUE));
  701. $scU=$scUser->find(array('where' => 'uid='.$userinfo[0]->id, 'asArray' => TRUE));
  702. $scU[0]['userName']=$userinfo[0]->username;$scU[0]['userpasswd']=$userinfo[0]->userpasswd;$scU[0]['useremail']=$userinfo[0]->useremail;
  703. $scU[0]['mobile']=$userinfo[0]->mobile;
  704. if (!$usArray['actime'] && empty($userinfo[0]->mobile))
  705. exit('-3');
  706. echo json_encode($scU);
  707. die();
  708. } else {
  709. exit('-2');
  710. }
  711. }
  712. /**
  713. * 同步登陆
  714. * @param string $uid 用户id
  715. * @return string javascript用户同步登陆js
  716. */
  717. public function synlogin() {
  718. //判断本应用是否开启同步登陆
  719. if ($this->applist[$this->appid]['synlogin']) {
  720. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : '';
  721. $this->password = isset($this->data['password']) ? $this->data['password'] : '';
  722. $res = '';
  723. //ucenter登陆部份
  724. if ($this->config['ucuse']) {
  725. pc_base::load_config('uc_config');
  726. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  727. $r = $this->db->get_one(array('uid' => $this->uid), "ucuserid");
  728. if ($r['ucuserid'])
  729. $res .= uc_user_synlogin($r['ucuserid']);
  730. }
  731. foreach ($this->applist as $v) {
  732. if (!$v['synlogin'])
  733. continue;
  734. if ($v['appid'] != $this->appid) {
  735. $tmp_s = strstr($v['url'] . $v['apifilename'], '?') ? '&' : '?';
  736. $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>';
  737. }
  738. }
  739. exit($res);
  740. } else {
  741. exit('0');
  742. }
  743. }
  744. /**
  745. * 同步退出
  746. * @return string javascript用户同步退出js
  747. */
  748. public function synlogout() {
  749. if ($this->applist[$this->appid]['synlogin']) {
  750. $res = '';
  751. //ucenter登陆部份
  752. if ($this->config['ucuse']) {
  753. pc_base::load_config('uc_config');
  754. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  755. $res .= uc_user_synlogout();
  756. }
  757. foreach ($this->applist as $v) {
  758. if (!$v['synlogin'])
  759. continue;
  760. if ($v['appid'] != $this->appid) {
  761. $tmp_s = strstr($v['url'] . $v['apifilename'], '?') ? '&' : '?';
  762. $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>';
  763. }
  764. }
  765. exit($res);
  766. } else {
  767. exit;
  768. }
  769. }
  770. /**
  771. * 获取应用列表
  772. */
  773. public function getapplist() {
  774. $applist = getcache('applist', 'admin');
  775. exit(serialize($applist));
  776. }
  777. /**
  778. * 获取积分兑换规则
  779. */
  780. public function getcredit($return = '') {
  781. $creditcache = getcache('creditlist', 'admin');
  782. foreach ($creditcache as $v) {
  783. if ($v['fromid'] == $this->appid) {
  784. $creditlist[$v['from'] . '_' . $v['to']] = $v;
  785. }
  786. }
  787. if ($return) {
  788. return $creditlist;
  789. } else {
  790. exit(serialize($creditlist));
  791. }
  792. }
  793. /**
  794. * 兑换积分
  795. * @param int $uid phpssouid
  796. * @param int $from 本系统积分类型id
  797. * @param int $toappid 目标系统应用appid
  798. * @param int $to 目标系统积分类型id
  799. * @param int $credit 本系统扣除积分数
  800. * @return bool {1:成功;0:失败}
  801. */
  802. public function changecredit() {
  803. $this->uid = isset($this->data['uid']) ? $this->data['uid'] : exit('0');
  804. $this->toappid = isset($this->data['toappid']) ? $this->data['toappid'] : exit('0');
  805. $this->from = isset($this->data['from']) ? $this->data['from'] : exit('0');
  806. $this->to = isset($this->data['to']) ? $this->data['to'] : exit('0');
  807. $this->credit = isset($this->data['credit']) ? $this->data['credit'] : exit('0');
  808. $this->appname = $this->applist[$this->appid]['name'];
  809. $outcredit = $this->getcredit(1);
  810. //目标系统积分增加数
  811. $this->credit = floor($this->credit * $outcredit[$this->from . '_' . $this->to]['torate'] / $outcredit[$this->from . '_' . $this->to]['fromrate']);
  812. /* 插入消息队列 */
  813. $noticedata['appname'] = $this->appname;
  814. $noticedata['uid'] = $this->uid;
  815. $noticedata['toappid'] = $this->toappid;
  816. $noticedata['totypeid'] = $this->to;
  817. $noticedata['credit'] = $this->credit;
  818. messagequeue::add('change_credit', $noticedata);
  819. exit('1');
  820. }
  821. /**
  822. * 检查用户名
  823. * @param string $username 用户名
  824. * @return int {-4:用户名禁止注册;-1:用户名已经存在 ;1:成功}
  825. */
  826. public function checkname($is_return = 0) {
  827. if (empty($this->username)) {
  828. if ($is_return) {
  829. return -1;
  830. } else {
  831. exit('-1');
  832. }
  833. }
  834. //非法关键词判断
  835. $denyusername = $this->settings['denyusername'];
  836. if (is_array($denyusername)) {
  837. $denyusername = implode("|", $denyusername);
  838. $pattern = '/^(' . str_replace(array('\\*', ' ', "\|"), array('.*', '', '|'), preg_quote($denyusername, '/')) . ')$/i';
  839. if (preg_match($pattern, $this->username)) {
  840. if ($is_return) {
  841. return -4;
  842. } else {
  843. exit('-4');
  844. }
  845. }
  846. }
  847. //UCenter部分
  848. if ($this->config['ucuse']) {
  849. pc_base::load_config('uc_config');
  850. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  851. $rs = uc_user_checkname($this->username);
  852. if ($rs < 1) {
  853. exit('-4');
  854. }
  855. }
  856. $r = $this->db->get_one(array('username' => $this->username));
  857. if ($is_return) {
  858. return !empty($r) ? -1 : 1;
  859. } else {
  860. echo!empty($r) ? -1 : 1;
  861. exit;
  862. }
  863. }
  864. /**
  865. * 检查email
  866. * @param string $email email
  867. * @return int {-1:email已经存在 ;-5:邮箱禁止注册;1:成功}
  868. */
  869. public function checkemail($is_return = 0) {
  870. $this->email = isset($this->email) ? $this->email : isset($this->data['email']) ? $this->data['email'] : '';
  871. if (empty($this->email)) {
  872. if ($is_return) {
  873. return -1;
  874. } else {
  875. exit('-1');
  876. }
  877. }
  878. //非法关键词判断
  879. $denyemail = $this->settings['denyemail'];
  880. if (is_array($denyemail)) {
  881. $denyemail = implode("|", $denyemail);
  882. $pattern = '/^(' . str_replace(array('\\*', ' ', "\|"), array('.*', '', '|'), preg_quote($denyemail, '/')) . ')$/i';
  883. if (preg_match($pattern, $this->email)) {
  884. if ($is_return) {
  885. return -5;
  886. } else {
  887. exit('-5');
  888. }
  889. }
  890. }
  891. //UCenter部分
  892. if ($this->config['ucuse']) {
  893. pc_base::load_config('uc_config');
  894. require_once PHPCMS_PATH . 'api/uc_client/client.php';
  895. $rs = uc_user_checkemail($this->email);
  896. if ($rs < 1) {
  897. exit('-5');
  898. }
  899. }
  900. $r = $this->db->get_one(array('email' => $this->email));
  901. if ($is_return) {
  902. return !empty($r) ? -1 : 1;
  903. } else {
  904. !empty($r) ? exit('-1') : exit('1');
  905. }
  906. }
  907. /**
  908. * 检查mobile
  909. * @param string $mobile mobile
  910. * @return int {-2:mobile已经存在;-3:mobile格式错误;-5:手机禁止注册;1:成功}
  911. */
  912. public function checkmobile($is_return = 0) {
  913. $this->mobile = isset($this->mobile) ? $this->mobile : isset($this->data['mobile']) ? $this->data['mobile'] : '';
  914. if (empty($this->mobile)) {
  915. if ($is_return) {
  916. return -5;
  917. } else {
  918. exit('-5');
  919. }
  920. }
  921. // //非法关键词判断
  922. // $denymobile = $this->settings['denyemobile'];
  923. // if (is_array($denymobile)) {
  924. // $denymobile = implode("|", $denymobile);
  925. // $pattern = '/^(' . str_replace(array('\\*', ' ', "\|"), array('.*', '', '|'), preg_quote($denymobile, '/')) . ')$/i';
  926. // if (preg_match($pattern, $this->mobile)) {
  927. // if ($is_return) {
  928. // return -5;
  929. // } else {
  930. // exit('-5');
  931. // }
  932. // }
  933. // }
  934. if(!preg_match("/^1[34578]{1}\d{9}$/",$this->mobile)){
  935. if ($is_return) {
  936. return -3;
  937. } else {
  938. exit('-3');
  939. }
  940. }
  941. Doo::loadModel('users');
  942. $users = new Users();
  943. $r = $users->getRowByMobile(trim($this->mobile));
  944. // var_dump($r);
  945. // exit;
  946. if ($is_return) {
  947. return !empty($r) ? -2 : 1;
  948. } else {
  949. !empty($r) ? exit('-2') : exit('1');
  950. }
  951. }
  952. /**
  953. * 检查用户名
  954. * @param string $username 用户名
  955. * @return int {-4:用户名禁止注册;-1:用户名已经存在 ;1:成功}
  956. */
  957. public function checknamebymobile($is_return = 0) {
  958. if (empty($this->username)) {
  959. if ($is_return) {
  960. return -4;
  961. } else {
  962. exit('-4');
  963. }
  964. }
  965. if (!$this->CheckLengthBetween($this->username, 3, 20)) {
  966. if ($is_return) {
  967. return -4;
  968. } else {
  969. exit('-4');
  970. }
  971. }
  972. //非法关键词判断
  973. if (preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$this->username)) {
  974. if ($is_return) {
  975. return -4;
  976. } else {
  977. exit('-4');
  978. }
  979. }
  980. //
  981. // //UCenter部分
  982. // if ($this->config['ucuse']) {
  983. // pc_base::load_config('uc_config');
  984. // require_once PHPCMS_PATH . 'api/uc_client/client.php';
  985. // $rs = uc_user_checkname($this->username);
  986. // if ($rs < 1) {
  987. // exit('-4');
  988. // }
  989. // }
  990. Doo::loadModel('users');
  991. $users = new Users();
  992. $r = $users->getRowByName($this->username);
  993. if ($is_return) {
  994. return !empty($r) ? -1 : 1;
  995. } else {
  996. echo!empty($r) ? -1 : 1;
  997. exit;
  998. }
  999. }
  1000. function CheckLengthBetween($C_cahr, $I_len1, $I_len2=100)
  1001. {
  1002. $C_cahr = trim($C_cahr);
  1003. if (strlen($C_cahr) < $I_len1) return false;
  1004. if (strlen($C_cahr) > $I_len2) return false;
  1005. return true;
  1006. }
  1007. /**
  1008. * 检查mobilecode
  1009. * @param string $mobile mobile
  1010. * @return int {-6:验证码错误或过期;1:成功}
  1011. */
  1012. public function checkcode($is_return = 0) {
  1013. $this->mobile = isset($this->mobile) ? $this->mobile : isset($this->data['mobile']) ? $this->data['mobile'] : '';
  1014. $this->code = isset($this->code) ? $this->code : isset($this->data['code']) ? $this->data['code'] : '';
  1015. if (empty($this->mobile)) {
  1016. if ($is_return) {
  1017. return -5;
  1018. } else {
  1019. exit('-5');
  1020. }
  1021. }
  1022. if (empty($this->code)) {
  1023. if ($is_return) {
  1024. return -6;
  1025. } else {
  1026. exit('-6');
  1027. }
  1028. }
  1029. if($this->checkSmsCode($this->mobile,$this->code)){
  1030. if ($is_return) {
  1031. return 1;
  1032. } else {
  1033. exit('1');
  1034. }
  1035. }else{
  1036. if ($is_return) {
  1037. return -6;
  1038. } else {
  1039. exit('-6');
  1040. }
  1041. }
  1042. }
  1043. // 检测手机验证码和验证手机是否符合
  1044. private function checkSmsCode($mobile,$code){
  1045. $now = time();
  1046. $old = $now-900;
  1047. Doo::loadModel('mobilecode');
  1048. $mobilecode = new mobilecode();
  1049. $user = $mobilecode->find(array(
  1050. 'select' => 'id',
  1051. 'where' => 'mobile='.$mobile.' and code='.$code.' and time between '.$old.' and '.$now,
  1052. 'desc' => 'id',
  1053. 'limit' => 1,
  1054. 'asArray' => true
  1055. ));
  1056. if ($user) {
  1057. $mobilecode->mobile=$mobile;
  1058. $mobilecode->delete();
  1059. return TRUE;
  1060. } else {
  1061. return FALSE;
  1062. }
  1063. }
  1064. /**
  1065. * 上传头像处理
  1066. * 传入头像压缩包,解压到指定文件夹后删除非图片文件
  1067. */
  1068. public function uploadavatar() {
  1069. //根据用户id创建文件夹
  1070. $encodestr = $this->authcode(rawurldecode($this->params['authstr']));
  1071. if (isset($encodestr)) {
  1072. $this->uid = $encodestr;
  1073. } else {
  1074. exit('0');
  1075. }
  1076. $dir1 = ceil($this->uid / 10000);
  1077. $dir2 = ceil($this->uid % 10000 / 1000);
  1078. //创建图片存储文件夹
  1079. $avatarfile = 'data/avatar/';
  1080. $dir = $avatarfile . $dir1 . '/' . $dir2 . '/' . $this->uid . '/';
  1081. if (!file_exists($dir)) {
  1082. mkdir($dir, 0777, true);
  1083. }
  1084. //存储flashpost图片
  1085. $this->data['avatardata'] = $GLOBALS['HTTP_RAW_POST_DATA'];
  1086. $filename = Doo::conf()->SITE_PATH . $dir . $this->uid . '.zip';
  1087. file_put_contents($filename, $this->data['avatardata']);
  1088. //解压缩文件
  1089. Doo::loadClass('pclzip.lib');
  1090. // pc_base::load_app_class('pclzip', 'phpsso', 0);
  1091. $archive = new PclZip($filename);
  1092. if ($archive->extract(PCLZIP_OPT_PATH, $dir) == 0) {
  1093. die("Error : " . $archive->errorInfo(true));
  1094. }
  1095. // 判断文件安全,删除压缩包和非jpg图片
  1096. $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
  1097. if ($handle = opendir($dir)) {
  1098. while (false !== ($file = readdir($handle))) {
  1099. if ($file !== '.' && $file !== '..') {
  1100. if (!in_array($file, $avatararr)) {
  1101. @unlink($dir . $file);
  1102. } else {
  1103. $info = @getimagesize($dir . $file);
  1104. if (!$info || $info[2] != 2) {
  1105. @unlink($dir . $file);
  1106. }
  1107. }
  1108. }
  1109. }
  1110. closedir($handle);
  1111. }
  1112. exit('1');
  1113. }
  1114. /**
  1115. * 存储支付方式
  1116. *
  1117. */
  1118. public function setPayWay() {
  1119. $authstr = $this->authcode($_POST['data']);
  1120. $ddddd = explode('&', urldecode($authstr));
  1121. foreach ($ddddd as $k => $v) {
  1122. list($key, $val) = explode(' = ', $v);
  1123. $TmpArray[$key] = $val;
  1124. }
  1125. Doo::loadModel('users_payway');
  1126. $upayway = new UsersPayway();
  1127. $upayway->uid = $TmpArray['uid'];
  1128. $upayway->idbank = $TmpArray['idbank'];
  1129. $upayway->idalipay = $TmpArray['alipay'];
  1130. $upayway->idtenpay = $TmpArray['tenpay'];
  1131. if ($upayway->insert())
  1132. exit('1');
  1133. }
  1134. /**
  1135. * 实名认证
  1136. *
  1137. */
  1138. public function setVerify() {
  1139. Doo::loadModel('users_verify');
  1140. }
  1141. /**
  1142. * 删除用户头像
  1143. * @return {0:失败;1:成功}
  1144. */
  1145. public function deleteavatar() {
  1146. //根据用户id创建文件夹
  1147. if (isset($this->data['uid'])) {
  1148. $this->uid = $this->data['uid'];
  1149. } else {
  1150. exit('0');
  1151. }
  1152. $dir1 = ceil($this->uid / 10000);
  1153. $dir2 = ceil($this->uid % 10000 / 1000);
  1154. //图片存储文件夹
  1155. $avatarfile = pc_base::load_config('system', 'upload_path') . 'avatar/';
  1156. $dir = $avatarfile . $dir1 . '/' . $dir2 . '/' . $this->uid . '/';
  1157. $this->db->update(array('avatar' => 0), array('uid' => $this->uid));
  1158. if (!file_exists($dir)) {
  1159. exit('1');
  1160. } else {
  1161. if ($handle = opendir($dir)) {
  1162. while (false !== ($file = readdir($handle))) {
  1163. if ($file !== '.' && $file !== '..') {
  1164. @unlink($dir . $file);
  1165. }
  1166. }
  1167. closedir($handle);
  1168. @rmdir($dir);
  1169. exit('1');
  1170. }
  1171. }
  1172. }
  1173. /**
  1174. *
  1175. * @param type $string
  1176. * @param type $operation
  1177. * @param type $key
  1178. * @param type $expiry
  1179. * @return string
  1180. */
  1181. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  1182. $ckey_length = 4;
  1183. $key = md5($key != '' ? $key : Doo::conf()->AUTHKEY);
  1184. $keya = md5(substr($key, 0, 16));
  1185. $keyb = md5(substr($key, 16, 16));
  1186. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  1187. $cryptkey = $keya . md5($keya . $keyc);
  1188. $key_length = strlen($cryptkey);
  1189. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  1190. $string_length = strlen($string);
  1191. $result = '';
  1192. $box = range(0, 255);
  1193. $rndkey = array();
  1194. for ($i = 0; $i <= 255; $i++) {
  1195. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  1196. }
  1197. for ($j = $i = 0; $i < 256; $i++) {
  1198. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  1199. $tmp = $box[$i];
  1200. $box[$i] = $box[$j];
  1201. $box[$j] = $tmp;
  1202. }
  1203. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  1204. $a = ($a + 1) % 256;
  1205. $j = ($j + $box[$a]) % 256;
  1206. $tmp = $box[$a];
  1207. $box[$a] = $box[$j];
  1208. $box[$j] = $tmp;
  1209. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  1210. }
  1211. if ($operation == 'DECODE') {
  1212. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  1213. return substr($result, 26);
  1214. } else {
  1215. return '';
  1216. }
  1217. } else {
  1218. return $keyc . str_replace(' = ', '', base64_encode($result));
  1219. }
  1220. }
  1221. public function LoginSetCookie() {
  1222. $user = Doo::db()->find('users', array(
  1223. 'where' => 'username = ?',
  1224. 'param' => array($this->params['username']),
  1225. ));
  1226. 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"');
  1227. // 通行证
  1228. setcookie(Doo::conf()->COOKIEPRE . 'auth', $this->authcode($user[0]->id . "\t" . $this->clientIP(), 'ENCODE'), 0, '/', '192.168.1.140', 0);
  1229. setcookie(Doo::conf()->COOKIEPRE . '_userid', $this->authcode($user[0]->id, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  1230. setcookie(Doo::conf()->COOKIEPRE . '_username', $this->authcode($user[0]->username, 'ENCODE'), 0, '/', '192.168.1.140', 0);
  1231. // 文库
  1232. }
  1233. public function getBank() {
  1234. // TODO:远程字符串解析提取出来做成函数
  1235. $authstr = $this->authcode($_POST['data']);
  1236. $ddddd = explode('&', urldecode($authstr));
  1237. foreach ($ddddd as $k => $v) {
  1238. list($key, $val) = explode('=', $v);
  1239. $TmpArray[$key] = $val;
  1240. $this->$key = $val;
  1241. }
  1242. $uid = $TmpArray['uid'];
  1243. if ($uid > 0) {
  1244. $r = Doo::db()->find('users', array(
  1245. 'select' => 'bank',
  1246. 'where' => 'id=?',
  1247. 'param' => array($uid),
  1248. 'asArray' => TRUE
  1249. )
  1250. );
  1251. }
  1252. exit(json_encode(array($r[0]['bank'])));
  1253. }
  1254. public function getPayOrder() {
  1255. // TODO:远程字符串解析提取出来做成函数
  1256. $authstr = $this->authcode($_POST['data']);
  1257. $ddddd = explode('&', urldecode($authstr));
  1258. foreach ($ddddd as $k => $v) {
  1259. list($key, $val) = explode('=', $v);
  1260. $TmpArray[$key] = $val;
  1261. $this->$key = $val;
  1262. }
  1263. $uid = $TmpArray['uid'];
  1264. if ($uid > 0) {
  1265. $r = Doo::db()->find('pay', array(
  1266. 'where' => 'userid=?',
  1267. 'param' => array($uid),
  1268. 'asArray' => TRUE
  1269. )
  1270. );
  1271. }
  1272. exit(json_encode($r));
  1273. }
  1274. public function madd() {
  1275. // TODO:认证,变量判断
  1276. $authstr = $this->authcode($_POST['data']);
  1277. $ddddd = explode('&', urldecode($authstr));
  1278. foreach ($ddddd as $k => $v) {
  1279. list($key, $val) = explode('=', $v);
  1280. $TmpArray[$key] = $val;
  1281. $this->$key = $val;
  1282. }
  1283. $uid = $TmpArray['uid'];
  1284. if ($uid > 0) {
  1285. $usersArray = Doo::db()->find('users', array(
  1286. 'where' => 'id=?',
  1287. 'param' => array($uid),
  1288. 'asArray' => TRUE
  1289. )
  1290. );
  1291. if (empty($usersArray)) {
  1292. exit('0');
  1293. } else {
  1294. $usersObject = Doo::loadModel('users', TRUE);
  1295. $usersObject->id = $usersArray[0]['id'];
  1296. $usersObject->bank = new DooDbExpression('bank+' . $this->bank);
  1297. if ($usersObject->update()) {
  1298. //TODO:写入日志
  1299. Doo::loadModel('income_pay');
  1300. $incomepay = new IncomePay();
  1301. $incomepay->uid = $usersArray[0]['id'];
  1302. $incomepay->money = $this->bank;
  1303. $incomepay->type = 1;
  1304. $incomepay->comefrom = $this->appid == 'ask' ? '问答(zhzdwd.com)收入' : '文库(zhzdwk.com)收入';
  1305. $incomepay->addtime = time();
  1306. $incomepay->insert();
  1307. exit('1');
  1308. } else {
  1309. exit('0');
  1310. }
  1311. }
  1312. } else {
  1313. exit('0');
  1314. }
  1315. }
  1316. public function msub() {
  1317. // TODO:远程字符串解析提取出来做成函数
  1318. $authstr = $this->authcode($_POST['data']);
  1319. $ddddd = explode('&', urldecode($authstr));
  1320. foreach ($ddddd as $k => $v) {
  1321. list($key, $val) = explode('=', $v);
  1322. $TmpArray[$key] = $val;
  1323. $this->$key = $val;
  1324. }
  1325. $uid = $TmpArray['uid'];
  1326. if ($uid > 0) {
  1327. $usersArray = Doo::db()->find('users', array(
  1328. 'where' => 'id=?',
  1329. 'param' => array($uid),
  1330. 'asArray' => TRUE
  1331. )
  1332. );
  1333. if (empty($usersArray)) {
  1334. exit('0');
  1335. } else {
  1336. if ($usersArray[0]['bank'] < $this->bank) {
  1337. exit('-1');
  1338. }
  1339. $usersObject = Doo::loadModel('users', TRUE);
  1340. $usersObject->id = $usersArray[0]['id'];
  1341. $usersObject->bank = new DooDbExpression('bank-' . $this->bank);
  1342. if ($usersObject->update()) {
  1343. //TODO:写入日志
  1344. Doo::loadModel('income_pay');
  1345. $incomepay = new IncomePay();
  1346. $incomepay->uid = $usersArray[0]['id'];
  1347. $incomepay->money = $this->bank;
  1348. $incomepay->type = 2;
  1349. $incomepay->comefrom = $this->appid == 'ask' ? '问答(zhzdwd.com)消费' : '文库(zhzdwk.com)消费';
  1350. $incomepay->addtime = time();
  1351. $incomepay->insert();
  1352. exit('1');
  1353. } else {
  1354. exit('0');
  1355. }
  1356. }
  1357. } else {
  1358. exit('0');
  1359. }
  1360. }
  1361. /**
  1362. * random 获取字符串
  1363. * @param int $length
  1364. * @return string $hash
  1365. */
  1366. public function random_k($length = 6, $type = 0) {
  1367. $hash = '';
  1368. $chararr = array ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' );
  1369. $chars = $chararr [$type];
  1370. $max = strlen ( $chars ) - 1;
  1371. PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
  1372. for($i = 0; $i < $length; $i ++) {
  1373. $hash .= $chars [mt_rand ( 0, $max )];
  1374. }
  1375. return $hash;
  1376. }
  1377. /**
  1378. * 实现短信验证码接口
  1379. *
  1380. */
  1381. protected function sendSms($number,$code){
  1382. $send = array(
  1383. 'apikey' => 'fb5ef483e44b9556512a9febef376051',
  1384. 'mobile' => $number,
  1385. 'text' => '【纵横通行账号】您的验证码是'.$code.',15分钟内有效。'
  1386. );
  1387. $data = http_build_query($send);
  1388. $res = json_decode($this->_httpClient($data));
  1389. $resArr = $this->objectToArray($res);
  1390. if (!empty($resArr) && $resArr["code"] == 0) return true;
  1391. else {
  1392. if (empty($this->errorMsg)) $this->errorMsg = isset($resArr["msg"]) ? $resArr["msg"] : '未知错误';
  1393. return false;
  1394. }
  1395. }
  1396. //对象转数组,使用get_object_vars返回对象属性组成的数组
  1397. function objectToArray($array){
  1398. if(is_object($array)) {
  1399. $array = (array)$array;
  1400. } if(is_array($array)) {
  1401. foreach($array as $key=>$value) {
  1402. $array[$key] = $this->objectToArray($value);
  1403. }
  1404. }
  1405. return $array;
  1406. }
  1407. /**
  1408. * POST方式访问短信接口
  1409. * @param string $data
  1410. * @return mixed
  1411. */
  1412. private function _httpClient($data) {
  1413. $sms_api_url = 'http://sms.haotingyun.com/v2/sms/single_send.json';
  1414. try {
  1415. $ch = curl_init();
  1416. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
  1417. curl_setopt($ch, CURLOPT_URL,$sms_api_url);
  1418. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1419. curl_setopt($ch, CURLOPT_POST, 1);
  1420. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  1421. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1422. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1423. $res = curl_exec($ch);
  1424. curl_close($ch);
  1425. return $res;
  1426. } catch (Exception $e) {
  1427. $this->errorMsg = $e->getMessage();
  1428. return false;
  1429. }
  1430. }
  1431. /**
  1432. * 随机生成验证码
  1433. */
  1434. function getRandomCheckCode() {
  1435. $chars = '0123456789';
  1436. mt_srand((double)microtime()*1000000*getmypid());
  1437. $CheckCode="";
  1438. while(strlen($CheckCode)<6)
  1439. $CheckCode.=substr($chars,(mt_rand()%strlen($chars)),1);
  1440. return $CheckCode;
  1441. }
  1442. // public function askUpdate(){
  1443. // $authstr = $this->authcode($_POST['data']);
  1444. // $ddddd = explode('&', urldecode($authstr));
  1445. // foreach ($ddddd as $k => $v) {
  1446. // list($key, $val) = explode('=', $v);
  1447. // $TmpArray[$key] = $val;
  1448. // $this->$key = $val;
  1449. // }
  1450. // $uname = $TmpArray['uname'];
  1451. // if (!empty($uname)) {
  1452. // $usersArray = Doo::db()->find('users', array(
  1453. // 'where' => 'username=?',
  1454. // 'param' => array($uname),
  1455. // 'asArray' => TRUE
  1456. // )
  1457. // );
  1458. //
  1459. // if (empty($usersArray)) {
  1460. // exit('0');
  1461. // } else {
  1462. // //TODO:写入日志
  1463. // Doo::loadModel('income_pay');
  1464. // $incomepay = new IncomePay();
  1465. // $incomepay->uid = $usersArray[0]['id'];
  1466. // $incomepay->money = $this->money;
  1467. // $incomepay->type = $this->type;
  1468. // $incomepay->comefrom = $this->type == 2 ? '问答(zhzdwd.com)消费' : '问答(zhzdwd.com)收入';
  1469. // $incomepay->addtime = $this->time;
  1470. // $incomepay->insert();
  1471. // exit('1');
  1472. // }
  1473. // } else {
  1474. // exit('0');
  1475. // }
  1476. // }
  1477. //
  1478. // public function wenkuUpdate(){
  1479. // $authstr = $this->authcode($_POST['data']);
  1480. // $ddddd = explode('&', urldecode($authstr));
  1481. // foreach ($ddddd as $k => $v) {
  1482. // list($key, $val) = explode('=', $v);
  1483. // $TmpArray[$key] = $val;
  1484. // $this->$key = $val;
  1485. // }
  1486. // //TODO:写入日志
  1487. // Doo::loadModel('income_pay');
  1488. // $incomepay = new IncomePay();
  1489. // $incomepay->uid = $this->uid;
  1490. // $incomepay->money = $this->money;
  1491. // $incomepay->type = $this->type;
  1492. // $incomepay->comefrom = $this->type == 2 ? '文库(zhzdwk.com)消费' : '文库(zhzdwk.com)收入';
  1493. // $incomepay->addtime = $this->time;
  1494. // $incomepay->insert();
  1495. // exit('1');
  1496. // }
  1497. }
  1498. ?>