Completed
Push — master ( 23b6b6...825bd8 )
by Greg
04:51
created

classes/DomainMOD/GoDaddy.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/GoDaddy.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 GoDaddy
25
{
26
27
    public function getApiUrl($domain, $command)
28
    {
29
        $base_url = 'https://api.godaddy.com/v1/';
30
        if ($command == 'domainlist') {
31
            return $base_url . 'domains?statusGroups=VISIBLE&limit=10000';
32
        } elseif ($command == 'info') {
33
            return $base_url . 'domains/' . $domain;
34
        } else {
35
            return 'Unable to build API URL';
36
        }
37
    }
38
39 View Code Duplication
    public function apiCall($api_key, $api_secret, $full_url)
40
    {
41
        $handle = curl_init($full_url);
42
        curl_setopt($handle, CURLOPT_HTTPHEADER, array(
43
            'Authorization: sso-key ' . $api_key . ':' . $api_secret,
44
            'Accept: application/json'));
45
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
46
        $result = curl_exec($handle);
47
        curl_close($handle);
48
        return $result;
49
    }
50
51 View Code Duplication
    public function getDomainList($api_key, $api_secret)
52
    {
53
        $api_url = $this->getApiUrl('', 'domainlist');
54
        $api_results = $this->apiCall($api_key, $api_secret, $api_url);
55
        $array_results = $this->convertToArray($api_results);
56
57
        // confirm that the api call was successful
58
        if (isset($array_results[0]['domain'])) {
59
60
            $domain_list = array();
61
            $domain_count = 0;
62
63
            foreach ($array_results as $domain) {
64
65
                $domain_list[] = $domain['domain'];
66
                $domain_count++;
67
68
            }
69
70
        } else {
71
72
            // if the API call failed assign empty values
73
            $domain_list = '';
74
            $domain_count = '';
75
76
        }
77
78
        return array($domain_count, $domain_list);
79
    }
80
81
    public function getFullInfo($api_key, $api_secret, $domain)
82
    {
83
        $api_url = $this->getApiUrl($domain, 'info');
84
        $api_results = $this->apiCall($api_key, $api_secret, $api_url);
85
        $array_results = $this->convertToArray($api_results);
86
87
        // confirm that the api call was successful
88
        if (isset($array_results['domain'])) {
89
90
            // get expiration date
91
            $expiration_date = substr($array_results['expires'], 0, 10);
92
93
            // get dns servers
94
            $dns_result = $array_results['nameServers'];
95
            $dns_servers = $this->processDns($dns_result);
96
97
            // get privacy status
98
            $privacy_result = (string) $array_results['privacy'];
99
            $privacy_status = $this->processPrivacy($privacy_result);
100
101
            // get auto renewal status
102
            $autorenewal_result = (string) $array_results['renewAuto'];
103
            $autorenewal_status = $this->processAutorenew($autorenewal_result);
104
105
        } else {
106
107
            // if the API call failed assign empty values
108
            $expiration_date = '';
109
            $dns_servers = '';
110
            $privacy_status = '';
111
            $autorenewal_status = '';
112
113
        }
114
115
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
116
    }
117
118
    public function convertToArray($api_result)
119
    {
120
        return json_decode($api_result, true);
121
    }
122
123 View Code Duplication
    public function processDns($dns_result)
1 ignored issue
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $dns_servers = array();
126
        if (!empty($dns_result)) {
127
            $dns_servers = array_filter($dns_result);
128
        } else {
129
            $dns_servers[0] = 'no.dns-servers.1';
130
            $dns_servers[1] = 'no.dns-servers.2';
131
        }
132
        return $dns_servers;
133
    }
134
135
    public function processPrivacy($privacy_result)
136
    {
137
        if ($privacy_result == '') {
138
            $privacy_status = '0';
139
        } else {
140
            $privacy_status = '1';
141
        }
142
        return $privacy_status;
143
    }
144
145
    public function processAutorenew($autorenewal_result)
146
    {
147
        if ($autorenewal_result == '') {
148
            $autorenewal_status = '0';
149
        } else {
150
            $autorenewal_status = '1';
151
        }
152
        return $autorenewal_status;
153
    }
154
155
} //@formatter:on
156