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

classes/DomainMOD/Fabulous.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/Fabulous.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 Fabulous
25
{
26
27 View Code Duplication
    public function getApiUrl($account_username, $account_password, $domain, $command)
28
    {
29
        $base_url = 'https://api.fabulous.com/';
30
        if ($command == 'domainlist') {
31
            return $base_url . 'listDomains?username=' . $account_username . '&password=' . $account_password;
32
        } elseif ($command == 'info') {
33
            return $base_url . 'domainInfo?username=' . $account_username . '&password=' . $account_password . '&domain=' . $domain;
34
        } else {
35
            return 'Unable to build API URL';
36
        }
37
    }
38
39 View Code Duplication
    public function apiCall($full_url)
40
    {
41
        $handle = curl_init($full_url);
42
        curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
43
        $result = curl_exec($handle);
44
        curl_close($handle);
45
        return $result;
46
    }
47
48 View Code Duplication
    public function getDomainList($account_username, $account_password)
49
    {
50
        $api_url = $this->getApiUrl($account_username, $account_password, '', 'domainlist');
51
        $api_results = $this->apiCall($api_url);
52
        $array_results = $this->convertToArray($api_results);
53
54
        // confirm that the api call was successful
55
        if ($array_results[0]['response']['reason'] == 'Success') {
56
57
            $domain_list = array();
58
            $domain_count = 0;
59
60
            foreach ($array_results[0]['response']['results']['result']['values']['value'] as $domain) {
61
62
                $domain_list[] = $domain;
63
                $domain_count++;
64
65
            }
66
67
        } else {
68
69
            // if the API call failed assign empty values
70
            $domain_list = '';
71
            $domain_count = '';
72
73
        }
74
75
        return array($domain_count, $domain_list);
76
    }
77
78
    public function getFullInfo($account_username, $account_password, $domain)
79
    {
80
        $api_url = $this->getApiUrl($account_username, $account_password, $domain, 'info');
81
        $api_results = $this->apiCall($api_url);
82
        $array_results = $this->convertToArray($api_results);
83
84
        // confirm that the api call was successful
85
        if ($array_results[0]['response']['reason'] == 'Success') {
86
87
            // get expiration date
88
            $expiration_date = $array_results[0]['response']['results']['result']['expiry'];
89
90
            // get dns servers
91
            $dns_result = $array_results[0]['response']['results']['result']['nameservers']['nameserver'];
92
            $dns_servers = $this->processDns($dns_result);
93
94
            // get privacy status
95
            $privacy_status = $array_results[0]['response']['results']['result']['whoisprivacyenabled'];
96
97
            // get auto renewal status
98
            $autorenewal_status = $array_results[0]['response']['results']['result']['autorenewstatus'];
99
100
        } else {
101
102
            // if the API call failed assign empty values
103
            $expiration_date = '';
104
            $dns_servers = '';
105
            $privacy_status = '';
106
            $autorenewal_status = '';
107
108
        }
109
110
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
111
    }
112
113
    public function convertToArray($api_result)
114
    {
115
        $xml = simplexml_load_string($api_result);
116
        $json = json_encode((array($xml)), true);
117
        return json_decode($json, true);
118
    }
119
120 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...
121
    {
122
        $dns_servers = array();
123
        if (!empty($dns_result)) {
124
            $dns_servers = array_filter($dns_result);
125
        } else {
126
            $dns_servers[0] = 'no.dns-servers.1';
127
            $dns_servers[1] = 'no.dns-servers.2';
128
        }
129
        return $dns_servers;
130
    }
131
132
} //@formatter:on
133