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

OpenSrs::getFullInfo()   C

Complexity

Conditions 9
Paths 4

Size

Total Lines 58
Code Lines 26

Duplication

Lines 12
Ratio 20.69 %

Importance

Changes 0
Metric Value
dl 12
loc 58
rs 6.9928
c 0
b 0
f 0
cc 9
eloc 26
nc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * /classes/DomainMOD/OpenSrs.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 OpenSrs
25
{
26
27
    public function domainList()
28
    {
29
        $start_date = gmdate("Y-m-d", mktime(date("H") - 12, date("i"), date("s"), date("m"), date("d"), date("Y")));
30
        $end_date = gmdate("Y-m-d", mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y") + 19));
31
32
        $xml = <<<EOD
33
                <?xml version='1.0' encoding='UTF-8' standalone='no' ?>
34
                <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
35
                <OPS_envelope>
36
                <header>
37
                    <version>0.9</version>
38
                </header>
39
                <body>
40
                <data_block>
41
                    <dt_assoc>
42
                        <item key="protocol">XCP</item>
43
                        <item key="object">DOMAIN</item>
44
                        <item key="action">GET_DOMAINS_BY_EXPIREDATE</item>
45
                        <item key="attributes">
46
                         <dt_assoc>
47
                                <item key="exp_from">$start_date</item>
48
                                <item key="exp_to">$end_date</item>
49
                                <item key="page">1</item>
50
                                <item key="limit">10000</item>
51
                         </dt_assoc>
52
                        </item>
53
                    </dt_assoc>
54
                </data_block>
55
                </body>
56
                </OPS_envelope>
57
EOD;
58
        return $xml;
59
    }
60
61
    public function domainInfo($domain)
62
    {
63
        $xml = <<<EOD
64
                <?xml version='1.0' encoding='UTF-8' standalone='no' ?>
65
                <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
66
                <OPS_envelope>
67
                <header>
68
                    <version>0.9</version>
69
                </header>
70
                <body>
71
                <data_block>
72
                    <dt_assoc>
73
                        <item key="protocol">XCP</item>
74
                        <item key="object">DOMAIN</item>
75
                        <item key="action">GET</item>
76
                        <item key="attributes">
77
                         <dt_assoc>
78
                                <item key="domain">$domain</item>
79
                                <item key="type">all_info</item>
80
                         </dt_assoc>
81
                        </item>
82
                    </dt_assoc>
83
                </data_block>
84
                </body>
85
                </OPS_envelope>
86
EOD;
87
        return $xml;
88
    }
89
90
    public function domainPrivacy($domain)
91
    {
92
        $xml = <<<EOD
93
                <?xml version='1.0' encoding='UTF-8' standalone='no' ?>
94
                <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
95
                <OPS_envelope>
96
                <header>
97
                    <version>0.9</version>
98
                </header>
99
                <body>
100
                <data_block>
101
                    <dt_assoc>
102
                        <item key="protocol">XCP</item>
103
                        <item key="object">DOMAIN</item>
104
                        <item key="action">GET</item>
105
                        <item key="attributes">
106
                         <dt_assoc>
107
                                <item key="domain">$domain</item>
108
                                <item key="type">whois_privacy_state</item>
109
                         </dt_assoc>
110
                        </item>
111
                    </dt_assoc>
112
                </data_block>
113
                </body>
114
                </OPS_envelope>
115
EOD;
116
        return $xml;
117
    }
118
119
    public function apiCall($xml, $account_username, $api_key)
120
    {
121
        $handle = curl_init('https://rr-n1-tor.opensrs.net:55443'); // Production Environment
122
        // $handle = curl_init('https://horizon.opensrs.net:55443'); // Test environment
123
        curl_setopt($handle, CURLOPT_HTTPHEADER, array(
124
            'Content-Type:text/xml',
125
            'X-Username:' . $account_username,
126
            'X-Signature:' . md5(md5($xml . $api_key) .  $api_key)));
127
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
128
        curl_setopt($handle, CURLOPT_POST, 1);
129
        curl_setopt($handle, CURLOPT_POSTFIELDS, $xml);
130
        $result = curl_exec($handle);
131
        return $result;
132
    }
133
134
    public function getDomainList($account_username, $api_key)
135
    {
136
        $api_xml = $this->domainList();
137
        $api_results = $this->apiCall($api_xml, $account_username, $api_key);
138
        $api_call_status = $this->apiStatus($api_results);
139
140
        $domain_list = array();
141
        $domain_count = 0;
142
143 View Code Duplication
        if ($api_call_status == '1') {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
144
145
            foreach(preg_split("/((\r?\n)|(\r\n?))/", $api_results) as $xml_line) {
146
147
                if (preg_match('/<item key="name">(.*)<\/item>/', $xml_line, $match)) {
148
                    $domain_list[] = $match[1];
149
                    $domain_count++;
150
                }
151
152
            }
153
154
        }
155
        return array($domain_count, $domain_list);
156
    }
157
158
    public function getFullInfo($account_username, $api_key, $domain)
159
    {
160
        // get the partial domain info (expiration date, dns servers, and auto renewal status)
161
        $api_xml = $this->domainInfo($domain);
162
        $api_results = $this->apiCall($api_xml, $account_username, $api_key);
163
        $api_call_status = $this->apiStatus($api_results);
164
165
        $expiration_date = '';
166
        $dns_result = array();
167
        $autorenewal_status = '';
168
        $dns_servers = array();
169
170
        if ($api_call_status == '1') {
171
172
            foreach(preg_split("/((\r?\n)|(\r\n?))/", $api_results) as $xml_line) {
173
174
                // get expiration date
175
                if (preg_match('/<item key="expiredate">(.*)<\/item>/', $xml_line, $match)) {
176
                    $expiration_date = $this->processExpiry($match[1]);
177
                }
178
179
                // get dns servers
180
                if (preg_match('/<item key="name">(.*)<\/item>/', $xml_line, $match)) {
181
                    $dns_result[] = $match[1];
182
                }
183
184
                // get auto renewal status
185
                if (preg_match('/<item key="auto_renew">(.*)<\/item>/', $xml_line, $match)) {
186
                    $autorenewal_status = $this->processAutorenew($match[1]);
187
                }
188
189
            }
190
            $dns_servers = $this->processDns($dns_result);
191
192
        }
193
194
        // get the privacy status
195
        $api_xml = $this->domainPrivacy($domain);
196
        $api_results = $this->apiCall($api_xml, $account_username, $api_key);
197
        $api_call_status = $this->apiStatus($api_results);
198
199
        $privacy_status = '';
200
201 View Code Duplication
        if ($api_call_status == '1') {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
202
203
            foreach(preg_split("/((\r?\n)|(\r\n?))/", $api_results) as $xml_line) {
204
205
                // get privacy status
206
                if (preg_match('/<item key="state">(.*)<\/item>/', $xml_line, $match)) {
207
                    $privacy_status = $this->processPrivacy($match[1]);
208
                }
209
210
            }
211
212
        }
213
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
214
215
    }
216
217
    public function apiStatus($api_results)
218
    {
219
        $status = '';
220
        foreach(preg_split("/((\r?\n)|(\r\n?))/", $api_results) as $xml_line) {
221
            if (preg_match('/<item key="response_code">200<\/item>/', $xml_line)) {
222
                $status = '1';
223
            }
224
        }
225
        return $status;
226
    }
227
228
    public function processExpiry($expiry_result)
229
    {
230
        $unix_expiration_date = strtotime($expiry_result);
231
        return gmdate("Y-m-d", $unix_expiration_date);
232
    }
233
234 View Code Duplication
    public function processDns($dns_result)
1 ignored issue
show
Duplication introduced by
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...
235
    {
236
        $dns_servers = array();
237
        if (!empty($dns_result)) {
238
            $dns_servers = array_filter($dns_result);
239
        } else {
240
            $dns_servers[0] = 'no.dns-servers.1';
241
            $dns_servers[1] = 'no.dns-servers.2';
242
        }
243
        return $dns_servers;
244
    }
245
246
    public function processPrivacy($privacy_result)
247
    {
248
        if ($privacy_result == 'enabled') {
249
            $privacy_status = '1';
250
        } else {
251
            $privacy_status = '0';
252
        }
253
        return $privacy_status;
254
    }
255
256
    public function processAutorenew($autorenewal_result)
257
    {
258
        if ($autorenewal_result == '1') {
259
            $autorenewal_status = '1';
260
        } else {
261
            $autorenewal_status = '0';
262
        }
263
        return $autorenewal_status;
264
    }
265
266
} //@formatter:on
267