SafetyController.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace Home\Controller;
  3. /**
  4. * 安全
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-03-02T22:48:35+0800
  9. */
  10. class SafetyController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2017-03-02T22:48:35+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. }
  26. /**
  27. * [Index 首页]
  28. * @author Devil
  29. * @blog http://gong.gg/
  30. * @version 0.0.1
  31. * @datetime 2017-02-22T16:50:32+0800
  32. */
  33. public function Index()
  34. {
  35. // 安全信息列表
  36. $this->assign('safety_panel_list', L('safety_panel_list'));
  37. // 数据列表
  38. $data = array(
  39. 'mobile' => $this->user['mobile_security'],
  40. 'email' => $this->user['email_security'],
  41. );
  42. $this->assign('data', $data);
  43. $this->display('Index');
  44. }
  45. /**
  46. * [LoginPwdInfo 登录密码修改页面]
  47. * @author Devil
  48. * @blog http://gong.gg/
  49. * @version 0.0.1
  50. * @datetime 2017-03-28T10:12:20+0800
  51. */
  52. public function LoginPwdInfo()
  53. {
  54. $this->display('LoginPwdInfo');
  55. }
  56. /**
  57. * [MobileInfo 原手机号码修改页面]
  58. * @author Devil
  59. * @blog http://gong.gg/
  60. * @version 0.0.1
  61. * @datetime 2017-03-28T10:12:20+0800
  62. */
  63. public function MobileInfo()
  64. {
  65. if(empty($this->user['mobile']))
  66. {
  67. redirect(U('Home/Safety/NewMobileInfo'));
  68. }
  69. $this->display('MobileInfo');
  70. }
  71. /**
  72. * [NewMobileInfo 新手机号码修改页面]
  73. * @author Devil
  74. * @blog http://gong.gg/
  75. * @version 0.0.1
  76. * @datetime 2017-03-28T10:12:20+0800
  77. */
  78. public function NewMobileInfo()
  79. {
  80. if(!isset($_SESSION['safety_sms']) && !empty($this->user['mobile']))
  81. {
  82. $this->error(L('safety_original_accounts_check_error'), U('Home/Safety/MobileInfo'));
  83. }
  84. $this->display('NewMobileInfo');
  85. }
  86. /**
  87. * [EmailInfo 电子邮箱修改页面]
  88. * @author Devil
  89. * @blog http://gong.gg/
  90. * @version 0.0.1
  91. * @datetime 2017-03-28T10:12:20+0800
  92. */
  93. public function EmailInfo()
  94. {
  95. if(empty($this->user['email']))
  96. {
  97. redirect(U('Home/Safety/NewEmailInfo'));
  98. }
  99. $this->display('EmailInfo');
  100. }
  101. /**
  102. * [NewEmailInfo 新电子邮箱修改页面]
  103. * @author Devil
  104. * @blog http://gong.gg/
  105. * @version 0.0.1
  106. * @datetime 2017-03-28T10:12:20+0800
  107. */
  108. public function NewEmailInfo()
  109. {
  110. if(!isset($_SESSION['safety_email']) && !empty($this->user['email']))
  111. {
  112. $this->error(L('safety_original_accounts_check_error'), U('Home/Safety/EmailInfo'));
  113. }
  114. $this->display('NewEmailInfo');
  115. }
  116. /**
  117. * [VerifyEntry 验证码显示]
  118. * @author Devil
  119. * @blog http://gong.gg/
  120. * @version 0.0.1
  121. * @datetime 2017-03-05T15:10:21+0800
  122. */
  123. public function VerifyEntry()
  124. {
  125. $this->CommonVerifyEntry('safety');
  126. }
  127. /**
  128. * [LoginPwdUpdate 登录密码修改]
  129. * @author Devil
  130. * @blog http://gong.gg/
  131. * @version 0.0.1
  132. * @datetime 2017-03-28T10:38:23+0800
  133. */
  134. public function LoginPwdUpdate()
  135. {
  136. // 是否ajax请求
  137. if(!IS_AJAX)
  138. {
  139. $this->error(L('common_unauthorized_access'));
  140. }
  141. // 安全设置
  142. $m = D('Safety');
  143. // 校验
  144. if($m->create($_POST, 5) !== false)
  145. {
  146. // 获取用户账户信息
  147. $user = $m->field(array('id', 'pwd', 'salt'))->find($this->user['id']);
  148. // 密码校验
  149. if(LoginPwdEncryption(trim(I('my_pwd')), $user['salt']) != $user['pwd'])
  150. {
  151. $this->ajaxReturn(L('safety_my_pwd_error'), -4);
  152. }
  153. // 更新用户密码
  154. $salt = GetNumberCode(6);
  155. $data = array(
  156. 'pwd' => LoginPwdEncryption(trim(I('new_pwd')), $salt),
  157. 'salt' => $salt,
  158. 'upd_time' => time(),
  159. );
  160. // 更新数据库
  161. if($m->where(array('id'=>$this->user['id']))->save($data) !== false)
  162. {
  163. $this->ajaxReturn(L('common_operation_success'));
  164. } else {
  165. $this->ajaxReturn(L('common_operation_error'), -100);
  166. }
  167. } else {
  168. $this->ajaxReturn($m->getError(), -1);
  169. }
  170. }
  171. /**
  172. * [VerifySend 验证码发送]
  173. * @author Devil
  174. * @blog http://gong.gg/
  175. * @version 0.0.1
  176. * @datetime 2017-03-05T19:17:10+0800
  177. */
  178. public function VerifySend()
  179. {
  180. // 是否ajax请求
  181. if(!IS_AJAX)
  182. {
  183. $this->error(L('common_unauthorized_access'));
  184. }
  185. // 参数
  186. $type = I('type');
  187. $accounts = I('accounts');
  188. if(empty($accounts))
  189. {
  190. $accounts = ($type == 'sms') ? $this->user['mobile'] : $this->user['email'];
  191. } else {
  192. // 帐号是否已存在
  193. $this->IsExistAccounts($accounts, $type);
  194. }
  195. // 验证码基础参数
  196. $img_verify_param = array(
  197. 'key_prefix' => 'safety',
  198. 'expire_time' => MyC('common_verify_expire_time'),
  199. 'time_interval' => MyC('common_verify_time_interval'),
  200. );
  201. // 是否开启图片验证码
  202. $verify = $this->CommonIsImaVerify($img_verify_param);
  203. // 发送验证码
  204. $verify_param = array(
  205. 'key_prefix' => md5('safety_'.$accounts),
  206. 'expire_time' => MyC('common_verify_expire_time'),
  207. 'time_interval' => MyC('common_verify_time_interval'),
  208. );
  209. $code = GetNumberCode(6);
  210. if($type == 'sms')
  211. {
  212. $obj = new \My\Sms($verify_param);
  213. $state = $obj->SendText($accounts, MyC('home_sms_user_mobile_binding'), $code);
  214. } else {
  215. $obj = new \My\Email($verify_param);
  216. $email_param = array(
  217. 'email' => $accounts,
  218. 'content' => MyC('home_email_user_email_binding'),
  219. 'title' => MyC('home_site_name').' - '.L('safety_email_send_title'),
  220. 'code' => $code,
  221. );
  222. $state = $obj->SendHtml($email_param);
  223. }
  224. // 状态
  225. if($state)
  226. {
  227. // 清除验证码
  228. if(isset($verify) && is_object($verify))
  229. {
  230. $verify->Remove();
  231. }
  232. $this->ajaxReturn(L('common_send_success'));
  233. } else {
  234. $this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100);
  235. }
  236. }
  237. /**
  238. * [IsExistAccounts 帐号是否已存在]
  239. * @author Devil
  240. * @blog http://gong.gg/
  241. * @version 0.0.1
  242. * @datetime 2017-03-28T18:01:43+0800
  243. * @param [string] $accounts [帐号, 手机|邮箱]
  244. * @param [string] $type [帐号类型, sms|email]
  245. */
  246. private function IsExistAccounts($accounts, $type)
  247. {
  248. if($type == 'sms')
  249. {
  250. $user = M('User')->where(array('mobile'=>$accounts))->getField('id');
  251. } else {
  252. $user = M('User')->where(array('email'=>$accounts))->getField('id');
  253. }
  254. if(!empty($user))
  255. {
  256. $msg = ($type == 'sms') ? L('common_mobile_exist_error') : L('common_email_exist_error');
  257. $this->ajaxReturn($msg, -10);
  258. }
  259. }
  260. /**
  261. * [VerifyCheck 原账户验证码校验]
  262. * @author Devil
  263. * @blog http://gong.gg/
  264. * @version 0.0.1
  265. * @datetime 2017-03-28T15:57:19+0800
  266. */
  267. public function VerifyCheck()
  268. {
  269. // 是否ajax请求
  270. if(!IS_AJAX)
  271. {
  272. $this->error(L('common_unauthorized_access'));
  273. }
  274. // 参数
  275. $type = I('type');
  276. $accounts = I('accounts');
  277. if(empty($accounts))
  278. {
  279. $accounts = ($type == 'sms') ? $this->user['mobile'] : $this->user['email'];
  280. }
  281. // 验证码校验
  282. $verify_param = array(
  283. 'key_prefix' => md5('safety_'.$accounts),
  284. 'expire_time' => MyC('common_verify_expire_time')
  285. );
  286. if($type == 'sms')
  287. {
  288. $obj = new \My\Sms($verify_param);
  289. } else {
  290. $obj = new \My\Email($verify_param);
  291. }
  292. // 是否已过期
  293. if(!$obj->CheckExpire())
  294. {
  295. $this->ajaxReturn(L('common_verify_expire'), -10);
  296. }
  297. // 是否正确
  298. if($obj->CheckCorrect(I('verify')))
  299. {
  300. // 校验成功标记
  301. $_SESSION['safety_'.$type] = true;
  302. // 清除验证码
  303. $obj->Remove();
  304. $this->ajaxReturn(L('common_success'));
  305. } else {
  306. $this->ajaxReturn(L('common_verify_error'), -11);
  307. }
  308. }
  309. /**
  310. * [AccountsUpdate 账户更新]
  311. * @author Devil
  312. * @blog http://gong.gg/
  313. * @version 0.0.1
  314. * @datetime 2017-03-28T17:04:36+0800
  315. */
  316. public function AccountsUpdate()
  317. {
  318. // 是否ajax请求
  319. if(!IS_AJAX)
  320. {
  321. $this->error(L('common_unauthorized_access'));
  322. }
  323. // 参数
  324. $type = I('type');
  325. $accounts = I('accounts');
  326. if(empty($type) || empty($accounts))
  327. {
  328. $this->ajaxReturn(L('common_param_error'), -1);
  329. }
  330. // 帐号是否已存在
  331. $this->IsExistAccounts($accounts, $type);
  332. // 验证码校验
  333. $verify_param = array(
  334. 'key_prefix' => md5('safety_'.$accounts),
  335. 'expire_time' => MyC('common_verify_expire_time')
  336. );
  337. if($type == 'sms')
  338. {
  339. $obj = new \My\Sms($verify_param);
  340. } else {
  341. $obj = new \My\Email($verify_param);
  342. }
  343. // 是否已过期
  344. if(!$obj->CheckExpire())
  345. {
  346. $this->ajaxReturn(L('common_verify_expire'), -10);
  347. }
  348. // 是否正确
  349. if(!$obj->CheckCorrect(I('verify')))
  350. {
  351. $this->ajaxReturn(L('common_verify_error'), -11);
  352. }
  353. // 更新帐号
  354. $field = ($type == 'sms') ? 'mobile' : 'email';
  355. $data = array(
  356. $field => I('accounts'),
  357. 'upd_time' => time(),
  358. );
  359. // 更新数据库
  360. if(M('User')->where(array('id'=>$this->user['id']))->save($data) !== false)
  361. {
  362. // 更新用户session数据
  363. $this->UserLoginRecord($this->user['id']);
  364. // 校验成功标记
  365. unset($_SESSION['safety_'.$type]);
  366. // 清除验证码
  367. $obj->Remove();
  368. $this->ajaxReturn(L('common_operation_success'));
  369. } else {
  370. $this->ajaxReturn(L('common_operation_error'), -100);
  371. }
  372. }
  373. }
  374. ?>