]> git.plutz.net Git - rawnet/blob - index.cgi
For progress frame use chunked encoding instead of long poll
[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 export MD_HTML="false"
11 if [ "$(which awk)" ]; then
12   markdown() { awk -f "$_EXEC/cgilite/markdown.awk"; }
13 else
14   markdown() { busybox awk -f "$_EXEC/cgilite/markdown.awk"; }
15 fi
16
17 checked(){
18   local check="$1"; shift 1;
19   for comp in "$@"; do
20     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
21       printf 'checked="checked"'
22       break;
23     fi 2>/dev/null
24   done
25 }
26 selected(){
27   local check="$1"; shift 1;
28   for comp in "$@"; do
29     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
30       printf 'selected="selected"'
31       break;
32     fi 2>/dev/null
33   done
34 }
35
36 w_user_login(){
37   if [ ! "$USER_ID" ]; then
38     cat <<-EOF
39         [form #user_login .login method=POST
40           [label Login]
41           [input name=uname placeholder="Username or Email" autocomplete=off]
42           [input type=password name=pw placeholder="Passphrase"]
43           [submit "action" "user_login" Login]
44           $([ "$USER_REGISTRATION" = true ] && printf '[a href="%s/register/" Register]' "$_BASE")
45         ]
46         EOF
47   elif [ "$USER_ID" ]; then
48     cat <<-EOF
49         [form #user_login .logout method=POST
50           [p Logged in as [span . $(HTML ${USER_NAME})]]
51           $([ "$USER_REGISTRATION" != true ] && printf '[a href="%s/invite/" Invite Friend]' "$_BASE")
52           [submit "action" "user_logout" Logout]
53         ]
54         EOF
55   fi
56 }
57
58 yield_page(){
59   title="${1:-RAW:NET}" page="$2"
60   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
61                   "Content-Security-Policy: script-src 'none'" \
62                   ''
63   { cat <<-EOF
64         [!DOCTYPE HTML]
65         [html [head
66           [meta name="viewport" content="width=device-width"]
67           [link rel="stylesheet" type="text/css" href="$_BASE/cgilite/common.css"]
68           [link rel="stylesheet" type="text/css" href="$_BASE/rawnet.css"]
69           [title . $(HTML "$title")]
70         ] [body class="$page"
71           [header
72             [form method=POST action="$_BASE/search/"
73               [input name=search placeholder="Search"]
74             ]
75             $(w_user_login)
76           ][main
77         EOF
78   cat
79   printf ']]]'
80   } |"$_EXEC/cgilite/html-sh.sed" -u
81 }
82
83 case ${PATH_INFO} in
84   /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
85   *.css)
86     . "${_EXEC}/cgilite/file.sh"
87     FILE "${_EXEC}/${PATH_INFO}"
88     ;;
89   /login/)
90     if [ "$USER_ID" ]; then
91       REDIRECT "${_BASE}/"
92     else
93       yield_page 'RAW:NET Login' login <<-EOF
94         $(w_user_login)
95         EOF
96     fi
97     ;;
98   /register/)
99     if [ "$USER_ID" -a "$(GET user_register)" = confirm ]; then
100       printf 'Refresh: 2; url=%s\r\n' "/${_BASE#/}"
101       yield_page "RAW:NET Register confirm" "message register_confirm" <<-EOF
102         User registration successful!
103         EOF
104       exit 0
105     fi
106     yield_page 'RAW:NET Register User' register <<-EOF
107         $(w_user_register)
108         EOF
109     ;;
110   /recover/)
111     yield_page 'RAW:NET Recover Account' recover <<-EOF
112         $(w_user_recover)
113         EOF
114     ;;
115   /invite/)
116     if [ "$USER_ID" -a "$(GET user_register)" = confirm ]; then
117       printf 'Refresh: 2; url=%s\r\n' "/${_BASE#/}"
118       yield_page "RAW:NET Account activation" "message invite_confirm" <<-EOF
119         Account activation successful!
120         EOF
121       exit 0
122     fi
123     yield_page 'RAW:NET Invite User' invite <<-EOF
124         [nav [a href="../" Channels] - [span Invite]]
125         $(w_user_invite)
126         EOF
127     ;;
128   /video/*/*.mp4|/video/*/*.webm|/video/*/*.jpg)
129     . "${_EXEC}/cgilite/file.sh"
130     FILE "${_DATA}/${PATH_INFO#/video/}"
131     ;;
132   /channel/*/*/*)
133     action="${PATH_INFO##*/}"
134     video="${PATH_INFO%/*}" video="${video##*/}"
135     channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
136     . "$_EXEC/page_video.sh"
137     ;;
138   /channel/*/*/)
139     action=""
140     video="${PATH_INFO%/}" video="${video##*/}"
141     channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
142     . "$_EXEC/page_video.sh"
143     ;;
144   /channel/*/*)
145     action="${PATH_INFO##*/}"
146     video=""
147     channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
148     . "$_EXEC/page_channel.sh"
149     ;;
150   /channel/*/)
151     action=""
152     video=""
153     channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
154     . "$_EXEC/page_channel.sh"
155     ;;
156   /|/channel/)
157     . "${_EXEC}/page_channel.sh"
158     ;;
159   /playlist/*) . "${_EXEC}/page_playlist.sh";;
160   /search/*) . "${_EXEC}/page_search.sh";;
161   *) . "${_EXEC}/page_404.sh";;
162 esac
163
164 exit 0