Completed
Pull Request — master (#2)
by Michael
07:48
created

AdminerFileUpload::AdminerFileUpload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
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]+') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
		$this->uploadPath = $uploadPath;
21
		$this->displayPath = ($displayPath !== null ? $displayPath : $uploadPath);
22
		$this->extensions = $extensions;
23
	}
24
25
	function editInput($table, $field, $attrs, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attrs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $function is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $original is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		if ($val != '&nbsp;' && preg_match('~(.*)_path$~', $field['field'], $regs)) {
49
			$link = "$this->displayPath$_GET[select]/$regs[1]-$val";
50
		}
51
	}
52
53
}
54