aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/helper/file_updater/file_updater.php
blob: cc0f5c6b5fc6abc72a5cbb50651767f3d6575c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\install\helper\file_updater;

use phpbb\filesystem\exception\filesystem_exception;
use phpbb\filesystem\filesystem;
use phpbb\install\exception\file_updater_failure_exception;

/**
 * File updater for direct filesystem access
 */
class file_updater implements file_updater_interface
{
	/**
	 * @var filesystem
	 */
	protected $filesystem;

	/**
	 * @var string
	 */
	protected $phpbb_root_path;

	/**
	 * Constructor
	 *
	 * @param filesystem	$filesystem
	 * @param string		$phpbb_root_path
	 */
	public function __construct(filesystem $filesystem, $phpbb_root_path)
	{
		$this->filesystem		= $filesystem;
		$this->phpbb_root_path	= $phpbb_root_path;
	}

	/**
	 * {@inheritdoc}
	 *
	 * @throws file_updater_failure_exception	When the file is not writable
	 * @throws filesystem_exception				When the filesystem class fails
	 */
	public function delete_file($path_to_file)
	{
		$this->filesystem->remove($this->phpbb_root_path . $path_to_file);
	}

	/**
	 * {@inheritdoc}
	 *
	 * @throws file_updater_failure_exception	When the file is not writable
	 * @throws filesystem_exception				When the filesystem class fails
	 */
	public function create_new_file($path_to_file_to_create, $source, $create_from_content = false)
	{
		$path_to_file_to_create = $this->phpbb_root_path . $path_to_file_to_create;

		$dir = dirname($path_to_file_to_create);
		if (!$this->filesystem->exists($dir))
		{
			$this->make_dir($dir);
		}

		$original_dir_perms = false;

		if (!$this->filesystem->is_writable($dir))
		{
			// Extract last 9 bits we actually need
			$original_dir_perms = @fileperms($dir) & 511;
			$this->filesystem->phpbb_chmod($dir, filesystem::CHMOD_ALL);
		}

		if (!$create_from_content)
		{
			try
			{
				$this->filesystem->copy($source, $path_to_file_to_create);
			}
			catch (filesystem_exception $e)
			{
				$this->write_file($path_to_file_to_create, $source, $create_from_content);
			}
		}
		else
		{
			$this->write_file($path_to_file_to_create, $source, $create_from_content);
		}

		if ($original_dir_perms !== false)
		{
			$this->filesystem->phpbb_chmod($dir, $original_dir_perms);
		}
	}

	/**
	 * {@inheritdoc}
	 *
	 * @throws file_updater_failure_exception	When the file is not writable
	 * @throws filesystem_exception				When the filesystem class fails
	 */
	public function update_file($path_to_file_to_update, $source, $create_from_content = false)
	{
		$path_to_file_to_update = $this->phpbb_root_path . $path_to_file_to_update;
		$original_file_perms = false;

		// Maybe necessary for binary files
		$dir = dirname($path_to_file_to_update);
		if (!$this->filesystem->exists($dir))
		{
			$this->make_dir($dir);
		}

		if (!$this->filesystem->is_writable($path_to_file_to_update))
		{
			// Extract last 9 bits we actually need
			$original_file_perms = @fileperms($path_to_file_to_update) & 511;
			$this->filesystem->phpbb_chmod($path_to_file_to_update, filesystem::CHMOD_WRITE);
		}

		if (!$create_from_content)
		{
			try
			{
				$this->filesystem->copy($source, $path_to_file_to_update, true);
			}
			catch (filesystem_exception $e)
			{
				$this->write_file($path_to_file_to_update, $source, $create_from_content);
			}
		}
		else
		{
			$this->write_file($path_to_file_to_update, $source, $create_from_content);
		}

		if ($original_file_perms !== false)
		{
			$this->filesystem->phpbb_chmod($path_to_file_to_update, $original_file_perms);
		}
	}

	/**
	 * Creates directory structure
	 *
	 * @param string	$path	Path to the directory where the file should be placed (and non-existent)
	 */
	private function make_dir($path)
	{
		if (is_dir($path))
		{
			return;
		}

		$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
		$this->filesystem->mkdir($path, 493); // 493 === 0755
	}

	/**
	 * Fallback function for file writing
	 *
	 * @param string		$path_to_file			Path to the file's location
	 * @param string		$source					Path to file to copy or string with the new file's content
	 * @param bool|false	$create_from_content	Whether or not to use $source as the content, false by default
	 *
	 * @throws file_updater_failure_exception	When the file is not writable
	 */
	private function write_file($path_to_file, $source, $create_from_content = false)
	{
		if (!$create_from_content)
		{
			$source = @file_get_contents($source);
		}

		$file_pointer = @fopen($path_to_file, 'w');

		if (!is_resource($file_pointer))
		{
			throw new file_updater_failure_exception();
		}

		@fwrite($file_pointer, $source);
		@fclose($file_pointer);
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_method_name()
	{
		return 'direct_file';
	}
}