SMF1_1   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 70
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableTest() 0 3 1
A setDefines() 0 3 1
A getPrefix() 0 5 1
A getDbName() 0 3 1
A getName() 0 3 1
A getVersion() 0 3 1
A getAttachmentDirs() 0 19 3
A fetchSetting() 0 11 3
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
class SMF1_1 extends Importers\AbstractSourceImporter
11
{
12
	protected $setting_file = '/Settings.php';
13
14
	protected $smf_attach_folders = null;
15
16
	public function getName()
17
	{
18
		return 'SMF1_1';
19
	}
20
21
	public function getVersion()
22
	{
23
		return 'ElkArte 1.0';
24
	}
25
26
	public function setDefines()
27
	{
28
		define('SMF', 1);
29
	}
30
31
	public function getPrefix()
32
	{
33
		$db_name = $this->getDbName();
34
		$db_prefix = $this->fetchSetting('db_prefix');
35
		return '`' . $db_name . '`.' . $db_prefix;
36
	}
37
38
	public function getDbName()
39
	{
40
		return $this->fetchSetting('db_name');
41
	}
42
43
	public function getTableTest()
44
	{
45
		return 'members';
46
	}
47
48
	public function fetchSetting($name)
49
	{
50
		static $content = null;
51
52
		if ($content === null)
53
			$content = file_get_contents($this->path . '/Settings.php');
54
55
		$match = array();
56
		preg_match('~\$' . $name . '\s*=\s*\'(.*?)\';~', $content, $match);
57
58
		return isset($match[1]) ? $match[1] : '';
59
	}
60
61
	public function getAttachmentDirs()
62
	{
63
		if ($this->smf_attach_folders === null)
64
		{
65
			$from_prefix = $this->config->from_prefix;
66
67
			$request = $this->db->query("
68
				SELECT value
69
				FROM {$from_prefix}settings
70
				WHERE variable='attachmentUploadDir';");
71
			list ($smf_attachments_dir) = $this->db->fetch_row($request);
72
73
			$this->smf_attach_folders = @unserialize($smf_attachments_dir);
74
75
			if (!is_array($this->smf_attach_folders))
76
				$this->smf_attach_folders = array(1 => $smf_attachments_dir);
77
		}
78
79
		return $this->smf_attach_folders;
80
	}
81
}
82
83
function moveAttachment(&$row, $db, $from_prefix, $attachmentUploadDir)
84
{
85
	static $smf_folders = null;
86
87
	if ($smf_folders === null)
88
	{
89
		$request = $db->query("
90
			SELECT value
91
			FROM {$from_prefix}settings
92
			WHERE variable='attachmentUploadDir';");
93
		list ($smf_attachments_dir) = $db->fetch_row($request);
94
95
		$smf_folders = @unserialize($smf_attachments_dir);
96
		if (!is_array($smf_folders))
97
			$smf_folders = array(1 => $smf_attachments_dir);
98
	}
99
100
	// If something is broken, better try to account for it as well.
101
	if (isset($row['id_folder']) && isset($smf_folders[$row['id_folder']]))
102
		$smf_attachments_dir = $smf_folders[$row['id_folder']];
103
	else
104
		$smf_attachments_dir = $smf_folders[1];
105
106
	if (empty($row['file_hash']))
107
	{
108
		$row['file_hash'] = createAttachmentFileHash($row['filename']);
109
		$source_file = $row['filename'];
110
	}
111
	else
112
		$source_file = $row['id_attach'] . '_' . $row['file_hash'];
113
114
	if (empty($row['mime_type']))
115
	{
116
		$fileext = '';
117
		$mimetype = '';
118
		$is_thumb = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $is_thumb is dead and can be removed.
Loading history...
119
120
		if (preg_match('/\.(jpg|jpeg|gif|png)(_thumb)?$/i',$row['filename'],$m))
121
		{
122
			$fileext = strtolower($m[1]);
123
			$is_thumb = !empty($m[2]);
124
125
			if (empty($row['mime_type']))
126
			{
127
				// AFAIK, all thumbnails got created as PNG
128
				if ($is_thumb) $mimetype = 'image/png';
129
				elseif ($fileext == 'jpg') $mimetype = 'image/jpeg';
130
				else $mimetype = 'image/'.$fileext;
131
			}
132
		}
133
		else if (preg_match('/\.([a-z][a-z0-9]*)$/i',$row['filename'],$m))
134
		{
135
			$fileext = strtolower($m[1]);
136
		}
137
138
		if (empty($row['fileext'])) $row['fileext'] = $fileext;
139
140
		// try using getimagesize to calculate the mime type, otherwise use the $mimetype set from above
141
		$size = @getimagesize($row['filename']);
142
143
		$row['mime_type'] = empty($size['mime']) ? $mimetype : $size['mime'];
144
	}
145
146
	copy_file($smf_attachments_dir . '/' . $source_file, $attachmentUploadDir . '/' . $row['id_attach'] . '_' . $row['file_hash'] . '.elk');
147
}
148