1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
<?php
icms_loadLanguageFile('core', 'captcha');
class icms_form_elements_captcha_Object {
public $active = TRUE;
public $mode = "text";
public $config = array();
public $message = array();
public function __construct() {
$this->config = @include dirname(__FILE__) . "/config.php";
global $icmsConfigCaptcha;
$this->setMode($icmsConfigCaptcha['captcha_mode']);
}
static public function &instance() {
static $instance;
if (!isset($instance)) {
$instance = new self();
}
return $instance;
}
public function setConfig($name, $val) {
if ($name == "mode") {
$this->setMode($val);
} elseif (isset($this->$name)) {
$this->$name = $val;
} else {
$this->config[$name] = $val;
}
return TRUE;
}
public function setMode($mode = NULL) {
if (!empty($mode) && in_array($mode, array("text", "image"))) {
$this->mode = $mode;
if ($this->mode != "image") {
return;
}
}
if (!extension_loaded('gd')) {
$this->mode = "text";
} else {
$required_functions = array(
"imagecreatetruecolor", "imagecolorallocate", "imagefilledrectangle",
"imagejpeg", "imagedestroy", "imageftbbox"
);
foreach ($required_functions as $func) {
if (!function_exists($func)) {
$this->mode = "text";
break;
}
}
}
}
public function init(
$name = 'icmscaptcha', $skipmember = NULL, $num_chars = NULL,
$fontsize_min = NULL, $fontsize_max = NULL, $background_type = NULL,
$background_num = NULL)
{
global $icmsConfigCaptcha;
foreach (array_keys($this->config) as $key) {
if (isset(${$key}) && ${$key} !== NULL) {
$this->config[$key] = ${$key};
}
}
$this->config["name"] = $name;
$groups = is_object(icms::$user) ? icms::$user->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
if (array_intersect($groups, $icmsConfigCaptcha['captcha_skipmember']) && is_object(icms::$user)) {
$this->active = FALSE;
} elseif ($icmsConfigCaptcha['captcha_mode'] =='none') {
$this->active = FALSE;
}
}
public function verify($skipMember = NULL) {
global $icmsConfig, $icmsConfigCaptcha;
$sessionName = @$_SESSION['icms_form_elements_captcha_Object_name'];
$skipMember = ($skipMember === NULL) ? @$_SESSION['icms_form_elements_captcha_Object_skipmember'] : $skipMember;
$maxAttempts = (int) (@$_SESSION['icms_form_elements_captcha_Object_maxattempts']);
$is_valid = FALSE;
$groups = is_object(icms::$user) ? icms::$user->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
if (array_intersect($groups, $icmsConfigCaptcha['captcha_skipmember']) && is_object(icms::$user)) {
$is_valid = TRUE;
} elseif (!empty($maxAttempts) && $_SESSION['icms_form_elements_captcha_Object_attempt_'.$sessionName] > $maxAttempts) {
$this->message[] = ICMS_CAPTCHA_TOOMANYATTEMPTS;
} elseif (!empty($_SESSION['icms_form_elements_captcha_Object_sessioncode'])) {
$func = ($icmsConfigCaptcha['captcha_casesensitive']) ? "strcmp" : "strcasecmp";
$is_valid = ! $func(trim(@$_POST[$sessionName]), $_SESSION['icms_form_elements_captcha_Object_sessioncode']);
}
if (!empty($maxAttempts)) {
if (!$is_valid) {
$_SESSION['icms_form_elements_captcha_Object_attempt_'.$sessionName]++;
$this->message[] = ICMS_CAPTCHA_INVALID_CODE;
} else {
$_SESSION['icms_form_elements_captcha_Object_attempt_'.$sessionName] = NULL;
}
}
$this->destroyGarbage(TRUE);
return $is_valid;
}
public function getCaption() {
return defined("ICMS_CAPTCHA_CAPTION") ? constant("ICMS_CAPTCHA_CAPTION") : "";
}
public function getMessage() {
return implode("<br />", $this->message);
}
public function destroyGarbage($clearSession = FALSE) {
$class = "icms_form_elements_captcha_" . ucfirst($this->mode);
$captcha_handler = new $class();
if (method_exists($captcha_handler, "destroyGarbage")) {
$captcha_handler->loadConfig($this->config);
$captcha_handler->destroyGarbage();
}
if ($clearSession) {
$_SESSION['icms_form_elements_captcha_Object_name'] = NULL;
$_SESSION['icms_form_elements_captcha_Object_skipmember'] = NULL;
$_SESSION['icms_form_elements_captcha_Object_sessioncode'] = NULL;
$_SESSION['icms_form_elements_captcha_Object_maxattempts'] = NULL;
}
return TRUE;
}
public function render() {
global $icmsConfigCaptcha;
$form = "";
if (!$this->active || empty($this->config["name"])) {
return $form;
}
$_SESSION['icms_form_elements_captcha_Object_name'] = $this->config["name"];
$_SESSION['icms_form_elements_captcha_Object_skipmember'] = $icmsConfigCaptcha['captcha_skipmember'];
$maxAttempts = $icmsConfigCaptcha['captcha_maxattempt'];
$_SESSION['icms_form_elements_captcha_Object_maxattempts'] = $maxAttempts;
if (!empty($maxAttempts)) {
$_SESSION['icms_form_elements_captcha_Object_maxattempts_'. $_SESSION['icms_form_elements_captcha_Object_name']] = $maxAttempts;
}
if (!empty($maxAttempts) && @$_SESSION['icms_form_elements_captcha_Object_attempt_' . $this->config["name"]] > $maxAttempts) {
$form = ICMS_CAPTCHA_TOOMANYATTEMPTS;
} else {
$form = $this->loadForm();
}
return $form;
}
public function loadForm() {
$class = "icms_form_elements_captcha_" . ucfirst($this->mode);
$captcha_handler = new $class();
$captcha_handler->loadConfig($this->config);
$form = $captcha_handler->render();
return $form;
}
}