/** * Calls the corresponding function in the block and executes the data retrieval function. * @return \SimpleXMLElement The XML data tree */ public final function callMemcacheBlock() { $callback = $this->getCallback(); $db = $this->getDatabase(); $mem = new \System\Cache\Memcache\Memcache(); $xml = null; if ($mem->keyExists($this->key)) { $xmlString = $mem->get($this->key); if ($xmlString) { //suppress warning here in case of malformed xml $xml = @simplexml_load_string($xmlString); if ($xml instanceof \SimpleXMLElement) { return $xml; } else { $errorLogger = \System\Log\ErrorLogger::getInstance(); $errorLogger->out('[MemcacheBlock] Could not read memcache key ' . $this->key . ' as XML. Regenerating.', \System\Log\LoggerLevel::LEVEL_WARNING); } } } if (is_callable($callback)) { $xml = call_user_func($callback, $this); $mem->store($this->key, $xml->asXML(), $this->timeout); } else { throw new \System\Error\Exception\InvalidMethodException('The given callback cannot be called. Does it exist and is it public?'); } return $xml; }
/** * Provides functionality to check if a given IP is blocked by a common blacklist * Do note this system requires the use of the PERMABAN_* directives * @param string The IP Address to check. * @return bool True if the IP is allowed, false otherwise */ public static final function isIPAllowed($ipAddress) { //if there is an explicit empty PERMABAN, we accept everything if (PERMABAN_HOST == '') { return true; } $allowed = true; $mc = new \System\Cache\Memcache\Memcache(); $key = self::MEMCACHE_KEY . $ipAddress; //we get the value from the memcache, and only recheck if the blocked user is on it. if (!($allowed = $mc->get($key))) { $db = \System\Db\Database::getConnection(PERMABAN_HOST, PERMABAN_USER, PERMABAN_PASS, PERMABAN_NAME, PERMABAN_PORT); $query = new \System\Db\Query($db, \System\HTTP\Visitor\PermaBan\SQL_PERMABAN_CHECK_PERMABAN); $query->bind($ipAddress, \System\Db\QueryType::TYPE_STRING); $results = $db->query($query); $allowed = $results->count() == 0; $mc->store($key, $allowed); } return $allowed; }