<commit_before><?php
/**
 * index.php is supposed to be executed only by crawlers! Otherwise index.html is displayed
 * the logic is controlled in .htaccess
 */

// this is a crawler -> show simplified site with just the essential data
$index = file_get_contents('index.html');

$phpself = $_SERVER['REQUEST_URI'];

if (preg_match("/\/view\//i", $phpself)) {

    $id = (int)end(explode("/", $phpself));

    $ogStartTag = '<!-- PHP-REWRITE:ogTags -->';
    $ogEndTag = '<!-- /PHP-REWRITE -->';

    $posStart = strpos($index, $ogStartTag);
    $posEnd = strpos($index, $ogEndTag);

    // This is for showing single voluntaryWorkItem details
    $detailsTag = '<!-- PHP-REWRITE:vwDetails/ -->';
    $posDetails = strpos($index, $detailsTag);
    $toReplace = substr($index, $posDetails, strlen($detailsTag));

    // Fetch data from API...
    $ch = curl_init();
    // TODO: change this to live server
    curl_setopt($ch, CURLOPT_URL, "http://jaffatron.com:5000/voluntary_works/" . $id);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $out = json_decode($output);
    $vw = $out->voluntary_works[0];
    $muni = $out->linked->municipalities[0]->name;
    $type = strtolower($out->linked->types[0]->name);

    if ($posDetails !== FALSE) {

        $detailSegment = '
        <h1>'. $vw->name .'</h1>
        <p>'. $vw->description .'</p>
        <h3>'. $muni .'</h3>
        <p>'. $type .'</p>
        ';

        $index = str_replace($toReplace, $detailSegment, $index);
    }

    if ($posStart !== FALSE && $posEnd !== FALSE) {

        // Tags were found, start constructing replacement
        $replacementLength = ($posEnd - $posStart + strlen($ogEndTag));
        $toReplace = substr($index, $posStart, $replacementLength);
        $ogTag = '<meta property="og:%s" content="%s"/>';

        // TODO: Construct data based on the fetched details
        $ogData = array(
            'title' => 'Talkoopäivä 17.5. '.$vw->name.', '.$muni,
            'url' => 'http://talkoot.fi/hakemisto/view/' . $id,
            'image' => 'http://talkoot.fi/hakemisto/app/img/icons/'. $type .'.png',
            'description' => $vw->description
        );

        $replacement = '';
        foreach ($ogData as $key => $value) {
            $replacement .= sprintf($ogTag, $key, $value) . "\n";
        }

        $index = str_replace($toReplace, $replacement, $index);
    }
}
// TODO: the above info could be cached, if possible, to avoid extra roundtrip
echo($index);

?><commit_msg>Reference of-tags always same host as requested<commit_after><?php
/**
 * index.php is supposed to be executed only by crawlers! Otherwise index.html is displayed
 * the logic is controlled in .htaccess
 */

// this is a crawler -> show simplified site with just the essential data
$index = file_get_contents('index.html');

$phpself = $_SERVER['REQUEST_URI'];
$host = $_SERVER['HTTP_HOST'];

if (preg_match("/\/view\//i", $phpself)) {

    $id = (int)end(explode("/", $phpself));

    $ogStartTag = '<!-- PHP-REWRITE:ogTags -->';
    $ogEndTag = '<!-- /PHP-REWRITE -->';

    $posStart = strpos($index, $ogStartTag);
    $posEnd = strpos($index, $ogEndTag);

    // This is for showing single voluntaryWorkItem details
    $detailsTag = '<!-- PHP-REWRITE:vwDetails/ -->';
    $posDetails = strpos($index, $detailsTag);
    $toReplace = substr($index, $posDetails, strlen($detailsTag));

    // Fetch data from API...
    $ch = curl_init();
    // TODO: change this to live server
    curl_setopt($ch, CURLOPT_URL, "http://jaffatron.com:5000/voluntary_works/" . $id);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $out = json_decode($output);
    $vw = $out->voluntary_works[0];
    $muni = $out->linked->municipalities[0]->name;
    $type = strtolower($out->linked->types[0]->name);

    if ($posDetails !== FALSE) {

        $detailSegment = '
        <h1>'. $vw->name .'</h1>
        <p>'. $vw->description .'</p>
        <h3>'. $muni .'</h3>
        <p>'. $type .'</p>
        ';

        $index = str_replace($toReplace, $detailSegment, $index);
    }

    if ($posStart !== FALSE && $posEnd !== FALSE) {

        // Tags were found, start constructing replacement
        $replacementLength = ($posEnd - $posStart + strlen($ogEndTag));
        $toReplace = substr($index, $posStart, $replacementLength);
        $ogTag = '<meta property="og:%s" content="%s"/>';

        // TODO: Construct data based on the fetched details
        $ogData = array(
            'title' => 'Talkoopäivä 17.5. '.$vw->name.', '.$muni,
            'url' => 'http://' . $host .'/hakemisto/view/' . $id,
            'image' => 'http://' . $host .'/hakemisto/app/img/icons/'. $type .'.png',
            'description' => $vw->description
        );

        $replacement = '';
        foreach ($ogData as $key => $value) {
            $replacement .= sprintf($ogTag, $key, $value) . "\n";
        }

        $index = str_replace($toReplace, $replacement, $index);
    }
}
// TODO: the above info could be cached, if possible, to avoid extra roundtrip
echo($index);

?>
{'fraction_non_alphanumeric': 0.1891156462585034, 'fraction_numerical': 0.0058309037900874635, 'mean_word_length': 2.688888888888889, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1906956', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1697, 'n_words': 400}