-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathormdocument.class.inc.php
More file actions
506 lines (457 loc) · 16.4 KB
/
ormdocument.class.inc.php
File metadata and controls
506 lines (457 loc) · 16.4 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
/**
* Copyright (C) 2013-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Service\Events\EventData;
use Combodo\iTop\Service\Events\EventService;
/**
* ormDocument
* encapsulate the behavior of a binary data set that will be stored an attribute of class AttributeBlob
*
* @package itopORM
*/
class ormDocument
{
/**
* @var string For content that should be displayed in the browser
* @link https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Disposition#syntaxe
* @since 3.1.0
*/
public const ENUM_CONTENT_DISPOSITION_INLINE = 'inline';
/**
* @var string For content that should be downloaded on the device. Mind that "attachment" Content-Disposition has nothing to do with the "Attachment" class from the DataModel.
* @link https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Disposition#syntaxe
* @since 3.1.0
*/
public const ENUM_CONTENT_DISPOSITION_ATTACHMENT = 'attachment';
/**
* @var int Default downloads count of the document, should always be 0.
* @since 3.1.0
*/
public const DEFAULT_DOWNLOADS_COUNT = 0;
private static $aKnownExtensions = [
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'dot' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'vsd' => 'application/x-visio',
'vdx' => 'application/visio.drawing',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'zip' => 'application/zip',
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'exe' => 'application/octet-stream',
];
public static function GetKnownExtensions(): array
{
return self::$aKnownExtensions;
}
protected $m_data;
protected $m_sMimeType;
protected $m_sFileName;
/**
* @var int $m_iDownloadsCount Number of times the document has been downloaded (through the standard API!). Note that download from the browser's cache won't appear.
* @since 3.1.0
*/
private $m_iDownloadsCount;
/**
* Constructor
*
* @param null $data
* @param string $sMimeType
* @param string $sFileName
* @param int $iDownloadsCount
*
* @since 3.1.0 N°2889 Add $iDownloadsCount parameter
*/
public function __construct($data = null, $sMimeType = 'text/plain', $sFileName = '', $iDownloadsCount = self::DEFAULT_DOWNLOADS_COUNT)
{
$this->m_data = $data;
$this->m_sMimeType = $sMimeType;
$this->m_sFileName = $sFileName;
$this->m_iDownloadsCount = $iDownloadsCount;
}
/**
* @param string $sPath Absolute path of the document to read
*
* @return \ormDocument
* @throws \Exception
*/
public static function FromFile(string $sPath): ormDocument
{
$sPath = utils::RealPath($sPath, APPROOT);
if (false === $sPath) {
throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it.");
}
$sData = @file_get_contents($sPath);
if (false === $sData) {
throw new Exception("Failed to load the file '$sPath'. The file does not exist or the current process is not allowed to access it.");
}
$sExtension = strtolower(pathinfo($sPath, PATHINFO_EXTENSION));
$sFileName = basename($sPath);
$sMimeType = 'text/plain';
if (array_key_exists($sExtension, ormDocument::$aKnownExtensions)) {
$sMimeType = ormDocument::$aKnownExtensions[$sExtension];
} else if (extension_loaded('fileinfo')) {
$fInfo = new finfo(FILEINFO_MIME);
$sMimeType = $fInfo->file($sPath);
}
return new ormDocument($sData, $sMimeType, $sFileName);
}
public function __toString()
{
if($this->IsEmpty()) return '';
return MyHelpers::beautifulstr($this->m_data, 100, true);
}
public function IsEmpty()
{
return ($this->m_data == null);
}
/**
* @param \ormDocument $oCompared
*
* @return bool True if the current ormDocument is equals to $oCompared EXCEPT for its download count. False if any other property is different OR if count is the same.
* @since 3.1.0 N°6502
*/
public function EqualsExceptDownloadsCount(ormDocument $oCompared): bool
{
// First checking equality on others properties
if ($oCompared->GetData() !== $this->GetData()) {
return false;
}
if ($oCompared->GetMimeType() !== $this->GetMimeType()) {
return false;
}
if ($oCompared->GetFileName() !== $this->GetFileName()) {
return false;
}
// Finally check equality of the download count
if ($oCompared->GetDownloadsCount() === $this->GetDownloadsCount()) {
return false;
} else {
return true;
}
}
public function GetMimeType()
{
return $this->m_sMimeType;
}
public function GetMainMimeType()
{
$iSeparatorPos = strpos($this->m_sMimeType, '/');
if ($iSeparatorPos > 0)
{
return substr($this->m_sMimeType, 0, $iSeparatorPos);
}
return $this->m_sMimeType;
}
/**
* @return int size in bits
* @uses strlen which returns the no of bits used
* @since 2.7.0
*/
public function GetSize()
{
return strlen($this->m_data);
}
/**
* @param int $precision
*
* @return string
* @uses utils::BytesToFriendlyFormat()
*/
public function GetFormattedSize($precision = 2)
{
$bytes = $this->GetSize();
return utils::BytesToFriendlyFormat($bytes, $precision);
}
public function GetData()
{
return $this->m_data;
}
public function GetFileName()
{
return $this->m_sFileName;
}
/**
* @see static::DownloadDocument()
* @see static::$m_iDownloadsCount
* @return int Number of times the document has been downloaded (through the standard API!)
* @since 3.1.0
*/
public function GetDownloadsCount(): int
{
// Force cast to get 0 instead of null on fields prior to the features that have never been downloaded.
return (int) $this->m_iDownloadsCount;
}
/**
* Increase the number of downloads of the document by $iNumber
*
* @param int $iNumber Step to increase the counter with, default is 1.
* @return void
* @since 3.1.0
*/
public function IncreaseDownloadsCount($iNumber = 1): void
{
$this->m_iDownloadsCount += $iNumber;
}
public function GetAsHTML()
{
$sResult = '';
if ($this->IsEmpty()) {
// If the filename is not empty, display it, this is used
// by the creation wizard while the file has not yet been uploaded
$sResult = utils::EscapeHtml($this->GetFileName());
} else {
$data = $this->GetData();
$sSize = utils::BytesToFriendlyFormat(strlen($data));
$iDownloadsCount = $this->GetDownloadsCount();
$sDownloadsCountForHtml = utils::HtmlEntities(Dict::Format('Core:ormValue:ormDocument:DownloadsCount', $iDownloadsCount));
$sDownloadsCountTooltipForHtml = utils::HtmlEntities(Dict::Format('Core:ormValue:ormDocument:DownloadsCount+', $iDownloadsCount));
$sResult = utils::EscapeHtml($this->GetFileName()).' ('.$sSize.' / '.$sDownloadsCountForHtml.' <i class="fas fa-cloud-download-alt" data-tooltip-content="'.$sDownloadsCountTooltipForHtml.'"></i>)<br/>';
}
return $sResult;
}
/**
* Returns an hyperlink to display the document *inline*
* @return string
*/
public function GetDisplayLink($sClass, $Id, $sAttCode)
{
$sUrl = $this->GetDisplayURL($sClass, $Id, $sAttCode);
return "<a href=\"$sUrl\" target=\"_blank\" >".utils::EscapeHtml($this->GetFileName())."</a>\n";
}
/**
* Returns an hyperlink to download the document (content-disposition: attachment)
* @return string
*/
public function GetDownloadLink($sClass, $Id, $sAttCode)
{
$sUrl = $this->GetDownloadURL($sClass, $Id, $sAttCode);
return "<a href=\"$sUrl\">".utils::EscapeHtml($this->GetFileName())."</a>\n";
}
/**
* Returns an URL to display a document like an image
* @return string
*/
public function GetDisplayURL($sClass, $Id, $sAttCode)
{
$sSignature = $this->GetSignature();
// TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route)
return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode&s=$sSignature&cache=86400";
}
/**
* Returns an URL to download a document like an image (uses HTTP caching)
* @return string
*/
public function GetDownloadURL($sClass, $Id, $sAttCode)
{
// Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files)
$sSignature = $this->GetSignature();
// TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route)
return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.document.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode&s=$sSignature&cache=86400";
}
public function IsPreviewAvailable()
{
$bRet = false;
switch($this->GetMimeType())
{
case 'image/png':
case 'image/jpg':
case 'image/jpeg':
case 'image/gif':
case 'image/bmp':
case 'image/svg+xml':
$bRet = true;
break;
}
return $bRet;
}
/**
* Downloads a document to the browser, either as 'inline' or 'attachment'
*
* @param WebPage $oPage The web page for the output
* @param string $sClass Class name of the object
* @param mixed $id Identifier of the object
* @param string $sAttCode Name of the attribute containing the document to download
* @param string $sContentDisposition Either 'inline' or 'attachment'
* @param string $sSecretField The attcode of the field containing a "secret" to be provided in order to retrieve the file
* @param string $sSecretValue The value of the secret to be compared with the value of the attribute $sSecretField
*
* @return void
*/
public static function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment', $sSecretField = null, $sSecretValue = null)
{
try
{
$oObj = MetaModel::GetObject($sClass, $id, false, false);
if (!is_object($oObj))
{
// If access to the document is not granted, check if the access to the host object is allowed
$oObj = MetaModel::GetObject($sClass, $id, false, true);
if ($oObj instanceof Attachment) {
$sItemClass = $oObj->Get('item_class');
$sItemId = $oObj->Get('item_id');
$oHost = MetaModel::GetObject($sItemClass, $sItemId, false, false);
if (!is_object($oHost)) {
$oObj = null;
}
}
if (!is_object($oObj)) {
throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
}
}
if (($sSecretField != null) && ($oObj->Get($sSecretField) != $sSecretValue))
{
usleep(200);
throw new Exception("Invalid secret for class '$sClass' - the object does not exist or you are not allowed to view it");
}
/** @var \ormDocument $oDocument */
$oDocument = $oObj->Get($sAttCode);
if (is_object($oDocument))
{
$aEventData = array(
'debug_info' => $oDocument->GetFileName(),
'object' => $oObj,
'att_code' => $sAttCode,
'document' => $oDocument,
'content_disposition' => $sContentDisposition,
);
EventService::FireEvent(new EventData(\EVENT_DOWNLOAD_DOCUMENT, $sClass, $aEventData));
$oPage->TrashUnexpectedOutput();
$oPage->SetContentType($oDocument->GetMimeType());
$oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
$oPage->add($oDocument->GetData());
// Update downloads count only when content disposition is set to "attachment" as other disposition are to display the document within the page
if($sContentDisposition === static::ENUM_CONTENT_DISPOSITION_ATTACHMENT) {
$oDocument->IncreaseDownloadsCount();
$oObj->Set($sAttCode, $oDocument);
// $oObj can be a \DBObject or \cmdbAbstractObject so we ahve to protect it
if (method_exists($oObj, 'AllowWrite')) {
// AllowWrite method is implemented in cmdbAbstractObject, but $oObject could be a DBObject or CMDBObject
$oObj->AllowWrite();
}
$oObj->DBUpdate();
}
}
}
catch(Exception $e)
{
$oPage->p($e->getMessage());
}
}
/**
* Resize an image so that it fits in the given dimensions
* @param int $iMaxImageWidth Maximum width for the resized image
* @param int $iMaxImageHeight Maximum height for the resized image
* @param array|null $aFinalDimensions Image dimensions after resizing or null if unable to read the image
* @return ormDocument The resampled image
*
*/
public function ResizeImageToFit(int $iMawWidth, int $iMaxHeight, array|null &$aFinalDimensions = null) : static
{
$aFinalDimensions = null;
// If gd extension is not loaded, we put a warning in the log and return the image as is
if (extension_loaded('gd') === false) {
IssueLog::Warning('Image could not be resized as the "gd" extension does not seem to be loaded. Its dimensions will remain the same instead of ' . $iMawWidth . 'x' . $iMaxHeight);
return $this;
}
$oGdImage = false;
switch($this->GetMimeType()) {
case 'image/gif':
case 'image/jpeg':
case 'image/png':
$oGdImage = @imagecreatefromstring($this->GetData());
break;
default:
// Unsupported image type, return the image as-is
return $this;
}
if ($oGdImage === false) {
IssueLog::Warning('Image could not be resized as . It will remain as imagecreatefromstring could not read its data.Its dimensions will remain the same instead of ' . $iMawWidth . 'x' . $iMaxHeight);
return $this;
}
$iWidth = imagesx($oGdImage);
$iHeight = imagesy($oGdImage);
if ( ($iMawWidth === 0 || $iWidth <= $iMawWidth) && ($iMaxHeight === 0 || $iHeight <= $iMaxHeight)) {
// No need to resize
$aFinalDimensions = [
'width' => $iWidth,
'height' =>$iHeight
];
return $this;
}
$fScale = min($iMawWidth / $iWidth, $iMaxHeight / $iHeight);
$iNewWidth = (int)($iWidth * $fScale);
$iNewHeight = (int)($iHeight * $fScale);
$oNewGdImage = imagecreatetruecolor($iNewWidth, $iNewHeight);
$aFinalDimensions = [
'width' => $iNewWidth,
'height' =>$iNewHeight
];
// Preserve transparency
if($this->GetMimeType() == "image/gif" || $this->GetMimeType() == "image/png") {
imagecolortransparent($oNewGdImage, imagecolorallocatealpha($oNewGdImage, 0, 0, 0, 127));
imagealphablending($oNewGdImage, false);
imagesavealpha($oNewGdImage, true);
}
imagecopyresampled($oNewGdImage, $oGdImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iWidth, $iHeight);
ob_start();
switch ($this->GetMimeType()) {
case 'image/gif':
imagegif($oNewGdImage); // send image to output buffer
break;
case 'image/jpeg':
imagejpeg($oNewGdImage, null, 80); // null = send image to output buffer, 80 = good quality
break;
case 'image/png':
imagepng($oNewGdImage, null, 5); // null = send image to output buffer, 5 = medium compression
break;
}
$oResampledImage = new ormDocument(ob_get_contents(), $this->GetMimeType(), $this->GetFileName());
@ob_end_clean();
imagedestroy($oGdImage);
imagedestroy($oNewGdImage);
return $oResampledImage;
}
/**
* @return string
*/
public function GetSignature(): string
{
return md5($this->GetData() ?? '');
}
}