Completed
Push — stable7 ( 35746e...825360 )
by
unknown
29:41
created

newfile.php ➔ progress()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
eloc 17
nc 6
nop 6
dl 0
loc 24
rs 8.5125
1
<?php
2
3
// Init owncloud
4
global $eventSource;
5
6
if(!OC_User::isLoggedIn()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \OC_User::isLoggedIn() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
7
	exit;
8
}
9
10
\OC::$session->close();
11
12
// Get the params
13
$dir = isset( $_REQUEST['dir'] ) ? '/'.trim($_REQUEST['dir'], '/\\') : '';
14
$filename = isset( $_REQUEST['filename'] ) ? trim($_REQUEST['filename'], '/\\') : '';
15
$content = isset( $_REQUEST['content'] ) ? $_REQUEST['content'] : '';
16
$source = isset( $_REQUEST['source'] ) ? trim($_REQUEST['source'], '/\\') : '';
17
18
if($source) {
19
	$eventSource=new OC_EventSource();
20
} else {
21
	OC_JSON::callCheck();
22
}
23
24
function progress($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
0 ignored issues
show
Unused Code introduced by
The parameter $severity 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 $message 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 $message_code 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...
25
	static $filesize = 0;
26
	static $lastsize = 0;
27
	global $eventSource;
28
29
	switch($notification_code) {
30
		case STREAM_NOTIFY_FILE_SIZE_IS:
31
			$filesize = $bytes_max;
32
			break;
33
34
		case STREAM_NOTIFY_PROGRESS:
35
			if ($bytes_transferred > 0) {
36
				if (!isset($filesize)) {
37
				} else {
38
					$progress = (int)(($bytes_transferred/$filesize)*100);
39
					if($progress>$lastsize) { //limit the number or messages send
40
						$eventSource->send('progress', $progress);
41
					}
42
					$lastsize=$progress;
43
				}
44
			}
45
			break;
46
	}
47
}
48
49
$l10n = \OC_L10n::get('files');
50
51
$result = array(
52
	'success' 	=> false,
53
	'data'		=> NULL
54
);
55
$trimmedFileName = trim($filename);
56
57 View Code Duplication
if($trimmedFileName === '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
	$result['data'] = array('message' => (string)$l10n->t('File name cannot be empty.'));
59
	OCP\JSON::error($result);
60
	exit();
61
}
62
if($trimmedFileName === '.' || $trimmedFileName === '..') {
63
	$result['data'] = array('message' => (string)$l10n->t('"%s" is an invalid file name.', $trimmedFileName));
0 ignored issues
show
Documentation introduced by
$trimmedFileName is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
	OCP\JSON::error($result);
65
	exit();
66
}
67
68 View Code Duplication
if(!OCP\Util::isValidFileName($filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
	$result['data'] = array('message' => (string)$l10n->t("Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed."));
70
	OCP\JSON::error($result);
71
	exit();
72
}
73
74 View Code Duplication
if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
	$result['data'] = array('message' => (string)$l10n->t(
76
			'The target folder has been moved or deleted.'),
77
			'code' => 'targetnotfound'
78
		);
79
	OCP\JSON::error($result);
80
	exit();
81
}
82
83
//TODO why is stripslashes used on foldername in newfolder.php but not here?
84
$target = $dir.'/'.$filename;
85
86 View Code Duplication
if (\OC\Files\Filesystem::file_exists($target)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
	$result['data'] = array('message' => (string)$l10n->t(
88
			'The name %s is already used in the folder %s. Please choose a different name.',
89
			array($filename, $dir))
90
		);
91
	OCP\JSON::error($result);
92
	exit();
93
}
94
95
if($source) {
96
	$httpHelper = \OC::$server->getHTTPHelper();
97
	if(!$httpHelper->isHTTPURL($source)) {
98
		OCP\JSON::error(array('data' => array('message' => $l10n->t('Not a valid source'))));
99
		exit();
100
	}
101
102
	if (!ini_get('allow_url_fopen')) {
103
		$eventSource->send('error', array('message' => $l10n->t('Server is not allowed to open URLs, please check the server configuration')));
104
		$eventSource->close();
105
		exit();
106
	}
107
108
	$source = $httpHelper->getFinalLocationOfURL($source);
109
110
	$ctx = stream_context_create(\OC::$server->getHTTPHelper()->getDefaultContextArray(), array('notification' =>'progress'));
111
112
	$sourceStream=@fopen($source, 'rb', false, $ctx);
113
	$result = 0;
114
	if (is_resource($sourceStream)) {
115
		$meta = stream_get_meta_data($sourceStream);
116
		if (isset($meta['wrapper_data']) && is_array($meta['wrapper_data'])) {
117
			//check stream size
118
			$storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
119
			$freeSpace = $storageStats['freeSpace'];
120
121
			foreach($meta['wrapper_data'] as $header) {
122
				if (strpos($header, ':') === false){
123
					continue;
124
				}
125
				list($name, $value) = explode(':', $header);
126
				if ('content-length' === strtolower(trim($name))) {
127
					$length = (int) trim($value);
128
129
					if ($length > $freeSpace) {
130
						$delta = $length - $freeSpace;
131
						$humanDelta = OCP\Util::humanFileSize($delta);
132
133
						$eventSource->send('error', array('message' => (string)$l10n->t('The file exceeds your quota by %s', array($humanDelta))));
134
						$eventSource->close();
135
						fclose($sourceStream);
136
						exit();
137
					}
138
				}
139
			}
140
		}
141
		$result=\OC\Files\Filesystem::file_put_contents($target, $sourceStream);
142
	}
143
	if($result) {
144
		$meta = \OC\Files\Filesystem::getFileInfo($target);
145
		$data = \OCA\Files\Helper::formatFileInfo($meta);
146
		$eventSource->send('success', $data);
147
	} else {
148
		$eventSource->send('error', array('message' => $l10n->t('Error while downloading %s to %s', array($source, $target))));
149
	}
150
	if (is_resource($sourceStream)) {
151
		fclose($sourceStream);
152
	}
153
	$eventSource->close();
154
	exit();
155
} else {
156
	$success = false;
157
	if (!$content) {
158
		$templateManager = OC_Helper::getFileTemplateManager();
159
		$mimeType = OC_Helper::getMimetypeDetector()->detectPath($target);
160
		$content = $templateManager->getTemplate($mimeType);
161
	}
162
163
	if($content) {
164
		$success = \OC\Files\Filesystem::file_put_contents($target, $content);
165
	} else {
166
		$success = \OC\Files\Filesystem::touch($target);
167
	}
168
169
	if($success) {
170
		$meta = \OC\Files\Filesystem::getFileInfo($target);
171
		OCP\JSON::success(array('data' => \OCA\Files\Helper::formatFileInfo($meta)));
172
		exit();
173
	}
174
}
175
176
OCP\JSON::error(array('data' => array( 'message' => $l10n->t('Error when creating the file') )));
177