22 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
33 var $message = array();
38 function __construct()
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'];
53 function XoopsCaptcha()
63 function &getInstance()
66 if (!isset($instance)) {
68 $instance =
new $class();
79 function loadConfig($filename = null)
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;
87 if (file_exists(
$file = $this->path_plugin .
'/' . $filename)) {
88 $plugin_config = include
$file;
91 $config = array_merge($basic_config, $plugin_config);
92 foreach ($config as $key => $val) {
105 if (isset($this->active)) {
106 return $this->active;
108 if (!empty($this->config[
'disabled'])) {
109 $this->active =
false;
110 return $this->active;
112 if (!empty($this->config[
'skipmember']) && is_object(
$GLOBALS[
'xoopsUser'])) {
113 $this->active =
false;
114 return $this->active;
116 if (!isset($this->handler)) {
117 $this->loadHandler();
119 $this->active = isset($this->handler);
120 return $this->active;
129 function loadHandler($name = null)
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) {
136 $this->handler = null;
137 if (file_exists(
$file = $this->path_basic .
'/' . $name .
'.php')) {
140 if (file_exists(
$file = $this->path_plugin .
'/' . $name .
'.php')) {
145 if (!class_exists($class)) {
146 $class =
'XoopsCaptchaText';
147 require_once $this->path_basic .
'/text.php';
149 $handler =
new $class($this);
150 if ($handler->isActive()) {
152 $this->handler->loadConfig($name);
165 foreach (
$configs as $key => $val) {
166 $this->setConfig($key, $val);
178 function setConfig($name, $val)
180 if (isset($this->$name)) {
183 $this->config[$name] = $val;
198 function verify($skipMember = null, $name = null)
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"];
206 if (!$this->isActive()) {
209 }
else if (is_object(
$GLOBALS[
'xoopsUser']) && ! empty($skipMember)) {
212 }
else if (!empty($maxAttempts) && $attempt > $maxAttempts) {
216 $is_valid = $this->handler->verify($sessionName);
226 $_SESSION[
"{$sessionName}_attempt"] = null;
228 $this->destroyGarbage(
true);
237 function getCaption()
239 return defined(
'_CAPTCHA_CAPTION') ? constant(
'_CAPTCHA_CAPTION') :
'';
247 function getMessage()
249 return implode(
'<br />', $this->message);
255 function destroyGarbage($clearSession =
false)
257 $this->loadHandler();
258 if (is_callable($this->handler,
'destroyGarbage')) {
259 $this->handler->destroyGarbage();
263 $_SESSION[$this->name .
'_skipmember'] = null;
265 $_SESSION[$this->name .
'_maxattempts'] = null;
278 $_SESSION[$this->name .
'_name'] = $this->name;
279 $_SESSION[$this->name .
'_skipmember'] = $this->config[
'skipmember'];
281 if (!$this->active || empty($this->config[
'name'])) {
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;
291 if (!empty($maxAttempts) && $attempt > $maxAttempts) {
295 $form = $this->loadForm();
305 function renderValidationJS()
307 if (!$this->active || empty($this->config[
'name'])) {
310 return $this->handler->renderValidationJS();
319 function setCode($code = null)
321 $code = ($code === null) ? $this->handler->getCode() : $code;
323 $_SESSION[$this->name .
'_code'] = $code;
336 $form = $this->handler->render();
395 $this->config = empty($name) ? $this->handler->config : array_merge($this->handler->config, $this->handler->loadConfig($name));
405 return strval($this->code);
431 if (!empty(
$_SESSION[
"{$sessionName}_code"])) {
432 $func = !empty($this->config[
'casesensitive']) ?
'strcmp' :
'strcasecmp';
433 $is_valid = !$func(trim(@
$_POST[$sessionName]),
$_SESSION[
"{$sessionName}_code"]);