PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/cms/captcha/captcha.php

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