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

/**
  * Contacts the Device Classification Server and sets the the appropriate properties
  * @package Core
  */
  
class DeviceClassifier {
    protected $userAgent = '';
    protected $classification = array(
        'pagetype' => 'unknown',
        'platform' => 'unknown',
        'browser'  => 'unknown',
    );
    protected $version = 1;
    
    protected function cookieKey() {
        return KUROGO_IS_API ? 'apiDeviceClassification': 'deviceClassification';
    }
    
    protected function unknownClassification() {
        return array(
            'pagetype' => 'unknown',
            'platform' => 'unknown',
            'browser'  => 'unknown',
        );
    }
    
    protected function classificationForString($string, $stringIsFromCookie=false) {
        $classification = $this->unknownClassification();
        
        if ((substr($string, 0, 1) == '{') && (($json = json_decode($string, true)) !== false)) {
            // JSON format used by new style cookies
            if (is_array($json)) {
                if (isset($json['pagetype'])) {
                    $classification['pagetype'] = $json['pagetype'];
                }
                if (isset($json['platform'])) {
                    $classification['platform'] = $json['platform'];
                }
                if (isset($json['browser'])) {
                    $classification['browser'] = $json['browser'];
                }
            }
            
        } else {
            // Hyphen-separated format used by debugging device override and old cookies
            $parts = explode('-', $string);
            if (count($parts) && strlen($parts[0])) {
                $classification['pagetype'] = $parts[0];
                
                if (count($parts) > 1 && strlen($parts[1])) {
                    $classification['platform'] = $parts[1];
                    
                    if (count($parts) > 2 && strlen($parts[2]) && $parts[2] != '0' && $parts[2] != '1') {
                        $classification['browser'] = $parts[2];
                    }
                }
            }
            
            if ($stringIsFromCookie) {error_log("Upgrading cookie");
                $this->setDeviceCookie(); // Upgrade non-json cookie values to json
            }
        }

        return $classification;        
    }
    
    protected function stringForClassification($classification, $computerReadable=true) {
        if ($computerReadable) {
            return json_encode($classification);
            
        } else {
            return implode('-', array(
                $classification['pagetype'], 
                $classification['platform'],
                $classification['browser'],
            ));
        }
    }
    
    public static function getDeviceDetectionTypes() {
        return array(
            0 => Kurogo::getLocalizedString('DEVICE_DETECTION_INTERNAL'),
            1 => Kurogo::getLocalizedString('DEVICE_DETECTION_EXTERNAL')
        );
    }
  
    protected function getDeviceCookieString() {
        return $this->stringForClassification($this->classification);
    }
    
    protected function setDeviceFromCookieString($string) {
        $this->classification = $this->classificationForString($string, true);
    }
    
    public function getDevice() {
        return $this->stringForClassification($this->classification, false);
    }
    
    protected function setDevice($device) {
        $this->classification = $this->classificationForString($device);
    }
    
    protected function cacheFolder() {
        return CACHE_DIR . "/DeviceDetection";
    }
  
    protected function cacheLifetime() {
        return Kurogo::getSiteVar('MOBI_SERVICE_CACHE_LIFETIME');
    }
    
    function __construct($device = null) {
        $this->version = intval(Kurogo::getSiteVar('MOBI_SERVICE_VERSION'));
        
        $this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        
        if ($device && strlen($device)) {
            Kurogo::log(LOG_DEBUG, "Setting device to $device (override)", "deviceDetection");<commit_msg>Upgrade cookie after setting device classification<commit_after><?php
/**
  * @package Core
  */

/**
  * Contacts the Device Classification Server and sets the the appropriate properties
  * @package Core
  */
  
class DeviceClassifier {
    protected $userAgent = '';
    protected $classification = array(
        'pagetype' => 'unknown',
        'platform' => 'unknown',
        'browser'  => 'unknown',
    );
    protected $version = 1;
    
    protected function cookieKey() {
        return KUROGO_IS_API ? 'apiDeviceClassification': 'deviceClassification';
    }
    
    protected function unknownClassification() {
        return array(
            'pagetype' => 'unknown',
            'platform' => 'unknown',
            'browser'  => 'unknown',
        );
    }
    
    protected function classificationForString($string, &$stringIsJSON=null) {
        $classification = $this->unknownClassification();
        $isJSON = true;
        
        if ((substr($string, 0, 1) == '{') && (($json = json_decode($string, true)) !== false)) {
            // JSON format used by new style cookies
            if (is_array($json)) {
                if (isset($json['pagetype'])) {
                    $classification['pagetype'] = $json['pagetype'];
                }
                if (isset($json['platform'])) {
                    $classification['platform'] = $json['platform'];
                }
                if (isset($json['browser'])) {
                    $classification['browser'] = $json['browser'];
                }
            }
            
        } else {
            // Hyphen-separated format used by debugging device override and old cookies
            $isJSON = false;
            
            $parts = explode('-', $string);
            if (count($parts) && strlen($parts[0])) {
                $classification['pagetype'] = $parts[0];
                
                if (count($parts) > 1 && strlen($parts[1])) {
                    $classification['platform'] = $parts[1];
                    
                    if (count($parts) > 2 && strlen($parts[2]) && $parts[2] != '0' && $parts[2] != '1') {
                        $classification['browser'] = $parts[2];
                    }
                }
            }
        }
        
        if (isset($stringIsJSON)) {
            $stringIsJSON = $isJSON;
        }
        
        return $classification;        
    }
    
    protected function stringForClassification($classification, $computerReadable=true) {
        if ($computerReadable) {
            return json_encode($classification);
            
        } else {
            return implode('-', array(
                $classification['pagetype'], 
                $classification['platform'],
                $classification['browser'],
            ));
        }
    }
    
    public static function getDeviceDetectionTypes() {
        return array(
            0 => Kurogo::getLocalizedString('DEVICE_DETECTION_INTERNAL'),
            1 => Kurogo::getLocalizedString('DEVICE_DETECTION_EXTERNAL')
        );
    }
  
    protected function getDeviceCookieString() {
        return $this->stringForClassification($this->classification);
    }
    
    protected function setDeviceFromCookieString($string) {
        $this->classification = $this->classificationForString($string, $stringIsJSON);
        if (!$stringIsJSON) {
            // old cookie format, overwrite
            $this->setDeviceCookie();
        }
    }
    
    public function getDevice() {
        return $this->stringForClassification($this->classification, false);
    }
    
    protected function setDevice($device) {
        $this->classification = $this->classificationForString($device);
    }
    
    protected function cacheFolder() {
        return CACHE_DIR . "/DeviceDetection";
    }
  
    protected function cacheLifetime() {
        return Kurogo::getSiteVar('MOBI_SERVICE_CACHE_LIFETIME');
    }
    
    function __construct($device = null) {
        $this->version = intval(Kurogo::getSiteVar('MOBI_SERVICE_VERSION'));
        
        $this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        
        if ($device && strlen($device)) {
            Kurogo::log(LOG_DEBUG, "Setting device to $device (override)", "deviceDetection");
{'fraction_non_alphanumeric': 0.133419689119171, 'fraction_numerical': 0.004003768252472915, 'mean_word_length': 1.9387543252595156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '2098436', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2144, 'n_words': 426}