1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author lukasz chalat |
4
|
|
|
* @package midcom.helper.imagepopup |
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
|
|
|
/** |
10
|
|
|
* This handler get uploaded file and save it in database and write to file. |
11
|
|
|
* |
12
|
|
|
* @package midcom.helper.imagepopup |
13
|
|
|
*/ |
14
|
|
|
class midcom_helper_imagepopup_handler_upload extends midcom_baseclasses_components_handler |
15
|
|
|
{ |
16
|
2 |
|
public function _handler_upload(array &$data, string $guid = null) |
17
|
|
|
{ |
18
|
|
|
// Get the file |
19
|
2 |
|
reset($_FILES); |
20
|
2 |
|
$temp = array_shift($_FILES); |
21
|
|
|
|
22
|
|
|
// Verify file extension |
23
|
2 |
|
if ( is_uploaded_file($temp['tmp_name']) |
24
|
2 |
|
&& !in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), ["gif", "jpg", "png"])) { |
|
|
|
|
25
|
|
|
throw new midcom_error('Invalid extension.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Get the data |
29
|
2 |
|
$temp_name = $temp['tmp_name']; |
30
|
2 |
|
$mimetype = $temp['type']; |
31
|
2 |
|
$parentguid = $guid ?: $this->_topic->guid; |
32
|
|
|
|
33
|
|
|
// Set modified filename |
34
|
2 |
|
$filename = $this->get_modify_filename($temp['name']); |
35
|
|
|
|
36
|
|
|
// Insert the image into database |
37
|
2 |
|
$attachment = $this->insert_database($filename, $mimetype, $parentguid); |
38
|
|
|
|
39
|
2 |
|
$data['attachment'] = $attachment; |
40
|
|
|
|
41
|
|
|
// Get target to write the file |
42
|
2 |
|
$target = $this->get_data_from_database($filename, $parentguid); |
43
|
|
|
|
44
|
|
|
// Write the file |
45
|
2 |
|
$this->write_the_file($temp_name, $target); |
46
|
|
|
|
47
|
|
|
// Make a response for editor.uploadImages() function |
48
|
2 |
|
$location = midcom_db_attachment::get_url($attachment); |
49
|
|
|
|
50
|
2 |
|
$data['location'] = $location; |
51
|
|
|
|
52
|
|
|
// Return image location as JSON |
53
|
2 |
|
return new midcom_response_json(['location' => $location]); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
private function get_modify_filename(string $filename) : string |
57
|
|
|
{ |
58
|
2 |
|
$filename = midcom_db_attachment::safe_filename($filename); |
59
|
2 |
|
$pieces = explode('.', $filename); |
60
|
2 |
|
$core = array_shift($pieces); |
61
|
2 |
|
$split = preg_split("/-(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})/", $core); |
62
|
2 |
|
$core = array_shift($split); |
63
|
2 |
|
$extension = array_pop($pieces); |
64
|
2 |
|
return $core . "-" . date('Y-m-d-H-i-s', time()) . "." . $extension; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
private function get_data_from_database(string $filename, string $parentguid) : midcom_db_attachment |
68
|
|
|
{ |
69
|
2 |
|
$query = midcom_db_attachment::new_query_builder(); |
70
|
2 |
|
$query->add_constraint('name', '=', $filename); |
71
|
2 |
|
$query->add_constraint('parentguid', '=', $parentguid); |
72
|
2 |
|
$entry = $query->execute(); |
73
|
|
|
|
74
|
2 |
|
if (empty($entry)) { |
75
|
|
|
throw new midcom_error_notfound("There is no match in database " . midcom_connection::get_error_string()); |
76
|
|
|
} |
77
|
2 |
|
if (count($entry) == 1) { |
78
|
2 |
|
return $entry[0]; |
79
|
|
|
} |
80
|
|
|
throw new midcom_error('There is more than just one object' . midcom_connection::get_error_string()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $tmp The temporary location of file |
85
|
|
|
* @param midcom_db_attachment $target The final destination for file |
86
|
|
|
*/ |
87
|
2 |
|
private function write_the_file(string $tmp, midcom_db_attachment $target) |
88
|
|
|
{ |
89
|
2 |
|
$source = fopen($tmp, 'r'); |
90
|
2 |
|
if (!$source) { |
|
|
|
|
91
|
|
|
throw new midcom_error("Could not open file " . $tmp . " for reading."); |
92
|
|
|
} |
93
|
2 |
|
$stat = $target->copy_from_handle($source); |
94
|
2 |
|
fclose($source); |
95
|
2 |
|
if (!$stat) { |
96
|
|
|
throw new midcom_error('Failed to copy from handle: ' . midcom_connection::get_error_string()); |
97
|
|
|
} |
98
|
2 |
|
} |
99
|
|
|
|
100
|
2 |
|
private function insert_database(string $filename, string $mimetype, string $parentguid) : midcom_db_attachment |
101
|
|
|
{ |
102
|
2 |
|
$attachment = new midcom_db_attachment(); |
103
|
2 |
|
$attachment->name = $filename; |
104
|
2 |
|
$attachment->title = $filename; |
105
|
2 |
|
$attachment->mimetype = $mimetype; |
106
|
2 |
|
$attachment->parentguid = $parentguid; |
107
|
2 |
|
if (!$attachment->create()) { |
108
|
|
|
throw new midcom_error('Failed to create derived image: ' . midcom_connection::get_error_string()); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
return $attachment; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|