{% block title %}{{description}}{% endblock %}
{% block content %}
-<h1>{{description}}</h1>
-<p><a href="{{links.edit}}">edit</a></p>
-{% for file in files %}
+<h1>{{repo.getDescription}}</h1>
+<p><a href="{{repo.getLink('edit')}}">edit</a></p>
+{% for file in repo.getFiles %}
<div>
- <h2>{{file.filename}}</h2>
+ <h2>{{file.getFilename}}</h2>
<p>
- <a href="{{file.raw}}">raw</a>
+ <a href="{{file.getLink('raw')}}">raw</a>
</p>
- <pre>{{file.content}}</pre>
+ <pre>{{file.getContent}}</pre>
</div>
{% endfor %}
{% endblock %}
--- /dev/null
+<!DOCTYPE html>
+<html>
+ <head>
+ <!--<link rel="stylesheet" href="phorkie.css" />-->
+ <title>Error - Phorkie</title>
+ </head>
+ <body>
+ <article>
+ <h1>Error</h1>
+ <p>
+ {{exception.getMessage}}
+ </p>
+ </article>
+ </body>
+</html>
--- /dev/null
+<?php
+namespace Phorkie;
+
+class Exception extends \Exception
+{
+ public $httpStatusCode = 500;
+}
+
+?>
--- /dev/null
+<?php
+namespace Phorkie;
+
+/**
+ * Input from e.g. the URL is invalid, like a non-numeric string when one was
+ * expected
+ */
+class Exception_Input extends Exception
+{
+ public function __construct($msg = '', $code = 0)
+ {
+ parent::__construct($msg, $code);
+ $this->httpStatusCode = 400;
+ }
+}
+
+?>
--- /dev/null
+<?php
+namespace Phorkie;
+
+/**
+ * Something could not be found
+ */
+class Exception_NotFound extends Exception
+{
+ public function __construct($msg = '', $code = 0)
+ {
+ parent::__construct($msg, $code);
+ $this->httpStatusCode = 404;
+ }
+}
+
+?>
--- /dev/null
+<?php
+namespace Phorkie;
+
+class File
+{
+ /**
+ * Full path to the file
+ *
+ * @var string
+ */
+ public $path;
+
+ /**
+ * Repository this file belongs to
+ *
+ * @var string
+ */
+ public $repo;
+
+ public function __construct($path, Repository $repo)
+ {
+ $this->path = $path;
+ $this->repo = $repo;
+ }
+
+ /**
+ * Get filename relative to the repository path
+ *
+ * @return string
+ */
+ public function getFilename()
+ {
+ return basename($this->path);
+ }
+
+ /**
+ * Returns the type of the file, as used internally by Phorkie
+ *
+ * @return string
+ */
+ public function getType()
+ {
+ return substr($this->path, strrpos($this->path, '.') + 1);
+ }
+
+ public function getContent()
+ {
+ return file_get_contents($this->path);
+ }
+
+ /**
+ * Get a link to the file
+ *
+ * @param string $type Link type. Supported are:
+ * - "raw"
+ * - "display"
+ *
+ * @return string
+ */
+ public function getLink($type)
+ {
+ if ($type == 'raw') {
+ return '/' . $this->repo->id . '/raw/' . $this->getFilename();
+ }
+ throw new Exception('Unknown type');
+ }
+}
+
+?>
\ No newline at end of file
--- /dev/null
+<?php
+namespace Phorkie;
+
+
+class Repository
+{
+ /**
+ * Repository ID (number in repositories directory)
+ *
+ * @var integer
+ */
+ public $id;
+
+ /**
+ * Full path to the git repository
+ *
+ * @var string
+ */
+ public $repoDir;
+
+ /**
+ * Load Repository data from GET-Request
+ *
+ * @return void
+ *
+ * @throws Exception When something is wrong
+ */
+ public function loadFromRequest()
+ {
+ if (!isset($_GET['id'])) {
+ throw new Exception_Input('Paste ID missing');
+ }
+ if (!is_numeric($_GET['id'])) {
+ throw new Exception_Input('Paste ID not numeric');
+ }
+ $this->id = (int)$_GET['id'];
+
+ $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
+ if (!is_dir($repoDir)) {
+ throw new Exception_NotFound('Paste not found');
+ }
+ $this->repoDir = $repoDir;
+ }
+
+ /**
+ * Loads the list of files in this repository
+ *
+ * @return File[] Array of files
+ */
+ public function getFiles()
+ {
+ $files = glob($this->repoDir . '/*');
+ $arFiles = array();
+ foreach ($files as $path) {
+ $arFiles[] = new File($path, $this);
+ }
+ return $arFiles;
+ }
+
+ public function getDescription()
+ {
+ return file_get_contents($this->repoDir . '/.git/description');
+ }
+
+ /**
+ * Get a link to the repository
+ *
+ * @param string $type Link type. Supported are:
+ * - "edit"
+ * - "display"
+ *
+ * @return string
+ */
+ public function getLink($type)
+ {
+ if ($type == 'edit') {
+ return '/' . $this->id . '/edit';
+ } else if ($type == 'display') {
+ return '/' . $this->id;
+ }
+ throw new Exception('Unknown type');
+ }
+
+}
+
+?>
<?php
+namespace Phorkie;
/**
* Display paste contents
- *
*/
require_once 'www-header.php';
-if (!isset($_GET['id'])) {
- errout(400, 'Paste ID missing');
-}
-if (!is_numeric($_GET['id'])) {
- errout(400, 'Paste ID not numeric');
-}
-$id = (int)$_GET['id'];
-$repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $id;
-if (!is_dir($repoDir)) {
- errout(404, 'Paste not found');
-}
+$repo = new Repository();
+$repo->loadFromRequest();
-$files = glob($repoDir . '/*');
+/*
$tplFiles = array();
foreach ($files as $file) {
$tplFile = array();
$tplFile['raw'] = '/' . $id . '/raw/' . $tplFile['filename'];
$tplFiles[] = $tplFile;
}
+*/
render(
'display',
array(
+ 'repo' => $repo,
+ /*
'description' => file_get_contents($repoDir . '/.git/description'),
'files' => $tplFiles,
'links' => array(
'edit' => '/' . $id . '/edit'
)
+ */
)
);
?>
<?php
+namespace Phorkie;
/**
* Show paste creation form
*
<?php
+namespace Phorkie;
+set_include_path(
+ __DIR__ . '/../src/'
+ . PATH_SEPARATOR . get_include_path()
+);
+spl_autoload_register(
+ function ($class) {
+ $file = str_replace(array('\\', '_'), '/', $class) . '.php';
+ $hdl = @fopen($file, 'r', true);
+ if ($hdl !== false) {
+ fclose($hdl);
+ require $file;
+ }
+ }
+);
+set_exception_handler(
+ function ($e) {
+ if ($e instanceof Exception) {
+ header('HTTP/1.0 ' . $e->httpStatusCode);
+ } else {
+ header('HTTP/1.0 500 Internal server error');
+ }
+ render('exception', array('exception' => $e));
+ exit();
+ }
+);
+
require_once __DIR__ . '/../data/config.default.php';
require_once 'VersionControl/Git.php';
require_once 'Twig/Autoloader.php';
-Twig_Autoloader::register();
+\Twig_Autoloader::register();
-$loader = new Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
-$twig = new Twig_Environment(
+$loader = new \Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']);
+$twig = new \Twig_Environment(
$loader,
array(
//'cache' => '/path/to/compilation_cache',
header('Location: /' . $target);
exit();
}
-function errout($statusCode, $message)
-{
- header('HTTP/1.0 ' . $statusCode);
- echo $message;
- exit();
-}
-function get_type_from_file($file)
-{
- return substr($file, strrpos($file, '.') + 1);
-}
?>
\ No newline at end of file