1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
5
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
7
|
|
|
|
8
|
|
|
This file is part of Divine CMS. |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
22
|
|
|
|
23
|
|
|
class ModelToolUpload extends \Divine\Engine\Core\Model |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
public function addUpload($name, $filename) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$code = sha1(uniqid(mt_rand(), true)); |
28
|
|
|
|
29
|
|
|
$this->db->query(" |
30
|
|
|
INSERT INTO `upload` |
31
|
|
|
SET `name` = '" . $this->db->escape($name) . "', |
32
|
|
|
`filename` = '" . $this->db->escape($filename) . "', |
33
|
|
|
`code` = '" . $this->db->escape($code) . "', |
34
|
|
|
`date_added` = NOW() |
35
|
|
|
"); |
36
|
|
|
|
37
|
|
|
return $code; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function deleteUpload($upload_id) |
41
|
|
|
{ |
42
|
|
|
$this->db->query(" |
43
|
|
|
DELETE |
44
|
|
|
FROM upload |
45
|
|
|
WHERE upload_id = '" . (int)$upload_id . "' |
46
|
|
|
"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getUpload($upload_id) |
50
|
|
|
{ |
51
|
|
|
$query = $this->db->query(" |
52
|
|
|
SELECT * |
53
|
|
|
FROM `upload` |
54
|
|
|
WHERE upload_id = '" . (int)$upload_id . "' |
55
|
|
|
"); |
56
|
|
|
|
57
|
|
|
return $query->row; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getUploadByCode($code) |
61
|
|
|
{ |
62
|
|
|
$query = $this->db->query(" |
63
|
|
|
SELECT * |
64
|
|
|
FROM upload |
65
|
|
|
WHERE code = '" . $this->db->escape($code) . "' |
66
|
|
|
"); |
67
|
|
|
|
68
|
|
|
return $query->row; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getUploads($data = array()) |
72
|
|
|
{ |
73
|
|
|
$sql = " |
|
|
|
|
74
|
|
|
SELECT * |
75
|
|
|
FROM upload |
76
|
|
|
"; |
77
|
|
|
|
78
|
|
|
$implode = array(); |
79
|
|
|
|
80
|
|
|
if (!empty($data['filter_name'])) { |
81
|
|
|
$implode[] = "name LIKE '" . $this->db->escape($data['filter_name']) . "%'"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!empty($data['filter_filename'])) { |
85
|
|
|
$implode[] = "filename LIKE '" . $this->db->escape($data['filter_filename']) . "%'"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!empty($data['filter_date_added'])) { |
89
|
|
|
$implode[] = "date_added = '" . $this->db->escape($data['filter_date_added']) . "%'"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($implode) { |
93
|
|
|
$sql .= " WHERE " . implode(" AND ", $implode); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$sort_data = array( |
97
|
|
|
'name', |
98
|
|
|
'filename', |
99
|
|
|
'date_added' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { |
103
|
|
|
$sql .= " ORDER BY " . $data['sort']; |
|
|
|
|
104
|
|
|
} else { |
105
|
|
|
$sql .= " ORDER BY date_added"; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (isset($data['order']) && ($data['order'] == 'DESC')) { |
109
|
|
|
$sql .= " DESC"; |
|
|
|
|
110
|
|
|
} else { |
111
|
|
|
$sql .= " ASC"; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (isset($data['start']) || isset($data['limit'])) { |
115
|
|
|
if ($data['start'] < 0) { |
116
|
|
|
$data['start'] = 0; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($data['limit'] < 1) { |
120
|
|
|
$data['limit'] = 20; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$query = $this->db->query($sql); |
127
|
|
|
|
128
|
|
|
return $query->rows; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getTotalUploads() |
132
|
|
|
{ |
133
|
|
|
$sql = " |
|
|
|
|
134
|
|
|
SELECT COUNT(*) AS total |
135
|
|
|
FROM upload |
136
|
|
|
"; |
137
|
|
|
|
138
|
|
|
$implode = array(); |
139
|
|
|
|
140
|
|
|
if (!empty($data['filter_name'])) { |
|
|
|
|
141
|
|
|
$implode[] = "name LIKE '" . $this->db->escape($data['filter_name']) . "%'"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (!empty($data['filter_filename'])) { |
145
|
|
|
$implode[] = "filename LIKE '" . $this->db->escape($data['filter_filename']) . "%'"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!empty($data['filter_date_added'])) { |
149
|
|
|
$implode[] = "date_added = '" . $this->db->escape($data['filter_date_added']) . "'"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($implode) { |
153
|
|
|
$sql .= " WHERE " . implode(" AND ", $implode); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$query = $this->db->query($sql); |
157
|
|
|
|
158
|
|
|
return $query->row['total']; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.