From: Paul Hänsch Date: Tue, 28 Sep 2021 19:01:04 +0000 (+0200) Subject: channel view and overview pages X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=fdce2b28611f69f277d6801c910efbd914111be0;p=rawnet channel view and overview pages --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3c57b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +channels.db +users.db +serverkey diff --git a/index.cgi b/index.cgi index a2059e2..fe32414 100755 --- a/index.cgi +++ b/index.cgi @@ -1,64 +1,73 @@ #!/bin/sh +USER_REQUIREEMAIL=false + . "${_EXEC:-${0%/*}}"/cgilite/cgilite.sh -. "$_EXEC"/cgilite/session.sh -. "$_EXEC"/cgilite/file.sh -. "$_EXEC"/cgilite/storage.sh -. "$_EXEC"/widgets.sh +. "$_EXEC"/cgilite/session.sh nocookie +. "$_EXEC"/cgilite/users.sh PATH_INFO="$(PATH "/${PATH_INFO#${_BASE}}")" -# bookmarks_cookie="$(COOKIE bookmarks |grep -xE '[0-9a-zA-Z:=]{16}(/[0-9a-zA-Z:=]{16})?( [0-9a-zA-Z:=]{16}(/[0-9a-zA-Z:=]{16})?)*')" -bookmarks='' -for bm in $(COOKIE bookmarks); do - [ -f "${_DATA}/$(checkid "${bm%/*}")" ] && bookmarks="${bookmarks}${bookmarks:+ }${bm}" -done -if [ "$bookmarks" ]; then - SET_COOKIE +$((182 * 86400)) bookmarks="${bm}" Path="${_BASE}/" +export MD_HTML="false" +if [ "$(which awk)" ]; then + markdown() { awk -f "$_EXEC/cgilite/markdown.awk"; } +else + markdown() { busybox awk -f "$_EXEC/cgilite/markdown.awk"; } fi yield_page(){ - title="${1:-Webpoll}" page="$2" + title="${1:-RAW:NET}" page="$2" printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \ "Content-Security-Policy: script-src 'none'" \ '' { printf '[html [head [meta name="viewport" content="width=device-width"] - [link rel="stylesheet" type="text/css" href="%s/common.css"] - [link rel="stylesheet" type="text/css" href="%s/widgets.css"] - [link rel="stylesheet" type="text/css" href="%s/webpoll.css"] + [link rel="stylesheet" type="text/css" href="%s/cgilite/common.css"] + [link rel="stylesheet" type="text/css" href="%s/rawnet.css"] [title %s] ] [body class="%s" - ' "$_BASE" "$_BASE" "$_BASE" "$(HTML "$title")" "$page" + ' "$_BASE" "$_BASE" "$(HTML "$title")" "$page" + printf ' + [header + [form method=POST action="%s/search/" + [input name=search placeholder="Search"] + ] + ]' "$_BASE" cat printf '] ]' } |"$_EXEC/cgilite/html-sh.sed" -u } -pagename() { - local id="$1" - local file="$_DATA/$id" - if [ -f "$file" ]; then - DBM "$file" get title || printf 'Unnamed Page' - else - return 1; - fi -} - - page_home() { . "$_EXEC"/home.sh; } -page_newdate() { . "$_EXEC"/newdate.sh; } - page_poll() { . "$_EXEC"/poll.sh; } - case ${PATH_INFO} in /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';; - /common.css) FILE "$_EXEC/cgilite/common.css";; - /widgets.css|/webpoll.css) FILE "${_EXEC}/${PATH_INFO}";; - /) page_home;; - /[0-9a-zA-Z:=]???????????????/[0-9a-zA-Z:=]???????????????) page_newdate;; - /*/newoptions);; - /[0-9a-zA-Z:=]???????????????) page_poll;; - /*) page_home;; + *.css) + . "${_EXEC}/cgilite/file.sh" + FILE "${_EXEC}/${PATH_INFO}" + ;; + /login/) + if [ "$USER_ID" ]; then + REDIRECT "${_BASE}/" + else + yield_page 'RAW:NET Login' login <<-EOF + $(w_user_login) + EOF + fi + ;; + /register/) + yield_page 'RAW:NET Register User' register <<-EOF + $(w_user_register) + EOF + ;; + /recover/) + yield_page 'RAW:NET Recover Account' recover <<-EOF + $(w_user_recover) + EOF + ;; + /|/channel/*) . "${_EXEC}/page_channel.sh";; + /playlist/*) . "${_EXEC}/page_playlist.sh";; + /search/*) . "${_EXEC}/page_search.sh";; + *) . "${_EXEC}/page_404.sh";; esac exit 0 diff --git a/page_404.sh b/page_404.sh new file mode 100755 index 0000000..a4acce8 --- /dev/null +++ b/page_404.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +printf 'Status: 404 Not Found\r\n' + +yield_page '' 404 <<-EOF +[h1 404][p +[span Page not found or nevermore] +[span Quoth the server: 404] +] +EOF diff --git a/page_channel.sh b/page_channel.sh new file mode 100644 index 0000000..223f882 --- /dev/null +++ b/page_channel.sh @@ -0,0 +1,177 @@ +#!/bin/sh + +chan_db="$_DATA/channels.db" + +channel='' video='' action='' +path_info="$PATH_INFO" +path_info="${path_info#/channel/}" +if [ "$(checkid "${path_info%%/*}")" ]; then + channel="${path_info%%/*}" + path_info="${path_info#*/}" +fi +if [ "$(checkid "${path_info%%/*}")" ]; then + video="${path_info%%/*}" + path_info="${path_info#*/}" +fi +action="${path_info}" +unset path_info + +# Channel +# ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE + +if [ "$channel" -a -f "$chan_db" -a -r "$chan_db" ]; then + read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \ + CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \ + CHANNEL_FUTUREUSE <<-EOF + $(grep "^${channel} " "${chan_db}") + EOF + if [ "$CHANNEL_ID" ]; then + CHANNEL_NAME="$(UNSTRING "$CHANNEL_NAME")" + CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")" + CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")" + CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")" + vid_db="${_DATA}/${CHANNEL_ID}/videos.db" + else + channel='' + fi +fi + +update_channel(){ + local id="${1}" name="${2}" description="${3}" logo="${4}" theme="${5}" \ + authors="${6}" descr_cache="${7}" futureuse="${8}" + local ID INFO + if LOCK "$chan_db"; then + while read -r ID INFO; do + if [ "$id" = "$ID" ]; then + printf '%s %s %s %s %s %s %s %s\n' \ + "$id" "$(STRING "$name")" "$(STRING "$description")" \ + "${logo:-\\}" "${theme:-\\}" "$(STRING "$authors")" \ + "$(printf %s "$description" |markdown |STRING)" \ + "${futureuse:-\\}" + else + printf '%s %s\n' "$ID" "$INFO" + fi + done <"$chan_db" >"${chan_db}.$$" + mv -- "${chan_db}.$$" "${chan_db}" + RELEASE "$chan_db" + else + return 1 + fi +} + +# Video +# ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS + +[ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in + newchannel) + channel="$(POST channel |checkid)" + if [ ! "$USER_ID" ]; then + REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOTALLOWED" + elif LOCK "$chan_db"; then + if grep -q '^${channel} ' "$chan_db"; then + RELEASE "$chan_db" + REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_EXISTS" + else + printf '%s \\ \\ \\ \\ %s \\ \\\n' \ + "$channel" "$(STRING "$USER_ID")" \ + >>"$chan_db" + RELEASE "$chan_db" + REDIRECT "${_BASE}/channel/${channel}/edit" + fi + else + REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK" + fi + ;; + update_channel) + if [ ! "$channel" ]; then + REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL" + elif [ ! "$USER_ID" ]; then + REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN" + elif [ "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then + REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED" + elif update_channel "$channel" "$(POST name)" "$(POST description)" \ + "" "" "$USER_ID" "" ""; then + REDIRECT "${_BASE}/channel/${channel}/" + else + REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK" + fi + ;; + newvideo) + video="$(POST video |checkid)" + # database video create + REDIRECT "${_BASE}/channel/${channel}/${video}/" + ;; +esac + +w_video(){ + local CID="$1" thumb + local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE + if read -r ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE; then + thumb="${_BASE}/${CID}/thumb_${ID}.jpg" + cat <<-EOF + [div .video + [h3 . $(UNSTRING "$NAME" |HTML)] + [img href="${thumb}" alt="$(UNSTRING "$DESCRIPTION" |HTML)"] + ] + EOF + else + return 1 + fi +} + +w_channel(){ + local vid_db + local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE + if read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; then + vid_db="${_DATA}/${ID}/videos.db" + cat <<-EOF + [div .channel + [h2 . $(UNSTRING "$NAME" |HTML)] + [div .description . $(UNSTRING "$DESCR_CACHE")] + $( [ -f "$vid_db" -a -r "$vid_db" ] \ + && while w_video "$ID"; do :; done <"$vid_db" + ) + ] + EOF + else + return 1 + fi +} + +w_channel_list(){ + if [ $USER_ID ]; then + printf ' + [form .channel .newchannel method=POST + [hidden "channel" "%s"] + [submit "action" "newchannel" New Channel] + ]' "$(timeid)" + fi + [ -f "$chan_db" -a -r "$chan_db" ] \ + && while w_channel; do :; done <"$chan_db" +} + +if [ "$channel" -a "$video" ]; then + . ${_EXEC}/page_video.sh +elif [ "$channel" -a "$action" = edit ]; then + yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF + [form .channel .edit method=POST + [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"] + + [submit "action" "update_channel" . Update] + ] + EOF +elif [ "$channel" ]; then + yield_page "$CHANNEL_NAME" "channel" <<-EOF + [h1 .name $(HTML "$CHANNEL_NAME")] + [div .description . ${CHANNEL_DESCR_CACHE}] + [div .videos + $( [ -f "$vid_db" -a -r "$vid_db" ] \ + && while w_video "$ID"; do :; done <"$vid_db" + ) + ] + EOF +else + yield_page "Channels" "channels" <<-EOF + $(w_channel_list) + EOF +fi