]> Pileus Git - vpaste/blob - index.cgi
Use /bin/sh for vpaste script
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009-2013 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' \
19             -e 's/[^a-zA-Z0-9.:=_\-]/ /g' |
20         xargs echo -e
21 }
22 function get_param {
23         get_modeline | awk -v "key=$1" 'BEGIN{RS=" "; FS="="}; $1 ~ key {print $2}'
24 }
25
26 # Extract an uploaded file from standard input
27 #   $1 is the name of the input to extract
28 function cut_file {
29         bnd="${CONTENT_TYPE/*boundary\=/}"
30         awk -v "want=$1" -v "bnd=$bnd" '
31                 BEGIN { RS="\r\n" }
32
33                 # reset based on boundaries
34                 $0 == "--"bnd""     { st=1; next; }
35                 $0 == "--"bnd"--"   { st=0; next; }
36                 $0 == "--"bnd"--\r" { st=0; next; }
37
38                 # search for wanted file
39                 st == 1 && $0 ~  "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
40                 st == 1 && $0 == "" { st=9; next; }
41
42                 # wait for newline, then start printing
43                 st == 2 && $0 == "" { st=3; next; }
44                 st == 3             { print $0    }
45         ' | head -c $((128*1024)) # Limit size to 128K
46 }
47
48 # Respond to a request
49 function respond {
50         local allow ctype heads files texts gzip h f
51         while [ "$1" ]; do
52                 case $1 in
53                         -y) allow="true"; ;;
54                         -c) ctype="$2"; shift ;;
55                         -h) heads=("${heads[@]}" "$2"); shift ;;
56                         -f) files=("${files[@]}" "$2"); shift ;;
57                         *)  texts=("${texts[@]}" "$1"); ;;
58                 esac
59                 shift
60         done
61
62         # Check if the browser supports gzip
63         if [[ "$HTTP_ACCEPT_ENCODING" == *'gzip'* ]]; then
64                 gzip=true
65         fi
66
67         # Output header
68         if [ "$ctype" ]; then
69                 echo "Content-Type: $ctype; charset=UTF-8"
70         else
71                 echo "Content-Type: text/plain; charset=UTF-8"
72         fi
73         if [ "$gzip" ]; then
74                 echo "Content-Encoding: gzip"
75         fi
76         if [ "$allow" ]; then
77                 echo "Access-Control-Allow-Origin: *"
78                 echo "Access-Control-Allow-Headers: Content-Type"
79         fi
80         for h in "${heads[@]}"; do
81                 echo "$h"
82         done
83         echo
84
85         # Output text messages
86         if [ "$texts" ]; then
87                 if [ "$gzip" ]; then
88                         echo "${texts[@]}" | gzip
89                 else
90                         echo "${texts[@]}"
91                 fi
92                 exit
93         fi
94
95         # Output body files
96         if [ "$files" ]; then
97                 for f in "${files[@]}"; do
98                         if   [[   "$gzip" && "$f" != *'.gz' ]]; then
99                                 gzip < "$f"
100                         elif [[ ! "$gzip" && "$f" == *'.gz' ]]; then
101                                 zcat < "$f"
102                         else
103                                 cat "$f"
104                         fi
105                 done
106                 exit
107         fi
108
109         # Gzip remaining stream
110         if [ "$gzip" ]; then
111                 exec 1> >(gzip)
112         fi
113 }
114
115 # Format and output a file
116 function format {
117         # Create a temp file with the provided modeline
118         tmp="$(mktemp)"
119         sed -e  "1ivim: $(get_modeline)" \
120             -e "\$avim: $(get_modeline)" "$1" > "$tmp"
121
122         # Determine cache name
123         md5="$(cat index.cgi vimrc "$tmp" /usr/bin/ex | md5sum -b)"
124         out="cache/${md5% *}.htm"
125         zip="$out.gz"
126
127         # Cache the file, if needed
128         if [ ! -f "$zip" ]; then
129                 # - I have some plugins in ~/.vim
130                 # - Run ex in pty to trick it into thinking that it
131                 #   has a real terminal, note that we also have to set
132                 #   term=xterm-256color in vimrc
133                 HOME=/home/andy \
134                 /home/vpaste/bin/pty \
135                 /usr/bin/ex -nXZ -i NONE -u vimrc \
136                         '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
137                         '+sil! set iconstring= ruf= stl= tal=' \
138                         "+sil! set titlestring=$1\ -\ vpaste.net" \
139                         '+sil! set noml' \
140                         '+sil! 1d|$d|'$2 \
141                         '+sil! %s/\r//g' \
142                         '+sil! TOhtml'   \
143                         "+sav! $out"     \
144                         '+qall!'         \
145                         "$tmp" >/dev/null 2>&1
146                 gzip "$out"
147         fi
148         rm "$tmp"
149
150         # Output the file
151         respond -y -c "text/html" -f "$zip"
152 }
153
154 # List previous pastes
155 function do_cmd {
156         respond
157         case "$1" in
158         ls)
159                 ls -t db | column
160                 ;;
161         head)
162                 awk -v 'rows=4' -v 'cols=60' '
163                         FNR==1      { gsub(/.*\//, "", FILENAME);
164                                       print FILENAME
165                                       print "-----" }
166                         FNR==1,/^$/ { next }
167                         /\S/        { i++; printf "%."cols"s\n", $0 }
168                         i>=rows     { nextfile  }
169                         ENDFILE     { i=0; print ""  }
170                 ' $(ls -t db/*)
171                 ;;
172         stat)
173                 ls -l --time-style='+%Y %m' db |
174                 awk -v 'hdr=Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec' '
175                         BEGIN { printf "%64s\n", hdr }
176                         NR>1  { cnt[$6+0][$7+0]++ }
177                         END   { for (y in cnt) {
178                                   printf "%4d", y
179                                   for (m=1; m<=12; m++)
180                                     printf "%5s", cnt[y][m]
181                                   printf "\n" } }'
182                 ;;
183         esac
184 }
185
186 # Format a file for viewing
187 function do_print {
188         if [ -f "./$1" ]; then
189                 input="$1"
190         elif [ -f "db/$1" ]; then
191                 input="db/$1"
192                 trim='1,/^$/d' # sed command to remove cruft
193         else
194                 respond -h 'Status: 404 Not Found' \
195                         "File '$1' not found"
196         fi
197
198         # Check for javascript
199         if [[ "$input" == 'embed.js' &&
200               "$HTTP_ACCEPT" != *'html'* ]]; then
201                 respond -c text/javascript -f "$input"
202         fi
203
204         # Check for raw paste
205         if [[ "$QUERY_STRING" == 'raw'* ||
206               "$REQUEST_URI"  != *'?'* &&
207               ( "$input"       != 'db/'* ||
208                 "$HTTP_ACCEPT" != *'html'* ) ]]; then
209                 respond
210                 sed "$trim" "$input"
211                 exit
212         fi
213
214         # Output the file
215         format "$input" "$trim"
216 }
217
218 # Format a file for viewing
219 function do_view {
220         format -
221 }
222
223 # Upload handler
224 function do_upload {
225         body=$(cat -)
226         spam=$(echo -n "$body" | cut_file "ignoreme")
227         text=$(echo -n "$body" | cut_file "(text|x)")
228         bans=$(echo -n "$REMOTE_ADDR" | grep -f blacklist)
229         
230         [ ! -z "$spam" ] && respond -h "Status: 403 Forbidden" "Spam check.."
231         [ ! -z "$bans" ] && respond -h "Status: 403 Forbidden" "You have been banned"
232         [   -z "$text" ] && respond "No text pasted"
233
234         # Format and save message
235         output="$(mktemp db/XXXXX)"
236         cat >"$output" <<-EOF
237                 vim: $(get_modeline)
238                 Date: $(date -R)
239                 From: $REMOTE_ADDR
240                 User-Agent: $HTTP_USER_AGENT
241
242                 $text
243         EOF
244
245         # Redirect user
246         uri="$url$(basename "$output")"
247         respond -h 'Status: 302 Found' \
248                 -h "Location: $uri"    \
249                 "$uri"
250 }
251
252 # Default index page
253 function do_help {
254         filetypes=$(
255                 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
256                 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
257                 sort | uniq
258         )
259         uploads=$(ls -t db 2>/dev/null | head -n 5)
260         filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
261         vpaste='<a href="vpaste?ft=sh">vpaste</a>'
262         pileus='http://pileus.org/tools/vpaste'
263         gitweb='http://pileus.org/git/?p=vpaste'
264         mailman='http://pileus.org/mailman/listinfo/dev'
265         repo='git://pileus.org/vpaste'
266
267         respond -c text/html
268         cat <<-EOF
269         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
270           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
271         <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
272                 <head>
273                         <title>vpaste.net - Vim based pastebin</title>
274                         <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
275                         <meta name="description" content="vpaste: Vim based pastebin" />
276                         <meta name="keywords" content="vpaste,paste,pastebin,vim" />
277                         <meta name="google-site-verification" content="OvHF73zD7osJ1VSq9rJxnMFlja36944ud6CiP_iXQnI" />
278                         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
279                         <style type="text/css">
280                                 *          { margin: 0;
281                                              padding: 0; }
282                                 body       { font-family: sans-serif; }
283                                 input      { padding: 2px 6px 3px 6px; }
284                                 a          { text-decoration: none; }
285                                 /* Items */
286                                 textarea   { width: 100%; }
287                                 .box       { display: none;
288                                              border-top: solid 1px #888; }
289                                 /* box contents */
290                                 h1         { margin-top: 1.0em;
291                                              font-size: larger; }
292                                 ul,dd,dl,p { margin: 0 0 0 2em; }
293                                 dt         { font-weight: bold;
294                                              padding: 0.5em 0 0 0; }
295                                 .upload    { font-family: monospace; }
296                                 .cmds dd   { font-family: monospace; }
297                         </style>
298                         <style type="text/css" media="screen and (min-device-width:800px)">
299                                 body       { margin: 4em 8em; }
300                                 textarea   { margin-bottom: 0.5em; }
301                                 .buttons   { float: left; }
302                                 .links     { float: right; }
303                                 .links *   { margin-left: 0.5em; }
304                                 .box       { clear: both;
305                                              margin-top: 2.7em; }
306                         </style>
307                         <style type="text/css" media="screen and (max-device-width:800px)">
308                                 textarea, input, select, .links
309                                            { display: block;
310                                              width: 100%;
311                                              margin-bottom: 0.75em; }
312                                 body       { margin: 1em; }
313                                 textarea   { height: 17em; }
314                                 select     { height: 2em; }
315                                 input      { height: 3em; }
316                                 .links     { text-align: center; }
317                                 .links a   { margin: 0em 0.25em; }
318                                 #vpaste    { font-size: large; }
319                                 .sep       { display: block;
320                                              height: 0.5em;
321                                              visibility:hidden; }
322                         </style>
323                         <script type="text/javascript">
324                                 //<![CDATA[
325                                 function show(id) {
326                                         var boxes = document.getElementsByClassName('box')
327                                         for (var i = 0; i < boxes.length; i++) {
328                                                 var box = boxes[i]
329                                                 if (box.id == id && box.style.display != 'block')
330                                                         box.style.display = 'block'
331                                                 else
332                                                         box.style.display = "none"
333                                         }
334                                 }
335                                 function autoshow() {
336                                         var id  = document.location.toString().replace(/.*#/, '')
337                                         var box = document.getElementById(id)
338                                         if (box) box.style.display = "block"
339                                 }
340                                 //]]>
341                         </script>
342                 </head>
343
344                 <body onload="autoshow()">
345                         <form id="form" method="post" action="" enctype="multipart/form-data">
346                                 <div>
347                                         <input style="display:none" type="text" name="ignoreme" value="" />
348                                         <textarea name="text" cols="80" rows="25"></textarea>
349                                 </div>
350                                 <div class="buttons">
351                                         <select onchange="document.getElementById('form').action =
352                                                           document.location + '?ft=' + this.value;">
353                                                 <option value="" disabled="disabled">Filetype</option>
354                                                 <option value="">None</option>
355                                                 $(for ft in $filetypes; do
356                                                         echo "<option$(
357                                                         [ "$ft" = "$filetype" ] &&
358                                                                 echo ' selected="selected"'
359                                                         )>$ft</option>"
360                                                 done)
361                                         </select>
362                                         <input type="submit" value="Paste" />
363                                 </div>
364                                 <div class="links">
365                                         <a href="" id="vpaste">vpaste</a> <span class="sep">-</span>
366                                         <a href="#usage"   onclick="show('usage'  )">Usage</a>
367                                         <a href="#devel"   onclick="show('devel'  )">Development</a>
368                                         <a href="#uploads" onclick="show('uploads')">Uploads</a>
369                                 </div>
370                         </form>
371
372                         <div class="box" id="usage">
373                                 <h1>Pasting</h1>
374                                 <dl class="cmds">
375                                         <dt>From a shell</dt>
376                                         <dd> $vpaste file [option=value,..]</dd>
377                                         <dd> &lt;command&gt; | $vpaste [option=value,..]</dd>
378
379                                         <dt>From Vim</dt>
380                                         <dd> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
381                                         <dd> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
382
383                                         <dt>With curl</dt>
384                                         <dd> &lt;command&gt; | curl -F 'text=&lt;-' $url[?option=value,..]</dd>
385                                 </dl>
386
387                                 <h1>Options</h1>
388                                 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
389                                 <p>Options specified when uploading are saved as defaults.</p>
390
391                                 <dl>
392                                         <dt>bg, background={light|dark}</dt>
393                                         <dd>Background color to use for the page</dd>
394                                         <dt>et, expandtab</dt>
395                                         <dd>Expand tabs to spaces</dd>
396                                         <dt>fdm, foldmethod=(syntax|indent)</dt>
397                                         <dd>Turn on dynamic code folding</dd>
398                                         <dt>ft, filetype={filetype}</dt>
399                                         <dd>A filetype to use for highlighting, see above menu for supported types</dd>
400                                         <dt>nu, number</dt>
401                                         <dd>Add line numbers</dd>
402                                         <dt>ts, tabstop=[N]</dt>
403                                         <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
404                                         <dt>...</dt>
405                                         <dd>See :help modeline for more information</dd>
406                                 </dl>
407                         </div>
408
409                         <div class="box" id="devel">
410                                 <h1>License</h1>
411                                 <p>Copyright © 2009-2013
412                                    Andy Spencer &lt;andy753421@gmail.com&gt;</p>
413                                 <p>See individual files for licenses</p>
414
415                                 <h1>Source code</h1>
416                                 <dl>
417                                         <dt>Client</dt>
418                                         <dd><a href="vpaste?ft=sh">vpaste</a>
419                                             <a href="embed.js?ft=javascript">embed.js</a></dd>
420                                         <dt>Server</dt>
421                                         <dd><a href="index.cgi?ft=sh">index.cgi</a>
422                                             <a href="vimrc?ft=vim">vimrc</a>
423                                             <a href="htaccess?ft=apache">htaccess</a>
424                                             <a href="robots.txt?ft=robots">robots.txt</a>
425                                             <a href="sitemap.xml?ft=xml">sitemap.xml</a>
426                                             <a href="blacklist?raw">blacklist</a></dd>
427                                         <dt>Patches</dt>
428                                         <dd><a href="2html.patch?ft=diff">2html.patch</a></dd>
429                                         <dt>Development</dt>
430                                         <dd><a href="$pileus">homepage</a>
431                                             <a href="$gitweb">gitweb</a><br />
432                                             <code>git clone $repo</code></dd>
433                                 </dl>
434
435                                 <h1>Bugs</h1>
436                                 <ul>
437                                         <li>Using strange filetypes (ft=2html) may result in strange output.</li>
438                                         <li>Other issues can be sent to the pileus.org
439                                             <a href="$mailman">mailing list</a>.</li>
440                                 </ul>
441                         </div>
442
443                         <div class="box" id="uploads">
444                                 <h1>Recent Uploads</h1>
445                                 <ul>$(for upload in ${uploads[@]}; do
446                                     echo -n "<li>"
447                                     echo -n "<span class=\"upload\">$upload</span> "
448                                     echo -n "<a href='$upload?raw'>text</a> "
449                                     echo -n "<a href='$upload'>rainbow</a>"
450                                     echo "</li>"
451                                 done)
452                                 </ul>
453                                 <p><a href="ls">list all</a></p>
454                                 <p><a href="head">sample all</a></p>
455                                 <p><a href="stat">statistics</a></p>
456                         </div>
457                 </body>
458         </html>
459         EOF
460 }
461
462 # Main
463 PATH=/bin:/usr/bin
464 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
465 pathinfo="${REQUEST_URI/*\/}"
466 pathinfo="${pathinfo/\?*}"
467
468 if [ "$pathinfo" = ls ]; then
469         do_cmd ls
470 elif [ "$pathinfo" = head ]; then
471         do_cmd head
472 elif [ "$pathinfo" = stat ]; then
473         do_cmd stat
474 elif [ "$pathinfo" = view ]; then
475         do_view
476 elif [ "$pathinfo" ]; then
477         do_print "$pathinfo"
478 elif [ "$CONTENT_TYPE" ]; then
479         do_upload
480 else
481         do_help
482 fi