Passed
Push — master ( 07d5b2...0d04db )
by Spuds
58s queued 16s
created

phpBB33::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD https://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 1.0
8
 */
9
10
/**
11
 * Class phpBB33
12
 */
13
class phpBB33 extends Importers\AbstractSourceImporter
14
{
15
	protected $setting_file = '/config.php';
16
17
	public function getName()
18
	{
19
		return 'phpBB3_3';
20
	}
21
22
	public function getVersion()
23
	{
24
		return 'ElkArte 1.1';
25
	}
26
27
	public function setDefines()
28
	{
29
		if (!defined('IN_PHPBB'))
30
		{
31
			define('IN_PHPBB', 1);
32
		}
33
	}
34
35
	public function getPrefix()
36
	{
37
		$table_prefix = $this->fetchSetting('table_prefix');
38
39
		return '`' . $this->getDbName() . '`.' . $table_prefix;
40
	}
41
42
	public function getDbName()
43
	{
44
		return $this->fetchSetting('dbname');
45
	}
46
47
	public function getTableTest()
48
	{
49
		return 'users';
50
	}
51
52
	public function phpbb_copy_files($dir, $row, $id_attach, $destination_path, $thumb = false)
53
	{
54
		// Use the Utils function to get extra details
55
		list($ext, $basename, $mime_type) = attachment_type($row['filename']);
56
57
		// Prep for the copy
58
		$file = (($thumb) ? 'thumb_' : '') . $row['physical_filename'];
59
		$source = $dir . '/' . $file;
60
		$file_hash = createAttachmentFilehash($file);
61
		$destination = $destination_path . '/' . $id_attach . '_' . $file_hash . '.elk';
62
		$type = 0;
63
64
		// Copy it over
65
		copy_file($source, $destination);
66
67
		// An image must have a legit width/height
68
		$width = 0;
69
		$height = 0;
70
		if (!empty($ext))
71
		{
72
			list ($width, $height) = getimagesize($destination);
73
			if (!empty($width))
74
			{
75
				$type = ($thumb) ? 3 : 0;
76
			}
77
		}
78
79
		// Prepare our insert
80
		return array(
81
			'id_attach' => $id_attach,
82
			'id_thumb' => !$thumb && !empty($row['thumbnail']) ? ++$id_attach : 0,
83
			'size' => file_exists($destination) ? filesize($destination) : 0,
84
			'filename' => $basename . '.' . ($thumb ? $ext . '_thumb' : $ext),
85
			'file_hash' => $file_hash,
86
			'file_ext' => $ext,
87
			'mime_type' => $mime_type,
88
			'attachment_type' => $type,
89
			'id_msg' => $row['id_msg'],
90
			'downloads' => $row['downloads'],
91
			'width' => $width,
92
			'height' => $height
93
		);
94
	}
95
}
96
97
// Utility functions specific to phpbb
98
99
/**
100
 * @param int $percent
101
 *
102
 * @return int
103
 */
104
function percent_to_px($percent)
105
{
106
	return (int) (11 * ((int) $percent / 100.0));
107
}
108
109
/**
110
 * Normalize BBC
111
 *
112
 * @param string $message
113
 *
114
 * @return mixed|string
115
 */
116
function phpbb_replace_bbc($message)
117
{
118
	$message = preg_replace(
119
		array(
120
			'~\[quote=&quot;(.+?)&quot;\:(.+?)\]~is',
121
			'~\[quote\:(.+?)\]~is',
122
			'~\[/quote\:(.+?)\]~is',
123
			'~\[b\:(.+?)\]~is',
124
			'~\[/b\:(.+?)\]~is',
125
			'~\[i\:(.+?)\]~is',
126
			'~\[/i\:(.+?)\]~is',
127
			'~\[u\:(.+?)\]~is',
128
			'~\[/u\:(.+?)\]~is',
129
			'~\[url\:(.+?)\]~is',
130
			'~\[/url\:(.+?)\]~is',
131
			'~\[url=(.+?)\:(.+?)\]~is',
132
			'~\[/url\:(.+?)\]~is',
133
			'~\<a(.+?) href="(.+?)">(.+?)</a>~is',
134
			'~\[img\:(.+?)\]~is',
135
			'~\[/img\:(.+?)\]~is',
136
			'~\[size=(.+?)\:(.+?)\]~is',
137
			'~\[/size\:(.+?)?\]~is',
138
			'~\[color=(.+?)\:(.+?)\]~is',
139
			'~\[/color\:(.+?)\]~is',
140
			'~\[code=(.+?)\:(.+?)\]~is',
141
			'~\[code\:(.+?)\]~is',
142
			'~\[/code\:(.+?)\]~is',
143
			'~\[list=(.+?)\:(.+?)\]~is',
144
			'~\[list\:(.+?)\]~is',
145
			'~\[/list\:(.+?)\]~is',
146
			'~\[\*\:(.+?)\]~is',
147
			'~\[/\*\:(.+?)\]~is',
148
			'~\<img src=\"{SMILIES_PATH}/(.+?)\" alt=\"(.+?)\" title=\"(.+?)\" /\>~is',
149
		),
150
		array(
151
			'[quote author="$1"]',
152
			'[quote]',
153
			'[/quote]',
154
			'[b]',
155
			'[/b]',
156
			'[i]',
157
			'[/i]',
158
			'[u]',
159
			'[/u]',
160
			'[url]',
161
			'[/url]',
162
			'[url=$1]',
163
			'[/url]',
164
			'[url=$2]$3[/url]',
165
			'[img]',
166
			'[/img]',
167
			'[size=' . percent_to_px("\1") . 'px]',
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $percent of percent_to_px(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
			'[size=' . percent_to_px(/** @scrutinizer ignore-type */ "\1") . 'px]',
Loading history...
168
			'[/size]',
169
			'[color=$1]',
170
			'[/color]',
171
			'[code=$1]',
172
			'[code]',
173
			'[/code]',
174
			'[list type=$1]',
175
			'[list]',
176
			'[/list]',
177
			'[li]',
178
			'[/li]',
179
			'$2',
180
		), $message);
181
182
	$message = preg_replace('~\[size=(.+?)px\]~is', "[size=" . ('\1' > '99' ? 99 : '"\1"') . "px]", $message);
183
184
	$message = strtr($message, array(
185
		'[list type=1]' => '[list type=decimal]',
186
		'[list type=a]' => '[list type=lower-alpha]',
187
	));
188
189
	return stripslashes($message);
190
}
191
192
function userDataDefine(&$row, $db, $config)
193
{
194
	static $board_timezone = null, $avatar_dir, $attachment_type, $phpbb_avatar_upload_path, $phpbb_avatar_salt;
195
196
	// Try to only do this collection once
197
	if (!isset($board_timezone))
198
	{
199
		$request2 = $db->query("
200
			SELECT 
201
			    config_value
202
			FROM {$config->from_prefix}config
203
			WHERE config_name = 'board_timezone'
204
			LIMIT 1"
205
		);
206
		list ($board_timezone) = $db->fetch_row($request2);
207
		$db->free_result($request2);
208
209
		// Find out where uploaded avatars go
210
		$request2 = $db->query("
211
			SELECT 
212
				value
213
			FROM {$config->to_prefix}settings
214
			WHERE variable = 'custom_avatar_dir'
215
			LIMIT 1"
216
		);
217
		list ($avatar_dir) = $db->fetch_row($request2);
218
		$attachment_type = '1';
219
		$db->free_result($request2);
220
221
		// Not custom so ...
222
		if (empty($avatar_dir))
223
		{
224
			$request2 = $db->query("
225
				SELECT 
226
					value
227
				FROM {$config->to_prefix}settings
228
				WHERE variable = 'attachmentUploadDir'
229
				LIMIT 1");
230
			list ($avatar_dir) = $db->fetch_row($request2);
231
			$attachment_type = '0';
232
			$db->free_result($request2);
233
		}
234
235
		$request2 = $db->query("
236
			SELECT 
237
				config_value
238
			FROM {$config->from_prefix}config
239
			WHERE config_name = 'avatar_path'
240
			LIMIT 1");
241
		$temp = $db->fetch_assoc($request2);
242
		$phpbb_avatar_upload_path = $_POST['path_from'] . '/' . $temp['config_value'];
243
		$db->free_result($request2);
244
245
		$request2 = $db->query("
246
			SELECT 
247
				config_value
248
			FROM {$config->from_prefix}config
249
			WHERE config_name = 'avatar_salt'
250
			LIMIT 1");
251
		$temp = $db->fetch_assoc($request2);
252
		$phpbb_avatar_salt = $temp['config_value'];
253
		$db->free_result($request2);
254
	}
255
256
	// Fix signatures
257
	$row['signature'] = phpbb_replace_bbc(unParse($row['signature'], $row['user_sig_bbcode_uid']));
258
	unset($row['user_sig_bbcode_uid']);
259
260
	// Convert time zones to time offset in hours
261
	$dt = new \DateTime('now', new \DateTimeZone($board_timezone));
262
	$offset_from = $dt->getOffset();
263
	$timestamp = $dt->getTimestamp();
264
	$offset_to = $dt->setTimezone(new \DateTimezone($row['time_offset'] ?? $board_timezone))->setTimestamp($timestamp)->getOffset();
265
	$row['time_offset'] = $offset_to / 3600 - $offset_from / 3600;
266
267
	// Determine Avatars
268
	//	AVATAR_UPLOAD	=> 'avatar.driver.upload', or 1
269
	//	AVATAR_REMOTE	=> 'avatar.driver.remote', or 2
270
	//	AVATAR_GALLERY	=> 'avatar.driver.local', or 3
271
	if (empty($row['user_avatar_type']))
272
	{
273
		$row['avatar'] = '';
274
	}
275
	elseif (($row['user_avatar_type'] === 'avatar.driver.upload' || $row['user_avatar_type'] === '1')
276
		&& !empty($row['avatar']))
277
	{
278
		// If the avatar type is uploaded, copy avatar with the correct name.
279
		$phpbb_avatar_ext = substr(strchr($row['avatar'], '.'), 1);
280
		$elk_avatar_filename = 'avatar_' . $row['id_member'] . strrchr($row['avatar'], '.');
281
282
		if (file_exists($phpbb_avatar_upload_path . '/' . $phpbb_avatar_salt . '_' . $row['id_member'] . '.' . $phpbb_avatar_ext))
283
		{
284
			@copy($phpbb_avatar_upload_path . '/' . $phpbb_avatar_salt . '_' . $row['id_member'] . '.' . $phpbb_avatar_ext, $avatar_dir . '/' . $elk_avatar_filename);
285
		}
286
		else
287
		{
288
			@copy($phpbb_avatar_upload_path . '/' . $row['avatar'], $avatar_dir . '/' . $elk_avatar_filename);
289
		}
290
291
		$id = $row['id_member'];
292
		$db_filename = substr(addslashes($elk_avatar_filename), 0, 255);
293
		$db->query("INSERT INTO {$config->to_prefix}attachments
294
			(id_msg, id_member, filename, attachment_type)
295
			VALUES(0, '$id', '$db_filename', '$attachment_type')"
296
		);
297
298
		$row['avatar'] = '';
299
	}
300
	elseif ($row['user_avatar_type'] === 'avatar.driver.local' || $row['user_avatar_type'] === '3')
301
	{
302
		$row['avatar'] = substr('gallery/' . $row['avatar'], 0, 255);
303
	}
304
305
	unset($row['user_avatar_type']);
306
}
307
308
function unParse($message, $bbcode_uid = null)
309
{
310
	// @todo how should we handle phpbb3 bbcode which uses s9e\TextFormatter
311
	// a simple "[b]My[/b] Signature" will be saved in the db as
312
	// "<r><B><s>[b]</s>My<e>[/b]</e></B> Signature</r>"
313
	// Is it better to leave it alone and use a separate utility to convert the db after ??
314
	if (preg_match('#^<[rt][ >]#', $message))
315
	{
316
		$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
317
		$message = htmlspecialchars($message, ENT_COMPAT);
318
319
		$message = str_replace("\r\n", '<br />', $message);
320
	}
321
	else
322
	{
323
		if ($bbcode_uid)
324
		{
325
			$match = array("[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
326
			$replace = array('', '', '', '');
327
		}
328
		else
329
		{
330
			$match = array("\n", "\r\n");
331
			$replace = array('<br />', '<br />');
332
		}
333
		$message = str_replace($match, $replace, $message);
334
335
		$match = array(
336
			'#<!\-\- e \-\-><a href="mailto:(.*?)">.*?</a><!\-\- e \-\->#',
337
			'#<!\-\- l \-\-><a (?:class="[\w-]+" )?href="(.*?)(?:(&amp;|\?)sid=[0-9a-f]{32})?">.*?</a><!\-\- l \-\->#',
338
			'#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="http://(.*?)">\2</a><!\-\- \1 \-\->#',
339
			'#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)">.*?</a><!\-\- \1 \-\->#',
340
			'#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#',
341
			'#<!\-\- .*? \-\->#s',
342
			'#<.*?>#s',
343
		);
344
		$replace = array('\1', '\1', '\2', '\2', '\1', '', '');
345
		$message = preg_replace($match, $replace, $message);
346
	}
347
348
	return $message;
349
}
350