1 /**
2 * 检查是否是一个序列化数据.
3 *
4 * If $data is not an string, then returned value will always be false.
5 * Serialized data is always a string.
6 *
7 * @since 2.0.5
8 *
9 * @param mixed $data Value to check to see if was serialized.
10 * @return bool False if not serialized and true if it was.
11 */
12 function is_serialized( $data ) {
13 // if it isn't a string, it isn't serialized
14 if ( !is_string( $data ) )
15 return false;
16 $data = trim( $data );
17 if ( 'N;' == $data )
18 return true;
19 if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
20 return false;
21 switch ( $badions[1] ) {
22 case 'a' :
23 case 'O' :
24 case 's' :
25 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
26 return true;
27 break;
28 case 'b' :
29 case 'i' :
30 case 'd' :
31 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
32 return true;
33 break;
34 }
35 return false;
36 }
37
38 /**
39 * 检查是否是一个字符串序列化数据
40 *
41 * @since 2.0.5
42 *
43 * @param mixed $data Serialized data
44 * @return bool False if not a serialized string, true if it is.
45 */
46 function is_serialized_string( $data ) {
47 // if it isn't a string, it isn't a serialized string
48 if ( !is_string( $data ) )
49 return false;
50 $data = trim( $data );
51 if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
52 return true;
53 return false;
54 }
55
56 /**
57 * http状态转换成说明
58 *
59 * @since 2.3.0
60 *
61 * @param int $code HTTP status code.
62 * @return string Empty string if not found, or description if found.
63 */
64 function get_status_header_desc( $code ) {
65 global $wp_header_to_desc;
66
67 $code = absint( $code );
68
69 if ( !isset( $wp_header_to_desc ) ) {
70 $wp_header_to_desc = array(
71 100 => 'Continue',
72 101 => 'Switching Protocols',
73
74 200 => 'OK',
75 201 => 'Created',
76 202 => 'Accepted',
77 203 => 'Non-Authoritative Information',
78 204 => 'No Content',
79 205 => 'Reset Content',
80 206 => 'Partial Content',
81
82 300 => 'Multiple Choices',
83 301 => 'Moved Permanently',
84 302 => 'Found',
85 303 => 'See Other',
86 304 => 'Not Modified',
87 305 => 'Use Proxy',
88 307 => 'Temporary Redirect',
89
90 400 => 'Bad Request',
91 401 => 'Unauthorized',
92 403 => 'Forbidden',
93 404 => 'Not Found',
94 405 => 'Method Not Allowed',
95 406 => 'Not Acceptable',
96 407 => 'Proxy Authentication Required',
97 408 => 'Request Timeout',
98 409 => 'Conflict',
99 410 => 'Gone',
100 411 => 'Length Required',
101 412 => 'Precondition Failed',
102 413 => 'Request Entity Too Large',
103 414 => 'Request-URI Too Long',
104 415 => 'Unsupported Media Type',
105 416 => 'Requested Range Not Satisfiable',
106 417 => 'Expectation Failed',
107
108 500 => 'Internal Server Error',
109 501 => 'Not Implemented',
110 502 => 'Bad Gateway',
111 503 => 'Service Unavailable',
112 504 => 'Gateway Timeout',
113 505 => 'HTTP Version Not Supported'
114 );
115 }
116
117 if ( isset( $wp_header_to_desc[$code] ) )
118 return $wp_header_to_desc[$code];
119 else
120 return '';
121 }
122
123 /**
124 * 发送一个让页面不缓存的http头信息
125 *
126 * Different browsers support different nocache headers, so several headers must
127 * be sent so that all of them get the point that no caching should occur.
128 *
129 * @since 2.0.0
130 */
131 function nocache_headers() {
132 // why are these @-silenced when other header calls aren't?
133 @header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
134 @header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
135 @header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
136 @header( 'Pragma: no-cache' );
137 }
138
139 /**
140 * 让javascript缓存10天的http头信息
141 *
142 * @since 2.1.0
143 */
144 function cache_javascript_headers() {
145 $expiresOffset = 864000; // 10 days
146 header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
147 header( "Vary: Accept-Encoding" ); // Handle proxies
148 header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
149 }
150
151 /**
152 * 递归创建一个完整的目录(整个目录中的子目录不存在都创建)
153 *
154 * Will attempt to set permissions on folders.
155 *
156 * @since 2.0.1
157 *
158 * @param string $target Full path to attempt to create.
159 * @return bool Whether the path was created or not. True if path already exists.
160 */
161 function wp_mkdir_p( $target ) {
162 // from php.net/mkdir user contributed notes
163 $target = str_replace( '//', '/', $target );
164 if ( file_exists( $target ) )
165 return @is_dir( $target );
166
167 // Attempting to create the directory may clutter up our display.
168 if ( @mkdir( $target ) ) {
169 $stat = @stat( dirname( $target ) );
170 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
171 @chmod( $target, $dir_perms );
172 return true;
173 } elseif ( is_dir( dirname( $target ) ) ) {
174 return false;
175 }
176
177 // If the above failed, attempt to create the parent node, then try again.
178 if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) )
179 return wp_mkdir_p( $target );
180
181 return false;
182 }
183
184 /**
185 * 检查是否是个绝对路径 ('/foo/bar', 'c:\windows').
186 *
187 * @since 2.5.0
188 *
189 * @param string $path File path
190 * @return bool True if path is absolute, false is not absolute.
191 */
192 function path_is_absolute( $path ) {
193 // this is definitive if true but fails if $path does not exist or contains a symbolic link
194 if ( realpath($path) == $path )
195 return true;
196
197 if ( strlen($path) == 0 || $path{0} == '.' )
198 return false;
199
200 // windows allows absolute paths like this
201 if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
202 return true;
203
204 // a path starting with / or \ is absolute; anything else is relative
205 return (bool) preg_match('#^[/\\\\]#', $path);
206 }
207
208 /**
209 * 根据扩展名获得文件类型
210 *
211 * @package WordPress
212 * @since 2.5.0
213 * @uses apply_filters() Calls 'ext2type' hook on default supported types.
214 *
215 * @param string $ext The extension to search.
216 * @return string|null The file type, example: audio, video, document, spreadsheet, etc. Null if not found.
217 */
218 function wp_ext2type( $ext ) {
219 $ext2type = apply_filters('ext2type', array(
220 'audio' => array('aac','ac3','aif','aiff','mp1','mp2','mp3','m3a','m4a','m4b','ogg','ram','wav','wma'),
221 'video' => array('asf','avi','divx','dv','mov','mpg','mpeg','mp4','mpv','ogm','qt','rm','vob','wmv'),
222 'document' => array('doc','docx','pages','odt','rtf','pdf'),
223 'spreadsheet' => array('xls','xlsx','numbers','ods'),
224 'interactive' => array('ppt','pptx','key','odp','swf'),
225 'text' => array('txt'),
226 'archive' => array('tar','bz2','gz','cab','dmg','rar','sea','sit','sqx','zip'),
227 'code' => array('css','html','php','js'),
228 ));
229 foreach ( $ext2type as $type => $exts )
230 if ( in_array($ext, $exts) )
231 return $type;
232 }
233
234 /**
235 * 转换成正整数
236 *
237 * @since 2.5.0
238 *
239 * @param mixed $maybeint Data you wish to have convered to an absolute integer
240 * @return int An absolute integer
241 */
242 function absint( $maybeint ) {
243 return abs( intval( $maybeint ) );
244 }
2 * 检查是否是一个序列化数据.
3 *
4 * If $data is not an string, then returned value will always be false.
5 * Serialized data is always a string.
6 *
7 * @since 2.0.5
8 *
9 * @param mixed $data Value to check to see if was serialized.
10 * @return bool False if not serialized and true if it was.
11 */
12 function is_serialized( $data ) {
13 // if it isn't a string, it isn't serialized
14 if ( !is_string( $data ) )
15 return false;
16 $data = trim( $data );
17 if ( 'N;' == $data )
18 return true;
19 if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
20 return false;
21 switch ( $badions[1] ) {
22 case 'a' :
23 case 'O' :
24 case 's' :
25 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
26 return true;
27 break;
28 case 'b' :
29 case 'i' :
30 case 'd' :
31 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
32 return true;
33 break;
34 }
35 return false;
36 }
37
38 /**
39 * 检查是否是一个字符串序列化数据
40 *
41 * @since 2.0.5
42 *
43 * @param mixed $data Serialized data
44 * @return bool False if not a serialized string, true if it is.
45 */
46 function is_serialized_string( $data ) {
47 // if it isn't a string, it isn't a serialized string
48 if ( !is_string( $data ) )
49 return false;
50 $data = trim( $data );
51 if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
52 return true;
53 return false;
54 }
55
56 /**
57 * http状态转换成说明
58 *
59 * @since 2.3.0
60 *
61 * @param int $code HTTP status code.
62 * @return string Empty string if not found, or description if found.
63 */
64 function get_status_header_desc( $code ) {
65 global $wp_header_to_desc;
66
67 $code = absint( $code );
68
69 if ( !isset( $wp_header_to_desc ) ) {
70 $wp_header_to_desc = array(
71 100 => 'Continue',
72 101 => 'Switching Protocols',
73
74 200 => 'OK',
75 201 => 'Created',
76 202 => 'Accepted',
77 203 => 'Non-Authoritative Information',
78 204 => 'No Content',
79 205 => 'Reset Content',
80 206 => 'Partial Content',
81
82 300 => 'Multiple Choices',
83 301 => 'Moved Permanently',
84 302 => 'Found',
85 303 => 'See Other',
86 304 => 'Not Modified',
87 305 => 'Use Proxy',
88 307 => 'Temporary Redirect',
89
90 400 => 'Bad Request',
91 401 => 'Unauthorized',
92 403 => 'Forbidden',
93 404 => 'Not Found',
94 405 => 'Method Not Allowed',
95 406 => 'Not Acceptable',
96 407 => 'Proxy Authentication Required',
97 408 => 'Request Timeout',
98 409 => 'Conflict',
99 410 => 'Gone',
100 411 => 'Length Required',
101 412 => 'Precondition Failed',
102 413 => 'Request Entity Too Large',
103 414 => 'Request-URI Too Long',
104 415 => 'Unsupported Media Type',
105 416 => 'Requested Range Not Satisfiable',
106 417 => 'Expectation Failed',
107
108 500 => 'Internal Server Error',
109 501 => 'Not Implemented',
110 502 => 'Bad Gateway',
111 503 => 'Service Unavailable',
112 504 => 'Gateway Timeout',
113 505 => 'HTTP Version Not Supported'
114 );
115 }
116
117 if ( isset( $wp_header_to_desc[$code] ) )
118 return $wp_header_to_desc[$code];
119 else
120 return '';
121 }
122
123 /**
124 * 发送一个让页面不缓存的http头信息
125 *
126 * Different browsers support different nocache headers, so several headers must
127 * be sent so that all of them get the point that no caching should occur.
128 *
129 * @since 2.0.0
130 */
131 function nocache_headers() {
132 // why are these @-silenced when other header calls aren't?
133 @header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
134 @header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
135 @header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
136 @header( 'Pragma: no-cache' );
137 }
138
139 /**
140 * 让javascript缓存10天的http头信息
141 *
142 * @since 2.1.0
143 */
144 function cache_javascript_headers() {
145 $expiresOffset = 864000; // 10 days
146 header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
147 header( "Vary: Accept-Encoding" ); // Handle proxies
148 header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
149 }
150
151 /**
152 * 递归创建一个完整的目录(整个目录中的子目录不存在都创建)
153 *
154 * Will attempt to set permissions on folders.
155 *
156 * @since 2.0.1
157 *
158 * @param string $target Full path to attempt to create.
159 * @return bool Whether the path was created or not. True if path already exists.
160 */
161 function wp_mkdir_p( $target ) {
162 // from php.net/mkdir user contributed notes
163 $target = str_replace( '//', '/', $target );
164 if ( file_exists( $target ) )
165 return @is_dir( $target );
166
167 // Attempting to create the directory may clutter up our display.
168 if ( @mkdir( $target ) ) {
169 $stat = @stat( dirname( $target ) );
170 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
171 @chmod( $target, $dir_perms );
172 return true;
173 } elseif ( is_dir( dirname( $target ) ) ) {
174 return false;
175 }
176
177 // If the above failed, attempt to create the parent node, then try again.
178 if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) )
179 return wp_mkdir_p( $target );
180
181 return false;
182 }
183
184 /**
185 * 检查是否是个绝对路径 ('/foo/bar', 'c:\windows').
186 *
187 * @since 2.5.0
188 *
189 * @param string $path File path
190 * @return bool True if path is absolute, false is not absolute.
191 */
192 function path_is_absolute( $path ) {
193 // this is definitive if true but fails if $path does not exist or contains a symbolic link
194 if ( realpath($path) == $path )
195 return true;
196
197 if ( strlen($path) == 0 || $path{0} == '.' )
198 return false;
199
200 // windows allows absolute paths like this
201 if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
202 return true;
203
204 // a path starting with / or \ is absolute; anything else is relative
205 return (bool) preg_match('#^[/\\\\]#', $path);
206 }
207
208 /**
209 * 根据扩展名获得文件类型
210 *
211 * @package WordPress
212 * @since 2.5.0
213 * @uses apply_filters() Calls 'ext2type' hook on default supported types.
214 *
215 * @param string $ext The extension to search.
216 * @return string|null The file type, example: audio, video, document, spreadsheet, etc. Null if not found.
217 */
218 function wp_ext2type( $ext ) {
219 $ext2type = apply_filters('ext2type', array(
220 'audio' => array('aac','ac3','aif','aiff','mp1','mp2','mp3','m3a','m4a','m4b','ogg','ram','wav','wma'),
221 'video' => array('asf','avi','divx','dv','mov','mpg','mpeg','mp4','mpv','ogm','qt','rm','vob','wmv'),
222 'document' => array('doc','docx','pages','odt','rtf','pdf'),
223 'spreadsheet' => array('xls','xlsx','numbers','ods'),
224 'interactive' => array('ppt','pptx','key','odp','swf'),
225 'text' => array('txt'),
226 'archive' => array('tar','bz2','gz','cab','dmg','rar','sea','sit','sqx','zip'),
227 'code' => array('css','html','php','js'),
228 ));
229 foreach ( $ext2type as $type => $exts )
230 if ( in_array($ext, $exts) )
231 return $type;
232 }
233
234 /**
235 * 转换成正整数
236 *
237 * @since 2.5.0
238 *
239 * @param mixed $maybeint Data you wish to have convered to an absolute integer
240 * @return int An absolute integer
241 */
242 function absint( $maybeint ) {
243 return abs( intval( $maybeint ) );
244 }