main_module::import()   F
last analyzed

Complexity

Conditions 64
Paths > 20000

Size

Total Lines 367
Code Lines 206

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 4160

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 206
c 3
b 1
f 0
dl 0
loc 367
rs 0
ccs 0
cts 295
cp 0
cc 64
nc 1016376
nop 0
crap 4160

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * phpBB Gallery - ACP Import Extension
4
 *
5
 * @package   phpbbgallery/acpimport
6
 * @author    nickvergessen
7
 * @author    satanasov
8
 * @author    Leinad4Mind
9
 * @copyright 2007-2012 nickvergessen, 2014- satanasov, 2018- Leinad4Mind
10
 * @license   GPL-2.0-only
11
 */
12
13
namespace phpbbgallery\acpimport\acp;
14
15
class main_module
16
{
17
	var $u_action;
18
19
	function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed. ( Ignorable by Annotation )

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

19
	function main($id, /** @scrutinizer ignore-unused */ $mode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

19
	function main(/** @scrutinizer ignore-unused */ $id, $mode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
	{
21
		global $auth, $cache, $config, $db, $template, $user, $phpEx, $phpbb_root_path, $phpbb_container, $gallery_url, $gallery_config, $gallery_album;
22
23
		$gallery_url = $phpbb_container->get('phpbbgallery.core.url');
24
		$gallery_config = $phpbb_container->get('phpbbgallery.core.config');
25
		$gallery_album = $phpbb_container->get('phpbbgallery.core.album');
26
		$gallery_url->_include('functions_display', 'phpbb');
27
28
		$user->add_lang_ext('phpbbgallery/core', array('gallery_acp', 'gallery'));
29
		$this->tpl_name = 'gallery_acpimport';
0 ignored issues
show
Bug Best Practice introduced by
The property tpl_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
		add_form_key('acp_gallery');
0 ignored issues
show
Bug introduced by
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

30
		/** @scrutinizer ignore-call */ 
31
  add_form_key('acp_gallery');
Loading history...
31
32
		$this->page_title = $user->lang['ACP_IMPORT_ALBUMS'];
0 ignored issues
show
Bug Best Practice introduced by
The property page_title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
		$this->import();
34
	}
35
36
	function import()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
	{
38
		global $db, $template, $user, $phpbb_dispatcher, $phpbb_container, $gallery_url, $request, $table_prefix ,$gallery_config, $gallery_album, $request;
39
40
		$import_schema = $request->variable('import_schema', '');
41
		$images = $request->variable('images', array(''), true);
42
43
		$submit = (isset($_POST['submit'])) ? true : ((empty($images)) ? false : true);
44
45
		if ($import_schema)
46
		{
47
			if ($gallery_url->_file_exists($import_schema, 'import', ''))
48
			{
49
				include($gallery_url->_return_file($import_schema, 'import', ''));
50
				// Replace the md5 with the ' again and remove the space at the end to prevent \' troubles
51
				$user_data['username'] = utf8_substr(str_replace("{{$import_schema}}", "'", $user_data['username']), 0, -1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_data seems to be never defined.
Loading history...
Bug introduced by
The function utf8_substr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

51
				$user_data['username'] = /** @scrutinizer ignore-call */ utf8_substr(str_replace("{{$import_schema}}", "'", $user_data['username']), 0, -1);
Loading history...
Comprehensibility Best Practice introduced by
$user_data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $user_data = array(); before regardless.
Loading history...
52
				$image_name = utf8_substr(str_replace("{{$import_schema}}", "'", $image_name), 0, -1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_name seems to be never defined.
Loading history...
53
			}
54
			else
55
			{
56
				global $phpEx;
57
				trigger_error($user->lang('MISSING_IMPORT_SCHEMA', ($import_schema . '.' . $phpEx)), E_USER_WARNING);
58
			}
59
60
			$images_loop = 0;
61
			foreach ($images as $image_src)
62
			{
63
				/**
64
				* Import the images
65
				*/
66
67
				$image_src = str_replace("{{$import_schema}}", "'", $image_src);
68
				$image_src_full = $gallery_url->path('import') . utf8_decode($image_src);
69
				if (file_exists($image_src_full))
70
				{
71
					$filetype = getimagesize($image_src_full);
72
					$filetype_ext = '';
73
74
					$error_occurred = false;
75
					switch ($filetype['mime'])
76
					{
77
						case 'image/jpeg':
78
						case 'image/jpg':
79
						case 'image/pjpeg':
80
							$filetype_ext = '.jpg';
81
							$read_function = 'imagecreatefromjpeg';
82
							if ((substr(strtolower($image_src), -4) != '.jpg') && (substr(strtolower($image_src), -5) != '.jpeg'))
83
							{
84
								$this->log_import_error($import_schema, sprintf($user->lang['FILETYPE_MIMETYPE_MISMATCH'], $image_src, $filetype['mime']));
85
								$error_occurred = true;
86
							}
87
						break;
88
89
						case 'image/png':
90
						case 'image/x-png':
91
							$filetype_ext = '.png';
92
							$read_function = 'imagecreatefrompng';
93
							if (substr(strtolower($image_src), -4) != '.png')
94
							{
95
								$this->log_import_error($import_schema, sprintf($user->lang['FILETYPE_MIMETYPE_MISMATCH'], $image_src, $filetype['mime']));
96
								$error_occurred = true;
97
							}
98
						break;
99
100
						case 'image/gif':
101
						case 'image/giff':
102
							$filetype_ext = '.gif';
103
							$read_function = 'imagecreatefromgif';
104
							if (substr(strtolower($image_src), -4) != '.gif')
105
							{
106
								$this->log_import_error($import_schema, sprintf($user->lang['FILETYPE_MIMETYPE_MISMATCH'], $image_src, $filetype['mime']));
107
								$error_occurred = true;
108
							}
109
						break;
110
111
						case 'image/webp':
112
							$filetype_ext = '.webp';
113
							$read_function = 'imagecreatefromwebp';
114
							if (substr(strtolower($image_src), -5) != '.webp')
115
							{
116
								$this->log_import_error($import_schema, sprintf($user->lang['FILETYPE_MIMETYPE_MISMATCH'], $image_src, $filetype['mime']));
117
								$error_occurred = true;
118
							}
119
						break;
120
121
						default:
122
							$this->log_import_error($import_schema, $user->lang['NOT_ALLOWED_FILE_TYPE']);
123
							$error_occurred = true;
124
						break;
125
					}
126
					$image_filename = md5(unique_id()) . $filetype_ext;
0 ignored issues
show
Bug introduced by
The function unique_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
					$image_filename = md5(/** @scrutinizer ignore-call */ unique_id()) . $filetype_ext;
Loading history...
127
					$file_link = $gallery_url->path('upload') . $image_filename;
128
129
					if (!$error_occurred || !@move_uploaded_file($image_src_full, $file_link))
130
					{
131
						if (!@copy($image_src_full, $file_link))
132
						{
133
							$user->add_lang('posting');
134
							$this->log_import_error($import_schema, sprintf($user->lang['GENERAL_UPLOAD_ERROR'], $file_link));
135
							$error_occurred = true;
136
						}
137
					}
138
139
					if (!$error_occurred)
140
					{
141
						@chmod($file_link, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

141
						/** @scrutinizer ignore-unhandled */ @chmod($file_link, 0777);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
142
143
						$sql_ary = array(
144
							'image_filename' 		=> $image_filename,
145
							'image_desc'			=> '',
146
							'image_desc_uid'		=> '',
147
							'image_desc_bitfield'	=> '',
148
							'image_user_id'			=> $user_data['user_id'],
149
							'image_username'		=> $user_data['username'],
150
							'image_username_clean'	=> utf8_clean_string($user_data['username']),
0 ignored issues
show
Bug introduced by
The function utf8_clean_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

150
							'image_username_clean'	=> /** @scrutinizer ignore-call */ utf8_clean_string($user_data['username']),
Loading history...
151
							'image_user_colour'		=> $user_data['user_colour'],
152
							'image_user_ip'			=> $user->ip,
153
							'image_time'			=> $start_time + $done_images,
154
							'image_album_id'		=> $album_id,
155
							'image_status'			=> (int) \phpbbgallery\core\block::STATUS_APPROVED,
156
							//'image_exif_data'		=> '',
157
						);
158
159
						$image_tools = $phpbb_container->get('phpbbgallery.core.file.tool');
160
						$image_tools->set_image_options($gallery_config->get('max_filesize'), $gallery_config->get('max_height'), $gallery_config->get('max_width'));
161
						$image_tools->set_image_data($file_link);
162
163
						$additional_sql_data = [];
164
165
						/**
166
						* Event to trigger before mass update
167
						*
168
						* @event phpbbgallery.acpimport.update_image_before
169
						* @var	array	additional_sql_data		array of additional sql_data
170
						* @var	string	file_link				String with real file link
171
						* @since 1.2.0
172
						*/
173
						$vars = array('additional_sql_data', 'file_link');
174
						extract($phpbb_dispatcher->trigger_event('phpbbgallery.acpimport.update_image_before', compact($vars)));
175
176
						if (($filetype[0] > $gallery_config->get('max_width')) || ($filetype[1] > $gallery_config->get('max_height')))
177
						{
178
							/**
179
							* Resize oversize images
180
							*/
181
							if ($gallery_config->get('allow_resize'))
182
							{
183
								$image_tools->resize_image($gallery_config->get('max_width'), $gallery_config->get('max_height'));
184
								if ($image_tools->resized)
185
								{
186
									$image_tools->write_image($file_link, $gallery_config->get('jpg_quality'), true);
187
								}
188
							}
189
						}
190
						$file_updated = (bool) $image_tools->resized;
191
192
						/**
193
						* Event to trigger before mass update
194
						*
195
						* @event phpbbgallery.acpimport.update_image
196
						* @var	array	additional_sql_data		array of additional sql_data
197
						* @var	bool	file_updated			is file resized
198
						* @since 1.2.0
199
						*/
200
						$vars = array('additional_sql_data', 'file_updated');
201
						extract($phpbb_dispatcher->trigger_event('phpbbgallery.acpimport.update_image', compact($vars)));
202
203
						$sql_ary = array_merge($sql_ary, $additional_sql_data);
204
205
						// Try to get real filesize from temporary folder (not always working) ;)
206
						$sql_ary['filesize_upload'] = (@filesize($file_link)) ? @filesize($file_link) : 0;
207
208
						if ($filename || ($image_name == ''))
209
						{
210
							$sql_ary['image_name'] = str_replace("_", " ", utf8_substr($image_src, 0, utf8_strrpos($image_src, '.')));
0 ignored issues
show
Bug introduced by
The function utf8_strrpos was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

210
							$sql_ary['image_name'] = str_replace("_", " ", utf8_substr($image_src, 0, /** @scrutinizer ignore-call */ utf8_strrpos($image_src, '.')));
Loading history...
211
						}
212
						else
213
						{
214
							$sql_ary['image_name'] = str_replace('{NUM}', $num_offset + $done_images, $image_name);
215
						}
216
						$sql_ary['image_name_clean'] = utf8_clean_string($sql_ary['image_name']);
217
218
						// Put the images into the database
219
						$db->sql_query('INSERT INTO ' . $table_prefix . 'gallery_images ' . $db->sql_build_array('INSERT', $sql_ary));
220
						// If the source image is imported, we delete it.
221
						if (file_exists($image_src_full))
222
						{
223
							@unlink($image_src_full);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

223
							/** @scrutinizer ignore-unhandled */ @unlink($image_src_full);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
224
						}
225
					}
226
					$done_images++;
227
				}
228
229
				// Remove the image from the list
230
				unset($images[$images_loop]);
231
				$images_loop++;
232
				if ($images_loop == 10)
233
				{
234
					// We made 10 images, so we end for this turn
235
					break;
236
				}
237
			}
238
			if ($images_loop)
239
			{
240
				$image_user = $phpbb_container->get('phpbbgallery.core.user');
241
				$image_user->set_user_id($user_data['user_id']);
242
				$image_user->update_images($images_loop);
243
244
				$gallery_config->inc('num_images', $images_loop);
245
				$todo_images = $todo_images - $images_loop;
246
			}
247
			$gallery_album->update_info($album_id);
248
249
			if (!$todo_images)
250
			{
251
				unlink($gallery_url->_return_file($import_schema, 'import', ''));
252
				$errors = @file_get_contents($gallery_url->_return_file($import_schema . '_errors', 'import', ''));
253
				@unlink($gallery_url->_return_file($import_schema . '_errors', 'import', ''));
254
				if (!$errors)
255
				{
256
					trigger_error(sprintf($user->lang['IMPORT_FINISHED'], $done_images) . adm_back_link($this->u_action));
0 ignored issues
show
Bug introduced by
The function adm_back_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

256
					trigger_error(sprintf($user->lang['IMPORT_FINISHED'], $done_images) . /** @scrutinizer ignore-call */ adm_back_link($this->u_action));
Loading history...
257
				}
258
				else
259
				{
260
					$errors = explode("\n", $errors);
261
					trigger_error(sprintf($user->lang['IMPORT_FINISHED_ERRORS'], $done_images - sizeof($errors)) . implode('<br />', $errors) . adm_back_link($this->u_action), E_USER_WARNING);
262
				}
263
			}
264
			else
265
			{
266
				// Write the new list
267
				$this->create_import_schema($import_schema, $album_id, $user_data, $start_time, $num_offset, $done_images, $todo_images, $image_name, $filename, $images);
268
269
				// Redirect
270
				$forward_url = $this->u_action . "&amp;import_schema=$import_schema";
271
				meta_refresh(1, $forward_url);
0 ignored issues
show
Bug introduced by
The function meta_refresh was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

271
				/** @scrutinizer ignore-call */ 
272
    meta_refresh(1, $forward_url);
Loading history...
272
				trigger_error(sprintf($user->lang['IMPORT_DEBUG_MES'], $done_images, $todo_images));
273
			}
274
		}
275
		else if ($submit)
276
		{
277
			if (!check_form_key('acp_gallery'))
0 ignored issues
show
Bug introduced by
The function check_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

277
			if (!/** @scrutinizer ignore-call */ check_form_key('acp_gallery'))
Loading history...
278
			{
279
				trigger_error('FORM_INVALID', E_USER_WARNING);
280
			}
281
			if (!$images)
282
			{
283
				trigger_error('NO_FILE_SELECTED', E_USER_WARNING);
284
			}
285
286
			// Who is the uploader?
287
			$username = $request->variable('username', '', true);
288
			$user_id = 0;
289
			if ($username)
290
			{
291
				if (!function_exists('user_get_id_name'))
292
				{
293
					$gallery_url->_include('functions_user', 'phpbb');
294
				}
295
				user_get_id_name($user_id, $username);
296
			}
297
			if (is_array($user_id))
0 ignored issues
show
introduced by
The condition is_array($user_id) is always false.
Loading history...
298
			{
299
				$user_id = $user_id[0];
300
			}
301
			if (!$user_id)
302
			{
303
				$user_id = $user->data['user_id'];
304
			}
305
306
			$sql = 'SELECT username, user_colour, user_id
307
				FROM ' . USERS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\acpimport\acp\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
308
				WHERE user_id = ' . (int) $user_id;
309
			$result = $db->sql_query($sql);
310
			$user_row = $db->sql_fetchrow($result);
311
			$db->sql_freeresult($result);
312
			if (!$user_row)
313
			{
314
				trigger_error('HACKING_ATTEMPT', E_USER_WARNING);
315
			}
316
317
			$album_id = $request->variable('album_id', 0);
318
			if (isset($_POST['users_pega']))
319
			{
320
				$image_user =  $phpbb_container->get('phpbbgallery.core.user');
321
				$image_user->set_user_id($user_row['user_id']);
322
				if ($user->data['user_id'] != $user_row['user_id'])
323
				{
324
					$album_id = $image_user->get_data('personal_album_id');
325
					if (!$album_id)
326
					{
327
						// The User has no personal album
328
						$album_id = $gallery_album->generate_personal_album($user_row['username'], $user_row['user_id'], $user_row['user_colour'], $image_user);
329
					}
330
					unset($image_user);
331
				}
332
				else
333
				{
334
					$album_id = $image_user->get_data('personal_album_id');
335
					if (!$album_id)
336
					{
337
						$album_id = $gallery_album->generate_personal_album($user_row['username'], $user_row['user_id'], $user_row['user_colour'], $image_user);
338
					}
339
				}
340
			}
341
342
			// Where do we put them to?
343
			$sql = 'SELECT album_id, album_name
344
				FROM ' . $table_prefix . 'gallery_albums
345
				WHERE album_id = ' . (int) $album_id;
346
			$result = $db->sql_query($sql);
347
			$album_row = $db->sql_fetchrow($result);
348
			$db->sql_freeresult($result);
349
			if (!$album_row)
350
			{
351
				trigger_error('HACKING_ATTEMPT', E_USER_WARNING);
352
			}
353
354
			$start_time = time();
355
			$import_schema = md5($start_time);
356
			$filename = ($request->variable('filename', '') == 'filename') ? true : false;
357
			$image_name = $request->variable('image_name', '', true);
358
			$num_offset = $request->variable('image_num', 0);
359
360
			$this->create_import_schema($import_schema, $album_row['album_id'], $user_row, $start_time, $num_offset, 0, sizeof($images), $image_name, $filename, $images);
361
362
			$forward_url = $this->u_action . "&amp;import_schema=$import_schema";
363
			meta_refresh(2, $forward_url);
364
			trigger_error('IMPORT_SCHEMA_CREATED');
365
		}
366
367
		$handle = opendir($gallery_url->path('import'));
368
		$files = array();
369
		while ($file = readdir($handle))
370
		{
371
			if (!is_dir($gallery_url->path('import') . $file) && (
372
			((substr(strtolower($file), -5) == '.webp') && $gallery_config->get('allow_webp')) ||
373
			((substr(strtolower($file), -4) == '.png') && $gallery_config->get('allow_png')) ||
374
			((substr(strtolower($file), -4) == '.gif') && $gallery_config->get('allow_gif')) ||
375
			((substr(strtolower($file), -4) == '.jpg') && $gallery_config->get('allow_jpg')) ||
376
			((substr(strtolower($file), -5) == '.jpeg') && $gallery_config->get('allow_jpg'))
377
			))
378
			{
379
				$files[utf8_strtolower($file)] = $file;
0 ignored issues
show
Bug introduced by
The function utf8_strtolower was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

379
				$files[/** @scrutinizer ignore-call */ utf8_strtolower($file)] = $file;
Loading history...
380
			}
381
		}
382
		closedir($handle);
383
384
		// Sort the files by name again
385
		ksort($files);
386
		foreach ($files as $file)
387
		{
388
			// Get file name encoding
389
			$encoding = mb_detect_encoding($file, ['UTF-8', 'ISO-8859-1', 'Windows-1252'], true);
390
			$template->assign_block_vars('imagerow', [
391
				'FILE_NAME'	=> $encoding === 'UTF-8' ? $file : mb_convert_encoding($file, 'UTF-8', $encoding),
392
			]);
393
		}
394
395
		$template->assign_vars(array(
396
			'S_IMPORT_IMAGES'				=> true,
397
			'ACP_GALLERY_TITLE'				=> $user->lang['ACP_IMPORT_ALBUMS'],
398
			'ACP_GALLERY_TITLE_EXPLAIN'		=> $user->lang['ACP_IMPORT_ALBUMS_EXPLAIN'],
399
			'L_IMPORT_DIR_EMPTY'			=> sprintf($user->lang['IMPORT_DIR_EMPTY'], $gallery_url->path('import')),
400
			'S_ALBUM_IMPORT_ACTION'			=> $this->u_action,
401
			'S_SELECT_IMPORT' 				=> $gallery_album->get_albumbox(false, 'album_id', false, false, false, (int) \phpbbgallery\core\block::PUBLIC_ALBUM, (int) \phpbbgallery\core\block::TYPE_UPLOAD),
402
			'U_FIND_USERNAME'				=> $gallery_url->append_sid('phpbb', 'memberlist', 'mode=searchuser&amp;form=acp_gallery&amp;field=username&amp;select_single=true'),
403
		));
404
	}
405
406
	function create_import_schema($import_schema, $album_id, $user_row, $start_time, $num_offset, $done_images, $todo_images, $image_name, $filename, $images)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
407
	{
408
		global $gallery_url;
409
410
		$import_file = "<?php\n\nif (!defined('IN_PHPBB'))\n{\n	exit;\n}\n\n";
411
		$import_file .= "\$album_id = " . $album_id . ";\n";
412
		$import_file .= "\$start_time = " . $start_time . ";\n";
413
		$import_file .= "\$num_offset = " . $num_offset . ";\n";
414
		$import_file .= "\$done_images = " . $done_images . ";\n";
415
		$import_file .= "\$todo_images = " . $todo_images . ";\n";
416
		// We add a space at the end of the name, to not get troubles with \';
417
		$import_file .= "\$image_name = '" . str_replace("'", "{{$import_schema}}", $image_name) . " ';\n";
418
		$import_file .= "\$filename = " . (($filename) ? 'true' : 'false') . ";\n";
419
		$import_file .= "\$user_data = array(\n";
420
		$import_file .= "	'user_id'		=> " . $user_row['user_id'] . ",\n";
421
		// We add a space at the end of the name, to not get troubles with \',
422
		$import_file .= "	'username'		=> '" . str_replace("'", "{{$import_schema}}", $user_row['username']) . " ',\n";
423
		$import_file .= "	'user_colour'	=> '" . $user_row['user_colour'] . "',\n";
424
		$import_file .= ");\n";
425
		$import_file .= "\$images = array(\n";
426
427
		// We need to replace some characters to find the image and not produce syntax errors
428
		$replace_chars = array("'", "&amp;");
429
		$replace_with = array("{{$import_schema}}", "&");
430
431
		foreach ($images as $image_src)
432
		{
433
			$import_file .= "	'" . str_replace($replace_chars, $replace_with, $image_src) . "',\n";
434
		}
435
		$import_file .= ");\n\n?" . '>'; // Done this to prevent highlighting editors getting confused!
436
437
		// Write to disc
438
		if (($gallery_url->_file_exists($import_schema, 'import', '') && $gallery_url->_is_writable($import_schema, 'import', '')) || $gallery_url->_is_writable('', 'import', ''))
439
		{
440
			$written = true;
441
			if (!($fp = @fopen($gallery_url->_return_file($import_schema, 'import', ''), 'w')))
442
			{
443
				$written = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $written is dead and can be removed.
Loading history...
444
			}
445
			if (!(@fwrite($fp, $import_file)))
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $stream of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

445
			if (!(@fwrite(/** @scrutinizer ignore-type */ $fp, $import_file)))
Loading history...
446
			{
447
				$written = false;
448
			}
449
			@fclose($fp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fclose(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

449
			/** @scrutinizer ignore-unhandled */ @fclose($fp);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like $fp can also be of type false; however, parameter $stream of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

449
			@fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
450
		}
451
	}
452
453
	function log_import_error($import_schema, $error)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
454
	{
455
		global $gallery_url;
456
457
		$error_file = $gallery_url->_return_file($import_schema . '_errors', 'import', '');
458
		$content = @file_get_contents($error_file);
459
		file_put_contents($error_file, $content .= (($content) ? "\n" : '') . $error);
460
	}
461
}
462