-
Notifications
You must be signed in to change notification settings - Fork 8
/
ext.php
141 lines (120 loc) · 4.2 KB
/
ext.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
<?php
/**
*
* @copyright (c) Derky <http://www.derky.nl>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace derky\sortablescaptcha;
class ext extends \phpbb\extension\base
{
const SORTABLES_CAPTCHA_SERVICE_NAME = 'derky.sortablescaptcha.captcha.sortables';
const CONFIG_SORTABLES_CAPTCHA_WAS_DEFAULT = 'sortables_captcha_was_default';
/**
* Check the phpBB version to determine if this extension can be enabled.
* Sortables extends from phpBB's default Q&A captcha. Since phpBB 3.2.6 object arguments were no longer
* passed by reference which resulted in "declaration should be compatible" errors.
*
* @return boolean
*/
public function is_enableable()
{
$config = $this->container->get('config');
return version_compare($config['version'], '3.2.6', '>=');
}
/**
* Single enable step
*
* @param mixed $old_state State returned by previous call of this method
* @return mixed Returns false after last step, otherwise temporary state
*/
public function enable_step($old_state)
{
// Run parent enable step method
$return_parent = parent::enable_step($old_state);
// After the last migration
if ($return_parent === false)
{
// Atomatically reinstate sortables when it was disabled while being the default captcha.
// Sortables will not be set as default captcha for new and purged installations because it requires manual configuration.
$this->handle_default_captcha_on('enable');
}
return $return_parent;
}
/**
* Single disable step
*
* @param mixed $old_state State returned by previous call of this method
* @return mixed Returns false after last step, otherwise temporary state
*/
public function disable_step($old_state)
{
switch ($old_state)
{
case '': // Before the first migration
// If sortables is the default captcha, reset to phpBB's default captcha and store a sortables was default remember flag.
return $this->handle_default_captcha_on('disable');
default:
// Run parent disable step method
return parent::disable_step($old_state);
}
}
/**
* Single purge step
*
* @param mixed $old_state State returned by previous call of this method
* @return mixed Returns false after last step, otherwise temporary state
*/
public function purge_step($old_state)
{
switch ($old_state)
{
case '': // Before the first migration
// Remove the remember flag for sortables being the default captcha.
return $this->handle_default_captcha_on('purge');
default:
// Run parent purge step method
return parent::purge_step($old_state);
}
}
/**
* In order to update an extension, it has to be disabled. If Sortables Captcha is set as the default captcha, the board will break because it can't find the service anymore.
* On disable this method will restore phpBB's default GD captcha and adds a remember flag to automatically reinstate Sortables Captcha as the default captcha when the extension is re-enabled.
*
* @param string $step enable,disable,purge
* @return string
*/
public function handle_default_captcha_on($step)
{
// Get config
$config = $this->container->get('config');
switch ($step)
{
case 'enable':
// On enabling check if sortables was the default captcha before it was disabled
if ($config['sortables_captcha_was_default'])
{
// Restore sortables as the board's default captcha
$config->set('captcha_plugin', self::SORTABLES_CAPTCHA_SERVICE_NAME);
}
// Always clean up the remember flag
$config->delete(self::CONFIG_SORTABLES_CAPTCHA_WAS_DEFAULT);
break;
case 'disable':
// Check if sortables currently is the default captcha
if ($config['captcha_plugin'] === self::SORTABLES_CAPTCHA_SERVICE_NAME)
{
// Add a remember flag to the config
$config->set(self::CONFIG_SORTABLES_CAPTCHA_WAS_DEFAULT, true);
// Restore captcha to phpBB's default GD captcha.
$config->set('captcha_plugin', 'core.captcha.plugins.gd');
}
break;
case 'purge':
// When the database tables are purged, this captcha can't be set as default captcha until it's reconfigured manually. Therefore delete the remember flag.
$config->delete(self::CONFIG_SORTABLES_CAPTCHA_WAS_DEFAULT);
break;
}
return 'default_captcha_handled';
}
}