/**
 * Prints first available serie episode link.
 */
function fetchSerie($episode)
{
    $content = file_get_contents($episode, false, getExplorerContext(getSiteHash()));
    preg_match_all("/rel=\"(.*)\"/siU", $content, $links, PREG_SET_ORDER);
    foreach ($links as $link) {
        $link = "http://kino.to/aGET/Mirror/" . html_entity_decode($link[1]);
        $content = file_get_contents($link, false, getExplorerContext(getSiteHash()));
        if (strpos($content, "Megavideo.com")) {
            $fileLink = addMegavideoLink($link);
        } else {
            if (strpos($content, "Bitload.com (Flash)")) {
                $fileLink = addBitloadLink($link);
            } else {
                if (strpos($content, "Various (Flash)")) {
                    $fileLink = addNovamovLink($link);
                } else {
                    if (strpos($content, "Archiv.to (Flash)")) {
                        $fileLink = addArchivToFlash($link);
                    }
                }
            }
        }
        if ($fileLink) {
            echo $fileLink;
            return;
        }
    }
    //
    echo "ERROR";
}
function fetchPlayEpisode($host, $link)
{
    $template = new KinotoSeriesTemplate();
    //Recover info and set to template
    $template->setMovieTitle($_SESSION["serieTitle"]);
    $template->setImage($_SESSION["serieImage"]);
    $template->setDescription($_SESSION["serieDescription"]);
    $content = file_get_contents($link, false, getExplorerContext(getSiteHash()));
    switch ($host) {
        case "Megavideo.com":
            addMegavideoLink($template, $content);
            break;
        case "Bitload.com (Flash)":
            addBitloadLink($template, $content);
            break;
        case "Bitload.com (DivX)":
            addBitloadDivxLink($template, $content);
            break;
        case "Various (Flash)":
            addNovamovLink($template, $content);
            break;
        case "Archiv.to (Flash)":
            addArchivToFlash($template, $content);
            break;
        case "Archiv.to (DivX)":
            addArchivToDivx($template, $content);
            break;
    }
    $template->generateView(KinotoSeriesTemplate::VIEW_PLAY, $_SESSION["serieTitle"]);
}
define("SCRAPER_URL", SERVER_HOST_AND_PATH . "php/scraper/kinoto/");
if (isset($_GET["params"])) {
    $title = base64_decode($_GET["title"]);
    $params = base64_decode($_GET["params"]);
    $host = base64_decode($_GET["host"]);
    $image = base64_decode($_GET["image"]);
    $template = new KinotoTemplate();
    $template->setMovieTitle($title);
    $template->setImage($image);
    $url = "http://kino.to/aGET/Mirror/" . str_replace("&", "&", $params);
    switch ($host) {
        case "Megavideo.com":
            addMegavideoLink($template, $url);
            break;
        case "Bitload.com (Flash)":
            addBitloadLink($template, $url);
            break;
        case "Bitload.com (DivX)":
            addBitloadDivxLink($template, $url);
            break;
        case "Various (Flash)":
            addNovamovLink($template, $url);
            break;
        case "Archiv.to (Flash)":
            addArchivToFlash($template, $url);
            break;
        case "Archiv.to (DivX)":
            addArchivToDivx($template, $url);
            break;
    }
    $template->generateView(KinotoTemplate::VIEW_PLAY);