Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CUploadFile.php « helpers « classes « include « php « frontends - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2da8854faff7a71583435afb5d03604807a03b93 (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
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CUploadFile {

	protected $name;
	protected $type;
	protected $tmp_name;
	protected $error;
	protected $size;

	/**
	 * Get upload_max_filesize value in bytes defined in php.ini.
	 * If value is not defined in php.ini, returns default 2M.
	 *
	 * @static
	 *
	 * @return int
	 */
	static public function getMaxUploadSize() {
		$maxSize = trim(ini_get('upload_max_filesize'));

		if ($maxSize === '') {
			$maxSize = '2m'; // PHP default value
		}

		return str2mem($maxSize);
	}

	/**
	 * Accepts file array from $_FILES global variable.
	 *
	 * @throws Exception if file_uploads are disabled
	 *
	 * @param $file
	 */
	public function __construct($file) {
		if (!ini_get('file_uploads')) {
			throw new Exception(_('Unable to upload file because "file_uploads" is disabled.'));
		}

		$this->name = $file['name'];
		$this->type = $file['type'];
		$this->tmp_name = $file['tmp_name'];
		$this->error = $file['error'];
		$this->size = $file['size'];
	}

	/**
	 * Get content of uploaded file.
	 *
	 * @throws Exception
	 * @return string
	 */
	public function getContent() {
		if (!$this->isValid()) {
			throw new Exception($this->getErrorMessage());
		}

		return file_get_contents($this->tmp_name);
	}

	/**
	 * Get extension of uploaded file.
	 *
	 * @throws Exception
	 * @return string
	 */
	public function getExtension() {
		if (!$this->isValid()) {
			throw new Exception($this->getErrorMessage());
		}

		return pathinfo($this->name, PATHINFO_EXTENSION);
	}

	/**
	 * Get size in bytes of uploaded file.
	 *
	 * @return int
	 */
	public function getSize() {
		return $this->size;
	}

	/**
	 * Get error.
	 *
	 * @return int
	 */
	public function getError() {
		return $this->error;
	}

	/**
	 * Returns true if file was uploaded without errors.
	 *
	 * @return bool
	 */
	public function isValid() {
		return ($this->error == UPLOAD_ERR_OK);
	}

	/**
	 * Validate image size.
	 *
	 * @throws Exception if image size is 1MB or greater.
	 */
	public function validateImageSize() {
		if ($this->size > ZBX_MAX_IMAGE_SIZE) {
			throw new Exception(_('Image size must be less than 1MB.'));
		}
	}

	/**
	 * Return true if file was uploaded, maybe with errors.
	 *
	 * @return bool
	 */
	public function wasUploaded() {
		return ($this->error != UPLOAD_ERR_NO_FILE);
	}

	/**
	 * Get error message.
	 *
	 * @return string
	 */
	protected function getErrorMessage() {
		switch ($this->error) {
			case UPLOAD_ERR_OK:
				return '';

			case UPLOAD_ERR_INI_SIZE:
				return _s('File is too big, max upload size is %1$s bytes.', self::getMaxUploadSize());

			case UPLOAD_ERR_NO_FILE:
				return _('No file was uploaded.');

			default:
				return _('Incorrect file upload.');
		}
	}
}