<?php
include_once 'Controller.php';

if (isset($_REQUEST['shortcode'])) {
    $short_url = $_REQUEST['shortcode'];



    $short_url_decoded = urldecode($short_url);
    $short_url_decoded_nospace = str_replace(' ', '', $short_url_decoded);
    $special_char = substr($short_url_decoded_nospace, -1);
    if (in_array($special_char, array('|', '_', '-'))) {
        include_once 'frame.php';
        exit();
    }



    $long_url = Controller::get_long_url($short_url);
    if ($long_url) {

        Controller::run_webhook($short_url);

        if (strpos($long_url, 'https://') === FALSE) {
            if (strpos($long_url, 'http://') === FALSE) {
                $long_url = "//$long_url";
            }
        }
        ?>
        <script>
            var win = window.open('<?php echo $long_url; ?>', '_self');
            win.focus();
        </script>
        <?php
    } else {
        echo "Sorry, the link you clicked was invalid or has expired";
    }
} else {
    ?>
    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <?
    		$domain1 = $_SERVER['HTTP_HOST']; 
    		$domainsplit = explode('.',$domain1);
    ?>
    <html>
        <head>
            <title><? echo $_SERVER['HTTP_HOST']; ?> - <? echo Preferences::SITE_SLOGAN; ?></title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width">
            <meta property="og:title" content="Click Here To Open Website" />
            <meta property="og:image" content="http://go-4.net/clickhere.png" />

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        </head>
        <link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
        <style>
            html { 
                background: url(cityscape.jpg) no-repeat center center fixed; 
                -webkit-background-size: cover;
                -moz-background-size: cover;
                -o-background-size: cover;
                background-size: cover;
            }
            input, select,textarea {
                -webkit-appearance: none;
                -webkit-box-shadow: rgba(0, 0, 0, 0.0470588) 0px 0px 0px 3px;
                -webkit-font-smoothing: antialiased;
                -webkit-rtl-ordering: logical;
                -webkit-user-select: text;
                -webkit-writing-mode: horizontal-tb;
                background-color: rgb(255, 255, 255);

                background-position: 96% 50% !important;
                background-repeat: no-repeat !important;
                border-bottom-color: rgba(0, 0, 0, 0.2);
                border-bottom-left-radius: 5px;
                border-bottom-right-radius: 5px;
                border-bottom-style: solid;
                border-bottom-width: 1px;
                border-image-outset: 0px;
                border-image-repeat: stretch;
                border-image-slice: 100%;
                border-image-source: none;
                border-image-width: 1;
                border-left-color: rgba(0, 0, 0, 0.2);
                border-left-style: solid;
                border-left-width: 1px;
                border-right-color: rgba(0, 0, 0, 0.2);
                border-right-style: solid;
                border-right-width: 1px;
                border-top-color: rgba(0, 0, 0, 0.2);
                border-top-left-radius: 5px;
                border-top-right-radius: 5px;
                border-top-style: solid;
                border-top-width: 1px;
                box-shadow: rgba(0, 0, 0, 0.0470588) 0px 0px 0px 3px;
                box-sizing: border-box;
                color: rgb(47, 47, 47);
                cursor: auto;
                display: inline-block;
                font-family: sans-serif;
                font-size: 24px;
                font-stretch: normal;
                font-style: normal;
                font-variant: normal;
                font-weight: normal;
                height: 42px;
                letter-spacing: normal;
                line-height: 22.8571434020996px;
                margin-bottom: 0px;
                margin-left: 0px;
                margin-right: 0px;
                margin-top: 6px;
                outline-color: rgb(47, 47, 47);
                outline-style: none;
                outline-width: 0px;
                padding-bottom: 8px;
                padding-left: 15px;
                padding-right: 15px;
                padding-top: 8px;
                text-align: center;
                text-indent: 0px;
                text-rendering: auto;
                text-shadow: none;
                text-transform: none;
                width: 94%;
                word-spacing: 0px;
                writing-mode: lr-tb;
            }
            button {
                width:400px;
                height:60px;
                margin-top:20px;
                font-size:28px;
                color:white;
                background: green; /* Old browsers */
                font-family:Ubuntu !important;
                font-weight:bold;
                border-radius:10px;
                border:2px solid #00CC00;
                cursor:pointer
            }
            button:hover {
                background:#33CC33; text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
            }
            .auto-style1 {
                color: #FFFF00;
                font-size: 20px;text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
            }
            .auto-style2 {
                color: #FFFFFF;
                font-size: 40px;text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
            }
            .auto-style4 {
                color: #FFFF12;
                font-size: 28px;
                text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);;margin-top:-60px;
            }
            .auto-style5 {
                font-size: 80px;
                text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
                color: #FFFF12;
            }
        </style>
        <body style="text-align:center;font-family:Ubuntu;margin:0">
            <div style="background:black;opacity:.4;height:180px;border-bottom:4px solid yellow"></div>
            <div style="width:100%;height:180px;margin:0 auto">
            	<div style="width:100%;margin-top:-180px;z-index:9999;position:fixed;"><br>
                    <span class="auto-style5" style="text-transform:uppercase;font-size:6vw"><? echo $domainsplit[0]; ?></span>
                    <span class="auto-style5" style="text-transform:uppercase;font-size:3vw;color:white"><? echo $domainsplit[1]; ?></span>
                    <div style="clear:both"></div><br><br><br>
                    <p class="auto-style4" style="color:white"><? echo Preferences::SITE_SLOGAN; ?></p><br><br>
                    <img src="g04.gif" alt="" style="margin-top:-8px"/>
                 </div>
           </div>
                
            <div style="margin-top:-50px">
            	<div>


                    <textarea type="text" style="width: 500px" id="longurl" placeholder="Enter URL to Fetch & Shorten" name="longurl" ></textarea><br/>

                    <div id="response" style="display:none;"><label><br><span class="auto-style1">Your ShortURL Has Been 
                                Fetched, WOOOF!</span></label><br/>
                        <textarea id="shorturl" style="width: 500px" readonly></textarea></div><br/>

                    <button id="submit" value="Submit" onclick="get_short_url();" >Generate Short URL</button>
                    <a  id="tryagain" href="/" style="display:none"><button>Fetch Another Short URL</button></a>
                    <span id="processing" style="display: none;color:yellow">&nbspProcessing...</span>
                </div>
            </div>

            <script>
                function isUrlValid(url) {
                    return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url);
                }
                function get_short_url() {
                    $('#processing').show();
                    var longURL = $('#longurl').val();
                    var url = $.trim(longURL);
                    url_validate = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
                    if (!url_validate.test(url)) {
                        $("#longurl").css("background", "red");
                        alert("Enter A Valid URL");
                        $('#processing').hide();
                    }
                    else {
                        $.post("server.php", {cmd: 'get_short_url', long_url: encodeURI(longURL)}, function (res) {
                            $("#longurl").css("background", "white");
                            $('#shorturl').val(res);
                            $('#processing').hide();
                            $('#submit').hide();
                            $('#response').show();
                            $('#tryagain').show();
                        });
                    }
                }
            </script>
        </body>
    </html>
    <?php
}
