#!/bin/sh
-chan_db="$_DATA/channels.db"
+["$includepage_channel" ] && return 0
+includepage_channel="$0"
-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
+. "$_EXEC/db_channel.sh"
+. "$_EXEC/widgets.sh"
-update_channel(){
- local id="${1}" name description logo theme authors descr_cache futureuse
- local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE
- local arg
-
- for arg in "$@"; do case $arg in
- name=*) name="${arg#*=}";;
- description=*) description="${arg#*=}";;
- logo=*) logo="${arg#*=}";;
- theme=*) theme="${arg#*=}";;
- authors=*) authors="${arg#*=}";;
- esac; done
-
- if LOCK "$chan_db"; then
- while read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; do
- if [ "$id" = "$ID" ]; then
- printf '%s %s %s %s %s %s %s %s\n' \
- "$id" "$(STRING "${name-$(UNSTRING "$NAME")}")" \
- "$(STRING "${description-$(UNSTRING "$DESCRIPTION")}")" \
- "${logo:-${logo-${LOGO}}${logo+\\}}" \
- "${theme:-${theme-${THEME}}${theme+\\}}" \
- "$(STRING "${authors-$(UNSTRING "${AUTHORS}")}")" \
- "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
- "${FUTUREUSE:-\\}"
- else
- printf '%s %s %s %s %s %s %s %s\n' \
- "$ID" "$NAME" "$DESCRIPTION" "$LOGO" "$THEME" "$AUTHORS" \
- "$DESCR_CACHE" "$FUTUREUSE"
- fi
- done <"$chan_db" >"${chan_db}.$$"
- mv -- "${chan_db}.$$" "${chan_db}"
- RELEASE "$chan_db"
- else
- return 1
- fi
-}
-
-AUTHOR(){
- if [ "$CHANNEL_ID" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
- return 0
- else
- return 1
- fi
-}
-
-# Video
-# ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS
+read_channel "$channel"
[ "$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
+ elif new_channel "$channel"; then
+ REDIRECT "${_BASE}/channel/$channel/edit"
else
REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK"
fi
;;
update_channel)
- if [ ! "$channel" ]; then
+ if [ ! "$CHANNEL_ID" ]; then
REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
elif [ ! "$USER_ID" ]; then
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
elif ! AUTHOR; then
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
- elif update_channel "$channel" "name=$(POST name)" \
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
+ elif update_channel "$CHANNEL_ID" "name=$(POST name)" \
"description=$(POST description)" \
"authors=$USER_ID"; then
- REDIRECT "${_BASE}/channel/${channel}/"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
else
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOLOCK"
fi
;;
update_channel_cancel)
- REDIRECT "${_BASE}/channel/${channel}/"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
;;
newvideo)
+ . "$_EXEC/db_video.sh"
video="$(POST video |checkid)"
AUTHOR \
- && mkdir -p -- "${_DATA}/${channel}/"
+ && mkdir -p -- "${_DATA}/$CHANNEL_ID/"
if [ ! "$video" ]; then
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_INVALID_ID"
- elif [ ! "$channel" ]; then
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_INVALID_ID"
+ elif [ ! "$CHANNEL_ID" ]; then
REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
elif [ ! "$USER_ID" ]; then
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
elif ! AUTHOR; then
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
- elif LOCK "$vid_db"; then
- if grep -q "^${video} " "$vid_db"; then
- RELEASE "$vid_db"
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_EXISTS"
- else
- # ID NAME DESC RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE
- printf '%s \\ \\ \\ \\ \\ \\ void %s \\ \\\n' \
- "$video" "$(STRING "$USER_ID")" \
- >>"$vid_db"
- RELEASE "$vid_db"
- REDIRECT "${_BASE}/channel/${channel}/${video}/edit"
- fi
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
+ elif new_video "$video"; then
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/$video/"
else
- REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_NOLOCK"
+ REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NEWVIDEO_NOLOCK"
fi
;;
esac
-w_video(){
- local thumb
- local VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
- VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
- VIDEO_DESCR_CACHE VIDEO_FUTUREUSE
-
- if read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
- VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
- VIDEO_DESCR_CACHE VIDEO_FUTUREUSE; then
- VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
- VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
- VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
- VIDEO_LENGTH="$(printf %i "$VIDEO_LENGTH" 2>&-)"
-
- [ "${VIDEO_STATUS}" = public ] || AUTHOR || return 0
-
- thumb="${_BASE}/video/${CHANNEL_ID}/${VIDEO_ID}_thumb.jpg"
- [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
- printf '[div .video .thumb
- [a href="%s/channel/%s/%s/"
- [h3 . %s]
- [figure [img src="%s" alt=""]]
- [span .duration . %i:%02i]
- [div .description . %s]
- ]
- ]' "$_BASE" "$CHANNEL_ID" "$VIDEO_ID" \
- "$(HTML "${VIDEO_NAME:-(Unnamed Video)}")" \
- "$thumb" \
- "$((VIDEO_LENGTH / 60))" "$((VIDEO_LENGTH % 60))" \
- "$(UNSTRING "$DESCR_CACHE")"
- else
- return 1
- fi
-}
-
-w_channel(){
- local vid_db
- local CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
- CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
-
- if read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
- CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
- CHANNEL_FUTUREUSE; 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"
- cat <<-EOF
- [div .channel
- [div .description
- [h2 [a href="${_BASE}/channel/${CHANNEL_ID}/" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
- ${CHANNEL_DESCR_CACHE}
- ]$(
- [ -f "$vid_db" -a -r "$vid_db" ] \
- && while w_video; 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
- AUTHOR || REDIRECT "${_BASE}/${channel}/#ERROR_EDIT_NOTALLOWED"
+if [ "$CHANNEL_ID" -a "$action" = edit ]; then
+ AUTHOR || REDIRECT "${_BASE}/$CHANNEL_ID/#ERROR_EDIT_NOTALLOWED"
yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
[form .channel .edit method=POST
- [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"]
+ [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name" autocomplete=off]
[textarea name="description" placeholder="Description" . $(HTML "$CHANNEL_DESCRIPTION")]
[submit "action" "update_channel" . Update]
[submit "action" "update_channel_cancel" . Cancel]
]
EOF
-elif [ "$channel" ]; then
+elif [ "$CHANNEL_ID" ]; then
yield_page "$CHANNEL_NAME" "channel" <<-EOF
- [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]
- $(AUTHOR && printf ' - [a href="edit" edit]')
- ]
+ [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
[h1 .name $(HTML "$CHANNEL_NAME")]
[div .description . ${CHANNEL_DESCR_CACHE}]
+ $(AUTHOR && printf '[a .button href="edit" edit]')
[h1 .videos Videos]
[div .videos . $(
AUTHOR && printf '
- [form .video .newvideo method=POST
- [hidden "video" "%s"]
- [submit "action" "newvideo" New Video]
- ]' "$(timeid)"
- [ -f "$vid_db" -a -r "$vid_db" ] \
- && while w_video "$ID"; do :; done <"$vid_db"
+ [form .video .newvideo method=POST
+ [hidden "video" "%s"]
+ [submit "action" "newvideo" New Video]
+ ]' "$(timeid)"
+ list_videos |while w_video; do :; done
)]
EOF
else
yield_page "Channels" "channels" <<-EOF
- $(w_channel_list)
+ $([ "$USER_ID" ] && printf '
+ [form .channel .newchannel method=POST
+ [hidden "channel" "%s"]
+ [submit "action" "newchannel" New Channel]
+ ]' "$(timeid)"
+ )
+ $([ -f "$chan_db" -a -r "$chan_db" ] \
+ && while w_channel; do :; done <"$chan_db"
+ )
EOF
fi