Completed
Push — dev ( cd7c3e...c5c07e )
by Greg
02:17
created

Namecheap::getDomainList()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.867
c 0
b 0
f 0
cc 5
nc 5
nop 3
1
<?php
2
/**
3
 * /classes/DomainMOD/Namecheap.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 Namecheap
25
{
26
    public $format;
27
    public $log;
28
29
    public function __construct()
30
    {
31
        $this->format = new Format();
32
        $this->log = new Log('class.namecheap');
33
    }
34
35
    public function getApiUrl($api_key, $command, $domain, $account_username, $api_ip_address, $page_number)
36
    {
37
        $base_url = 'https://api.namecheap.com/xml.response?&ApiUser=' . $account_username . '&ApiKey=' . $api_key . '&UserName=' . $account_username;
38
        if ($command == 'domainlist') {
39
            return $base_url . '&Command=namecheap.domains.getList&ClientIp=' . $api_ip_address . '&PageSize=100' . '&Page=' . $page_number;
40
        } elseif ($command == 'info') {
41
            return $base_url . '&Command=namecheap.domains.getinfo&ClientIp=' . $api_ip_address . '&DomainName=' . $domain;
42
        } elseif ($command == 'autorenewal') {
43
            return $base_url . '&Command=namecheap.domains.getList&SearchTerm=' . $domain . '&ClientIp=' . $api_ip_address;
44
        } else {
45
            return 'Unable to build API URL';
46
        }
47
    }
48
49 View Code Duplication
    public function apiCall($full_url)
50
    {
51
        $handle = curl_init($full_url);
52
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
53
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
54
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
55
        $result = curl_exec($handle);
56
        curl_close($handle);
57
        return $result;
58
    }
59
60
    public function getDomainList($account_username, $api_key, $api_ip_address)
61
    {
62
        $domain_list = array();
63
        $domain_count = 0;
64
        $page_count = 1;
65
        $error = 0;
66
67
        while ($error == 0) {
68
69
            $api_url = $this->getApiUrl($api_key, 'domainlist', '', $account_username, $api_ip_address, $page_count);
70
            $api_results = $this->apiCall($api_url);
71
72
            if (strlen($api_results) > 600) {
73
74
                $array_results = $this->convertToArray($api_results);
75
76
                // confirm that the api call was successful
77
                if ($array_results[0]['@attributes']['Status'] == "OK") {
78
79
                    foreach ($array_results[0]['CommandResponse']['DomainGetListResult']['Domain'] as $domain) {
80
81
                        $domain_list[] = $domain['@attributes']['Name'];
82
                        $domain_count++;
83
84
                    }
85
86
                } else {
87
88
                    $log_message = 'Unable to get domain list';
89
                    $log_extra = array('Username' => $account_username, 'API Key' => $this->format->obfusc($api_key), 'IP Address' => $api_ip_address);
90
                    $this->log->error($log_message, $log_extra);
91
92
                }
93
94
                $page_count++;
95
96
            } else {
97
98
                $error = 1;
99
100
            }
101
102
        }
103
104
        return array($domain_count, $domain_list);
105
    }
106
107
    public function getFullInfo($account_username, $api_key, $api_ip_address, $domain)
108
    {
109
        $expiration_date = '';
110
        $dns_servers = array();
111
        $privacy_status = '';
112
        $autorenewal_status = '';
113
114
        $api_url = $this->getApiUrl($api_key, 'info', $domain, $account_username, $api_ip_address, '');
115
        $api_results = $this->apiCall($api_url);
116
        $array_results = $this->convertToArray($api_results);
117
118
        // confirm that the api call was successful
119
        if ($array_results[0]['@attributes']['Status'] == "OK") {
120
121
            // get expiration date
122
            $expiration_date = date('Y-m-d', strtotime($array_results[0]['CommandResponse']['DomainGetInfoResult']['DomainDetails']['ExpiredDate']));
123
124
            // get dns servers
125
            $dns_result = $array_results[0]['CommandResponse']['DomainGetInfoResult']['DnsDetails']['Nameserver'];
126
            $dns_servers = $this->processDns($dns_result);
127
128
            // get privacy status
129
            $privacy_result = $array_results[0]['CommandResponse']['DomainGetInfoResult']['Whoisguard']['@attributes']['Enabled'];
130
            $privacy_status = $this->processPrivacy($privacy_result);
131
132
        } else {
133
134
            $log_message = 'Unable to get partial domain details';
135
            $log_extra = array('Domain' => $domain, 'Username' => $account_username, 'API Key' => $this->format->obfusc($api_key), 'IP Address' => $api_ip_address);
136
            $this->log->error($log_message, $log_extra);
137
138
        }
139
140
        // since the auto renewal status can only be retrieved through Namecheap's getList command, I have to re-run
141
        // a new command just for this
142
        $api_url = $this->getApiUrl($api_key, 'autorenewal', $domain, $account_username, $api_ip_address, '');
143
        $api_results = $this->apiCall($api_url);
144
        $array_results = $this->convertToArray($api_results);
145
146
        // confirm that the api call was successful
147
        if ($array_results[0]['@attributes']['Status'] == "OK") {
148
149
            foreach ($array_results[0]['CommandResponse']['DomainGetListResult']['Domain'] as $value) {
150
151
                if ($value['Name'] == $domain) {
152
153
                    $autorenewal_result = $value['AutoRenew'];
154
155
                }
156
157
            }
158
159
            $autorenewal_status = $this->processAutorenew($autorenewal_result);
160
161
        } else {
162
163
            $log_message = 'Unable to get auto renewal status';
164
            $log_extra = array('Domain' => $domain, 'Username' => $account_username, 'API Key' => $this->format->obfusc($api_key), 'IP Address' => $api_ip_address);
165
            $this->log->error($log_message, $log_extra);
166
167
        }
168
169
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
170
    }
171
               
172
    public function convertToArray($api_result)
173
    {
174
        $xml = simplexml_load_string($api_result);
175
        $json = json_encode((array($xml)), true);
176
        return json_decode($json, true);
177
    }
178
179 View Code Duplication
    public function processDns($dns_result)
180
    {
181
        $dns_servers = array();
182
        if (!empty($dns_result)) {
183
            $dns_servers = array_filter($dns_result);
184
        } else {
185
            $dns_servers[0] = 'no.dns-servers.1';
186
            $dns_servers[1] = 'no.dns-servers.2';
187
        }
188
        return $dns_servers;
189
    }
190
                    
191
    public function processPrivacy($privacy_result)
192
    {
193
        if ($privacy_result == 'True') {
194
            $privacy_status = '1';
195
        } else {
196
            $privacy_status = '0';
197
        }
198
        return $privacy_status;
199
    }
200
                    
201
    public function processAutorenew($autorenewal_result)
202
    {
203
        if ($autorenewal_result == 'true') {
204
            $autorenewal_status = '1';
205
        } else {
206
            $autorenewal_status = '0';
207
        }
208
        return $autorenewal_status;
209
    }
210
211
} //@formatter:on
212