]> git.plutz.net Git - rawnet/blob - page_channel.sh
video styling (controls, size, etc...)
[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 description logo theme authors descr_cache futureuse
41   local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE
42   local arg
43
44   for arg in "$@"; do case $arg in
45     name=*) name="${arg#*=}";;
46     description=*) description="${arg#*=}";;
47     logo=*) logo="${arg#*=}";;
48     theme=*) theme="${arg#*=}";;
49     authors=*) authors="${arg#*=}";;
50   esac; done
51
52   if LOCK "$chan_db"; then
53     while read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; do
54       if [ "$id" = "$ID" ]; then
55         printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
56                "$id" "$(STRING "${name-$(UNSTRING "$NAME")}")" \
57                "$(STRING "${description-$(UNSTRING "$DESCRIPTION")}")" \
58                "${logo:-${logo-${LOGO}}${logo+\\}}" \
59                "${theme:-${theme-${THEME}}${theme+\\}}" \
60                "$(STRING "${authors-$(UNSTRING "${AUTHORS}")}")" \
61                "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
62                "${FUTUREUSE:-\\}"
63       else
64         printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
65                "$ID" "$NAME" "$DESCRIPTION" "$LOGO" "$THEME" "$AUTHORS" \
66                "$DESCR_CACHE" "$FUTUREUSE"
67       fi
68     done <"$chan_db" >"${chan_db}.$$"
69     mv -- "${chan_db}.$$" "${chan_db}"
70     RELEASE "$chan_db"
71   else
72     return 1
73   fi
74 }
75
76 AUTHOR(){
77   if [ "$CHANNEL_ID" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
78     return 0
79   else
80     return 1
81   fi
82 }
83
84 # Video
85 # ID    NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS  UPLOADER        HITS
86
87 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
88   newchannel)
89     channel="$(POST channel |checkid)"
90     if [ ! "$USER_ID" ]; then
91       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOTALLOWED"
92     elif LOCK "$chan_db"; then
93       if grep -q '^${channel}   ' "$chan_db"; then
94         RELEASE "$chan_db"
95         REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_EXISTS"
96       else
97         printf '%s      \\      \\      \\      \\      %s      \\      \\\n' \
98                "$channel" "$(STRING "$USER_ID")" \
99                >>"$chan_db"
100         RELEASE "$chan_db"
101         REDIRECT "${_BASE}/channel/${channel}/edit"
102       fi
103     else
104       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK"
105     fi
106     ;;
107   update_channel)
108     if [ ! "$channel" ]; then
109       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
110     elif [ ! "$USER_ID" ]; then
111       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
112     elif ! AUTHOR; then
113       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
114     elif update_channel "$channel" "name=$(POST name)" \
115                         "description=$(POST description)" \
116                         "authors=$USER_ID"; then
117       REDIRECT "${_BASE}/channel/${channel}/"
118     else
119       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK"
120     fi
121     ;;
122   update_channel_cancel)
123     REDIRECT "${_BASE}/channel/${channel}/"
124     ;;
125   newvideo)
126     video="$(POST video |checkid)"
127
128     AUTHOR \
129     && mkdir -p -- "${_DATA}/${channel}/"
130
131     if [ ! "$video" ]; then
132       REDIRECT "${_BASE}/channel/${channel}/#ERROR_INVALID_ID"
133     elif [ ! "$channel" ]; then
134       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
135     elif [ ! "$USER_ID" ]; then
136       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
137     elif ! AUTHOR; then
138       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
139     elif LOCK "$vid_db"; then
140       if grep -q "^${video}     " "$vid_db"; then
141         RELEASE "$vid_db"
142         REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_EXISTS"
143       else
144                 # ID    NAME    DESC    RESX    RESY    LENGTH  COVER   STATUS  UPLOADER HITS   FUTUREUSE
145         printf '%s      \\      \\      \\      \\      \\      \\      void    %s      \\      \\\n' \
146                "$video" "$(STRING "$USER_ID")" \
147                >>"$vid_db"
148         RELEASE "$vid_db"
149         REDIRECT "${_BASE}/channel/${channel}/${video}/edit"
150       fi
151     else
152       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_NOLOCK"
153     fi
154     ;;
155 esac
156
157 w_video(){
158   local thumb
159   local VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
160         VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
161         VIDEO_DESCR_CACHE VIDEO_FUTUREUSE
162
163   if read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
164              VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
165              VIDEO_DESCR_CACHE VIDEO_FUTUREUSE; then
166            VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
167     VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
168     VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
169          VIDEO_LENGTH="$(printf %i "$VIDEO_LENGTH" 2>&-)"
170
171     [ "${VIDEO_STATUS}" = public ] || AUTHOR || return 0
172
173     thumb="${_BASE}/video/${CHANNEL_ID}/${VIDEO_ID}_thumb.jpg"
174     [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
175     printf '[div .video .thumb
176               [a href="%s/channel/%s/%s/"
177                 [h3 . %s]
178                 [figure [img src="%s" alt=""]]
179                 [span .duration . %i:%02i]
180                 [div .description . %s]
181               ]
182             ]' "$_BASE" "$CHANNEL_ID" "$VIDEO_ID" \
183                "$(HTML "${VIDEO_NAME:-(Unnamed Video)}")" \
184                "$thumb" \
185                "$((VIDEO_LENGTH / 60))" "$((VIDEO_LENGTH % 60))" \
186                "$(UNSTRING "$DESCR_CACHE")"
187   else
188     return 1
189   fi
190 }
191
192 w_channel(){
193   local vid_db
194   local CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
195         CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
196
197   if read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
198              CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
199              CHANNEL_FUTUREUSE; then
200            CHANNEL_NAME="$(UNSTRING "$CHANNEL_NAME")"
201     CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")"
202         CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")"
203     CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")"
204
205     vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
206     cat <<-EOF
207         [div .channel
208           [div .description
209             [h2 [a href="${_BASE}/channel/${CHANNEL_ID}/" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
210             ${CHANNEL_DESCR_CACHE}
211           ]$(
212             [ -f "$vid_db" -a -r "$vid_db" ] \
213             && while w_video; do :; done <"$vid_db"
214           )
215         ]
216         EOF
217   else
218     return 1
219   fi
220 }
221
222 w_channel_list(){
223   if [ $USER_ID ]; then
224     printf '
225     [form .channel .newchannel method=POST
226       [hidden "channel" "%s"]
227       [submit "action" "newchannel" New Channel]
228     ]' "$(timeid)"
229   fi
230   [ -f "$chan_db" -a -r "$chan_db" ] \
231   && while w_channel; do :; done <"$chan_db"
232 }
233
234 if [ "$channel" -a "$video" ]; then
235   . ${_EXEC}/page_video.sh
236 elif [ "$channel" -a "$action" = edit ]; then
237   AUTHOR || REDIRECT "${_BASE}/${channel}/#ERROR_EDIT_NOTALLOWED"
238   yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
239         [form .channel .edit method=POST
240           [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"]
241           [textarea name="description" placeholder="Description" . $(HTML "$CHANNEL_DESCRIPTION")]
242           [submit "action" "update_channel" . Update]
243           [submit "action" "update_channel_cancel" . Cancel]
244         ]
245         EOF
246 elif [ "$channel" ]; then
247   yield_page "$CHANNEL_NAME" "channel" <<-EOF
248         [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]
249           $(AUTHOR && printf ' - [a href="edit" edit]')
250         ]
251         [h1 .name $(HTML "$CHANNEL_NAME")]
252         [div .description . ${CHANNEL_DESCR_CACHE}]
253         [h1 .videos Videos]
254         [div .videos . $(
255           AUTHOR && printf '
256              [form .video .newvideo method=POST
257                [hidden "video" "%s"]
258                [submit "action" "newvideo" New Video]
259              ]' "$(timeid)"
260           [ -f "$vid_db" -a -r "$vid_db" ] \
261           && while w_video "$ID"; do :; done <"$vid_db"
262         )]
263         EOF
264 else
265   yield_page "Channels" "channels" <<-EOF
266         $(w_channel_list)
267         EOF
268 fi