<?php

namespace lib;
if(!defined('ACCESS_CONTROL')) {header($_SERVER['SERVER_PROTOCOL']. ' 404'); exit('404 not found');}

class Helpers {
    public static function size_readable($size, $max = null, $system = 'si', $retstring = '%01.2f %s')
    {
        // Pick units
        $systems['si']['prefix'] = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
        $systems['si']['size']   = 1000;
        $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
        $systems['bi']['size']   = 1024;
        $sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];

        // Max unit to display
        $depth = count($sys['prefix']) - 1;
        if ($max && false !== $d = array_search($max, $sys['prefix'])) {
            $depth = $d;
        }

        // Loop
        $i = 0;
        while ($size >= $sys['size'] && $i < $depth) {
            $size /= $sys['size'];
            $i++;
        }

        return sprintf($retstring, $size, $sys['prefix'][$i]);
    }

    public static function cut_long_string($string, $max_length=15)
    {
        if(mb_strlen($string, 'UTF-8') > $max_length)
            return mb_substr($string, 0, $max_length, 'UTF-8').'...';
        else return $string;
    }
    public static function platforms()
    {
        /*VERSION_UNKNOWN	equ 0
		VERSION_2000	equ 1
		VERSION_XP		equ 2
		VERSION_2003	equ 3
		VERSION_VISTA	equ 4
		VERSION_2008	equ 5
		VERSION_SEVEN	equ 6
		VERSION_2008_R2	equ 7
		VERSION_8		equ 8
		VERSION_8_1		equ 9
		VERSION_10		equ 10
        */
        return array(
            'Other', 'Windows 2000', 'Windows XP', 'Windows 2003', 'Windows Vista', 'Windows 2008', 'Windows 7', 'Windows 2008 R2', 'Windows 8', 'Windows 8.1', 'Windows 10'
        );
    }

    public static function country_name($alpha)
    {
        $countries = self::country_list();
        return isset($countries[$alpha]) ? $countries[$alpha] : 'Unknown';
    }

    public static function country_list()
    {
        return array(
            'US'=> 'United States',             'AU'=> 'Australia',         'CA'=> 'Canada',
            'CH'=> 'Switzerland',               'DE'=> 'Germany',           'ES'=> 'Spain',
            'GB'=> 'United Kingdom',            'IT'=> 'Italy',             'AD'=> 'Andorra',
            'AE'=> 'United Arab Emirates',      'AF'=> 'Afghanistan',       'AG'=> 'Antigua and Barbuda',
            'AI'=> 'Anguilla',                  'AL'=> 'Albania',           'AM'=> 'Armenia',
            'AO'=> 'Angola',                    'AQ'=> 'Antarctica',        'AR'=> 'Argentina',
            'AS'=> 'American Samoa',            'AT'=> 'Austria',           'AW'=> 'Aruba',
            'AX'=> 'Åland Islands',             'AZ'=> 'Azerbaijan',        'BA'=> 'Bosnia and Herzegovina',
            'BB'=> 'Barbados',                  'BD'=> 'Bangladesh',        'BE'=> 'Belgium',
            'BF'=> 'Burkina Faso',              'BG'=> 'Bulgaria',          'BH'=> 'Bahrain',
            'BI'=> 'Burundi',                   'BJ'=> 'Benin',             'BL'=> 'Saint BarthГ©lemy',
            'BM'=> 'Bermuda',                   'BN'=> 'Brunei',            'BO'=> 'Bolivia',
            'BR'=> 'Brazil',                    'BS'=> 'Bahamas, The',      'BT'=> 'Bhutan',
            'BV'=> 'Bouvet Island',             'BW'=> 'Botswana',          'BY'=> 'Belarus',
            'BZ'=> 'Belize',                    'CC'=> 'Cocos (Keeling) Islands',
            'CD'=> 'Congo, Democratic Republic of the',                     'CF'=> 'Central African Republic',
            'CG'=> 'Congo, Republic of the',    'CI'=> 'Cote d\'Ivoire',    'CK'=> 'Cook Islands',
            'CL'=> 'Chile',                     'CM'=> 'Cameroon',          'CN'=> 'China',
            'CO'=> 'Colombia',                  'CP'=> 'Clipperton Island', 'CR'=> 'Costa Rica',
            'CU'=> 'Cuba',                      'CV'=> 'Cape Verde',        'CX'=> 'Christmas Island',
            'CY'=> 'Cyprus',                    'CZ'=> 'Czech Republic',    'DJ'=> 'Djibouti',
            'DK'=> 'Denmark',                   'DM'=> 'Dominica',          'DO'=> 'Dominican Republic',
            'DZ'=> 'Algeria',                   'EC'=> 'Ecuador',           'EE'=> 'Estonia',
            'EG'=> 'Egypt',                     'EH'=> 'Western Sahara',    'ER'=> 'Eritrea',
            'ET'=> 'Ethiopia',                  'FI'=> 'Finland',           'FJ'=> 'Fiji',
            'FK'=> 'Falkland Islands (Islas Malvinas)',                     'FM'=> 'Micronesia, Federated States of',
            'FO'=> 'Faroe Islands',             'FR'=> 'France',            'GA'=> 'Gabon',
            'GD'=> 'Grenada',                   'GE'=> 'Georgia',           'GF'=> 'French Guiana',
            'GG'=> 'Guernsey',                  'GH'=> 'Ghana',             'GI'=> 'Gibraltar',
            'GL'=> 'Greenland',                 'GM'=> 'Gambia, The',       'GN'=> 'Guinea',
            'GP'=> 'Guadeloupe',                'GQ'=> 'Equatorial Guinea', 'GR'=> 'Greece',
            'GS'=> 'South Georgia and the South Sandwich Islands',          'GT'=> 'Guatemala',
            'GU'=> 'Guam',                      'GW'=> 'Guinea-Bissau',     'GY'=> 'Guyana',
            'HK'=> 'Hong Kong',                 'HM'=> 'Heard Island and McDonald Islands',
            'HN'=> 'Honduras',                  'HR'=> 'Croatia',           'HT'=> 'Haiti',
            'HU'=> 'Hungary',                   'ID'=> 'Indonesia',         'IE'=> 'Ireland',
            'IL'=> 'Israel',                    'IM'=> 'Isle of Man',       'IN'=> 'India',
            'IO'=> 'British Indian Ocean Territory',                        'IQ'=> 'Iraq',
            'IR'=> 'Iran',                      'IS'=> 'Iceland',           'JE'=> 'Jersey',
            'JM'=> 'Jamaica',                   'JO'=> 'Jordan',            'JP'=> 'Japan',
            'KE'=> 'Kenya',                     'KG'=> 'Kyrgyzstan',        'KH'=> 'Cambodia',
            'KI'=> 'Kiribati',                  'KM'=> 'Comoros',           'KN'=> 'Saint Kitts and Nevis',
            'KP'=> 'Korea, North',              'KR'=> 'Korea, South',      'KW'=> 'Kuwait',
            'KY'=> 'Cayman Islands',            'KZ'=> 'Kazakhstan',        'LA'=> 'Laos',
            'LB'=> 'Lebanon',                   'LC'=> 'Saint Lucia',       'LI'=> 'Liechtenstein',
            'LK'=> 'Sri Lanka',                 'LR'=> 'Liberia',           'LS'=> 'Lesotho',
            'LT'=> 'Lithuania',                 'LU'=> 'Luxembourg',        'LV'=> 'Latvia',
            'LY'=> 'Libya',                     'MA'=> 'Morocco',           'MC'=> 'Monaco',
            'MD'=> 'Moldova',                   'ME'=> 'Montenegro',        'MF'=> 'Saint Martin',
            'MG'=> 'Madagascar',                'MH'=> 'Marshall Islands',  'MK'=> 'Macedonia',
            'ML'=> 'Mali',                      'MM'=> 'Myanmar',           'MN'=> 'Mongolia',
            'MO'=> 'Macau',                     'MP'=> 'Northern Mariana Islands',
            'MQ'=> 'Martinique',                'MR'=> 'Mauritania',        'MS'=> 'Montserrat',
            'MT'=> 'Malta',                     'MU'=> 'Mauritius',         'MV'=> 'Maldives',
            'MW'=> 'Malawi',                    'MX'=> 'Mexico',            'MY'=> 'Malaysia',
            'MZ'=> 'Mozambique',                'NA'=> 'Namibia',           'NC'=> 'New Caledonia',
            'NE'=> 'Niger',                     'NF'=> 'Norfolk Island',    'NG'=> 'Nigeria',
            'NI'=> 'Nicaragua',                 'NL'=> 'Netherlands',       'NO'=> 'Norway',
            'NP'=> 'Nepal',                     'NR'=> 'Nauru',             'NU'=> 'Niue',
            'NZ'=> 'New Zealand',               'OM'=> 'Oman',              'PA'=> 'Panama',
            'PE'=> 'Peru',                      'PF'=> 'French Polynesia',  'PG'=> 'Papua New Guinea',
            'PH'=> 'Philippines',               'PK'=> 'Pakistan',          'PL'=> 'Poland',
            'PM'=> 'Saint Pierre and Miquelon', 'PN'=> 'Pitcairn',          'PR'=> 'Puerto Rico',
            'PS'=> 'Palestinian Territory, Occupied',                       'PT'=> 'Portugal',
            'PW'=> 'Palau',                     'PY'=> 'Paraguay',          'QA'=> 'Qatar',
            'RE'=> 'Reunion',                   'RO'=> 'Romania',           'RS'=> 'Serbia',
            'RU'=> 'Russia',                    'RW'=> 'Rwanda',            'SA'=> 'Saudi Arabia',
            'SB'=> 'Solomon Islands',           'SC'=> 'Seychelles',        'SD'=> 'South Sudan',
            'SE'=> 'Sweden',                    'SG'=> 'Singapore',         'SH'=> 'Saint Helena',
            'SI'=> 'Slovenia',                  'SJ'=> 'Svalbard and Jan Mayen',
            'SK'=> 'Slovakia',                  'SL'=> 'Sierra Leone',      'SM'=> 'San Marino',
            'SN'=> 'Senegal',                   'SO'=> 'Somalia',           'SR'=> 'Suriname',
            'ST'=> 'Sao Tome and Principe',     'SV'=> 'El Salvador',       'SY'=> 'Syria',
            'SZ'=> 'Swaziland',                 'TC'=> 'Turks and Caicos Islands',
            'TD'=> 'Chad',                      'TF'=> 'French Southern Lands',
            'TG'=> 'Togo',                      'TH'=> 'Thailand',          'TJ'=> 'Tajikistan',
            'TK'=> 'Tokelau',                   'TL'=> 'Timor-Leste',       'TM'=> 'Turkmenistan',
            'TN'=> 'Tunisia',                   'TO'=> 'Tonga',             'TR'=> 'Turkey',
            'TT'=> 'Trinidad and Tobago',       'TV'=> 'Tuvalu',            'TW'=> 'Taiwan',
            'TZ'=> 'Tanzania',                  'UA'=> 'Ukraine',           'UG'=> 'Uganda',
            'UM'=> 'United States Pacific Island Wildlife Refuges',         'UY'=> 'Uruguay',
            'UZ'=> 'Uzbekistan',                'VA'=> 'Holy See (Vatican City)',
            'VC'=> 'Saint Vincent and the Grenadines',                      'VE'=> 'Venezuela',
            'VG'=> 'British Virgin Islands',    'VI'=> 'Virgin Islands',    'VN'=> 'Vietnam',
            'VU'=> 'Vanuatu',                   'WF'=> 'Wallis and Futuna', 'WS'=> 'Samoa',
            'YE'=> 'Yemen',                     'YT'=> 'Mayotte',           'ZA'=> 'South Africa',
            'ZM'=> 'Zambia',                    'ZW'=> 'Zimbabwe'
        );
    }

}