first code that allows you to create pastes and view them
authorChristian Weiske <cweiske@cweiske.de>
Sun, 25 Mar 2012 19:08:29 +0000 (21:08 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 25 Mar 2012 19:08:29 +0000 (21:08 +0200)
.gitignore [new file with mode: 0644]
README.rst
data/config.default.php
data/templates/base.htm [new file with mode: 0644]
data/templates/display.htm [new file with mode: 0644]
data/templates/index.htm [new file with mode: 0644]
www/.htaccess
www/display.php [new file with mode: 0644]
www/index.php
www/www-header.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..f82ddfd
--- /dev/null
@@ -0,0 +1,2 @@
+README.html
+repos
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ea8a940d1f8388b93e8bd370fab88955599538e7 100644 (file)
@@ -0,0 +1,37 @@
+************************************
+Phorkie - PHP and Git based pastebin
+************************************
+
+URLs
+====
+
+``/``
+  Index page. Shows form for new paste
+``/[0-9]+``
+  Display page for paste
+``/[0-9]/edit``
+  Edit the paste
+``/[0-9]+/raw/(.+)``
+  Display raw file contents
+``/[0-9]/delete``
+  Delete the paste
+``/search(/.+)?``
+  Search for term
+``/list(/[0-9])?``
+  List all pastes
+
+
+Internal directory layout
+=========================
+::
+
+  repos/
+    1/ - git repository for paste #1
+      .git/
+        description - Description for the repository
+    2/ - git repository for paste #2
+
+
+Search
+======
+Use ``ack-grep``
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ec686222284ed491b02fbe6135db18fda3ff2a26 100644 (file)
@@ -0,0 +1,6 @@
+<?php
+$GLOBALS['phorkie']['cfg'] = array(
+    'repos' => __DIR__ . '/../repos/',
+    'tpl'   => __DIR__ . '/templates/',
+);
+?>
\ No newline at end of file
diff --git a/data/templates/base.htm b/data/templates/base.htm
new file mode 100644 (file)
index 0000000..f406f00
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+ <head>
+  <!--<link rel="stylesheet" href="phorkie.css" />-->
+  <title>{% block title %}{% endblock %} - Phorkie</title>
+ </head>
+ <body>
+  <nav>
+   <ul>
+    <a href="/">New paste</a>
+    <a href="/list">List all</a>
+    <a href="/search">Search</a>
+   </ul>
+  </nav>
+  <article>{% block content %}{% endblock %}</article>
+ </body>
+</html>
\ No newline at end of file
diff --git a/data/templates/display.htm b/data/templates/display.htm
new file mode 100644 (file)
index 0000000..1f33b61
--- /dev/null
@@ -0,0 +1,16 @@
+{% extends "base.htm" %}
+{% block title %}{{description}}{% endblock %}
+
+{% block content %}
+<h1>{{description}}</h1>
+<p><a href="{{links.edit}}">edit</a></p>
+{% for file in files %}
+<div>
+ <h2>{{file.filename}}</h2>
+ <p>
+  <a href="{{file.raw}}">raw</a>
+ </p>
+ <pre>{{file.content}}</pre>
+</div>
+{% endfor %}
+{% endblock %}
diff --git a/data/templates/index.htm b/data/templates/index.htm
new file mode 100644 (file)
index 0000000..eea509b
--- /dev/null
@@ -0,0 +1,25 @@
+
+<form method="post" action="/">
+ <p>
+  <label for="description">Description</label>
+  <input type="text" name="description" id="description" value="{{description}}"/>
+ </p>
+ <p>
+  <label for="filename_1">Filename</label>
+  <input type="text" name="file[1][name]" id="filename_1" value="{{ file[1]['name'] }}"/>
+ </p>
+ <p>
+  <label for="type_1">Type</label>
+  <select name="file[1][type]" id="type_1">
+   <option value="css">CSS</option>
+   <option value="php">PHP</option>
+   <option value="xml">XML</option>
+  </select>
+ </p>
+ <p>
+  <textarea name="file[1][content]" id="content_1">{{ file[1]['content'] }}</textarea>
+ </p>
+ <p>
+  <input type="submit" value="Go"/>
+ </p>
+</form>
\ No newline at end of file
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..f41e1bf393ed8ff59f2b9ecda0f58d61235a3eee 100644 (file)
@@ -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 (file)
index 0000000..5bd06bb
--- /dev/null
@@ -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'
+        )
+    )
+);
+?>
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..5276bf2c96f6c14f436f34eec566006357317754 100644 (file)
@@ -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 (file)
index 0000000..28607da
--- /dev/null
@@ -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