|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Data 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 2020 Marcel Scherello |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
|
13
|
|
|
|
|
14
|
|
|
use OCP\ILogger; |
|
15
|
|
|
|
|
16
|
|
|
class JsonService |
|
17
|
|
|
{ |
|
18
|
|
|
private $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
ILogger $logger |
|
22
|
|
|
) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->logger = $logger; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get the items for the selected category |
|
29
|
|
|
* |
|
30
|
|
|
* @NoAdminRequired |
|
31
|
|
|
* @param array $option |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function read($option) |
|
35
|
|
|
{ |
|
36
|
|
|
$string = $option['url']; |
|
37
|
|
|
$path = $option['path']; |
|
38
|
|
|
$auth = $option['auth']; |
|
39
|
|
|
$data = array(); |
|
40
|
|
|
|
|
41
|
|
|
$ch = curl_init(); |
|
42
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
|
|
|
|
|
43
|
|
|
curl_setopt($ch, CURLOPT_URL, $string); |
|
44
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
|
45
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
46
|
|
|
'OCS-APIRequest: true' |
|
47
|
|
|
)); |
|
48
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $auth); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true); |
|
50
|
|
|
$result = curl_exec($ch); |
|
|
|
|
|
|
51
|
|
|
curl_close($ch); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$json = json_decode($result, true); |
|
54
|
|
|
$array = $this->get_nested_array_value($json, $path); |
|
55
|
|
|
|
|
56
|
|
|
if (is_array($array)) { |
|
|
|
|
|
|
57
|
|
|
foreach ($array as $key => $value) { |
|
58
|
|
|
array_push($data, ['dimension1' => '', 'dimension2' => $key, 'dimension3' => $value]); |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
$key = end(explode('/', $path)); |
|
62
|
|
|
array_push($data, ['dimension1' => '', 'dimension2' => $key, 'dimension3' => $array]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$header = array(); |
|
66
|
|
|
$header['dimension1'] = ''; |
|
67
|
|
|
$header['dimension2'] = 'Key'; |
|
68
|
|
|
$header['dimension3'] = 'Value'; |
|
69
|
|
|
|
|
70
|
|
|
$result = [ |
|
71
|
|
|
'header' => $header, |
|
72
|
|
|
'data' => $data, |
|
73
|
|
|
'error' => 0, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* get array object from string |
|
81
|
|
|
* |
|
82
|
|
|
* @NoAdminRequired |
|
83
|
|
|
* @param $array |
|
84
|
|
|
* @param $path |
|
85
|
|
|
* @param string $delimiter |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
private function get_nested_array_value(&$array, $path, $delimiter = '/') |
|
89
|
|
|
{ |
|
90
|
|
|
$pathParts = explode($delimiter, $path); |
|
91
|
|
|
|
|
92
|
|
|
$current = &$array; |
|
93
|
|
|
foreach ($pathParts as $key) { |
|
94
|
|
|
$current = &$current[$key]; |
|
95
|
|
|
} |
|
96
|
|
|
return $current; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* template for options & settings |
|
101
|
|
|
* |
|
102
|
|
|
* @NoAdminRequired |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getTemplate() |
|
106
|
|
|
{ |
|
107
|
|
|
$template = array(); |
|
108
|
|
|
array_push($template, ['id' => 'url', 'name' => 'JSON Url', 'placeholder' => 'url']); |
|
109
|
|
|
array_push($template, ['id' => 'auth', 'name' => 'Authentication', 'placeholder' => 'User:Password']); |
|
110
|
|
|
array_push($template, ['id' => 'path', 'name' => 'JSON path', 'placeholder' => 'x/y/z']); |
|
111
|
|
|
array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']); |
|
112
|
|
|
return $template; |
|
113
|
|
|
} |
|
114
|
|
|
} |