/
ext.php
208 lines (185 loc) · 5.68 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
*
* @package gn36/versionchecknotifier
* @copyright (c) 2015 Martin Beckmann
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace gn36\versionchecknotifier;
class ext extends \phpbb\extension\base
{
/**
* An array of all defined notification types so they can be properly enabled/disabled
* @var array
*/
protected $notification_types = array(
// do not add 'gn36.versionchecknotifier.notification.type.base',
'gn36.versionchecknotifier.notification.type.ext_update',
'gn36.versionchecknotifier.notification.type.phpbb_update',
);
/**
* List extra dependencies of this extensions not added to composer.json (e.g. because composer doesn't know them)
* @return multitype:string
*/
protected function extra_dependencies()
{
return array();
}
/**
* Split Version info up into known parts
* @param unknown $string
* @return array|boolean|NULL
*/
protected function split_version_info($string)
{
$pattern = '#(>=|>|gt(?:;=?)?|ge|==?|eq|!=|<>|ne|<=|<|le|lt(?:;=?)?(?:gt;)?)([0-9._*-PLAHBETRCDV]+?)(?:$|,)#is';
$matches = null;
preg_match_all($pattern, $string, $matches);
if (!$matches)
{
return false;
}
$ret = array();
$ret['version'] = $matches[2];
$ret['operator'] = $matches[1];
$ret['operator'] = str_replace(array('gt;', 'lt;'), array('>', '<'), $ret['operator']);
return $ret;
}
/**
* @see \phpbb\extension\base::is_enableable()
*/
function is_enableable()
{
$config = $this->container->get('config');
$mgr = $this->container->get('ext.manager');
$template = $this->container->get('template');
$meta_mgr = $mgr->create_extension_metadata_manager($this->extension_name, $template);
$meta = $meta_mgr->get_metadata();
if (isset($meta['require']))
{
$require = $meta['require'];
}
else
{
$require = array();
}
if (isset($meta['extra']['soft_require']))
{
$require = array_merge($require, $meta['extra']['soft_require']);
}
$require = array_merge($require, $this->extra_dependencies());
/** @var $user \phpbb\user */
$user = $this->container->get('user');
$user->add_lang_ext($this->extension_name, 'install');
foreach ($require as $key => $value)
{
$info = $this->split_version_info($value);
foreach ($info['version'] as $vkey => $version)
{
switch (strtolower($key))
{
case 'php':
if (!phpbb_version_compare(PHP_VERSION, $version, $info['operator'][$vkey]))
{
trigger_error($user->lang('WRONG_PHP_VERSION') . adm_back_link(append_sid('index.' . $this->container->getParameter('core.php_ext'), 'i=acp_extensions&mode=main')), E_USER_WARNING);
return false;
}
break;
case 'phpbb':
case 'phpbb/phpbb':
if (!phpbb_version_compare($config['version'], $version, $info['operator'][$vkey]))
{
trigger_error($user->lang('WRONG_PHPBB_VERSION') . adm_back_link(append_sid('index.' . $this->container->getParameter('core.php_ext'), 'i=acp_extensions&mode=main')), E_USER_WARNING);
return false;
}
break;
case 'gn36/phpbb-oo-posting-api':
if (!file_exists(__DIR__ . '/vendor/gn36/phpbb-oo-posting-api/src/Gn36/OoPostingApi/post.php'))
{
trigger_error($user->lang('MISSING_DEPENDENCIES') . adm_back_link(append_sid('index.' . $this->container->getParameter('core.php_ext'), 'i=acp_extensions&mode=main')), E_USER_WARNING);
return false;
}
break;
default:
// This should be an extension as a requirement
if (!$mgr->is_enabled($key))
{
trigger_error($user->lang('MISSING_EXTENSION', $key) . adm_back_link(append_sid('index.' . $this->container->getParameter('core.php_ext'), 'i=acp_extensions&mode=main')), E_USER_WARNING);
return false;
}
$ext_meta_mgr = $mgr->create_extension_metadata_manager($key, $template);
$ext_meta = $ext_meta_mgr->get_metadata();
$ext_version = $ext_meta['version'];
if (!phpbb_version_compare($ext_version, $version, $info['operator'][$vkey]))
{
trigger_error($user->lang('WRONG_EXTENSION_VERSION', $key) . adm_back_link(append_sid('index.' . $this->container->getParameter('core.php_ext'), 'i=acp_extensions&mode=main')), E_USER_WARNING);
return false;
}
}
}
}
// Apparently passed all checks
return true;
}
/**
* @see \phpbb\extension\base::enable_step()
*/
public function enable_step($old_state)
{
switch ($old_state)
{
case '':
$phpbb_notifications = $this->container->get('notification_manager');
foreach ($this->notification_types as $notification_type)
{
$phpbb_notifications->enable_notifications($notification_type);
}
return 'notifications';
break;
default:
return parent::enable_step($old_state);
break;
}
}
/**
* @see \phpbb\extension\base::disable_step()
*/
public function disable_step($old_state)
{
switch ($old_state)
{
case '':
$phpbb_notifications = $this->container->get('notification_manager');
foreach ($this->notification_types as $notification_type)
{
$phpbb_notifications->disable_notifications($notification_type);
}
return 'notifications';
break;
default:
return parent::disable_step($old_state);
break;
}
}
/**
* @see \phpbb\extension\base::purge_step()
*/
public function purge_step($old_state)
{
switch ($old_state)
{
case '':
$phpbb_notifications = $this->container->get('notification_manager');
foreach ($this->notification_types as $notification_type)
{
$phpbb_notifications->purge_notifications($notification_type);
}
return 'notifications';
break;
default:
return parent::purge_step($old_state);
break;
}
}
}