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

Freenom::processExpiry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * /classes/DomainMOD/Freenom.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 Freenom
25
{
26
27
    public function getApiKey($connection, $account_id)
28
    {
29
        $error = new Error();
30
        $sql = "SELECT username, `password`
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
                $account_username = $row->username;
41
                $account_password = $row->password;
42
43
            }
44
45
        } else {
46
47
            echo "No API Credentials Found";
48
            exit;
49
50
        }
51
52
        return array($account_username, $account_password);
53
    }
54
55
    public function getApiUrl($account_username, $account_password, $domain, $command)
56
    {
57
        $base_url = 'https://api.freenom.com/v2/';
58
        if ($command == 'domainlist') {
59
            return $base_url . 'domain/list?results_per_page=10000&email=' . $account_username . '&password=' . $account_password;
60
        } elseif ($command == 'info') {
61
            return $base_url . 'domain/getinfo?domainname=' . $domain . '&email=' . $account_username . '&password=' . $account_password;
62
        } else {
63
            return 'Unable to build API URL';
64
        }
65
    }
66
67
    public function apiCall($full_url)
68
    {
69
        $handle = curl_init($full_url);
70
        curl_setopt( $handle, CURLOPT_RETURNTRANSFER, TRUE );
71
        $result = curl_exec($handle);
72
        curl_close($handle);
73
        return $result;
74
    }
75
76
    public function getDomainList($account_username, $account_password)
77
    {
78
        $api_url = $this->getApiUrl($account_username, $account_password, '', 'domainlist');
79
        $api_results = $this->apiCall($api_url);
80
        $array_results = $this->convertToArray($api_results);
81
82
        // confirm that the api call was successful
83
        if ($array_results['status'] == 'OK') {
84
85
            $domain_list = array();
86
            $domain_count = 0;
87
88
            foreach ($array_results['domain'] AS $domain) {
89
90
                $domain_list[] = $domain['domainname'];
91
                $domain_count++;
92
93
            }
94
95
        } else {
96
97
            // if the API call failed assign empty values
98
            $domain_list = '';
99
            $domain_count = '';
100
101
        }
102
103
        return array($domain_count, $domain_list);
104
    }
105
106
    public function getFullInfo($account_username, $account_password, $domain)
107
    {
108
        $api_url = $this->getApiUrl($account_username, $account_password, $domain, 'info');
109
        $api_results = $this->apiCall($api_url);
110
        $array_results = $this->convertToArray($api_results);
111
112
        // confirm that the api call was successful
113
        if ($array_results['status'] == 'OK') {
114
115
            // get expiration date
116
            $expiration_date = $this->processExpiry($array_results['domain'][0]['expirationdate']);
117
118
            // get dns servers
119
            $dns_result = $array_results['domain'][0]['nameserver'];
120
            $dns_servers = $this->processDns($dns_result);
121
122
            // get privacy status
123
            $privacy_status = $this->processPrivacy($array_results['domain'][0]['idshield']);
124
125
            // get auto renewal status
126
            $autorenewal_status = $this->processAutorenew($array_results['domain'][0]['autorenew']);
127
128
        } else {
129
130
            // if the API call failed assign empty values
131
            $expiration_date = '';
132
            $dns_servers = '';
133
            $privacy_status = '';
134
            $autorenewal_status = '';
135
136
        }
137
138
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
139
    }
140
141
    public function convertToArray($api_result)
142
    {
143
        return json_decode($api_result, TRUE);
144
    }
145
146
    public function processExpiry($expiry_result)
147
    {
148
        $expiry_result = substr_replace($expiry_result, '-', 4, 0);
149
        $expiry_result = substr_replace($expiry_result, '-', 7, 0);
150
        return $expiry_result;
151
    }
152
153
    public function processDns($dns_result)
154
    {
155
        if (!empty($dns_result)) {
156
            $dns_servers = array();
157
            foreach ($dns_result AS $nameserver) {
158
                $dns_servers[] = $nameserver['hostname'];
159
            }
160
        } else {
161
            $dns_servers[0] = 'no.dns-servers.1';
162
            $dns_servers[1] = 'no.dns-servers.2';
163
        }
164
        return $dns_servers;
165
    }
166
167
    public function processPrivacy($privacy_result)
168
    {
169
        if ($privacy_result == 'enabled') {
170
            $privacy_status = '1';
171
        } else {
172
            $privacy_status = '0';
173
        }
174
        return $privacy_status;
175
    }
176
177
    public function processAutorenew($autorenewal_result)
178
    {
179
        if ($autorenewal_result == 'enabled') {
180
            $autorenewal_status = '1';
181
        } else {
182
            $autorenewal_status = '0';
183
        }
184
        return $autorenewal_status;
185
    }
186
187
} //@formatter:on
188