| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
|
| 23 |
/** |
| 24 |
* Security helper class responsible for creation and verification nonce values. |
| 25 |
* |
| 26 |
* @category Visualizer |
| 27 |
* @package Security |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
class Visualizer_Security { |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns nonce salt. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* |
| 38 |
* @static |
| 39 |
* @access private |
| 40 |
* @param string $action The action what requires the nonce. |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
private static function _getSalt( $action = '' ) { |
| 44 |
return array( |
| 45 |
'__ip' => @$_SERVER['REMOTE_ADDR'], |
| 46 |
'__agent' => urlencode( @$_SERVER['HTTP_USER_AGENT'] ), |
| 47 |
'__userid' => get_current_user_id(), |
| 48 |
'__action' => $action, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates nonce. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @static |
| 58 |
* @access public |
| 59 |
* @param string $action The action what requires the nonce. |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public static function createNonce( $action = '' ) { |
| 63 |
return wp_create_nonce( implode( '/', array_slice( explode( '/', home_url() ), 0, 3 ) ) . add_query_arg( self::_getSalt( $action ) ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns TRUE if nonce correct. Otherwise FALSE. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @static |
| 72 |
* @access public |
| 73 |
* @param string $nonce The nonce to verify. |
| 74 |
* @param string $action The action what requires the nonce. |
| 75 |
* @return boolean TRUE if nonce is correct. Otherwise FALSE. |
| 76 |
*/ |
| 77 |
public static function verifyNonce( $nonce, $action = '' ) { |
| 78 |
return wp_verify_nonce( $nonce, add_query_arg( self::_getSalt( $action ), $_SERVER['HTTP_REFERER'] ) ); |
| 79 |
} |
| 80 |
|
| 81 |
} |