]> git.plutz.net Git - rawnet/blob - index.cgi
automatic thumbnail generation and serving
[rawnet] / index.cgi
1 #!/bin/sh
2
3 USER_REGISTRATION=false
4 USER_REQUIREEMAIL=false
5
6 . "${_EXEC:-${0%/*}}"/cgilite/cgilite.sh
7 . "$_EXEC"/cgilite/session.sh nocookie
8 . "$_EXEC"/cgilite/users.sh
9
10 PATH_INFO="$(PATH "/${PATH_INFO#${_BASE}}")"
11
12 export MD_HTML="false"
13 if [ "$(which awk)" ]; then
14   markdown() { awk -f "$_EXEC/cgilite/markdown.awk"; }
15 else
16   markdown() { busybox awk -f "$_EXEC/cgilite/markdown.awk"; }
17 fi
18
19 checked(){
20   local check="$1"; shift 1;
21   for comp in "$@"; do
22     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
23       printf 'checked="checked"'
24       break;
25     fi 2>/dev/null
26   done
27 }
28 selected(){
29   local check="$1"; shift 1;
30   for comp in "$@"; do
31     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
32       printf 'selected="selected"'
33       break;
34     fi 2>/dev/null
35   done
36 }
37
38 w_user_login(){
39   if [ ! "$USER_ID" ]; then
40     cat <<-EOF
41         [form #user_login .login method=POST
42           [label Login]
43           [input name=uname placeholder="Username or Email" autocomplete=off]
44           [input type=password name=pw placeholder="Passphrase"]
45           [submit "action" "user_login" Login]
46           $([ "$USER_REGISTRATION" = true ] && printf '[a href="%s/register/" Register]' "$_BASE")
47         ]
48         EOF
49   elif [ "$USER_ID" ]; then
50     cat <<-EOF
51         [form #user_login .logout method=POST
52           [p Logged in as [span . $(HTML ${USER_NAME})]]
53           $([ "$USER_REGISTRATION" != true ] && printf '[a href="%s/invite/" Invite Friend]' "$_BASE")
54           [submit "action" "user_logout" Logout]
55         ]
56         EOF
57   fi
58 }
59
60 yield_page(){
61   title="${1:-RAW:NET}" page="$2"
62   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
63                   "Content-Security-Policy: script-src 'none'" \
64                   ''
65   { cat <<-EOF
66         [!DOCTYPE HTML]
67         [html [head
68           [meta name="viewport" content="width=device-width"]
69           [link rel="stylesheet" type="text/css" href="$_BASE/cgilite/common.css"]
70           [link rel="stylesheet" type="text/css" href="$_BASE/rawnet.css"]
71           [title . $(HTML "$title")]
72         ] [body class="$page"
73           [header
74             [form method=POST action="$_BASE/search/"
75               [input name=search placeholder="Search"]
76             ]
77             $(w_user_login)
78           ][main
79         EOF
80   cat
81   printf ']]]'
82   } |"$_EXEC/cgilite/html-sh.sed" -u
83 }
84
85 case ${PATH_INFO} in
86   /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
87   *.css)
88     . "${_EXEC}/cgilite/file.sh"
89     FILE "${_EXEC}/${PATH_INFO}"
90     ;;
91   /login/)
92     if [ "$USER_ID" ]; then
93       REDIRECT "${_BASE}/"
94     else
95       yield_page 'RAW:NET Login' login <<-EOF
96         $(w_user_login)
97         EOF
98     fi
99     ;;
100   /register/)
101     yield_page 'RAW:NET Register User' register <<-EOF
102         $(w_user_register)
103         EOF
104     ;;
105   /recover/)
106     yield_page 'RAW:NET Recover Account' recover <<-EOF
107         $(w_user_recover)
108         EOF
109     ;;
110   /video/*/*.mp4|/video/*/*_thumb.jpg)
111     . "${_EXEC}/cgilite/file.sh"
112     FILE "${_DATA}/${PATH_INFO#/video/}"
113     ;;
114   /|/channel/*) . "${_EXEC}/page_channel.sh";;
115   /playlist/*) . "${_EXEC}/page_playlist.sh";;
116   /search/*) . "${_EXEC}/page_search.sh";;
117   *) . "${_EXEC}/page_404.sh";;
118 esac
119
120 exit 0