Completed
Pull Request — master (#114)
by Jakub
14:25
created

visual_demo_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 6
crap 2
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2018 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
 * Visual demo controller
15
 */
16
class visual_demo_controller
17
{
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\config\config */
22
	protected $config;
23
24
	/** @var \phpbb\request\request */
25
	protected $request;
26
27
	/** @var \phpbb\user */
28
	protected $user;
29
30
	/** @var string */
31
	protected $root_path;
32
33
	/** @var string */
34
	protected $php_ext;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\auth\auth       $auth
40
	 * @param \phpbb\config\config   $config
41
	 * @param \phpbb\request\request $request
42
	 * @param \phpbb\user            $user
43
	 * @param string                 $root_path
44
	 * @param string                 $php_ext
45
	 */
46
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\request\request $request, \phpbb\user $user, $root_path, $php_ext)
47
	{
48
		$this->auth = $auth;
49
		$this->config = $config;
50
		$this->request = $request;
51
		$this->user = $user;
52
		$this->root_path = $root_path;
53
		$this->php_ext = $php_ext;
54
	}
55
56
	/**
57
	 * Visual demo handler
58
	 *
59
	 * When called by an admin, add or remove the visual demo cookie
60
	 * and direct them to an appropriate forum page to view.
61
	 *
62
	 * @param string $action enable|disable
63
	 * @return \Symfony\Component\HttpFoundation\JsonResponse
64
	 * @throws \phpbb\exception\http_exception An http exception
65
	 */
66
	public function handle($action)
67
	{
68
		// Protect against unauthorised access or CSRF/XSRF attacks
69
		if (!$this->auth->acl_get('a_') || !check_link_hash($this->request->variable('hash', ''), 'visual_demo'))
70
		{
71
			throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
72
		}
73
74
		if ($action === 'disable' && $this->request->is_set($this->config['cookie_name'] . '_phpbb_ads_visual_demo', \phpbb\request\request_interface::COOKIE))
75
		{
76
			// If cookie already exists, lets destroy it and redirect user to previous page viewed.
77
			$this->user->set_cookie('phpbb_ads_visual_demo', '', 1);
78
			$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
79
		}
80
		else
81
		{
82
			// Since cookie does not exist yet, lets create one and send user to the index page.
83
			$this->user->set_cookie('phpbb_ads_visual_demo', time(), 0);
84
			$redirect = "{$this->root_path}index.{$this->php_ext}";
85
		}
86
87
		// Send a JSON response if an AJAX request was used
88
		if ($this->request->is_ajax())
89
		{
90
			return new \Symfony\Component\HttpFoundation\JsonResponse(array(
91
				'success' => true,
92
			));
93
		}
94
95
		// Redirect user to a page
96
		$redirect = reapply_sid($redirect);
97
		redirect($redirect);
98
99
		// We shouldn't get here, but throw an http exception just in case
100
		throw new \phpbb\exception\http_exception(500, 'GENERAL_ERROR');
101
	}
102
}
103