<?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);

require __DIR__ . "/../includes/includes.php";
require "functions.php";

$obj = new db();

$url = str_replace("/amp/", "", $_SERVER['REQUEST_URI']);
$breadcrumb = explode("/", rtrim( $url ) );
$permalink = $breadcrumb[count($breadcrumb)-1];

$note = $obj->get_results("SELECT * FROM post WHERE pl LIKE '{$permalink}%'");
$note = count($note) ? $note[0] : false;
if(!$note) {
	header('HTTP/1.0 404 Not Found');
	exit();
}

$note_content = html_to_amp($note->contenido);
$note_content = preg_replace( '#<object.*?<\/object>#s', '', $note_content);

$note->imagen = validateImagen($note->imagen);

$dict_ = noteobj_to_notedict($note);

$dict = array_merge($dict_, array("NOTE_CONTENT" => $note_content));

$amp = build_amp($dict);

//header('Content-Type:text/plain');
header('Content-Type: text/html; charset=utf-8');
print $amp;


//DEBUG HELPERS

function consolelog( $log ) {
    header('Content-Type:text/plain');
    if( is_string( $log ) ) {
        echo $log;
    } else {
        var_dump( $log );
    }
    exit();
}

?>