1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.db |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use midgard\portable\api\blob; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MidCOM level replacement for the Midgard Attachment record with framework support. |
13
|
|
|
* |
14
|
|
|
* @property string $name Filename of the attachment |
15
|
|
|
* @property string $title Title of the attachment |
16
|
|
|
* @property string $location Location of the attachment in the blob directory structure |
17
|
|
|
* @property string $mimetype MIME type of the attachment |
18
|
|
|
* @property string $parentguid GUID of the object the attachment is attached to |
19
|
|
|
* @package midcom.db |
20
|
|
|
*/ |
21
|
|
|
class midcom_db_attachment extends midcom_core_dbaobject |
22
|
|
|
{ |
23
|
|
|
public $__midcom_class_name__ = __CLASS__; |
24
|
|
|
public $__mgdschema_class_name__ = 'midgard_attachment'; |
25
|
|
|
|
26
|
|
|
public $_use_rcs = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Internal tracking state variable, holds the file handle of any open |
30
|
|
|
* attachment. |
31
|
|
|
* |
32
|
|
|
* @var resource |
33
|
|
|
*/ |
34
|
|
|
private $_open_handle; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Internal tracking state variable, true if the attachment has a handle opened in write mode |
38
|
|
|
*/ |
39
|
|
|
private $_open_write_mode = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Opens the attachment for file IO. |
43
|
|
|
* |
44
|
|
|
* Returns a filehandle that can be used with the usual PHP file functions if successful, |
45
|
|
|
* the handle has to be closed with the close() method when you no longer need it, don't |
46
|
|
|
* let it fall over the end of the script. |
47
|
|
|
* |
48
|
|
|
* <b>Important Note:</b> It is important to use the close() member function of |
49
|
|
|
* this class to close the file handle, not just fclose(). Otherwise, the upgrade |
50
|
|
|
* notification switches will fail. |
51
|
|
|
* |
52
|
|
|
* @param string $mode The mode which should be used to open the attachment, same as |
53
|
|
|
* the mode parameter of the PHP fopen call. This defaults to write access. |
54
|
|
|
* @return resource|false A file handle to the attachment if successful, false on failure. |
55
|
|
|
*/ |
56
|
18 |
|
public function open(string $mode = 'w') |
57
|
|
|
{ |
58
|
18 |
|
if (!$this->id) { |
59
|
|
|
debug_add('Cannot open a non-persistent attachment.', MIDCOM_LOG_WARN); |
60
|
|
|
debug_print_r('Object state:', $this); |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
if ($this->_open_handle !== null) { |
65
|
|
|
debug_add("Warning, the Attachment {$this->id} already had an open file handle, we close it implicitly.", MIDCOM_LOG_WARN); |
66
|
|
|
$this->close(); |
67
|
|
|
} |
68
|
|
|
|
69
|
18 |
|
$blob = new blob($this->__object); |
70
|
18 |
|
$handle = $blob->get_handler($mode); |
71
|
|
|
|
72
|
18 |
|
if (!$handle) { |
|
|
|
|
73
|
|
|
debug_add("Failed to open attachment with mode {$mode}, last Midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
$this->_open_write_mode = ($mode[0] != 'r'); |
78
|
18 |
|
$this->_open_handle = $handle; |
79
|
|
|
|
80
|
18 |
|
return $handle; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Read the file and return its contents |
85
|
|
|
*/ |
86
|
|
|
public function read() : ?string |
87
|
|
|
{ |
88
|
|
|
$blob = new blob($this->__object); |
89
|
|
|
return $blob->read_content(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Close the open write handle obtained by the open() call again. |
94
|
|
|
* It is required to call this function instead of a simple fclose to ensure proper |
95
|
|
|
* upgrade notifications. |
96
|
|
|
*/ |
97
|
18 |
|
public function close() |
98
|
|
|
{ |
99
|
18 |
|
if ($this->_open_handle === null) { |
100
|
|
|
debug_add("Tried to close non-open attachment {$this->id}", MIDCOM_LOG_WARN); |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
18 |
|
fclose($this->_open_handle); |
105
|
18 |
|
$this->_open_handle = null; |
106
|
|
|
|
107
|
18 |
|
if ($this->_open_write_mode) { |
108
|
|
|
// We need to update the attachment now, this cannot be done in the Midgard Core |
109
|
|
|
// at this time. |
110
|
18 |
|
if (!$this->update()) { |
111
|
11 |
|
debug_add("Failed to update attachment {$this->id}", MIDCOM_LOG_WARN); |
112
|
11 |
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
7 |
|
$this->file_to_cache(); |
116
|
7 |
|
$this->_open_write_mode = false; |
117
|
|
|
} |
118
|
18 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Rewrite a filename to URL safe form |
122
|
|
|
* |
123
|
|
|
* @todo add possibility to use the file utility to determine extension if missing. |
124
|
|
|
*/ |
125
|
7 |
|
public static function safe_filename(string $filename) : string |
126
|
|
|
{ |
127
|
|
|
// we could use basename() or pathinfo() here, except that it swallows multibyte chars at the |
128
|
|
|
// beginning of the string if we run in e.g. C locale.. |
129
|
7 |
|
$parts = explode('/', trim($filename)); |
130
|
7 |
|
$filename = end($parts); |
131
|
|
|
|
132
|
7 |
|
if (preg_match('/^(.*)(\..*?)$/', $filename, $ext_matches)) { |
133
|
4 |
|
$name = $ext_matches[1]; |
134
|
4 |
|
$ext = $ext_matches[2]; |
135
|
|
|
} else { |
136
|
3 |
|
$name = $filename; |
137
|
3 |
|
$ext = ''; |
138
|
|
|
} |
139
|
7 |
|
return midcom_helper_misc::urlize($name) . $ext; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the path to the document in the static cache |
144
|
|
|
*/ |
145
|
1 |
|
private function get_cache_path() : string |
146
|
|
|
{ |
147
|
|
|
// Copy the file to the static directory |
148
|
1 |
|
$cacheroot = midcom::get()->config->get('attachment_cache_root'); |
149
|
1 |
|
$subdir = $this->guid[0]; |
150
|
1 |
|
if (!file_exists("{$cacheroot}/{$subdir}/{$this->guid}")) { |
151
|
1 |
|
mkdir("{$cacheroot}/{$subdir}/{$this->guid}", 0777, true); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return "{$cacheroot}/{$subdir}/{$this->guid}/{$this->name}"; |
155
|
|
|
} |
156
|
|
|
|
157
|
6 |
|
public static function get_url($attachment, string $name = null) : string |
158
|
|
|
{ |
159
|
6 |
|
if (is_string($attachment)) { |
160
|
|
|
$guid = $attachment; |
161
|
|
|
if (null === $name) { |
162
|
|
|
$mc = self::new_collector('guid', $guid); |
163
|
|
|
$names = $mc->get_values('name'); |
164
|
|
|
$name = array_pop($names); |
165
|
|
|
} |
166
|
6 |
|
} elseif (midcom::get()->dbfactory->is_a($attachment, 'midgard_attachment')) { |
167
|
6 |
|
$guid = $attachment->guid; |
168
|
6 |
|
$name = $attachment->name; |
169
|
|
|
} else { |
170
|
|
|
throw new midcom_error('Invalid attachment identifier'); |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
if (!$guid) { |
174
|
2 |
|
return ''; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
if (midcom::get()->config->get('attachment_cache_enabled')) { |
178
|
|
|
$subdir = $guid[0]; |
179
|
|
|
|
180
|
|
|
if (file_exists(midcom::get()->config->get('attachment_cache_root') . '/' . $subdir . '/' . $guid . '/' . $name)) { |
181
|
|
|
return midcom::get()->config->get('attachment_cache_url') . '/' . $subdir . '/' . $guid . '/' . urlencode($name); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Use regular MidCOM attachment server |
186
|
4 |
|
return midcom_connection::get_url('self') . 'midcom-serveattachmentguid-' . $guid . '/' . urlencode($name); |
187
|
|
|
} |
188
|
|
|
|
189
|
7 |
|
public function file_to_cache() |
190
|
|
|
{ |
191
|
7 |
|
if (!midcom::get()->config->get('attachment_cache_enabled')) { |
192
|
7 |
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
if (!$this->can_do('midgard:read', 'EVERYONE')) { |
196
|
|
|
debug_add("Attachment {$this->name} ({$this->guid}) is not publicly readable, not caching."); |
197
|
|
|
$this->remove_from_cache(); |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
$filename = $this->get_cache_path(); |
202
|
|
|
|
203
|
1 |
|
if (file_exists($filename) && is_link($filename)) { |
204
|
|
|
debug_add("Attachment {$this->name} ({$this->guid}) is already in cache as {$filename}, skipping."); |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Then symlink the file |
209
|
1 |
|
if (@symlink($this->get_path(), $filename)) { |
210
|
1 |
|
debug_add("Symlinked attachment {$this->name} ({$this->guid}) as {$filename}."); |
211
|
1 |
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Symlink failed, actually copy the data |
215
|
|
|
if (!copy($this->get_path(), $filename)) { |
216
|
|
|
debug_add("Failed to cache attachment {$this->name} ({$this->guid}), copying failed."); |
217
|
|
|
return; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
debug_add("Symlinking attachment {$this->name} ({$this->guid}) as {$filename} failed, data copied instead."); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
private function remove_from_cache() |
224
|
|
|
{ |
225
|
|
|
$filename = $this->get_cache_path(); |
226
|
|
|
if (file_exists($filename)) { |
227
|
|
|
@unlink($filename); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Simple wrapper for stat() on the blob object. |
233
|
|
|
* |
234
|
|
|
* @return mixed Either a stat array as for stat() or false on failure. |
235
|
|
|
*/ |
236
|
5 |
|
public function stat() |
237
|
|
|
{ |
238
|
5 |
|
if (!$this->id) { |
239
|
|
|
debug_add('Cannot open a non-persistent attachment.', MIDCOM_LOG_WARN); |
240
|
|
|
debug_print_r('Object state:', $this); |
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
5 |
|
$path = $this->get_path(); |
245
|
5 |
|
if (!file_exists($path)) { |
246
|
|
|
debug_add("File {$path} that blob {$this->guid} points to cannot be found", MIDCOM_LOG_WARN); |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
5 |
|
return stat($path); |
251
|
|
|
} |
252
|
|
|
|
253
|
8 |
|
public function get_path() : string |
254
|
|
|
{ |
255
|
8 |
|
if (!$this->id) { |
256
|
|
|
return ''; |
257
|
|
|
} |
258
|
8 |
|
return (new blob($this->__object))->get_path(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Internal helper, computes an MD5 string which is used as an attachment location. |
263
|
|
|
* If the location already exists, it will iterate until an unused location is found. |
264
|
|
|
*/ |
265
|
21 |
|
private function _create_attachment_location() : string |
266
|
|
|
{ |
267
|
21 |
|
$max_tries = 500; |
268
|
|
|
|
269
|
21 |
|
for ($i = 0; $i < $max_tries; $i++) { |
270
|
21 |
|
$name = strtolower(md5(uniqid('', true))); |
271
|
21 |
|
$location = strtoupper($name[0] . '/' . $name[1]) . '/' . $name; |
272
|
|
|
|
273
|
|
|
// Check uniqueness |
274
|
21 |
|
$qb = self::new_query_builder(); |
275
|
21 |
|
$qb->add_constraint('location', '=', $location); |
276
|
21 |
|
$result = $qb->count_unchecked(); |
277
|
|
|
|
278
|
21 |
|
if ($result == 0) { |
279
|
21 |
|
debug_add("Created this location: {$location}"); |
280
|
21 |
|
return $location; |
281
|
|
|
} |
282
|
|
|
debug_add("Location {$location} is in use, retrying"); |
283
|
|
|
} |
284
|
|
|
throw new midcom_error('could not create attachment location'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Simple creation event handler which fills out the location field if it |
289
|
|
|
* is still empty with a location generated by _create_attachment_location(). |
290
|
|
|
*/ |
291
|
21 |
|
public function _on_creating() : bool |
292
|
|
|
{ |
293
|
21 |
|
if (empty($this->mimetype)) { |
294
|
15 |
|
$this->mimetype = 'application/octet-stream'; |
295
|
|
|
} |
296
|
|
|
|
297
|
21 |
|
$this->location = $this->_create_attachment_location(); |
298
|
|
|
|
299
|
21 |
|
return true; |
300
|
|
|
} |
301
|
|
|
|
302
|
8 |
|
public function update_cache() |
303
|
|
|
{ |
304
|
|
|
// Check if the attachment can be read anonymously |
305
|
8 |
|
if ( midcom::get()->config->get('attachment_cache_enabled') |
306
|
8 |
|
&& !$this->can_do('midgard:read', 'EVERYONE')) { |
307
|
|
|
// Not public file, ensure it is removed |
308
|
|
|
$this->remove_from_cache(); |
309
|
|
|
} |
310
|
8 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Updated callback, triggers watches on the parent(!) object. |
314
|
|
|
*/ |
315
|
8 |
|
public function _on_updated() |
316
|
|
|
{ |
317
|
8 |
|
$this->update_cache(); |
318
|
8 |
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Deleted callback, triggers watches on the parent(!) object. |
322
|
|
|
*/ |
323
|
19 |
|
public function _on_deleted() |
324
|
|
|
{ |
325
|
19 |
|
if (midcom::get()->config->get('attachment_cache_enabled')) { |
326
|
|
|
// Remove attachment cache |
327
|
|
|
$this->remove_from_cache(); |
328
|
|
|
} |
329
|
19 |
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Updates the contents of the attachments with the contents given. |
333
|
|
|
* |
334
|
|
|
* @param mixed $source File contents. |
335
|
|
|
*/ |
336
|
|
|
public function copy_from_memory($source) : bool |
337
|
|
|
{ |
338
|
|
|
$dest = $this->open(); |
339
|
|
|
if (!$dest) { |
|
|
|
|
340
|
|
|
debug_add('Could not open attachment for writing, last Midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); |
341
|
|
|
return false; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
fwrite($dest, $source); |
345
|
|
|
|
346
|
|
|
$this->close(); |
347
|
|
|
return true; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Updates the contents of the attachments with the contents of the resource identified |
352
|
|
|
* by the filehandle passed. |
353
|
|
|
* |
354
|
|
|
* @param resource $source The handle to read from. |
355
|
|
|
*/ |
356
|
18 |
|
public function copy_from_handle($source) : bool |
357
|
|
|
{ |
358
|
18 |
|
$dest = $this->open(); |
359
|
18 |
|
if (!$dest) { |
|
|
|
|
360
|
|
|
debug_add('Could not open attachment for writing, last Midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
18 |
|
stream_copy_to_stream($source, $dest); |
365
|
|
|
|
366
|
18 |
|
$this->close(); |
367
|
18 |
|
return true; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Updates the contents of the attachments with the contents of the file specified. |
372
|
|
|
* This is a wrapper for copy_from_handle. |
373
|
|
|
* |
374
|
|
|
* @param string $filename The file to read. |
375
|
|
|
*/ |
376
|
16 |
|
public function copy_from_file($filename) : bool |
377
|
|
|
{ |
378
|
16 |
|
$source = @fopen($filename, 'r'); |
379
|
16 |
|
if (!$source) { |
|
|
|
|
380
|
|
|
debug_add('Could not open file for reading.' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); |
381
|
|
|
midcom::get()->debug->log_php_error(MIDCOM_LOG_WARN); |
382
|
|
|
return false; |
383
|
|
|
} |
384
|
16 |
|
$result = $this->copy_from_handle($source); |
385
|
16 |
|
fclose($source); |
386
|
16 |
|
return $result; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|