<commit_before><?php
/**
  * @package Core
  */

/**
  * Name of the cookie used for device classification
  */
define('COOKIE_KEY', 'deviceClassification');

/**
  * Contacts the Device Classification Server and sets the the appropriate properties
  * @package Core
  */
class DeviceClassifier {
  private $pagetype = 'unknown';
  private $platform = 'unknown';
  private $certs = false;

  public function getDevice() {
    return implode('-', array(
      $this->pagetype, 
      $this->platform, 
      $this->certs ? '1' : '0')
    );
  }
  private function setDevice($device) {
    $parts = explode('-', $device);
    $this->pagetype = $parts[0];
    $this->platform = count($parts) > 1 && strlen($parts[1]) ? $parts[1] : 'unknown';
    $this->certs    = count($parts) > 2 && strlen($parts[2]) ? $parts[2] : false;
  }
  
  function __construct($device = null) {
    
    if ($device && strlen($device)) {
      $this->setDevice($device); // user override of device detection
      //error_log(__FUNCTION__."(): device forced to '$device' <{$_SERVER['REQUEST_URI']}>");
      
    } else if (isset($_COOKIE[COOKIE_KEY])) {
      $this->setDevice($_COOKIE[COOKIE_KEY]);
      //error_log(__FUNCTION__."(): choosing device cookie '{$_COOKIE['layout']}' <{$_SERVER['REQUEST_URI']}>");
      
    } elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
      $query = http_build_query(array(
        'user-agent' => $_SERVER['HTTP_USER_AGENT'],
      ));
      
      $json = file_get_contents($GLOBALS['siteConfig']->getVar('MOBI_SERVICE_URL').'?'.$query);
      $data = json_decode($json, true);
      
      switch (strtolower($data['pagetype'])) {
        case 'basic':
          if ($data['platform'] == 'computer' || $data['platform'] == 'spider') {
            $this->pagetype = 'compliant';
            
          } else if ($data['platform'] == 'bbplus') {
            $this->pagetype = 'compliant';
            
          } else {<commit_msg>cache results from the device detection server. Question whether having the cookie is really needed now.<commit_after><?php
/**
  * @package Core
  */

/**
  * Name of the cookie used for device classification
  */
define('COOKIE_KEY', 'deviceClassification');

/**
  * Contacts the Device Classification Server and sets the the appropriate properties
  * @package Core
  */
class DeviceClassifier {
  private $pagetype = 'unknown';
  private $platform = 'unknown';
  private $certs = false;

  public function getDevice() {
    return implode('-', array(
      $this->pagetype, 
      $this->platform, 
      $this->certs ? '1' : '0')
    );
  }
  private function setDevice($device) {
    $parts = explode('-', $device);
    $this->pagetype = $parts[0];
    $this->platform = count($parts) > 1 && strlen($parts[1]) ? $parts[1] : 'unknown';
    $this->certs    = count($parts) > 2 && strlen($parts[2]) ? $parts[2] : false;
  }
  
  private function cacheFolder()
  {
    return CACHE_DIR . "/DeviceDetection";
  }

  private function cacheLifetime()
  {
    return 900;
  }
  
  function __construct($device = null) {
    
    if ($device && strlen($device)) {
      $this->setDevice($device); // user override of device detection
      //error_log(__FUNCTION__."(): device forced to '$device' <{$_SERVER['REQUEST_URI']}>");
      
    } else if (isset($_COOKIE[COOKIE_KEY])) {
      $this->setDevice($_COOKIE[COOKIE_KEY]);
      //error_log(__FUNCTION__."(): choosing device cookie '{$_COOKIE['layout']}' <{$_SERVER['REQUEST_URI']}>");
      
    } elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
    
      $user_agent = $_SERVER['HTTP_USER_AGENT'];
      
      /* see if the server has cached the results from the the device detection server */
      $cache = new DiskCache($this->cacheFolder(), $this->cacheLifetime(), TRUE);
      $cacheFilename = md5($user_agent);

      if ($cache->isFresh($cacheFilename)) {
           $json = $cache->read($cacheFilename);
      } else {

          $query = http_build_query(array(
            'user-agent' => $user_agent
          ));
          
          $url = $GLOBALS['siteConfig']->getVar('MOBI_SERVICE_URL').'?'.$query;
          $json = file_get_contents($url);

          $cache->write($json, $cacheFilename);
      }            

      $data = json_decode($json, true);
      
      switch (strtolower($data['pagetype'])) {
        case 'basic':
          if ($data['platform'] == 'computer' || $data['platform'] == 'spider') {
            $this->pagetype = 'compliant';
            
          } else if ($data['platform'] == 'bbplus') {
            $this->pagetype = 'compliant';
            
          } else {
{'fraction_non_alphanumeric': 0.17393190197354153, 'fraction_numerical': 0.004771199306007373, 'mean_word_length': 2.802143446001649, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4858612', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1402, 'n_words': 308}