Repository navigation
Expand file tree
/
Copy pathext.php
More file actions
151 lines (131 loc) · 7.4 KB
/
Copy pathext.php
File metadata and controls
151 lines (131 loc) · 7.4 KB
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
<?php
declare(strict_types=1);
namespace phpbbseo\framework;
class ext extends \phpbb\extension\base
{
public function enable_step($old_state)
{
switch ($old_state) {
case false:
$container = $this->container;
$config = $container->get('config');
$config->set('phpbbseo_framework_enable', '1');
$storeDir = $this->extension_path . 'store/';
if (!is_dir($storeDir)) {
@mkdir($storeDir, 0755, true);
}
// Default routes fallback to ensure immediate routing availability
$defaultRoutes = [
['regex' => '#^/topic/(?P<slug>[^/]+)-(?P<id>[0-9]+)/page/(?P<page>[0-9]+)/?$#u', 'resource' => 'topic', 'is_page' => true],
['regex' => '#^/topic/(?P<slug>[^/]+)-(?P<id>[0-9]+)/page-(?P<page>[0-9]+)/?$#u', 'resource' => 'topic', 'is_page' => true],
['regex' => '#^/topic/(?P<slug>[^/]+)-(?P<id>[0-9]+)/?$#u', 'resource' => 'topic', 'is_page' => false],
['regex' => '#^/forum/(?P<slug>[^/]+)-(?P<id>[0-9]+)/page/(?P<page>[0-9]+)/?$#u', 'resource' => 'forum', 'is_page' => true],
['regex' => '#^/forum/(?P<slug>[^/]+)-(?P<id>[0-9]+)/page-(?P<page>[0-9]+)/?$#u', 'resource' => 'forum', 'is_page' => true],
['regex' => '#^/forum/(?P<slug>[^/]+)-(?P<id>[0-9]+)/?$#u', 'resource' => 'forum', 'is_page' => false],
['regex' => '#^/member/(?P<slug>[^/]+)-(?P<id>[0-9]+)/?$#u', 'resource' => 'member', 'is_page' => false],
['regex' => '#^/group/(?P<slug>[^/]+)-(?P<id>[0-9]+)/?$#u', 'resource' => 'group', 'is_page' => false],
];
@file_put_contents($storeDir . 'compiled_routes.php', "<?php\n// Auto-generated route cache.\ndeclare(strict_types=1);\n\nreturn " . var_export($defaultRoutes, true) . ";\n");
// If container services are available, compile exact configured patterns
try {
if ($container->has('phpbbseo.framework.rewrite.permalink_configuration') && $container->has('phpbbseo.framework.rewrite.route_cache_compiler')) {
/** @var \phpbbseo\framework\Rewrite\PermalinkConfiguration $permalinkConfig */
$permalinkConfig = $container->get('phpbbseo.framework.rewrite.permalink_configuration');
/** @var \phpbbseo\framework\Rewrite\RouteCacheCompiler $routeCompiler */
$routeCompiler = $container->get('phpbbseo.framework.rewrite.route_cache_compiler');
$patterns = [
'forum' => $permalinkConfig->getPattern('forum'),
'forum_page' => $permalinkConfig->getPattern('forum_page'),
'topic' => $permalinkConfig->getPattern('topic'),
'topic_page' => $permalinkConfig->getPattern('topic_page'),
'member' => $permalinkConfig->getPattern('member'),
'group' => $permalinkConfig->getPattern('group'),
];
$routeCompiler->compileAndDump($patterns);
}
} catch (\Throwable) {
// Safe core defaults are already written above
}
// If Safe Uninstall rules were previously active, restore normal Lite routing
try {
require_once $this->extension_path . 'SafeUninstall/SafeUninstallManager.php';
$safeUninstall = null;
if ($container->has('phpbbseo.framework.safe_uninstall.manager')) {
$safeUninstall = $container->get('phpbbseo.framework.safe_uninstall.manager');
} else {
$safeUninstall = new \phpbbseo\framework\SafeUninstall\SafeUninstallManager(
$this->container->getParameter('core.root_path'),
null,
$config,
$container->has('cache.driver') ? $container->get('cache.driver') : null
);
}
if ($safeUninstall !== null && $safeUninstall->isPrepared() && $safeUninstall->isHtaccessWritable()) {
$safeUninstall->restore();
}
} catch (\Throwable) {
// Fail-safe
}
return 'all';
default:
return parent::enable_step($old_state);
}
}
public function disable_step($old_state)
{
switch ($old_state) {
case false:
$container = $this->container;
$config = $container->get('config');
$config->set('phpbbseo_framework_enable', '0');
// Write disabled marker so rewrite.php safely bypasses SEO interception without loops
$storeDir = $this->extension_path . 'store/';
if (!is_dir($storeDir)) {
@mkdir($storeDir, 0755, true);
}
$cacheFile = $storeDir . 'compiled_routes.php';
@file_put_contents($cacheFile, "<?php\n// Disabled extension route cache marker.\ndeclare(strict_types=1);\n\nreturn ['__disabled' => true];\n");
// Invalidate Pro llms.txt cache so rewrite.php bypasses fast path when disabled
$proLlmsCache = $this->container->getParameter('core.root_path') . 'ext/phpbbseo/pro/store/llms.txt.cache';
if (is_file($proLlmsCache)) {
@unlink($proLlmsCache);
}
// Prepare Safe Uninstall fallback rules in .htaccess so SEO URLs 301-redirect to native phpBB
try {
require_once $this->extension_path . 'SafeUninstall/SafeUninstallManager.php';
$safeUninstall = null;
if ($container->has('phpbbseo.framework.safe_uninstall.manager')) {
$safeUninstall = $container->get('phpbbseo.framework.safe_uninstall.manager');
} else {
$safeUninstall = new \phpbbseo\framework\SafeUninstall\SafeUninstallManager(
$this->container->getParameter('core.root_path'),
null,
$config,
$container->has('cache.driver') ? $container->get('cache.driver') : null
);
}
if ($safeUninstall !== null && !$safeUninstall->isPrepared() && $safeUninstall->isHtaccessWritable()) {
$safeUninstall->prepare();
}
} catch (\Throwable) {
// Fail-safe
}
return 'all';
default:
return parent::disable_step($old_state);
}
}
public function purge_step($old_state)
{
switch ($old_state) {
case false:
$cacheFile = $this->extension_path . 'store/compiled_routes.php';
if (file_exists($cacheFile)) {
@unlink($cacheFile);
}
return 'all';
default:
return parent::purge_step($old_state);
}
}
}