1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22class CControllerImageCreate extends CController {
23
24	protected function checkInput() {
25		$fields = [
26			'name'      => 'required | not_empty | db images.name',
27			'imagetype' => 'required | fatal | db images.imagetype'
28		];
29
30		$ret = $this->validateInput($fields);
31
32		if (!$ret) {
33			switch ($this->getValidationError()) {
34				case self::VALIDATION_ERROR:
35					$url = (new CUrl('zabbix.php'))
36						->setArgument('action', 'image.edit')
37						->setArgument('imagetype', $this->getInput('imagetype'));
38
39					$response = new CControllerResponseRedirect($url);
40					$response->setFormData($this->getInputAll());
41					CMessageHelper::setErrorTitle(_('Cannot add image'));
42					$this->setResponse($response);
43					break;
44
45				case self::VALIDATION_FATAL_ERROR:
46					$this->setResponse(new CControllerResponseFatal());
47					break;
48			}
49		}
50
51		return $ret;
52	}
53
54	protected function checkPermissions() {
55		if (!$this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL)) {
56			return false;
57		}
58
59		$this->image = [
60			'imageid'   => 0,
61			'imagetype' => $this->getInput('imagetype'),
62			'name'      => $this->getInput('name', '')
63		];
64
65		return true;
66	}
67
68	/**
69	 * @param $error string
70	 *
71	 * @return string|null
72	 */
73	protected function uploadImage(&$error) {
74		try {
75			if (array_key_exists('image', $_FILES)) {
76				$file = new CUploadFile($_FILES['image']);
77
78				if ($file->wasUploaded()) {
79					$file->validateImageSize();
80					return base64_encode($file->getContent());
81				}
82
83				return null;
84			}
85			else {
86				return null;
87			}
88		}
89		catch (Exception $e) {
90			$error = $e->getMessage();
91		}
92
93		return null;
94	}
95
96	protected function doAction() {
97		$image = $this->uploadImage($error);
98
99		if ($error) {
100			$url = (new CUrl('zabbix.php'))
101				->setArgument('action', 'image.edit')
102				->setArgument('imagetype', $this->getInput('imagetype'));
103
104			$response = new CControllerResponseRedirect($url);
105			error($error);
106			$response->setFormData($this->getInputAll());
107			CMessageHelper::setErrorTitle(_('Cannot add image'));
108
109			return $this->setResponse($response);
110		}
111
112		$result = API::Image()->create([
113			'imagetype' => $this->getInput('imagetype'),
114			'name'      => $this->getInput('name'),
115			'image'     => $image
116		]);
117
118		if ($result) {
119			add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_IMAGE, 'Image ['.$this->getInput('name').'] created');
120
121			$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
122				->setArgument('action', 'image.list')
123				->setArgument('imagetype', $this->getInput('imagetype'))
124			);
125			CMessageHelper::setSuccessTitle(_('Image added'));
126		}
127		else {
128			$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
129				->setArgument('action', 'image.edit')
130				->setArgument('imagetype', $this->getInput('imagetype'))
131			);
132			$response->setFormData($this->getInputAll());
133			CMessageHelper::setErrorTitle(_('Cannot add image'));
134		}
135
136		$this->setResponse($response);
137	}
138}
139