SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/DefaultCaptcha.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. /**
  17. * Default Sprout CAPTCHA; doesn't need to talk to external servers
  18. **/
  19. class DefaultCaptcha
  20. {
  21.  
  22. /**
  23.   * Shows a captcha field
  24.   **/
  25. public static function field()
  26. {
  27. Session::instance();
  28.  
  29.  
  30. $num = mt_rand(0,99999);
  31.  
  32. echo '<div class="captcha">';
  33. echo ' <div class="info">';
  34. echo ' <a href="javascript:;" onclick="$(\'img.captcha\').attr(\'src\', \'SITE/captcha/image/' . $num . '?x=\' + new Date().getTime());">';
  35. echo ' <img src="ROOT/media/images/icons-16x16/refresh.png" alt="Refresh" title="Refresh" width="16" height="16">';
  36. echo ' </a>';
  37. echo ' <a href="SITE/captcha/about" rel="facebox">';
  38. echo ' <img src="ROOT/media/images/icons-16x16/help.png" alt="Help" title="Help" width="16" height="16">';
  39. echo ' </a>';
  40. echo ' </div>';
  41. echo ' <img src="SITE/captcha/image/' . $num . '" alt="" width="200" height="50" class="captcha">';
  42. echo ' <input type="text" name="captcha_text" class="captcha" autocomplete="off">';
  43. echo ' <input type="hidden" name="captcha_num" value="' . $num . '">';
  44. echo '</div>';
  45. }
  46.  
  47. /**
  48.   * Checks the captcha field against the submitted text
  49.   **/
  50. public static function check()
  51. {
  52. Session::instance();
  53.  
  54. if (empty($_SESSION['captcha'])) return false;
  55.  
  56. $_POST['captcha_num'] = (int) $_POST['captcha_num'];
  57.  
  58. if ($_POST['captcha_num'] == 0) return false;
  59. if (! isset($_SESSION['captcha'][$_POST['captcha_num']])) return false;
  60.  
  61. $exp = strtolower($_SESSION['captcha'][$_POST['captcha_num']]);
  62. $prov = strtolower($_POST['captcha_text']);
  63.  
  64. unset ($_SESSION['captcha'][$_POST['captcha_num']]);
  65.  
  66. if ($exp != $prov) {
  67. return false;
  68. }
  69.  
  70. return true;
  71. }
  72.  
  73. }
  74.  
  75.  
  76.