<?php
/*
 * Serverside PhP script for requesting parsers for DSBDirect
 * Copyright (C) 2019 Moritz Zwerger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


//vars
$root = '/users/fynn/parserfiles'; //folder where to save the tables
//max length for url content
$maxFileSize = 200000; //200kb

//check if url argument is passed
if (!isset($_POST['url'])) {
    //fail because of malformed request
    fail();
}

$url = $_POST['url'];

//check if url is valid url
if (!filter_var($url, FILTER_VALIDATE_URL)) {
    //not valid
    fail();
}

//check if url is from dsb
if (!startsWith($url, 'https://app.dsbcontrol.de/data/') && !startsWith($url, 'https://light.dsbcontrol.de/DSBlightWebsite/Data/')) {
    // url is invalid, we only allow domains from dsb
    fail();
}


//check if data folder exists. if not, create it

if (!is_dir($root) && !mkdir($root, 0770, true)) {
    //unable to create
    fail();
}

$content = file_get_contents($url, false, null, 0, $maxFileSize);
//check if content is available
if ($content === false) {
    //error
    fail();
}
//set path and generate hash value
$path = $root . '/' . sha1($content) . ".htm";

//check if file already exists
if (file_exists($path)) {
    //file already exists. No need to overwrite it.
    die("ok");
}

//write to file
$file = fopen($path, "w") or fail(); //open file in write mode. If it fails throw error
fwrite($file, $content); //write
fclose($file); //close file handler
chmod($path, 0770); //change permission

//ok. return
die("ok");


function fail()
{
    http_response_code(400);
    die('fail');
}

function startsWith($string, $startString)
{
    $len = strlen($startString);
    return (substr($string, 0, $len) === $startString);
}
