[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> misc.php (source)

   1  <?php 
   2  
   3  /**
   4   * Retrieves a value from $_POST
   5   *
   6   * @param string $key name of post value
   7   * @param mixed $default value to return if $_POST[$key] doesn't exist. Default is ''
   8   * @return mixed $_POST[$key] if exists or $default
   9   */
  10  function gp_post( $key, $default = '' ) {
  11      return gp_array_get( $_POST, $key, $default );
  12  }
  13  
  14  /**
  15   * Retrieves a value from $_GET
  16   *
  17   * @param string $key name of get value
  18   * @param mixed $default value to return if $_GET[$key] doesn't exist. Default is ''
  19   * @return mixed $_GET[$key] if exists or $default
  20   */
  21  function gp_get( $key, $default = '' ) {
  22      return gp_array_get( $_GET, $key, $default );
  23  }
  24  
  25  /**
  26   * Retrieves a value from $array
  27   *
  28   * @param array $array
  29   * @param string $key name of array value
  30   * @param mixed $default value to return if $array[$key] doesn't exist. Default is ''
  31   * @return mixed $array[$key] if exists or $default
  32   */
  33  
  34  function gp_array_get( $array, $key, $default = '' ) {
  35      return isset( $array[$key] )? $array[$key] : $default;
  36  }
  37  
  38  function gp_const_get( $name, $default = '' ) {
  39      return defined( $name )? constant( $name ) : $default;
  40  }
  41  
  42  function gp_const_set( $name, $value ) {
  43      if ( defined( $name) ) {
  44          return false;
  45      }
  46      define( $name, $value );
  47      return true;
  48  }
  49  
  50  
  51  function gp_member_get( $object, $key, $default = '' ) {
  52      return isset( $object->$key )? $object->$key : $default;
  53  }
  54  
  55  /**
  56   * Makes from an array of arrays a flat array.
  57   *
  58   * @param array $array the arra to flatten
  59   * @return array flattenned array
  60   */
  61  function gp_array_flatten( $array ) {
  62      $res = array();
  63      foreach( $array as $value ) {
  64          $res = array_merge( $res, is_array( $value )? gp_array_flatten( $value ) : array( $value ) );
  65      }
  66      return $res;
  67  }
  68  
  69  /**
  70   * Passes the message set through the next redirect.
  71   *
  72   * Works best for edit requests, which want to pass error message or notice back to the listing page.
  73   * 
  74   * @param string $message The message to be passed
  75   * @param string $key Optional. Key for the message. You can pass several messages under different keys.
  76   * A key has one message. The default is 'notice'.
  77   */
  78  function gp_notice_set( $message, $key = 'notice' ) {
  79      backpress_set_cookie( '_gp_notice_'.$key, $message, 0, gp_url_path() );
  80  }
  81  
  82  /**
  83   * Retrieves a notice message, set by {@link gp_notice()}
  84   * 
  85   * @param string $key Optional. Message key. The default is 'notice'
  86   */
  87  function gp_notice( $key = 'notice' ) {
  88      return gp_array_get( GP::$redirect_notices, $key );
  89  }
  90  
  91  function gp_populate_notices() {
  92      GP::$redirect_notices = array();
  93      $prefix = '_gp_notice_';
  94      foreach ($_COOKIE as $key => $value ) {
  95          if ( gp_startswith( $key, $prefix ) && $suffix = substr( $key, strlen( $prefix ) )) {
  96              GP::$redirect_notices[$suffix] = $value;
  97              backpress_set_cookie( $key, '', 0, gp_url_path() );
  98          }
  99      }
 100  }
 101  
 102  /**
 103   * Sets headers, which redirect to another page.
 104   * 
 105   * @param string $location The path to redirect to
 106   * @param int $status Status code to use
 107   * @return bool False if $location is not set
 108   */
 109  function gp_redirect( $location, $status = 302 ) {
 110      // TODO: add server-guessing code from bb-load.php in a function in gp-includes/system.php
 111      global $is_IIS;
 112  
 113      $location = apply_filters( 'gp_redirect', $location, $status );
 114      $status = apply_filters( 'gp_redirect_status', $status, $location );
 115  
 116      if ( !$location ) // allows the gp_redirect filter to cancel a redirect
 117          return false;
 118  
 119      if ( $is_IIS ) {
 120          header( "Refresh: 0;url=$location" );
 121      } else {
 122          if ( php_sapi_name() != 'cgi-fcgi' )
 123              status_header( $status ); // This causes problems on IIS and some FastCGI setups
 124          header( "Location: $location" );
 125      }
 126  }
 127  
 128  /**
 129   * Returns a function, which returns the string "odd" or the string "even" alternatively.
 130   */
 131  function gp_parity_factory() {
 132      return create_function( '', 'static $parity = "odd"; if ($parity == "even") $parity = "odd"; else $parity = "even"; return $parity;');
 133  }
 134  
 135  /**
 136   * Builds SQL LIMIT/OFFSET clause for the given page
 137   * 
 138   * @param integer $page The page number. The first page is 1.
 139   * @param integer $per_page How many items are there in a page
 140   */
 141  function gp_limit_for_page( $page, $per_page ) {
 142      global $gpdb;
 143      $page = $page? $page - 1 : 0;
 144      return sprintf( 'LIMIT %d OFFSET %d', $per_page, $per_page * $page );
 145  }
 146  
 147  
 148  function _gp_get_secret_key( $key, $default_key = false ) {
 149      if ( !$default_key ) {
 150          global $gp_default_secret_key;
 151          $default_key = $gp_default_secret_key;
 152      }
 153  
 154      if ( defined( $key ) && '' != constant( $key ) && $default_key != constant( $key ) ) {
 155          return constant( $key );
 156      }
 157  
 158      return $default_key;
 159  }
 160  
 161  function _gp_get_salt( $constants, $option = false ) {
 162      if ( !is_array( $constants ) ) {
 163          $constants = array( $constants );
 164      }
 165  
 166      foreach ($constants as $constant ) {
 167          if ( defined( $constant ) ) {
 168              return constant( $constant );
 169          }
 170      }
 171  
 172      if ( !defined( 'GP_INSTALLING' ) || !GP_INSTALLING ) {
 173          if ( !$option ) {
 174              $option = strtolower( $constants[0] );
 175          }
 176          $salt = gp_get_option( $option );
 177          if ( empty( $salt ) ) {
 178              $salt = gp_generate_password();
 179              gp_update_option( $option, $salt );
 180          }
 181          return $salt;
 182      }
 183  
 184      return '';
 185  }
 186  
 187  if ( !function_exists( 'gp_salt' ) ) :
 188  function gp_salt($scheme = 'auth') {
 189      $secret_key = _gp_get_secret_key( 'GP_SECRET_KEY' );
 190  
 191      switch ($scheme) {
 192          case 'auth':
 193              $secret_key = _gp_get_secret_key( 'GP_AUTH_KEY', $secret_key );
 194              $salt = _gp_get_salt( array( 'GP_AUTH_SALT', 'GP_SECRET_SALT' ) );
 195              break;
 196  
 197          case 'secure_auth':
 198              $secret_key = _gp_get_secret_key( 'GP_SECURE_AUTH_KEY', $secret_key );
 199              $salt = _gp_get_salt( 'GP_SECURE_AUTH_SALT' );
 200              break;
 201  
 202          case 'logged_in':
 203              $secret_key = _gp_get_secret_key( 'GP_LOGGED_IN_KEY', $secret_key );
 204              $salt = _gp_get_salt( 'GP_LOGGED_IN_SALT' );
 205              break;
 206  
 207          case 'nonce':
 208              $secret_key = _gp_get_secret_key( 'GP_NONCE_KEY', $secret_key );
 209              $salt = _gp_get_salt( 'GP_NONCE_SALT' );
 210              break;
 211  
 212          default:
 213              // ensure each auth scheme has its own unique salt
 214              $salt = hash_hmac( 'md5', $scheme, $secret_key );
 215              break;
 216      }
 217  
 218      return apply_filters( 'salt', $secret_key . $salt, $scheme );
 219  }
 220  endif;
 221  
 222  if ( !function_exists( 'gp_hash' ) ) :
 223  function gp_hash( $data, $scheme = 'auth' ) {
 224      $salt = gp_salt( $scheme );
 225  
 226      return hash_hmac( 'md5', $data, $salt );
 227  }
 228  endif;
 229  
 230  if ( !function_exists( 'gp_generate_password' ) ) :
 231  /**
 232   * Generates a random password drawn from the defined set of characters
 233   * @return string the password
 234   */
 235  function gp_generate_password( $length = 12, $special_chars = true ) {
 236      return WP_Pass::generate_password( $length, $special_chars );
 237  }
 238  endif;
 239  
 240  /**
 241   * Returns an array of arrays, where the i-th array contains the i-th element from
 242   * each of the argument arrays. The returned array is truncated in length to the length
 243   * of the shortest argument array.
 244   * 
 245   * The function works only with numerical arrays.
 246   */
 247  function gp_array_zip() {
 248      $args = func_get_args();
 249      if ( !is_array( $args ) ) {
 250          return false;
 251      }
 252      if ( empty( $args ) ) {
 253          return array();
 254      }
 255      $res = array();
 256      foreach ( $args as &$array ) {
 257          if ( !is_array( $array) ) {
 258              return false;
 259          }
 260          reset( $array );
 261      }
 262      $all_have_more = true;
 263      while (true) {
 264          $this_round = array();
 265          foreach ( $args as &$array ) {
 266              $all_have_more = ( list( $key, $value ) = each( $array ) );
 267              if ( !$all_have_more ) {
 268                  break;
 269              }
 270              $this_round[] = $value;
 271          }
 272          if ( $all_have_more ) {
 273              $res[] = $this_round;
 274          } else {
 275              break;
 276          }
 277      }
 278      return $res;
 279  }
 280  
 281  function gp_array_any( $callback, $array ) {
 282      foreach( $array as $item ) {
 283          if ( $callback( $item ) ) {
 284              return true;
 285          }
 286      }
 287      return false;
 288  }
 289  
 290  function gp_array_all( $callback, $array ) {
 291      foreach( $array as $item ) {
 292          if ( !$callback( $item ) ) {
 293              return false;
 294          }
 295      }
 296      return true;
 297  }
 298  
 299  function gp_error_log_dump( $value ) {
 300      if ( is_array( $value ) || is_object( $value ) ) {
 301          $value = print_r( $value, true );
 302      }
 303      error_log( $value );
 304  }
 305  
 306  function gp_object_has_var( $object, $var_name ) {
 307      return in_array( $var_name, array_keys( get_object_vars( $object ) ) );
 308  }


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.