1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2021 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
13
|
|
|
|
14
|
|
|
use OCP\IL10N; |
15
|
|
|
use OCP\ILogger; |
16
|
|
|
|
17
|
|
|
class Json implements IDatasource |
18
|
|
|
{ |
19
|
|
|
private $logger; |
20
|
|
|
private $l10n; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
IL10N $l10n, |
24
|
|
|
ILogger $logger |
25
|
|
|
) |
26
|
|
|
{ |
27
|
|
|
$this->l10n = $l10n; |
28
|
|
|
$this->logger = $logger; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string Display Name of the datasource |
33
|
|
|
*/ |
34
|
|
|
public function getName(): string |
35
|
|
|
{ |
36
|
|
|
return $this->l10n->t('JSON'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int digit unique datasource id |
41
|
|
|
*/ |
42
|
|
|
public function getId(): int |
43
|
|
|
{ |
44
|
|
|
return 6; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array available options of the datasoure |
49
|
|
|
*/ |
50
|
|
|
public function getTemplate(): array |
51
|
|
|
{ |
52
|
|
|
$template = array(); |
53
|
|
|
array_push($template, ['id' => 'url', 'name' => 'JSON Url', 'placeholder' => 'url']); |
54
|
|
|
array_push($template, ['id' => 'auth', 'name' => $this->l10n->t('Authentication'), 'placeholder' => 'User:Password']); |
55
|
|
|
array_push($template, ['id' => 'path', 'name' => $this->l10n->t('JSON path'), 'placeholder' => 'x/y/z']); |
56
|
|
|
array_push($template, ['id' => 'timestamp', 'name' => $this->l10n->t('Timestamp of dataload'), 'placeholder' => 'false/true', 'type' => 'tf']); |
57
|
|
|
return $template; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Read the Data |
62
|
|
|
* @param $option |
63
|
|
|
* @return array available options of the datasoure |
64
|
|
|
*/ |
65
|
|
|
public function readData($option): array |
66
|
|
|
{ |
67
|
|
|
$string = $option['url']; |
68
|
|
|
$path = $option['path']; |
69
|
|
|
$auth = $option['auth']; |
70
|
|
|
$data = array(); |
71
|
|
|
|
72
|
|
|
$ch = curl_init(); |
73
|
|
|
if ($ch !== false) { |
74
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
75
|
|
|
curl_setopt($ch, CURLOPT_URL, $string); |
76
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
77
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
78
|
|
|
'OCS-APIRequest: true' |
79
|
|
|
)); |
80
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $auth); |
81
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true); |
82
|
|
|
$curlResult = curl_exec($ch); |
83
|
|
|
curl_close($ch); |
84
|
|
|
} else { |
85
|
|
|
$curlResult = ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$json = json_decode($curlResult, true); |
|
|
|
|
89
|
|
|
$paths = explode(',', $path); |
90
|
|
|
|
91
|
|
|
foreach ($paths as $singlePath) { |
92
|
|
|
$array = $this->get_nested_array_value($json, $singlePath); |
93
|
|
|
|
94
|
|
|
if (is_array($array)) { |
95
|
|
|
foreach ($array as $key => $value) { |
96
|
|
|
$pathArray = explode('/', $singlePath); |
97
|
|
|
$group = end($pathArray); |
98
|
|
|
array_push($data, [$group, $key, $value]); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
$pathArray = explode('/', $singlePath); |
102
|
|
|
$key = end($pathArray); |
103
|
|
|
array_push($data, ['', $key, $array]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$header = array(); |
108
|
|
|
$header[0] = ''; |
109
|
|
|
$header[1] = 'Key'; |
110
|
|
|
$header[2] = 'Value'; |
111
|
|
|
|
112
|
|
|
return [ |
113
|
|
|
'header' => $header, |
114
|
|
|
'dimensions' => array_slice($header, 0, count($header) - 1), |
115
|
|
|
'data' => $data, |
116
|
|
|
'error' => 0, |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* get array object from string |
122
|
|
|
* |
123
|
|
|
* @NoAdminRequired |
124
|
|
|
* @param $array |
125
|
|
|
* @param $path |
126
|
|
|
* @param string $delimiter |
127
|
|
|
* @return array|string |
128
|
|
|
*/ |
129
|
|
|
private function get_nested_array_value(&$array, $path, $delimiter = '/') |
130
|
|
|
{ |
131
|
|
|
$pathParts = explode($delimiter, $path); |
132
|
|
|
|
133
|
|
|
$current = &$array; |
134
|
|
|
foreach ($pathParts as $key) { |
135
|
|
|
$current = &$current[$key]; |
136
|
|
|
} |
137
|
|
|
return $current; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |