<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @author Chris - cqix
 * @author yikk
 * @license MIT License http://www.opensource.org/licenses/mit-license.php
 * @package libraries
 * @version $Id$
 */

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

/**
 * Add a new methode to catch the order of elements
 */
class My_User_agent extends CI_User_agent
{
    protected $languages_sort_key;

    /**
     * Set the accepted languages
     *
     * @return	void
     */
    protected function _us_set_languages()
    {
        if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') {
            $this->languages_sort_key = array();
            $this->languages = array();
            preg_match_all('/([\w\-,]*);q=([0-9\.]+),?/i', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])), $matches, PREG_SET_ORDER);
            foreach ($matches as $match) {
                $this->languages_sort_key[(string)$match[2]] = explode(',', $match[1]);
            }
            krsort($this->languages_sort_key, SORT_NUMERIC);
            foreach ($this->languages_sort_key as $match) {
                $strpos = strpos($match[0], '-');
                $this->languages[] = ($strpos === false) ? $match[0] : substr($match[0], 0, $strpos);
            }
        }

        if (count($this->languages) == 0) {
            $this->languages_sort_key = array('0'=>array('Undefined'));
            $this->languages = array('Undefined');
        }
    }

    /**
     * Get the accepted languages
     *
     * @param   boolean $type   Set true=Ordered, false=Unordered (Default=false)
     * @return  array
     */
    public function languages($type=false)
    {
        if (count($this->languages) == 0) {
            $this->_us_set_languages();
        }

        return ($type===true) ? $this->languages_sort_key : $this->languages;
    }
    
    /**
     * Test for really old browser
     * 
     * Latest update = Support 'let' https://caniuse.com/#feat=let
     * 
     * @return  boolean
     */
    public function is_really_old_browser()
    {
        switch ($this->browser) {
            case 'Internet Explorer':
                return true;
            case 'Trident (Internet Explorer)':
                return true;
            case 'Chrome':
                $version = intval(substr($this->version, 0, strpos($this->version,'.')));
                if ($version<49) {
                    return true;
                }
                break;
            case 'Opera':
                $version =  (strpos($this->version,'.') !== false) ? intval(substr($this->version, 0, strpos($this->version,'.'))) : $this->version;
                if ($version<36) {
                    return true;
                }
                break;
            case 'Firefox':
                if ($this->version<44) {
                    return true;
                }
                break;
            case 'Safari':
                if ($this->version<11) {
                    return true;
                }
                break;
        }
        
        return false;
    }
    
} // END class My_User_agent
