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

/Examples/Rbac/Lib/Action/PublicAction.class.php

http://thinkphp.googlecode.com/
PHP | 231 lines | 187 code | 11 blank | 33 comment | 21 complexity | 26785c4dcd39a78a9412948d9ecfe9e3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. class PublicAction extends Action {
  12. // ????????
  13. protected function checkUser() {
  14. if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
  15. $this->assign('jumpUrl','Public/login');
  16. $this->error('????');
  17. }
  18. }
  19. // ????
  20. public function top() {
  21. C('SHOW_RUN_TIME',false); // ??????
  22. C('SHOW_PAGE_TRACE',false);
  23. $model = M("Group");
  24. $list = $model->where('status=1')->getField('id,title');
  25. $this->assign('nodeGroupList',$list);
  26. $this->display();
  27. }
  28. // ????
  29. public function footer() {
  30. C('SHOW_RUN_TIME',false); // ??????
  31. C('SHOW_PAGE_TRACE',false);
  32. $this->display();
  33. }
  34. // ????
  35. public function menu() {
  36. $this->checkUser();
  37. if(isset($_SESSION[C('USER_AUTH_KEY')])) {
  38. //?????
  39. $menu = array();
  40. if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])) {
  41. //?????????????
  42. $menu = $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
  43. }else {
  44. //??????????????
  45. $node = M("Node");
  46. $id = $node->getField("id");
  47. $where['level']=2;
  48. $where['status']=1;
  49. $where['pid']=$id;
  50. $list = $node->where($where)->field('id,name,group_id,title')->order('sort asc')->select();
  51. $accessList = $_SESSION['_ACCESS_LIST'];
  52. foreach($list as $key=>$module) {
  53. if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
  54. //????????
  55. $module['access'] = 1;
  56. $menu[$key] = $module;
  57. }
  58. }
  59. //??????
  60. $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]] = $menu;
  61. }
  62. if(!empty($_GET['tag'])){
  63. $this->assign('menuTag',$_GET['tag']);
  64. }
  65. //dump($menu);
  66. $this->assign('menu',$menu);
  67. }
  68. C('SHOW_RUN_TIME',false); // ??????
  69. C('SHOW_PAGE_TRACE',false);
  70. $this->display();
  71. }
  72. // ???? ??????
  73. public function main() {
  74. $info = array(
  75. '????'=>PHP_OS,
  76. '????'=>$_SERVER["SERVER_SOFTWARE"],
  77. 'PHP????'=>php_sapi_name(),
  78. 'ThinkPHP??'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">??????</a> ]',
  79. '??????'=>ini_get('upload_max_filesize'),
  80. '??????'=>ini_get('max_execution_time').'?',
  81. '?????'=>date("Y?n?j? H:i:s"),
  82. '????'=>gmdate("Y?n?j? H:i:s",time()+8*3600),
  83. '?????/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
  84. '????'=>round((@disk_free_space(".")/(1024*1024)),2).'M',
  85. 'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
  86. 'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
  87. 'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
  88. );
  89. $this->assign('info',$info);
  90. $this->display();
  91. }
  92. // ??????
  93. public function login() {
  94. if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
  95. $this->display();
  96. }else{
  97. $this->redirect('Index/index');
  98. }
  99. }
  100. public function index()
  101. {
  102. //???????????
  103. redirect(__APP__);
  104. }
  105. // ????
  106. public function logout()
  107. {
  108. if(isset($_SESSION[C('USER_AUTH_KEY')])) {
  109. unset($_SESSION[C('USER_AUTH_KEY')]);
  110. unset($_SESSION);
  111. session_destroy();
  112. $this->assign("jumpUrl",__URL__.'/login/');
  113. $this->success('?????');
  114. }else {
  115. $this->error('?????');
  116. }
  117. }
  118. // ????
  119. public function checkLogin() {
  120. if(empty($_POST['account'])) {
  121. $this->error('?????');
  122. }elseif (empty($_POST['password'])){
  123. $this->error('?????');
  124. }elseif (empty($_POST['verify'])){
  125. $this->error('??????');
  126. }
  127. //??????
  128. $map = array();
  129. // ??????????
  130. $map['account'] = $_POST['account'];
  131. $map["status"] = array('gt',0);
  132. if($_SESSION['verify'] != md5($_POST['verify'])) {
  133. $this->error('??????');
  134. }
  135. import ( '@.ORG.Util.RBAC' );
  136. $authInfo = RBAC::authenticate($map);
  137. //??????????????????
  138. if(false === $authInfo) {
  139. $this->error('??????????');
  140. }else {
  141. if($authInfo['password'] != md5($_POST['password'])) {
  142. $this->error('?????');
  143. }
  144. $_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];
  145. $_SESSION['email'] = $authInfo['email'];
  146. $_SESSION['loginUserName'] = $authInfo['nickname'];
  147. $_SESSION['lastLoginTime'] = $authInfo['last_login_time'];
  148. $_SESSION['login_count'] = $authInfo['login_count'];
  149. if($authInfo['account']=='admin') {
  150. $_SESSION['administrator'] = true;
  151. }
  152. //??????
  153. $User = M('User');
  154. $ip = get_client_ip();
  155. $time = time();
  156. $data = array();
  157. $data['id'] = $authInfo['id'];
  158. $data['last_login_time'] = $time;
  159. $data['login_count'] = array('exp','login_count+1');
  160. $data['last_login_ip'] = $ip;
  161. $User->save($data);
  162. // ??????
  163. RBAC::saveAccessList();
  164. $this->success('?????');
  165. }
  166. }
  167. // ????
  168. public function changePwd()
  169. {
  170. $this->checkUser();
  171. //????????????????????
  172. if(md5($_POST['verify']) != $_SESSION['verify']) {
  173. $this->error('??????');
  174. }
  175. $map = array();
  176. $map['password']= pwdHash($_POST['oldpassword']);
  177. if(isset($_POST['account'])) {
  178. $map['account'] = $_POST['account'];
  179. }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
  180. $map['id'] = $_SESSION[C('USER_AUTH_KEY')];
  181. }
  182. //????
  183. $User = M("User");
  184. if(!$User->where($map)->field('id')->find()) {
  185. $this->error('?????????????');
  186. }else {
  187. $User->password = pwdHash($_POST['password']);
  188. $User->save();
  189. $this->success('???????');
  190. }
  191. }
  192. public function profile() {
  193. $this->checkUser();
  194. $User = M("User");
  195. $vo = $User->getById($_SESSION[C('USER_AUTH_KEY')]);
  196. $this->assign('vo',$vo);
  197. $this->display();
  198. }
  199. public function verify()
  200. {
  201. $type = isset($_GET['type'])?$_GET['type']:'gif';
  202. import("@.ORG.Util.Image");
  203. Image::buildImageVerify(4,1,$type);
  204. }
  205. // ????
  206. public function change() {
  207. $this->checkUser();
  208. $User = D("User");
  209. if(!$User->create()) {
  210. $this->error($User->getError());
  211. }
  212. $result = $User->save();
  213. if(false !== $result) {
  214. $this->success('???????');
  215. }else{
  216. $this->error('??????!');
  217. }
  218. }
  219. }
  220. ?>