Caldera_Forms_Entry_Token

Entry token abstraction -- used for allowing cf_ee edits/ entry editor edits.

Defined (1)

The class is defined in the following location(s).

/classes/entry/token.php  
  1. class Caldera_Forms_Entry_Token { 
  2. /** 
  3. * Check a token 
  4. * @since 1.5.0.6 
  5. * @param string $test_token Encoded token to test 
  6. * @param string $entry_id ID of entry to test 
  7. * @param string $form_id ID of form to test 
  8. * @return bool|WP_Error 
  9. */ 
  10. public static function verify_token( $test_token, $entry_id, $form_id ) { 
  11. $compare_token = self::make_test_token( $entry_id, $form_id ); 
  12. if( ! hash_equals( $compare_token, $test_token ) ) { 
  13. return new WP_Error( 'error', __( "Permission denied.", 'caldera-forms' ) ); 
  14.  
  15.  
  16. return true; 
  17.  
  18.  
  19. /** 
  20. * Create entry edit token for stored entry 
  21. * @since 1.5.0.6 
  22. * @param string $entry_id ID of entry 
  23. * @param array $form Form config 
  24. * @return string 
  25. */ 
  26. public static function create_entry_token( $entry_id, $form ) { 
  27. $details = Caldera_Forms::get_entry_detail( $entry_id, $form ); 
  28. return self::make_token( $entry_id, $form[ 'ID' ], $details[ 'datestamp' ], $details[ 'user_id' ] ); 
  29.  
  30. /** 
  31. * Check a token vs a what it should be. 
  32. * @since 1.5.0.6 
  33. * @param string $test_token Encoded token to test 
  34. * @param string $compare_token Token to compare against 
  35. * @return bool|WP_Error 
  36. */ 
  37. protected static function check_token( $test_token, $compare_token ) { 
  38. if( ! hash_equals( $compare_token, $test_token ) ) { 
  39. return new WP_Error( 'error', __( "Permission denied.", 'caldera-forms' ) ); 
  40.  
  41. return true; 
  42.  
  43. /** 
  44. * Make token to test against 
  45. * Uses current user, not saved user 
  46. * @since 1.5.0.6 
  47. * @param int $entry_id ID of entry 
  48. * @param array $form Form config 
  49. * @return string 
  50. */ 
  51. protected static function make_test_token( $entry_id, $form ) { 
  52. $details = Caldera_Forms::get_entry_detail( $entry_id, $form ); 
  53. return self::make_token( $entry_id, $form[ 'ID' ], $details[ 'datestamp' ], get_current_user_id() ); 
  54.  
  55.  
  56. /** 
  57. * Make entry token 
  58. * @since 1.5.0.6 
  59. * @param int $entry_id ID of entry ID of stored entry 
  60. * @param string $form_id Form ID 
  61. * @param string $datestamp Datestamp of entry 
  62. * @param int $user_id ID of user who created entry 
  63. * @return string 
  64. */ 
  65. protected static function make_token( $entry_id, $form_id, $datestamp, $user_id ) { 
  66. $token_array = array( 
  67. 'id' => (int) $entry_id,  
  68. 'datestamp' => $datestamp,  
  69. 'user_id' => (int) $user_id,  
  70. 'form_id' => $form_id 
  71. ); 
  72.  
  73. return sha1( json_encode( $token_array ) ); 
  74.