[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/ -> functions-compat.php (source)

   1  <?php
   2  /*
   3   * YOURLS
   4   * Compatibility functions when either missing from older PHP versions or not included by default
   5   */
   6  
   7  // @codeCoverageIgnoreStart
   8  
   9  /**
  10   * json_encode for PHP, should someone run a distro without php-json -- see http://askubuntu.com/questions/361424/
  11   *
  12   */
  13  if( !function_exists( 'json_encode' ) ) {
  14  	function json_encode( $array ) {
  15          return yourls_array_to_json( $array );
  16      }
  17  }
  18  
  19  /**
  20   * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
  21   *
  22   * @param array $array the array to convert.
  23   * @return mixed The resulting JSON string, or false if the argument was not an array.
  24   * @author Andy Rusterholz
  25   * @link http://php.net/json_encode (see comments)
  26   */
  27  function yourls_array_to_json( $array ){
  28  
  29      if( !is_array( $array ) ){
  30          return false;
  31      }
  32  
  33      $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
  34      if( $associative ){
  35  
  36          $construct = array();
  37          foreach( $array as $key => $value ){
  38  
  39              // We first copy each key/value pair into a staging array,
  40              // formatting each key and value properly as we go.
  41  
  42              // Format the key:
  43              if( is_numeric( $key ) ){
  44                  $key = "key_$key";
  45              }
  46              $key = '"'.addslashes( $key ).'"';
  47  
  48              // Format the value:
  49              if( is_array( $value )){
  50                  $value = yourls_array_to_json( $value );
  51              } else if( !is_numeric( $value ) || is_string( $value ) ){
  52                  $value = '"'.addslashes( $value ).'"';
  53              }
  54  
  55              // Add to staging array:
  56              $construct[] = "$key: $value";
  57          }
  58  
  59          // Then we collapse the staging array into the JSON form:
  60          $result = "{ " . implode( ", ", $construct ) . " }";
  61  
  62      } else { // If the array is a vector (not associative):
  63  
  64          $construct = array();
  65          foreach( $array as $value ){
  66  
  67              // Format the value:
  68              if( is_array( $value )){
  69                  $value = yourls_array_to_json( $value );
  70              } else if( !is_numeric( $value ) || is_string( $value ) ){
  71                  $value = '"'.addslashes($value).'"';
  72              }
  73  
  74              // Add to staging array:
  75              $construct[] = $value;
  76          }
  77  
  78          // Then we collapse the staging array into the JSON form:
  79          $result = "[ " . implode( ", ", $construct ) . " ]";
  80      }
  81  
  82      return $result;
  83  }
  84  
  85  
  86  /**
  87   * BC Math functions (assuming if one doesn't exist, none does)
  88   *
  89   */
  90  if ( !function_exists( 'bcdiv' ) ) {
  91  	function bcdiv( $dividend, $divisor ) {
  92          $quotient = floor( $dividend/$divisor );
  93          return $quotient;
  94      }
  95  	function bcmod( $dividend, $modulo ) {
  96          $remainder = $dividend%$modulo;
  97          return $remainder;
  98      }
  99  	function bcmul( $left, $right ) {
 100          return $left * $right;
 101      }
 102  	function bcadd( $left, $right ) {
 103          return $left + $right;
 104      }
 105  	function bcpow( $base, $power ) {
 106          return pow( $base, $power );
 107      }
 108  }
 109  
 110  // @codeCoverageIgnoreEnd


Generated: Sat Feb 22 05:10:06 2025 Cross-referenced by PHPXref 0.7.1