CaptchaLib.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /**
  23. * @param array $data:
  24. * - captcha_time, result/captcha
  25. * @param array $options:
  26. * - salt (required)
  27. * - checkSession, checkIp, hashType (all optional)
  28. * 2011-06-11 ms
  29. */
  30. public static function buildHash($data, $options, $init = false) {
  31. if ($init) {
  32. $data['captcha_time'] = time();
  33. $data['captcha'] = $data['result'];
  34. }
  35. $hashValue = date(FORMAT_DB_DATETIME, (int)$data['captcha_time']).'_';
  36. $hashValue .= ($options['checkSession'])?session_id().'_' : '';
  37. $hashValue .= ($options['checkIp'])?env('REMOTE_ADDR').'_' : '';
  38. if (empty($options['type']) || $options['type'] !== 'passive') {
  39. $hashValue .= $data['captcha'];
  40. }
  41. return Security::hash($hashValue, isset($options['hashType']) ? $options['hashType'] : null, $options['salt']);
  42. }
  43. }