调用whois client

时间:2017-10-26 04:07:16

标签: php sockets whois

这是我的whois服务器代码。当我在服务器中执行(whois domain1.lk)时,它被回答了2或3分钟并给出了警告 PHP警告:socket_read():无法从套接字[104]读取:第97行/home/user1/whoisserver/socket.php中的对等连接重置 PHP警告:socket_write():无法写入socket [32]:第117行/home/chamara/whoisserver/socket.php中的管道损坏 无法写输出

<?php

// Creating the DB Connection-------------------------------------------------------------------------------

$mysqli = new mysqli("localhost", "root", "", "db1");
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
// Creating the Socket Connection---------------------------------------------------------------------------

$address = "localhost";
$port = 43;
set_time_limit(0);
if (false == ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
    echo "could not create socket";
}

socket_bind($socket, $address, $port) or die("could not bind socket");
socket_listen($socket);

$clients = array($socket);

while (true) {

    $read = array();
    $read[] = $socket;
    $read1 = array_merge($read, $clients);

    if (
        socket_select($read1, $write = NULL, $except = NULL, 0) < 1) {
        continue;
    }

    if (in_array($socket, $read1)) {
        if (( $client = socket_accept($socket)) === false) {
            socket_strerror(socket_last_error($socket));
            break;
        }
        $clients[] = $client;
        $key = array_keys($clients, $client);
    }
    foreach ($clients as $key => $client1) {

        if (in_array($client1, $read1)) {

// Getting user input------------------------------------------------------------------------------------------------

            $string = 'Enter a number' . "\r\n" . "\r\n";
            socket_write($client, $string, strlen($string)) or die("Could not write output\n");

            $str = '';
            while ($input = socket_read($client, 1024)) {
                $str .= $input;
                if (strpos($str, "\n") !== false) {
                    break 1;
                }
            }

// Check whether a valied domain name or not------------------------------------------------------------------------------------

            if (!preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $str)) {

// Split input into two parts------------------------------------------------------------------------------------

                $part = explode(".", $str);

                $last_part1 = $part[count($part) - 1];
                $last_part = trim($last_part1);

                if ($last_part != 'lk') {

                    socket_write($client, "\r\n" . 'This is not a Registered Domain under  Registry' . "\n") or die("Could not write output\n");
                    socket_write($client, "\r\n" . '-------------------------------END------------------------------' . "\n") or die("Could not write output\n");
                } else {

                    $lastBeforePart = $part[count($part) - 2];

                    if (($lastBeforePart == 'com') || ($lastBeforePart == 'hotel') || ($lastBeforePart == 'edu') || ($lastBeforePart == 'org') || ($lastBeforePart == 'web')) {

                        $part1 = $part[count($part) - 3];
                        $part2 = $lastBeforePart . "." . $last_part;
                    } else {
                        $part1 = $lastBeforePart;
                        $part2 = $last_part;
                    }

                    socket_write($client, "\r\n" . 'This Service is Provided by the  Registry. ') or die("Could not write output\n");
                    socket_write($client, "\r\n" . 'Visit  Domain at www.test.lk') or die("Could not write output\n");
                    socket_write($client, "\r\n" . 'Contact Us at 123456 ' . "\n") or die("Could not write output\n");

//DB query----------------------------------------------------------------------------------------------------------

                    $sql = "SELECT DomainCategory.Name "
                        . "FROM DomainName_Client, DomainNameType, DomainCategory, OrderDomain_Client "
                        . "WHERE DomainName_Client.Name='$part1' "
                        . "AND DomainNameType.Name='$part2' "
                        . "AND DomainName_Client.TypeID=DomainNameType.ID "
                        . "AND DomainCategory.ID=DomainName_Client.DomainCategoryID "
                        . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID";

                    $sql1 = "SELECT OrderItem_Client.Created,OrderItem_Client.ExpirationDate "
                        . "FROM DomainName_Client, DomainNameType, OrderDomain_Client, OrderItem_Client "
                        . "WHERE DomainName_Client.Name='$part1' "
                        . "AND DomainNameType.Name='$part2'"
                        . "AND DomainName_Client.TypeID=DomainNameType.ID "
                        . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID  "
                        . "AND OrderDomain_Client.OrderItemID = OrderItem_Client.ID ";

                    $sql2 = "SELECT Customer_Client.FirstName "
                        . "FROM DomainName_Client, DomainNameType, OrderDomain_Client, OrderItem_Client, Order_Client,Customer_Client "
                        . "WHERE DomainName_Client.Name='$part1' "
                        . "AND DomainNameType.Name='$part2' "
                        . "AND DomainName_Client.TypeID=DomainNameType.ID "
                        . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID "
                        . "AND OrderDomain_Client.OrderItemID=OrderItem_Client.ID "
                        . "AND Order_Client.ID= OrderItem_Client.OrderID "
                        . "AND Customer_Client.ID= Order_Client.RegistrantID";

                    $sql3 = "SELECT Organization_Client.Name "
                        . "FROM DomainName_Client, DomainNameType, OrderDomain_Client,  OrderItem_Client, Order_Client,Organization_Client "
                        . "WHERE DomainName_Client.Name='$part1' "
                        . "AND DomainNameType.Name='$part2' "
                        . "AND DomainName_Client.TypeID=DomainNameType.ID "
                        . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID "
                        . "AND OrderDomain_Client.OrderItemID = OrderItem_Client.ID "
                        . "AND OrderItem_Client.OrderID= Order_Client.ID "
                        . "AND Organization_Client.ID= Order_Client.OrganizationID";

                    $sql4 = "SELECT ResourceRecord_Client.Type, ResourceRecord_Client.Parameter, ResourceRecord_Client.Value "
                        . "FROM DomainName_Client, DomainNameType, OrderDomain_Client, ResourceRecord_Client "
                        . "WHERE DomainName_Client.Name='$part1' "
                        . "AND DomainNameType.Name='$part2' "
                        . "AND DomainName_Client.TypeID=DomainNameType.ID "
                        . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID "
                        . "AND ResourceRecord_Client.OrderDomainID=OrderDomain_Client.ID ";

                    $sql5 = "SELECT NameServerRecord_Client.NameServer, NameServerRecord_Client.IPAddress, NameServerRecord_Client.TTL "
                        . "FROM DomainName_Client, DomainNameType, OrderDomain_Client, NameServerRecord_Client "
                        . "WHERE DomainName_Client.Name='$part1'"
                        . "AND DomainNameType.Name='$part2' "
                        . "AND DomainName_Client.TypeID=DomainNameType.ID " . "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID "
                        . "AND NameServerRecord_Client.OrderDomainID=OrderDomain_Client.ID";

                    $sqlNew = "SELECT Name " . "FROM DomainName_Client "
                        . "WHERE Name='$part1'";

                    $resultNew = $mysqli->query($sqlNew);
                    $rowNew = $resultNew->num_rows;
                    // var_dump($rowNew);

                    if ($rowNew > 0) {


// Category name-----------------------------------------------------------------------------------------------

                        $result = $mysqli->query($sql);
                        $row0 = $result->num_rows;

                        if ($row0 > 0) {

                            $row = $result->fetch_array(MYSQLI_ASSOC);
                            $category = $row['Name'];

                            socket_write($client, "\r\n" . 'Category     :   ' . $category . "\n") or die("Could not write output\n");
                        } else {
                            socket_write($client, "\r\n" . 'Category     :   No any Category' . "\n") or die("Could not write output\n");
                        }

// Date details-----------------------------------------------------------------------------------------------

                        $result1 = $mysqli->query($sql1);
                        $row1 = $result1->num_rows;

                        if ($row1 > 0) {

                            $row = $result1->fetch_array(MYSQLI_ASSOC);
                            $created_date = $row['Created'];
                            $edited_date = $row ['ExpirationDate'];

                            if ($created_date == '') {

                                $created_date = 'There is no any created date';
                            } else {
                                $created_date = $row['Created'];
                            }
                            if ($edited_date == '') {

                                $edited_date = 'There is no expiration date';
                            } else {
                                $edited_date = $row['ExpirationDate'];
                            }

                            socket_write($client, "\r\n" . 'Created on   :  ' . $created_date) or die("Could not write output\n");
                            socket_write($client, "\r\n" . 'Expires on   :  ' . $edited_date . "\n") or die("Could not write output\n");
                        } else {
                            $created_date = 'There is no any created date';
                            $edited_date = 'There is no expiration date';

                            socket_write($client, "\r\n" . 'Created on   :  ' . $created_date) or die("Could not write output\n");
                            socket_write($client, "\r\n" . 'Expires on   :  ' . $edited_date . "\n") or die("Could not write output\n");
                        }

// company details-----------------------------------------------------------------------------------------------

                        $result3 = $mysqli->query($sql3);
                        $row3 = $result3->num_rows;

                        if ($row3 > 0) {

                            $row = $result3->fetch_array(MYSQLI_ASSOC);
                            $company = $row['Name'];

                            socket_write($client, "\r\n" . 'Company Name        :   ' . $company) or die("Could not write output\n");
                        } else {
                            socket_write($client, "\r\n" . 'Company Name        :   No any Company Name') or die("Could not write output\n");
                        }

// User contact details-----------------------------------------------------------------------------------------------

                        $result2 = $mysqli->query($sql2);
                        $row4 = $result2->num_rows;

                        if ($row4 > 0) {

                            $row = $result2->fetch_array(MYSQLI_ASSOC);
                            $FirstName = $row['FirstName'];

                            socket_write($client, "\r\n" . 'Registrant Contact  :  ' . $FirstName . "\n") or die("Could not write output\n");
                        } else {
                            socket_write($client, "\r\n" . 'Registrant Contact  :  No any Registrant Contact ' . "\n") or die("Could not write output\n");
                        }

// Resource records-----------------------------------------------------------------------------------------------

                        $result4 = $mysqli->query($sql4);
                        $row5 = $result4->num_rows;

                        if ($row5 > 0) {
                            socket_write($client, "\r\n" . 'Resource Record Information' . "\n") or die("Could not write output\n");

                            while ($row = $result4->fetch_assoc()) {

                                $Type = $row['Type'];
                                $value = $row['Value'];
                                $parameter = $row['Parameter'];
                                if ($Type == '') {
                                    socket_write($client, "\r\n" . 'Type        :   No any type') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'Type        :   ' . $Type) or die("Could not write output\n");
                                }

                                if ($value == '') {
                                    socket_write($client, "\r\n" . 'Value        :   No any value') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'Value       :   ' . $value) or die("Could not write output\n");
                                }
                                if ($parameter == '') {
                                    socket_write($client, "\r\n" . 'Parameter   :   No any Parameter') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'Parameter   :   ' . $parameter . "\n") or die("Could not write output\n");
                                }
                            }
                        }
// Name server records-----------------------------------------------------------------------------------------------

                        $result5 = $mysqli->query($sql5);
                        $row6 = $result5->num_rows;

                        if ($row6 > 0) {
                            socket_write($client, "\r\n" . 'NameServer Record Information' . "\n") or die("Could not write output\n");

                            while ($row = $result5->fetch_assoc()) {

                                $ip = $row['IPAddress'];
                                $ttl = $row ['TTL'];
                                $nameserver = $row['NameServer'];
                                if ($nameserver == '') {
                                    socket_write($client, "\r\n" . 'NameServer  :   No any Name Server') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'NameServer  :   ' . $nameserver) or die("Could not write output\n");
                                }
                                if ($ip == '') {
                                    socket_write($client, "\r\n" . 'IP Address  :   No any IP Address') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'IP Address  :   ' . $ip) or die("Could not write output\n");
                                }
                                if ($ttl == '') {
                                    socket_write($client, "\r\n" . 'TTL     :   No any TTL') or die("Could not write output\n");
                                } else {
                                    socket_write($client, "\r\n" . 'TTL         :   ' . $ttl . "\n") or die("Could not write output\n");
                                }
                            }
                        }
                        socket_write($client, "\r\n" . '-------------------------------END------------------------------' . "\n") or die("Could not write output\n");
                    } else {
                        socket_write($client, "\r\n" . 'This is not a Registered Domain under  Registry' . "\n") or die("Could not write output\n");
                        socket_write($client, "\r\n" . '-------------------------------END------------------------------' . "\n") or die("Could not write output\n");
                    }
                }
            } else {
                socket_write($client, "\r\n" . 'This is not a Registered Domain under Registry' . "\n") or die("Could not write output\n");
                socket_write($client, "\r\n" . '-------------------------------END------------------------------' . "\n") or die("Could not write output\n");
            }
            socket_close($socket);
        }
    }
}

0 个答案:

没有答案