diff options
author | bie <bie@pha.hk> | 2014-04-14 19:52:40 +0200 |
---|---|---|
committer | bie <bie@pha.hk> | 2014-04-14 19:52:40 +0200 |
commit | e5a20e6113a0a1e303cffa54945d120de3ce31b2 (patch) | |
tree | ac3dbff93f9073b14f387fc6f6b488130938375a /tears.cgi | |
download | tears-e5a20e6113a0a1e303cffa54945d120de3ce31b2.tar.xz |
dette er kode...?
Diffstat (limited to 'tears.cgi')
-rwxr-xr-x | tears.cgi | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/tears.cgi b/tears.cgi new file mode 100755 index 0000000..6698f07 --- /dev/null +++ b/tears.cgi @@ -0,0 +1,222 @@ +#!/usr/local/bin/haserl --shell=/bin/bash + +<% + +VERSION=0.1 +BASE="/~bie/tears" +HOST="low.radionova.no" + +path=$(pdecode <<<"${PATH_INFO#/}") + +error() { + echo -e "Content-Type: text/plain; charset=utf-8\n" + echo "$1" + exit 1 +} + +irc() { + teatime -c '#radionova' "$1" +} + +byline() { + echo # noe som fungerer både på lunix og bsd her +} + +info() { + stat -c '%n: %U %G %a %Y' $1 +} + +link() { + [ "$BASE/$2" = $REQUEST_URI ] && echo "$1" || echo "<a href='$BASE/$2'>$1</a>" +} + +qlink() { + text="$1" && url="$BASE/$2" && shift 2 + IFS=\& && echo -n "<a href='$url?$*'>$text</a>" +} + +redirect() { + echo "Content-Type: text/html" + echo -e "Location: https://$HOST$1\n" + echo "٩(͡๏̯͡๏)۶>" +} + +kalender() { + [ "$action" = index.html -o "$action" = ditt -o "$action" = gruppe ] || return + echo "<p><b>for mye? reis i tid…</b>" + IFS=- read y m d <<<"$GET_dato" + if [ -z "$y" ]; then + for directory in $(cut -d / -f 2 | sort | uniq); do + year=$(basename $directory) + echo "<a href='?dato=$year'>$year</a>" + done + elif [ -z "$m" ]; then + echo "<b><a href='?'>$y</a></del></b><br>" + for directory in $(cut -d / -f 3 | sort | uniq); do + month=$(basename $directory) + echo "<a href='?dato=$y-$month'>$month</a>" + done + elif [ -z "$d" ]; then + echo "<b><a href='?'>$y</a>/<a href='?dato=$y'>$m</a></b><br>" + for directory in $(cut -d / -f 4 | sort | uniq); do + day=$(basename $directory) + echo "<a href='?dato=$y-$m-$day'>$day</a>" + done + else + echo "<b><a href='?'>$y</a>/<a href='?dato=$y'>$m</a>/<a href='?dato=$y-$m'>$d</a></b>" + fi +} + +meny() { + echo -e "Content-Type: text/html; charset=utf-8\n" + echo "<!doctype html>" + echo "<title>tears</title>" + echo "<link href=$BASE/tears.css rel=stylesheet>" + echo "<script src=$BASE/tears.js></script>" + echo "<div id='meny'>" + link "<pre>$(figlet -f threepoint tears)</pre>" "" + echo "<p><b>hei, $(whoami) ♥</b><br>" + link 'skriv en artikkel' 'skriv' + echo "<p><b>du har tilgang til…</b><br>" + link 'dine artikler' 'ditt' && echo "<br>" + for group in $available; do + link "$group" "gruppe/$group" + done + kalender + echo "<p><b>kos deg! ٩(͡๏̯͡๏)۶</b>" + echo "<p class=footer><b>tears $VERSION</b><br>" + link 'todo' 'todo' && echo '|' && link 'teatime' 'teatime' + echo "</div>" +} + +liste() { + n=$((${GET_n:-0} + 7)) + $finder -printf '%h\t%TT\t%f\n' | sort -r | head -$n | tail -7 | while read + do + d=$(cut -f 1 <<<"$REPLY") + f=$(cut -f 3 <<<"$REPLY") + kort $d/$f + done + echo "<hr><p>" + qlink "eldre artikler…" "$action/$path" "dato=$GET_dato" "n=$n" +} + +tags() { + find -L tags -samefile "$1" -execdir dirname {} + | while read file; do + echo -n "${file##*/} " + done + + # tag "$1" "$POST_tags" +} + +tag() { + echo "<p>" + for tag in $2; do + echo "-- sjekke om tag $tag er ok --" + echo "<br>" + echo ln -s "$1" "tags/$tag/$(tr / - <<<$1)" + echo "<br>" + done +} + +skjema() { + [ ! -f "$1" ] && echo "<p>finner ikke $1: <pre>$(xxd <<<$1)</pre>" && exit + [ ! -w "$1" ] && echo "<p>du har ikke skrivetilgang" && exit + echo "<form method='post' action='$BASE/oppdater/$1'>" + lupin -f <$1 + echo "<p><label>Magiske ord:</label>" + echo "<input type='text' name='tags' value='$(tags $1)'>" + tag "$1" "ok test haha" + echo "<p><input type='submit' value='Lagre'>" + echo "</form>" + echo "<p><b>$(info $1)</b>" +} + +vis() { + [ ! -f "$1" ] && echo "<p>finner ikke $1: <pre>$(xxd <<<$1)</pre>" && exit + [ ! -r "$1" ] && echo "<p>du har ikke lesetilgang" && exit + lupin -h <$1 +} + +lagre() { + path=$(date +%Y/%m/%d -d $POST_publisert) + slug=$(sed -e 's|[ /]|-|g' -e 's|[?:]||g' <<<$POST_title) + [ -z "$slug" ] && error "Ugyldig overskrift" + [ -f "artikler/$path/$slug.txt" ] && error "Artikkelen finnes allerede" + mkdir -m 777 -p "artikler/$path" + [ -z "$POST_privat" ] && umask 0002 + lupin -l -e >"artikler/$path/$slug.txt" + chgrp $POST_gruppe "artikler/$path/$slug.txt" + irc "$(whoami) skriver ny artikkel: $POST_title" + redirect "$BASE/rediger/artikler/$path/$slug.txt" +} + +oppdater() { + [ ! -f "$1" ] && error "finner ikke $1" + [ ! -w "$1" ] && error "ikke skrivetilgang til $1" + [ -z "$POST_title" ] && error "ingen overskrift" + lupin -l -e >$1 + irc "$(whoami) redigerte $POST_title" + redirect "$BASE/rediger/$path" +} + +kort() { + IFS=/. read -r type y m d hhmm slug ext <<< "$1" + ( + read -r tittel + echo "<h2>$tittel</h2>" "<p class='x'>$(byline $1), $y/$m/$d" + [ -r "$1" ] && echo "·" && link "vis" $(pencode / <<<"vis/$1") + [ -w "$1" ] && echo "·" && link "rediger" $(pencode / <<<"rediger/$1") + [ -z "$2" ] && head -1 | marxup - || marxup - + ) < $1 +} + +forbered() { + echo "<form method=post action='$BASE/lagre/$path'>" + echo "<p><label>Overskrift:</label>" + echo "<input type=text name=title size=24>" + echo "<p><label>Dato:</label>" + echo "<input type=date name=publisert value=$(date +%Y-%m-%d) size=10>" + echo "<p><label>*</label>" + echo "<select name='type'>" + for type in lupin/*; do + id=$(basename $type .lua) + echo "<option value='$id'>$(sed 's|.|\U&|' <<<$id)</option>" + done + echo "</select>" + echo "for" + echo "<select name=gruppe>" + for group in $available; do + echo "<option>$group</option>" + done + echo "</select>" + echo "(<input type=checkbox name=privat>privat artikkel?)" + echo "<p><label>*</label><input type=submit value='Forbered artikkel...'>" + echo "</form>" +} + +[ -f tears.rc ] && . tears.rc + +umask 0022 + +groups=$(groups | tr " " "\n" | sort) +available=$(comm -12 - <(ls redaksjoner | sort) <<<"$groups") +action=$(basename $SCRIPT_FILENAME) +kilde="artikler/$(sed 's|-|/|g' <<<$GET_dato)" +finder="find $kilde -type f" +[ "$action" = gruppe ] && finder="$finder -group $path" +[ "$action" = ditt ] && finder="$finder -user $REMOTE_USER" +[ "$REQUEST_METHOD" = GET ] && $finder | meny +case $action in + index.html|ditt|gruppe) liste;; + skriv) forbered;; + rediger) skjema $path;; + vis) vis $path;; + lagre) lagre;; + oppdater) oppdater $path;; + teatime) irc "$(whoami) <3 teatime"; echo "<p><img src='http://atei.st/src/1352063360909860.jpg'><br>";; + spökvåningen) echo "<p>spök";; + todo) echo "<pre>$(cat TODO)</pre>";; +esac +%> + |