public
Description: PHP web notepad, like a wiki, saves every 2 seconds
Homepage: http://edit.sunfox.org/
Clone URL: git://github.com/sunny/edith.git
edith / index.php
100644 126 lines (96 sloc) 2.953 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* Edith's dispatching controller.
* RESTFULly answers to GET, HEAD, POST, PUT and DELETE to these resources:
* /{pagename}
* /{pagename}/{representation}
*/
 
@include 'config.php';
if (!defined('EDITH_URI'))
  die('Please copy config.php.example to config.php');
if (!is_dir(EDITH_DATA_PATH))
  die(EDITH_DATA_PATH . " is not a directory");
 
// mime types to send for each template
$TEMPLATES = array(
  'html' => 'text/html',
  'txt' => 'text/plain'
);
 
// include libraries
require 'lib/helpers.php';
require 'lib/page.class.php';
 
 
// find page and repr from request
$method = $_SERVER['REQUEST_METHOD'];
$request_uri = substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['PHP_SELF'])));
preg_match('#^/?([^/]+?)(?:/(.+))?/?$#', $request_uri, $request_matches);
 
$page = new Page($request_matches[1]);
$page_exists = $page->exists();
 
$representation = $request_matches[2];
 
// don't allow pages with unsafe names
if (!$page->has_safe_name()) {
  header('HTTP/1.0 404 Not Found');
  exit('The page name can only contain dashes, dots and alphanumerical characters.');
}
 
// {pagename}/{representation}
if ($representation != '') {
 
  if (!$page_exists) {
    header('HTTP/1.0 404 Not Found');
    die("404 Not Found: $page->name");
  }
 
  if (!isset($TEMPLATES[$representation])) {
    header('HTTP/1.0 404 Not Found');
    $representations = implode(array_keys($TEMPLATES), ', ');
    die("Representation can only be one of: $representations.");
  }
 
  switch ($method) {
 
    case 'GET': case 'HEAD':
      header('Content-type: '.$TEMPLATES[$representation]);
      if ($method == 'HEAD')
        exit;
      $page->load();
      require "templates/$representation.php";
      exit;
 
    case 'POST': case 'PUT': case 'DELETE':
      header('HTTP/1.0 405 Method Not Allowed');
      header('Allow: GET, HEAD');
      exit;
 
    default:
      header('HTTP/1.0 501 Not Implemented');
      header('Allow: GET, HEAD');
      exit;
 
  }
}
 
// /{pagename}
 
 
switch ($method) {
 
  case 'GET': case 'HEAD':
    if (!$page_exists)
      header('HTTP/1.0 404 Not Found');
 
    header('Content-type: text/html');
 
    if ($method == 'GET') {
      $page->load();
      $template = (!$page_exists or $page->is_writeable()) ? 'default' : 'html';
      require "templates/$template.php";
    }
 
    exit;
 
  case 'DELETE':
    if (!$page_exists)
      header('HTTP/1.0 404 Not Found');
    else
      $page->delete();
    exit;
 
  case 'PUT': case 'POST':
    $page->text = request_var('text');
    if (!$page->save()) {
      header('HTTP/1.0 500 Internal Server Error');
      die('Error saving page.');
    }
 
    if (!$page_exists)
      header('HTTP/1.0 201 Created');
    if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
      exit('Saved successfully!');
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    exit;
 
  default:
    header('HTTP/1.0 501 Not Implemented');
    header('Allow: GET, HEAD');
    exit;
 
}