Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <?php
  2. /*
  3. * Name: FT-NONCE-LIB
  4. * Created By: Full Throttle Development, LLC (http://fullthrottledevelopment.com)
  5. * Created On: July 2009
  6. * Last Modified On: August 12, 2009
  7. * Last Modified By: Glenn Ansley (glenn@fullthrottledevelopment.com)
  8. * Version: 0.2
  9. */
  10.  
  11. /*
  12. Copyright 2009 Full Throttle Development, LLC
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 3 of the License, or
  17. (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27.  
  28. define( 'FT_NONCE_UNIQUE_KEY' , '' );
  29. define( 'FT_NONCE_DURATION' , 300 ); // 300 makes link or form good for 5 minutes from time of generation
  30. define( 'FT_NONCE_KEY' , '_nonce' );
  31.  
  32. // This method creates a key / value pair for a url string
  33. function ft_nonce_create_query_string( $action = '' , $user = '' ){
  34. return FT_NONCE_KEY."=".ft_nonce_create( $action , $user );
  35. }
  36.  
  37. // This method creates an nonce for a form field
  38. function ft_nonce_create_form_input( $action = '' , $user='' ){
  39. echo "<input type='hidden' name='".FT_NONCE_KEY."' value='".ft_nonce_create( $action . $user )."' />";
  40. }
  41.  
  42. // This method creates an nonce. It should be called by one of the previous two functions.
  43. function ft_nonce_create( $action = '' , $user='' ){
  44. return substr( ft_nonce_generate_hash( $action . $user ), -12, 10);
  45. }
  46.  
  47. // This method validates an nonce
  48. function ft_nonce_is_valid( $nonce , $action = '' , $user='' ){
  49. // Nonce generated 0-12 hours ago
  50. if ( substr(ft_nonce_generate_hash( $action . $user ), -12, 10) == $nonce ){
  51. return true;
  52. }
  53. return false;
  54. }
  55.  
  56. // This method generates the nonce timestamp
  57. function ft_nonce_generate_hash( $action='' , $user='' ){
  58. $i = ceil( time() / ( FT_NONCE_DURATION / 2 ) );
  59. return md5( $i . $action . $user . $action );
  60. }
  61.  
  62. if ( FT_NONCE_UNIQUE_KEY == '' ){ die( 'You must enter a unique key on line 2 of ft_nonce_lib.php to use this library.'); }
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement