-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalLinksComponent.php
161 lines (136 loc) · 5.22 KB
/
ExternalLinksComponent.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
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 10.02.2016
*/
namespace skeeks\yii2\externalLinks;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\Application;
use yii\web\Response;
use yii\web\View;
/**
* Class ExternalLinksComponent
* @package skeeks\yii2\externalLinks
*/
class ExternalLinksComponent extends Component implements BootstrapInterface
{
const EVENT_BEFORE_PROCESSING = 'beforeProcessing';
/**
* @var bool
*/
public $enabled = true;
/**
* Do not change the links in which there are domain names
* @var array
*/
public $noReplaceLinksOnDomains = [
//'skeeks.com',
//'www.skeeks.com',
];
/**
* @var bool Do not change absolute references to the domain name obtained from the information \Yii::$app->request->hostInfo
*/
public $noReplaceLocalDomain = true;
/**
* The base of the new url
* @var string
*/
public $backendRoute = '/externallinks/redirect/redirect';
/**
* @var string
*/
public $backendRouteParam = 'url';
/**
* @var bool Include links in the coding b64_encode
*/
public $enabledB64Encode = true;
/**
* @param \yii\web\Application $app
*/
public function bootstrap($app)
{
if ($app instanceof Application)
{
$app->response->on(Response::EVENT_AFTER_PREPARE, function(Event $e) use ($app)
{
/**
* @var $response Response
*/
$response = $e->sender;
$this->trigger(self::EVENT_BEFORE_PROCESSING);
if ($this->enabled && !$app->request->isAjax && !$app->request->isPjax && $app->response->format == Response::FORMAT_HTML)
{
\Yii::beginProfile('ExternalLinks');
$content = $response->content;
$this->initReplaceLinks();
$matches = [];
if (preg_match_all("/<[Aa][\s]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\s]*([^ \"'>\s#]+)[^>]*>/", (string) $content, $matches))
{
if (isset($matches[1]))
{
foreach ($matches[1] as $link)
{
//Относительные ссылки пропускать
if (Url::isRelative($link))
{
continue;
}
if ($dataLink = parse_url($link))
{
//Для этого хоста не нужно менять ссылку
$host = ArrayHelper::getValue($dataLink, 'host');
if (in_array($host, $this->noReplaceLinksOnDomains))
{
continue;
}
}
$linkForUrl = $link;
if ($this->enabledB64Encode)
{
$linkForUrl = base64_encode($link);
}
$newUrl = Url::to([$this->backendRoute, $this->backendRouteParam => $linkForUrl]);
//replacing references only after <body
$bodyPosition = strpos($content, '<body');
$headerContent = substr($content, 0, $bodyPosition);
$bodyContent = substr($content, $bodyPosition, strlen($content));
$resultContent = '';
$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;
}
}
}
$response->content = $content;
\Yii::endProfile('ExternalLinks');
}
});
}
}
/**
* @return $this
*/
protected function initReplaceLinks()
{
if ($this->noReplaceLocalDomain && \Yii::$app->request->hostInfo)
{
if ($dataLink = parse_url(\Yii::$app->request->hostInfo))
{
//Для этого хоста не нужно менять ссылку
$host = ArrayHelper::getValue($dataLink, 'host');
$this->noReplaceLinksOnDomains[] = $host;
}
}
return $this;
}
}