| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Post |
| 3 |
{ |
| 4 |
protected $_postPrefix = 'hc-'; |
| 5 |
|
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
$this->_sanitize(); |
| 9 |
} |
| 10 |
|
| 11 |
public function get( $k = NULL ) |
| 12 |
{ |
| 13 |
if( ! empty($_POST) ){ |
| 14 |
$nonce = isset( $_REQUEST['hc_nonce'] ) ? $_REQUEST['hc_nonce'] : ''; |
| 15 |
if( function_exists('wp_verify_nonce') ){ |
| 16 |
if( ! wp_verify_nonce( $nonce, 'shiftcontroller' ) ){ |
| 17 |
die( 'Security check failed' ); |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
$return = array(); |
| 23 |
|
| 24 |
if( ! empty($_POST) ){ |
| 25 |
foreach( array_keys($_POST) as $key ){ |
| 26 |
if( substr($key, 0, strlen($this->_postPrefix)) != $this->_postPrefix ){ |
| 27 |
continue; |
| 28 |
} |
| 29 |
$my_key = substr($key, strlen($this->_postPrefix)); |
| 30 |
$return[$my_key] = $this->_fetch_from_array($_POST, $key); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if( NULL !== $k ){ |
| 35 |
$return = array_key_exists($k, $return) ? $return[$k] : NULL; |
| 36 |
} |
| 37 |
|
| 38 |
return $return; |
| 39 |
} |
| 40 |
|
| 41 |
protected function _fetch_from_array($array, $index = '') |
| 42 |
{ |
| 43 |
if ( ! isset($array[$index])){ |
| 44 |
return FALSE; |
| 45 |
} |
| 46 |
return $array[$index]; |
| 47 |
} |
| 48 |
|
| 49 |
protected function _sanitize() |
| 50 |
{ |
| 51 |
if (is_array($_POST) AND count($_POST) > 0){ |
| 52 |
foreach ($_POST as $key => $val){ |
| 53 |
if( substr($key, 0, strlen($this->_postPrefix)) !== $this->_postPrefix ){ |
| 54 |
continue; |
| 55 |
} |
| 56 |
$_POST[$this->_cleanInputKeys($key)] = $this->_cleanInputData($val); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
protected function _cleanInputKeys( $str ) |
| 62 |
{ |
| 63 |
if ( ! preg_match("/^[a-z0-9:_\/\-\~]+$/i", $str)){ |
| 64 |
exit('Disallowed Key Characters on: ' . '"' . esc_html($str) . '"' . '<br>'); |
| 65 |
} |
| 66 |
return $str; |
| 67 |
} |
| 68 |
|
| 69 |
protected function _cleanInputData( $str ) |
| 70 |
{ |
| 71 |
if( is_array($str) ){ |
| 72 |
$new_array = array(); |
| 73 |
foreach ($str as $key => $val){ |
| 74 |
$new_array[$this->_cleanInputKeys($key)] = $this->_cleanInputData($val); |
| 75 |
} |
| 76 |
return $new_array; |
| 77 |
} |
| 78 |
|
| 79 |
/* We strip slashes if magic quotes is on to keep things consistent |
| 80 |
NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 81 |
it will probably not exist in future versions at all. |
| 82 |
*/ |
| 83 |
$need_strip = FALSE; |
| 84 |
if( version_compare(PHP_VERSION, '5.4', '<') && get_magic_quotes_gpc() ){ |
| 85 |
$need_strip = TRUE; |
| 86 |
} |
| 87 |
elseif( defined('WPINC') ){ |
| 88 |
$need_strip = TRUE; |
| 89 |
} |
| 90 |
|
| 91 |
if( $need_strip ){ |
| 92 |
$str = stripslashes($str); |
| 93 |
} |
| 94 |
|
| 95 |
// Remove control characters |
| 96 |
$str = HC3_Functions::removeInvisibleCharacters($str); |
| 97 |
|
| 98 |
// Standardize newlines |
| 99 |
if (strpos($str, "\r") !== FALSE){ |
| 100 |
$str = str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str); |
| 101 |
} |
| 102 |
|
| 103 |
return $str; |
| 104 |
} |
| 105 |
} |