-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
143 lines (128 loc) · 3.03 KB
/
common.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
///////////////////////////////////
// LoveZhi PHPMVC System
// Longbill(longbill.cn@gmail.com)
// www.longbill.cn
// @2008-2009
// under Creative Commons License
///////////////////////////////////
/*
./common.php:
common things of every request
*/
//not support php version less than 4.1.0
PHP_VERSION < "4.1.0" && die('Sorry! Your PHP version is too old!');
//default time zone
function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT-8');
//for no cookie client to handle session_id via GET or POST
//( e.g. flash post data )
if (!defined('SESSION_STARTED'))
{
$_REQUEST['PHPSID'] && session_id($_REQUEST['PHPSID']);
session_start();
define('SESSION_STARTED','true');
}
//check convert encoding function
if(!function_exists('iconv'))
{
if (function_exists('mb_convert_encoding'))
{
function iconv($f,$t,$s) { return mb_convert_encoding ($s,$t,$f);}
}
else
{
function iconv() { die("<div style='color:red;font-size:20px;'>no convert encoding function found!</div>"); }
}
}
if (!function_exists('new_stripslashes'))
{
function new_stripslashes($string)
{
if(!is_array($string)) return stripslashes($string);
foreach($string as $key => $val)
{
$string[$key] = new_stripslashes($val);
}
return $string;
}
}
//cancle global variables
if (ini_get('register_globals'))
{
if (isset($_REQUEST['GLOBALS'])) die('what do you want to do?');
$_notunset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
$_input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
foreach($_input as $_k => $_v)
{
if (!in_array($_k, $_notunset) && isset($GLOBALS[$_k]))
{
$GLOBALS[$_k] = NULL;
unset($GLOBALS[$_k]);
}
}
unset($_input);
unset($_notunset);
}
//magic gpc
if(get_magic_quotes_gpc())
{
$_GET = new_stripslashes($_GET);
$_POST = new_stripslashes($_POST);
$_COOKIE = new_stripslashes($_COOKIE);
$_REQUEST = new_stripslashes($_REQUEST);
}
if (!function_exists("file_get_contents"))
{
function file_get_contents($path)
{
if (!file_exists($path)) return false;
$fp=@fopen($path,"r");
$all=fread($fp,filesize($path));
fclose($fp);
return $all;
}
}
if (!function_exists("file_put_contents"))
{
function file_put_contents($path,$val)
{
$fp=@fopen($path,"w");
fputs($fp,$val);
fclose($fp);
return true;
}
}
if (!function_exists('json_encode'))
{
function json_encode($arr)
{
$keys = array_keys($arr);
$isarr = true;
$json = "";
for ( $i=0; $i<count($keys); $i++)
{
if ($keys[$i] !== $i)
{
$isarr = false;
break;
}
}
$json = $space;
$json.= ($isarr)?"[":"{";
for( $i=0; $i<count($keys); $i++)
{
if ($i!=0) $json.= ",";
$item = $arr[$keys[$i]];
$json.= ($isarr)?"":$keys[$i].':';
if (is_array($item))
$json.= json_encode($item);
else if (is_string($item))
$json.= '"'.str_replace(array("\r","\n"),array('\r','\n'),$item).'"';
else
$json.= $item;
}
$json.= ($isarr)?"]":"}";
return $json;
}
}
?>