From: <du...@us...> - 2012-10-12 00:38:15
|
Revision: 10214 http://sourceforge.net/p/xoops/svn/10214 Author: dugris Date: 2012-10-12 00:38:13 +0000 (Fri, 12 Oct 2012) Log Message: ----------- Read/Write config files in xoops_data/configs folder Modified Paths: -------------- XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php XoopsCore/branches/2.6.x/2.6.0/htdocs/class/xoopsform/formcaptcha.php Property Changed: ---------------- XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php Modified: XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php =================================================================== --- XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php 2012-10-12 00:32:32 UTC (rev 10213) +++ XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php 2012-10-12 00:38:13 UTC (rev 10214) @@ -1,482 +1,537 @@ -<?php -/* - You may not change or alter any portion of this comment or credits - of supporting developers from this source code or any supporting source code - which is considered copyrighted (c) material of the original comment or credit authors. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -*/ - -/** - * CAPTCHA configurations for Image mode - * - * Based on DuGris' SecurityImage - * - * @copyright The XOOPS project http://sourceforge.net/projects/xoops/ - * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) - * @package class - * @subpackage captcha - * @since 2.3.0 - * @author Taiwen Jiang <ph...@us...> - * @version $Id$ - */ - -defined('XOOPS_ROOT_PATH') or die('Restricted access'); - -class XoopsCaptcha -{ - /** - * @var bool - */ - public $active; - - /** - * @var XoopsCaptchaMethod - */ - public $handler; - - /** - * @var string - */ - public $path_basic; - - /** - * @var string - */ - public $path_plugin; - - /** - * @var string - */ - public $name; - - /** - * @var array - */ - public $config = array(); - - /** - * @var array - */ - public $message = array(); - - /** - * construct - */ - public function __construct() - { - $xoops = Xoops::getInstance(); - $xoops->loadLanguage('captcha'); - // Load static configurations - $this->path_basic = XOOPS_ROOT_PATH . '/class/captcha'; - $this->path_plugin = XOOPS_ROOT_PATH . '/Frameworks/captcha'; - $this->config = $this->loadConfig(); - $this->name = $this->config['name']; - } - - /** - * Get Instance - * - * @return XoopsCaptcha - */ - static function getInstance() - { - static $instance; - if (!isset($instance)) { - $class = __CLASS__; - $instance = new $class(); - } - return $instance; - } - - /** - * XoopsCaptcha::loadConfig() - * - * @param string $filename - * @return array - */ - public function loadConfig($filename = null) - { - $basic_config = array(); - $plugin_config = array(); - $filename = empty($filename) ? 'config.php' : 'config.' . $filename . '.php'; - if (file_exists($file = $this->path_basic . '/' . $filename)) { - $basic_config = include $file; - } - if (file_exists($file = $this->path_plugin . '/' . $filename)) { - $plugin_config = include $file; - } - - $config = array_merge($basic_config, $plugin_config); - foreach ($config as $key => $val) { - $config[$key] = $val; - } - return $config; - } - - /** - * XoopsCaptcha::isActive() - * - * @return bool - */ - public function isActive() - { - $xoops = Xoops::getInstance(); - - if (isset($this->active)) { - return $this->active; - } - if (!empty($this->config['disabled'])) { - $this->active = false; - return $this->active; - } - if (!empty($this->config['skipmember']) && $xoops->isUser()) { - $this->active = false; - return $this->active; - } - if (!isset($this->handler)) { - $this->loadHandler(); - } - $this->active = isset($this->handler); - return $this->active; - } - - /** - * XoopsCaptcha::loadHandler() - * - * @param string $name - * @return XoopsCatchaMethod - */ - public function loadHandler($name = null) - { - $name = !empty($name) ? $name : (empty($this->config['mode']) ? 'text' : $this->config['mode']); - $class = 'XoopsCaptcha' . ucfirst($name); - if (!empty($this->handler) && get_class($this->handler) == $class) { - return $this->handler; - } - $this->handler = null; - if (file_exists($file = $this->path_basic . '/' . $name . '.php')) { - require_once $file; - } else { - if (file_exists($file = $this->path_plugin . '/' . $name . '.php')) { - require_once $file; - } - } - - if (!class_exists($class)) { - $class = 'XoopsCaptchaText'; - require_once $this->path_basic . '/text.php'; - } - /* @var $handler XoopsCaptchaMethod */ - $handler = new $class($this); - if ($handler->isActive()) { - $this->handler = $handler; - $this->handler->loadConfig($name); - } - return $this->handler; - } - - /** - * XoopsCaptcha::setConfigs() - * - * @param mixed $configs - * @return bool - */ - public function setConfigs($configs) - { - foreach ($configs as $key => $val) { - $this->setConfig($key, $val); - } - return true; - } - - /** - * XoopsCaptcha::setConfig() - * - * @param mixed $name - * @param mixed $val - * @return bool - */ - public function setConfig($name, $val) - { - if (isset($this->$name)) { - $this->$name = $val; - } else { - $this->config[$name] = $val; - } - return true; - } - - /** - * XoopsCaptcha::verify() - * - * Verify user submission - * - * @param bool $skipMember - * @param string $name - * @return bool - */ - public function verify($skipMember = null, $name = null) - { - $xoops = Xoops::getInstance(); - $sessionName = empty($name) ? $this->name : $name; - $skipMember = ($skipMember === null) ? $_SESSION["{$sessionName}_skipmember"] : $skipMember; - $maxAttempts = $_SESSION["{$sessionName}_maxattempts"]; - $attempt = $_SESSION["{$sessionName}_attempt"]; - $is_valid = false; - // Skip CAPTCHA verification if disabled - if (!$this->isActive()) { - $is_valid = true; - // Skip CAPTCHA for member if set - } else { - if ($xoops->isUser() && !empty($skipMember)) { - $is_valid = true; - // Kill too many attempts - } else { - if (!empty($maxAttempts) && $attempt > $maxAttempts) { - $this->message[] = _CAPTCHA_TOOMANYATTEMPTS; - // Verify the code - } else { - $is_valid = $this->handler->verify($sessionName); - } - } - } - - if (!$is_valid) { - // Increase the attempt records on failure - $_SESSION["{$sessionName}_attempt"]++; - // Log the error message - $this->message[] = _CAPTCHA_INVALID_CODE; - } else { - // reset attempt records on success - $_SESSION["{$sessionName}_attempt"] = null; - } - $this->destroyGarbage(true); - return $is_valid; - } - - /** - * XoopsCaptcha::getCaption() - * - * @return string - */ - public function getCaption() - { - return defined('_CAPTCHA_CAPTION') ? constant('_CAPTCHA_CAPTION') : ''; - } - - /** - * XoopsCaptcha::getMessage() - * - * @return sdtring - */ - public function getMessage() - { - return implode('<br />', $this->message); - } - - /** - * Destory historical stuff - * - * @param bool $clearSession - * @return bool - */ - public function destroyGarbage($clearSession = false) - { - $this->loadHandler(); - $this->handler->destroyGarbage(); - - if ($clearSession) { - $_SESSION[$this->name . '_name'] = null; - $_SESSION[$this->name . '_skipmember'] = null; - $_SESSION[$this->name . '_code'] = null; - $_SESSION[$this->name . '_maxattempts'] = null; - } - - return true; - } - - /** - * XoopsCaptcha::render() - * - * @return string - */ - public function render() - { - $_SESSION[$this->name . '_name'] = $this->name; - $_SESSION[$this->name . '_skipmember'] = $this->config['skipmember']; - $form = ''; - if (!$this->active || empty($this->config['name'])) { - return $form; - } - - $maxAttempts = $this->config['maxattempts']; - $_SESSION[$this->name . '_maxattempts'] = $maxAttempts; - $attempt = isset($_SESSION[$this->name . '_attempt']) ? $_SESSION[$this->name . '_attempt'] : 0; - $_SESSION[$this->name . '_attempt'] = $attempt; - - // Failure on too many attempts - if (!empty($maxAttempts) && $attempt > $maxAttempts) { - $form = _CAPTCHA_TOOMANYATTEMPTS; - // Load the form element - } else { - $form = $this->loadForm(); - } - return $form; - } - - /** - * XoopsCaptcha::renderValidationJS() - * - * @return string - */ - public function renderValidationJS() - { - if (!$this->active || empty($this->config['name'])) { - return ''; - } - return $this->handler->renderValidationJS(); - } - - /** - * XoopsCaptcha::setCode() - * - * @param mixed $code - * @return bool - */ - public function setCode($code = null) - { - $code = ($code === null) ? $this->handler->getCode() : $code; - if (!empty($code)) { - $_SESSION[$this->name . '_code'] = $code; - return true; - } - return false; - } - - /** - * XoopsCaptcha::loadForm() - * - * @return string - */ - public function loadForm() - { - $form = $this->handler->render(); - $this->setCode(); - return $form; - } -} - -/** - * Abstract class for CAPTCHA method - * - * Currently there are two types of CAPTCHA forms, text and image - * The default mode is "text", it can be changed in the priority: - * 1 If mode is set through XoopsFormCaptcha::setConfig("mode", $mode), take it - * 2 Elseif mode is set though captcha/config.php, take it - * 3 Else, take "text" - */ -class XoopsCaptchaMethod -{ - /** - * @var XoopsCaptcha - */ - public $handler; - - /** - * @var array - */ - public $config; - - /** - * @var string - */ - public $code; - - /** - * XoopsCaptchaMethod::__construct() - * - * @param mixed $handler - */ - public function __construct($handler = null) - { - $this->handler = $handler; - } - - /** - * XoopsCaptchaMethod::isActive() - * - * @return bool - */ - public function isActive() - { - return true; - } - - /** - * XoopsCaptchaMethod::loadConfig() - * - * @param string $name - * @return array - */ - public function loadConfig($name = '') - { - $this->config = empty($name) ? $this->handler->config - : array_merge($this->handler->config, $this->handler->loadConfig($name)); - } - - /** - * XoopsCaptchaMethod::getCode() - * - * @return string - */ - public function getCode() - { - return strval($this->code); - } - - /** - * XoopsCaptchaMethod::render() - * - * @return string - */ - public function render() - { - return ''; - } - - /** - * @return string - */ - public function renderValidationJS() - { - return ''; - } - - /** - * XoopsCaptchaMethod::verify() - * - * @param mixed $sessionName - * @return bool - */ - public function verify($sessionName = null) - { - $is_valid = false; - if (!empty($_SESSION["{$sessionName}_code"])) { - $func = !empty($this->config['casesensitive']) ? 'strcmp' : 'strcasecmp'; - $is_valid = !$func(trim(@$_POST[$sessionName]), $_SESSION["{$sessionName}_code"]); - } - return $is_valid; - } - - /** - * @return bool - */ - public function destroyGarbage() - { - return true; - } - +<?php +/* + You may not change or alter any portion of this comment or credits + of supporting developers from this source code or any supporting source code + which is considered copyrighted (c) material of the original comment or credit authors. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +*/ + +/** + * CAPTCHA configurations for Image mode + * + * Based on DuGris' SecurityImage + * + * @copyright The XOOPS project http://sourceforge.net/projects/xoops/ + * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) + * @package class + * @subpackage captcha + * @since 2.3.0 + * @author Taiwen Jiang <ph...@us...> + * @version $Id$ + */ + +defined('XOOPS_ROOT_PATH') or die('Restricted access'); + +class XoopsCaptcha +{ + /** + * @var bool + */ + public $active; + + /** + * @var XoopsCaptchaMethod + */ + public $handler; + + /** + * @var string + */ + public $path_basic; + + /** + * @var string + */ + public $path_plugin; + + /** + * @var string + */ + public $configPath; + + /** + * @var string + */ + public $name; + + /** + * @var array + */ + public $config = array(); + + /** + * @var array + */ + public $message = array(); + + /** + * construct + */ + public function __construct() + { + $xoops = Xoops::getInstance(); + $xoops->loadLanguage('captcha'); + // Load static configurations + $this->path_basic = XOOPS_ROOT_PATH . '/class/captcha'; + $this->path_plugin = XOOPS_ROOT_PATH . '/Frameworks/captcha'; + $this->configPath = XOOPS_VAR_PATH . '/configs/'; + $this->config = $this->loadConfig(); + $this->name = $this->config['name']; + } + + /** + * Get Instance + * + * @return XoopsCaptcha + */ + static function getInstance() + { + static $instance; + if (!isset($instance)) { + $class = __CLASS__; + $instance = new $class(); + } + return $instance; + } + + /** + * XoopsCaptcha::loadConfig() + * + * @param string $filename + * @return array + */ + function loadConfig( $name = 'config') { + if ( $name == 'config' ) { + $filename = 'captcha.config'; + } else { + $filename = 'captcha.config.' . $name; + } + if ( !$config = $this->readConfig($filename) ) { + $config = $this->loadBasicConfig( $name ); + $this->writeConfig($filename, $config ); + } + return $config; + } + + /** + * XoopsCaptcha::loadBasicConfig() + * + * @param string $filename + * @return array + */ + function loadBasicConfig($filename = null) + { + $basic_config = array(); + $plugin_config = array(); + $filename = ($filename == 'config') ? 'config.php' : 'config.' . $filename . '.php'; + if (file_exists($file = $this->path_basic . '/' . $filename)) { + $basic_config = include $file; + } + if (file_exists($file = $this->path_plugin . '/' . $filename)) { + $plugin_config = include $file; + } + + $config = array_merge($basic_config, $plugin_config); + foreach ($config as $key => $val) { + $config[$key] = $val; + } + return $config; + } + + /** + * XoopsCaptcha::readConfig() + * + * @param string $filename + * @return array + */ + function readConfig( $filename = 'config') + { + $path_file = $this->configPath . $filename . '.php'; + XoopsLoad::load('XoopsFile'); + $file = XoopsFile::getHandler('file', $path_file); + return eval(@$file->read()); + } + + /** + * XoopsCaptcha::writeConfig() + * + * @param string $filename + * @param array $config + * @return array + */ + function writeConfig($filename = 'config', $config) + { + $path_file = $this->configPath . $filename . '.php'; + XoopsLoad::load('XoopsFile'); + $file = XoopsFile::getHandler('file', $path_file); + return $file->write( 'return ' . var_export($config, true) . ';'); + } + + /** + * XoopsCaptcha::isActive() + * + * @return bool + */ + public function isActive() + { + $xoops = Xoops::getInstance(); + + if (isset($this->active)) { + return $this->active; + } + if (!empty($this->config['disabled'])) { + $this->active = false; + return $this->active; + } + if (!empty($this->config['skipmember']) && $xoops->isUser()) { + $this->active = false; + return $this->active; + } + if (!isset($this->handler)) { + $this->loadHandler(); + } + $this->active = isset($this->handler); + return $this->active; + } + + /** + * XoopsCaptcha::loadHandler() + * + * @param string $name + * @return XoopsCatchaMethod + */ + public function loadHandler($name = null) + { + $name = !empty($name) ? $name : (empty($this->config['mode']) ? 'text' : $this->config['mode']); + $class = 'XoopsCaptcha' . ucfirst($name); + if (!empty($this->handler) && get_class($this->handler) == $class) { + return $this->handler; + } + $this->handler = null; + if (file_exists($file = $this->path_basic . '/' . $name . '.php')) { + require_once $file; + } else { + if (file_exists($file = $this->path_plugin . '/' . $name . '.php')) { + require_once $file; + } + } + + if (!class_exists($class)) { + $class = 'XoopsCaptchaText'; + require_once $this->path_basic . '/text.php'; + } + /* @var $handler XoopsCaptchaMethod */ + $handler = new $class($this); + if ($handler->isActive()) { + $this->handler = $handler; + $this->handler->loadConfig($name); + } + return $this->handler; + } + + /** + * XoopsCaptcha::setConfigs() + * + * @param mixed $configs + * @return bool + */ + public function setConfigs($configs) + { + foreach ($configs as $key => $val) { + $this->setConfig($key, $val); + } + return true; + } + + /** + * XoopsCaptcha::setConfig() + * + * @param mixed $name + * @param mixed $val + * @return bool + */ + public function setConfig($name, $val) + { + if (isset($this->$name)) { + $this->$name = $val; + } else { + $this->config[$name] = $val; + } + return true; + } + + /** + * XoopsCaptcha::verify() + * + * Verify user submission + * + * @param bool $skipMember + * @param string $name + * @return bool + */ + public function verify($skipMember = null, $name = null) + { + $xoops = Xoops::getInstance(); + $sessionName = empty($name) ? $this->name : $name; + $skipMember = ($skipMember === null) ? $_SESSION["{$sessionName}_skipmember"] : $skipMember; + $maxAttempts = $_SESSION["{$sessionName}_maxattempts"]; + $attempt = $_SESSION["{$sessionName}_attempt"]; + $is_valid = false; + // Skip CAPTCHA verification if disabled + if (!$this->isActive()) { + $is_valid = true; + // Skip CAPTCHA for member if set + } else { + if ($xoops->isUser() && !empty($skipMember)) { + $is_valid = true; + // Kill too many attempts + } else { + if (!empty($maxAttempts) && $attempt > $maxAttempts) { + $this->message[] = _CAPTCHA_TOOMANYATTEMPTS; + // Verify the code + } else { + $is_valid = $this->handler->verify($sessionName); + } + } + } + + if (!$is_valid) { + // Increase the attempt records on failure + $_SESSION["{$sessionName}_attempt"]++; + // Log the error message + $this->message[] = _CAPTCHA_INVALID_CODE; + } else { + // reset attempt records on success + $_SESSION["{$sessionName}_attempt"] = null; + } + $this->destroyGarbage(true); + return $is_valid; + } + + /** + * XoopsCaptcha::getCaption() + * + * @return string + */ + public function getCaption() + { + return defined('_CAPTCHA_CAPTION') ? constant('_CAPTCHA_CAPTION') : ''; + } + + /** + * XoopsCaptcha::getMessage() + * + * @return sdtring + */ + public function getMessage() + { + return implode('<br />', $this->message); + } + + /** + * Destory historical stuff + * + * @param bool $clearSession + * @return bool + */ + public function destroyGarbage($clearSession = false) + { + $this->loadHandler(); + $this->handler->destroyGarbage(); + + if ($clearSession) { + foreach ($this->config as $k => $config ) { + $_SESSION[$this->name . '_' . $k] = null; + } + } + return true; + } + + /** + * XoopsCaptcha::render() + * + * @return string + */ + public function render() + { + $sessionName = $this->config['name']; + $_SESSION[$sessionName . '_name'] = $sessionName; + foreach ($this->config as $k => $config ) { + $_SESSION[$sessionName . '_' . $k] = $config; + } + $form = ''; + if (!$this->active || empty($this->config['name'])) { + return $form; + } + + $maxAttempts = $this->config['maxattempts']; + $attempt = isset($_SESSION[$sessionName . '_attempt']) ? $_SESSION[$sessionName . '_attempt'] : 0; + $_SESSION[$sessionName . '_attempt'] = $attempt; + + // Failure on too many attempts + if (!empty($maxAttempts) && $attempt > $maxAttempts) { + $form = _CAPTCHA_TOOMANYATTEMPTS; + // Load the form element + } else { + $form = $this->loadForm(); + } + return $form; + } + + /** + * XoopsCaptcha::renderValidationJS() + * + * @return string + */ + public function renderValidationJS() + { + if (!$this->active || empty($this->config['name'])) { + return ''; + } + return $this->handler->renderValidationJS(); + } + + /** + * XoopsCaptcha::setCode() + * + * @param mixed $code + * @return bool + */ + public function setCode($code = null) + { + $code = ($code === null) ? $this->handler->getCode() : $code; + if (!empty($code)) { + $_SESSION[$this->name . '_code'] = $code; + return true; + } + return false; + } + + /** + * XoopsCaptcha::loadForm() + * + * @return string + */ + public function loadForm() + { + $form = $this->handler->render(); + $this->setCode(); + return $form; + } +} + +/** + * Abstract class for CAPTCHA method + * + * Currently there are two types of CAPTCHA forms, text and image + * The default mode is "text", it can be changed in the priority: + * 1 If mode is set through XoopsFormCaptcha::setConfig("mode", $mode), take it + * 2 Elseif mode is set though captcha/config.php, take it + * 3 Else, take "text" + */ +class XoopsCaptchaMethod +{ + /** + * @var XoopsCaptcha + */ + public $handler; + + /** + * @var array + */ + public $config; + + /** + * @var string + */ + public $code; + + /** + * XoopsCaptchaMethod::__construct() + * + * @param mixed $handler + */ + public function __construct($handler = null) + { + $this->handler = $handler; + } + + /** + * XoopsCaptchaMethod::isActive() + * + * @return bool + */ + public function isActive() + { + return true; + } + + /** + * XoopsCaptchaMethod::loadConfig() + * + * @param string $name + * @return array + */ + public function loadConfig($name = '') + { + $this->config = empty($name) ? $this->handler->config + : array_merge($this->handler->config, $this->handler->loadConfig($name)); + + } + + /** + * XoopsCaptchaMethod::getCode() + * + * @return string + */ + public function getCode() + { + return strval($this->code); + } + + /** + * XoopsCaptchaMethod::render() + * + * @return string + */ + public function render() + { + return ''; + } + + /** + * @return string + */ + public function renderValidationJS() + { + return ''; + } + + /** + * XoopsCaptchaMethod::verify() + * + * @param mixed $sessionName + * @return bool + */ + public function verify($sessionName = null) + { + $is_valid = false; + if (!empty($_SESSION["{$sessionName}_code"])) { + $func = !empty($this->config['casesensitive']) ? 'strcmp' : 'strcasecmp'; + $is_valid = !$func(trim(@$_POST[$sessionName]), $_SESSION["{$sessionName}_code"]); + } + return $is_valid; + } + + /** + * @return bool + */ + public function destroyGarbage() + { + return true; + } + } \ No newline at end of file Property changes on: XoopsCore/branches/2.6.x/2.6.0/htdocs/class/captcha/xoopscaptcha.php ___________________________________________________________________ Deleted: svn:eol-style - native Modified: XoopsCore/branches/2.6.x/2.6.0/htdocs/class/xoopsform/formcaptcha.php =================================================================== --- XoopsCore/branches/2.6.x/2.6.0/htdocs/class/xoopsform/formcaptcha.php 2012-10-12 00:32:32 UTC (rev 10213) +++ XoopsCore/branches/2.6.x/2.6.0/htdocs/class/xoopsform/formcaptcha.php 2012-10-12 00:38:13 UTC (rev 10214) @@ -58,6 +58,7 @@ $this->captchaHandler = XoopsCaptcha::getInstance(); $configs['name'] = $name; $configs['skipmember'] = $skipmember; + $configs = $this->captchaHandler->loadConfig(); $this->captchaHandler->setConfigs($configs); if (! $this->captchaHandler->isActive()) { $this->setHidden(); |