1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// XOOPS - PHP Content Management System // |
5
|
|
|
// Copyright (c) 2000-2016 XOOPS.org // |
6
|
|
|
// <http://xoops.org/> // |
7
|
|
|
// ------------------------------------------------------------------------ // |
8
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
9
|
|
|
include_once XOOPS_ROOT_PATH . '/modules/smartpartner/include/common.php'; |
10
|
|
|
require_once(SMARTPARTNER_ROOT_PATH . 'class/baseObjectHandler.php'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* smartpartnerMimetype class |
14
|
|
|
* |
15
|
|
|
* Information about an individual mimetype |
16
|
|
|
* |
17
|
|
|
* <code> |
18
|
|
|
* $hMime = xoops_getModuleHandler('mimetype', 'smartpartner'); |
19
|
|
|
* $mimetype =& $hMime->get(1); |
20
|
|
|
* $mime_id = $mimetype->getVar('id'); |
21
|
|
|
* </code> |
22
|
|
|
* |
23
|
|
|
* @author Eric Juden <[email protected]> |
24
|
|
|
* @access public |
25
|
|
|
* @package smartpartner |
26
|
|
|
*/ |
27
|
|
|
class smartpartnerMimetype extends XoopsObject |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* smartpartnerMimetype constructor. |
31
|
|
|
* @param null $id |
32
|
|
|
*/ |
33
|
|
|
public function __construct($id = null) |
34
|
|
|
{ |
35
|
|
|
$this->initVar('mime_id', XOBJ_DTYPE_INT, null, false); |
36
|
|
|
$this->initVar('mime_ext', XOBJ_DTYPE_TXTBOX, null, true, 60); |
37
|
|
|
$this->initVar('mime_types', XOBJ_DTYPE_TXTAREA, null, false, 1024); |
38
|
|
|
$this->initVar('mime_name', XOBJ_DTYPE_TXTBOX, null, true, 255); |
39
|
|
|
$this->initVar('mime_admin', XOBJ_DTYPE_INT, null, false); |
40
|
|
|
$this->initVar('mime_user', XOBJ_DTYPE_INT, null, false); |
41
|
|
|
|
42
|
|
|
if (isset($id)) { |
43
|
|
|
if (is_array($id)) { |
44
|
|
|
$this->assignVars($id); |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
|
|
$this->setNew(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} // end of class |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Class smartpartnerMimetypeHandler |
54
|
|
|
*/ |
55
|
|
|
class smartpartnerMimetypeHandler extends SmartpartnerBaseObjectHandler |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Name of child class |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
* @access private |
62
|
|
|
*/ |
63
|
|
|
public $classname = 'smartpartnermimetype'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* DB Table Name |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
* @access private |
70
|
|
|
*/ |
71
|
|
|
public $_dbtable = 'smartpartner_mimetypes'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor |
75
|
|
|
* |
76
|
|
|
* @param object|XoopsDatabase $db reference to a xoopsDB object |
77
|
|
|
*/ |
78
|
|
|
public function __construct(XoopsDatabase $db) |
79
|
|
|
{ |
80
|
|
|
parent::init($db); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* retrieve a mimetype object from the database |
85
|
|
|
* @param int $id ID of mimetype |
86
|
|
|
* @return object {@link smartpartnerMimetype} |
|
|
|
|
87
|
|
|
* @access public |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function &get($id) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$id = (int)$id; |
92
|
|
|
if ($id > 0) { |
93
|
|
|
$sql = $this->_selectQuery(new Criteria('mime_id', $id)); |
94
|
|
|
if (!$result = $this->_db->query($sql)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
$numrows = $this->_db->getRowsNum($result); |
98
|
|
|
if ($numrows == 1) { |
99
|
|
|
$obj = new $this->classname($this->_db->fetchArray($result)); |
100
|
|
|
|
101
|
|
|
return $obj; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* retrieve objects from the database |
110
|
|
|
* |
111
|
|
|
* @param object $criteria {@link CriteriaElement} conditions to be met |
|
|
|
|
112
|
|
|
* @return array array of {@link smartpartnerMimetype} objects |
113
|
|
|
* @access public |
114
|
|
|
*/ |
115
|
|
|
public function &getObjects($criteria = null) |
116
|
|
|
{ |
117
|
|
|
$ret = array(); |
118
|
|
|
$limit = $start = 0; |
119
|
|
|
$sql = $this->_selectQuery($criteria); |
120
|
|
|
if (isset($criteria)) { |
121
|
|
|
$limit = $criteria->getLimit(); |
122
|
|
|
$start = $criteria->getStart(); |
123
|
|
|
} |
124
|
|
|
//echo "<br>$sql<br>";exit; |
|
|
|
|
125
|
|
|
$result = $this->_db->query($sql, $limit, $start); |
126
|
|
|
// if no records from db, return empty array |
127
|
|
|
if (!$result) { |
128
|
|
|
return $ret; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Add each returned record to the result array |
132
|
|
|
while ($myrow = $this->_db->fetchArray($result)) { |
133
|
|
|
$obj = new $this->classname($myrow); |
134
|
|
|
$ret[] =& $obj; |
135
|
|
|
unset($obj); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $ret; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Format mime_types into array |
143
|
|
|
* |
144
|
|
|
* @param null $mime_ext |
145
|
|
|
* @return array array of mime_types |
146
|
|
|
* @access public |
147
|
|
|
*/ |
148
|
|
|
public function getArray($mime_ext = null) |
149
|
|
|
{ |
150
|
|
|
/*global $smartPartnerIsAdmin, $xoopsUser, $xoopsModule; |
|
|
|
|
151
|
|
|
|
152
|
|
|
$ret = array(); |
153
|
|
|
if ($xoopsUser && !$smartPartnerIsAdmin) { |
154
|
|
|
// For user uploading |
155
|
|
|
$crit = new CriteriaCompo(new Criteria('mime_user', 1)); //$sql = sprintf("SELECT * FROM %s WHERE mime_user=1", $xoopsDB->prefix('smartpartner_mimetypes')); |
156
|
|
|
} elseif ($xoopsUser && $smartPartnerIsAdmin) { |
157
|
|
|
// For admin uploading |
158
|
|
|
$crit = new CriteriaCompo(new Criteria('mime_admin', 1)); //$sql = sprintf("SELECT * FROM %s WHERE mime_admin=1", $xoopsDB->prefix('smartpartner_mimetypes')); |
159
|
|
|
} else { |
160
|
|
|
return $ret; |
161
|
|
|
} |
162
|
|
|
if ($mime_ext) { |
163
|
|
|
$crit->add(new Criteria('mime_ext', $mime_ext)); |
164
|
|
|
} |
165
|
|
|
$result = $this->getObjects($crit); |
166
|
|
|
|
167
|
|
|
// if no records from db, return empty array |
168
|
|
|
if (!$result) { |
169
|
|
|
return $ret; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
foreach ($result as $mime) { |
173
|
|
|
$line = explode(" ", $mime->getVar('mime_types')); |
174
|
|
|
foreach ($line as $row) { |
175
|
|
|
$allowed_mimetypes[] = array('type'=>$row, 'ext'=>$mime->getVar('mime_ext')); |
176
|
|
|
} |
177
|
|
|
}*/ |
178
|
|
|
global $xoopsModuleConfig; |
|
|
|
|
179
|
|
|
$mymetypesArray = include_once(SMARTPARTNER_ROOT_PATH . '/include/mimetypes.inc.php'); |
180
|
|
|
foreach (explode('|', $xoopsModuleConfig['allowed_ext']) as $ext) { |
181
|
|
|
$allowed_mimetypes[] = array('type' => $mymetypesArray[$ext], 'ext' => $ext); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $allowed_mimetypes; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Checks to see if the user uploading the file has permissions to upload this mimetype |
189
|
|
|
* @param $post_field file being uploaded |
190
|
|
|
* @return false if no permission, return mimetype if has permission |
|
|
|
|
191
|
|
|
* @access public |
192
|
|
|
*/ |
193
|
|
|
public function checkMimeTypes($post_field) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$fname = $_FILES[$post_field]['name']; |
196
|
|
|
$farray = explode('.', $fname); |
197
|
|
|
$fextension = strtolower($farray[count($farray) - 1]); |
|
|
|
|
198
|
|
|
|
199
|
|
|
$allowed_mimetypes = $this->getArray(); |
200
|
|
|
if (empty($allowed_mimetypes)) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
foreach ($allowed_mimetypes as $mime) { |
|
|
|
|
204
|
|
|
if ($mime['type'] == $_FILES[$post_field]['type']) { |
205
|
|
|
$allowed_mimetypes = $mime['type']; |
206
|
|
|
break; |
207
|
|
|
} else { |
208
|
|
|
$allowed_mimetypes = false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $allowed_mimetypes; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Create a "select" SQL query |
217
|
|
|
* @param object $criteria {@link CriteriaElement} to match |
|
|
|
|
218
|
|
|
* @param bool $join |
219
|
|
|
* @return string SQL query |
|
|
|
|
220
|
|
|
* @access private |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function _selectQuery($criteria = null, $join = false) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
if (!$join) { |
225
|
|
|
$sql = sprintf('SELECT * FROM %s', $this->_db->prefix($this->_dbtable)); |
226
|
|
|
} else { |
227
|
|
|
echo 'no need for join...'; |
228
|
|
|
exit; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
231
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
232
|
|
|
if ($criteria->getSort() != '') { |
233
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $sql; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $obj |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function _insertQuery($obj) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
// Copy all object vars into local variables |
247
|
|
|
foreach ($obj->cleanVars as $k => $v) { |
248
|
|
|
${$k} = $v; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$sql = sprintf('INSERT INTO %s (mime_id, mime_ext, mime_types, mime_name, mime_admin, mime_user) VALUES |
252
|
|
|
(%u, %s, %s, %s, %u, %u)', $this->_db->prefix($this->_dbtable), $mime_id, $this->_db->quoteString($mime_ext), $this->_db->quoteString($mime_types), $this->_db->quoteString($mime_name), |
|
|
|
|
253
|
|
|
$mime_admin, $mime_user); |
|
|
|
|
254
|
|
|
|
255
|
|
|
return $sql; |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param $obj |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function _updateQuery($obj) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
// Copy all object vars into local variables |
265
|
|
|
foreach ($obj->cleanVars as $k => $v) { |
266
|
|
|
${$k} = $v; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$sql = sprintf('UPDATE %s SET mime_ext = %s, mime_types = %s, mime_name = %s, mime_admin = %u, mime_user = %u WHERE |
270
|
|
|
mime_id = %u', $this->_db->prefix($this->_dbtable), $this->_db->quoteString($mime_ext), $this->_db->quoteString($mime_types), $this->_db->quoteString($mime_name), $mime_admin, |
|
|
|
|
271
|
|
|
$mime_user, $mime_id); |
|
|
|
|
272
|
|
|
|
273
|
|
|
return $sql; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param $obj |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function _deleteQuery($obj) |
281
|
|
|
{ |
282
|
|
|
$sql = sprintf('DELETE FROM %s WHERE mime_id = %u', $this->_db->prefix($this->_dbtable), $obj->getVar('mime_id')); |
283
|
|
|
|
284
|
|
|
return $sql; |
|
|
|
|
285
|
|
|
} |
286
|
|
|
} // end class |
287
|
|
|
|
288
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.