SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Captcha.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. use Exception;
  17.  
  18. use Kohana;
  19.  
  20.  
  21. /**
  22.  * Means to generate and process CAPTCHAs of various implementations
  23.  */
  24. class Captcha
  25. {
  26. protected static $class;
  27.  
  28.  
  29. /**
  30.   * Loads the default captcha class from config
  31.   */
  32. public static function init()
  33. {
  34. $class = Kohana::config('sprout.captcha');
  35. if (!$class) $class = 'default_captcha';
  36. self::useClass($class);
  37. }
  38.  
  39.  
  40. /**
  41.   * Sets which library to use for CAPTCHA implementation
  42.   * @param string $class_name e.g. DefaultCaptcha or Recaptcha
  43.   */
  44. public static function useClass($class_name)
  45. {
  46. $class_name = (string) $class_name;
  47.  
  48. if (strpos($class_name, '\\') === false) {
  49. $class_name = 'Sprout\\Helpers\\' . $class_name;
  50. }
  51.  
  52. if (!class_exists($class_name)) throw new Exception('Invalid class');
  53. self::$class = $class_name;
  54. }
  55.  
  56.  
  57. /**
  58.   * Shows a captcha field
  59.   * @return void Calls echo directly
  60.   **/
  61. public static function field()
  62. {
  63. if (self::$class == '') self::init();
  64. $class = self::$class;
  65. $class::field();
  66. }
  67.  
  68. /**
  69.   * Checks the captcha field against the submitted text
  70.   * @return bool
  71.   **/
  72. public static function check()
  73. {
  74. if (self::$class == '') self::init();
  75. $class = self::$class;
  76. return $class::check();
  77. }
  78.  
  79. }
  80.  
  81.  
  82.