Passed
Pull Request — master (#121)
by Matt
01:27
created

bbcodes_installer::first_pass_match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 10
ccs 1
cts 1
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2016 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use phpbb\db\driver\driver_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\group\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\group\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Class bbcodes_installer
20
 *
21
 * @package vse\abbc3\core
22
 */
23
class bbcodes_installer extends acp_manager
24
{
25
	/** @var \acp_bbcodes */
0 ignored issues
show
Bug introduced by
The type acp_bbcodes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
	protected $acp_bbcodes;
27
28
	/** @var string */
29
	protected $phpbb_root_path;
30
31
	/** @var string */
32
	protected $php_ext;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param driver_interface $db
38
	 * @param helper           $group_helper
39
	 * @param language         $language
40
	 * @param request          $request
41
	 * @param string           $phpbb_root_path
42
	 * @param string           $php_ext
43
	 * @access public
44
	 */
45 2
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request, $phpbb_root_path, $php_ext)
46
	{
47 2
		parent::__construct($db, $group_helper, $language, $request);
48
49 2
		$this->phpbb_root_path = $phpbb_root_path;
50 2
		$this->php_ext         = $php_ext;
51
52 2
		$this->acp_bbcodes     = $this->get_acp_bbcodes();
53
54 2
		$this->language->add_lang('acp/posting');
55 2
	}
56
57
	/**
58
	 * Installs bbcodes, used by migrations to perform add/updates
59
	 *
60
	 * @param array $bbcodes Array of bbcodes to install
61
	 * @access public
62
	 */
63 2
	public function install_bbcodes(array $bbcodes)
64
	{
65 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
66
		{
67 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
68
69 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
70 2
			{
71 1
				$this->update_bbcode($bbcode, $bbcode_data);
72 1
				continue;
73
			}
74
75 2
			$this->add_bbcode($bbcode_data);
76 2
		}
77
78 2
		$this->resynchronize_bbcode_order();
79 2
	}
80
81
	/**
82
	 * Deletes bbcodes, used by migrations to perform add/updates.
83
	 * Don't delete just by ID. Also match replace fields. This is to ensure
84
	 * we only delete BBCodes created by ABBC3, and not if, for example
85
	 * ABBC3 BBCodes that have been altered by the user.
86
	 *
87 2
	 * @param array $bbcodes Array of bbcodes to delete
88
	 * @access public
89 2
	 */
90
	public function delete_bbcodes(array $bbcodes)
91 2
	{
92
		// Addresses MSSQL Error: 402 The data types ntext and varchar are incompatible in the equal to operator
93 2
		$is_mssql = strpos($this->db->get_sql_layer(), 'mssql') === 0;
94 2
95 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
96 2
		{
97 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
98 2
99 2
			if (!($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag'])))
100 2
			{
101 2
				continue;
102
			}
103 2
104 2
			$sql = 'DELETE FROM ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
			WHERE ' . ($is_mssql ? 'CONVERT(NVARCHAR(MAX), first_pass_match) = N' : 'first_pass_match = ') . "'" . $this->db->sql_escape($bbcode_data['first_pass_match']) . "'
106
				AND " . ($is_mssql ? 'CONVERT(NVARCHAR(MAX), first_pass_replace) = N' : 'first_pass_replace = ') . "'" . $this->db->sql_escape($bbcode_data['first_pass_replace']) . "'
107
				AND bbcode_id = " . (int) $bbcode['bbcode_id'];
108
109
			$this->db->sql_query($sql);
110
		}
111
112 2
		$this->resynchronize_bbcode_order();
113
	}
114 2
115 2
	/**
116 1
	 * Get the acp_bbcodes class
117 1
	 *
118
	 * @return \acp_bbcodes
119 2
	 * @access protected
120
	 */
121
	protected function get_acp_bbcodes()
122
	{
123
		if (!class_exists('acp_bbcodes'))
124
		{
125
			include $this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext;
126
		}
127
128
		return new \acp_bbcodes();
129 2
	}
130
131 2
	/**
132
	 * Build the bbcode
133 2
	 *
134 2
	 * @param array $bbcode_data Initial bbcode data
135 2
	 * @return array Complete bbcode data array
136 2
	 * @access protected
137 2
	 */
138 2
	protected function build_bbcode(array $bbcode_data)
139 2
	{
140
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
141 2
142
		return array_replace($bbcode_data, [
143
			'bbcode_tag'          => $data['bbcode_tag'],
144
			'first_pass_match'    => $data['first_pass_match'],
145
			'first_pass_replace'  => $data['first_pass_replace'],
146
			'second_pass_match'   => $data['second_pass_match'],
147
			'second_pass_replace' => $data['second_pass_replace'],
148
		]);
149
	}
150 2
151
	/**
152 2
	 * Get the max bbcode id value
153
	 *
154
	 * @return int bbcode identifier
155
	 * @access protected
156
	 */
157
	protected function get_max_bbcode_id()
158
	{
159
		return $this->get_max_column_value('bbcode_id');
160
	}
161
162
	/**
163 2
	 * Check if bbcode exists
164
	 *
165
	 * @param string $bbcode_name Name of bbcode
166 2
	 * @param string $bbcode_tag  Tag name of bbcode
167 2
	 * @return array|false Existing bbcode data array or false if not found
168 2
	 * @access protected
169 2
	 */
170 2
	public function bbcode_exists($bbcode_name, $bbcode_tag)
171 2
	{
172
		$sql = 'SELECT bbcode_id
173 2
			FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
174
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
175
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
176
		$result = $this->db->sql_query($sql);
177
		$row = $this->db->sql_fetchrow($result);
178
		$this->db->sql_freeresult($result);
179
180
		return $row ? (array) $row : false;
181
	}
182
183 1
	/**
184
	 * Update existing bbcode
185 1
	 *
186 1
	 * @param array $old_bbcode Existing bbcode data
187 1
	 * @param array $new_bbcode New bbcode data
188 1
	 * @access protected
189 1
	 */
190
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
191
	{
192
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
193
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
194
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
195
		$this->db->sql_query($sql);
196
	}
197 2
198
	/**
199 2
	 * Add new bbcode
200
	 *
201 2
	 * @param array $bbcode_data New bbcode data
202 2
	 * @access protected
203 1
	 */
204 1
	protected function add_bbcode(array $bbcode_data)
205
	{
206 2
		$bbcode_id = max($this->get_max_bbcode_id(), NUM_CORE_BBCODES) + 1;
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\NUM_CORE_BBCODES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
207 2
208 2
		if ($bbcode_id <= BBCODE_LIMIT)
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODE_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
209
		{
210 2
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
211
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
212 2
			$bbcode_data['display_on_posting'] = (int) !array_key_exists('display_on_posting', $bbcode_data);
213 2
214 2
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
215
		}
216
	}
217
}
218