-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
207 lines (171 loc) · 5.88 KB
/
Module.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace wdmg\robots;
/**
* Yii2 Robots.txt
*
* @category Module
* @version 1.1.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-robots
* @copyright Copyright (c) 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
use wdmg\helpers\ArrayHelper;
use wdmg\robots\models\Rules;
use Yii;
use wdmg\base\BaseModule;
use wdmg\votes\components\Votes;
use yii\base\InvalidConfigException;
/**
* Votes module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\robots\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "list/index";
/**
* @var string, the name of module
*/
public $name = "Robots.txt";
/**
* @var string, the description of module
*/
public $description = "Generating and edit the `robots.txt` file";
/**
* @var string the path to webroot `robots.txt` file
*/
public $robotsWebRoot = "@webroot/robots.txt";
/**
* @var string the module version
*/
private $version = "1.1.0";
/**
* @var integer, priority of initialization
*/
private $priority = 10;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// Set version of current module
$this->setVersion($this->version);
// Set priority of current module
$this->setPriority($this->priority);
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = null)
{
$items = [
'label' => $this->name,
'url' => [$this->routePrefix . '/'. $this->id],
'icon' => 'fa fa-robot',
'active' => in_array(\Yii::$app->controller->module->id, [$this->id])
];
if (!is_null($options)) {
if (isset($options['count'])) {
$items['label'] .= '<span class="badge badge-default float-right">' . $options['count'] . '</span>';
unset($options['count']);
}
if (is_array($options))
$items = ArrayHelper::merge($items, $options);
}
return $items;
}
/**
* {@inheritdoc}
*/
public function bootstrap($app)
{
parent::bootstrap($app);
if (isset(Yii::$app->params["robots.robotsWebRoot"]))
$this->robotsWebRoot = Yii::$app->params["robots.robotsWebRoot"];
if (!is_string($this->robotsWebRoot))
throw new InvalidConfigException("Module property `robotsWebRoot` must be a string.");
if (!$this->isConsole()) {
$model = new Rules();
\yii\base\Event::on(get_class($model), $model::EVENT_AFTER_INSERT, function ($event) use ($model) {
$this->genRobotsTxt();
});
\yii\base\Event::on(get_class($model), $model::EVENT_AFTER_UPDATE, function ($event) use ($model) {
$this->genRobotsTxt();
});
\yii\base\Event::on(get_class($model), $model::EVENT_AFTER_DELETE, function ($event) use ($model) {
$this->genRobotsTxt();
});
}
}
public function genRobotsTxt() {
$model = new Rules();
$rules = $model::getPublished(true);
$list = ArrayHelper::map($rules, 'rule', 'mode', 'robot');
$list = array_reverse($list);
$output = '';
if (is_countable($list)) {
list($host, $delay, $sitemap) = '';
foreach ($list as $robot => $items) {
$output .= "User-agent: " . strval($robot) . "\r\n";
foreach ($items as $rule => $mode) {
if ($mode == $model::RULE_MODE_ALLOW)
$output .= "Allow: " . strval($rule) . "\r\n";
elseif ($mode == $model::RULE_MODE_DISALLOW)
$output .= "Disallow: " . strval($rule) . "\r\n";
elseif ($mode == $model::RULE_MODE_CLEAN)
$output .= "Clean-Param: " . strval($rule) . "\r\n";
elseif ($mode == $model::RULE_MODE_HOST)
$host .= "Host: " . strval($rule) . "\r\n";
elseif ($mode == $model::RULE_MODE_DELAY)
$delay .= "Crawl-delay: " . strval($rule) . "\r\n";
elseif ($mode == $model::RULE_MODE_SITEMAP)
$sitemap .= "Sitemap: " . strval($rule) . "\r\n";
}
$output .= "\r\n";
}
if (!empty($host))
$output .= $host;
if (!empty($delay))
$output .= $delay;
if (!empty($sitemap))
$output .= $sitemap;
}
$path = Yii::getAlias($this->robotsWebRoot);
if (!empty($output)) {
if (file_put_contents($path, $output)) {
if (!$this->isConsole()) {
Yii::$app->getSession()->setFlash(
'success',
Yii::t('app/modules/robots', 'Robots.txt has been successfully regenerated!')
);
}
return true;
} else {
if (!$this->isConsole()) {
Yii::$app->getSession()->setFlash(
'danger',
Yii::t('app/modules/robots', 'An error occurred while saving `robots.txt` file.')
);
}
}
}
return false;
}
public function getRobotsTxt() {
$path = Yii::getAlias($this->robotsWebRoot);
if (file_exists($path) && is_writable($path)) {
$handle = fopen($path, 'r');
$output = fread($handle, filesize($path));
fclose($handle);
return $output;
}
}
}