acp_listener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * VigLink extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\viglink\event;
12
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
15
/**
16
 * ACP Event listener
17
 */
18
class acp_listener implements EventSubscriberInterface
19
{
20
	/** @var \phpbb\config\config $config Config object */
21
	protected $config;
22
23
	/** @var \phpbb\request\request_interface $request Request interface */
24
	protected $request;
25
26
	/** @var \phpbb\template\template $template Template object */
27
	protected $template;
28
29
	/** @var \phpbb\language\language $language Language object */
30
	protected $language;
31
32
	/** @var \phpbb\user $user User object */
33
	protected $user;
34
35
	/** @var \phpbb\viglink\acp\viglink_helper $helper VigLink helper object */
36
	protected $helper;
37
38
	/** @var string $phpbb_root_path phpBB root path */
39
	protected $phpbb_root_path;
40
41
	/** @var string $php_ext PHP file extension */
42
	protected $php_ext;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\config\config $config
48
	 * @param \phpbb\language\language $language
49
	 * @param \phpbb\request\request_interface $request phpBB request
50
	 * @param \phpbb\template\template $template
51
	 * @param \phpbb\user $user User object
52
	 * @param \phpbb\viglink\acp\viglink_helper $viglink_helper Viglink helper object
53
	 * @param string $phpbb_root_path phpBB root path
54
	 * @param string $php_ext PHP file extension
55
	 */
56
	public function __construct(\phpbb\config\config $config, \phpbb\language\language $language, \phpbb\request\request_interface $request,
57
								\phpbb\template\template $template, \phpbb\user $user, \phpbb\viglink\acp\viglink_helper $viglink_helper,
58
								$phpbb_root_path, $php_ext)
59
	{
60
		$this->config = $config;
61
		$this->language = $language;
62
		$this->request = $request;
63
		$this->template = $template;
64
		$this->user = $user;
65
		$this->helper = $viglink_helper;
66
		$this->phpbb_root_path = $phpbb_root_path;
67
		$this->php_ext = $php_ext;
68
	}
69
70
	/**
71
	 * {@inheritDoc}
72
	 */
73
	public static function getSubscribedEvents()
74
	{
75
		return array(
76
			'core.acp_main_notice'				=> 'set_viglink_services',
77
			'core.acp_help_phpbb_submit_before'	=> 'update_viglink_settings',
78
		);
79
	}
80
81
	/**
82
	 * Check if phpBB is allowing VigLink services to run.
83
	 *
84
	 * VigLink will be disabled if phpBB is disallowing it to run.
85
	 *
86
	 * @return void
87
	 */
88
	public function set_viglink_services()
89
	{
90
		try
91
		{
92
			$this->helper->set_viglink_services();
93
		}
94
		catch (\RuntimeException $e)
95
		{
96
			$this->helper->log_viglink_error($e->getMessage());
97
		}
98
99
		// Only redirect once every 24 hours
100
		if (empty($this->config['viglink_ask_admin']) && $this->user->data['user_type'] == USER_FOUNDER && (time() - intval($this->config['viglink_ask_admin_last']) > 86400))
101
		{
102
			$this->config->set('viglink_ask_admin_last', time());
103
			redirect(append_sid($this->phpbb_root_path . 'adm/index.' . $this->php_ext, 'i=acp_help_phpbb&mode=help_phpbb'));
104
		}
105
	}
106
107
	/**
108
	 * Update VigLink settings
109
	 *
110
	 * @param array $event Event data
111
	 *
112
	 * @return void
113
	 */
114
	public function update_viglink_settings($event)
115
	{
116
		$this->language->add_lang('viglink_module_acp', 'phpbb/viglink');
117
118
		$viglink_setting = $this->request->variable('enable-viglink', false);
119
120
		if (!empty($event['submit']))
121
		{
122
			$this->config->set('viglink_enabled', $viglink_setting);
123
			if (empty($this->config['viglink_ask_admin']))
124
			{
125
				$this->config->set('viglink_ask_admin', time());
126
			}
127
		}
128
129
		$this->template->assign_vars(array(
130
			'S_ENABLE_VIGLINK'				=> !empty($this->config['viglink_enabled']) || !$this->config['help_send_statistics_time'],
131
			'S_VIGLINK_ASK_ADMIN'			=> empty($this->config['viglink_ask_admin']) && $this->user->data['user_type'] == USER_FOUNDER,
132
			'ACP_VIGLINK_SETTINGS_CHANGE'	=> $this->language->lang('ACP_VIGLINK_SETTINGS_CHANGE', append_sid($this->phpbb_root_path . 'adm/index.' . $this->php_ext, 'i=-phpbb-viglink-acp-viglink_module&mode=settings')),
133
		));
134
	}
135
}
136