This repository was archived by the owner on Oct 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.php
More file actions
166 lines (140 loc) · 5.12 KB
/
Copy pathext.php
File metadata and controls
166 lines (140 loc) · 5.12 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
*
* Site Logo extension for the phpBB Forum Software package
*
* @copyright (c) 2023, Kailey Snay, https://www.snayhomelab.com/
* @copyright (c) 2026, phpBB Modders, https://www.phpbbmodders.com/
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace phpbbmodders\sitelogo;
/**
* Site Logo extension base
*
* Handles the switch from the old "kaileymsnay/sitelogo" package name: an existing
* install's data is moved over to the new name when this extension is
* enabled, so nothing is installed twice and no settings are lost.
*/
class ext extends \phpbb\extension\base
{
/** Package name this extension used before the rename */
const OLD_EXT_NAME = 'kaileymsnay/sitelogo';
/** Package name this extension uses now */
const NEW_EXT_NAME = 'phpbbmodders/sitelogo';
/** PHP namespace prefix used before the rename */
const OLD_NAMESPACE = '\\kaileymsnay\\sitelogo\\';
/** PHP namespace prefix used now */
const NEW_NAMESPACE = '\\phpbbmodders\\sitelogo\\';
/**
* Require phpBB 3.3.0+ and PHP 7.4+, and refuse to enable while the old
* copy is still enabled; both would run at once and the old one's data
* can't be moved while it's in use.
*
* @return bool|array True if enableable, otherwise false or an array of reasons
*/
public function is_enableable()
{
$config = $this->container->get('config');
if (!phpbb_version_compare($config['version'], '3.3.0', '>=') || !version_compare(PHP_VERSION, '7.4.0', '>='))
{
return false;
}
if ($this->container->get('ext.manager')->is_enabled(self::OLD_EXT_NAME))
{
return ['Disable the old "' . self::OLD_EXT_NAME . '" extension first (keep its data, do not delete it).'];
}
return true;
}
/**
* Move an old install's data to the new name, then enable as usual.
*
* @param mixed $old_state State returned by previous call of this method
* @return bool|string
*/
public function enable_step($old_state)
{
if ($old_state === false)
{
$this->move_old_install();
// The migrator loaded its state before this ran; reload it so the
// moved migration history counts as already installed.
$this->migrator->load_migration_state();
}
return parent::enable_step($old_state);
}
/**
* Rewrite everything the database stores under the old name.
*
* Rows are filtered in PHP rather than with LIKE, because namespace
* backslashes and underscores are special characters in LIKE patterns
* on some databases. The tables involved are small.
*/
protected function move_old_install()
{
$db = $this->container->get('dbal.conn');
$prefix = $this->container->getParameter('core.table_prefix');
$db->sql_transaction('begin');
// Migration history, including each migration's dependency list
$result = $db->sql_query('SELECT migration_name, migration_depends_on FROM ' . $prefix . 'migrations');
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
foreach ($rows as $row)
{
if ($this->new_class_name($row['migration_name']) === $row['migration_name'])
{
continue;
}
$depends_on = unserialize($row['migration_depends_on'], ['allowed_classes' => false]);
$depends_on = is_array($depends_on) ? array_map([$this, 'new_class_name'], $depends_on) : [];
$db->sql_query('UPDATE ' . $prefix . 'migrations SET ' . $db->sql_build_array('UPDATE', [
'migration_name' => $this->new_class_name($row['migration_name']),
'migration_depends_on' => serialize($depends_on),
]) . " WHERE migration_name = '" . $db->sql_escape($row['migration_name']) . "'");
}
// Module classes and their "ext_vendor/name" auth checks
$result = $db->sql_query('SELECT module_id, module_basename, module_auth FROM ' . $prefix . 'modules');
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
foreach ($rows as $row)
{
$basename = $this->new_class_name($row['module_basename']);
$auth = str_replace('ext_' . self::OLD_EXT_NAME, 'ext_' . self::NEW_EXT_NAME, $row['module_auth']);
if ($basename !== $row['module_basename'] || $auth !== $row['module_auth'])
{
$db->sql_query('UPDATE ' . $prefix . 'modules SET ' . $db->sql_build_array('UPDATE', [
'module_basename' => $basename,
'module_auth' => $auth,
]) . ' WHERE module_id = ' . (int) $row['module_id']);
}
}
// The old extension's own record (only once it's disabled)
$db->sql_query('DELETE FROM ' . $prefix . "ext
WHERE ext_name = '" . $db->sql_escape(self::OLD_EXT_NAME) . "'
AND ext_active = 0");
$db->sql_transaction('commit');
}
/**
* Map an old fully qualified class name to the new namespace.
*
* @param string $class_name Class name, possibly under the old namespace
* @return string
*/
protected function new_class_name($class_name)
{
if (!is_string($class_name))
{
return $class_name;
}
// Class names may be stored with or without the leading backslash
foreach ([self::OLD_NAMESPACE, ltrim(self::OLD_NAMESPACE, '\\')] as $old)
{
if (strpos($class_name, $old) === 0)
{
$new = ($old === self::OLD_NAMESPACE) ? self::NEW_NAMESPACE : ltrim(self::NEW_NAMESPACE, '\\');
return $new . substr($class_name, strlen($old));
}
}
return $class_name;
}
}