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(){ |
|
52 | 2 | $response = $this->getDefaultResponse(); |
|
53 | try { |
||
54 | 2 | $feed = $this->fetcher->getFeed(); |
|
55 | 1 | if ($feed->isValid()){ |
|
56 | 1 | $response['success'] = true; |
|
57 | 1 | $response['data']['feed'] = $feed; |
|
58 | } |
||
59 | 1 | } catch (\Exception $e){ |
|
60 | 1 | $response['exception'] = $e; |
|
61 | } |
||
62 | |||
63 | 2 | return $response; |
|
64 | } |
||
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 | // Fixme: Daily channel has no checksum |
||
74 | 2 | $isDailyChannel = $this->fetcher->getUpdateChannel() == 'daily'; |
|
75 | 2 | if (!$isDailyChannel){ |
|
76 | 2 | $md5 = $this->fetcher->getMd5($feed); |
|
77 | |||
78 | } |
||
79 | 2 | $path = $this->fetcher->getBaseDownloadPath($feed); |
|
80 | |||
81 | 2 | if ($isDailyChannel){ |
|
82 | $this->fsHelper->removeIfExists($path); |
||
83 | } |
||
84 | 2 | if ($isDailyChannel || !$this->checkIntegrity($path, $md5)){ |
|
|
|||
85 | 1 | $this->fetcher->getOwncloud($feed, $progressCallback); |
|
86 | } |
||
87 | |||
88 | 1 | if ($isDailyChannel || $this->checkIntegrity($path, $md5)){ |
|
89 | 1 | $response['success'] = true; |
|
90 | 1 | $response['data']['path'] = $path; |
|
91 | } else { |
||
92 | 1 | $response['exception'] = new \Exception('Deleted ' . $feed->getDownloadedFileName() . ' due to wrong checksum'); |
|
93 | } |
||
94 | 1 | } catch (\Exception $e) { |
|
95 | 1 | if (isset($path)){ |
|
96 | 1 | $this->fsHelper->removeIfExists($path); |
|
97 | } |
||
98 | 1 | $response['exception'] = $e; |
|
99 | } |
||
100 | 2 | return $response; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Check if package is not corrupted on download |
||
105 | * @param string $path |
||
106 | * @param string $md5 |
||
107 | * @return boolean |
||
108 | */ |
||
109 | 2 | protected function checkIntegrity($path, $md5){ |
|
110 | 2 | $fileExists = $this->fsHelper->fileExists($path); |
|
111 | 2 | $checksumMatch = $md5 === $this->fsHelper->md5File($path); |
|
112 | 2 | if ($fileExists && !$checksumMatch){ |
|
113 | 1 | $this->fsHelper->removeIfExists($path); |
|
114 | 1 | $fileExists = false; |
|
115 | } |
||
116 | 2 | return $fileExists; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get a Feed instance |
||
121 | * @param bool $useCache |
||
122 | * @return \Owncloud\Updater\Utils\Feed |
||
123 | */ |
||
124 | 2 | protected function getFeed($useCache = true){ |
|
130 | |||
131 | /** |
||
132 | * Init response array |
||
133 | * @return array |
||
134 | */ |
||
135 | 4 | protected function getDefaultResponse(){ |
|
143 | } |
||
144 |
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: