forked from yakamara/redaxo_yrewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.php
168 lines (140 loc) · 7.01 KB
/
boot.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
<?php
/**
* YREWRITE Addon.
*
* @author [email protected]
*
* @package redaxo\yrewrite
*
* @psalm-scope-this rex_addon
* @var rex_addon $this
*/
if (!rex::isBackend()) {
$path = rtrim(dirname($_SERVER['SCRIPT_NAME']), DIRECTORY_SEPARATOR) . '/';
rex_url::init(new rex_path_default_provider($path, 'redaxo', false));
} elseif (rex::getUser()) {
rex_view::addCssFile($this->getAssetsUrl('yrewrite.css'));
}
// Additional permissions for url & seo editing
rex_perm::register('yrewrite[url]', rex_i18n::msg('yrewrite_perm_url_edit'));
rex_perm::register('yrewrite[seo]', rex_i18n::msg('yrewrite_perm_seo_edit'));
rex_extension::register('PACKAGES_INCLUDED', function ($params) {
rex_yrewrite::init();
if ('robots' == rex_request('rex_yrewrite_func', 'string')) {
$robots = new rex_yrewrite_seo();
$robots->sendRobotsTxt();
}
// if anything changes -> refresh PathFile
if (rex::isBackend()) {
$extensionPoints = [
'CAT_ADDED', 'CAT_UPDATED', 'CAT_DELETED', 'CAT_STATUS', 'CAT_MOVED',
'ART_ADDED', 'ART_UPDATED', 'ART_DELETED', 'ART_STATUS', 'ART_MOVED', 'ART_COPIED',
'ART_META_UPDATED', 'ART_TO_STARTARTICLE', 'ART_TO_CAT', 'CAT_TO_ART',
/* 'CLANG_ADDED', */ 'CLANG_UPDATED', /* 'CLANG_DELETED', */
/* 'ARTICLE_GENERATED' */
// 'ALL_GENERATED'
];
foreach ($extensionPoints as $extensionPoint) {
rex_extension::register($extensionPoint, static function (rex_extension_point $ep) {
$params = $ep->getParams();
$params['subject'] = $ep->getSubject();
$params['extension_point'] = $ep->getName();
rex_yrewrite::generatePathFile($params);
});
}
// prevent article deletion if used in domain settings
rex_extension::register('ART_PRE_DELETED', static function (rex_extension_point $ep) {
$warning = [];
$params = $ep->getParams();
$article_id = $params['id'];
$sql = rex_sql::factory();
$sql->setQuery('SELECT id, domain FROM `' . rex::getTablePrefix() . 'yrewrite_domain` '
.'WHERE start_id = :article_id OR mount_id = :article_id OR notfound_id = :article_id', [
'article_id' => $article_id,
]);
// Warnings
for ($i = 0; $i < $sql->getRows(); ++$i) {
$message = '<a href="'. rex_url::backendPage('yrewrite/domains', ['func' => 'edit', 'data_id' => $sql->getValue('id')]) .'">YRewrite '. rex_i18n::msg('yrewrite_domains') .': '. $sql->getValue('domain') .'</a>';
$warning[] = $message;
$sql->next();
}
if (count($warning) > 0) {
throw new rex_api_exception(rex_i18n::msg('yrewrite_error_article_in_use') .'<ul><li>'. implode('</li><li>', $warning) .'</li></ul>');
}
});
// prevent deletion of seo image
rex_extension::register('MEDIA_IS_IN_USE', static function (rex_extension_point $ep) {
$warning = $ep->getSubject();
$params = $ep->getParams();
$filename = $params['filename'];
$sql = rex_sql::factory();
$sql->setQuery('SELECT id, clang_id, name FROM `' . rex::getTablePrefix() . 'article` WHERE yrewrite_image = ?', [$filename]);
for ($i = 0; $i < $sql->getRows(); ++$i) {
$message = rex_i18n::msg('yrewrite_seoimage_error_delete') .' <a href="javascript:openPage(\'index.php?page=content/edit&mode=edit&article_id='.
$sql->getValue('id') .'&clang='. $sql->getValue('clang_id') .'\')">'. $sql->getValue('name') .'</a>';
if (!in_array($message, $warning)) {
$warning[] = $message;
}
$sql->next();
}
return $warning;
});
}
// rex_extension::register('ALL_GENERATED', 'rex_yrewrite::init');
rex_extension::register('URL_REWRITE', static function (rex_extension_point $ep) {
$params = $ep->getParams();
$params['subject'] = $ep->getSubject();
return rex_yrewrite::rewrite($params);
});
rex_extension::register('MEDIA_MANAGER_URL', static function (rex_extension_point $ep) {
return rex_yrewrite::rewriteMedia($ep->getParams());
});
if ('cli' !== PHP_SAPI) {
rex_yrewrite::prepare();
}
if (rex::isBackend()) {
if (!$this->getConfig('yrewrite_hide_url_block') && rex::getUser() instanceof rex_user && rex::getUser()->hasPerm('yrewrite[url]')) {
rex_extension::register('STRUCTURE_CONTENT_SIDEBAR', function (rex_extension_point $ep) {
$params = $ep->getParams();
$subject = $ep->getSubject();
$panel = include rex_path::addon('yrewrite', 'pages/content.yrewrite_url.php');
$fragment = new rex_fragment();
$fragment->setVar('title', '<i class="rex-icon rex-icon-info"></i> '.rex_i18n::msg('yrewrite_rewriter'), false);
$fragment->setVar('body', $panel, false);
$fragment->setVar('article_id', $params['article_id'], false);
$fragment->setVar('collapse', true);
$fragment->setVar('collapsed', false);
$content = $fragment->parse('core/page/section.php');
return $subject.$content;
});
}
if (!$this->getConfig('yrewrite_hide_seo_block') && rex::getUser() instanceof rex_user && rex::getUser()->hasPerm('yrewrite[seo]')) {
rex_extension::register('STRUCTURE_CONTENT_SIDEBAR', function (rex_extension_point $ep) {
$params = $ep->getParams();
$subject = $ep->getSubject();
$panel = include rex_path::addon('yrewrite', 'pages/content.yrewrite_seo.php');
$fragment = new rex_fragment();
$fragment->setVar('title', '<i class="rex-icon rex-icon-info"></i> '.rex_i18n::msg('yrewrite_rewriter_seo'), false);
$fragment->setVar('body', $panel, false);
$fragment->setVar('article_id', $params['article_id'], false);
$fragment->setVar('clang', $params['clang'], false);
$fragment->setVar('ctype', $params['ctype'], false);
$fragment->setVar('collapse', true);
$fragment->setVar('collapsed', false);
$content = $fragment->parse('core/page/section.php');
return $subject.$content;
});
}
}
}, rex_extension::EARLY);
if ('sitemap' == rex_request('rex_yrewrite_func', 'string')) {
rex_extension::register('PACKAGES_INCLUDED', static function ($params) {
$sitemap = new rex_yrewrite_seo();
$sitemap->sendSitemap();
}, rex_extension::LATE);
}
rex_extension::register('YREWRITE_PREPARE', static function (rex_extension_point $ep) {
$params = $ep->getParams();
$params['subject'] = $ep->getSubject();
return rex_yrewrite_forward::getForward($params);
});