Completed
Push — skip-by-size ( 8783f0...918c28 )
by Victor
05:40
created

RequestHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C getUploadSize() 0 34 11
A isScriptName() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2017 Viktar Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus;
10
11
use \OCP\IRequest;
12
13
class RequestHelper {
14
15
	/** @var  IRequest */
16
	private $request;
17
18
	public function __construct(IRequest $request) {
19
		$this->request = $request;
20
	}
21
22
	/**
23
	 * @param string $path
24
	 * @return int|null
25
	 */
26
	public function getUploadSize($path) {
27
		$uploadSize = null;
28
29
		try {
30
			$requestMethod = $this->request->getMethod();
31
			$isRemoteScript = $this->isScriptName('remote.php');
32
			// Are we uploading anything?
33
			if (in_array($requestMethod, ['MOVE', 'PUT']) && $isRemoteScript) {
34
				$pathInfo = $this->request->getPathInfo();
35
36
				// Chunks are not scanned
37
				$isChunk = \OC_FileChunking::isWebdavChunk()
38
				           || ($requestMethod === 'PUT' &&  strpos($path, 'uploads/') === 0);
39
				if ($isChunk) {
40
					return null;
41
				}
42
43
				$isDavPathV1 = $pathInfo === '/webdav' || strpos($pathInfo, '/webdav/') === 0;
0 ignored issues
show
Unused Code introduced by
$isDavPathV1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
				$isDavPathV2 = $pathInfo === '/dav/files'
0 ignored issues
show
Unused Code introduced by
$isDavPathV2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
					|| strpos($pathInfo, '/dav/files/') === 0
46
					|| strpos($pathInfo, '/dav/uploads/') === 0;
47
48
				if ($requestMethod === 'PUT') {
49
					$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
50
				} else {
51
					$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
52
				}
53
			}
54
		} catch (\Exception $e) {
55
			// Happens in CLI mode
56
		}
57
58
		return $uploadSize;
59
	}
60
61
	/**
62
	 *
63
	 * @param $string
64
	 * @return bool
65
	 */
66
	public function isScriptName($string) {
67
		$pattern = sprintf('|/%s|', preg_quote($string));
68
		return preg_match($pattern, $this->request->getScriptName()) === 1;
69
	}
70
}
71