CaptchaLib.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. App::uses('Security', 'Utility');
  3. if (!defined('FORMAT_DB_DATE')) {
  4. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s');
  5. }
  6. /**
  7. * used by captcha helper and behavior
  8. */
  9. class CaptchaLib {
  10. public static $defaults = array (
  11. 'dummyField' => 'homepage',
  12. 'method' => 'hash',
  13. 'type' => 'both',
  14. 'checkSession' => false,
  15. 'checkIp' => false,
  16. 'salt' => '',
  17. );
  18. # what type of captcha
  19. public static $types = array('passive', 'active', 'both');
  20. # what method to use
  21. public static $methods = array('hash', 'db', 'session');
  22. public function __construct() {
  23. }
  24. /**
  25. * @param array $data:
  26. * - captcha_time, result/captcha
  27. * @param array $options:
  28. * - salt (required)
  29. * - checkSession, checkIp, hashType (all optional)
  30. * 2011-06-11 ms
  31. */
  32. public static function buildHash($data, $options, $init = false) {
  33. if ($init) {
  34. $data['captcha_time'] = time();
  35. $data['captcha'] = $data['result'];
  36. }
  37. $hashValue = date(FORMAT_DB_DATETIME, (int)$data['captcha_time']).'_';
  38. $hashValue .= ($options['checkSession'])?session_id().'_' : '';
  39. $hashValue .= ($options['checkIp'])?env('REMOTE_ADDR').'_' : '';
  40. $hashValue .= $data['captcha'];
  41. return Security::hash($hashValue, isset($options['hashType']) ? $options['hashType'] : null, $options['salt']);
  42. }
  43. }