diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-03-25 21:08:29 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-03-25 21:08:29 +0200 |
| commit | 6d0777840e50ce98f3d96629b4e92bbdccd3001c (patch) | |
| tree | 161a753bf54b4fec0c4357b16dc68ff838e20383 /www | |
| parent | 568bf6f9a487a3dc33ce52e45cd31cfbea2cb79e (diff) | |
| download | phorkie-6d0777840e50ce98f3d96629b4e92bbdccd3001c.tar.gz phorkie-6d0777840e50ce98f3d96629b4e92bbdccd3001c.zip | |
first code that allows you to create pastes and view them
Diffstat (limited to 'www')
| -rw-r--r-- | www/.htaccess | 5 | ||||
| -rw-r--r-- | www/display.php | 42 | ||||
| -rw-r--r-- | www/index.php | 52 | ||||
| -rw-r--r-- | www/www-header.php | 36 |
4 files changed, 135 insertions, 0 deletions
diff --git a/www/.htaccess b/www/.htaccess index e69de29..f41e1bf 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -0,0 +1,5 @@ +RewriteEngine On +RewriteBase / +#RewriteCond %{REQUEST_FILENAME} -f + +RewriteRule ^([0-9]+)$ /display.php?id=$1 diff --git a/www/display.php b/www/display.php new file mode 100644 index 0000000..5bd06bb --- /dev/null +++ b/www/display.php @@ -0,0 +1,42 @@ +<?php +/** + * 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'); +} + +$files = glob($repoDir . '/*'); +$tplFiles = array(); +foreach ($files as $file) { + $tplFile = array(); + $tplFile['filename'] = basename($file); + $tplFile['type'] = get_type_from_file($file); + //FIXME: highlight + $tplFile['content'] = file_get_contents($file); + $tplFile['raw'] = '/' . $id . '/raw/' . $tplFile['filename']; + $tplFiles[] = $tplFile; +} + +render( + 'display', + array( + '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 e69de29..5276bf2 100644 --- a/www/index.php +++ b/www/index.php @@ -0,0 +1,52 @@ +<?php +/** + * Show paste creation form + * + * Elements: + * - description + * - file name (default: default.php) + * - content + * + * Creates and redirects to display page + */ +require_once 'www-header.php'; + +if (isset($_POST['file'])) { + //save + $repoDir = $GLOBALS['phorkie']['cfg']['repos']; + $n = count(glob($repoDir . '/*', GLOB_ONLYDIR)); + $dir = $repoDir . '/' . $n . '/'; + mkdir($dir, 0777);//FIXME + $vc = new VersionControl_Git($dir); + $vc->initRepository(); + file_put_contents($dir . '.git/description', $_POST['description']); + + foreach ($_POST['file'] as $num => $arFile) { + if ($arFile['name'] != '') { + $fname = $arFile['name']; + } else { + $fname = 'phork' . $num . '.' . $arFile['type']; + } + $fpath = $dir . $fname; + file_put_contents($fpath, $arFile['content']); + //fixme: let the class do that when it is able to + $command = $vc->getCommand('add') + ->addArgument($fname) + ->execute(); + } + $command = $vc->getCommand('commit') + ->setOption('message', 'initial paste') + ->execute(); + //redirect to phork + redirect($n); +} + +$phork = array( + '1' => array( + 'filename' => '', + 'content' => '', + 'type' => '' + ) +); +render('index', array('file' => $phork, 'description' => '')); +?>
\ No newline at end of file diff --git a/www/www-header.php b/www/www-header.php new file mode 100644 index 0000000..28607da --- /dev/null +++ b/www/www-header.php @@ -0,0 +1,36 @@ +<?php +require_once __DIR__ . '/../data/config.default.php'; +require_once 'VersionControl/Git.php'; +require_once 'Twig/Autoloader.php'; +Twig_Autoloader::register(); + +$loader = new Twig_Loader_Filesystem($GLOBALS['phorkie']['cfg']['tpl']); +$twig = new Twig_Environment( + $loader, + array( + //'cache' => '/path/to/compilation_cache', + 'debug' => true + ) +); + +function render($tplname, $vars) +{ + $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm'); + echo $template->render($vars); +} +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 |
