-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathExternalLinks.php
181 lines (154 loc) · 5.34 KB
/
ExternalLinks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace lo\plugins\core\extralinks;
use lo\plugins\BasePlugin;
use Yii;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\Response;
/**
* Plugin Name: External Links
* Plugin URI: https://github.com/loveorigami/yii2-plugins-system/tree/master/src/core/extralinks
* Version: 2.0
* Description: AutoCorrect external links after rendering html page
* Author: Andrey Lukyanov
* Author URI: https://github.com/loveorigami
*/
class ExternalLinks extends BasePlugin
{
/**
* @var array
*/
public static $configResponse = [
'noReplaceLinksOnDomains' => [
//'google.com'
],
'noReplaceLinksOnSubDomains' => [
//'google.com'
],
'noReplaceLocalDomain' => true,
'redirectRoute' => '/externallinks/redirect',
'redirectRouteParam' => 'url',
'enabledB64Encode' => true,
];
/**
* @var array
*/
public static $configController = [
'redirectRouteParam' => 'url',
'enabledB64Encode' => true,
];
/**
* @return array
*/
public static function events()
{
return [
Response::class => [
Response::EVENT_AFTER_PREPARE => ['parse', self::$configResponse],
],
RedirectController::class => [
RedirectController::EVENT_LOAD_CONFIG => ['loadConfig', self::$configController],
],
];
}
/**
* @var array
*/
protected static $_config = [];
/**
* @param Event $event
*/
public static function parse($event)
{
/** @var $response Response */
$response = $event->sender;
$request = Yii::$app->request;
if (!$request->isAjax && !$request->isPjax && $response->format == Response::FORMAT_HTML) {
Yii::beginProfile('ExternalLinks');
self::initConfig($event->data);
$content = $response->content;
$matches = [];
if (preg_match_all("/<[Aa][\s]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\s]*([^ \"'>\s#]+)[^>]*>/", $content, $matches)) {
if (isset($matches[1])) {
$content = self::parseContent($content, $matches[1]);
}
};
$response->content = $content;
Yii::endProfile('ExternalLinks');
}
}
/**
* @param RedirectEvent $event
*/
public static function loadConfig($event)
{
$event->config = ArrayHelper::merge(self::$configController, $event->data);
}
/**
* @param $content
* @param array $links
* @return string
*/
protected static function parseContent($content, array $links)
{
foreach ($links as $link) {
//Относительные ссылки пропускать
if (Url::isRelative($link)) {
continue;
}
if ($dataLink = parse_url($link)) {
// Для этого хоста не нужно менять ссылку
$host = ArrayHelper::getValue($dataLink, 'host');
if (in_array($host, self::$_config['noReplaceLinksOnDomains'])) {
continue;
}
// Не заменять ссылки для субдоменов из списка
$noReplace = false;
foreach (self::$_config['noReplaceLinksOnSubDomains'] as $sub) {
$pos = strpos($host, $sub);
//echo $host . PHP_EOL;
//echo $sub . "\r\n";
if ($pos !== false) {
$noReplace = true;
continue;
}
}
if ($noReplace) {
continue;
}
}
$linkForUrl = $link;
if (self::$_config['enabledB64Encode']) {
$linkForUrl = base64_encode($link);
}
$newUrl = Url::to([self::$_config['redirectRoute'], self::$_config['redirectRouteParam'] => $linkForUrl]);
//replacing references only after <body
$bodyPosition = strpos($content, '<body');
$headerContent = substr($content, 0, $bodyPosition);
$bodyContent = substr($content, $bodyPosition, strlen($content));
$replaceUrl = 'href="' . $newUrl . '"';
$bodyContent = str_replace('href="' . $link . '"', $replaceUrl, $bodyContent);
$replaceUrl = 'href=\'' . $newUrl . '\'';
$bodyContent = str_replace('href=\'' . $link . '\'', $replaceUrl, $bodyContent);
$resultContent = $headerContent . $bodyContent;
$content = $resultContent;
}
return $content;
}
/**
* @param array $data
*/
protected static function initConfig(array $data)
{
$request = Yii::$app->request;
self::$_config = ArrayHelper::merge(self::$configResponse, $data);
if (self::$_config['noReplaceLocalDomain'] && $request->hostInfo) {
if ($dataLink = parse_url($request->hostInfo)) {
//Для этого хоста не нужно менять ссылку
$host = ArrayHelper::getValue($dataLink, 'host');
self::$_config['noReplaceLinksOnDomains'][] = $host;
}
}
}
}