XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
xoopscaptcha.php
Go to the documentation of this file.
1 <?php
22 defined('XOOPS_ROOT_PATH') or die('Restricted access');
23 
25 {
26  // static $instance;
27  var $active;
28  var $handler;
31  var $name;
32  var $config = array();
33  var $message = array(); // Logging error messages
34 
38  function __construct()
39  {
40  xoops_loadLanguage('captcha');
41  // Load static configurations
42  $this->path_basic = XOOPS_ROOT_PATH . '/class/captcha';
43  $this->path_plugin = XOOPS_ROOT_PATH . '/Frameworks/captcha';
44  $this->config = $this->loadConfig();
45  $this->name = $this->config['name'];
46  }
47 
53  function XoopsCaptcha()
54  {
55  $this->__construct();
56  }
57 
63  function &getInstance()
64  {
65  static $instance;
66  if (!isset($instance)) {
67  $class = __CLASS__;
68  $instance = new $class();
69  }
70  return $instance;
71  }
72 
79  function loadConfig($filename = null)
80  {
81  $basic_config = array();
82  $plugin_config = array();
83  $filename = empty($filename) ? 'config.php' : 'config.' . $filename . '.php';
84  if (file_exists($file = $this->path_basic . '/' . $filename)) {
85  $basic_config = include $file;
86  }
87  if (file_exists($file = $this->path_plugin . '/' . $filename)) {
88  $plugin_config = include $file;
89  }
90 
91  $config = array_merge($basic_config, $plugin_config);
92  foreach ($config as $key => $val) {
93  $config[$key] = $val;
94  }
95  return $config;
96  }
97 
103  function isActive()
104  {
105  if (isset($this->active)) {
106  return $this->active;
107  }
108  if (!empty($this->config['disabled'])) {
109  $this->active = false;
110  return $this->active;
111  }
112  if (!empty($this->config['skipmember']) && is_object($GLOBALS['xoopsUser'])) {
113  $this->active = false;
114  return $this->active;
115  }
116  if (!isset($this->handler)) {
117  $this->loadHandler();
118  }
119  $this->active = isset($this->handler);
120  return $this->active;
121  }
122 
129  function loadHandler($name = null)
130  {
131  $name = !empty($name) ? $name : (empty($this->config['mode']) ? 'text' : $this->config['mode']);
132  $class = 'XoopsCaptcha' . ucfirst($name);
133  if (!empty($this->handler) && get_class($this->handler) == $class) {
134  return $this->handler;
135  }
136  $this->handler = null;
137  if (file_exists($file = $this->path_basic . '/' . $name . '.php')) {
138  require_once $file;
139  } else {
140  if (file_exists($file = $this->path_plugin . '/' . $name . '.php')) {
141  require_once $file;
142  }
143  }
144 
145  if (!class_exists($class)) {
146  $class = 'XoopsCaptchaText';
147  require_once $this->path_basic . '/text.php';
148  }
149  $handler = new $class($this);
150  if ($handler->isActive()) {
151  $this->handler = $handler;
152  $this->handler->loadConfig($name);
153  }
154  return $this->handler;
155  }
156 
163  function setConfigs($configs)
164  {
165  foreach ($configs as $key => $val) {
166  $this->setConfig($key, $val);
167  }
168  return true;
169  }
170 
178  function setConfig($name, $val)
179  {
180  if (isset($this->$name)) {
181  $this->$name = $val;
182  } else {
183  $this->config[$name] = $val;
184  }
185  return true;
186  }
187 
198  function verify($skipMember = null, $name = null)
199  {
200  $sessionName = empty($name) ? $this->name : $name;
201  $skipMember = ($skipMember === null) ? $_SESSION["{$sessionName}_skipmember"] : $skipMember;
202  $maxAttempts = $_SESSION["{$sessionName}_maxattempts"];
203  $attempt = $_SESSION["{$sessionName}_attempt"];
204  $is_valid = false;
205  // Skip CAPTCHA verification if disabled
206  if (!$this->isActive()) {
207  $is_valid = true;
208  // Skip CAPTCHA for member if set
209  } else if (is_object($GLOBALS['xoopsUser']) && ! empty($skipMember)) {
210  $is_valid = true;
211  // Kill too many attempts
212  } else if (!empty($maxAttempts) && $attempt > $maxAttempts) {
213  $this->message[] = _CAPTCHA_TOOMANYATTEMPTS;
214  // Verify the code
215  } else {
216  $is_valid = $this->handler->verify($sessionName);
217  }
218 
219  if (!$is_valid) {
220  // Increase the attempt records on failure
221  $_SESSION["{$sessionName}_attempt"]++;
222  // Log the error message
223  $this->message[] = _CAPTCHA_INVALID_CODE;
224  } else {
225  // reset attempt records on success
226  $_SESSION["{$sessionName}_attempt"] = null;
227  }
228  $this->destroyGarbage(true);
229  return $is_valid;
230  }
231 
237  function getCaption()
238  {
239  return defined('_CAPTCHA_CAPTION') ? constant('_CAPTCHA_CAPTION') : '';
240  }
241 
247  function getMessage()
248  {
249  return implode('<br />', $this->message);
250  }
251 
255  function destroyGarbage($clearSession = false)
256  {
257  $this->loadHandler();
258  if (is_callable($this->handler, 'destroyGarbage')) {
259  $this->handler->destroyGarbage();
260  }
261  if ($clearSession) {
262  $_SESSION[$this->name . '_name'] = null;
263  $_SESSION[$this->name . '_skipmember'] = null;
264  $_SESSION[$this->name . '_code'] = null;
265  $_SESSION[$this->name . '_maxattempts'] = null;
266  }
267 
268  return true;
269  }
270 
276  function render()
277  {
278  $_SESSION[$this->name . '_name'] = $this->name;
279  $_SESSION[$this->name . '_skipmember'] = $this->config['skipmember'];
280  $form = '';
281  if (!$this->active || empty($this->config['name'])) {
282  return $form;
283  }
284 
285  $maxAttempts = $this->config['maxattempts'];
286  $_SESSION[$this->name . '_maxattempts'] = $maxAttempts;
287  $attempt = isset($_SESSION[$this->name . '_attempt']) ? $_SESSION[$this->name . '_attempt'] : 0;
288  $_SESSION[$this->name . '_attempt'] = $attempt;
289 
290  // Failure on too many attempts
291  if (!empty($maxAttempts) && $attempt > $maxAttempts) {
293  // Load the form element
294  } else {
295  $form = $this->loadForm();
296  }
297  return $form;
298  }
299 
305  function renderValidationJS()
306  {
307  if (!$this->active || empty($this->config['name'])) {
308  return '';
309  }
310  return $this->handler->renderValidationJS();
311  }
312 
319  function setCode($code = null)
320  {
321  $code = ($code === null) ? $this->handler->getCode() : $code;
322  if (!empty($code)) {
323  $_SESSION[$this->name . '_code'] = $code;
324  return true;
325  }
326  return false;
327  }
328 
334  function loadForm()
335  {
336  $form = $this->handler->render();
337  $this->setCode();
338  return $form;
339  }
340 }
341 
352 {
353  var $handler;
354  var $config;
355  var $code;
356 
362  function __construct($handler = null)
363  {
364  $this->handler = $handler;
365  }
366 
372  function XoopsCaptchaMethod($handler = null)
373  {
374  $this->__construct($handler);
375  }
376 
382  function isActive()
383  {
384  return true;
385  }
386 
393  function loadConfig($name = '')
394  {
395  $this->config = empty($name) ? $this->handler->config : array_merge($this->handler->config, $this->handler->loadConfig($name));
396  }
397 
403  function getCode()
404  {
405  return strval($this->code);
406  }
407 
413  function render()
414  {
415  }
416 
418  {
419  return '';
420  }
421 
428  function verify($sessionName = null)
429  {
430  $is_valid = false;
431  if (!empty($_SESSION["{$sessionName}_code"])) {
432  $func = !empty($this->config['casesensitive']) ? 'strcmp' : 'strcasecmp';
433  $is_valid = !$func(trim(@$_POST[$sessionName]), $_SESSION["{$sessionName}_code"]);
434  }
435  return $is_valid;
436  }
437 
438 }
439 
440 ?>