<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

add_action('wp_loaded', 'my_custom_redirect');

if (isset($_POST['submit'])) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'locations';
    $all = $wpdb->get_results('SELECT * FROM ' . $table_name);
    if ($all) {
        $ART = 0;
        $APLT = 0;
        $url_tc = parse_url($_POST['url'], PHP_URL_HOST);
        $ip = gethostbyname($url_tc);
        $HC = '';
        $another_table_name = $wpdb->prefix . 'user_requests';
        foreach ($all as $record) {
            $url = $record->url;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, "url=" . $_POST['url']);
            $response = json_decode(curl_exec($curl));
            $create_table_name = $wpdb->prefix . 'request_details';
            $charset_collate = $wpdb->get_charset_collate();
            $sql = "CREATE TABLE $create_table_name (
                id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                userReqId text,
                locationId text NOT NULL,
                loadTime text NOT NULL,
                resTime text NOT NULL) $charset_collate";

            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
            $reqID = 0;
            if ($wpdb->get_results('SELECT * FROM ' . $another_table_name)) {
                $reqID = sizeof($wpdb->get_results('SELECT * FROM ' . $another_table_name)) + 1;
            } else {
                $reqID += 1;
            }

            if ($response->status == 'success') {
                $success = $wpdb->insert($create_table_name, array(
                    "userReqId" => $reqID,
                    "loadTime" => $response->pageLoadTime,
                    "resTime" => $response->reqTime,
                    "locationId" => $record->id
                ));
            }

            $ART += $response->reqTime;
            $APLT += $response->pageLoadTime;
            $HC = $response->oName;
            curl_close($curl);
            $hc_table_name = $wpdb->prefix . 'hosting_company_details';
            $charset_collate = $wpdb->get_charset_collate();
            $sql = "CREATE TABLE $hc_table_name (
                id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                HCId text,
                HCname text,
                locationId text NOT NULL,
                APLT text NOT NULL,
                ART text NOT NULL) $charset_collate";

            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
            $hcd = $wpdb->get_results('SELECT * FROM ' . $hc_table_name . ' WHERE HCname = "' . $response->oName . '" AND locationId = "'.$record->id.'"')[0];
            if($hcd) {
                $hcd->APLT = ($response->pageLoadTime+$hcd->APLT)/2;
                $hcd->ART = ($response->reqTime+$hcd->ART)/2;
                $new_hc = array(
                    'id' => $hcd->id,
                    'HCId' => $hcd->id,
                    'HCname' => $hcd->name,
                    'locationId' => $hcd->locationId,
                    'APLT' => $hcd->APLT,
                    'ART' => $hcd->ART
                );
                $success = $wpdb->update($hc_table_name, $new_hc, array('id' => $new_hc['id']));
            }
            else{
                $hosting_table = $wpdb->prefix . 'hosting_companies';
                $hc = $wpdb->get_results('SELECT * FROM ' . $hosting_table . ' WHERE legal_name = "' . $response->oName . '"')[0];
                $success = $wpdb->insert($hc_table_name, array(
                    'HCId' => $hc->id,
                    'HCname' => $hc->legal_name,
                    'locationId' => $record->id,
                    'APLT' => $response->pageLoadTime,
                    'ART' => $response->reqTime
                ));
            }

        }
        $ART = $ART / sizeof($all);
        $APLT = $APLT / sizeof($all);
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE $another_table_name (
                id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                userIp text NOT NULL,
                URL text NOT NULL,
                ART text NOT NULL,
                APLT text NOT NULL,
                SSC text ,
                CLC text ,
                HC text ) $charset_collate";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
            $success = $wpdb->insert($another_table_name, array(
            "userIp" => $ip,
            "URL" => $_POST['url'],
            "ART" => $ART,
            "APLT" => $APLT,
            "SSC" => null,
            "CLC" => null,
            "HC" => $HC
        ));
        $hosting_table = $wpdb->prefix . 'hosting_companies';
        $hc = $wpdb->get_results('SELECT * FROM ' . $hosting_table . ' WHERE legal_name = "' . $response->oName . '"')[0];
        if($hc){
            $hc->APLT = ($hc->APLT+$APLT)/2;
            $hc->ART = ($hc->ART+$ART)/2;
            $new_hc = array(
                'id' => $hc->id,
                'name'=> $hc->name,
                'legal_name'=> $hc->legal_name,
                'APLT' => $hc->APLT,
                'ART' => $hc->ART
            );
            $success = $wpdb->update($hosting_table, $new_hc, array('id' => $new_hc['id']));
        }

    }
    $location = $_SERVER['HTTP_REFERER'];
    wp_redirect($location);
    exit;
}


