Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
robotsfile.php
1
<?php
8
namespace
Bitrix\Seo
;
9
10
use
Bitrix\Main\IO
;
11
use
Bitrix\Main\SiteTable
;
12
13
class
RobotsFile
extends
IO\File
14
{
15
const
ROBOTS_FILE_NAME
=
'robots.txt'
;
16
const
SECTION_RULE
=
'User-Agent'
;
17
const
SITEMAP_RULE
=
'Sitemap'
;
18
24
const
EMPTY_DISALLOW_RULE
=
'Disallow: # empty Disallow instruction SHOULD be there'
;
25
26
protected
$siteId
=
''
;
27
protected
$documentRoot
;
28
29
protected
$robotsFile
=
null
;
30
31
protected
$contents
= array();
32
protected
$bLoaded
=
false
;
33
34
public
function
__construct
(
$siteId
)
35
{
36
$this->siteId =
$siteId
;
37
$this->documentRoot = SiteTable::getDocumentRoot($this->siteId);
38
39
parent::__construct(
IO
\Path::combine($this->documentRoot, self::ROBOTS_FILE_NAME));
40
}
41
42
public
function
addRule
($rule, $section =
'*'
, $bCheckUnique =
true
)
43
{
44
$this->
load
();
45
if
($bCheckUnique)
46
{
47
$strRule = ToUpper($this->
getRuleText
($rule));
48
$arRules = $this->
getSection
($section);
49
foreach
($arRules as $existingRule)
50
{
51
$strExistingRule = ToUpper($this->
getRuleText
($existingRule));
52
if
($strRule == $strExistingRule)
53
{
54
return
true
;
55
}
56
}
57
}
58
59
$this->
addSectionRule
($section, $rule);
60
61
$this->
save
();
62
}
63
64
public
function
getRuleText
($rule)
65
{
66
return
implode(
': '
, $rule);
67
}
68
69
public
function
parseRule
($strRule)
70
{
71
if
(mb_substr($strRule, 0, 1) ==
'#'
)
72
{
73
return
array($strRule);
74
}
75
else
76
{
77
return
preg_split(
"/:\s*/"
, $strRule, 2);
78
}
79
}
80
81
public
function
getRules
($rule, $section =
'*'
)
82
{
83
$this->
load
();
84
$arRules = array();
85
if
(isset($this->contents[$section]))
86
{
87
$rule = ToUpper($rule);
88
foreach
($this->contents[$section] as $arRule)
89
{
90
if
(ToUpper($arRule[0]) == $rule)
91
{
92
$arRules[] = $arRule;
93
}
94
}
95
}
96
return
$arRules;
97
}
98
99
protected
function
getSection
($section)
100
{
101
$section = ToUpper($section);
102
foreach
($this->contents as $currentAgent => $arRules)
103
{
104
if
(ToUpper($currentAgent) == $section)
105
{
106
return
$arRules;
107
}
108
}
109
110
return
array();
111
}
112
113
protected
function
addSectionRule
($section, $rule)
114
{
115
$section = ToUpper($section);
116
foreach
($this->contents as $currentAgent => $arRules)
117
{
118
if
(ToUpper($currentAgent) == $section)
119
{
120
$this->contents[$section][] = $rule;
121
return
;
122
}
123
}
124
125
$this->contents[$section] = array($rule);
126
}
127
128
protected
function
load
()
129
{
130
if
($this->
isExists
() && !$this->bLoaded)
131
{
132
$contents
= $this->
getContents
();
133
$arLines = preg_split(
"/\\n+/"
,
$contents
);
134
$currentAgent =
''
;
135
if
(count($arLines) > 0)
136
{
137
$strSectionCompare = ToUpper(self::SECTION_RULE);
138
foreach
($arLines as $line)
139
{
140
$line = trim($line);
141
142
if
($line <>
''
)
143
{
144
$rule = $this->
parseRule
($line);
145
if
(ToUpper($rule[0]) == $strSectionCompare)
146
{
147
$currentAgent = $rule[1];
148
}
149
elseif ($currentAgent !=
''
)
150
{
151
152
if
(!is_array($this->contents[$currentAgent]))
153
{
154
$this->contents[$currentAgent] = array();
155
}
156
157
$this->contents[$currentAgent][] = $rule;
158
}
159
}
160
}
161
}
162
}
163
$this->bLoaded =
true
;
164
}
165
166
protected
function
save
()
167
{
168
if
(count($this->contents) > 0)
169
{
170
$strContent =
''
;
171
$nn =
"\r\n"
;
172
173
foreach
($this->contents as $currentAgent => $arRules)
174
{
175
if
(is_array($arRules) && count($arRules) > 0)
176
{
177
$strContent .=
178
($strContent ==
''
?
''
: $nn)
179
.$this->
getRuleText
(array(self::SECTION_RULE, $currentAgent)).$nn;
180
181
foreach
($arRules as $rule)
182
{
183
$strContent .= $this->
getRuleText
($rule).$nn;
184
}
185
}
186
}
187
188
$this->
putContents
($strContent);
189
}
190
}
191
}
192
Bitrix\Main\IO\File
Definition
file.php:7
Bitrix\Main\IO\File\getContents
getContents()
Definition
file.php:57
Bitrix\Main\IO\File\putContents
putContents($data, $flags=self::REWRITE)
Definition
file.php:65
Bitrix\Main\IO\File\isExists
isExists()
Definition
file.php:51
Bitrix\Main\SiteTable
Definition
site.php:32
Bitrix\Seo\RobotsFile
Definition
robotsfile.php:14
Bitrix\Seo\RobotsFile\addSectionRule
addSectionRule($section, $rule)
Definition
robotsfile.php:113
Bitrix\Seo\RobotsFile\$robotsFile
$robotsFile
Definition
robotsfile.php:29
Bitrix\Seo\RobotsFile\SITEMAP_RULE
const SITEMAP_RULE
Definition
robotsfile.php:17
Bitrix\Seo\RobotsFile\$bLoaded
$bLoaded
Definition
robotsfile.php:32
Bitrix\Seo\RobotsFile\getRuleText
getRuleText($rule)
Definition
robotsfile.php:64
Bitrix\Seo\RobotsFile\$documentRoot
$documentRoot
Definition
robotsfile.php:27
Bitrix\Seo\RobotsFile\parseRule
parseRule($strRule)
Definition
robotsfile.php:69
Bitrix\Seo\RobotsFile\getRules
getRules($rule, $section=' *')
Definition
robotsfile.php:81
Bitrix\Seo\RobotsFile\EMPTY_DISALLOW_RULE
const EMPTY_DISALLOW_RULE
Definition
robotsfile.php:24
Bitrix\Seo\RobotsFile\load
load()
Definition
robotsfile.php:128
Bitrix\Seo\RobotsFile\ROBOTS_FILE_NAME
const ROBOTS_FILE_NAME
Definition
robotsfile.php:15
Bitrix\Seo\RobotsFile\getSection
getSection($section)
Definition
robotsfile.php:99
Bitrix\Seo\RobotsFile\__construct
__construct($siteId)
Definition
robotsfile.php:34
Bitrix\Seo\RobotsFile\$contents
$contents
Definition
robotsfile.php:31
Bitrix\Seo\RobotsFile\$siteId
$siteId
Definition
robotsfile.php:26
Bitrix\Seo\RobotsFile\addRule
addRule($rule, $section=' *', $bCheckUnique=true)
Definition
robotsfile.php:42
Bitrix\Seo\RobotsFile\SECTION_RULE
const SECTION_RULE
Definition
robotsfile.php:16
Bitrix\Seo\RobotsFile\save
save()
Definition
robotsfile.php:166
Bitrix\Main\IO
Definition
directory.php:2
Bitrix\Seo
modules
seo
lib
robotsfile.php
Создано системой
1.10.0