<commit_before><?php

namespace voku\cache;

/**
 * Cache: global-cache class
 *
 * can use different cache-adapter:
 * - Redis
 * - Memcache / Memcached
 * - APC / APCu
 * - Xcache
 * - Array
 *
 * @package   voku\cache
 */
class Cache implements iCache
{

  /**
   * @var iAdapter
   */
  private $adapter;

  /**
   * @var iSerializer
   */
  private $serializer;

  /**
   * @var string
   */
  private $prefix = '';

  /**
   * @var Boolean
   */
  private $cacheIsReady = false;

  /**
   * @var bool
   */
  private $active = true;

  /**
   * __construct
   *
   * @param null|iAdapter    $adapter
   * @param null|iSerializer $serializer
   * @param boolean          $checkForUser check for dev-ip or if cms-user is logged-in
   * @param boolean          $cacheEnabled false will disable the cache (use it e.g. for global settings)
   */
  public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true)
  {
    static $adapterCache;

    // check for active-cache
    $this->setActive($cacheEnabled);
    if ($this->active !== true) {
      return false;
    }

    // test the cache also for dev
    $testCache = isset($_GET['testCache']) ? (int)$_GET['testCache'] : 0;

    // check for user-session / dev / ip && no testCache is set
    if ($checkForUser === true && $testCache != 1) {
      if (
          // $_SERVER == client
          (
              isset($_SERVER['SERVER_ADDR'])
              &&
              $_SERVER['SERVER_ADDR'] == $this->getClientIp()
          )
          ||
          // user is loggedIn
          (
              isset($_SESSION['userID'])
              &&
              $_SESSION['userID']
          )
          ||
          // user is a dev
          $this->checkForDev() === true
      ) {
        return false;
      }
    }

    // add default prefix
    $this->setPrefix((isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '') . '_' . (isset($_SESSION['language']) ? $_SESSION['language'] : '') . '_' . (isset($_SESSION['language_extra']) ? $_SESSION['language_extra'] : ''));

    if ($adapter === null || !is_object($adapter)) {

      if (is_object($adapterCache)) {
        $adapter = $adapterCache;
      } else {

        $memcached = null;
        $isMemcachedAvailable = false;
        if (extension_loaded('memcached')) {
          $memcached = new \Memcached();
          $isMemcachedAvailable = $memcached->addServer('127.0.0.1', '11211');
        }

        if ($isMemcachedAvailable === false) {
          $memcached = null;
        }

        $adapterMemcached = new AdapterMemcached($memcached);<commit_msg>[!]: use class-variable "adminSession" and no global-session variable<commit_after><?php

namespace voku\cache;

/**
 * Cache: global-cache class
 *
 * can use different cache-adapter:
 * - Redis
 * - Memcache / Memcached
 * - APC / APCu
 * - Xcache
 * - Array
 *
 * @package   voku\cache
 */
class Cache implements iCache
{

  /**
   * @var iAdapter
   */
  private $adapter;

  /**
   * @var iSerializer
   */
  private $serializer;

  /**
   * @var string
   */
  private $prefix = '';

  /**
   * @var Boolean
   */
  private $cacheIsReady = false;

  /**
   * @var bool
   */
  private $active = true;

  /**
   * @var mixed
   */
  private $adminSession = false;

  /**
   * __construct
   *
   * @param null|iAdapter    $adapter
   * @param null|iSerializer $serializer
   * @param boolean          $checkForUser check for dev-ip or if cms-user is logged-in
   * @param boolean          $cacheEnabled false will disable the cache (use it e.g. for global settings)
   * @param string|boolean   $adminSession set a user-id, if the user is a admin (so we can disable cache for this user)
   */
  public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $adminSession = false)
  {
    static $adapterCache;

    // check for active-cache
    $this->setActive($cacheEnabled);
    if ($this->active !== true) {
      return false;
    }

    $this->$adminSession = $adminSession;

    // test the cache also for dev
    $testCache = isset($_GET['testCache']) ? (int)$_GET['testCache'] : 0;

    // check for user-session / dev / ip && no testCache is set
    if ($checkForUser === true && $testCache != 1) {
      if (
          // $_SERVER == client
          (
              isset($_SERVER['SERVER_ADDR'])
              &&
              $_SERVER['SERVER_ADDR'] == $this->getClientIp()
          )
          ||
          // user is loggedIn
          (
          $this->adminSession
          )
          ||
          // user is a dev
          $this->checkForDev() === true
      ) {
        return false;
      }
    }

    // add default prefix
    $this->setPrefix((isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '') . '_' . (isset($_SESSION['language']) ? $_SESSION['language'] : '') . '_' . (isset($_SESSION['language_extra']) ? $_SESSION['language_extra'] : ''));

    if ($adapter === null || !is_object($adapter)) {

      if (is_object($adapterCache)) {
        $adapter = $adapterCache;
      } else {

        $memcached = null;
        $isMemcachedAvailable = false;
        if (extension_loaded('memcached')) {
          $memcached = new \Memcached();
          $isMemcachedAvailable = $memcached->addServer('127.0.0.1', '11211');
        }

        if ($isMemcachedAvailable === false) {
          $memcached = null;
        }

        $adapterMemcached = new AdapterMemcached($memcached);
{'fraction_non_alphanumeric': 0.15190103692923412, 'fraction_numerical': 0.004729852646898308, 'mean_word_length': 2.116780045351474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1041160', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1626, 'n_words': 392}