1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Extcal; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* You may not change or alter any portion of this comment or credits |
7
|
|
|
* of supporting developers from this source code or any supporting source code |
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
use Xmf\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
19
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
20
|
|
|
* @package extcal |
21
|
|
|
* @since |
22
|
|
|
* @author XOOPS Development Team, |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
// // require_once __DIR__ . '/ExtcalPersistableObjectHandler.php'; |
26
|
|
|
require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class FileHandler. |
30
|
|
|
*/ |
31
|
|
|
class FileHandler extends ExtcalPersistableObjectHandler |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @param \XoopsDatabase|null $db |
35
|
|
|
*/ |
36
|
|
|
public function __construct(\XoopsDatabase $db = null) |
37
|
|
|
{ |
38
|
|
|
if (null === $db) { |
39
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
40
|
|
|
} |
41
|
|
|
parent::__construct($db, 'extcal_file', File::class, 'file_id'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $eventId |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function createFile($eventId) |
50
|
|
|
{ |
51
|
|
|
$userId = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
52
|
|
|
|
53
|
|
|
$allowedMimeType = []; |
54
|
|
|
$mimeType = require_once XOOPS_ROOT_PATH . '/include/mimetypes.inc.php'; |
55
|
|
|
foreach ($GLOBALS['xoopsModuleConfig']['allowed_file_extention'] as $fileExt) { |
56
|
|
|
$allowedMimeType[] = $mimeType[$fileExt]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$uploader = new \XoopsMediaUploader(XOOPS_ROOT_PATH . '/uploads/extcal', $allowedMimeType, 3145728); |
60
|
|
|
$uploader->setPrefix($userId . '-' . $eventId . '_'); |
61
|
|
|
if ($uploader->fetchMedia('event_file')) { |
62
|
|
|
if (!$uploader->upload()) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$data = [ |
70
|
|
|
'file_name' => $uploader->getSavedFileName(), |
71
|
|
|
'file_nicename' => $uploader->getMediaName(), |
72
|
|
|
'file_mimetype' => $uploader->getMediaType(), |
73
|
|
|
'file_size' => $_FILES['event_file']['size'], |
74
|
|
|
'file_date' => \time(), |
75
|
|
|
'file_approved' => 1, |
76
|
|
|
'event_id' => $eventId, |
77
|
|
|
'uid' => $userId, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$file = $this->create(); |
81
|
|
|
$file->setVars($data); |
82
|
|
|
|
83
|
|
|
return $this->insert($file); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $file |
88
|
|
|
*/ |
89
|
|
|
public function deleteFile($file) |
90
|
|
|
{ |
91
|
|
|
$this->_deleteFile($file); |
92
|
|
|
$this->deleteById($file->getVar('file_id')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $eventId |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getEventFiles($eventId) |
101
|
|
|
{ |
102
|
|
|
$criteria = new \CriteriaCompo(); |
103
|
|
|
$criteria->add(new \Criteria('file_approved', 1)); |
104
|
|
|
$criteria->add(new \Criteria('event_id', $eventId)); |
105
|
|
|
|
106
|
|
|
return $this->getObjects($criteria); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $eventId |
111
|
|
|
*/ |
112
|
|
|
public function updateEventFile($eventId) |
113
|
|
|
{ |
114
|
|
|
$criteria = new \CriteriaCompo(); |
115
|
|
|
$criteria->add(new \Criteria('file_approved', 1)); |
116
|
|
|
$criteria->add(new \Criteria('event_id', $eventId)); |
117
|
|
|
|
118
|
|
|
if (Request::hasVar('filetokeep', 'POST')) { |
119
|
|
|
if (\is_array($_POST['filetokeep'])) { |
120
|
|
|
$count = \count($_POST['filetokeep']); |
|
|
|
|
121
|
|
|
$in = '(' . $_POST['filetokeep'][0]; |
122
|
|
|
\array_shift($_POST['filetokeep']); |
123
|
|
|
foreach ($_POST['filetokeep'] as $elmt) { |
124
|
|
|
$in .= ',' . $elmt; |
125
|
|
|
} |
126
|
|
|
$in .= ')'; |
127
|
|
|
} else { |
128
|
|
|
$in = '(' . $_POST['filetokeep'] . ')'; |
129
|
|
|
} |
130
|
|
|
$criteria->add(new \Criteria('file_id', $in, 'NOT IN')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$files = $this->getObjects($criteria); |
134
|
|
|
foreach ($files as $file) { |
135
|
|
|
$this->deleteFile($file); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $fileId |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function getFile($fileId) |
145
|
|
|
{ |
146
|
|
|
return $this->get($fileId); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $files |
151
|
|
|
*/ |
152
|
|
|
public function formatFilesSize(&$files) |
153
|
|
|
{ |
154
|
|
|
foreach ($files as $i => $iValue) { |
155
|
|
|
$this->formatFileSize($files[$i]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $file |
161
|
|
|
*/ |
162
|
|
|
public function formatFileSize(&$file) |
163
|
|
|
{ |
164
|
|
|
if ($file['file_size'] > 1000) { |
165
|
|
|
$file['formated_file_size'] = \round($file['file_size'] / 1000) . 'kb'; |
166
|
|
|
} else { |
167
|
|
|
$file['formated_file_size'] = '1kb'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $file |
173
|
|
|
*/ |
174
|
|
|
public function _deleteFile($file) |
175
|
|
|
{ |
176
|
|
|
if (\is_file(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'))) { |
177
|
|
|
\unlink(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name')); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|