PluginProbe
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / trunk
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress vtrunk
4.12.0 4.10.0 4.9.0 4.8.1 trunk 1.0 1.1 1.12.1 1.2.3 1.2.4 1.2.5 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2 All 171 releases
custom-facebook-feed / inc / CFF_Autolink.php
CFF_Autolink.php
305 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Autolink
5 *
6 * @since 2.19
7 */
8
9 namespace CustomFacebookFeed;
10
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 class CFF_Autolink
16 {
17 public static function cff_autolink($text = '', $link_color = '', $span_tag = false, $limit = 100, $tagfill = 'class="cff-break-word"', $auto_title = true)
18 {
19 $text = self::cff_autolink_do($text, $link_color, '![a-z][a-z-]+://!i', $limit, $tagfill, $auto_title, $span_tag);
20 $text = self::cff_autolink_do($text, $link_color, '!(mailto|skype):!i', $limit, $tagfill, $auto_title, $span_tag);
21 $text = self::cff_autolink_do($text, $link_color, '!www\\.!i', $limit, $tagfill, $auto_title, 'http://', $span_tag);
22 return $text;
23 }
24
25 public static function cff_autolink_do($text, $link_color, $sub, $limit, $tagfill, $auto_title, $span_tag, $force_prefix = null)
26 {
27 $text_l = StrToLower($text);
28 $cursor = 0;
29 $loop = 1;
30 $buffer = '';
31
32 $autolink_options = [
33 // Should http:// be visibly stripped from the front
34 // of URLs?
35 'strip_protocols' => true,
36 ];
37
38 while (($cursor < strlen($text)) && $loop) {
39 $ok = 1;
40 $matched = preg_match($sub, $text_l, $m, PREG_OFFSET_CAPTURE, $cursor);
41
42 if (!$matched) {
43 $loop = 0;
44 $ok = 0;
45 } else {
46 $pos = $m[0][1];
47 $sub_len = strlen($m[0][0]);
48
49 $pre_hit = substr($text, $cursor, $pos - $cursor);
50 $hit = substr($text, $pos, $sub_len);
51 $pre = substr($text, 0, $pos);
52 $post = substr($text, $pos + $sub_len);
53
54 $fail_text = $pre_hit . $hit;
55 $fail_len = strlen($fail_text);
56
57 //
58 // substring found - first check to see if we're inside a link tag already...
59 //
60
61 $bits = preg_split("!</a>!i", $pre);
62 $last_bit = array_pop($bits);
63 if (preg_match("!<a\s!i", $last_bit)) {
64 // echo "fail 1 at $cursor<br />\n";
65
66 $ok = 0;
67 $cursor += $fail_len;
68 $buffer .= $fail_text;
69 }
70 }
71
72 //
73 // looks like a nice spot to autolink from - check the pre
74 // to see if there was whitespace before this match
75 //
76
77 if ($ok) {
78 if ($pre) {
79 if (!preg_match('![\s\(\[\{>]$!s', $pre)) {
80 // echo "fail 2 at $cursor ($pre)<br />\n";
81
82 $ok = 0;
83 $cursor += $fail_len;
84 $buffer .= $fail_text;
85 }
86 }
87 }
88
89 //
90 // we want to autolink here - find the extent of the url
91 //
92
93 if ($ok) {
94 if (preg_match('/^([a-z0-9\-\.\/\-_%~!?=,:;&+*#@\(\)\$]+)/i', $post, $matches)) {
95 $url = $hit . $matches[1];
96
97 $cursor += strlen($url) + strlen($pre_hit);
98 $buffer .= $pre_hit;
99
100 $url = html_entity_decode($url);
101
102
103 //
104 // remove trailing punctuation from url
105 //
106
107 while (preg_match('|[.,!;:?]$|', $url)) {
108 $url = substr($url, 0, strlen($url) - 1);
109 $cursor--;
110 }
111 foreach (array('()', '[]', '{}') as $pair) {
112 $o = substr($pair, 0, 1);
113 $c = substr($pair, 1, 1);
114 if (preg_match("!^(\\$c|^)[^\\$o]+\\$c$!", $url)) {
115 $url = substr($url, 0, strlen($url) - 1);
116 $cursor--;
117 }
118 }
119
120
121 //
122 // nice-i-fy url here
123 //
124
125 $link_url = $url;
126 $display_url = $url;
127
128 if ($force_prefix) {
129 $link_url = $force_prefix . $link_url;
130 }
131
132 if ($autolink_options['strip_protocols']) {
133 if (preg_match('!^(http|https)://!i', $display_url, $m)) {
134 $display_url = substr($display_url, strlen($m[1]) + 3);
135 }
136 }
137
138 $display_url = self::cff_autolink_label($display_url, $limit);
139
140
141 //
142 // add the url
143 //
144
145 if ($display_url != $link_url && !preg_match('@title=@msi', $tagfill) && $auto_title) {
146 $display_quoted = preg_quote($display_url, '!');
147
148 if (!preg_match("!^(http|https)://{$display_quoted}$!i", $link_url)) {
149 $tagfill .= ' title="' . $link_url . '"';
150 }
151 }
152
153 $link_url_enc = HtmlSpecialChars($link_url);
154 $display_url_enc = HtmlSpecialChars($display_url);
155
156
157 if (substr($link_url_enc, 0, 4) !== "http") {
158 $link_url_enc = 'https://' . $link_url_enc;
159 }
160 $buffer .= "<a href=\"{$link_url_enc}\" rel='nofollow noopener noreferrer'>{$display_url_enc}</a>";
161 } else {
162 // echo "fail 3 at $cursor<br />\n";
163
164 $ok = 0;
165 $cursor += $fail_len;
166 $buffer .= $fail_text;
167 }
168 }
169 }
170
171 //
172 // add everything from the cursor to the end onto the buffer.
173 //
174
175 $buffer .= substr($text, $cursor);
176
177 return $buffer;
178 }
179
180
181 public static function cff_autolink_label($text, $limit)
182 {
183 if (!$limit) {
184 return $text;
185 }
186
187 if (strlen($text) > $limit) {
188 return substr($text, 0, $limit - 3) . '...';
189 }
190
191 return $text;
192 }
193
194
195 public static function cff_autolink_email($text = '', $tagfill = '')
196 {
197 $atom = '[^()<>@,;:\\\\".\\[\\]\\x00-\\x20\\x7f]+'; // from RFC822
198
199 // die($atom);
200
201 $text_l = StrToLower($text);
202 $cursor = 0;
203 $loop = 1;
204 $buffer = '';
205
206 while (($cursor < strlen($text)) && $loop) {
207 //
208 // find an '@' symbol
209 //
210
211 $ok = 1;
212 $pos = strpos($text_l, '@', $cursor);
213
214 if ($pos === false) {
215 $loop = 0;
216 $ok = 0;
217 } else {
218 $pre = substr($text, $cursor, $pos - $cursor);
219 $hit = substr($text, $pos, 1);
220 $post = substr($text, $pos + 1);
221
222 $fail_text = $pre . $hit;
223 $fail_len = strlen($fail_text);
224
225 // die("$pre::$hit::$post::$fail_text");
226
227 //
228 // substring found - first check to see if we're inside a link tag already...
229 //
230
231 $bits = preg_split("!</a>!i", $pre);
232 $last_bit = array_pop($bits);
233 if (preg_match("!<a\s!i", $last_bit)) {
234 // echo "fail 1 at $cursor<br />\n";
235
236 $ok = 0;
237 $cursor += $fail_len;
238 $buffer .= $fail_text;
239 }
240 }
241
242 //
243 // check backwards
244 //
245
246 if ($ok) {
247 if (preg_match("!($atom(\.$atom)*)\$!", $pre, $matches)) {
248 // move matched part of address into $hit
249
250 $len = strlen($matches[1]);
251 $plen = strlen($pre);
252
253 $hit = substr($pre, $plen - $len) . $hit;
254 $pre = substr($pre, 0, $plen - $len);
255 } else {
256 // echo "fail 2 at $cursor ($pre)<br />\n";
257
258 $ok = 0;
259 $cursor += $fail_len;
260 $buffer .= $fail_text;
261 }
262 }
263
264 //
265 // check forwards
266 //
267
268 if ($ok) {
269 if (preg_match("!^($atom(\.$atom)*)!", $post, $matches)) {
270 // move matched part of address into $hit
271
272 $len = strlen($matches[1]);
273
274 $hit .= substr($post, 0, $len);
275 $post = substr($post, $len);
276 } else {
277 // echo "fail 3 at $cursor ($post)<br />\n";
278
279 $ok = 0;
280 $cursor += $fail_len;
281 $buffer .= $fail_text;
282 }
283 }
284
285 //
286 // commit
287 //
288
289 if ($ok) {
290 $cursor += strlen($pre) + strlen($hit);
291 $buffer .= $pre;
292 $buffer .= "<a href=\"mailto:$hit\"$tagfill>$hit</a>";
293 }
294 }
295
296 //
297 // add everything from the cursor to the end onto the buffer.
298 //
299
300 $buffer .= substr($text, $cursor);
301
302 return $buffer;
303 }
304 }
305