| [ Index ] |
PHP Cross Reference of Xoops v2.4.5 code documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * CAPTCHA configurations for Image mode 4 * 5 * Based on DuGris' SecurityImage 6 * 7 * You may not change or alter any portion of this comment or credits 8 * of supporting developers from this source code or any supporting source code 9 * which is considered copyrighted (c) material of the original comment or credit authors. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 * 14 * @copyright The XOOPS project http://sourceforge.net/projects/xoops/ 15 * @license http://www.fsf.org/copyleft/gpl.html GNU public license 16 * @package class 17 * @subpackage CAPTCHA 18 * @since 2.3.0 19 * @author Taiwen Jiang <phppp@users.sourceforge.net> 20 * @version $Id: xoopscaptcha.php 4897 2010-06-19 02:55:48Z phppp $ 21 */ 22 defined('XOOPS_ROOT_PATH') or die('Restricted access'); 23 24 class XoopsCaptcha 25 { 26 // static $instance; 27 var $active; 28 var $handler; 29 var $path_basic; 30 var $path_plugin; 31 var $name; 32 var $config = array(); 33 var $message = array(); // Logging error messages 34 35 /** 36 * construct 37 */ 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 . '/class/captcha'; 44 $this->config = $this->loadConfig(); 45 $this->name = $this->config['name']; 46 } 47 48 /** 49 * Xoops Captcha Construct 50 * 51 * @return XoopsCaptcha 52 */ 53 function XoopsCaptcha() 54 { 55 $this->__construct(); 56 } 57 58 /** 59 * Get Instance 60 * 61 * @return Instance 62 */ 63 function &getInstance() 64 { 65 static $instance; 66 if (!isset($instance)) { 67 $class = __CLASS__; 68 $instance = new $class(); 69 } 70 return $instance; 71 } 72 73 /** 74 * XoopsCaptcha::loadConfig() 75 * 76 * @param mixed $filename 77 * @return 78 */ 79 function loadConfig($filename = null) 80 { 81 $config = false; 82 $filename = empty($filename) ? 'config.php' : 'config.' . $filename . '.php'; 83 if (file_exists($file = $this->path_basic . '/' . $filename)) { 84 $config = include $file; 85 if (file_exists($file = $this->path_plugin . '/' . $filename)) { 86 $config_plugin = include $file; 87 foreach ($config_plugin as $key => $val) { 88 $config[$key] = $val; 89 } 90 } 91 } 92 return $config; 93 } 94 95 /** 96 * XoopsCaptcha::isActive() 97 * 98 * @return 99 */ 100 function isActive() 101 { 102 if (isset($this->active)) { 103 return $this->active; 104 } 105 if (!empty($this->config['disabled'])) { 106 $this->active = false; 107 return $this->active; 108 } 109 if (!empty($this->config['skipmember']) && is_object($GLOBALS['xoopsUser'])) { 110 $this->active = false; 111 return $this->active; 112 } 113 if (!isset($this->handler)) { 114 $this->loadHandler(); 115 } 116 $this->active = isset($this->handler); 117 return $this->active; 118 } 119 120 /** 121 * XoopsCaptcha::loadHandler() 122 * 123 * @param mixed $name 124 * @return 125 */ 126 function loadHandler($name = null) 127 { 128 $name = !empty($name) ? $name : (empty($this->config['mode']) ? 'text' : $this->config['mode']); 129 $class = 'XoopsCaptcha' . ucfirst($name); 130 if (!empty($this->handler) && get_class($this->handler) == $class) { 131 return $this->handler; 132 } 133 $this->handler = null; 134 if (file_exists($file = $this->path_basic . '/' . $name . '.php')) { 135 include_once $file; 136 } else { 137 if (file_exists($file = $this->path_plugin . '/' . $name . '.php')) { 138 include_once $file; 139 } 140 } 141 if (!class_exists($class)) { 142 $class = 'text'; 143 require_once $this->path_basic . '/text.php'; 144 } 145 $handler = new $class($this); 146 if ($handler->isActive()) { 147 $this->handler = $handler; 148 } 149 return $this->handler; 150 } 151 152 /** 153 * XoopsCaptcha::setConfigs() 154 * 155 * @param mixed $configs 156 * @return 157 */ 158 function setConfigs($configs) 159 { 160 foreach ($configs as $key => $val) { 161 $this->setConfig($key, $val); 162 } 163 return true; 164 } 165 166 /** 167 * XoopsCaptcha::setConfig() 168 * 169 * @param mixed $name 170 * @param mixed $val 171 * @return 172 */ 173 function setConfig($name, $val) 174 { 175 if (isset($this->$name)) { 176 $this->$name = $val; 177 } else { 178 $this->config[$name] = $val; 179 } 180 return true; 181 } 182 183 /** 184 * Verify user submission 185 */ 186 /** 187 * XoopsCaptcha::verify() 188 * 189 * @param mixed $skipMember 190 * @param mixed $name 191 * @return 192 */ 193 function verify($skipMember = null, $name = null) 194 { 195 $sessionName = empty($name) ? $this->name : $name; 196 $skipMember = ($skipMember === null) ? @$_SESSION["{$sessionName}_skipmember"] : $skipMember; 197 $maxAttempts = intval(@$_SESSION["{$sessionName}_maxattempts"]); 198 $is_valid = false; 199 // Skip CAPTCHA verification if disabled 200 if (!$this->isActive()) { 201 $is_valid = true; 202 // Skip CAPTCHA for member if set 203 } else if (is_object($GLOBALS['xoopsUser']) && ! empty($skipMember)) { 204 $is_valid = true; 205 // Kill too many attempts 206 } else if (!empty($maxAttempts) && ! empty($_SESSION["{$sessionName}_attempt"]) > $maxAttempts) { 207 $this->message[] = _CAPTCHA_TOOMANYATTEMPTS; 208 // Verify the code 209 } else if (!empty($_SESSION["{$sessionName}_code"])) { 210 $func = !empty($this->config['casesensitive']) ? 'strcmp' : 'strcasecmp'; 211 $is_valid = !$func(trim(@$_POST[$sessionName]), $_SESSION["{$sessionName}_code"]); 212 } 213 // if(!empty($maxAttempts)) { 214 if (!$is_valid) { 215 // Increase the attempt records on failure 216 $_SESSION["{$sessionName}_attempt"] ++; 217 // Log the error message 218 $this->message[] = _CAPTCHA_INVALID_CODE; 219 } else { 220 // reset attempt records on success 221 $_SESSION["{$sessionName}_attempt"] = null; 222 } 223 $this->destroyGarbage(true); 224 return $is_valid; 225 } 226 227 /** 228 * XoopsCaptcha::getCaption() 229 * 230 * @return 231 */ 232 function getCaption() 233 { 234 return defined('_CAPTCHA_CAPTION') ? constant('_CAPTCHA_CAPTION') : ''; 235 } 236 237 /** 238 * XoopsCaptcha::getMessage() 239 * 240 * @return 241 */ 242 function getMessage() 243 { 244 return implode('<br />', $this->message); 245 } 246 247 /** 248 * Destory historical stuff 249 */ 250 function destroyGarbage($clearSession = false) 251 { 252 $this->loadHandler(); 253 if (is_callable($this->handler, 'destroyGarbage')) { 254 $this->handler->destroyGarbage(); 255 } 256 if ($clearSession) { 257 $_SESSION[$this->name . '_name'] = null; 258 $_SESSION[$this->name . '_skipmember'] = null; 259 $_SESSION[$this->name . '_code'] = null; 260 $_SESSION[$this->name . '_maxattempts'] = null; 261 } 262 263 return true; 264 } 265 266 /** 267 * XoopsCaptcha::render() 268 * 269 * @return 270 */ 271 function render() 272 { 273 $_SESSION[$this->name . '_name'] = $this->name; 274 $_SESSION[$this->name . '_skipmember'] = $this->config['skipmember']; 275 $form = ''; 276 if (!$this->active || empty($this->config['name'])) { 277 return $form; 278 } 279 $maxAttempts = $this->config['maxattempt']; 280 if (!empty($maxAttempts)) { 281 $_SESSION[$this->name . '_maxattempts'] = $maxAttempts; 282 } 283 // Failure on too many attempts 284 if (!empty($maxAttempts) && @$_SESSION[$this->name . '_attempt'] > $maxAttempts) { 285 $form = _CAPTCHA_TOOMANYATTEMPTS; 286 // Load the form element 287 } else { 288 $form = $this->loadForm(); 289 } 290 return $form; 291 } 292 293 /** 294 * XoopsCaptcha::setCode() 295 * 296 * @param mixed $code 297 * @return 298 */ 299 function setCode($code = null) 300 { 301 $code = ($code === null) ? $this->handler->getCode() : $code; 302 if (!empty($code)) { 303 $_SESSION[$this->name . '_code'] = $code; 304 return true; 305 } 306 return false; 307 } 308 309 /** 310 * XoopsCaptcha::loadForm() 311 * 312 * @return 313 */ 314 function loadForm() 315 { 316 $form = $this->handler->render(); 317 $this->setCode(); 318 return $form; 319 } 320 } 321 322 /** 323 * Abstract class for CAPTCHA method 324 * 325 * Currently there are two types of CAPTCHA forms, text and image 326 * The default mode is "text", it can be changed in the priority: 327 * 1 If mode is set through XoopsFormCaptcha::setConfig("mode", $mode), take it 328 * 2 Elseif mode is set though captcha/config.php, take it 329 * 3 Else, take "text" 330 */ 331 class XoopsCaptchaMethod 332 { 333 var $handler; 334 var $config; 335 var $code; 336 337 /** 338 * XoopsCaptchaMethod::__construct() 339 * 340 * @param mixed $handler 341 */ 342 function __construct($handler = null) 343 { 344 $this->handler = $handler; 345 } 346 347 /** 348 * XoopsCaptchaMethod::XoopsCaptchaMethod() 349 * 350 * @param mixed $handler 351 */ 352 function XoopsCaptchaMethod($handler = null) 353 { 354 $this->__construct($handler); 355 } 356 357 /** 358 * XoopsCaptchaMethod::isActive() 359 * 360 * @return 361 */ 362 function isActive() 363 { 364 return true; 365 } 366 367 /** 368 * XoopsCaptchaMethod::loadConfig() 369 * 370 * @param mixed $name 371 * @return 372 */ 373 function loadConfig($name) 374 { 375 $this->config = empty($name) ? $this->handler->config : array_merge($this->handler->config, $this->handler->loadConfig($name)); 376 } 377 378 /** 379 * XoopsCaptchaMethod::getCode() 380 * 381 * @return 382 */ 383 function getCode() 384 { 385 return strval($this->code); 386 } 387 388 /** 389 * XoopsCaptchaMethod::render() 390 * 391 * @return 392 */ 393 function render() 394 { 395 } 396 } 397 398 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Aug 1 01:39:09 2010 |