forked from MISP/MISP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrgImgHelper.php
138 lines (125 loc) · 4.43 KB
/
OrgImgHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
App::uses('AppHelper', 'View/Helper');
App::uses('FileAccessTool', 'Lib/Tools');
// Helper to retrieve org images with the given parameters
class OrgImgHelper extends AppHelper
{
const IMG_PATH = APP . 'files' . DS . 'img' . DS . 'orgs' . DS;
/** @var array */
private $imageCache = [];
public function getNameWithImg(array $organisation, $link = null)
{
if (!isset($organisation['Organisation'])) {
return '';
}
$orgImgName = $this->findOrgImage($organisation['Organisation']);
$baseurl = $this->_View->viewVars['baseurl'];
if (!$link) {
$link = $baseurl . '/organisations/view/' . (empty($organisation['Organisation']['id']) ? h($organisation['Organisation']['name']) : h($organisation['Organisation']['id']));
}
if ($orgImgName) {
$base64 = $this->_View->Image->base64(self::IMG_PATH . $orgImgName);
return sprintf('<a href="%s" style="background-image: url(\'%s\')" class="orgImg">%s</a>', $link, $base64, h($organisation['Organisation']['name']));
} else {
return sprintf('<a href="%s">%s</a>', $link, h($organisation['Organisation']['name']));
}
}
/**
* @param array $organisation
* @param int $size
* @param bool $withLink
* @return string
*/
public function getOrgLogo(array $organisation, $size, $withLink = true)
{
if (isset($organisation['Organisation'])) {
$options = $organisation['Organisation'];
} else {
$options = $organisation;
}
$options['size'] = $size;
return $this->getOrgImg($options, true, !$withLink);
}
/**
* @param array $organisation
* @return string|null
*/
public function getOrgLogoAsBase64(array $organisation)
{
$orgImgName = $this->findOrgImage($organisation);
if ($orgImgName) {
return $this->_View->Image->base64(self::IMG_PATH . $orgImgName);
}
return null;
}
/**
* @deprecated
*/
public function getOrgImg($options, $returnData = false, $raw = false)
{
$orgImgName = $this->findOrgImage($options);
$baseurl = $this->_View->viewVars['baseurl'];
if ($orgImgName) {
$size = !empty($options['size']) ? $options['size'] : 48;
$result = sprintf(
'<img src="%s" title="%s" width="%s" height="%s">',
$this->_View->Image->base64(self::IMG_PATH . $orgImgName),
isset($options['name']) ? h($options['name']) : h($options['id']),
(int)$size,
(int)$size
);
if (!$raw && !empty($options['id'])) {
$result = sprintf(
'<a href="%s/organisations/view/%s">%s</a>',
$baseurl,
empty($options['id']) ? h($options['name']) : h($options['id']),
$result
);
}
} else {
if ($raw) {
$result = sprintf(
'<span class="welcome">%s</span>',
h($options['name'])
);
} else {
$result = sprintf(
'<a href="%s/organisations/view/%s"><span class="welcome">%s</span></a>',
$baseurl,
empty($options['id']) ? h($options['name']) : h($options['id']),
h($options['name'])
);
}
}
if ($returnData) {
return $result;
} else {
echo $result;
}
}
/**
* @param array $options
* @return string|null
*/
private function findOrgImage(array $options)
{
if (isset($options['id']) && array_key_exists($options['id'], $this->imageCache)) {
return $this->imageCache[$options['id']];
}
$image = null;
foreach (['id', 'name', 'uuid'] as $field) {
if (isset($options[$field])) {
foreach (['png', 'svg'] as $extensions) {
if (file_exists(self::IMG_PATH . $options[$field] . '.' . $extensions)) {
$image = $options[$field] . '.' . $extensions;
break 2;
}
}
}
}
if (isset($options['id'])) {
$this->imageCache[$options['id']] = $image;
}
return $image;
}
}