diff options
| -rw-r--r-- | data/templates/display.htm | 12 | ||||
| -rw-r--r-- | data/templates/exception.htm | 15 | ||||
| -rw-r--r-- | src/Phorkie/Exception.php | 9 | ||||
| -rw-r--r-- | src/Phorkie/Exception/Input.php | 17 | ||||
| -rw-r--r-- | src/Phorkie/Exception/NotFound.php | 16 | ||||
| -rw-r--r-- | src/Phorkie/File.php | 69 | ||||
| -rw-r--r-- | src/Phorkie/Repository.php | 86 | ||||
| -rw-r--r-- | www/display.php | 21 | ||||
| -rw-r--r-- | www/index.php | 1 | ||||
| -rw-r--r-- | www/www-header.php | 43 |
10 files changed, 257 insertions, 32 deletions
diff --git a/data/templates/display.htm b/data/templates/display.htm index 1f33b61..ea2e2cd 100644 --- a/data/templates/display.htm +++ b/data/templates/display.htm @@ -2,15 +2,15 @@ {% 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 %} diff --git a/data/templates/exception.htm b/data/templates/exception.htm new file mode 100644 index 0000000..805342a --- /dev/null +++ b/data/templates/exception.htm @@ -0,0 +1,15 @@ +<!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> diff --git a/src/Phorkie/Exception.php b/src/Phorkie/Exception.php new file mode 100644 index 0000000..03031ef --- /dev/null +++ b/src/Phorkie/Exception.php @@ -0,0 +1,9 @@ +<?php +namespace Phorkie; + +class Exception extends \Exception +{ + public $httpStatusCode = 500; +} + +?> diff --git a/src/Phorkie/Exception/Input.php b/src/Phorkie/Exception/Input.php new file mode 100644 index 0000000..2b675c1 --- /dev/null +++ b/src/Phorkie/Exception/Input.php @@ -0,0 +1,17 @@ +<?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; + } +} + +?> diff --git a/src/Phorkie/Exception/NotFound.php b/src/Phorkie/Exception/NotFound.php new file mode 100644 index 0000000..a62d1f1 --- /dev/null +++ b/src/Phorkie/Exception/NotFound.php @@ -0,0 +1,16 @@ +<?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; + } +} + +?> diff --git a/src/Phorkie/File.php b/src/Phorkie/File.php new file mode 100644 index 0000000..f12d837 --- /dev/null +++ b/src/Phorkie/File.php @@ -0,0 +1,69 @@ +<?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 diff --git a/src/Phorkie/Repository.php b/src/Phorkie/Repository.php new file mode 100644 index 0000000..ae01f6c --- /dev/null +++ b/src/Phorkie/Repository.php @@ -0,0 +1,86 @@ +<?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'); + } + +} + +?> diff --git a/www/display.php b/www/display.php index 5bd06bb..b6b7d7f 100644 --- a/www/display.php +++ b/www/display.php @@ -1,23 +1,14 @@ <?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(); @@ -28,15 +19,19 @@ foreach ($files as $file) { $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' ) + */ ) ); ?> diff --git a/www/index.php b/www/index.php index 5276bf2..31aef7d 100644 --- a/www/index.php +++ b/www/index.php @@ -1,4 +1,5 @@ <?php +namespace Phorkie; /** * Show paste creation form * diff --git a/www/www-header.php b/www/www-header.php index 28607da..8b99234 100644 --- a/www/www-header.php +++ b/www/www-header.php @@ -1,11 +1,38 @@ <?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', @@ -23,14 +50,4 @@ function redirect($target) 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 |
