Admin
4 years ago
Builder
4 years ago
Helpers
4 years ago
CFF_Autolink.php
4 years ago
CFF_Blocks.php
4 years ago
CFF_Cache.php
4 years ago
CFF_Education.php
4 years ago
CFF_Elementor_Base.php
4 years ago
CFF_Elementor_Widget.php
4 years ago
CFF_Error_Reporter.php
4 years ago
CFF_FB_Settings.php
4 years ago
CFF_Feed_Elementor_Control.php
4 years ago
CFF_Feed_Locator.php
4 years ago
CFF_Feed_Pro.php
4 years ago
CFF_GDPR_Integrations.php
4 years ago
CFF_Group_Posts.php
4 years ago
CFF_HTTP_Request.php
4 years ago
CFF_Oembed.php
4 years ago
CFF_Parse.php
4 years ago
CFF_Resizer.php
4 years ago
CFF_Response.php
4 years ago
CFF_Shortcode.php
4 years ago
CFF_Shortcode_Display.php
4 years ago
CFF_SiteHealth.php
4 years ago
CFF_Utils.php
4 years ago
CFF_View.php
4 years ago
Custom_Facebook_Feed.php
4 years ago
SB_Facebook_Data_Encryption.php
4 years ago
SB_Facebook_Data_Manager.php
4 years ago
CFF_Autolink.php
323 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Autolink |
| 4 | * |
| 5 | * |
| 6 | * @since 2.19 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed; |
| 10 | |
| 11 | class CFF_Autolink{ |
| 12 | |
| 13 | |
| 14 | static function cff_autolink($text, $link_color='', $span_tag = false, $limit=100, $tagfill='class="cff-break-word"', $auto_title = true){ |
| 15 | $text = self::cff_autolink_do($text, $link_color, '![a-z][a-z-]+://!i', $limit, $tagfill, $auto_title, $span_tag); |
| 16 | $text = self::cff_autolink_do($text, $link_color, '!(mailto|skype):!i', $limit, $tagfill, $auto_title, $span_tag); |
| 17 | $text = self::cff_autolink_do($text, $link_color, '!www\\.!i', $limit, $tagfill, $auto_title, 'http://', $span_tag); |
| 18 | return $text; |
| 19 | } |
| 20 | |
| 21 | static function cff_autolink_do($text, $link_color, $sub, $limit, $tagfill, $auto_title, $span_tag, $force_prefix=null){ |
| 22 | |
| 23 | $text_l = StrToLower($text); |
| 24 | $cursor = 0; |
| 25 | $loop = 1; |
| 26 | $buffer = ''; |
| 27 | |
| 28 | $autolink_options = [ |
| 29 | # Should http:// be visibly stripped from the front |
| 30 | # of URLs? |
| 31 | 'strip_protocols' => true, |
| 32 | ]; |
| 33 | |
| 34 | while (($cursor < strlen($text)) && $loop){ |
| 35 | |
| 36 | $ok = 1; |
| 37 | $matched = preg_match($sub, $text_l, $m, PREG_OFFSET_CAPTURE, $cursor); |
| 38 | |
| 39 | if (!$matched){ |
| 40 | |
| 41 | $loop = 0; |
| 42 | $ok = 0; |
| 43 | |
| 44 | }else{ |
| 45 | |
| 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 | |
| 65 | #echo "fail 1 at $cursor<br />\n"; |
| 66 | |
| 67 | $ok = 0; |
| 68 | $cursor += $fail_len; |
| 69 | $buffer .= $fail_text; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | # |
| 74 | # looks like a nice spot to autolink from - check the pre |
| 75 | # to see if there was whitespace before this match |
| 76 | # |
| 77 | |
| 78 | if ($ok){ |
| 79 | |
| 80 | if ($pre){ |
| 81 | if (!preg_match('![\s\(\[\{>]$!s', $pre)){ |
| 82 | |
| 83 | #echo "fail 2 at $cursor ($pre)<br />\n"; |
| 84 | |
| 85 | $ok = 0; |
| 86 | $cursor += $fail_len; |
| 87 | $buffer .= $fail_text; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | # |
| 93 | # we want to autolink here - find the extent of the url |
| 94 | # |
| 95 | |
| 96 | if ($ok){ |
| 97 | if (preg_match('/^([a-z0-9\-\.\/\-_%~!?=,:;&+*#@\(\)\$]+)/i', $post, $matches)){ |
| 98 | |
| 99 | $url = $hit.$matches[1]; |
| 100 | |
| 101 | $cursor += strlen($url) + strlen($pre_hit); |
| 102 | $buffer .= $pre_hit; |
| 103 | |
| 104 | $url = html_entity_decode($url); |
| 105 | |
| 106 | |
| 107 | # |
| 108 | # remove trailing punctuation from url |
| 109 | # |
| 110 | |
| 111 | while (preg_match('|[.,!;:?]$|', $url)){ |
| 112 | $url = substr($url, 0, strlen($url)-1); |
| 113 | $cursor--; |
| 114 | } |
| 115 | foreach (array('()', '[]', '{}') as $pair){ |
| 116 | $o = substr($pair, 0, 1); |
| 117 | $c = substr($pair, 1, 1); |
| 118 | if (preg_match("!^(\\$c|^)[^\\$o]+\\$c$!", $url)){ |
| 119 | $url = substr($url, 0, strlen($url)-1); |
| 120 | $cursor--; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | # |
| 126 | # nice-i-fy url here |
| 127 | # |
| 128 | |
| 129 | $link_url = $url; |
| 130 | $display_url = $url; |
| 131 | |
| 132 | if ($force_prefix) $link_url = $force_prefix.$link_url; |
| 133 | |
| 134 | if ($autolink_options['strip_protocols']){ |
| 135 | if (preg_match('!^(http|https)://!i', $display_url, $m)){ |
| 136 | |
| 137 | $display_url = substr($display_url, strlen($m[1])+3); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $display_url = self::cff_autolink_label($display_url, $limit); |
| 142 | |
| 143 | |
| 144 | # |
| 145 | # add the url |
| 146 | # |
| 147 | |
| 148 | if ($display_url != $link_url && !preg_match('@title=@msi',$tagfill) && $auto_title) { |
| 149 | |
| 150 | $display_quoted = preg_quote($display_url, '!'); |
| 151 | |
| 152 | if (!preg_match("!^(http|https)://{$display_quoted}$!i", $link_url)){ |
| 153 | |
| 154 | $tagfill .= ' title="'.$link_url.'"'; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $link_url_enc = HtmlSpecialChars($link_url); |
| 159 | $display_url_enc = HtmlSpecialChars($display_url); |
| 160 | |
| 161 | |
| 162 | if( substr( $link_url_enc, 0, 4 ) !== "http" ) $link_url_enc = 'http://' . $link_url_enc; |
| 163 | $buffer .= "<a href=\"{$link_url_enc}\" rel='nofollow noopener noreferrer'>{$display_url_enc}</a>"; |
| 164 | |
| 165 | |
| 166 | }else{ |
| 167 | #echo "fail 3 at $cursor<br />\n"; |
| 168 | |
| 169 | $ok = 0; |
| 170 | $cursor += $fail_len; |
| 171 | $buffer .= $fail_text; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | # |
| 178 | # add everything from the cursor to the end onto the buffer. |
| 179 | # |
| 180 | |
| 181 | $buffer .= substr($text, $cursor); |
| 182 | |
| 183 | return $buffer; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static function cff_autolink_label($text, $limit){ |
| 188 | |
| 189 | if (!$limit){ return $text; } |
| 190 | |
| 191 | if (strlen($text) > $limit){ |
| 192 | return substr($text, 0, $limit-3).'...'; |
| 193 | } |
| 194 | |
| 195 | return $text; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | static function cff_autolink_email($text, $tagfill=''){ |
| 200 | |
| 201 | $atom = '[^()<>@,;:\\\\".\\[\\]\\x00-\\x20\\x7f]+'; # from RFC822 |
| 202 | |
| 203 | #die($atom); |
| 204 | |
| 205 | $text_l = StrToLower($text); |
| 206 | $cursor = 0; |
| 207 | $loop = 1; |
| 208 | $buffer = ''; |
| 209 | |
| 210 | while(($cursor < strlen($text)) && $loop){ |
| 211 | |
| 212 | # |
| 213 | # find an '@' symbol |
| 214 | # |
| 215 | |
| 216 | $ok = 1; |
| 217 | $pos = strpos($text_l, '@', $cursor); |
| 218 | |
| 219 | if ($pos === false){ |
| 220 | |
| 221 | $loop = 0; |
| 222 | $ok = 0; |
| 223 | |
| 224 | }else{ |
| 225 | |
| 226 | $pre = substr($text, $cursor, $pos-$cursor); |
| 227 | $hit = substr($text, $pos, 1); |
| 228 | $post = substr($text, $pos + 1); |
| 229 | |
| 230 | $fail_text = $pre.$hit; |
| 231 | $fail_len = strlen($fail_text); |
| 232 | |
| 233 | #die("$pre::$hit::$post::$fail_text"); |
| 234 | |
| 235 | # |
| 236 | # substring found - first check to see if we're inside a link tag already... |
| 237 | # |
| 238 | |
| 239 | $bits = preg_split("!</a>!i", $pre); |
| 240 | $last_bit = array_pop($bits); |
| 241 | if (preg_match("!<a\s!i", $last_bit)){ |
| 242 | |
| 243 | #echo "fail 1 at $cursor<br />\n"; |
| 244 | |
| 245 | $ok = 0; |
| 246 | $cursor += $fail_len; |
| 247 | $buffer .= $fail_text; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | # |
| 252 | # check backwards |
| 253 | # |
| 254 | |
| 255 | if ($ok){ |
| 256 | if (preg_match("!($atom(\.$atom)*)\$!", $pre, $matches)){ |
| 257 | |
| 258 | # move matched part of address into $hit |
| 259 | |
| 260 | $len = strlen($matches[1]); |
| 261 | $plen = strlen($pre); |
| 262 | |
| 263 | $hit = substr($pre, $plen-$len).$hit; |
| 264 | $pre = substr($pre, 0, $plen-$len); |
| 265 | |
| 266 | }else{ |
| 267 | |
| 268 | #echo "fail 2 at $cursor ($pre)<br />\n"; |
| 269 | |
| 270 | $ok = 0; |
| 271 | $cursor += $fail_len; |
| 272 | $buffer .= $fail_text; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | # |
| 277 | # check forwards |
| 278 | # |
| 279 | |
| 280 | if ($ok){ |
| 281 | if (preg_match("!^($atom(\.$atom)*)!", $post, $matches)){ |
| 282 | |
| 283 | # move matched part of address into $hit |
| 284 | |
| 285 | $len = strlen($matches[1]); |
| 286 | |
| 287 | $hit .= substr($post, 0, $len); |
| 288 | $post = substr($post, $len); |
| 289 | |
| 290 | }else{ |
| 291 | #echo "fail 3 at $cursor ($post)<br />\n"; |
| 292 | |
| 293 | $ok = 0; |
| 294 | $cursor += $fail_len; |
| 295 | $buffer .= $fail_text; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | # |
| 300 | # commit |
| 301 | # |
| 302 | |
| 303 | if ($ok) { |
| 304 | |
| 305 | $cursor += strlen($pre) + strlen($hit); |
| 306 | $buffer .= $pre; |
| 307 | $buffer .= "<a href=\"mailto:$hit\"$tagfill>$hit</a>"; |
| 308 | |
| 309 | } |
| 310 | |
| 311 | } |
| 312 | |
| 313 | # |
| 314 | # add everything from the cursor to the end onto the buffer. |
| 315 | # |
| 316 | |
| 317 | $buffer .= substr($text, $cursor); |
| 318 | |
| 319 | return $buffer; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | } |