<?php
$short_url = rtrim($short_url, $special_char);
$long_url = Controller::get_long_url($short_url);
if (empty($long_url)) {
    echo "Sorry, the link you clicked was invalid or has expired";
    exit();
}

Controller::run_webhook($short_url);

$meta_data = getMetaTags($long_url);
?>
<!DOCTYPE html>
<html>
    <head>
        <?php echo $meta_data['title']; ?>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <?php
        $meta_arr = $meta_data['meta_arr'];
        if (count($meta_arr) > 0) {
            foreach ($meta_arr as $meta_tag) {
                echo $meta_tag;
            }
        }
        ?>
    </head>

    <body style="margin:0px; overflow: overlay;" id="bodyelement"></body>

    <script>



        var screenWidth = window.innerWidth;
        var screenHeight = window.innerHeight;

        /*
         * Iframe height cannot be set once its loaded 
         * so set it on runtime
         */
        document.body.innerHTML = "<div  onclick=\"window.location.replace('<?php echo addslashes($long_url); ?>');\" onscroll=\"window.location.replace('<?php echo addslashes($long_url); ?>');\" style=\"min-height:100%;width:100vw;position:absolute;z-index:1000\"></div><iframe id=\"contentframe\" height=\"" + screenHeight + "\" width=\"" + screenWidth + "\" src=\"<?php echo $long_url; ?>\" style=\"margin:0;border:none\" scrolling=\"auto\"></iframe>";


        window.addEventListener('wheel', function (e) {
            window.location.replace('<?php echo addslashes($long_url); ?>');
        });
        window.addEventListener('scroll', function (e) {
            window.location.replace('<?php echo addslashes($long_url); ?>');
        });

    </script>


    <script>

        var iframeLoaded = false;
        var iframe = document.getElementById('contentframe');

        var siteprinturl = "<?php echo Preferences::DOMAIN; ?>siteprint.php?siteprint=<?php echo urlencode($long_url); ?>";

            var iframeOnloadEvent = function () {
                iframeLoaded = true;
                if (iframe.contentWindow.length > 0) {
                    // success
                    console.log('success');
                } else {
                    // load from cache/curl
                    if (iframe.src != siteprinturl) {
                        console.log('load from cache/curl');
                        console.log(iframe.src);
                        iframe.src = siteprinturl;
                    }

                }
            }



            if (iframe.attachEvent) {
                console.log('onload 1');
                iframe.attachEvent('onload', iframeOnloadEvent);
            } else {
                console.log('onload 2');
                iframe.onload = iframeOnloadEvent;
            }


            // iframe.onload event doesn't trigger in firefox if loading mixed content (http iframe in https parent) and it is blocked.
            setTimeout(function () {
                if (iframeLoaded === false) {
                    //iframe failed to load within 5s
                    console.log('iframe failed to load within 5s');
                    window.location.replace('<?php echo addslashes($long_url); ?>');
                }
            }, 5000);

    </script>


</html>

<?php

function getMetaTags($url) {

    $meta_arr = array();

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);

    $dataarr = explode('<meta', $data);

    if (count($dataarr) > 0) {
        foreach ($dataarr as $value) {
            if (strpos($value, 'content="') !== FALSE) {
                $metatagclosestringarr = explode('>', $value);
                $complete_meta_tag = '<meta ' . $metatagclosestringarr[0] . '>';
                $meta_arr[] = $complete_meta_tag;
            }
        }
    }

    $complete_title_tag = '<title></title>';
    $titlearr = explode('<title>', $data);
    if (count($titlearr) > 0) {
        foreach ($titlearr as $titlearrvalue) {
            if (strpos($titlearrvalue, 'title>') !== FALSE) {
                $titletagclosestringarr = explode('title>', $titlearrvalue);
                $complete_title_tag = '<title> ' . $titletagclosestringarr[0] . 'title>';
                break;
            }
        }
    }

    return array('title' => $complete_title_tag, 'meta_arr' => $meta_arr);
}
?>

