Kontakt - Datenschutz

Subversion

<?php
/**
 * ydlinks: atom feed
 *
 * @category  YD
 * @package   Web
 * @author    Sven Drieling <sd@sven-drieling.de>
 * @copyright 2014 Sven Drieling
 * @license   http://opensource.org/licenses/mit-license.php MIT license
 * @version   0.1.0alpha1
 */

namespace YD\Web\Links;


require_once __DIR__ . '/links_config.php';

// Improve security of session cookie
ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_secure'   , $linksConfig['cookieSecure']);
ini_set('session.cookie_httponly' , 1);


session_start();
if(!isset($_SESSION['isLogin'])) {
    $_SESSION['isLogin'] = false;
}


// Simple maintenance mode
if($linksConfig['maintenance'] and !$_SESSION['isLogin']) {
    $page['httpStatus']  = 503;
    $page['httpMessage'] = 'Service Unavailable';

    if('cgi-fcgi' === php_sapi_name()) { // TODO Check more sapis?
        header("Status: {$page['httpStatus']} {$page['httpMessage']}");
    } else {
        // TODO Check $_SERVER['SERVER_PROTOCOL']
        header("{$_SERVER['SERVER_PROTOCOL']} {$page['httpStatus']} {$page['httpMessage']}");
    }
    echo "Maintenance Mode.\n";
    exit;
}

require_once __DIR__ . '/Link.php';
require_once __DIR__ . '/Links.php';


$links = new \YD\Web\Links();
$links->view = function($link) use ($links) {
    $link->setEscapeCallback(function($string) {
        return \htmlspecialchars($string, ENT_QUOTES | ENT_XML1, 'UTF-8');
    });

    $date    = new \DateTime($link->created, new \DateTimeZone('UTC'));
    $created = $date->format(\DateTime::ATOM);


    /*
    http://web.archive.org/web/20080414203155/http://diveintomark.org/archives/2004/05/27/howto-atom-linkblog
    $result = <<<XML
<entry>
  <title type='html'><![CDATA[{$link->title}]]></title>
  <link rel='alternate' type='text/html' hreflang='de' href='http://www.sven-drieling.de/bookmarks/{$link->id}'/>
  <id>http://www.sven-drieling.de/bookmarks/{$link->id}</id>
  <published>{$created}</published>
  <summary type='html'><![CDATA[{$link->description}]]></summary>
  <link rel='related' type='text/html'
    href='{$link->§uri}'
    title='{$link->§title}'/>
</entry>

XML;
    */

    $result = <<<XML
<entry>
  <title type='html'><![CDATA[{$link->title}]]></title>
  <link rel='alternate' type='text/html' href='{$link->§uri}'/>
  <id>http://www.sven-drieling.de/bookmarks/{$link->id}</id>
  <published>{$created}</published>
  <updated>{$created}</updated>
  <summary type='html'><![CDATA[{$link->description}]]></summary>
</entry>

XML;


    return $result;
};



// Page content
$page = ['httpStatus'   => 200,
         'httpMessage'  => 'OK',

         'title'        => 'Bookmarks - Sven Drieling',
         'message'      => ['type'    => 'info',
                            'content' => ''],
         'lastModified' => '',
         'navigation'   => '',
         'content'      => '',
         'debug'        => ''];


// Command API
$command   = 'show';
$arguments = [];

$_POST = \array_map('trim', $_POST);

$template = $linksConfig['templateAtomFeed'];

try {
    switch($command) {
        case 'show':
                $date = new \DateTime($links->getLastModified(), new \DateTimeZone('UTC'));
                $page['lastModified'] = $date->format(\DateTime::ATOM);
                $page['content']      = $links->showLastX(20);
            break;


        default:
            $template = $linksConfig['templatePage'];

            $page['title']       = 'Error';
            $page['content']     = '<p>Unknown command.</p>';
            $page['httpStatus']  = 400;
            $page['httpMessage'] = 'Bad Request';
            break;
    }
} catch(\Exception $e) {
    $template = $linksConfig['templatePage'];

    $page['title']       = 'Error';
    $page['content']     = '<p>Internal Server Error</p>';
    $page['httpStatus']  = 500;                 // TODO Status code?
    $page['httpMessage'] = 'Internal Server Error';
}


$message = "<div id='message'>{$page['message']['content']}</div>"; // TODO Message type, CSS

$html = \str_replace('<v:title>',        $page['title'],        $template);
$html = \str_replace('<v:message>',      $message,              $html);
$html = \str_replace('<v:lastModified>', $page['lastModified'], $html);
$html = \str_replace('<v:navigation>',   $page['navigation'],   $html);
$html = \str_replace('<v:content>',      $page['content'],      $html);

if('cgi-fcgi' === php_sapi_name()) { // TODO Check more sapis?
    header("Status: {$page['httpStatus']} {$page['httpMessage']}");
} else {
    // TODO Check $_SERVER['SERVER_PROTOCOL']
    header("{$_SERVER['SERVER_PROTOCOL']} {$page['httpStatus']} {$page['httpMessage']}");
}


// Send errors as HTML
if(200 == $page['httpStatus']) {
    header('Content-Type: application/atom+xml; charset=utf-8');
} else {
    header('Content-Type: text/html; charset=utf-8');
}
header('Content-Length: ' . strlen($html));
echo $html;
?>