1
|
|
|
<?php |
2
|
|
|
//! delete |
3
|
|
|
|
4
|
|
|
/** Edit fields ending with "_path" by <input type="file"> and link to the uploaded files from select |
5
|
|
|
* @link https://www.adminer.org/plugins/#use |
6
|
|
|
* @author Jakub Vrana, http://www.vrana.cz/ |
7
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
8
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other) |
9
|
|
|
*/ |
10
|
|
|
class AdminerFileUpload { |
11
|
|
|
/** @access protected */ |
12
|
|
|
var $uploadPath, $displayPath, $extensions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string prefix for uploading data (create writable subdirectory for each table containing uploadable fields) |
16
|
|
|
* @param string prefix for displaying data, null stands for $uploadPath |
17
|
|
|
* @param string regular expression with allowed file extensions |
18
|
|
|
*/ |
19
|
|
|
function __construct($uploadPath = '../static/data/', $displayPath = null, $extensions = '[a-zA-Z0-9]+') { |
|
|
|
|
20
|
|
|
$this->uploadPath = $uploadPath; |
21
|
|
|
$this->displayPath = ($displayPath !== null ? $displayPath : $uploadPath); |
22
|
|
|
$this->extensions = $extensions; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function editInput($table, $field, $attrs, $value) { |
|
|
|
|
26
|
|
|
if (preg_match('~(.*)_path$~', $field['field'])) { |
27
|
|
|
return "<input type='file' name='fields-$field[field]'>"; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function processInput($field, $value, $function = '') { |
|
|
|
|
32
|
|
|
if (preg_match('~(.*)_path$~', $field['field'], $regs)) { |
33
|
|
|
$table = ($_GET['edit'] != '' ? $_GET['edit'] : $_GET['select']); |
34
|
|
|
$name = "fields-$field[field]"; |
35
|
|
|
if ($_FILES[$name]['error'] || !preg_match("~(\\.($this->extensions))?\$~", $_FILES[$name]['name'], $regs2)) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
//! unlink old |
39
|
|
|
$filename = uniqid() . $regs2[0]; |
40
|
|
|
if (!move_uploaded_file($_FILES[$name]['tmp_name'], "$this->uploadPath$table/$regs[1]-$filename")) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
return q($filename); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function selectVal($val, &$link, $field, $original) { |
|
|
|
|
48
|
|
|
if ($val != ' ' && preg_match('~(.*)_path$~', $field['field'], $regs)) { |
49
|
|
|
$link = "$this->displayPath$_GET[select]/$regs[1]-$val"; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.