]> Pileus Git - vpaste/blob - index.cgi
misc updates
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009-2012 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               ( "$input"       != 'db/'* ||
97                 "$HTTP_ACCEPT" != *'html'* ) ]]; then
98                 header text/plain
99                 sed "$trim" "$input"
100                 exit
101         fi
102
103         # Create a temp file with the provided modeline
104         output="$(mktemp)"
105         tmp="$(mktemp)"
106         sed "\$avim: $(get_modeline)" "$input" > "$tmp"
107
108         # - I have some plugins in ~/.vim
109         # - Run ex in screen to trick it into thinking that it
110         #   has a real terminal, note that we also have to set
111         #   term=xterm-256color in vimrc
112         HOME=/home/andy \
113         screen -D -m ex -nXZ -i NONE -u vimrc \
114                 '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
115                 '+sil! set iconstring= ruf= stl= tal=' \
116                 "+sil! set titlestring=$1\ -\ vpaste.net" \
117                 '+sil! set noml'     \
118                 '+sil! $d|'$trim     \
119                 '+sil! %s/\r//g' \
120                 '+sil! TOhtml'       \
121                 "+sav! $output" \
122                 '+qall!'        \
123                 "$tmp"
124
125         header text/html
126         cat "$output"
127         rm "$tmp" "$output"
128 }
129
130
131 # Upload handler
132 function do_upload {
133         body=$(cat -)
134         spam=$(echo -n "$body" | cut_file "ignoreme")
135         text=$(echo -n "$body" | cut_file "(text|x)")
136         [ ! -z "$spam" ] && message "Spam check.."
137         [   -z "$text" ] && message "No text pasted"
138
139         # Format and save message
140         output="$(mktemp db/XXXXX)"
141         cat >"$output" <<-EOF
142                 vim: $(get_modeline)
143                 Date: $(date -R)
144                 From: $REMOTE_ADDR
145
146                 $text
147         EOF
148
149         # Redirect user
150         uri="$url$(basename "$output")"
151         message -h 'Status: 302 Found' \
152                 -h "Location: $uri"    \
153                 "$uri"
154 }
155
156 # Default index page
157 function do_help {
158         filetypes=$(
159                 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
160                 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
161                 sort | uniq
162         )
163         uploads=$(ls -t db | head -n 5)
164         filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
165         vpaste='<a href="vpaste?ft=sh">vpaste</a>'
166
167         header text/html
168         cat <<-EOF
169         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
170           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
171         <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
172                 <head>
173                         <title>vpaste.net - Vim based pastebin</title>
174                         <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
175                         <meta name="description" content="vpaste: Vim based pastebin" />
176                         <meta name="keywords" content="vpaste,paste,pastebin,vim" />
177                         <meta name="google-site-verification" content="OvHF73zD7osJ1VSq9rJxnMFlja36944ud6CiP_iXQnI" />
178                         <style type="text/css">
179                                 *          { margin: 0;
180                                              padding: 0; }
181                                 body       { margin: 4em 8em 4em 8em;
182                                              font-family: sans-serif; }
183                                 /* Items */
184                                 textarea   { width: 100%;
185                                              margin-bottom: 0.5em; }
186                                 .buttons   { float: left; }
187                                 .links     { float: right; }
188                                 .links a   { text-decoration: none;
189                                              margin-left: 0.4em; }
190                                 .box       { display: none;
191                                              clear: both;
192                                              margin-top: 2.7em;
193                                              border-top: solid 1px #888; }
194                                 /* box contents */
195                                 h1         { margin-top: 1.0em;
196                                              font-size: larger; }
197                                 ul,dd,dl,p { margin: 0 0 0 2em; }
198                                 dt         { font-weight: bold;
199                                              padding: 0.5em 0 0 0; }
200                                 span       { font-family: monospace; }
201                                 .cmds dd   { font-family: monospace; }
202                         </style>
203                         <script>
204                                 function show(id) {
205                                         boxes = document.getElementsByClassName('box');
206                                         for (i=0; i<boxes.length; i++) {
207                                                 box = boxes[i]
208                                                 if (box.id == id && box.style.display != 'block')
209                                                         box.style.display = 'block'
210                                                 else
211                                                         box.style.display = "none"
212                                         }
213                                 }
214                         </script>
215                 </head>
216
217                 <body>
218                         <form id="form" method="post" action="" enctype="multipart/form-data">
219                                 <input style="display:none" type="text" name="ignoreme" value="" />
220                                 <textarea name="text" cols="80" rows="25"></textarea>
221                                 <div class="buttons">
222                                         <select onchange="document.getElementById('form').action =
223                                                           document.location + '?ft=' + this.value;">
224                                                 <option value="" disabled="disabled">Filetype</option>
225                                                 <option value="">None</option>
226                                                 $(for ft in $filetypes; do
227                                                         echo "<option$(
228                                                         [ "$ft" = "$filetype" ] &&
229                                                                 echo ' selected="selected"'
230                                                         )>$ft</option>"
231                                                 done)
232                                         </select>
233                                         <input type="submit" value="Paste" />
234                                 </div>
235                                 <div class="links">
236                                         <a href="javascript:show('usage'  )">Usage</a>
237                                         <a href="javascript:show('devel'  )">Development</a>
238                                         <a href="javascript:show('uploads')">Uploads</a>
239                                 </div>
240                         </form>
241
242                         <div class="box" id="usage">
243                                 <h1>Pasting</h1>
244                                 <dl class="cmds">
245                                         <dt>From a shell</dt>
246                                         <dd> $vpaste file [option=value,..]</dd>
247                                         <dd> &lt;command&gt; | $vpaste [option=value,..]</dd>
248
249                                         <dt>From Vim</dt>
250                                         <dd> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
251                                         <dd> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
252
253                                         <dt>With curl</dt>
254                                         <dd> &lt;command&gt; | curl -F 'text=&lt;-' $url[?option=value,..]</dd>
255                                 </dl>
256
257                                 <h1>Options</h1>
258                                 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
259                                 <p>Options specified when uploading are saved as defaults.</p>
260
261                                 <dl>
262                                         <dt>ft, filetype={filetype}</dt>
263                                         <dd>A filetype to use for highlighting, see above menu for supported types</dd>
264                                         <dt>fdm, foldmethod=(syntax|indent)</dt>
265                                         <dd>Turn on dynamic code folding</dd>
266                                         <dt>bg, background={light|dark}</dt>
267                                         <dd>Background color to use for the page</dd>
268                                         <dt>et, expandtab</dt>
269                                         <dd>Expand tabs to spaces</dd>
270                                         <dt>ts, tabstop=[N]</dt>
271                                         <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
272                                         <dt>...</dt>
273                                         <dd>See :help modeline for more information</dd>
274                                 </dl>
275                         </div>
276
277                         <div class="box" id="devel">
278                                 <h1>License</h1>
279                                 <p>Copyright © 2009-2012
280                                    Andy spencer &lt;andy753421@gmail.com&gt;</p>
281                                 <p>See individual files for license</p>
282
283                                 <h1>Source code</h1>
284                                 <ul>
285                                         <li><a href="vpaste?ft=sh">vpaste</a></li>
286                                         <li><a href="index.cgi?ft=sh">index.cgi</a>
287                                             <a href="vimrc?ft=vim">vimrc</a>
288                                             <a href="htaccess?ft=apache">htaccess</a>
289                                             <a href="robots.txt?ft=robots">robots.txt</a>
290                                             <a href="sitemap.xml?ft=xml">sitemap.xml</a></li>
291                                         <li><a href="2html.patch?ft=diff">2html.patch</a></li>
292                                         <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
293                                 </ul>
294
295                                 <h1>Bugs</h1>
296                                 <ul>
297                                         <li>Using strange filetypes (ft=2html) may result in strange output.</li>
298                                         <li><a href="mailto:andy753421@gmail.com?subject=vpaste bug">Other?</a></li>
299                                 </ul>
300                         </div>
301
302                         <div class="box" id="uploads">
303                                 <h1>Recent Uploads</h1>
304                                 <ul>$(for upload in ${uploads[@]}; do
305                                     echo -n "<li>"
306                                     echo -n "<span>$upload</span> "
307                                     echo -n "<a href='$upload?raw'>text</a> "
308                                     echo -n "<a href='$upload'>rainbow</a>"
309                                     echo "</li>"
310                                 done)
311                                 </ul>
312                                 <p><a href="ls">list all</a></p>
313                                 <p><a href="head">sample all</a></p>
314                         </div>
315                 </body>
316         </html>
317         EOF
318 }
319
320 # Main
321 PATH=/bin:/usr/bin
322 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
323 pathinfo="${REQUEST_URI/*\/}"
324 pathinfo="${pathinfo/\?*}"
325
326 if [ "$pathinfo" = ls ]; then
327         do_cmd ls
328 elif [ "$pathinfo" = head ]; then
329         do_cmd head
330 elif [ "$pathinfo" ]; then
331         do_print "$pathinfo"
332 elif [ "$CONTENT_TYPE" ]; then
333         do_upload
334 else
335         do_help
336 fi