Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
robotsfile.php
1<?php
8namespace Bitrix\Seo;
9
12
13class 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
putContents($data, $flags=self::REWRITE)
Definition file.php:65
addSectionRule($section, $rule)
getRules($rule, $section=' *')
addRule($rule, $section=' *', $bCheckUnique=true)