1 | <?php |
||
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){ |
|
50 | |||
51 | 2 | public function checkFeed(){ |
|
65 | |||
66 | 2 | public function downloadOwncloud($progressCallback = null){ |
|
67 | 2 | $response = $this->getDefaultResponse(); |
|
68 | 2 | if (is_null($progressCallback)){ |
|
69 | $progressCallback = function (){}; |
||
70 | } |
||
71 | try { |
||
72 | 2 | $feed = $this->getFeed(); |
|
73 | 2 | $path = $this->fetcher->getBaseDownloadPath($feed); |
|
74 | // Fixme: Daily channel has no checksum |
||
75 | 2 | $isDailyChannel = $this->fetcher->getUpdateChannel() == 'daily'; |
|
76 | 2 | if (!$isDailyChannel){ |
|
77 | 2 | $md5 = $this->fetcher->getMd5($feed); |
|
78 | } else { |
||
79 | // We can't check md5 so we don't trust the cache |
||
80 | $this->fsHelper->removeIfExists($path); |
||
81 | } |
||
82 | 2 | if ($isDailyChannel || !$this->checkIntegrity($path, $md5)){ |
|
|
|||
83 | 1 | $this->fetcher->getOwncloud($feed, $progressCallback); |
|
84 | } |
||
85 | |||
86 | 1 | if ($isDailyChannel || $this->checkIntegrity($path, $md5)){ |
|
87 | 1 | $response['success'] = true; |
|
88 | 1 | $response['data']['path'] = $path; |
|
89 | } else { |
||
90 | 1 | $response['exception'] = new \Exception('Deleted ' . $feed->getDownloadedFileName() . ' due to wrong checksum'); |
|
91 | } |
||
92 | 1 | } catch (\Exception $e) { |
|
93 | 1 | if (isset($path)){ |
|
94 | 1 | $this->fsHelper->removeIfExists($path); |
|
95 | } |
||
96 | 1 | $response['exception'] = $e; |
|
97 | } |
||
98 | 2 | return $response; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Check if package is not corrupted on download |
||
103 | * @param string $path |
||
104 | * @param string $md5 |
||
105 | * @return boolean |
||
106 | */ |
||
107 | 2 | protected function checkIntegrity($path, $md5){ |
|
108 | 2 | $fileExists = $this->fsHelper->fileExists($path); |
|
109 | 2 | $checksumMatch = $md5 === $this->fsHelper->md5File($path); |
|
110 | 2 | if ($fileExists && !$checksumMatch){ |
|
111 | 1 | $this->fsHelper->removeIfExists($path); |
|
112 | 1 | $fileExists = false; |
|
113 | } |
||
114 | 2 | return $fileExists; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get a Feed instance |
||
119 | * @param bool $useCache |
||
120 | * @return \Owncloud\Updater\Utils\Feed |
||
121 | */ |
||
122 | 2 | protected function getFeed($useCache = true){ |
|
128 | |||
129 | /** |
||
130 | * Init response array |
||
131 | * @return array |
||
132 | */ |
||
133 | 4 | protected function getDefaultResponse(){ |
|
141 | } |
||
142 |
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: