|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* @name ElkArte Forum |
|
6
|
|
|
* @copyright ElkArte Forum contributors |
|
7
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
|
8
|
|
|
* |
|
9
|
|
|
* This software is a derived product, based on: |
|
10
|
|
|
* |
|
11
|
|
|
* Simple Machines Forum (SMF) |
|
12
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
|
13
|
|
|
* license: BSD, See included LICENSE.TXT for terms and conditions. |
|
14
|
|
|
* |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace BBC; |
|
19
|
|
|
|
|
20
|
|
|
class Autolink |
|
21
|
|
|
{ |
|
22
|
|
|
protected $bbc; |
|
23
|
|
|
protected $url_enabled; |
|
24
|
|
|
protected $email_enabled; |
|
25
|
|
|
protected $possible_link; |
|
26
|
|
|
protected $possible_email; |
|
27
|
|
|
protected $search; |
|
28
|
|
|
protected $replace; |
|
29
|
|
|
protected $email_search; |
|
30
|
|
|
protected $email_replace; |
|
31
|
|
|
|
|
32
|
1 |
|
public function __construct(Codes $bbc) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->bbc = $bbc; |
|
35
|
|
|
|
|
36
|
1 |
|
$this->url_enabled = !$this->bbc->isDisabled('url'); |
|
37
|
1 |
|
$this->email_enabled = !$this->bbc->isDisabled('email'); |
|
38
|
|
|
|
|
39
|
1 |
|
$this->load(); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function parse(&$data) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->hasLinks($data)) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->parseLinks($data); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($this->hasEmails($data)) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->parseEmails($data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
call_integration_hook('integrate_autolink_area', array(&$data, $this->bbc)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function hasLinks($data) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->hasPossibleLink() && (strpos($data, '://') !== false || strpos($data, 'www.') !== false); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Parse any URLs.... have to get rid of the @ problems some things cause... stupid email addresses. |
|
63
|
|
|
public function parseLinks(&$data) |
|
64
|
|
|
{ |
|
65
|
|
|
// Switch out quotes really quick because they can cause problems. |
|
66
|
|
|
$data = str_replace(array(''', ' ', '"', '"', '<'), array('\'', "\xC2\xA0", '>">', '<"<', '<lt<'), $data); |
|
67
|
|
|
|
|
68
|
|
|
$result = preg_replace($this->search, $this->replace, $data); |
|
69
|
|
|
|
|
70
|
|
|
// Only do this if the preg survives. |
|
71
|
|
|
if (is_string($result)) |
|
72
|
|
|
{ |
|
73
|
|
|
$data = $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Switch those quotes back |
|
77
|
|
|
$data = str_replace(array('\'', "\xC2\xA0", '>">', '<"<', '<lt<'), array(''', ' ', '"', '"', '<'), $data); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function hasEmails($data) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->hasPossibleEmail() && strpos($data, '@') !== false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function parseEmails(&$data) |
|
86
|
|
|
{ |
|
87
|
|
|
// Next, emails... |
|
88
|
|
|
$data = preg_replace($this->email_search, $this->email_replace, $data); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Load the autolink regular expression to be used in autoLink() |
|
93
|
|
|
*/ |
|
94
|
1 |
|
protected function load() |
|
95
|
|
|
{ |
|
96
|
|
|
$search_url = array( |
|
97
|
1 |
|
'~(?<=[\s>\.(;\'"]|^)((?:http|https)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\p{L}\p{N}\-_\~%\.@!,\?&;=#(){}+:\'\\\\]*)*[/\p{L}\p{N}\-_\~%@\?;=#}\\\\])~ui', |
|
98
|
|
|
'~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\p{L}\p{N}\-_\~%\.@!,\?&;=#(){}+:\'\\\\]*)*[/\p{L}\p{N}\-_\~%@\?;=#}\\\\])~ui' |
|
99
|
1 |
|
); |
|
100
|
|
|
$replace_url = array( |
|
101
|
|
|
//'[url_auto=$1]$1[/url_auto]', |
|
102
|
|
|
//'[url_auto=$1]$1[/url_auto]', |
|
103
|
1 |
|
'[url]$1[/url]', |
|
104
|
1 |
|
'[url=http://$1]$1[/url]', |
|
105
|
1 |
|
); |
|
106
|
|
|
|
|
107
|
|
|
$search_email = array( |
|
108
|
1 |
|
'~(?<=[\?\s\x{A0}\[\]()*\\\;>]|^)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?,\s\x{A0}\[\]()*\\\]|$|<br />| |>|<|"|'|\.(?:\.|;| |\s|$|<br />))~u', |
|
109
|
1 |
|
'~(?<=<br />)([\w\-\.]{1,80}@[\w\-]+\.[\w\-\.]+[\w\-])(?=[?\.,;\s\x{A0}\[\]()*\\\]|$|<br />| |>|<|"|')~u', |
|
110
|
1 |
|
); |
|
111
|
|
|
$replace_email = array( |
|
112
|
|
|
//'[email_auto]$1[/email_auto]', |
|
113
|
|
|
//'[email_auto]$1[/email_auto]', |
|
114
|
1 |
|
'[email]$1[/email]', |
|
115
|
1 |
|
'[email]$1[/email]', |
|
116
|
1 |
|
); |
|
117
|
|
|
|
|
118
|
1 |
|
call_integration_hook('integrate_autolink_load', array(&$search_url, &$replace_url, &$search_email, &$replace_email, $this->bbc)); |
|
119
|
|
|
|
|
120
|
1 |
|
$this->search = $search_url; |
|
121
|
1 |
|
$this->replace = $replace_url; |
|
122
|
|
|
|
|
123
|
1 |
|
if (empty($search_url) || empty($replace_url)) |
|
124
|
1 |
|
{ |
|
125
|
|
|
$this->url_enabled = false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
$this->email_search = $search_email; |
|
129
|
1 |
|
$this->email_replace = $replace_email; |
|
130
|
|
|
|
|
131
|
1 |
|
if (empty($search_email) || empty($replace_email)) |
|
132
|
1 |
|
{ |
|
133
|
|
|
$this->email_enabled = false; |
|
134
|
|
|
} |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function setPossibleAutolink($message) |
|
138
|
|
|
{ |
|
139
|
|
|
$possible_link = $this->url_enabled && (strpos($message, '://') !== false || strpos($message, 'www.') !== false); |
|
140
|
|
|
$possible_email = $this->email_enabled && strpos($message, '@') !== false; |
|
141
|
|
|
|
|
142
|
|
|
// Your autolink integration might use something like tel.123456789.call. This makes that possible. |
|
143
|
|
|
call_integration_hook('integrate_possible_autolink', array(&$possible_link, &$possible_email)); |
|
144
|
|
|
|
|
145
|
|
|
$this->possible_link = $possible_link; |
|
146
|
|
|
$this->possible_email = $possible_email; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function hasPossible() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->hasPossibleLink() || $this->hasPossibleEmail(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function hasPossibleLink() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->possible_link; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function hasPossibleEmail() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->possible_email; |
|
162
|
|
|
} |
|
163
|
|
|
} |