<?php
error_reporting(E_PARSE | E_ERROR);

session_start();

$page = isset($_GET['page']) ? $_GET['page'] : '';
if (!$page) {
    $page = 'index';
}
$baseUrl = 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ? 's' : '').'://' . $_SERVER['HTTP_HOST'];
include_once "files/functions.php";

if (($page == 'contact' || $page == 'index') && !empty($_POST)) {
    if ($page == 'index') {
        if ($_SERVER['HTTP_REFERER'] != $baseUrl . '/')
            die();
        // PHP 8.1+ throws on mysqli errors by default; keep the old silent behaviour
        mysqli_report(MYSQLI_REPORT_OFF);
        $act = $_POST['act'];
        if ($act == 'submitScore') {
            $name = escape(strip_tags(htmlentities($_POST['name'])));
            $score = escape(strip_tags(htmlentities($_POST['score'])));
            if (!$name || !$score)
                die();
            $db = new mysqli('002.mysql.db.fge.5hosting.com', 'u1287_robert', '0LpJHa4C81', 'db1287_main');
            $sql = "INSERT IGNORE INTO game (score, name, created_at) VALUES ('" . $score . "', '" . $name . "', '" . date('Y-m-d H:i:s') . "')";
            mysqli_query($db, $sql);
            $id = mysqli_insert_id($db);
            mysqli_close($db);
            echo $score . '_' . $id . '_' . date('Y-m-d H:i:s');
            die();
        }
        else if ($act == 'deleteScore') {
            $id = escape(strip_tags(htmlentities($_POST['id'])));
            $date = escape(strip_tags(htmlentities($_POST['date'])));
            $db = new mysqli('002.mysql.db.fge.5hosting.com', 'u1287_robert', '0LpJHa4C81', 'db1287_main');
            $sql = "DELETE FROM game WHERE id = '" . $id . "' AND created_at = '" . $date . "'";
            mysqli_query($db, $sql);
            mysqli_close($db);
            die();
        } else if ($act == 'getScores') {
            $db = new mysqli('002.mysql.db.fge.5hosting.com', 'u1287_robert', '0LpJHa4C81', 'db1287_main');
            $sql = "SELECT name, score FROM game ORDER BY score DESC LIMIT 10";
            $result = mysqli_query($db, $sql);
            $return = array();
            while ($row = mysqli_fetch_assoc($result)) {
                $return[] = array('name' => $row['name'], 'score' => $row['score']);
            }
            echo json_encode($return);
            mysqli_close($db);
            die();
        }
    } else {
        include_once "files/PHPMailer/class.phpmailer.php";
        include_once "files/PHPMailer/class.smtp.php";
        $post = array();
        foreach ($_POST as $key => $value) {
            $post[$key] = escape(strip_tags(htmlentities($value)));
        }

        extract($post);

        $ok = true;
        if (!$first_name || !$last_name || !$email || !$message) {
            $ok = false;
            echo "Please fill all the required fields.";
            exit;
        }

        //recaptcha
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $params = [
            'secret' => '6Lda5jQUAAAAAFZ8S5zai_q3cRWWJOYxX9QfiUle',
            'response' => $post['g-recaptcha-response'],
            'remoteip' => $_SERVER['REMOTE_ADDR'],
        ];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);

        //exec curl
        $response = json_decode(curl_exec($curl), true);
        curl_close($curl);

        if ( !$response['success'] ) {
            $ok = false;
            echo "Please let us know you are not a robot.";
            exit;
        }
        dmp($response);

        if ($ok) {
            $subject = "A message from " . $first_name . " " . $last_name . " @ planetweb.ro";
            $to = "robert@planetweb.ro";
            $message = 'A message from ' . $first_name . ' ' . $last_name . ' (email: ' . $email . '), (phone: ' . $phone . ') <br/><br/>Message:<br/>' . $message;
            sendMail($to, $subject, $message);

            echo 'Thank you for your message!';
            exit;
        }
    }

    header('Location: index');
}

$pages = array('index', 'about-us', 'technologies', 'services', 'portfolio', 'contact', 'game');
$pageDetails = array(
    'index' => array(
        'title' => 'Welcome',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.',
        'og:image' => '',
        'og:image:type' => 'image/jpeg',
        'og:title' => '',
        'og:description' => '',
        'og:url' => '',
    ),
    'about-us' => array(
        'title' => 'About us, who we are',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.'
    ),
    'technologies' => array(
        'title' => 'Technologies we use',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.'
    ),
    'services' => array(
        'title' => 'Services we provide',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.'
    ),
    'portfolio' => array(
        'title' => 'Our portfolio',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.'
    ),
    'contact' => array(
        'title' => 'Contact us',
        'keywords' => 'planetweb.ro web development and web design, planetweb.ro, web development, web design, planetweb.ro web dev and design, planetweb.ro development firm, phalcon, zend frameword, zend, mysql, javascript, jquery, mysql, html, html5, css',
        'description' => 'Planetweb.ro Web development and Web design Company, Romania, Craiova. We work mostly in Phalcon and Zend framework.'
    ),
);
include_once "files/header.phtml";



if (!in_array($page, $pages)) {
    http_response_code(404);
    include_once "pages/error.phtml";
}

if (!file_exists("pages/" . $page . ".phtml")) {
    http_response_code(404);
    include_once "pages/error.phtml";
}

include_once "pages/" . $page . ".phtml";

include_once "files/footer.phtml";
