Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * The constructor initializes the session and sends the session headers
  * The session will be based on the given session handler. The session handler can be overridden by setting the SESSION_HANDLER definition.
  * Do note: if a handler fails to initialize, it will default to the DEFAULT_HANDLER.
  */
 public final function __construct()
 {
     if (!self::$headersSend) {
         //apply the session handler directive to our system
         if (!defined('SESSION_HANDLER')) {
             throw new \System\Error\Exception\SystemException('Invalid session handler given. Set to HANDLER_FILES; HANDLER_MEMCACHE; or a SessionHandler class');
         }
         self::$currentHandler = SESSION_HANDLER;
         switch (self::$currentHandler) {
             case SessionHandler::HANDLER_MEMCACHE:
                 //initialize memcache and establish a connection. This makes sure the MEMCACHE_* settings are defined and correct.
                 $memcache = new \System\Cache\Memcache\Memcache();
                 //if we are not connected, we do a fallthrough to regular files.
                 if ($memcache->isConnected()) {
                     ini_set('session.save_handler', 'memcache');
                     $hosts = $ports = array();
                     \System\Cache\Memcache\Memcache::getServers($hosts, $ports);
                     $connectArray = array();
                     foreach ($hosts as $index => $host) {
                         $connectArray[] = 'tcp://' . $host . ':' . $ports[$index];
                     }
                     //the save path expects something like 'tcp://IP:PORT'
                     ini_set('session.save_path', implode(',', $connectArray));
                     break;
                 }
                 //we do a fallthrough if the memcache is not connected and revert to files
                 self::$currentHandler = SessionHandler::HANDLER_FILES;
             case SessionHandler::HANDLER_FILES:
                 if (session_save_path() == '') {
                     session_save_path(PATH_TEMP);
                     //we use the temporary folder for this.
                 }
                 break;
             default:
                 if (class_exists(self::$currentHandler) && in_array('SessionHandlerInterface', class_implements(self::$currentHandler, true))) {
                     self::$customHandler = new self::$currentHandler();
                     session_set_save_handler(self::$customHandler, true);
                 } else {
                     throw new \System\Error\Exception\SystemException('Invalid session handler given: ' . self::$currentHandler);
                 }
         }
         //this implicitly sends a few headers
         session_cache_limiter('nocache');
         session_cache_expire(180);
         //this is the default, but we set it explicitly
         session_name(SITE_IDENTIFIER . '_session');
         session_start();
         //we should only set our headers once
         self::$headersSend = true;
     }
 }
Exemplo n.º 3
0
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * Stores a value from the LUTCache to the LUTCacheCache to relieve strain.
  * @param string The key to use for the LUT. Max 255 chars.
  * @param mixed The value to store. If the value is NULL, the entry is unset.
  */
 private static final function setLUTCacheCache($key, $value = null)
 {
     switch (LUTCACHE_CACHE) {
         case Types::CACHE_MEMCACHE:
             $mc = new \System\Cache\Memcache\Memcache();
             if ($value === null) {
                 unset($mc->{$key});
             } else {
                 $mc->store($key, $value, self::CACHE_CACHE_MEMCACHE_TIMEOUT);
             }
             break;
         case Types::CACHE_APC:
             $apc = new \System\Cache\APCCache\APCCache();
             if ($value === null) {
                 unset($apc->{$key});
             } else {
                 $apc->{$key} = $value;
             }
             break;
         case Types::CACHE_NONE:
         default:
             break;
     }
 }