AAAAhome/academiac/www/libraries/cms/form/rule/captcha.php000064400000004136151372735360017013 0ustar00 tag for the form field object. * @param mixed $value The form field value to validate. * @param string $group The field name group control value. This acts as as an array container for the field. * For example if the field has name="foo" and the group value is set to "bar" then the * full field name would end up being "bar[foo]". * @param JRegistry &$input An optional JRegistry object with the entire data set to validate against the entire form. * @param JForm &$form The form object for which the field is being tested. * * @return boolean True if the value is valid, false otherwise. * * @since 2.5 */ public function test(&$element, $value, $group = null, &$input = null, &$form = null) { $plugin = $element['plugin'] ? (string) $element['plugin'] : JFactory::getApplication()->getParams()->get('captcha', JFactory::getConfig()->get('captcha', 0)); $namespace = $element['namespace'] ? (string) $element['namespace'] : $form->getName(); // Use 0 for none if ($plugin === 0 || $plugin === '0') { return true; } else { $captcha = JCaptcha::getInstance($plugin, array('namespace' => $namespace)); } // Test the value. if (!$captcha->checkAnswer($value)) { $error = $captcha->getError(); if ($error instanceof Exception) { return $error; } else { return new JException($error); } } return true; } } home/academiac/www/libraries/cms/form/field/captcha.php000064400000005405151372737540017131 0ustar00 tag for the form field object. * @param mixed $value The form field value to validate. * @param string $group The field name group control value. This acts as as an array container for the field. * For example if the field has name="foo" and the group value is set to "bar" then the * full field name would end up being "bar[foo]". * * @return boolean True on success. * * @since 2.5 */ public function setup(&$element, $value, $group = null) { $result = parent::setup($element, $value, $group); $plugin = $this->element['plugin'] ? (string) $this->element['plugin'] : JFactory::getApplication()->getParams()->get('captcha', JFactory::getConfig()->get('captcha')); if ($plugin === 0 || $plugin === '0' || $plugin === '' || $plugin === null) { $this->hidden = true; } else { // Force field to be required. There's no reason to have a captcha if it is not required. // Obs: Don't put required="required" in the xml file, you just need to have validate="captcha" $this->required = true; $class = $this->element['class']; if (strpos($class, 'required') === false) { $this->element['class'] = $class . ' required'; } } return $result; } /** * Method to get the field input. * * @return string The field input. * * @since 2.5 */ protected function getInput() { $class = $this->element['class'] ? (string) $this->element['class'] : ''; $plugin = $this->element['plugin'] ? (string) $this->element['plugin'] : JFactory::getApplication()->getParams()->get('captcha', JFactory::getConfig()->get('captcha')); $namespace = $this->element['namespace'] ? (string) $this->element['namespace'] : $this->form->getName(); // Use 0 for none if ($plugin === 0 || $plugin === '0' || $plugin === '' || $plugin === null) { return ''; } else { if (($captcha = JCaptcha::getInstance($plugin, array('namespace' => $namespace))) == null) { return ''; } } return $captcha->display($this->name, $this->id, $class); } } home/academiac/www/libraries/cms/captcha/captcha.php000064400000014676151372740360016512 0ustar00_name = $captcha; $this->_load($options); } /** * Returns the global Captcha object, only creating it * if it doesn't already exist. * * @param string $captcha The plugin to use. * @param array $options Associative array of options. * * @return object The JCaptcha object. * * @since 2.5 */ public static function getInstance($captcha, array $options = array()) { $signature = md5(serialize(array($captcha, $options))); if (empty(self::$_instances[$signature])) { try { self::$_instances[$signature] = new JCaptcha($captcha, $options); } catch (RuntimeException $e) { JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); return null; } } return self::$_instances[$signature]; } /** * @return boolean True on success * * @since 2.5 */ public function initialise($id) { $args['id'] = $id ; $args['event'] = 'onInit'; try { $this->_captcha->update($args); } catch (Exception $e) { JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); return false; } return true; } /** * Get the HTML for the captcha. * * @return the return value of the function "onDisplay" of the selected Plugin. * * @since 2.5 */ public function display($name, $id, $class = '') { // Check if captcha is already loaded. if (is_null($this->_captcha)) { return; } // Initialise the Captcha. if (!$this->initialise($id)) { return; } $args['name'] = $name; $args['id'] = $id ? $id : $name; $args['class'] = $class ? 'class="'.$class.'"' : ''; $args['event'] = 'onDisplay'; return $this->_captcha->update($args); } /** * Checks if the answer is correct. * * @return the return value of the function "onCheckAnswer" of the selected Plugin. * * @since 2.5 */ public function checkAnswer($code) { //check if captcha is already loaded if (is_null(($this->_captcha))) { return; } $args['code'] = $code; $args['event'] = 'onCheckAnswer'; return $this->_captcha->update($args); } /** * Load the Captcha plug-in. * * @param array $options Associative array of options. * * @return void * * @since 2.5 * @throws RuntimeException */ private function _load(array $options = array()) { // Build the path to the needed captcha plugin $name = JFilterInput::getInstance()->clean($this->_name, 'cmd'); $path = JPATH_PLUGINS . '/captcha/' . $name . '/' . $name . '.php'; if (!JFile::exists($path)) { throw new RuntimeException(JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name)); } // Require plugin file require_once $path; // Get the plugin $plugin = JPluginHelper::getPlugin('captcha', $this->_name); if (!$plugin) { throw new RuntimeException(JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name)); } $params = new JRegistry($plugin->params); $plugin->params = $params; // Build captcha plugin classname $name = 'plgCaptcha' . $this->_name; $this->_captcha = new $name($this, (array)$plugin, $options); } /** * Get the state of the JEditor object * * @return mixed The state of the object. * * @since 2.5 */ public function getState() { return $this->_state; } /** * Attach an observer object * * @param object $observer An observer object to attach * * @return void * * @since 2.5 */ public function attach($observer) { if (is_array($observer)) { if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler'])) { return; } // Make sure we haven't already attached this array as an observer foreach ($this->_observers as $check) { if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler']) { return; } } $this->_observers[] = $observer; end($this->_observers); $methods = array($observer['event']); } else { if (!($observer instanceof JEditor)) { return; } // Make sure we haven't already attached this object as an observer $class = get_class($observer); foreach ($this->_observers as $check) { if ($check instanceof $class) { return; } } $this->_observers[] = $observer; $methods = array_diff(get_class_methods($observer), get_class_methods('JPlugin')); } $key = key($this->_observers); foreach ($methods as $method) { $method = strtolower($method); if (!isset($this->_methods[$method])) { $this->_methods[$method] = array(); } $this->_methods[$method][] = $key; } } /** * Detach an observer object * * @param object $observer An observer object to detach. * * @return boolean True if the observer object was detached. * * @since 2.5 */ public function detach($observer) { // Initialise variables. $retval = false; $key = array_search($observer, $this->_observers); if ($key !== false) { unset($this->_observers[$key]); $retval = true; foreach ($this->_methods as &$method) { $k = array_search($key, $method); if ($k !== false) { unset($method[$k]); } } } return $retval; } }