Completed
Push — develop ( c72359...458179 )
by Daniel
10:45
created

upload::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2017 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\controller;
11
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
14
class upload
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\files\factory */
20
	protected $files_factory;
21
22
	/** @var \phpbb\filesystem\filesystem */
23
	protected $filesystem;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\user */
29
	protected $user;
30
31
	/** @var string */
32
	protected $phpbb_root_path;
33
34
	/** @var string */
35
	protected $upload_dir = 'images/sitemaker_uploads/source/';
36
37
	/** @var array */
38
	protected $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'svg');
39 5
40
	/**
41 5
	 * Constructor
42 5
	 *
43 5
	 * @param \phpbb\auth\auth					$auth				Auth object
44 5
	 * @param \phpbb\files\factory				$files_factory		Files factory object
45 5
	 * @param \phpbb\filesystem\filesystem		$filesystem			File system
46
	 * @param \phpbb\language\language			$language			Language object
47
	 * @param \phpbb\user						$user				User object
48
	 * @param string							$phpbb_root_path	phpBB root path
49
	 */
50 5
	public function __construct(\phpbb\auth\auth $auth, \phpbb\files\factory $files_factory, \phpbb\filesystem\filesystem $filesystem, \phpbb\language\language $language, \phpbb\user $user, $phpbb_root_path)
51
	{
52
		$this->auth = $auth;
53 5
		$this->files_factory = $files_factory;
54 5
		$this->filesystem = $filesystem;
55 5
		$this->language = $language;
56
		$this->user = $user;
57 5
		$this->phpbb_root_path = $phpbb_root_path;
58 5
	}
59 1
60 1
	/**
61
	 * @return \Symfony\Component\HttpFoundation\Response
62
	 */
63 4
	public function handle()
64
	{
65 4
		$json_data = array(
66 4
			'location'	=> '',
67 1
			'message'   => '',
68 1
		);
69 1
70
		if (!$this->auth->acl_get('u_sm_filemanager'))
71
		{
72 3
			$json_data['message'] = $this->language->lang('NOT_AUTHORISED');
73
			return new JsonResponse($json_data, 401);
74
		}
75 4
76
		$this->handle_upload($json_data);
77
78
		return new JsonResponse($json_data);
79
	}
80
81
	/**
82 5
	 * @param array $json_data
83
	 * @return void
84 5
	 */
85 5
	protected function handle_upload(array &$json_data)
86
	{
87
		$file = $this->files_factory->get('files.upload')
88
			->set_disallowed_content(array())
89
			->set_allowed_extensions($this->allowed_extensions)
90
			->handle_upload('files.types.form', 'file');
91 4
92
		$this->set_filename($file);
93 4
94 4
		$user_dir = $this->get_user_dir();
95
		$destination = rtrim($this->upload_dir . $user_dir, '/');
96 4
		$file->move_file($destination, true);
97 4
98 2
		if (sizeof($file->error))
99 2
		{
100 2
			$file->remove();
101
			$json_data['message'] = implode('<br />', $file->error);
102 4
		}
103 4
		else
104
		{
105
			$json_data['location'] = $user_dir . $file->get('realname');
106
		}
107
	}
108 4
109
	/**
110 4
	 * @return string
111
	 */
112 4
	protected function get_user_dir()
113 4
	{
114 4
		$user_dir = '';
115 4
116
		// if user does not have root access, they must have a directory
117 4
		if (!$this->auth->acl_get('a_sm_filemanager'))
118 4
		{
119
			$user_dir = 'users/' . $this->user->data['username'] . '/';
120 4
121
			$destination = $this->phpbb_root_path . $this->upload_dir . $user_dir;
122
123
			if (!is_dir($destination))
124
			{
125
				$this->filesystem->mkdir($destination, 0755);
126
			}
127
		}
128
129
		return $user_dir;
130
	}
131
132
	/**
133
	 * @param \phpbb\files\filespec $file
134
	 * @return void
135
	 */
136
	protected function set_filename(\phpbb\files\filespec &$file)
137
	{
138
		$mode = 'real';
139
		$prefix = '';
140
141
		if (preg_match('/^(blobid|imagetools)\d?/i', $file->get('realname')))
142
		{
143
			$mode = 'unique';
144
			$prefix = 'sm_';
145
		}
146
147
		$file->clean_filename($mode, $prefix);
148
	}
149
150
	/**
151
	 * @param array $allowed_extensions
152
	 * @return void
153
	 */
154
	public function set_allowed_extensions(array $allowed_extensions)
155
	{
156
		$this->allowed_extensions = $allowed_extensions;
157
	}
158
}
159