Ticket #1188: html_encoding.php

File html_encoding.php, 4.8 KB (added by Ronald Parcinski, 9 years ago)
Line 
1<?php
2/*--------------------------------------------------------------
3 $Id: html_encoding.php 10730 2017-05-10 16:52:57Z web28 $
4
5 modified eCommerce Shopsoftware - community made shopping
6
7 copyright (c) 2010-2013 modified www.modified-shop.org
8
9 (c) 2013 rpa-com.de <web28> and hackersolutions.com <h-h-h>
10
11 Released under the GNU General Public License
12--------------------------------------------------------------*/
13
14define('ENCODE_DEFINED_CHARSETS','ASCII,UTF-8,ISO-8859-1,ISO-8859-15,cp866,cp1251,cp1252,KOI8-R,BIG5,GB2312,BIG5-HKSCS,Shift_JIS,EUC-JP');
15define('ENCODE_DEFAULT_CHARSET', 'ISO-8859-15');
16
17/**
18 * encode_htmlentities
19 */
20function encode_htmlentities($string, $flags = ENT_COMPAT, $encoding = '')
21{
22 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
23 $default_charset = isset($_SESSION['language_charset']) && in_array(strtoupper($_SESSION['language_charset']), $supported_charsets) ? strtoupper($_SESSION['language_charset']) : ENCODE_DEFAULT_CHARSET;
24 $encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : $default_charset;
25 return htmlentities($string, $flags , $encoding);
26}
27
28/**
29 * encode_htmlspecialchars
30 */
31function encode_htmlspecialchars($string, $flags = ENT_COMPAT, $encoding = '')
32{
33 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
34 $default_charset = isset($_SESSION['language_charset']) && in_array(strtoupper($_SESSION['language_charset']), $supported_charsets) ? strtoupper($_SESSION['language_charset']) : ENCODE_DEFAULT_CHARSET;
35 $encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : $default_charset;
36 return htmlspecialchars($string, $flags , $encoding);
37}
38
39/**
40 * encode_utf8
41 */
42function encode_utf8($string, $encoding = '', $force_utf8 = false)
43{
44 if (strtolower($_SESSION['language_charset']) == 'utf-8' || $force_utf8 === true) {
45 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
46 $cur_encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : mb_detect_encoding($string, ENCODE_DEFINED_CHARSETS);
47 if ($cur_encoding == 'UTF-8' && mb_check_encoding($string, 'UTF-8')) {
48 return $string;
49 } else {
50 return mb_convert_encoding($string, 'UTF-8', $cur_encoding);
51 }
52 } else {
53 return $string;
54 }
55}
56
57/**
58 * decode_htmlentities
59 */
60function decode_htmlentities($string, $flags = ENT_COMPAT, $encoding = '')
61{
62 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
63 $default_charset = isset($_SESSION['language_charset']) && in_array(strtoupper($_SESSION['language_charset']), $supported_charsets) ? strtoupper($_SESSION['language_charset']) : ENCODE_DEFAULT_CHARSET;
64 $encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : $default_charset;
65 return html_entity_decode($string, $flags , $encoding);
66}
67
68/**
69 * decode_htmlspecialchars
70 */
71function decode_htmlspecialchars($string, $flags = ENT_COMPAT, $encoding = '')
72{
73 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
74 $default_charset = isset($_SESSION['language_charset']) && in_array(strtoupper($_SESSION['language_charset']), $supported_charsets) ? strtoupper($_SESSION['language_charset']) : ENCODE_DEFAULT_CHARSET;
75 $encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : $default_charset;
76 return htmlspecialchars_decode($string, $flags , $encoding);
77}
78
79/**
80 * decode_utf8
81 */
82function decode_utf8($string, $encoding = '', $force_utf8 = false)
83{
84 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
85 $default_charset = isset($_SESSION['language_charset']) && in_array(strtoupper($_SESSION['language_charset']), $supported_charsets) ? strtoupper($_SESSION['language_charset']) : ENCODE_DEFAULT_CHARSET;
86 $encoding = !empty($encoding) && in_array(strtoupper($encoding), $supported_charsets) ? strtoupper($encoding) : $default_charset;
87 if (strtolower($_SESSION['language_charset']) != 'utf-8' || $force_utf8 === true) {
88 $cur_encoding = mb_detect_encoding($string);
89 if ($cur_encoding == 'UTF-8' && mb_check_encoding($string, 'UTF-8')) {
90 return mb_convert_encoding($string, $encoding, 'UTF-8');
91 } else {
92 return $string;
93 }
94 } else {
95 return $string;
96 }
97}
98
99/**
100 * get_supported_charset
101 */
102function get_supported_charset($charset = '')
103{
104 $charset = !empty($charset) ? $charset : (isset($_SESSION['language_charset']) ? $_SESSION['language_charset'] : null);
105 $supported_charsets = explode(',', strtoupper(ENCODE_DEFINED_CHARSETS));
106 $default_charset = isset($charset) && in_array(strtoupper($charset), $supported_charsets) ? strtoupper($charset) : ENCODE_DEFAULT_CHARSET;
107 return $default_charset;
108}