Completed
Push — dev ( c5c07e...787ff4 )
by Greg
03:32
created

Gandi::getApiUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * /classes/DomainMOD/Gandi.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2020 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class Gandi
25
{
26
    public $format;
27
    public $log;
28
29
    public function __construct()
30
    {
31
        $this->format = new Format();
32
        $this->log = new Log('class.gandi');
33
    }
34
35 View Code Duplication
    public function getApiUrl($command, $domain)
36
    {
37
        $base_url = 'https://api.gandi.net/v5/domain/';
38
        if ($command == 'domainlist') {
39
            return $base_url . 'domains';
40
        } elseif ($command == 'info') {
41
            return $base_url . 'domains/' . $domain;
42
        } else {
43
            return 'Unable to build API URL';
44
        }
45
    }
46
47
    public function apiCall($full_url, $api_key)
48
    {
49
            $handle = curl_init($full_url);
50
            curl_setopt($handle, CURLOPT_ENCODING, '');
51
            curl_setopt($handle, CURLOPT_MAXREDIRS, 10);
52
            curl_setopt($handle, CURLOPT_TIMEOUT, 30);
53
            curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
54
            curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'GET');
55
            curl_setopt($handle, CURLOPT_HTTPHEADER, array(
56
                'authorization: Apikey ' . $api_key));
57
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
58
            $result = curl_exec($handle);
59
            curl_close($handle);
60
61
        return $result;
62
    }
63
64
    public function getDomainList($api_key)
65
    {
66
        $domain_list = array();
67
        $domain_count = 0;
68
69
        $api_url = $this->getApiUrl('domainlist', '');
70
        $api_results = $this->apiCall($api_url, $api_key);
71
        $array_results = $this->convertToArray($api_results);
72
73
        // confirm that the api call was successful
74
        if (isset($array_results[0]['fqdn'])) {
75
76
            $domain_list = array();
77
            $domain_count = 0;
78
79
            foreach ($array_results as $result) {
80
81
                $domain_list[] = $result['fqdn'];
82
                $domain_count++;
83
84
            }
85
86
        } else {
87
88
            $log_message = 'Unable to get domain list';
89
            $log_extra = array('API Key' => $this->format->obfusc($api_key));
90
            $this->log->error($log_message, $log_extra);
91
92
        }
93
94
        return array($domain_count, $domain_list);
95
    }
96
97
    public function getFullInfo($api_key, $domain)
98
    {
99
        $expiration_date = '';
100
        $dns_servers = array();
101
        $privacy_status = '';
102
        $autorenewal_status = '';
103
104
        $api_url = $this->getApiUrl('info', $domain);
105
        $api_results = $this->apiCall($api_url, $api_key);
106
        $array_results = $this->convertToArray($api_results);
107
108
        // confirm that the api call was successful
109
        if (isset($array_results['dates']['registry_ends_at'])) {
110
111
            // get expiration date
112
            $expiration_date = substr($array_results['dates']['registry_ends_at'], 0, 10);
113
114
            // Gandi doesn't return the privacy status, so all domains get zero to public by default
115
            $privacy_status = '0';
116
117
            // get auto renewal status
118
            $autorenewal_result = (string) $array_results['autorenew']['enabled'];
119
            $autorenewal_status = $this->processAutorenew($autorenewal_result);
120
121
            // get dns servers
122
            $dns_result = $array_results['nameservers'];
123
            $dns_servers = $this->processDns($dns_result);
124
125
        } else {
126
127
            $log_message = 'Unable to get domain details';
128
            $log_extra = array('Domain' => $domain);
129
            $this->log->error($log_message, $log_extra);
130
131
        }
132
133
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
134
    }
135
136
    public function convertToArray($api_result)
137
    {
138
        return json_decode($api_result, true);
139
    }
140
141 View Code Duplication
    public function processDns($dns_result)
142
    {
143
        $dns_servers = array();
144
        if (!empty($dns_result)) {
145
            $dns_servers = array_filter($dns_result);
146
        } else {
147
            $dns_servers[0] = 'no.dns-servers.1';
148
            $dns_servers[1] = 'no.dns-servers.2';
149
        }
150
        return $dns_servers;
151
    }
152
153
    public function processAutorenew($autorenewal_result)
154
    {
155
        if ($autorenewal_result == '1') {
156
            $autorenewal_status = '1';
157
        } else {
158
            $autorenewal_status = '0';
159
        }
160
        return $autorenewal_status;
161
    }
162
163
} //@formatter:on
164