Source for file captcha.php

Documentation is available at captcha.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Captcha
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Joomla! Captcha base object
  14.  *
  15.  * @abstract
  16.  * @package     Joomla.Libraries
  17.  * @subpackage  Captcha
  18.  * @since       2.5
  19.  */
  20. class JCaptcha extends JObject
  21. {
  22.     /**
  23.      * An array of Observer objects to notify
  24.      *
  25.      * @var    array 
  26.      * @since  2.5
  27.      */
  28.     protected $_observers = array();
  29.  
  30.     /**
  31.      * The state of the observable object
  32.      *
  33.      * @var    mixed 
  34.      * @since  2.5
  35.      */
  36.     protected $_state = null;
  37.  
  38.     /**
  39.      * A multi dimensional array of [function][] = key for observers
  40.      *
  41.      * @var    array 
  42.      * @since  2.5
  43.      */
  44.     protected $_methods = array();
  45.  
  46.     /**
  47.      * Captcha Plugin object
  48.      *
  49.      * @var       JPlugin 
  50.      * @since  2.5
  51.      */
  52.     private $_captcha;
  53.  
  54.     /**
  55.      * Editor Plugin name
  56.      *
  57.      * @var string 
  58.      * @since  2.5
  59.      */
  60.     private $_name;
  61.  
  62.     /**
  63.      * Array of instances of this class.
  64.      *
  65.      * @var    array 
  66.      */
  67.     private static $_instances array();
  68.  
  69.     /**
  70.      * Class constructor.
  71.      *
  72.      * @param   string  $captcha  The editor to use.
  73.      * @param   array   $options  Associative array of options.
  74.      *
  75.      * @since 2.5
  76.      */
  77.     public function __construct($captcha$options)
  78.     {
  79.         $this->_name $captcha;
  80.         $this->_load($options);
  81.     }
  82.  
  83.     /**
  84.      * Returns the global Captcha object, only creating it
  85.      * if it doesn't already exist.
  86.      *
  87.      * @param   string  $captcha  The plugin to use.
  88.      * @param   array   $options  Associative array of options.
  89.      *
  90.      * @return  JCaptcha  Instance of this class.
  91.      *
  92.      * @since 2.5
  93.      */
  94.     public static function getInstance($captchaarray $options array())
  95.     {
  96.         $signature md5(serialize(array($captcha$options)));
  97.  
  98.         if (empty(self::$_instances[$signature]))
  99.         {
  100.             try
  101.             {
  102.                 self::$_instances[$signaturenew JCaptcha($captcha$options);
  103.             }
  104.             catch (RuntimeException $e)
  105.             {
  106.                 JFactory::getApplication()->enqueueMessage($e->getMessage()'error');
  107.                 return null;
  108.             }
  109.         }
  110.  
  111.         return self::$_instances[$signature];
  112.     }
  113.  
  114.     /**
  115.      * Fire the onInit event to initialise the captcha plug-in.
  116.      *
  117.      * @param   string  $id  The id of the field.
  118.      *
  119.      * @return  boolean  True on success
  120.      *
  121.      * @since    2.5
  122.      */
  123.     public function initialise($id)
  124.     {
  125.         $args['id']    $id;
  126.         $args['event''onInit';
  127.  
  128.         try
  129.         {
  130.             $this->_captcha->update($args);
  131.         }
  132.         catch (Exception $e)
  133.         {
  134.             JFactory::getApplication()->enqueueMessage($e->getMessage()'error');
  135.             return false;
  136.         }
  137.  
  138.         return true;
  139.     }
  140.  
  141.     /**
  142.      * Get the HTML for the captcha.
  143.      *
  144.      * @param   string  $name   The control name.
  145.      * @param   string  $id     The id for the control.
  146.      * @param   string  $class  Value for the HTML class attribute
  147.      *
  148.      * @return  mixed  The return value of the function "onDisplay" of the selected Plugin.
  149.      *
  150.      * @since   2.5
  151.      */
  152.     public function display($name$id$class '')
  153.     {
  154.         // Check if captcha is already loaded.
  155.         if (is_null($this->_captcha))
  156.         {
  157.             return;
  158.         }
  159.  
  160.         // Initialise the Captcha.
  161.         if (!$this->initialise($id))
  162.         {
  163.             return;
  164.         }
  165.  
  166.         $args['name']  $name;
  167.         $args['id']    $id $id $name;
  168.         $args['class'$class 'class="' $class '"' '';
  169.         $args['event''onDisplay';
  170.  
  171.         return $this->_captcha->update($args);
  172.     }
  173.  
  174.     /**
  175.      * Checks if the answer is correct.
  176.      *
  177.      * @param   string  $code  The answer.
  178.      *
  179.      * @return  mixed   The return value of the function "onCheckAnswer" of the selected Plugin.
  180.      *
  181.      * @since    2.5
  182.      */
  183.     public function checkAnswer($code)
  184.     {
  185.         // Check if captcha is already loaded
  186.         if (is_null(($this->_captcha)))
  187.         {
  188.             return;
  189.         }
  190.  
  191.         $args['code']  $code;
  192.         $args['event''onCheckAnswer';
  193.  
  194.         return $this->_captcha->update($args);
  195.     }
  196.  
  197.     /**
  198.      * Load the Captcha plug-in.
  199.      *
  200.      * @param   array  $options  Associative array of options.
  201.      *
  202.      * @return  void 
  203.      *
  204.      * @since    2.5
  205.      * @throws  RuntimeException
  206.      */
  207.     private function _load(array $options array())
  208.     {
  209.         // Build the path to the needed captcha plugin
  210.         $name JFilterInput::getInstance()->clean($this->_name'cmd');
  211.         $path JPATH_PLUGINS '/captcha/' $name '/' $name '.php';
  212.  
  213.         if (!is_file($path))
  214.         {
  215.             throw new RuntimeException(JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND'$name));
  216.         }
  217.  
  218.         // Require plugin file
  219.         require_once $path;
  220.  
  221.         // Get the plugin
  222.         $plugin JPluginHelper::getPlugin('captcha'$this->_name);
  223.         if (!$plugin)
  224.         {
  225.             throw new RuntimeException(JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND'$name));
  226.         }
  227.         $params new JRegistry($plugin->params);
  228.         $plugin->params $params;
  229.  
  230.         // Build captcha plugin classname
  231.         $name 'plgCaptcha' $this->_name;
  232.         $this->_captcha new $name($this(array) $plugin$options);
  233.     }
  234.  
  235.     /**
  236.      * Get the state of the JEditor object
  237.      *
  238.      * @return  mixed  The state of the object.
  239.      *
  240.      * @since   2.5
  241.      */
  242.     public function getState()
  243.     {
  244.         return $this->_state;
  245.     }
  246.  
  247.     /**
  248.      * Attach an observer object
  249.      *
  250.      * @param   object  $observer  An observer object to attach
  251.      *
  252.      * @return  void 
  253.      *
  254.      * @since   2.5
  255.      */
  256.     public function attach($observer)
  257.     {
  258.         if (is_array($observer))
  259.         {
  260.             if (!isset($observer['handler']|| !isset($observer['event']|| !is_callable($observer['handler']))
  261.             {
  262.                 return;
  263.             }
  264.  
  265.             // Make sure we haven't already attached this array as an observer
  266.             foreach ($this->_observers as $check)
  267.             {
  268.                 if (is_array($check&& $check['event'== $observer['event'&& $check['handler'== $observer['handler'])
  269.                 {
  270.                     return;
  271.                 }
  272.             }
  273.  
  274.             $this->_observers[$observer;
  275.             end($this->_observers);
  276.             $methods array($observer['event']);
  277.         }
  278.         else
  279.         {
  280.             if (!($observer instanceof JEditor))
  281.             {
  282.                 return;
  283.             }
  284.  
  285.             // Make sure we haven't already attached this object as an observer
  286.             $class get_class($observer);
  287.  
  288.             foreach ($this->_observers as $check)
  289.             {
  290.                 if ($check instanceof $class)
  291.                 {
  292.                     return;
  293.                 }
  294.             }
  295.  
  296.             $this->_observers[$observer;
  297.             $methods array_diff(get_class_methods($observer)get_class_methods('JPlugin'));
  298.         }
  299.  
  300.         $key key($this->_observers);
  301.  
  302.         foreach ($methods as $method)
  303.         {
  304.             $method strtolower($method);
  305.  
  306.             if (!isset($this->_methods[$method]))
  307.             {
  308.                 $this->_methods[$methodarray();
  309.             }
  310.  
  311.             $this->_methods[$method][$key;
  312.         }
  313.     }
  314.  
  315.     /**
  316.      * Detach an observer object
  317.      *
  318.      * @param   object  $observer  An observer object to detach.
  319.      *
  320.      * @return  boolean  True if the observer object was detached.
  321.      *
  322.      * @since   2.5
  323.      */
  324.     public function detach($observer)
  325.     {
  326.         $retval false;
  327.  
  328.         $key array_search($observer$this->_observers);
  329.  
  330.         if ($key !== false)
  331.         {
  332.             unset($this->_observers[$key]);
  333.             $retval true;
  334.  
  335.             foreach ($this->_methods as &$method)
  336.             {
  337.                 $k array_search($key$method);
  338.  
  339.                 if ($k !== false)
  340.                 {
  341.                     unset($method[$k]);
  342.                 }
  343.             }
  344.         }
  345.  
  346.         return $retval;
  347.     }
  348. }

Documentation generated on Tue, 19 Nov 2013 14:55:03 +0100 by phpDocumentor 1.4.3