]> Pileus Git - vpaste/blob - index.cgi
adding vpaste and some docs
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009 Andy Spencer
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # Remove url codings form stdin
16 function urldecode {
17         sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e
18 }
19
20 # Extract an uploaded file from standard input
21 # $1 is the boundary delimiter for the file
22 function cut_file {
23         awk "
24                 /--$1/          {st=1};
25                 st==2           {print \$0};
26                 /$1--/          {st=0};
27                 /^\\r$/ && st==1 {st=2};
28         " | head -c -2
29         # Remove trailing ^M's that come with CGI
30 }
31
32 # Format a file for viewing 
33 function do_print {
34         [ -f "$1" ] && input="$1" || input="db/$1" 
35         output="$(mktemp)"
36         modeline="$(echo $QUERY_STRING | urldecode)"
37
38         # I have some plugins in ~/.vim
39         #
40         # Run vimhi.sh in screen to trick it into thinking
41         # that it has a real terminal, not that we also have to
42         # set term=xterm-256color in vimrc
43         HOME=/home/andy \
44         screen -D -m ./vimhi.sh "$input" "$output" "$modeline"
45         cat "$output" 
46 }
47
48
49 # Upload handler
50 function do_upload {
51         output="$(mktemp db/XXXXX)"
52         uri="$SCRIPT_URI$(basename "$output")"
53         cut_file "$1" > "$output"
54         echo "$uri"
55 }
56
57 # Default index page
58 function do_help {
59 cat - <<EOF
60 <html>
61         <body>
62         <p>Usage:</p>
63         <pre>   cat foo | curl -F 'x=<-' $SCRIPT_URI</pre>
64         <p>Source:</p>
65         <ul>
66         <li><a href="index.cgi?ft=sh">index.cgi</a>
67         <li><a href="vimhi.sh?ft=sh">vimhi.sh</a>
68         <li><a href="vimrc?ft=vim">vimrc</a>
69         <li><a href="htaccess?ft=apache">htaccess</a>
70         </ul>
71         <p>Latest uploads:</p><ul>
72         $(for i in $(ls -t db | head -n 5); do
73                 uri="$SCRIPT_URI$i"
74                 echo "<li><a href='$uri'>$uri</a>"
75         done)
76         </ul>
77         </body>
78 </html>
79 EOF
80 }
81
82 # Main
83 pathinfo="${SCRIPT_URL/*vpaste\/}"
84 boundary="${CONTENT_TYPE/*boundary\=/}"
85
86 # Print out a generic header
87 echo Content-Type: text/html; charset=UTF-8
88 echo
89
90 if [ "$pathinfo" ]; then
91         do_print "$pathinfo"
92 elif [ "$CONTENT_TYPE" ]; then
93         do_upload "$boundary"
94 else
95         do_help
96 fi