Completed
Push — master ( 087ca5...964ba9 )
by Greg
04:32
created

DreamHost::getApiUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * /classes/DomainMOD/DreamHost.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 DreamHost
25
{
26
27
    public function getApiKey($connection, $account_id)
28
    {
29
        $error = new Error();
30
        $sql = "SELECT api_key
31
                FROM registrar_accounts
32
                WHERE id = '" . $account_id . "'
33
                LIMIT 1";
34
        $result = mysqli_query($connection, $sql) or $error->outputOldSqlError($connection);
35
36
        if (mysqli_num_rows($result) > 0) {
37
38
            while ($row = mysqli_fetch_object($result)) {
39
40
                $api_key = $row->api_key;
41
42
            }
43
44
        } else {
45
46
            echo "No API Credentials Found";
47
            exit;
48
49
        }
50
51
        return $api_key;
52
    }
53
54
    public function getApiUrl($api_key, $command)
55
    {
56
        $base_url = 'https://api.dreamhost.com/?key=' . $api_key;
57
        if ($command == 'domainlist') {
58
            return $base_url . '&cmd=domain-list_registrations&format=json';
59
        } else {
60
            return 'Unable to build API URL';
61
        }
62
    }
63
64
    public function apiCall($full_url)
65
    {
66
        $handle = curl_init($full_url);
67
        curl_setopt( $handle, CURLOPT_RETURNTRANSFER, TRUE );
68
        $result = curl_exec($handle);
69
        curl_close($handle);
70
        return $result;
71
    }
72
73
    public function getDomainList($connection, $api_key, $account_id)
74
    {
75
        $error = new Error();
76
        $api_url = $this->getApiUrl($api_key, 'domainlist');
77
        $api_results = $this->apiCall($api_url);
78
        $array_results = $this->convertToArray($api_results);
79
80
        // confirm that the api call was successful
81
        if ($array_results['result'] == 'success') {
82
83
            $domain_list = array();
84
            $domain_count = 0;
85
86
            foreach ($array_results['data'] AS $value) {
87
88
                $domain_list[] = $value['domain'];
89
                $expiry_date = $this->processExpiry($value['expires']);
90
                if ($value['ns1'] != '' && $value['ns1'] != 'unknown') $ns1 = $value['ns1'];
91
                if ($value['ns2'] != '' && $value['ns2'] != 'unknown') $ns2 = $value['ns2'];
92
                if ($value['ns3'] != '' && $value['ns3'] != 'unknown') $ns3 = $value['ns3'];
93
                if ($value['ns4'] != '' && $value['ns4'] != 'unknown') $ns4 = $value['ns4'];
94
                if ($value['ns5'] != '' && $value['ns5'] != 'unknown') $ns5 = $value['ns5'];
95
                if ($value['ns6'] != '' && $value['ns6'] != 'unknown') $ns6 = $value['ns6'];
96
                if ($value['ns7'] != '' && $value['ns7'] != 'unknown') $ns7 = $value['ns7'];
97
                if ($value['ns8'] != '' && $value['ns8'] != 'unknown') $ns8 = $value['ns8'];
98
                if ($value['ns9'] != '' && $value['ns9'] != 'unknown') $ns9 = $value['ns9'];
99
                if ($value['ns10'] != '' && $value['ns10'] != 'unknown') $ns10 = $value['ns10'];
100
                if ($value['autorenew'] == 'yes') {
101
                    $autorenew = '1';
102
                } else {
103
                    $autorenew = '0';
104
                }
105
                $privacy = '0';
106
107
                $sql = "INSERT INTO domain_queue_temp
108
                        (account_id, domain, expiry_date, ns1, ns2, ns3, ns4, ns5, ns6, ns7, ns8, ns9, ns10, autorenew, privacy)
109
                        VALUES
110
                        ('" . $account_id . "', '" . $domain_list[$domain_count] . "', '" . $expiry_date . "', '" . $ns1 . "', '" . $ns2 . "', '" . $ns3 . "', '" . $ns4 . "', '" . $ns5 . "', '" . $ns6 . "', '" . $ns7 . "', '" . $ns8 . "', '" . $ns9 . "', '" . $ns10 . "', '" . $autorenew . "', '" . $privacy . "')";
111
                $result = mysqli_query($connection, $sql) or $error->outputOldSqlError($connection);
112
113
                $domain_count++;
114
115
            }
116
117
        } else {
118
119
            // if the API call failed assign empty values
120
            $domain_list = '';
121
            $domain_count = '';
122
123
        }
124
125
        return array($domain_count, $domain_list);
126
    }
127
128
    public function getFullInfo($connection, $account_id, $domain)
129
    {
130
        $error = new Error();
131
        $sql = "SELECT id, expiry_date, ns1, ns2, ns3, ns4, ns5, ns6, ns7, ns8, ns9, ns10, autorenew, privacy
132
                FROM domain_queue_temp
133
                WHERE account_id = '" . $account_id . "'
134
                  AND domain = '" . $domain . "'
135
                ORDER BY id ASC";
136
        $result = mysqli_query($connection, $sql) or $error->outputOldSqlError($connection);
137
138
         $dns_result = array();
139
140
        while ($row = mysqli_fetch_object($result)) {
141
142
            $expiration_date = $row->expiry_date;
143
144
            $dns_result[0] = $row->ns1;
145
            $dns_result[1] = $row->ns2;
146
            $dns_result[2] = $row->ns3;
147
            $dns_result[3] = $row->ns4;
148
            $dns_result[4] = $row->ns5;
149
            $dns_result[5] = $row->ns6;
150
            $dns_result[6] = $row->ns7;
151
            $dns_result[7] = $row->ns8;
152
            $dns_result[8] = $row->ns9;
153
            $dns_result[9] = $row->ns10;
154
            $dns_servers = $this->processDns($dns_result);
155
156
            $privacy_status = $row->privacy;
157
158
            $autorenewal_status = $row->autorenew;
159
160
            $sql_temp = "DELETE FROM domain_queue_temp
161
                         WHERE id = '" . $row->id . "'";
162
            mysqli_query($connection, $sql_temp) or $error->outputOldSqlError($connection);
163
164
        }
165
166
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
167
    }
168
169
    public function convertToArray($api_result)
170
    {
171
        return json_decode($api_result, TRUE);
172
    }
173
174
    public function processExpiry($expiry_result)
175
    {
176
        $unix_expiration_date = strtotime($expiry_result);
177
        return gmdate("Y-m-d", $unix_expiration_date);
178
    }
179
180
    public function processDns($dns_result)
181
    {
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
} //@formatter:on
192