This repository was archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
356 lines (308 loc) · 12.6 KB
/
common.php
File metadata and controls
356 lines (308 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* MediaAttach
*
* @version $Id: common.php 110 2008-04-19 9:17:48Z weckamc $
* @author Axel Guckelsberger
* @link http://guite.de
* @copyright Copyright (C) 2008 by Guite
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*/
/* categorization modes */
define('MEDIAATTACH_CATMODE_NONE', 0);
define('MEDIAATTACH_CATMODE_CATEGORIES', 1);
define('MEDIAATTACH_CATMODE_MODULES', 2);
define('MEDIAATTACH_CATMODE_USERS', 4);
/**
* utility function for getting file names
*
* @return array file name depending on naming and full file path
*/
function _maIntGetFilenameForDefinition($filename, $extension, $naming, $namingprefix)
{
$uploaddir = pnModGetVar('MediaAttach', 'uploaddir');
$backupFilename = $filename;
$iterIndex = -1;
do {
if ($naming == 0) {
// original file name
$filenameCharCount = strlen($filename);
for ($y = 0; $y < $filenameCharCount; $y++) {
if (!ereg("([0-9A-Za-z_\.])", $filename[$y]))
$filename[$y] = '_';
}
// append incremented number
if ($iterIndex > 0) {
// strip off extension
$filename = str_replace('.' . $extension, '', $backupFilename);
// add iterated number
$filename .= (string) ++$iterIndex;
// readd extension
$filename .= '.' . $extension;
} else
$iterIndex++;
} else if ($naming == 1) {
// md5 name
$filename = md5(uniqid(mt_rand(), TRUE)) . '.' . $extension;
} else if ($naming == 2) {
// prefix with random number
$filename = $namingprefix . mt_rand(1, 999999) . '.' . $extension;
}
if (StringUtil::left($filename, 1) == '/' || StringUtil::left($filename, 1) == '\\') {
$filename = substr($filename, 1, strlen($filename) - 1);
}
} while (file_exists($uploaddir . '/' . $filename)); // repeat until we have a new name
// return file name as well as it's path
return array($filename, $uploaddir . '/' . $filename);
}
/**
* display a given file size in a readable format
*
* @param size string file size in bytes
* @param nodesc boolean if set to true the description will not be appended
* @param onlydesc boolean if set to true only the description will be returned
* @return string file size in a readable form
*/
function _maIntCalcReadableFilesize($size, $nodesc = false, $onlydesc = false)
{
$dom = ZLanguage::getModuleDomain('MediaAttach');
$sizeDesc = __('Bytes', $dom); // we have bytes as default
if ($size >= 1024) {
$size /= 1024;
$sizeDesc = __('KB', $dom); // kilobytes
}
if ($size >= 1024) {
$size /= 1024;
$sizeDesc = __('MB', $dom); // megabytes
}
if ($size >= 1024) {
$size /= 1024;
$sizeDesc = __('GB', $dom); // gigabytes
}
$sizeDesc = ' ' . $sizeDesc;
// format number
$dec_point = ',';
$thousands_separator = '.';
if ($size - number_format($size, 0) >= 0.005) {
$size = number_format($size, 2, $dec_point, $thousands_separator);
} else {
$size = number_format($size, 0, '', $thousands_separator);
}
// append size descriptor if desired
if (!$nodesc) {
$size .= $sizeDesc;
}
// return either only the description or the complete string
$result = ($onlydesc) ? $sizeDesc : $size;
return $result;
}
/**
* perform some checks and modifications on a given file object
*
* @param file array input object
* @param currentUser string current username
* @param ownHandling bool shall users modify their own files
* @return array modified object
*/
function _maIntPrepFileForTemplate($file, $currentUser, $ownHandling)
{
$dom = ZLanguage::getModuleDomain('MediaAttach');
// append default title if necessary
if ($file['title'] == '') {
$file['title'] = __('No title', $dom);
}
// call transform hooks
// list ($file['title'], $file['desc']) = pnModCallHooks('item', 'transform', '', array($file['title'], $file['desc']));
// determine if current user may modify this file in terms of config
$isOwner = ($ownHandling && ($currentUser == $file['uid']));
$currentmodname = $file['modname'];
$currentobjectid = $file['objectid'];
$currentfileid = $file['fileid'];
// determine if current user may modify this file in terms in general (config + permissions)
$file['allowedit'] = ($isOwner || SecurityUtil::checkPermission('MediaAttach::', "$currentmodname:$currentobjectid:$currentfileid", ACCESS_EDIT));
$file['allowdelete'] = ($isOwner || SecurityUtil::checkPermission('MediaAttach::', "$currentmodname:$currentobjectid:$currentfileid", ACCESS_DELETE));
// return our modified file instance
return $file;
}
/**
* utility function for determine bytes from megabytes
*
* @return int calculated file size
*/
function _maIntMBToByte($amount)
{
$amount = str_replace('.', '', $amount);
$amount = str_replace(',', '', $amount);
$amount = $amount * 1024 * 1024;
return $amount;
}
/**
* create appropriate WHERE string
*
* @param fileFilter string optional filter by file id
* @param formatFilter string optional filter by file format
* @param userFilter int optional filter by given user id
* @param moduleFilter string optional filter by module name
* @param objectidFilter string optional filter by object id
* @param searchfor string optional search term string
* @param bool string optional string 'AND' or 'OR'
* @param noexpand bool not used within selectExpanded* (default: false)
* @return string built string for $where
*/
function _maIntBuildWhereString($args)
{
$where = '';
// no further $args checking here --> has been done before
$pntables = pnDBGetTables();
$filescolumn = $pntables['ma_files_column'];
$formatscolumn = $pntables['ma_formats_column'];
$prefix = '';
if (!isset($args['noexpand']) || $args['noexpand'] != true) {
$prefix = 'tbl.';
}
if (!empty($args['fileFilter'])) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['fileid'] . ' IN (';
$firstone = 1;
foreach ($args['fileFilter'] as $currentFilter) {
if ($firstone)
$firstone = 0;
else
$where .= ',';
$where .= "'" . $currentFilter . "'";
}
$where .= ')';
}
if (!empty($args['formatFilter'])) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['extension'] . ' IN (';
$firstone = 1;
foreach ($args['formatFilter'] as $currentFilter) {
if ($firstone)
$firstone = 0;
else
$where .= ',';
$where .= "'" . $currentFilter . "'";
}
$where .= ')';
}
if (!empty($args['userFilter'])) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['uid'] . ' = ' . (int) DataUtil::formatForStore($args['userFilter']);
}
if (!empty($args['moduleFilter'])) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['modname'] . " = '" . DataUtil::formatForStore($args['moduleFilter']) . "'";
} elseif ($args['moduleFilter'] != false) {
$currentType = FormUtil::getPassedValue('type', '', 'GETPOST');
$currentFunc = FormUtil::getPassedValue('func', '', 'GETPOST');
$currentMod = pnModGetName();
if ($currentMod != 'MediaAttach' && $currentMod != 'Profile' && $currentMod != 'content' || ($currentMod == 'MediaAttach' && $currentType != 'account' && $currentType != 'ajax' && $currentType != 'external' && ($currentFunc != 'view' && $currentFunc != 'main' && !empty($currentFunc) && $currentFunc != 'getfilelist'))) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['modname'] . " != 'MediaAttach'";
}
}
if (!empty($args['objectidFilter'])) {
if (!empty($where))
$where .= ' AND ';
$where .= $prefix . $filescolumn['objectid'] . " = '" . DataUtil::formatForStore($args['objectidFilter']) . "'";
}
if (!empty($args['searchfor'])) {
$args['searchfor'] = DataUtil::formatForStore(trim($args['searchfor']));
if (!empty($where))
$where .= ' AND ';
$flag = false;
$words = explode(' ', $args['searchfor']);
$where .= '(';
foreach ($words as $word) {
if ($flag) {
if ($bool != 'AND' && $bool != 'OR')
$bool = 'OR';
$where .= ' ' . $bool . ' ';
}
$where .= '(' . $prefix . $filescolumn['title'] . " LIKE '%$word%' OR " . $prefix . $filescolumn['desc'] . " LIKE '%$word%') \n";
$flag = true;
}
$where .= ')';
}
return $where;
}
/**
* utility function to select a template for the current use case
*/
function _maIntChooseTemplate(&$render, $type, $func, $modname)
{
if ($render->template_exists('MediaAttach_' . $type . '_' . $func . '_' . DataUtil::formatForOS($modname) . '.htm')) {
return 'MediaAttach_' . $type . '_' . $func . '_' . DataUtil::formatForOS($modname) . '.htm';
} else {
return 'MediaAttach_' . $type . '_' . $func . '.htm';
}
}
/**
* helper method treating outsourced file list handling
*/
function _maIntProcessFileList(&$render, $itemsperpage, $customFilter, $dataSource = 'GET')
{
$dom = ZLanguage::getModuleDomain('MediaAttach');
$itemsperpage = (int) FormUtil::getPassedValue('itemsperpage', $itemsperpage, $dataSource);
$startnum = (int) FormUtil::getPassedValue('startnum', 0, $dataSource);
$sortby = FormUtil::getPassedValue('sortby', 'date', $dataSource);
$sortdir = FormUtil::getPassedValue('sortdir', ($sortby == 'date') ? 'desc' : 'asc', $dataSource);
$preview = (int) FormUtil::getPassedValue('preview', 0, $dataSource);
$onlyimages = (int) FormUtil::getPassedValue('onlyimages', 0, $dataSource);
$searchfor = FormUtil::getPassedValue('searchfor', '', $dataSource);
$thumbnr = (int) FormUtil::getPassedValue('thumbnr', 0, $dataSource);
if ($thumbnr == 0) {
$thumbnr = pnModGetVar('MediaAttach', 'defaultthumb');
}
$formatFilter = ($onlyimages == 1) ? array('gif', 'jpg', 'jpeg', 'png') : '';
$fetchArgs = array('startnum' => $startnum, 'numitems' => $itemsperpage, 'sortby' => $sortby, 'sortdir' => $sortdir, 'formatFilter' => $formatFilter, 'searchfor' => $searchfor);
if (!empty($customFilter) && is_array($customFilter)) {
foreach ($customFilter as $filterName => $filterValue) {
$fetchArgs[$filterName] = $filterValue;
}
}
$files = pnModAPIFunc('MediaAttach', 'user', 'getalluploads', $fetchArgs);
if ($files === false) {
return LogUtil::registerError(__('Error! Could not load items.', $dom));
}
$render->assign('sortby', DataUtil::formatForDisplay($sortby));
$render->assign('sortdir', DataUtil::formatForDisplay($sortdir));
$render->assign('itemsperpage', $itemsperpage);
$render->assign('preview', $preview);
$render->assign('onlyimages', $onlyimages);
$render->assign('thumbnr', $thumbnr);
if (empty($files)) {
$render->assign('filesthere', 0);
} else {
$numFiles = count($files);
$currentUser = pnUserGetVar('uid');
$ownHandling = pnModGetVar('MediaAttach', 'ownhandling');
for ($i = 0; $i < $numFiles; $i++) {
$files[$i] = _maIntPrepFileForTemplate($files[$i], $currentUser, $ownHandling);
}
$render->assign('filesthere', 1);
$render->assign('files', $files);
$render->assign('pager', array('numitems' => pnModAPIFunc('MediaAttach', 'user', 'countuploads', $fetchArgs), 'itemsperpage' => $itemsperpage));
}
return;
}
/**
* utility function to get all modules from which files can be imported
*/
function _maGetImportModules()
{
$supportedModules = Array('Downloads', 'mediashare', 'PhotoGallery', 'PNphpBB2', 'pnUpper');
$availableModules = Array();
foreach ($supportedModules as $modName) {
if (pnModAvailable($modName) && !file_exists('pnTemp/convertLog_MediaAttach_import_from_' . DataUtil::formatForOS($modName) . '.txt')) {
$availableModules[] = $modName;
}
}
return $availableModules;
}