-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.php
152 lines (128 loc) · 3.85 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('max_execution_time', 300);
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
include 'Request.php';
include 'config.php';
include 'vendor/autoload.php';
use \JJG\Request as Request;
use Iodev\Whois\Whois;
function Login($baseurl, $apikey)
{
global $accesstoken;
$request = new Request($baseurl . 'authenticate/login/' . $apikey);
$request->enableSSL();
$request->connectTimeout = 5;
$request->timeout = 10;
$request->setRequestType('GET');
$request->execute();
$response = $request->getResponse();
if (empty($response)) {
return false;
}
$response = json_decode($response, true);
if (!isset($response['parameters']['token'])) {
return false;
}
$accesstoken = $response['parameters']['token'];
}
function validatePostData($tldlist, $data)
{
$tldlist = array_flip($tldlist);
$data = array_filter($data);
$requestData = array();
foreach ($data as $key => $value) {
if (validateDomain($tldlist, $value)) {
$requestData['domainname[' . $key . ']'] = $value;
}
}
if (empty($requestData)) {
return false;
} else {
return $requestData;
}
}
function validateDomain($tldlist, $domain)
{
$tld = substr($domain, strpos($domain, ".") + 1);
$string_len = strlen($domain);
$tld_len = strlen(substr($domain, strrpos($domain, '.') + 1));
if($tld_len < 2) {
return false;
}
if ($string_len < 3 OR $string_len > 64) {
return false;
}
if(strpos($domain, '.') === FALSE) {
return false;
}
if(!isset($tldlist[$tld])) {
return false;
}
return true;
}
function bulksearch($data, $baseurl, $accesstoken)
{
$request = new Request($baseurl . 'domain/searchdomain/' . $accesstoken);
$request->setRequestType('POST');
$request->setPostFields($data);
$request->execute();
$response = $request->getResponse();
echo $response;
exit;
}
function whois($domain)
{
try {
$response = Whois::create()->lookupDomain($domain);
echo json_encode($response->getText());
}
catch (Exception $e) {
echo json_encode('An error occured, please report this: ' . $e->getMessage());
}
}
$action = $_REQUEST['action'];
// Get list of TLDs
$tldlist = file('tlds.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
switch ($action) {
case "bulksearch":
header("HTTP/1.1 200 OK");
$requestData = validatePostData($tldlist, $_REQUEST['domainlist']);
if($requestData) {
Login($baseurl, $apikey);
bulksearch($requestData, $baseurl, $accesstoken);
}
else {
echo json_encode(array("Error" => "Empty or invalid data was given."));
}
break;
case "whois":
if (validateDomain(array_flip($tldlist), $_REQUEST['domain'])) {
whois($_REQUEST['domain']);
}
break;
case "config":
echo json_encode($config);
break;
case "domainlist":
$data = array();
$searchstring = urldecode($_REQUEST['search']);
$searchstring = preg_split('/ +/', $searchstring);
foreach ($searchstring as $key => $value) {
if (strpos($value, '.') !== false) {
array_push($data, $value);
} else {
foreach ($tldlist as $tldNumber => $tld) {
$domain = $value . '.' . $tld;
array_push($data, $domain);
}
}
}
echo json_encode($data);
break;
default:
echo json_encode(array("Error" => "Empty data was given."));
}