1 | <?php |
||
33 | class DownloadController { |
||
34 | |||
35 | /** |
||
36 | * @var Fetcher |
||
37 | */ |
||
38 | protected $fetcher; |
||
39 | |||
40 | /** |
||
41 | * @var Registry |
||
42 | */ |
||
43 | protected $registry; |
||
44 | |||
45 | /** |
||
46 | * @var FilesystemHelper |
||
47 | */ |
||
48 | protected $fsHelper; |
||
49 | |||
50 | /** |
||
51 | * DownloadController constructor. |
||
52 | * |
||
53 | * @param Fetcher $fetcher |
||
54 | * @param Registry $registry |
||
55 | * @param FilesystemHelper $fsHelper |
||
56 | */ |
||
57 | 4 | public function __construct(Fetcher $fetcher, Registry $registry, FilesystemHelper $fsHelper){ |
|
62 | |||
63 | /** |
||
64 | * @return array |
||
65 | */ |
||
66 | 2 | public function checkFeed(){ |
|
78 | |||
79 | /** |
||
80 | * @param null $progressCallback |
||
81 | * @return array |
||
82 | */ |
||
83 | 2 | public function downloadOwncloud($progressCallback = null){ |
|
84 | 2 | $response = $this->getDefaultResponse(); |
|
85 | 2 | if (is_null($progressCallback)){ |
|
86 | $progressCallback = function (){}; |
||
87 | } |
||
88 | try { |
||
89 | 2 | $feed = $this->getFeed(); |
|
90 | 2 | $path = $this->fetcher->getBaseDownloadPath($feed); |
|
91 | // Fixme: Daily channel has no checksum |
||
92 | 2 | $isDailyChannel = $this->fetcher->getUpdateChannel() == 'daily'; |
|
93 | 2 | if (!$isDailyChannel){ |
|
94 | 2 | $md5 = $this->fetcher->getMd5($feed); |
|
95 | } else { |
||
96 | // We can't check md5 so we don't trust the cache |
||
97 | $this->fsHelper->removeIfExists($path); |
||
98 | } |
||
99 | 2 | if ($isDailyChannel || !$this->checkIntegrity($path, $md5)){ |
|
|
|||
100 | 1 | $this->fetcher->getOwncloud($feed, $progressCallback); |
|
101 | } |
||
102 | |||
103 | 1 | if ($isDailyChannel || $this->checkIntegrity($path, $md5)){ |
|
104 | 1 | $response['success'] = true; |
|
105 | 1 | $response['data']['path'] = $path; |
|
106 | } else { |
||
107 | 1 | $response['exception'] = new \Exception('Deleted ' . $feed->getDownloadedFileName() . ' due to wrong checksum'); |
|
108 | } |
||
109 | 1 | } catch (\Exception $e) { |
|
110 | 1 | if (isset($path)){ |
|
111 | 1 | $this->fsHelper->removeIfExists($path); |
|
112 | } |
||
113 | 1 | $response['exception'] = $e; |
|
114 | } |
||
115 | 2 | return $response; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Check if package is not corrupted on download |
||
120 | * @param string $path |
||
121 | * @param string $md5 |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 2 | protected function checkIntegrity($path, $md5){ |
|
125 | 2 | $fileExists = $this->fsHelper->fileExists($path); |
|
126 | 2 | $checksumMatch = $fileExists && $md5 === $this->fsHelper->md5File($path); |
|
127 | 2 | if (!$checksumMatch){ |
|
128 | 1 | $this->fsHelper->removeIfExists($path); |
|
129 | } |
||
130 | 2 | return $checksumMatch; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get a Feed instance |
||
135 | * @param bool $useCache |
||
136 | * @return \Owncloud\Updater\Utils\Feed |
||
137 | */ |
||
138 | 2 | protected function getFeed($useCache = true){ |
|
144 | |||
145 | /** |
||
146 | * Init response array |
||
147 | * @return array |
||
148 | */ |
||
149 | 4 | protected function getDefaultResponse(){ |
|
157 | } |
||
158 |
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: