123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace Cube\Validate;
- use Cube\Controller\Front,
- Cube\Session;
- class Captcha extends AbstractValidate
- {
- const SESSION_NAMESPACE = 'Captcha';
- protected $_message = "The captcha code is not valid.";
-
- protected $_session;
-
- public function getSession()
- {
- if (!($this->_session instanceof Session)) {
- $this->setSession();
- }
- return $this->_session;
- }
-
- public function setSession(Session $session = null)
- {
- if ($session === null) {
- $session = Front::getInstance()->getBootstrap()->getResource('session');
- }
- if (!($session instanceof Session)) {
- $session = new Session();
- $session->setNamespace(self::SESSION_NAMESPACE);
- }
- $this->_session = $session;
- return $this;
- }
-
- public function isValid()
- {
- $name = $this->getName();
- $value = $this->getValue();
- $codes = (array) $this->getSession()->get($name);
- if (($key = array_search($value, $codes)) !== false) {
- unset($codes[$key]);
- $this->getSession()->set($name, $codes);
- return true;
- }
- else {
- return false;
- }
- }
- }
|