]> Pileus Git - vpaste/blob - index.cgi
Set dropdown using GET param
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009-2011 Andy Spencer
4 #
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU Affero General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
13 # details.
14
15 # Remove url codings from stdin
16 function get_modeline {
17         echo "$QUERY_STRING" |
18         sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&?]/ /g' |
19         xargs echo -e
20 }
21 function get_param {
22         get_modeline | awk -v "key=$1" 'BEGIN{RS=" "; FS="="}; $1 ~ key {print $2}'
23 }
24
25 # Extract an uploaded file from standard input
26 #   $1 is the name of the input to extract
27 function cut_file {
28         bnd="${CONTENT_TYPE/*boundary\=/}"
29         awk -v "want=$1" -v "bnd=$bnd" '
30                 BEGIN { RS="\r\n" }
31
32                 # reset based on boundaries
33                 $0 == "--"bnd""     { st=1; next; }
34                 $0 == "--"bnd"--"   { st=0; next; }
35                 $0 == "--"bnd"--\r" { st=0; next; }
36
37                 # search for wanted file
38                 st == 1 && $0 ~  "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
39                 st == 1 && $0 == "" { st=9; next; }
40
41                 # wait for newline, then start printing
42                 st == 2 && $0 == "" { st=3; next; }
43                 st == 3             { print $0    }
44         ' | head -c $((128*1024)) # Limit size to 128K
45 }
46
47 # Print out a generic header
48 function header {
49         echo "Content-Type: $1; charset=UTF-8"
50         echo
51 }
52
53 # Print plain message and exit
54 function message {
55         while [ "$1" == '-h' ]; do
56                 shift; echo "$1"; shift
57         done
58         header text/plain
59         echo "$*"
60         exit
61 }
62
63 # List previous pastes
64 function do_cmd {
65         header text/plain
66         case "$1" in
67         head)
68                 for i in $(ls -t db/*); do
69                         basename $i
70                         basename $i | sed 's/./-/g'
71                         sed '1,/^$/d; /^\s*$/d' $i | sed -n '1,5s/\(.\{0,60\}\).*/\1/p'
72                         echo
73                 done
74                 ;;
75         ls)
76                 ls -t db | column
77                 ;;
78         esac
79 }
80
81 # Format a file for viewing
82 function do_print {
83         if [ -f "./$1" ]; then
84                 input="$1"
85         elif [ -f "db/$1" ]; then
86                 input="db/$1"
87                 trim='1,/^$/d' # sed command to remove cruft
88         else
89                 message -h 'Status: 404 Not Found' \
90                         "File '$1' not found"
91         fi
92
93         # Check for raw paste
94         if [[ "$QUERY_STRING" == 'raw'* ||
95               "$REQUEST_URI"  != *'?'* &&
96               "$HTTP_ACCEPT"  != *'html'* ]]; then
97                 header text/plain
98                 sed "$trim" "$input"
99                 exit
100         fi
101
102         # Create a temp file with the provided modeline
103         output="$(mktemp)"
104         tmp="$(mktemp)"
105         sed "\$avim: $(get_modeline)" "$input" > "$tmp"
106
107         # - I have some plugins in ~/.vim
108         # - Run ex in screen to trick it into thinking that it
109         #   has a real terminal, note that we also have to set
110         #   term=xterm-256color in vimrc
111         HOME=/home/andy \
112         screen -D -m ex -nXZ -i NONE -u vimrc \
113                 '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
114                 '+sil! set iconstring= ruf= stl= tal=' \
115                 "+sil! set titlestring=$1\ -\ vpaste.net" \
116                 '+sil! set noml'     \
117                 '+sil! $d|'$trim     \
118                 '+sil! %s/\r//g' \
119                 '+sil! TOhtml'       \
120                 "+sav! $output" \
121                 '+qall!'        \
122                 "$tmp"
123
124         header text/html
125         cat "$output"
126         rm "$tmp" "$output"
127 }
128
129
130 # Upload handler
131 function do_upload {
132         body=$(cat -)
133         spam=$(echo -n "$body" | cut_file "ignoreme")
134         text=$(echo -n "$body" | cut_file "(text|x)")
135         [ ! -z "$spam" ] && message "Spam check.."
136         [   -z "$text" ] && message "No text pasted"
137
138         # Format and save message
139         output="$(mktemp db/XXXXX)"
140         cat >"$output" <<-EOF
141                 vim: $(get_modeline)
142                 Date: $(date -R)
143                 From: $REMOTE_ADDR
144
145                 $text
146         EOF
147
148         # Redirect user
149         uri="$url$(basename "$output")"
150         message -h 'Status: 302 Found' \
151                 -h "Location: $uri"    \
152                 "$uri"
153 }
154
155 # Default index page
156 function do_help {
157         filetypes=$(
158                 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
159                 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
160                 sort | uniq
161         )
162         uploads=$(ls -t db | head -n 5)
163         filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
164
165         header text/html
166         cat <<-EOF
167         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
168           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
169         <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
170             <head>
171                 <title>vpaste.net - Vim based pastebin</title>
172                 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
173                 <meta name="description" content="vpaste: Vim based pastebin" />
174                 <meta name="keywords" content="vpaste,paste,pastebin,vim" />
175                 <style type="text/css">
176                     * { margin:0; padding:0; }
177                     body { margin:1em; }
178                     h4 { margin:1em 0 0 0; }
179                     blockquote,dd,dl,p,pre,ul { margin:0 0 0 2em; }
180                     dt { font-weight:bold; padding:0.5em 0 0 0; }
181                     blockquote { width:50em; font-size:small; }
182                     span { font-family:monospace; }
183                 </style>
184             </head>
185             <body>
186                 <form id="form" method="post" action="" enctype="multipart/form-data">
187                 <div style="margin:0 0 1.5em 0;">
188                 <input style="display:none" type="text" name="ignoreme" value="" />
189                 <textarea name="text" cols="80" rows="25" style="width:100%; height:20em;"></textarea>
190                 <select onchange="document.getElementById('form').action =
191                     document.location + '?ft=' + this.value;">
192                         <option value="" disabled="disabled">Filetype</option>
193                         <option value="">None</option>
194                         $(for ft in $filetypes; do
195                                 echo "<option$(
196                                 [ "$ft" = "$filetype" ] &&
197                                         echo ' selected="selected"'
198                                 )>$ft</option>"
199                         done)
200                 </select>
201                 <input type="submit" value="Paste" />
202                 </div>
203                 </form>
204
205                 <h4>NAME</h4>
206                 <p>vpaste: Vim based pastebin</p>
207
208                 <h4>SYNOPSIS</h4>
209                 <div>
210                 <pre> vpaste file [option=value,..]</pre>
211                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
212                 <br />
213                 <pre> &lt;command&gt; | curl -F 'text=&lt;-' $url[?option=value,..]</pre>
214                 <br />
215                 <pre> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
216                 <pre> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
217                 </div>
218
219                 <h4>DESCRIPTION</h4>
220                 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
221                 <p>Options specified when uploading are stored as defaults.</p>
222
223                 <h4>OPTIONS</h4>
224                 <dl>
225                 <dt>ft, filetype={filetype}</dt>
226                 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
227                 <dt>fdm, foldmethod=(syntax|indent)</dt>
228                 <dd>Turn on dynamic code folding</dd>
229                 <dt>bg, background={light|dark}</dt>
230                 <dd>Background color to use for the page</dd>
231                 <dt>et, expandtab</dt>
232                 <dd>Expand tabs to spaces</dd>
233                 <dt>ts, tabstop=[N]</dt>
234                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
235                 <dt>...</dt>
236                 <dd>See :help modeline for more information</dd>
237                 </dl>
238
239                 <h4>BUGS</h4>
240                 <ul>
241                 <li>Using strange filetypes (ft=2html) may result in strange output.</li>
242                 <li><a href="mailto:andy753421@gmail.com?subject=vpaste bug">Other?</a></li>
243                 </ul>
244
245                 <h4>SOURCE</h4>
246                 <ul>
247                 <li><a href="vpaste?ft=sh">vpaste</a></li>
248                 <li><a href="index.cgi?ft=sh">index.cgi</a>
249                     <a href="vimrc?ft=vim">vimrc</a>
250                     <a href="htaccess?ft=apache">htaccess</a>
251                     <a href="robots.txt?ft=robots">robots.txt</a>
252                     <a href="sitemap.xml?ft=xml">sitemap.xml</a></li>
253                 <li><a href="2html.patch?ft=diff">2html.patch</a></li>
254                 <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
255                 </ul>
256
257                 <h4>LATEST UPLOADS</h4>
258                 <ul>$(for upload in ${uploads[@]}; do
259                     echo -n "<li>"
260                     echo -n "<span>$upload</span> "
261                     echo -n "<a href='$upload?raw'>text</a> "
262                     echo -n "<a href='$upload'>rainbow</a>"
263                     echo "</li>"
264                 done)
265                 </ul>
266                 <p><a href="ls">list all</a></p>
267                 <p><a href="head">sample all</a></p>
268             </body>
269         </html>
270         EOF
271 }
272
273 # Main
274 PATH=/bin:/usr/bin
275 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
276 pathinfo="${REQUEST_URI/*\/}"
277 pathinfo="${pathinfo/\?*}"
278
279 if [ "$pathinfo" = ls ]; then
280         do_cmd ls
281 elif [ "$pathinfo" = head ]; then
282         do_cmd head
283 elif [ "$pathinfo" ]; then
284         do_print "$pathinfo"
285 elif [ "$CONTENT_TYPE" ]; then
286         do_upload
287 else
288         do_help
289 fi