PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/compat.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 170 lines | 125 code | 30 blank | 15 comment | 30 complexity | 668088169c02953f5f2a52cd26beaba8 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * WordPress implementation for PHP functions missing from older PHP versions.
  4. *
  5. * @package PHP
  6. * @access private
  7. */
  8. // Added in PHP 5.0
  9. if (!function_exists('http_build_query')) {
  10. function http_build_query($data, $prefix=null, $sep=null) {
  11. return _http_build_query($data, $prefix, $sep);
  12. }
  13. }
  14. // from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
  15. function _http_build_query($data, $prefix=null, $sep=null, $key='', $urlencode=true) {
  16. $ret = array();
  17. foreach ( (array) $data as $k => $v ) {
  18. if ( $urlencode)
  19. $k = urlencode($k);
  20. if ( is_int($k) && $prefix != null )
  21. $k = $prefix.$k;
  22. if ( !empty($key) )
  23. $k = $key . '%5B' . $k . '%5D';
  24. if ( $v === NULL )
  25. continue;
  26. elseif ( $v === FALSE )
  27. $v = '0';
  28. if ( is_array($v) || is_object($v) )
  29. array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
  30. elseif ( $urlencode )
  31. array_push($ret, $k.'='.urlencode($v));
  32. else
  33. array_push($ret, $k.'='.$v);
  34. }
  35. if ( NULL === $sep )
  36. $sep = ini_get('arg_separator.output');
  37. return implode($sep, $ret);
  38. }
  39. if ( !function_exists('_') ) {
  40. function _($string) {
  41. return $string;
  42. }
  43. }
  44. if (!function_exists('stripos')) {
  45. function stripos($haystack, $needle, $offset = 0) {
  46. return strpos(strtolower($haystack), strtolower($needle), $offset);
  47. }
  48. }
  49. if ( !function_exists('hash_hmac') ):
  50. function hash_hmac($algo, $data, $key, $raw_output = false) {
  51. return _hash_hmac($algo, $data, $key, $raw_output);
  52. }
  53. endif;
  54. function _hash_hmac($algo, $data, $key, $raw_output = false) {
  55. $packs = array('md5' => 'H32', 'sha1' => 'H40');
  56. if ( !isset($packs[$algo]) )
  57. return false;
  58. $pack = $packs[$algo];
  59. if (strlen($key) > 64)
  60. $key = pack($pack, $algo($key));
  61. $key = str_pad($key, 64, chr(0));
  62. $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
  63. $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
  64. $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
  65. if ( $raw_output )
  66. return pack( $pack, $hmac );
  67. return $hmac;
  68. }
  69. if ( !function_exists('mb_substr') ):
  70. function mb_substr( $str, $start, $length=null, $encoding=null ) {
  71. return _mb_substr($str, $start, $length, $encoding);
  72. }
  73. endif;
  74. function _mb_substr( $str, $start, $length=null, $encoding=null ) {
  75. // the solution below, works only for utf-8, so in case of a different
  76. // charset, just use built-in substr
  77. $charset = get_option( 'blog_charset' );
  78. if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) {
  79. return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length);
  80. }
  81. // use the regex unicode support to separate the UTF-8 characters into an array
  82. preg_match_all( '/./us', $str, $match );
  83. $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
  84. return implode( '', $chars );
  85. }
  86. if ( !function_exists( 'htmlspecialchars_decode' ) ) {
  87. // Added in PHP 5.1.0
  88. // Error checks from PEAR::PHP_Compat
  89. function htmlspecialchars_decode( $string, $quote_style = ENT_COMPAT )
  90. {
  91. if ( !is_scalar( $string ) ) {
  92. trigger_error( 'htmlspecialchars_decode() expects parameter 1 to be string, ' . gettype( $string ) . ' given', E_USER_WARNING );
  93. return;
  94. }
  95. if ( !is_int( $quote_style ) && $quote_style !== null ) {
  96. trigger_error( 'htmlspecialchars_decode() expects parameter 2 to be integer, ' . gettype( $quote_style ) . ' given', E_USER_WARNING );
  97. return;
  98. }
  99. return wp_specialchars_decode( $string, $quote_style );
  100. }
  101. }
  102. // For PHP < 5.2.0
  103. if ( !function_exists('json_encode') ) {
  104. function json_encode( $string ) {
  105. global $wp_json;
  106. if ( !is_a($wp_json, 'Services_JSON') ) {
  107. require_once( ABSPATH . WPINC . '/class-json.php' );
  108. $wp_json = new Services_JSON();
  109. }
  110. return $wp_json->encodeUnsafe( $string );
  111. }
  112. }
  113. if ( !function_exists('json_decode') ) {
  114. function json_decode( $string, $assoc_array = false ) {
  115. global $wp_json;
  116. if ( !is_a($wp_json, 'Services_JSON') ) {
  117. require_once( ABSPATH . WPINC . '/class-json.php' );
  118. $wp_json = new Services_JSON();
  119. }
  120. $res = $wp_json->decode( $string );
  121. if ( $assoc_array )
  122. $res = _json_decode_object_helper( $res );
  123. return $res;
  124. }
  125. function _json_decode_object_helper($data) {
  126. if ( is_object($data) )
  127. $data = get_object_vars($data);
  128. return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
  129. }
  130. }
  131. // pathinfo that fills 'filename' without extension like in PHP 5.2+
  132. function pathinfo52($path) {
  133. $parts = pathinfo($path);
  134. if ( !isset($parts['filename']) ) {
  135. $parts['filename'] = substr( $parts['basename'], 0, strrpos($parts['basename'], '.') );
  136. if ( empty($parts['filename']) ) // there's no extension
  137. $parts['filename'] = $parts['basename'];
  138. }
  139. return $parts;
  140. }