/
urls.php
192 lines (171 loc) · 8.79 KB
/
urls.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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Seo;
use Concrete\Core\Form\Service\Form;
use Concrete\Core\Page\Controller\DashboardSitePageController;
use Concrete\Core\Service\Manager\ServiceManager;
use Concrete\Core\Url\UrlImmutable;
use Punic\Misc;
class Urls extends DashboardSitePageController
{
protected const PRETTYURLSTATE_NOT_RECOGNIZED = 1;
protected const PRETTYURLSTATE_NOT_READABLE = 2;
protected const PRETTYURLSTATE_NOT_NEEDED = 3;
protected const PRETTYURLSTATE_NOT_WRITABLE = 4;
protected const PRETTYURLSTATE_UPDATED = 5;
public function view()
{
$globalConfig = $this->app->make('config');
$siteConfig = $this->getSite()->getConfigRepository();
$this->set('canonicalUrl', (string) $siteConfig->get('seo.canonical_url'));
$this->set('canonicalUrlAlternative', (string) $siteConfig->get('seo.canonical_url_alternative'));
$this->set('redirectToCanonicalUrl', (bool) $globalConfig->get('concrete.seo.redirect_to_canonical_url'));
$this->set('urlRewriting', (bool) $globalConfig->get('concrete.seo.url_rewriting'));
$this->set('canonicalTag', (bool) $siteConfig->get('seo.canonical_tag.enabled'));
}
public function save_urls()
{
$post = $this->request->request;
if (!$this->token->validate('save_urls')) {
$this->error->add($this->token->getErrorMessage());
}
$canonicalUrl = $this->getPostedCanonicalUrl('canonical_url');
$canonicalUrlAlternative = $this->getPostedCanonicalUrl('canonical_url_alternative');
$urlRewriting = (bool) $post->get('url_rewriting');
if ($this->error->has()) {
return $this->view();
}
$globalConfig = $this->app->make('config');
$siteConfig = $this->getSite()->getConfigRepository();
$siteConfig->save('seo.canonical_url', $canonicalUrl);
$siteConfig->save('seo.canonical_url_alternative', $canonicalUrlAlternative);
$globalConfig->save('concrete.seo.redirect_to_canonical_url', (bool) $post->get('redirect_to_canonical_url'));
$siteConfig->save('seo.canonical_tag.enabled', (bool) $post->get('canonical_tag'));
$globalConfig->save('concrete.seo.url_rewriting', $urlRewriting);
$prettyUrlState = $this->applyUrlRewriting($urlRewriting);
$this->flash('success', h('Settings Saved.') . '<br />' . $this->getPrettyUrlStateMessage($urlRewriting, $prettyUrlState), true);
return $this->buildRedirect($this->action(''));
}
protected function getPostedCanonicalUrl(string $name): string
{
$value = trim($this->request->request->get($name, ''));
if ($value === '') {
return '';
}
if (!preg_match('_^https?://._i', $value)) {
$this->error->add(t('The URL %1$s is not valid (it must start with %2$s or %3$s)', $value, 'http://', 'https://'));
return '';
}
try {
$normalizedValue = (string) UrlImmutable::createFromUrl($value);
} catch (\RuntimeException $x) {
$this->error->add(t('The URL %s is not valid.', $value));
return '';
}
return $normalizedValue;
}
protected function applyUrlRewriting(bool $enabled): int
{
$manager = $this->app->make(ServiceManager::class);
$services = $manager->getActiveServices();
if ($services === []) {
return static::PRETTYURLSTATE_NOT_RECOGNIZED;
}
$service = $services[0];
if (!$service->getStorage()->canRead()) {
return static::PRETTYURLSTATE_NOT_READABLE;
}
$rule = $service->getGenerator()->getRule('pretty_urls');
if ($rule === null) {
return static::PRETTYURLSTATE_NOT_NEEDED;
}
$configuration = $service->getStorage()->read();
if ($service->getConfigurator()->hasRule($configuration, $rule) === $enabled) {
return static::PRETTYURLSTATE_NOT_NEEDED;
}
if ($service->getStorage()->canWrite() === false) {
return static::PRETTYURLSTATE_NOT_WRITABLE;
}
if ($enabled) {
$configuration = $service->getConfigurator()->addRule($configuration, $rule);
} else {
$configuration = $service->getConfigurator()->removeRule($configuration, $rule);
}
$service->getStorage()->write($configuration);
return static::PRETTYURLSTATE_UPDATED;
}
protected function getPrettyUrlStateMessage(bool $urlRewriting, int $state): string
{
switch ($state) {
case static::PRETTYURLSTATE_NOT_RECOGNIZED:
$codes = [];
$manager = $this->app->make(ServiceManager::class);
foreach ($manager->getAllServices() as $service) {
$rule = $service->getGenerator()->getRule('pretty_urls');
if ($rule !== null) {
if (isset($codes[$rule->getCode()])) {
$codes[$rule->getCode()][] = $service->getName();
} else {
$codes[$rule->getCode()] = [$service->getName()];
}
}
}
$message = h(t('It was not possible to detect your server kind.'));
if ($urlRewriting) {
$message .= ' ' . h(t("Here's the configuration section for every supported server: please manually add the applicable one to your server configuration."));
} else {
$message .= ' ' . h(t("Here's the configuration section for every supported server: please manually remove the applicable one from your server configuration."));
}
$message .= '<dl>';
foreach ($codes as $code => $serviceNames) {
$message .= '<dt>' . h(tc(/*i18n %s is one or more server names */'For server', 'For %s', Misc::joinAnd($serviceNames))) . '</dt>';
$message .= '<dd>' . $this->getServerCodeHtml($code) . '</dd>';
}
return $message;
case static::PRETTYURLSTATE_NOT_READABLE:
case static::PRETTYURLSTATE_NOT_NEEDED:
case static::PRETTYURLSTATE_NOT_WRITABLE:
case static::PRETTYURLSTATE_UPDATED:
$manager = $this->app->make(ServiceManager::class);
$services = $manager->getActiveServices();
$service = $services[0];
$rule = $service->getGenerator()->getRule('pretty_urls');
switch ($state) {
case static::PRETTYURLSTATE_NOT_READABLE:
$message = t('It was not possible read your server configuration.');
if ($urlRewriting) {
$message .= ' ' . t('Please add this configuration section to your server configuration:');
} else {
$message .= ' ' . t('Please remove this configuration section from your server configuration');
}
break;
case static::PRETTYURLSTATE_NOT_NEEDED:
if ($urlRewriting) {
$message = t('The following rule was already in your server configuration');
} else {
$message = t('The following rule was already missing in your server configuration');
}
break;
case static::PRETTYURLSTATE_NOT_WRITABLE:
$message = t('It was not possible write your server configuration.');
if ($urlRewriting) {
$message .= ' ' . t('Please add this configuration section to your server configuration:');
} else {
$message .= ' ' . t('Please remove this configuration section from your server configuration');
}
break;
case static::PRETTYURLSTATE_UPDATED:
if ($urlRewriting) {
$message = t('The following rule has been added to the server configuration');
} else {
$message = t('The following rule has been removed from the server configuration');
}
break;
}
return h($message) .'<br>'. $this->getServerCodeHtml($rule->getCode());
}
}
protected function getServerCodeHtml(string $ruleCode): string
{
return $this->app->make(Form::class)->textarea('', h($ruleCode), ['rows' => substr_count($ruleCode, "\n") + 1, 'onclick' => 'this.select()', 'readonly' => 'readonly', 'class' => 'font-monospace w-100']);
}
}