|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Victor Dubiniuk <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Owncloud\Updater\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use Owncloud\Updater\Utils\Fetcher; |
|
25
|
|
|
use Owncloud\Updater\Utils\Registry; |
|
26
|
|
|
use Owncloud\Updater\Utils\FilesystemHelper; |
|
27
|
|
|
|
|
28
|
|
|
class DownloadController { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Fetcher |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fetcher; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Registry |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $registry; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var FilesystemHelper |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fsHelper; |
|
44
|
|
|
|
|
45
|
4 |
|
public function __construct(Fetcher $fetcher, Registry $registry, FilesystemHelper $fsHelper){ |
|
46
|
4 |
|
$this->fetcher = $fetcher; |
|
47
|
4 |
|
$this->registry = $registry; |
|
48
|
4 |
|
$this->fsHelper = $fsHelper; |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function checkFeed(){ |
|
52
|
2 |
|
$response = $this->getDefaultResponse(); |
|
53
|
|
|
try { |
|
54
|
2 |
|
$feed = $this->fetcher->getFeed(); |
|
55
|
1 |
|
$response['success'] = true; |
|
56
|
1 |
|
$response['data']['feed'] = $feed; |
|
57
|
2 |
|
} catch (\Exception $e){ |
|
58
|
1 |
|
$response['exception'] = $e; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function downloadOwncloud($progressCallback = null){ |
|
65
|
2 |
|
$response = $this->getDefaultResponse(); |
|
66
|
2 |
|
if (is_null($progressCallback)){ |
|
67
|
|
|
$progressCallback = function (){}; |
|
68
|
2 |
|
} |
|
69
|
|
|
try { |
|
70
|
2 |
|
$feed = $this->getFeed(); |
|
71
|
2 |
|
$path = $this->fetcher->getBaseDownloadPath($feed); |
|
72
|
|
|
// Fixme: Daily channel has no checksum |
|
73
|
2 |
|
$isDailyChannel = $this->fetcher->getUpdateChannel() == 'daily'; |
|
74
|
2 |
|
if (!$isDailyChannel){ |
|
75
|
2 |
|
$md5 = $this->fetcher->getMd5($feed); |
|
76
|
2 |
|
} else { |
|
77
|
|
|
// We can't check md5 so we don't trust the cache |
|
78
|
|
|
$this->fsHelper->removeIfExists($path); |
|
79
|
|
|
} |
|
80
|
2 |
|
if ($isDailyChannel || !$this->checkIntegrity($path, $md5)){ |
|
|
|
|
|
|
81
|
1 |
|
$this->fetcher->getOwncloud($feed, $progressCallback); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
if ($isDailyChannel || $this->checkIntegrity($path, $md5)){ |
|
85
|
1 |
|
$response['success'] = true; |
|
86
|
1 |
|
$response['data']['path'] = $path; |
|
87
|
1 |
|
} else { |
|
88
|
|
|
$response['exception'] = new \Exception('Deleted ' . $feed->getDownloadedFileName() . ' due to wrong checksum'); |
|
89
|
|
|
} |
|
90
|
2 |
|
} catch (\Exception $e) { |
|
91
|
2 |
|
if (isset($path)){ |
|
92
|
1 |
|
$this->fsHelper->removeIfExists($path); |
|
93
|
1 |
|
} |
|
94
|
1 |
|
$response['exception'] = $e; |
|
95
|
|
|
} |
|
96
|
2 |
|
return $response; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Check if package is not corrupted on download |
|
101
|
|
|
* @param string $path |
|
102
|
|
|
* @param string $md5 |
|
103
|
|
|
* @return boolean |
|
104
|
|
|
*/ |
|
105
|
2 |
|
protected function checkIntegrity($path, $md5){ |
|
106
|
2 |
|
$fileExists = $this->fsHelper->fileExists($path); |
|
107
|
2 |
|
$checksumMatch = $fileExists && $md5 === $this->fsHelper->md5File($path); |
|
108
|
2 |
|
if (!$checksumMatch){ |
|
109
|
1 |
|
$this->fsHelper->removeIfExists($path); |
|
110
|
1 |
|
} |
|
111
|
2 |
|
return $checksumMatch; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get a Feed instance |
|
116
|
|
|
* @param bool $useCache |
|
117
|
|
|
* @return \Owncloud\Updater\Utils\Feed |
|
118
|
|
|
*/ |
|
119
|
2 |
|
protected function getFeed($useCache = true){ |
|
120
|
2 |
|
if ($useCache && !is_null($this->registry->get('feed'))){ |
|
121
|
2 |
|
return $this->registry->get('feed'); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
return $this->fetcher->getFeed(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Init response array |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
4 |
|
protected function getDefaultResponse(){ |
|
131
|
|
|
return [ |
|
132
|
4 |
|
'success' => false, |
|
133
|
4 |
|
'exception' => '', |
|
134
|
4 |
|
'details' => '', |
|
135
|
4 |
|
'data' => [] |
|
136
|
4 |
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: