tinycm view plugins/forum/forum.cgi @ rev 121
Remove ashism ==
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Feb 26 12:26:25 2019 +0100 (2019-02-26) |
parents | 12c983066e52 |
children |
line source
1 #!/bin/sh
2 #
3 # TinyCM/TazBug Plugin - Support/discussion forum using #hashtags
4 #
5 # Display a topic: ?forum&topic=NB
6 # d= is used by html_header to set page title
7 # t= is used to set the topic number
8 #
10 forum="$tiny/$content/forum"
12 # Forum tools
13 forum_tools() {
14 cat << EOT
15 <div id="tools">
16 <a href="$script?forum=edit&t=new">$(gettext "New post")</a>
17 $([ "$(GET t)" ] && echo "<a href='$script?forum'>Forum</a>")
18 $([ "$index" = "forum" ] && echo "<a href='$script?d=index'>Index</a>")
19 $([ "$HG" = "yes" ] && echo "<a href='$script?hg'>Hg Log</a>")
20 </div>
21 EOT
22 }
24 # Topic tools for admin and author
25 topic_tools() {
26 cat << EOT
27 - <a href="?forum=edit&t=${t}">$(gettext "Edit it!")</a>
28 || <a href="$script?forum=rm&t=${t}">$(gettext "Remove")</a>
29 EOT
30 }
32 # Create a XML feed for a new topic
33 gen_rss() {
34 pubdate=$(date "+%a, %d %b %Y %X")
35 title="Forum: $(fgrep '====' ${forum}/${t}/topic.txt | sed s'/====//'g)"
36 cat > ${forum}/${t}/topic.xml << EOT
37 <item>
38 <title>$title</title>
39 <link>http://${SERVER_NAME}?forum=topic&t=$t</link>
40 <guid>topic-$t</guid>
41 <pubDate>$pubdate</pubDate>
42 <description>$hashtags</description>
43 </item>
44 EOT
45 }
47 new_msg() {
48 if check_auth; then
49 cat << EOT
50 <div id="edit">
51 <h3>$(gettext "New message")</h3>
52 <form method="get" action="$script" name="editor">
53 <textarea name="message"></textarea>
54 <input type="hidden" name="forum" value="msg" />
55 <input type="hidden" name="t" value="$t" />
56 <input type="submit" value="$(gettext 'Send message')" />
57 $(gettext "Wiki syntax is supported") -
58 <a href="?d=en/help">$(gettext "Help page")</a>
59 </form>
60 </div>
61 EOT
62 fi
63 }
65 # Display a forum topic with messages: show_topic NB
66 show_topic() {
67 t=${1%.txt}
68 . ${forum}/${t}/topic.conf
70 if [ -f "${PEOPLE}/${AUTHOR}/account.conf" ]; then
71 . ${PEOPLE}/${AUTHOR}/account.conf
72 else
73 echo "ERROR: ${PEOPLE}/${AUTHOR}/account.conf"
74 fi
76 echo "<div class='blogpost'>"
77 cat ${forum}/${t}/topic.txt | wiki_parser
78 cat << EOT
79 <div class="post-tools">
80 <a href="$script?user=$USER">$(get_gravatar $MAIL 24)</a>
81 <span class="date">$DATE</span>
82 EOT
84 # Topic tools for admin users (edit, remove)
85 if check_auth && admin_user; then
86 topic_tools
87 echo "</div>"
88 else
89 echo "</div>"
90 fi
91 echo "</div>"
93 # Hashtags
94 if [ -f "${forum}/${t}/hashtags.txt" ]; then
95 echo "<h3>Hashtags</h3>"
96 echo "<div id='hashtags'>"
97 link_hashtags
98 echo "</div>"
99 fi
100 }
102 # Display blog post: show_posts count
103 show_topics() {
104 for t in $(ls $forum | sort -r -n | head -n $1)
105 do
106 . ${forum}/${t}/topic.conf
107 . ${PEOPLE}/${AUTHOR}/account.conf
108 title="$(fgrep '====' ${forum}/${t}/topic.txt | sed s'/====//'g)"
109 cat << EOT
110 <div class="box topic">
111 <a href="?user=${AUTHOR}">$(get_gravatar $MAIL 24)</a>
112 <span class="date">$DATE</span> : <a href="?forum=topic&t=${t}">$title</a>
113 <span>
114 EOT
115 link_hashtags
116 echo "</span></div>"
117 done
118 }
120 show_messages() {
121 msgs=$(ls -1 $forum/$t/msg.*.txt | wc -l)
122 echo "<h3>$(gettext 'Messages'): $msgs</h3>"
123 [ "$msgs" = 0 ] && echo "<p>$(gettext 'Be the first to post a message!')<p/>"
124 for msg in $(ls -1tr $forum/$t/msg.*.conf)
125 do
126 . ${msg}
127 rm=""
128 # User can delete his post.
129 if [ "$user" = "$AUTHOR" ]; then
130 rm="- <a href=\"?forum=msgrm&t=$t&msg=$ID\">$(gettext 'Remove')</a>"
131 fi
132 # Display user gravatar, date and message
133 cat << EOT
134 <p>
135 <a href="?user=$AUTHOR">$(get_gravatar $MAIL 24)</a>
136 <span class="date">$DATE</span> $rm
137 </p>
138 <div class="forum-msg">
139 $(cat ${msg%.conf}.txt | wiki_parser)
140 </div>
141 EOT
142 unset NAME DATE
143 done
144 }
146 # Create HTML link for a topic #hashtags
147 link_hashtags() {
148 for h in $(cat ${forum}/${t}/hashtags.txt)
149 do
150 echo "<a href='?forum=hashtag&h=${h#\#}'>${h}</a>"
151 done
152 }
154 #
155 # Handle GET requests
156 #
157 if [ "$(GET forum)" ]; then
158 case " $(GET forum) " in
159 *\ edit\ *)
160 t="$(GET t)"
161 d="Edit: $(fgrep '====' ${forum}/${t}/topic.txt | sed s'/====//'g)"
162 header
163 html_header
164 user_box
165 if ! check_auth; then
166 gettext "You must be logged in to create a new Forum post"
167 html_footer && exit 0
168 fi
169 # New post
170 if [ "$t" = "new" ]; then
171 date=$(date '+%Y-%m-%d %H:%M')
172 last=$(ls -r $forum | head -n 1)
173 nb=${last%.txt}
174 t=$(($nb + 1))
175 conf=$(echo "====Title====")
176 else
177 hashtags="$(cat $forum/$t/hashtags.txt)"
178 fi
179 cat << EOT
180 <h2>$(gettext "Forum topic"): $t</h2>
182 <div id="edit">
183 <form method="get" action="$script" name="editor">
184 <input type="hidden" name="forum" value="save" />
185 <input type="hidden" name="t" value="$t" />
186 <textarea name="content">${conf}$(cat "$forum/$t/topic.txt")</textarea>
187 <div>
188 <b>#HashTags : </b>
189 <input type="text" name="hashtags" value="$hashtags" />
190 </div>
191 <input type="submit" value="$(gettext "Post topic")" />
192 $(gettext "Code Helper:")
193 $(cat lib/jseditor.html)
194 </form>
195 </div>
196 EOT
197 html_footer && exit 0 ;;
199 *\ save\ *)
200 t="$(GET t)"
201 hashtags="$(GET hashtags)"
202 if check_auth; then
203 [ -d "${forum}/${t}" ] || mkdir -p ${forum}/${t}
204 # New topic ?
205 if [ ! -f "${forum}/${t}/topic.txt" ]; then
206 echo "New Forum topic: <a href='$script?forum=topic&t=$t'>Read it!</a>" \
207 | log_activity
208 cat > ${forum}/${t}/topic.conf << EOT
209 # TinyCM Forum topic config file
210 AUTHOR="$user"
211 DATE="$(date '+%Y-%m-%d %H:%M')"
212 STATUS="open"
213 EOT
214 gen_rss
215 fi
216 # Write content and #hashtags to file
217 sed "s/$(echo -en '\r') /\n/g" > ${forum}/${t}/topic.txt << EOT
218 $(GET content)
219 EOT
220 echo "${hashtags}" > ${forum}/${t}/hashtags.txt
221 fi
222 header "Location: $script?forum=topic&t=$t" ;;
224 *\ msgrm\ *)
225 # Remove a message
226 t="$(GET t)"
227 if check_auth; then
228 rm -f ${forum}/${t}/msg."$(GET msg)".*
229 fi
230 header "Location: $script?forum=topic&t=$t" ;;
232 *\ msg\ *)
233 t="$(GET t)"
234 if check_auth; then
235 date=$(date "+%Y-%m-%d %H:%M")
236 msgs=$(ls -1 $forum/$t/msg.*.txt | wc -l)
237 count=$(($msgs + 1))
238 # Write config file
239 cat > ${forum}/${t}/msg.${count}.conf << EOT
240 # TinyCM Forum message config file
241 AUTHOR="$user"
242 DATE="$(date '+%Y-%m-%d %H:%M')"
243 ID="$count"
244 EOT
245 # Write message to file
246 sed "s/$(echo -en '\r') /\n/g" > ${forum}/${t}/msg.${count}.txt << EOT
247 $(GET message)
248 EOT
249 fi
250 header "Location: $script?forum=topic&t=$t" ;;
252 *\ rm\ *)
253 # Remove a forum topic
254 t="$(GET t)"
255 if [ -d "${forum}/${t}" ]; then
256 rm -rf ${forum}/${t}
257 fi
258 header "Location: $script?forum" ;;
260 *\ hashtag\ *)
261 # Display a hashtag
262 hashtag="$(GET h)"
263 header
264 html_header
265 user_box
266 echo "<h2>Forum Hashtag #$hashtag</h2>"
267 for h in $(fgrep -l "#$hashtag" ${forum}/*/hashtags.txt)
268 do
269 path=$(dirname ${h})
270 t=$(basename $path)
271 . ${path}/topic.conf
272 title="$(fgrep '====' $(dirname ${h})/topic.txt | sed s'/====//'g)"
273 cat << EOT
274 <div class='box topic'>
275 <span class='date'>$DATE</span> - <a href='?forum=topic&t=$t'>$title</a>
276 </div>
277 EOT
278 done
279 html_footer && exit 0 ;;
281 *\ topic\ *)
282 # Single post: get title from topic content before html_header
283 t="$(GET t)"
284 d="Forum: $(fgrep '====' ${forum}/${t}/topic.txt | sed s'/====//'g)"
285 header
286 html_header
287 user_box
288 # Forum tools for auth users
289 [ check_auth ] && forum_tools
290 show_topic "$t"
291 show_messages
292 new_msg
293 html_footer && exit 0 ;;
295 *)
296 d="$(gettext 'Forum Topics')"
297 count="20"
298 header
299 html_header
300 user_box
301 # Exit if plugin is disabled
302 if [ ! -d "$forum" ]; then
303 echo "<pre class='error box'>"
304 gettext "Forum plugin is not yet active."; echo
305 echo "Command: install -m 0777 -d $forum</pre>"
306 html_footer && exit 0
307 fi
308 echo $(GET topic)
309 # Forum tools for auth users
310 [ check_auth ] && forum_tools
311 echo "<h2>$d</h2>"
312 show_topics ${count} ;;
313 esac
314 html_footer && exit 0
315 fi