m1_init::remove_file_system()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
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\migrations;
14
15
use phpbb\db\migration\migration;
0 ignored issues
show
Bug introduced by
The type phpbb\db\migration\migration 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
17
class m1_init extends migration
18
{
19
	/**
20
	 * Migration dependencies
21
	 *
22
	 * @return array Array of migration dependencies
23
	 */
24
	public static function depends_on(): array
25
	{
26
		return ['\phpbbgallery\core\migrations\release_1_2_0'];
27
	}
28
29
	/**
30
	 * Revert the changes
31
	 *
32
	 * @return array Array of update data
33
	 */
34
	public function revert_data(): array
35
	{
36
		return [
37
				['custom', [[&$this, 'remove_file_system']]],
38
		];
39
	}
40
41
	/**
42
	 * Update data
43
	 *
44
	 * @return array Array of update data
45
	 */
46
	public function update_data(): array
47
	{
48
		return [
49
				['permission.add', ['a_gallery_import', true, 'a_board']],
50
				['module.add', [
51
					'acp',
52
					'PHPBB_GALLERY',
53
					[
54
						'module_basename' => '\phpbbgallery\acpimport\acp\main_module',
55
						'module_langname' => 'ACP_IMPORT_ALBUMS',
56
						'module_mode'     => 'import_images',
57
						'module_auth'     => 'ext_phpbbgallery/acpimport && acl_a_gallery_import',
58
					]
59
				]],
60
				['custom', [[&$this, 'create_file_system']]],
61
		];
62
	}
63
64
	/**
65
	 * Create import directory
66
	 *
67
	 * @return void
68
	 */
69
	public function create_file_system(): void
70
	{
71
		global $phpbb_root_path;
72
73
		$phpbbgallery_import_file = $phpbb_root_path . 'files/phpbbgallery/import';
74
75
		if (!is_dir($phpbbgallery_import_file))
76
		{
77
			if (is_writable($phpbb_root_path . 'files'))
78
			{
79
				@mkdir($phpbbgallery_import_file, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). 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

79
				/** @scrutinizer ignore-unhandled */ @mkdir($phpbbgallery_import_file, 0755, true);

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...
80
			}
81
		}
82
	}
83
84
	/**
85
	 * Remove import directory
86
	 *
87
	 * @return void
88
	 */
89
	public function remove_file_system(): void
90
	{
91
		global $phpbb_root_path;
92
93
		$phpbbgallery_import_file = $phpbb_root_path . 'files/phpbbgallery/import';
94
95
		// Clean dirs
96
		if (is_dir($phpbbgallery_import_file))
97
		{
98
			$this->recursiveRemoveDirectory($phpbbgallery_import_file);
99
		}
100
	}
101
102
	/**
103
	 * Recursively remove a directory
104
	 *
105
	 * @param string $directory Directory path
106
	 * @return void
107
	 */
108
	private function recursiveRemoveDirectory(string $directory): void
109
	{
110
		if (!is_dir($directory))
111
		{
112
				return;
113
		}
114
115
		$files = new \FilesystemIterator($directory);
116
		foreach ($files as $file)
117
		{
118
				if ($file->isDir())
119
				{
120
					$this->recursiveRemoveDirectory($file->getPathname());
121
				}
122
				else
123
				{
124
					@unlink($file->getPathname());
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

124
					/** @scrutinizer ignore-unhandled */ @unlink($file->getPathname());

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...
125
				}
126
		}
127
		@rmdir($directory);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). 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

127
		/** @scrutinizer ignore-unhandled */ @rmdir($directory);

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...
128
	}
129
}
130