| version 1.11, 2015/01/20 15:20:17 |
version 1.12, 2017/11/08 01:12:03 |
|
|
| * <?php |
* <?php |
| * require_once 'Platon.php'; |
* require_once 'Platon.php'; |
| * Platon::function_name(); |
* Platon::function_name(); |
| * ? > |
* ?> |
| * |
* |
| * @author Ondrej Jombik <nepto@platon.sk> |
* @author Ondrej Jombik <nepto@platon.sk> |
| |
* @author Igor Mino <igor@platon.sk> |
| * @package Platon |
* @package Platon |
| * @version 1.0 |
* @version 1.1 |
| * @access public |
* @access public |
| */ |
*/ |
| |
|
| /* $Platon: Metafox/lib/Platon.php,v 1.10 2014-10-15 13:00:16 igor Exp $ */ |
/* $Platon: phpPlatonLib/Platon/Platon.php,v 1.43 2017/11/08 01:09:07 nepto Exp $ */ |
| |
|
| class Platon |
class Platon |
| { |
{ |
|
|
| |
|
| header('Location: '.$url); |
header('Location: '.$url); |
| if (! $temporary) { |
if (! $temporary) { |
| switch($status_code) { |
switch($status_code) { |
| case 303: |
case 303: |
| header('HTTP/1.1 303 See Other'); |
header('HTTP/1.1 303 See Other'); |
| break; |
break; |
| case 302: |
case 302: |
| header('HTTP/1.1 302 Found'); |
header('HTTP/1.1 302 Found'); |
| break; |
break; |
| case 301: |
case 301: |
| default: |
default: |
| header('HTTP/1.1 301 Moved Permanently'); |
header('HTTP/1.1 301 Moved Permanently'); |
| break; |
break; |
| } |
} |
| } |
} |
| exit; |
exit; |
| } /* }}} */ |
} /* }}} */ |
|
|
| # end pretty_substr; |
# end pretty_substr; |
| # }}} |
# }}} |
| |
|
| /** |
/** |
| * Insert element into array on specific index. |
* Insert element into array on specific index. |
| * |
* |
| * @param array $arr input array |
* @param array $arr input array |
| * @param int $index position in array |
* @param int $index position in array |
| * @param mix $element inserted element |
* @param mix $element inserted element |
| * @return array output array |
* @return array output array |
| */ |
*/ |
| static function array_insert($arr, $index, $element) { |
static function array_insert($arr, $index, $element) /* {{{ */ |
| if (!is_array($arr)) { |
{ |
| return false; |
if (!is_array($arr)) { |
| } |
return false; |
| $index = intval($index); |
} |
| if ($index > count($arr)-1) { |
$index = intval($index); |
| array_push($arr, $element); |
if ($index > count($arr)-1) { |
| return $arr; |
array_push($arr, $element); |
| } |
return $arr; |
| $before = array_splice($arr, 0, $index); |
} |
| array_push($before, $element); |
$before = array_splice($arr, 0, $index); |
| return array_merge($before, $arr); |
array_push($before, $element); |
| } |
return array_merge($before, $arr); |
| |
} /* }}} */ |
| |
|
| /** |
/** |
| * Returns english grammar related number. |
* Returns english grammar related number. |
| * |
* |
|
|
| return $number; |
return $number; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| |
private static $_htmlspecialchars_encoding = ''; |
| |
|
| |
/** |
| |
* Set charset/encoding for out Platon::htmlspecialchars() |
| |
* |
| |
* @param string $encoding encoding, for more info |
| |
* see _deduct_htmlspecialchars_encoding() |
| |
* @return boolean always true |
| |
*/ |
| |
static function htmlspecialchars_encoding($encoding) /* {{{ */ |
| |
{ |
| |
Platon::$_htmlspecialchars_encoding = $encoding; |
| |
return true; |
| |
} /* }}} */ |
| |
|
| |
/** |
| |
* Encode special HTML characters based on encoding and PHP version |
| |
* |
| |
* @param string $string string to encode |
| |
* @param string $encoding encoding to use (use default when empty) |
| |
* @return string HTML encoded string |
| |
*/ |
| |
static function htmlspecialchars($string, $encoding = '') /* {{{ */ |
| |
{ |
| |
// no default encoding maps to no special transformation |
| |
static $map = array('' => ''); |
| |
if (strlen($encoding) <= 0) { |
| |
$encoding = Platon::$_htmlspecialchars_encoding; |
| |
} |
| |
if (! array_key_exists($encoding, $map)) { |
| |
if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 4) { |
| |
$map[$encoding]['flags'] = ENT_COMPAT | ENT_HTML401; |
| |
$map[$encoding]['encoding'] = Platon::_deduct_htmlspecialchars_encoding($encoding); |
| |
} elseif ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 6) |
| |
|| PHP_MAJOR_VERSION >= 7) { |
| |
$map[$encoding]['flags'] = ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED | ENT_SUBSTITUTE; |
| |
$map[$encoding]['encoding'] = Platon::_deduct_htmlspecialchars_encoding($encoding); |
| |
} else { |
| |
$map[$encoding] = ''; // no special htmlspecialchars() |
| |
} |
| |
} |
| |
if (is_array($map[$encoding])) { |
| |
return htmlspecialchars($string, |
| |
$map[$encoding]['flags'], |
| |
$map[$encoding]['encoding']); |
| |
} |
| |
// default |
| |
return htmlspecialchars($string); |
| |
} /* }}} */ |
| |
|
| |
static function _deduct_htmlspecialchars_encoding($encoding) /* {{{ */ |
| |
{ |
| |
// List of supported charsets http://php.net/manual/en/function.htmlspecialchars.php |
| |
$allowed = array( |
| |
'ISO-8859-1', 'ISO8859-1', // Western European, Latin-1 |
| |
'ISO-8859-5', 'ISO8859-5', // Little used cyrillic charset (Latin/Cyrillic) |
| |
'ISO-8859-15', 'ISO8859-15', // Western European, Latin-9. Adds the Euro sign and others |
| |
'UTF-8', // ASCII compatible multi-byte 8-bit Unicode |
| |
'cp866', 'ibm866', '866', // DOS-specific Cyrillic charset |
| |
'cp1251', 'Windows-1251', 'win-1251', '1251', // Windows-specific Cyrillic charset |
| |
'cp1252', 'Windows-1252', '1252', // Windows specific charset for Western European |
| |
'KOI8-R', 'koi8-ru', 'koi8r', // Russian |
| |
'BIG5', '950', // Traditional Chinese, mainly used in Taiwan |
| |
'GB2312', '936', // Simplified Chinese, national standard character set |
| |
'BIG5-HKSCS', // Big5 with Hong Kong extensions, Traditional Chinese |
| |
'Shift_JIS', 'SJIS', 'SJIS-win', 'cp932', '932', // Japanese |
| |
'EUC-JP', 'EUCJP', 'eucJP-win', // Japanese |
| |
'MacRoman', // Charset that was used by Mac OS. |
| |
'' // An empty string activates detection from script encoding (Zend multibyte), |
| |
// default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended. |
| |
); |
| |
if (in_array($encoding, $allowed)) { |
| |
return $encoding; |
| |
} |
| |
$encoding_upper = strtoupper($encoding); |
| |
foreach ($allowed as $allow) { |
| |
if (strtoupper($allow) == $encoding_upper) { |
| |
return $allow; |
| |
} |
| |
} |
| |
for ($len = strlen($encoding) - 1; $len > 0; $len--) { |
| |
$encoding_prefix = substr($encoding_upper, 0, $len); |
| |
foreach ($allowed as $allow) { |
| |
if (strtoupper(substr($allow, 0, min($len, strlen($allow)))) == $encoding_prefix) { |
| |
return $allow; |
| |
} |
| |
} |
| |
} |
| |
return null; |
| |
} /* }}} */ |
| |
|
| /** |
/** |
| * HTML special characters substitution with replacing space for ' '. |
* HTML special characters substitution with replacing space for ' '. |
| * |
* |
|
|
| */ |
*/ |
| static function htmlspecialchars2($str) /* {{{ */ |
static function htmlspecialchars2($str) /* {{{ */ |
| { |
{ |
| return str_replace(' ', ' ', htmlspecialchars($str)); |
return str_replace(' ', ' ', Platon::htmlspecialchars($str)); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /* |
/* |
|
|
| $accepted = preg_split('/,[[:space:]]*/', $accept_lang); |
$accepted = preg_split('/,[[:space:]]*/', $accept_lang); |
| $candidates = array(); |
$candidates = array(); |
| for ($i = 0; $i < count($accepted); $i++) { |
for ($i = 0; $i < count($accepted); $i++) { |
| if (preg_match('/^([a-z0-9_-]+);[[:space:]]*q=([0-9\.]+)/', $accepted[$i], $arr)) { |
if (preg_match('/^([a-z0-9_-]+);[[:space:]]*q=([0-9\.]+)/i', $accepted[$i], $arr)) { |
| $q = (double)$arr[2]; |
$q = (double)$arr[2]; |
| $l = $arr[1]; |
$l = $arr[1]; |
| } else { |
} else { |