Completed
Branch dev (623cfe)
by Greg
03:58
created

classes/DomainMOD/DnSimple.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * /classes/DomainMOD/DnSimple.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 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 DnSimple
25
{
26
    public $format;
27
    public $log;
28
29
    public function __construct()
30
    {
31
        $this->format = new Format();
32
        $this->log = new Log('dnsimple.class');
33
    }
34
35
    public function getApiUrl($account_id, $command, $domain)
36
    {
37
        $base_url = 'https://api.dnsimple.com/v2/';
38 View Code Duplication
        if ($command == 'accountid') {
39
            return $base_url . 'whoami';
40
        } elseif ($command == 'domainlist') {
41
            return $base_url . $account_id . '/domains';
42
        } elseif ($command == 'info') {
43
            return $base_url . $account_id . '/domains/' . $domain;
44
        } elseif ($command == 'dns') {
45
            return $base_url . $account_id . '/registrar/domains/' . $domain . '/delegation';
46
        } else {
47
            return 'Unable to build API URL';
48
        }
49
    }
50
51 View Code Duplication
    public function apiCall($api_key, $full_url)
52
    {
53
        $handle = curl_init($full_url);
54
        curl_setopt($handle, CURLOPT_HTTPHEADER, array(
55
            'Authorization: Bearer ' . $api_key,
56
            'Accept: application/json'));
57
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
58
        $result = curl_exec($handle);
59
        curl_close($handle);
60
        return $result;
61
    }
62
63
    public function getAccountId($api_key)
64
    {
65
        $api_url = $this->getApiUrl('', 'accountid', '');
66
        $api_results = $this->apiCall($api_key, $api_url);
67
        $array_results = $this->convertToArray($api_results);
68
69
        return $array_results['data']['account']['id'];
70
    }
71
72
    public function getDomainList($api_key, $account_id)
73
    {
74
        $domain_list = array();
75
        $domain_count = 0;
76
77
        $api_url = $this->getApiUrl($account_id, 'domainlist', '');
78
        $api_results = $this->apiCall($api_key, $api_url);
79
        $array_results = $this->convertToArray($api_results);
80
81
        // confirm that the api call was successful
82
        if (isset($array_results['data'][0]['name'])) {
83
84
            foreach ($array_results['data'] as $domain) {
85
86
                $domain_list[] = $domain['name'];
87
                $domain_count++;
88
89
            }
90
91
        } else {
92
93
            $log_message = 'Unable to get domain list';
94
            $log_extra = array('API Key' => $this->format->obfusc($api_key), 'Account ID' => $account_id);
95
            $this->log->error($log_message, $log_extra);
96
97
        }
98
99
        return array($domain_count, $domain_list);
100
    }
101
102
    public function getFullInfo($api_key, $account_id, $domain)
103
    {
104
        $expiration_date = '';
105
        $dns_servers = array();
106
        $privacy_status = '';
107
        $autorenewal_status = '';
108
109
        $api_url = $this->getApiUrl($account_id, 'info', $domain);
110
        $api_results = $this->apiCall($api_key, $api_url);
111
        $array_results = $this->convertToArray($api_results);
112
113
        // confirm that the api call was successful
114
        if (isset($array_results['data']['name'])) {
115
116
            // get expiration date
117
            $expiration_date = $array_results['data']['expires_on'];
118
119
            // get dns servers
120
            $api_url = $this->getApiUrl($account_id, 'dns', $domain);
121
            $api_results = $this->apiCall($api_key, $api_url);
122
            $array_results = $this->convertToArray($api_results);
123
            $dns_servers = $this->processDns($array_results['data']);
124
125
            // get privacy status
126
            $privacy_result = $array_results['data']['private_whois'];
127
            $privacy_status = $this->processPrivacy($privacy_result);
128
129
            // get auto renewal status
130
            $autorenewal_result = $array_results['data']['auto_renew'];
131
            $autorenewal_status = $this->processAutorenew($autorenewal_result);
132
133
        } else {
134
135
            $log_message = 'Unable to get domain details';
136
            $log_extra = array('Domain' => $domain, 'API Key' => $this->format->obfusc($api_key), 'Account ID' => $account_id);
137
            $this->log->error($log_message, $log_extra);
138
139
        }
140
141
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
142
    }
143
144
    public function convertToArray($api_result)
145
    {
146
        return json_decode($api_result, true);
147
    }
148
149 View Code Duplication
    public function processDns($dns_result)
150
    {
151
        $dns_servers = array();
152
        if (!empty($dns_result)) {
153
            $dns_servers = array_filter($dns_result);
154
        } else {
155
            $dns_servers[0] = 'no.dns-servers.1';
156
            $dns_servers[1] = 'no.dns-servers.2';
157
        }
158
        return $dns_servers;
159
    }
160
161
    public function processPrivacy($privacy_result)
162
    {
163
        if ($privacy_result == 'true') {
164
            $privacy_status = '1';
165
        } else {
166
            $privacy_status = '0';
167
        }
168
        return $privacy_status;
169
    }
170
171
    public function processAutorenew($autorenewal_result)
1 ignored issue
show
Coding Style Naming introduced by
The parameter $autorenewal_result is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
172
    {
173
        if ($autorenewal_result == 'true') {
174
            $autorenewal_status = '1';
175
        } else {
176
            $autorenewal_status = '0';
177
        }
178
        return $autorenewal_status;
179
    }
180
181
} //@formatter:on
182