PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.6
VikBooking Hotel Booking Engine & PMS v1.8.6
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / chat / attachment.php

attachment.php in VikBooking Hotel Booking Engine & PMS 1.8.6, at admin/helpers/src/chat/attachment.php

240 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * This class holds the information of a generic chat attachment.
16 *
17 * @since 1.8
18 */
19 class VBOChatAttachment implements JsonSerializable
20 {
21 /**
22 * The relative source path of the attachment.
23 *
24 * @var string
25 */
26 private $path;
27
28 /**
29 * The original name of the attachment.
30 *
31 * @var string
32 */
33 private $name;
34
35 /**
36 * The real name of the attachment.
37 *
38 * @var string
39 */
40 private $filename;
41
42 /**
43 * The file extension.
44 *
45 * @var string
46 */
47 private $extension;
48
49 /**
50 * The file mime type.
51 *
52 * @var string
53 */
54 private $type;
55
56 /**
57 * Class constructor.
58 *
59 * @param array|object $data The file information.
60 *
61 * The data array/object supports the following details.
62 *
63 * @var string path (required)
64 * @var string name (required)
65 * @var string filename (optional)
66 * @var string extension (optional)
67 * @var string type (optional)
68 */
69 public function __construct($data)
70 {
71 if (!is_array($data) && !is_object($data)) {
72 throw new \InvalidArgumentException('Cannot bind chat attachment! Array or object expected, ' . gettype($data) . ' given.', 400);
73 }
74
75 $data = (array) $data;
76
77 if (empty($data['path'])) {
78 throw new \InvalidArgumentException('Missing path attribute.', 400);
79 }
80
81 $root = VBOFactory::getPlatform()->getUri()->getAbsolutePath();
82
83 // preserve only the relative path of the path
84 $this->path = trim(str_replace($root, '', $data['path']), '\\/');
85
86 if (empty($data['name'])) {
87 throw new \InvalidArgumentException('Missing file name.', 400);
88 }
89
90 if (!isset($data['extension'])) {
91 // extract file extension from original file name
92 if (preg_match("/\.([a-z0-9]{2,})$/i", $data['name'], $match)) {
93 $data['extension'] = end($match);
94 } else {
95 // we have a file w/o extension
96 $data['extension'] = '';
97 }
98 }
99
100 $this->extension = $data['extension'];
101
102 // keep name without file extension
103 $this->name = preg_replace("/\.{$this->extension}$/", '', $data['name']);
104
105 if (empty($data['filename'])) {
106 do {
107 // use a random (probable) unique ID as file name
108 $data['filename'] = VBOPerformanceIndicator::uuid() . ($this->extension ? '.' . $this->extension : '');
109 // repeat in case we have been so unlucky
110 } while (JFile::exists($root . '/' . $this->path . '/' . $data['filename']));
111 }
112
113 $this->filename = $data['filename'];
114
115 // register file mime type
116 $this->type = $data['type'] ?? null;
117 }
118
119 /**
120 * Returns the original file name.
121 *
122 * @param bool $extension Whether the file extension should be included.
123 *
124 * @return string
125 */
126 public function getName(bool $extension = true)
127 {
128 if ($extension) {
129 return $this->name . ($this->extension ? '.' . $this->extension : '');
130 }
131
132 return $this->name;
133 }
134
135 /**
136 * Returns the real file name.
137 *
138 * @return string
139 */
140 public function getFilename()
141 {
142 return $this->filename;
143 }
144
145 /**
146 * Returns the file extension.
147 *
148 * @return string
149 */
150 public function getExtension()
151 {
152 return $this->extension;
153 }
154
155 /**
156 * Returns the file MIME type.
157 *
158 * @return string
159 */
160 public function getMimeType()
161 {
162 if (!$this->type) {
163 // fetch MIME type at runtime
164 $this->type = $this->exists() ? mime_content_type($this->getPath()) : '';
165 }
166
167 return $this->type;
168 }
169
170 /**
171 * Returns the full path of the attachment.
172 *
173 * @return string
174 */
175 public function getPath()
176 {
177 return JPath::clean(
178 VBOFactory::getPlatform()->getUri()->getAbsolutePath() . '/' . $this->path . '/' . $this->getFilename()
179 );
180 }
181
182 /**
183 * Returns the file URI.
184 *
185 * @return string
186 */
187 public function getUrl()
188 {
189 return JUri::root() . preg_replace("/\\\\+/", '/', $this->path) . '/' . $this->getFilename();
190 }
191
192 /**
193 * Returns the file size.
194 *
195 * @return int
196 */
197 public function getSize()
198 {
199 if (!$this->exists()) {
200 return 0;
201 }
202
203 return filesize($this->getPath());
204 }
205
206 /**
207 * Checks whether the attachment actually exists.
208 *
209 * @return bool
210 */
211 public function exists()
212 {
213 return JFile::exists($this->getPath());
214 }
215
216 /**
217 * @inheritDoc
218 *
219 * @see JsonSerializable
220 */
221 #[ReturnTypeWillChange]
222 public function jsonSerialize()
223 {
224 $size = $this->getSize();
225
226 return [
227 'name' => $this->getName(true),
228 'filename' => $this->getFilename(),
229 'extension' => $this->getExtension(),
230 'type' => $this->getMimeType(),
231 'url' => $this->getUrl(),
232 'path' => $this->path,
233 'size' => [
234 'value' => $size,
235 'text' => JHtml::fetch('number.bytes', $size, 'auto', 0),
236 ],
237 ];
238 }
239 }
240