|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Typography. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2014-2018 Peter Putzer. |
|
6
|
|
|
* Copyright 2009-2011 KINGdesk, LLC. |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU General Public License as published by |
|
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License along |
|
19
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
20
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
21
|
|
|
* |
|
22
|
|
|
* *** |
|
23
|
|
|
* |
|
24
|
|
|
* @package mundschenk-at/php-typography |
|
25
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace PHP_Typography; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Common regular expression components. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 5.0.0 |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class RE { |
|
36
|
|
|
/** |
|
37
|
|
|
* Find the HTML character representation for the following characters: |
|
38
|
|
|
* tab | line feed | carriage return | space | non-breaking space | ethiopic wordspace |
|
39
|
|
|
* ogham space mark | en quad space | em quad space | en-space | three-per-em space |
|
40
|
|
|
* four-per-em space | six-per-em space | figure space | punctuation space | em-space |
|
41
|
|
|
* thin space | hair space | narrow no-break space |
|
42
|
|
|
* medium mathematical space | ideographic space |
|
43
|
|
|
* Some characters are used inside words, we will not count these as a space for the purpose |
|
44
|
|
|
* of finding word boundaries: |
|
45
|
|
|
* zero-width-space ("​", "​") |
|
46
|
|
|
* zero-width-joiner ("‌", "‌", "‍") |
|
47
|
|
|
* zero-width-non-joiner ("‍", "‍", "‌") |
|
48
|
|
|
*/ |
|
49
|
|
|
const HTML_SPACES = ' |
|
50
|
|
|
\x{00a0} # no-break space |
|
51
|
|
|
| |
|
52
|
|
|
\x{1361} # ethiopic wordspace |
|
53
|
|
|
| |
|
54
|
|
|
\x{2000} # en quad-space |
|
55
|
|
|
| |
|
56
|
|
|
\x{2001} # em quad-space |
|
57
|
|
|
| |
|
58
|
|
|
\x{2002} # en space |
|
59
|
|
|
| |
|
60
|
|
|
\x{2003} # em space |
|
61
|
|
|
| |
|
62
|
|
|
\x{2004} # three-per-em space |
|
63
|
|
|
| |
|
64
|
|
|
\x{2005} # four-per-em space |
|
65
|
|
|
| |
|
66
|
|
|
\x{2006} # six-per-em space |
|
67
|
|
|
| |
|
68
|
|
|
\x{2007} # figure space |
|
69
|
|
|
| |
|
70
|
|
|
\x{2008} # punctuation space |
|
71
|
|
|
| |
|
72
|
|
|
\x{2009} # thin space |
|
73
|
|
|
| |
|
74
|
|
|
\x{200a} # hair space |
|
75
|
|
|
| |
|
76
|
|
|
\x{200b} # zero-width space |
|
77
|
|
|
| |
|
78
|
|
|
\x{200c} # zero-width joiner |
|
79
|
|
|
| |
|
80
|
|
|
\x{200d} # zero-width non-joiner |
|
81
|
|
|
| |
|
82
|
|
|
\x{202f} # narrow no-break space |
|
83
|
|
|
| |
|
84
|
|
|
\x{205f} # medium mathematical space |
|
85
|
|
|
| |
|
86
|
|
|
\x{3000} # ideographic space |
|
87
|
|
|
'; // required modifiers: x (multiline pattern) i (case insensitive) u (utf8). |
|
88
|
|
|
|
|
89
|
|
|
const NORMAL_SPACES = ' \f\n\r\t\v'; // equivalent to \s in non-Unicode mode. |
|
90
|
|
|
|
|
91
|
|
|
// Marker for strings that should not be replaced. |
|
92
|
|
|
const ESCAPE_MARKER = '_E_S_C_A_P_E_D_'; |
|
93
|
|
|
|
|
94
|
|
|
// Inserted HTML tags. |
|
95
|
|
|
const ESCAPED_HTML_OPEN = '_B_E_G_I_N_H_T_M_L_'; |
|
96
|
|
|
const ESCAPED_HTML_CLOSE = '_E_N_D_H_T_M_L_'; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* A pattern matching top-level domains. |
|
100
|
|
|
* |
|
101
|
|
|
* @var string |
|
102
|
|
|
*/ |
|
103
|
|
|
private static $top_level_domains_pattern; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Load a list of top-level domains from a file. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $path The full path and filename. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string A list of top-level domains concatenated with '|'. |
|
111
|
|
|
*/ |
|
112
|
1 |
|
private static function get_top_level_domains_from_file( $path ) { |
|
113
|
1 |
|
$domains = []; |
|
114
|
|
|
|
|
115
|
1 |
|
if ( \file_exists( $path ) ) { |
|
116
|
1 |
|
$file = new \SplFileObject( $path ); |
|
117
|
|
|
|
|
118
|
1 |
|
while ( ! $file->eof() ) { |
|
119
|
1 |
|
$line = $file->fgets(); |
|
120
|
|
|
|
|
121
|
1 |
|
if ( \preg_match( '#^[a-zA-Z0-9][a-zA-Z0-9-]*$#', $line, $matches ) ) { |
|
122
|
1 |
|
$domains[] = \strtolower( $matches[0] ); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
if ( \count( $domains ) > 0 ) { |
|
128
|
1 |
|
return \implode( '|', $domains ); |
|
129
|
|
|
} else { |
|
130
|
1 |
|
return 'ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|com|coop|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw'; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Retrieves a pattern matching all valid top-level domains. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public static function top_level_domains() { |
|
140
|
2 |
|
if ( empty( self::$top_level_domains_pattern ) ) { |
|
141
|
|
|
// Initialize valid top level domains from IANA list. |
|
142
|
1 |
|
self::$top_level_domains_pattern = self::get_top_level_domains_from_file( __DIR__ . '/IANA/tlds-alpha-by-domain.txt' ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
return self::$top_level_domains_pattern; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Replace < and > with escape markers. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $tags A string containing HTML markup (all other < and > must be entity encoded). |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public static function escape_tags( $tags ) { |
|
156
|
1 |
|
return str_replace( [ '<', '>' ], [ self::ESCAPED_HTML_OPEN, self::ESCAPED_HTML_CLOSE ], $tags ); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Replace tag escape markers with < and >. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $tags A string containing escaped HTML markup. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public static function unescape_tags( $tags ) { |
|
167
|
1 |
|
return str_replace( [ self::ESCAPED_HTML_OPEN, self::ESCAPED_HTML_CLOSE ], [ '<', '>' ], $tags ); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|