1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * Captcha field.
14 *
15 * @since 2.5
16 */
17 class JFormFieldCaptcha extends JFormField
18 {
19 /**
20 * The field type.
21 *
22 * @var string
23 * @since 2.5
24 */
25 protected $type = 'Captcha';
26
27 /**
28 * Method to get certain otherwise inaccessible properties from the form field object.
29 *
30 * @param string $name The property name for which to the the value.
31 *
32 * @return mixed The property value or null.
33 *
34 * @since 3.2
35 */
36 public function __get($name)
37 {
38 switch ($name)
39 {
40 case 'plugin':
41 case 'namespace':
42 return $this->$name;
43 }
44
45 return parent::__get($name);
46 }
47
48 /**
49 * Method to set certain otherwise inaccessible properties of the form field object.
50 *
51 * @param string $name The property name for which to the the value.
52 * @param mixed $value The value of the property.
53 *
54 * @return void
55 *
56 * @since 3.2
57 */
58 public function __set($name, $value)
59 {
60 switch ($name)
61 {
62 case 'plugin':
63 case 'namespace':
64 $this->$name = (string) $value;
65 break;
66
67 default:
68 parent::__set($name, $value);
69 }
70 }
71
72 /**
73 * Method to attach a JForm object to the field.
74 *
75 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
76 * @param mixed $value The form field value to validate.
77 * @param string $group The field name group control value. This acts as an array container for the field.
78 * For example if the field has name="foo" and the group value is set to "bar" then the
79 * full field name would end up being "bar[foo]".
80 *
81 * @return boolean True on success.
82 *
83 * @since 2.5
84 */
85 public function setup(SimpleXMLElement $element, $value, $group = null)
86 {
87 $result = parent::setup($element, $value, $group);
88
89 $app = JFactory::getApplication();
90
91 $default = $app->get('captcha');
92
93 if ($app->isClient('site'))
94 {
95 $default = $app->getParams()->get('captcha', $default);
96 }
97
98 $plugin = $this->element['plugin'] ?
99 (string) $this->element['plugin'] :
100 $default;
101
102 $this->plugin = $plugin;
103
104 if ($plugin === 0 || $plugin === '0' || $plugin === '' || $plugin === null)
105 {
106 $this->hidden = true;
107 }
108 else
109 {
110 // Force field to be required. There's no reason to have a captcha if it is not required.
111 // Obs: Don't put required="required" in the xml file, you just need to have validate="captcha"
112 $this->required = true;
113
114 if (strpos($this->class, 'required') === false)
115 {
116 $this->class .= ' required';
117 }
118 }
119
120 $this->namespace = $this->element['namespace'] ? (string) $this->element['namespace'] : $this->form->getName();
121
122 return $result;
123 }
124
125 /**
126 * Method to get the field input.
127 *
128 * @return string The field input.
129 *
130 * @since 2.5
131 */
132 protected function getInput()
133 {
134 if ($this->hidden)
135 {
136 return '';
137 }
138 else
139 {
140 if (($captcha = JCaptcha::getInstance($this->plugin, array('namespace' => $this->namespace))) == null)
141 {
142 return '';
143 }
144 }
145
146 return $captcha->display($this->name, $this->id, $this->class);
147 }
148 }
149