/**
  * Evaluates the result of the given callback
  * 
  * @param string $key Unique key for this
  * @param callable $callback Callback for evaluating the content
  * @return mixed Result of $callback()
  */
 public function getContent($key, $callback)
 {
     if ($this->nestedContentFilter) {
         return $this->nestedContentFilter->getContent($key, $callback);
     } else {
         return call_user_func($callback);
     }
 }
 /**
  * Init the selection from the query vars
  * @param array $passedArray
  * @return void
  */
 public function initFromHttpVars($passedArray = null)
 {
     if ($passedArray != null) {
         if (isset($passedArray["selection_nodes"]) && is_array($passedArray["selection_nodes"])) {
             $this->initFromNodes($passedArray["selection_nodes"]);
         } else {
             $this->initFromArray($passedArray);
         }
     } else {
         $this->initFromArray($_GET);
         $this->initFromArray($_POST);
     }
     if (isset($this->contentFilter)) {
         $this->contentFilter->filterUserSelection($this);
     }
 }
 public function getContent($key, $callback)
 {
     $cache = $this->getCache();
     // Return cached value if available
     $cacheEnabled = \Config::inst()->get(get_class(), 'cache_enabled');
     $result = isset($_GET['flush']) || !$cacheEnabled ? null : $cache->load($key);
     if ($result) {
         return $result;
     }
     // Fallback to generate result
     $result = parent::getContent($key, $callback);
     $cache->save($result, $key);
     return $result;
 }
Example #4
0
 /**
  * Fix unserialization issues by reloading object from data array
  */
 private function checkCFilterData()
 {
     if (!is_object($this->contentFilter) && is_array($this->contentFilterData)) {
         $cf = new ContentFilter(array());
         $cf->fromFilterArray($this->contentFilterData);
         $this->contentFilter = $cf;
     }
 }
 public function getContent($key, $callback)
 {
     // Bypass rate limiting if flushing, or timeout isn't set
     $timeout = \Config::inst()->get(get_class(), 'lock_timeout');
     if (isset($_GET['flush']) || !$timeout) {
         return parent::getContent($key, $callback);
     }
     // Generate result with rate limiting enabled
     $limitKey = $this->getCacheKey($key);
     $cache = $this->getCache();
     if ($lockedUntil = $cache->load($limitKey)) {
         if (time() < $lockedUntil) {
             // Politely inform visitor of limit
             $response = new \SS_HTTPResponse_Exception('Too Many Requests.', 429);
             $response->getResponse()->addHeader('Retry-After', 1 + $lockedUntil - time());
             throw $response;
         }
     }
     // Apply rate limit
     $cache->save(time() + $timeout, $limitKey);
     // Generate results
     $result = parent::getContent($key, $callback);
     // Reset rate limit with optional cooldown
     if ($cooldown = \Config::inst()->get(get_class(), 'lock_cooldown')) {
         // Set cooldown on successful query execution
         $cache->save(time() + $cooldown, $limitKey);
     } else {
         // Without cooldown simply disable lock
         $cache->remove($limitKey);
     }
     return $result;
 }