viglink_helper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 16
eloc 42
c 7
b 0
f 0
dl 0
loc 130
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B set_viglink_services() 0 38 7
A set_viglink_configs() 0 16 5
A log_viglink_error() 0 6 3
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\acp;
12
13
/**
14
 * Class to handle allowing or disallowing VigLink services
15
 */
16
class viglink_helper
17
{
18
	/** @var \phpbb\cache\driver\driver_interface $cache */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config $config */
22
	protected $config;
23
24
	/** @var \phpbb\file_downloader $file_downloader */
25
	protected $file_downloader;
26
27
	/** @var \phpbb\language\language $language */
28
	protected $language;
29
30
	/** @var \phpbb\log\log $log */
31
	protected $log;
32
33
	/** @var \phpbb\user $user */
34
	protected $user;
35
36
	/** @var bool Use SSL or not */
37
	protected $use_ssl = false;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param \phpbb\cache\driver\driver_interface $cache
43
	 * @param \phpbb\config\config                 $config
44
	 * @param \phpbb\file_downloader               $file_downloader
45
	 * @param \phpbb\language\language             $language
46
	 * @param \phpbb\log\log                       $log
47
	 * @param \phpbb\user                          $user
48
	 */
49
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\file_downloader $file_downloader, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\user $user)
50
	{
51
		$this->cache = $cache;
52
		$this->config = $config;
53
		$this->file_downloader = $file_downloader;
54
		$this->language = $language;
55
		$this->log = $log;
56
		$this->user = $user;
57
	}
58
59
	/**
60
	 * Obtains the latest VigLink services information from phpBB
61
	 *
62
	 * @param bool $force_update Ignores cached data. Defaults to false.
63
	 * @param bool $force_cache  Force the use of the cache. Override $force_update.
64
	 *
65
	 * @throws \RuntimeException
66
	 *
67
	 * @return void
68
	 */
69
	public function set_viglink_services($force_update = false, $force_cache = false)
70
	{
71
		$cache_key = '_versioncheck_viglink_' . $this->use_ssl;
72
73
		$info = $this->cache->get($cache_key);
74
75
		if ($info === false && $force_cache)
76
		{
77
			throw new \RuntimeException($this->language->lang('VERSIONCHECK_FAIL'));
78
		}
79
80
		if ($info === false || $force_update)
81
		{
82
			try
83
			{
84
				$info = $this->file_downloader->get('www.phpbb.com', '/viglink', 'enabled', 443);
85
			}
86
			catch (\phpbb\exception\runtime_exception $exception)
87
			{
88
				$prepare_parameters = array_merge(array($exception->getMessage()), $exception->get_parameters());
89
				throw new \RuntimeException(call_user_func_array(array($this->language, 'lang'), $prepare_parameters));
90
			}
91
92
			if ($info === '0')
93
			{
94
				$this->set_viglink_configs(array(
95
					'allow_viglink_phpbb'	=> false,
96
				));
97
			}
98
			else
99
			{
100
				$info = '1';
101
				$this->set_viglink_configs(array(
102
					'allow_viglink_phpbb'	=> true,
103
				));
104
			}
105
106
			$this->cache->put($cache_key, $info, 86400); // 24 hours
107
		}
108
	}
109
110
	/**
111
	 * Sets VigLink service configs as determined by phpBB
112
	 *
113
	 * @param array $data Array of VigLink file data.
114
	 *
115
	 * @return void
116
	 */
117
	protected function set_viglink_configs($data)
118
	{
119
		$viglink_configs = array(
120
			'allow_viglink_phpbb',
121
			'phpbb_viglink_api_key',
122
		);
123
124
		foreach ($viglink_configs as $cfg_name)
125
		{
126
			if (array_key_exists($cfg_name, $data) && ($data[$cfg_name] != $this->config[$cfg_name] || !isset($this->config[$cfg_name])))
127
			{
128
				$this->config->set($cfg_name, $data[$cfg_name]);
129
			}
130
		}
131
132
		$this->config->set('viglink_last_gc', time(), false);
133
	}
134
135
	/**
136
	 * Log a VigLink error message to the error log
137
	 *
138
	 * @param string $message The error message
139
	 */
140
	public function log_viglink_error($message)
141
	{
142
		$user_id = empty($this->user->data) ? ANONYMOUS : $this->user->data['user_id'];
143
		$user_ip = empty($this->user->ip) ? '' : $this->user->ip;
144
145
		$this->log->add('critical', $user_id, $user_ip, 'LOG_VIGLINK_CHECK_FAIL', false, array($message));
146
	}
147
}
148