Passed
Push — master ( 14e493...9c2d3e )
by Marcel
02:30
created

ExternalFileService::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\IL10N;
15
use OCP\ILogger;
16
17
class ExternalFileService
18
{
19
    private $logger;
20
    private $userId;
21
    private $l10n;
22
23
    public function __construct(
24
        $userId,
25
        IL10N $l10n,
26
        ILogger $logger
27
    )
28
    {
29
        $this->userId = $userId;
30
        $this->l10n = $l10n;
31
        $this->logger = $logger;
32
    }
33
34
    public function getName(): string
35
    {
36
        return $this->l10n->t('External url (csv)');
37
    }
38
39
    /**
40
     * Get the content from an external url
41
     *
42
     * @NoAdminRequired
43
     * @param array $option
44
     * @return array
45
     */
46
    public function read($option)
47
    {
48
        //$this->logger->error('dataset path: ' . $datasetMetadata['link']);
49
50
        $ch = curl_init();
51
        if ($ch !== false) {
52
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
53
            curl_setopt($ch, CURLOPT_HEADER, false);
54
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
55
            curl_setopt($ch, CURLOPT_URL, $option['link']);
56
            curl_setopt($ch, CURLOPT_REFERER, $option['link']);
57
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
58
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
59
            $curlResult = curl_exec($ch);
60
            curl_close($ch);
61
        } else {
62
            $curlResult = '';
63
        }
64
65
        $delimiter = $this->detectDelimiter($curlResult);
66
        $rows = str_getcsv($curlResult, "\n");
67
        $data = array();
68
        $header = array();
69
        $headerrow = 0;
70
71
        foreach ($rows as &$row) {
72
            $row = str_getcsv($row, $delimiter);
73
            if ($headerrow === 0) {
74
                $header = $row;
75
                $headerrow = 1;
76
            } else {
77
                array_push($data, $row);
78
            }
79
        }
80
        $result = [
81
            'header' => $header,
82
            'data' => $data
83
        ];
84
        return $result;
85
    }
86
87
    /**
88
     * template for options & settings
89
     *
90
     * @NoAdminRequired
91
     * @return array
92
     */
93
    public function getTemplate()
94
    {
95
        $template = array();
96
        array_push($template, ['id' => 'link', 'name' => 'External URL', 'placeholder' => 'url']);
97
        array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
98
        return $template;
99
    }
100
101
    private function detectDelimiter($data)
102
    {
103
        $delimiters = ["\t", ";", "|", ","];
104
        $data_2 = array();
105
        $delimiter = $delimiters[0];
106
        foreach ($delimiters as $d) {
107
            $firstRow = str_getcsv($data, "\n")[0];
108
            $data_1 = str_getcsv($firstRow, $d);
109
            if (sizeof($data_1) > sizeof($data_2)) {
110
                $delimiter = $d;
111
                $data_2 = $data_1;
112
            }
113
        }
114
        return $delimiter;
115
    }
116
}