]> git.plutz.net Git - rawnet/blob - page_channel.sh
control elements for channel editing
[rawnet] / page_channel.sh
1 #!/bin/sh
2
3 chan_db="$_DATA/channels.db"
4
5 channel='' video='' action=''
6 path_info="$PATH_INFO"
7 path_info="${path_info#/channel/}"
8 if [ "$(checkid "${path_info%%/*}")" ]; then
9   channel="${path_info%%/*}"
10   path_info="${path_info#*/}"
11 fi
12 if [ "$(checkid "${path_info%%/*}")" ]; then
13   video="${path_info%%/*}"
14   path_info="${path_info#*/}"
15 fi
16 action="${path_info}"
17 unset path_info
18
19 # Channel
20 # ID    NAME    DESCRIPTION     LOGO    THEME   AUTHORS DESCR_CACHE FUTUREUSE
21
22 if [ "$channel" -a -f "$chan_db" -a -r "$chan_db" ]; then
23   read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
24           CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
25           CHANNEL_FUTUREUSE <<-EOF
26         $(grep "^${channel}     " "${chan_db}")
27         EOF
28   if [ "$CHANNEL_ID" ]; then
29            CHANNEL_NAME="$(UNSTRING "${CHANNEL_NAME}")"
30     CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")"
31         CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")"
32     CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")"
33     vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
34   else
35     channel=''
36   fi
37 fi
38
39 update_channel(){
40   local id="${1}" name="${2}" description="${3}" logo="${4}" theme="${5}" \
41         authors="${6}" descr_cache="${7}" futureuse="${8}"
42   local ID INFO
43   if LOCK "$chan_db"; then
44     while read -r ID INFO; do
45       if [ "$id" = "$ID" ]; then
46         printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
47                "$id" "$(STRING "$name")" "$(STRING "$description")" \
48                "${logo:-\\}" "${theme:-\\}" "$(STRING "$authors")" \
49                "$(printf %s "$description" |markdown |STRING)" \
50                "${futureuse:-\\}"
51       else
52         printf '%s      %s\n' "$ID" "$INFO"
53       fi
54     done <"$chan_db" >"${chan_db}.$$"
55     mv -- "${chan_db}.$$" "${chan_db}"
56     RELEASE "$chan_db"
57   else
58     return 1
59   fi
60 }
61
62 # Video
63 # ID    NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS  UPLOADER        HITS
64
65 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
66   newchannel)
67     channel="$(POST channel |checkid)"
68     if [ ! "$USER_ID" ]; then
69       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOTALLOWED"
70     elif LOCK "$chan_db"; then
71       if grep -q '^${channel}   ' "$chan_db"; then
72         RELEASE "$chan_db"
73         REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_EXISTS"
74       else
75         printf '%s      \\      \\      \\      \\      %s      \\      \\\n' \
76                "$channel" "$(STRING "$USER_ID")" \
77                >>"$chan_db"
78         RELEASE "$chan_db"
79         REDIRECT "${_BASE}/channel/${channel}/edit"
80       fi
81     else
82       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK"
83     fi
84     ;;
85   update_channel)
86     if [ ! "$channel" ]; then
87       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
88     elif [ ! "$USER_ID" ]; then
89       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
90     elif [ "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
91       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
92     elif update_channel "$channel" "$(POST name)" "$(POST description)" \
93                         "" "" "$USER_ID" "" ""; then
94       REDIRECT "${_BASE}/channel/${channel}/"
95     else
96       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK"
97     fi
98     ;;
99   newvideo)
100     video="$(POST video |checkid)"
101     # database video create
102     REDIRECT "${_BASE}/channel/${channel}/${video}/"
103     ;;
104 esac
105
106 w_video(){
107   local CID="$1" thumb
108   local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE
109   if read -r ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS FUTUREUSE; then
110     thumb="${_BASE}/${CID}/thumb_${ID}.jpg"
111     cat <<-EOF
112         [div .video
113           [h3 . $(UNSTRING "$NAME" |HTML)]
114           [img href="${thumb}" alt="$(UNSTRING "$DESCRIPTION" |HTML)"]
115         ]
116         EOF
117   else
118     return 1
119   fi
120 }
121
122 w_channel(){
123   local vid_db
124   local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE
125   if read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; then
126     vid_db="${_DATA}/${ID}/videos.db"
127     [ "$NAME" = \\ ] && NAME="(UNNAMED CHANNEL)"
128     cat <<-EOF
129         [div .channel
130           [h2 [a href="${_BASE}/channel/${ID}/" $(UNSTRING "${NAME}" |HTML)]]
131           [div .description . $(UNSTRING "$DESCR_CACHE")]
132           $( [ -f "$vid_db" -a -r "$vid_db" ] \
133              && while w_video "$ID"; do :; done <"$vid_db"
134           )
135         ]
136         EOF
137   else
138     return 1
139   fi
140 }
141
142 w_channel_list(){
143   if [ $USER_ID ]; then
144     printf '
145     [form .channel .newchannel method=POST
146       [hidden "channel" "%s"]
147       [submit "action" "newchannel" New Channel]
148     ]' "$(timeid)"
149   fi
150   [ -f "$chan_db" -a -r "$chan_db" ] \
151   && while w_channel; do :; done <"$chan_db"
152 }
153
154 if [ "$channel" -a "$video" ]; then
155   . ${_EXEC}/page_video.sh
156 elif [ "$channel" -a "$action" = edit ]; then
157   [ "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
158   || REDIRECT "${_BASE}/${channel}/#ERROR_EDIT_NOTALLOWED"
159   yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
160         [form .channel .edit method=POST
161           [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"]
162           [textarea name="description" placeholder="Description" . $(HTML "$CHANNEL_DESCRIPTION")]
163           [submit "action" "update_channel" . Update]
164         ]
165         EOF
166 elif [ "$channel" ]; then
167   yield_page "$CHANNEL_NAME" "channel" <<-EOF
168         [h1 .name $(HTML "$CHANNEL_NAME")]
169         $( [ "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ] \
170            && printf '[a href="edit" edit]'
171         )
172         [div .description . ${CHANNEL_DESCR_CACHE}]
173         [div .videos
174           $( [ -f "$vid_db" -a -r "$vid_db" ] \
175              && while w_video "$ID"; do :; done <"$vid_db"
176           )
177         ]
178         EOF
179 else
180   yield_page "Channels" "channels" <<-EOF
181         $(w_channel_list)
182         EOF
183 fi