1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - passman |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Sander Brand <[email protected]> |
9
|
|
|
* @copyright Sander Brand 2016 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Passman\Controller; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
15
|
|
|
use OC\App\AppManager; |
16
|
|
|
use OCA\Passman\Service\IconService; |
17
|
|
|
use OCA\Passman\Utility\Utils; |
18
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
19
|
|
|
use OCP\AppFramework\Http\DataDownloadResponse; |
20
|
|
|
use OCP\AppFramework\Http\Response; |
21
|
|
|
use OCP\IConfig; |
22
|
|
|
use OCP\IRequest; |
23
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
24
|
|
|
use OCP\AppFramework\ApiController; |
25
|
|
|
use OCA\Passman\Service\CredentialService; |
26
|
|
|
use \OCP\App; |
27
|
|
|
use OCP\IURLGenerator; |
28
|
|
|
|
29
|
|
|
class IconController extends ApiController { |
30
|
|
|
private $userId; |
31
|
|
|
private $credentialService; |
32
|
|
|
private $am; |
33
|
|
|
private $urlGenerator; |
34
|
|
|
|
35
|
|
|
public function __construct($AppName, |
36
|
|
|
IRequest $request, |
37
|
|
|
$UserId, |
38
|
|
|
CredentialService $credentialService, |
39
|
|
|
AppManager $am, |
40
|
|
|
IURLGenerator $urlGenerator |
41
|
|
|
) { |
42
|
|
|
parent::__construct( |
43
|
|
|
$AppName, |
44
|
|
|
$request, |
45
|
|
|
'GET, POST, DELETE, PUT, PATCH, OPTIONS', |
46
|
|
|
'Authorization, Content-Type, Accept', |
47
|
|
|
86400); |
48
|
|
|
$this->userId = $UserId; |
49
|
|
|
$this->credentialService = $credentialService; |
50
|
|
|
$this->am = $am; |
51
|
|
|
$this->urlGenerator = $urlGenerator; |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @NoAdminRequired |
57
|
|
|
* @NoCSRFRequired |
58
|
|
|
*/ |
59
|
|
|
public function getSingleIcon($base64Url) { |
60
|
|
|
$url = base64_decode(str_replace('_','/', $base64Url)); |
61
|
|
|
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { |
62
|
|
|
$url = "http://" . $url; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$icon = new IconService($url); |
67
|
|
|
|
68
|
|
|
if ($icon->icoExists) { |
69
|
|
|
$icon_json['type']= $icon->icoType; |
|
|
|
|
70
|
|
|
$icon_json['content']= base64_encode($icon->icoData); |
71
|
|
|
return new JSONResponse($icon_json); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return new JSONResponse(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @NoAdminRequired |
79
|
|
|
* @NoCSRFRequired |
80
|
|
|
*/ |
81
|
|
|
public function getIcon($base64Url, $credentialId) { |
82
|
|
|
$url = base64_decode(str_replace('_','/', $base64Url)); |
83
|
|
|
|
84
|
|
|
if($credentialId) { |
85
|
|
|
try { |
86
|
|
|
$credential = $this->credentialService->getCredentialById($credentialId, $this->userId); |
87
|
|
|
$credential = $credential->jsonSerialize(); |
88
|
|
|
} catch (DoesNotExistException $e){ |
|
|
|
|
89
|
|
|
// Credential is not found, continue |
90
|
|
|
$credential = false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { |
95
|
|
|
$url = "http://" . $url; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$icon = new IconService($url); |
99
|
|
|
|
100
|
|
|
$data = base64_decode("iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAABHVBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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oExists) { |
104
|
|
|
$data = $icon->icoData; |
105
|
|
|
$type = $icon->icoType; |
106
|
|
|
} |
107
|
|
|
if (isset($credential) && $credential['user_id'] == $this->userId) { |
108
|
|
|
$iconData = [ |
109
|
|
|
'type' => ($type) ? $type : 'x-icon', |
110
|
|
|
'content' => base64_encode($data) |
111
|
|
|
]; |
112
|
|
|
$credential['icon'] = json_encode($iconData); |
113
|
|
|
try { |
114
|
|
|
if($credential) { |
115
|
|
|
$this->credentialService->updateCredential($credential); |
116
|
|
|
} |
117
|
|
|
} catch (DriverException $exception) { |
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @FIXME Syntax error or access violation: 1118 Row size too large |
120
|
|
|
* This happens when favicons are quite big. |
121
|
|
|
* Githubs one is 33kb and triggers the try catch |
122
|
|
|
*/ |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
$offset = 3600 * 24 * 30; |
128
|
|
|
$contentType = 'image/png'; |
129
|
|
|
$response = new DataDownloadResponse($data, 'icon', $contentType); |
130
|
|
|
|
131
|
|
|
$response->addHeader('Content-Type', $contentType); |
132
|
|
|
$response->addHeader('Content-Length:', mb_strlen($data)); |
133
|
|
|
$response->addHeader('Expires: ', gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"); |
134
|
|
|
$response->setETag($base64Url); |
135
|
|
|
$response->cacheFor($offset); |
136
|
|
|
|
137
|
|
|
return $response; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @NoAdminRequired |
142
|
|
|
* @NoCSRFRequired |
143
|
|
|
*/ |
144
|
|
|
public function getLocalIconList() { |
145
|
|
|
$dir = $this->am->getAppPath('passman'); |
146
|
|
|
$result = Utils::getDirContents($dir . '/img/icons'); |
147
|
|
|
|
148
|
|
|
$icons = []; |
149
|
|
|
foreach ($result as $icon) { |
150
|
|
|
$iconPath = $icon; |
151
|
|
|
$path = explode('passman/', $iconPath); |
152
|
|
|
$pack = explode('/', $path[1])[2]; |
153
|
|
|
$mime = mime_content_type($iconPath); |
154
|
|
|
//print_r($path); |
155
|
|
|
if($mime !== 'directory') { |
156
|
|
|
$icon = []; |
157
|
|
|
$icon['mimetype'] = mime_content_type($iconPath); |
158
|
|
|
$icon['url'] = $this->urlGenerator->linkTo('passman', $path[1]); |
159
|
|
|
$icon['pack'] = $pack; |
160
|
|
|
if(!isset($icons[$pack])){ |
161
|
|
|
$icons[$pack] = []; |
162
|
|
|
} |
163
|
|
|
$icons[$pack][] = $icon; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return new JSONResponse($icons); |
167
|
|
|
} |
168
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.