param.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * param.class.php 参数处理类
  4. *
  5. * @copyright (C) 2005-2012 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2012-9-17
  8. */
  9. class param {
  10. //路由配置
  11. private $route_config = '';
  12. public function __construct() {
  13. if(!get_magic_quotes_gpc()) {
  14. $_POST = new_addslashes($_POST);
  15. $_GET = new_addslashes($_GET);
  16. $_REQUEST = new_addslashes($_REQUEST);
  17. $_COOKIE = new_addslashes($_COOKIE);
  18. }
  19. $this->route_config = pc_base::load_config('route', SITE_URL) ? pc_base::load_config('route', SITE_URL) : pc_base::load_config('route', 'default');
  20. if(isset($this->route_config['data']['POST']) && is_array($this->route_config['data']['POST'])) {
  21. foreach($this->route_config['data']['POST'] as $_key => $_value) {
  22. if(!isset($_POST[$_key])) $_POST[$_key] = $_value;
  23. }
  24. }
  25. if(isset($this->route_config['data']['GET']) && is_array($this->route_config['data']['GET'])) {
  26. foreach($this->route_config['data']['GET'] as $_key => $_value) {
  27. if(!isset($_GET[$_key])) $_GET[$_key] = $_value;
  28. }
  29. }
  30. if(isset($_GET['page'])) {
  31. $_GET['page'] = max(intval($_GET['page']),1);
  32. $_GET['page'] = min($_GET['page'],1000000000);
  33. }
  34. return true;
  35. }
  36. /**
  37. * 获取模型
  38. */
  39. public function route_m() {
  40. $m = isset($_GET['m']) && !empty($_GET['m']) ? $_GET['m'] : (isset($_POST['m']) && !empty($_POST['m']) ? $_POST['m'] : '');
  41. $m = $this->safe_deal($m);
  42. if (empty($m)) {
  43. return $this->route_config['m'];
  44. } else {
  45. if(is_string($m)) return $m;
  46. }
  47. }
  48. /**
  49. * 获取控制器
  50. */
  51. public function route_c() {
  52. $c = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : (isset($_POST['c']) && !empty($_POST['c']) ? $_POST['c'] : '');
  53. $c = $this->safe_deal($c);
  54. if (empty($c)) {
  55. return $this->route_config['c'];
  56. } else {
  57. if(is_string($c)) return $c;
  58. }
  59. }
  60. /**
  61. * 获取事件
  62. */
  63. public function route_a() {
  64. $a = isset($_GET['a']) && !empty($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) && !empty($_POST['a']) ? $_POST['a'] : '');
  65. $a = $this->safe_deal($a);
  66. if (empty($a)) {
  67. return $this->route_config['a'];
  68. } else {
  69. if(is_string($a)) return $a;
  70. }
  71. }
  72. /**
  73. * 设置 cookie
  74. * @param string $var 变量名
  75. * @param string $value 变量值
  76. * @param int $time 过期时间
  77. */
  78. public static function set_cookie($var, $value = '', $time = 0) {
  79. $time = $time > 0 ? $time : ($value == '' ? SYS_TIME - 3600 : 0);
  80. $s = $_SERVER['SERVER_PORT'] == '443' ? 1 : 0;
  81. $var = pc_base::load_config('system','cookie_pre').$var;
  82. $_COOKIE[$var] = $value;
  83. if (is_array($value)) {
  84. foreach($value as $k=>$v) {
  85. setcookie($var.'['.$k.']', sys_auth($v, 'ENCODE'), $time, pc_base::load_config('system','cookie_path'), pc_base::load_config('system','cookie_domain'), $s);
  86. }
  87. } else {
  88. setcookie($var, sys_auth($value, 'ENCODE'), $time, pc_base::load_config('system','cookie_path'), pc_base::load_config('system','cookie_domain'), $s);
  89. }
  90. }
  91. /**
  92. * 获取通过 set_cookie 设置的 cookie 变量
  93. * @param string $var 变量名
  94. * @param string $default 默认值
  95. * @return mixed 成功则返回cookie 值,否则返回 false
  96. */
  97. public static function get_cookie($var, $default = '') {
  98. $var = pc_base::load_config('system','cookie_pre').$var;
  99. $value = isset($_COOKIE[$var])?addslashes(sys_auth($_COOKIE[$var],'DECODE')):$default;
  100. if(in_array($var,array('_userid','userid','siteid','_groupid','_roleid'))) {
  101. $value = intval($value);
  102. } elseif(in_array($var,array('_username','username','_nickname','admin_username','sys_lang'))) { // site_model auth
  103. $value = safe_replace($value);
  104. }
  105. return $value;
  106. }
  107. /**
  108. * 安全处理函数
  109. * 处理m,a,c
  110. */
  111. private function safe_deal($str) {
  112. return str_replace(array('/', '.'), '', $str);
  113. }
  114. }
  115. ?>