1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Plugin\BbcodeParser\src\Lib\Helper; |
14
|
|
|
|
15
|
|
|
use Cake\Validation\Validator; |
16
|
|
|
use Cake\View\Helper\UrlHelper; |
17
|
|
|
use MailObfuscator\View\Helper\MailObfuscatorHelper; |
18
|
|
|
use Saito\DomainParser; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @property MailObfuscatorHelper $MailObfuscator |
22
|
|
|
* @property UrlHelper $Url |
23
|
|
|
*/ |
24
|
|
|
trait UrlParserTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Enforces HTTPS-scheme on URL |
28
|
|
|
* |
29
|
|
|
* Applied if: |
30
|
|
|
* - current host runs on HTTPS |
31
|
|
|
* |
32
|
|
|
* @param string $url URL |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
protected function _urlToHttps(string $url): string |
36
|
|
|
{ |
37
|
|
|
if (!env('HTTPS')) { |
38
|
|
|
return $url; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return str_replace('http://', 'https://', $url); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generate email link |
46
|
|
|
* |
47
|
|
|
* @param string $url address |
48
|
|
|
* @param string $text title |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
protected function _email($url, $text = null) |
53
|
|
|
{ |
54
|
|
|
if (empty($text)) { |
55
|
|
|
$text = null; |
56
|
|
|
} |
57
|
|
|
$url = str_replace('mailto:', '', $url); |
58
|
|
|
|
59
|
|
|
return $this->MailObfuscator->link($url, $text); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Generate URL |
64
|
|
|
* |
65
|
|
|
* @param string $url URL |
66
|
|
|
* @param string $text title |
67
|
|
|
* @param bool $label show label |
68
|
|
|
* @param bool $truncate trunctate |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
protected function _url($url, $text, $label = false, $truncate = false) |
74
|
|
|
{ |
75
|
|
|
// add http:// to URLs without protocol |
76
|
|
|
if (strpos($url, '://') === false) { |
77
|
|
|
// use Cakes Validation class to detect valid URL |
78
|
|
|
$validator = new Validator(); |
79
|
|
|
$validator->add('url', ['url' => ['rule' => 'url']]); |
80
|
|
|
$errors = $validator->errors(['url' => $url]); |
81
|
|
|
if (empty($errors)) { |
82
|
|
|
$url = 'http://' . $url; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$out = "<a href='$url' class=\"richtext-link"; |
87
|
|
|
if ($truncate) { |
88
|
|
|
$out .= ' truncate'; |
89
|
|
|
} |
90
|
|
|
$out .= "\">$text</a>"; |
91
|
|
|
$out = $this->_decorateTarget($out); |
92
|
|
|
|
93
|
|
|
// add domain info: `[url=domain.info]my link[/url]` -> `my link [domain.info]` |
94
|
|
|
if ($label !== false && $label !== 'none' && $label !== 'false') { |
95
|
|
|
if (!empty($url) && preg_match('/\<img\s*?src=/', $text) !== 1) { |
96
|
|
|
$host = DomainParser::domainAndTld($url); |
97
|
|
|
if ($host !== null && $host !== env('SERVER_NAME')) { |
98
|
|
|
$out .= ' <span class=\'richtext-linkInfo\'>[' . $host . ']</span>'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $out; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Adds target="_blank" to *all* external links in arbitrary string $string |
108
|
|
|
* |
109
|
|
|
* @param string $string string |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function _decorateTarget($string) |
114
|
|
|
{ |
115
|
|
|
$decorator = function ($matches) { |
116
|
|
|
$out = ''; |
117
|
|
|
$url = $matches[1]; |
118
|
|
|
|
119
|
|
|
// preventing error message for parse_url('http://'); |
120
|
|
|
if (substr($url, -3) === '://') { |
121
|
|
|
return $matches[0]; |
122
|
|
|
} |
123
|
|
|
$parsedUrl = parse_url($url); |
124
|
|
|
|
125
|
|
|
if (isset($parsedUrl['host'])) { |
126
|
|
|
if ($parsedUrl['host'] !== env('SERVER_NAME') && $parsedUrl['host'] !== "www." . env('SERVER_NAME')) { |
127
|
|
|
$out = " rel='external' target='_blank'"; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $matches[0] . $out; |
132
|
|
|
}; |
133
|
|
|
|
134
|
|
|
return preg_replace_callback( |
135
|
|
|
'#href=["\'](.*?)["\']#is', |
136
|
|
|
$decorator, |
137
|
|
|
$string |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Creates an URL to an uploaded file based on $id |
143
|
|
|
* |
144
|
|
|
* @param string $id currently name in uploads folder |
145
|
|
|
* @return string URL |
146
|
|
|
*/ |
147
|
|
|
protected function _linkToUploadedFile(string $id): string |
148
|
|
|
{ |
149
|
|
|
// @bogus, there's an user-config for that |
150
|
|
|
$root = '/useruploads/'; |
151
|
|
|
|
152
|
|
|
return $this->Url->build($root . $id, ['fullBase' => true]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|