tazbug view web/bugs.cgi @ rev 29

Add plugins support
author Christophe Lincoln <pankso@slitaz.org>
date Wed Apr 11 17:42:48 2012 +0200 (2012-04-11)
parents c308afab2f37
children ea58d117d71a
line source
1 #!/bin/sh
2 #
3 # TazBug Web interface
4 #
5 # Copyright (C) 2012 SliTaz GNU/Linux - BSD License
6 #
7 . /usr/lib/slitaz/httphelper
8 [ -f "/etc/slitaz/tazbug.conf" ] && . /etc/slitaz/tazbug.conf
9 [ -f "../tazbug.conf" ] && . ../tazbug.conf
11 # Internal variable
12 bugdir="bug"
13 plugins="plugins"
14 sessions="/tmp/tazbug/sessions"
16 # Content negotiation for Gettext
17 IFS=","
18 for lang in $HTTP_ACCEPT_LANGUAGE
19 do
20 lang=${lang%;*} lang=${lang# } lang=${lang%-*}
21 [ -d "$lang" ] && break
22 case "$lang" in
23 en) lang="C" ;;
24 fr) lang="fr_FR" ;;
25 ru) lang="ru_RU" ;;
26 esac
27 done
28 unset IFS
29 export LANG=$lang LC_ALL=$lang
31 # Internationalization: $(gettext "")
32 . /usr/bin/gettext.sh
33 TEXTDOMAIN='tazbug'
34 export TEXTDOMAIN
36 #
37 # Functions
38 #
40 # HTML 5 header.
41 html_header() {
42 cat lib/header.html
43 }
45 # HTML 5 footer.
46 html_footer() {
47 cat << EOT
48 </div>
50 <div id="footer">
51 <a href="./">SliTaz Bugs</a> -
52 <a href="./?README">README</a>
53 </div>
55 </body>
56 </html>
57 EOT
58 }
60 # Check if user is auth
61 check_auth() {
62 auth="$(COOKIE auth)"
63 user="$(echo $auth | cut -d ":" -f 1)"
64 md5cookie="$(echo $auth | cut -d ":" -f 2)"
65 [ -f "$sessions/$user" ] && md5session="$(cat $sessions/$user)"
66 if [ "$md5cookie" == "$md5session" ] && [ "$auth" ]; then
67 return 0
68 else
69 return 1
70 fi
71 }
73 # Authentified or not
74 user_box() {
75 if check_auth; then
76 . $PEOPLE/$user/account.conf
77 cat << EOT
78 <div id="user">
79 <a href="?user=$user">$(get_gravatar $MAIL 20)</a>
80 <a href="?logout">Logout</a>
81 </div>
82 EOT
83 else
84 cat << EOT
85 <div id="user">
86 <a href="?login"><img src="images/avatar.png" alt="[ User ]" /></a>
87 <a href="?login">Login</a>
88 </div>
89 EOT
90 fi
91 cat << EOT
93 <div id="search">
94 <form method="get" action="./">
95 <input type="text" name="search" placeholder="$(gettext "Search")" />
96 <!-- <input type="submit" value="$(gettext "Search")" /> -->
97 </form>
98 </div>
100 <!-- Content -->
101 <div id="content">
103 EOT
104 }
106 # Login page
107 login_page() {
108 cat << EOT
109 <!-- Content -->
110 <div id="content">
112 <h2>$(gettext "Login")</h2>
114 <div id="account-info">
115 $(gettext "No account yet? Please signup using the SliTaz Bugs reporter
116 on your SliTaz system. <p>Tip: to attach big files or images, you can use
117 SliTaz Paste services:") <a href="http://paste.slitaz.org/">paste.slitaz.org</a>
118 </p>
119 </div>
121 <div id="login">
122 <form method="post" action="$SCRIPT_NAME">
123 <input type="text" name="auth" placeholder="$(gettext "User name")" />
124 <input type="password" name="pass" placeholder="$(gettext "Password")" />
125 <div>
126 <input type="submit" value="Login" />
127 $error
128 </div>
129 </form>
130 </div>
132 <div style="clear: both;"></div>
133 EOT
134 }
136 # Display user public profile.
137 public_people() {
138 cat << EOT
139 <pre>
140 Real name : $NAME
141 </pre>
142 EOT
143 }
145 # Display authentified user profile. TODO: change password
146 auth_people() {
147 cat << EOT
148 <pre>
149 Real name : $NAME
150 Email : $MAIL
151 Secure key : $KEY
152 </pre>
153 EOT
154 }
156 # Usage: list_bugs STATUS
157 list_bugs() {
158 echo "<h3>$1 Bugs</h3>"
159 for pr in critical standard
160 do
161 for bug in $(fgrep -H "$1" $bugdir/*/bug.conf | cut -d ":" -f 1)
162 do
163 . $bug
164 id=$(dirname $bug | cut -d "/" -f 2)
165 if [ "$PRIORITY" == "$pr" ]; then
166 cat << EOT
167 <pre>
168 Bug title : <strong>$BUG</strong> <a href="?id=$id">Show</a>
169 ID - Date : $id - $DATE
170 Creator : <a href="?user=$CREATOR">$CREATOR</a>
171 </pre>
172 EOT
173 fi
174 done
175 done
176 }
178 # Stripped down Wiki parser for bug desc and messages which are simply
179 # displayed in <pre>
180 wiki_parser() {
181 sed \
182 -e s"#http://\([^']*\).png#<img src='\0' alt='[ Image ]' />#"g \
183 -e s"#http://\([^']*\).*# <a href='\0'>\1</a>#"g
184 }
186 # Bug page
187 bug_page() {
188 if [ -f "$PEOPLE/$CREATOR/account.conf" ]; then
189 . $PEOPLE/$CREATOR/account.conf
190 else
191 MAIL="default"
192 fi
193 cat << EOT
194 <h2>Bug $id</h2>
195 <form method="get" action="./">
197 <p>
198 $(get_gravatar $MAIL 32) <strong>$STATUS</strong> $BUG - $DATE - Priority $PRIORITY
199 - $msgs messages
200 </p>
202 <pre>
203 $(echo "$DESC" | wiki_parser)
204 </pre>
206 <div id="tools">
207 EOT
208 if check_auth; then
209 if [ "$STATUS" == "OPEN" ]; then
210 cat << EOT
211 <a href="?id=$id&amp;close">$(gettext "Close bug")</a>
212 <a href="?edit=$id">$(gettext "Edit bug")</a>
213 EOT
214 else
215 cat << EOT
216 <a href="?id=$id&amp;open">$(gettext "Re open bug")</a>
217 EOT
218 fi
219 fi
220 cat << EOT
221 </div>
223 <h3>$(gettext "Messages")</h3>
224 EOT
225 [ "$msgs" == "0" ] && gettext "No messages"
226 for msg in $(ls -1tr $bugdir/$id/msg.*)
227 do
228 . $msg
229 if [ "$MSG" ]; then
230 msgid=$(echo $msg | cut -d "." -f 2)
231 del=""
232 # User can delete his post.
233 [ "$user" == "$USER" ] && \
234 del="<a href=\"?id=$id&amp;delmsg=$msgid\">delete</a>"
235 cat << EOT
236 <p><strong>$USER</strong> $DATE $del</p>
237 <pre>
238 $(echo "$MSG" | wiki_parser)
239 </pre>
240 EOT
241 fi
242 unset NAME DATE MSG
243 done
244 if check_auth; then
245 cat << EOT
246 <div>
247 <h3>$(gettext "New message")</h3>
249 <input type="hidden" name="id" value="$id" />
250 <textarea name="msg" rows="8"></textarea>
251 <p><input type="submit" value="$(gettext "Send message")" /></p>
252 </form>
253 </div>
254 EOT
255 fi
256 }
258 # Write a new message
259 new_msg() {
260 date=$(date "+%Y-%m-%d %H:%M")
261 msgs=$(ls -1 $bugdir/$id/msg.* | wc -l)
262 count=$(($msgs + 1))
263 if check_auth; then
264 USER="$user"
265 fi
266 sed "s/$(echo -en '\r') /\n/g" > $bugdir/$id/msg.$count << EOT
267 USER="$USER"
268 DATE="$date"
269 MSG="$(GET msg)"
270 EOT
271 }
273 # Create a new Bug
274 new_bug() {
275 count=$(ls -1 $bugdir | wc -l)
276 date=$(date "+%Y-%m-%d %H:%M")
277 # Sanity check, JS may be disabled.
278 [ ! "$(GET bug)" ] && echo "Missing bug title" && exit 1
279 [ ! "$(GET desc)" ] && echo "Missing bug description" && exit 1
280 if check_auth; then
281 USER="$user"
282 fi
283 mkdir -p $bugdir/$count
284 sed "s/$(echo -en '\r') /\n/g" > $bugdir/$count/bug.conf << EOT
285 # SliTaz Bug configuration
287 BUG="$(GET bug)"
288 STATUS="OPEN"
289 PRIORITY="$(GET priority)"
290 CREATOR="$USER"
291 DATE="$date"
292 PKGS="$(GET pkgs)"
294 DESC="$(GET desc)"
295 EOT
296 }
298 # New bug page for the web interface
299 new_bug_page() {
300 cat << EOT
301 <h2>$(gettext "New Bug")</h2>
302 <div id="newbug">
304 <form method="get" action="./" onsubmit="return checkNewBug();">
305 <input type="hidden" name="addbug" />
306 <table>
307 <tbody>
308 <tr>
309 <td>$(gettext "Bug title")*</td>
310 <td><input type="text" name="bug" /></td>
311 </tr>
312 <tr>
313 <td>$(gettext "Description")*</td>
314 <td><textarea name="desc"></textarea></td>
315 </tr>
316 <tr>
317 <td>$(gettext "Packages")</td>
318 <td><input type="text" name="pkgs" /></td>
319 </tr>
320 <tr>
321 <td>$(gettext "Priority")</td>
322 <td>
323 <select name="priority">
324 <option value="standard">$(gettext "Standard")</option>
325 <option value="critical">$(gettext "Critical")</option>
326 </select>
327 <input type="submit" value="$(gettext "Create Bug")" />
328 </td>
329 </tr>
330 </tbody>
331 </table>
332 </form>
334 <p>
335 $(gettext "* field is obligatory. You can also specify affected packages.")
336 </p>
338 </div>
339 EOT
340 }
342 # Edit/Save a bug configuration file
343 edit_bug() {
344 cat << EOT
345 <h2>$(gettext "Edit Bug $bug")</h2>
346 <div id="edit">
348 <form method="get" action="./">
349 <textarea name="bugconf">$(cat $bugdir/$bug/bug.conf)</textarea>
350 <input type="hidden" name="bug" value="$bug" />
351 <input type="submit" value="$(gettext "Save configuration")" />
352 </form>
354 </div>
355 EOT
356 }
358 save_bug() {
359 bug="$(GET bug)"
360 content="$(GET bugconf)"
361 sed "s/$(echo -en '\r') /\n/g" > $bugdir/$bug/bug.conf << EOT
362 $content
363 EOT
364 }
366 # Close a fixed bug
367 close_bug() {
368 sed -i s'/OPEN/CLOSED/' $bugdir/$id/bug.conf
369 }
371 # Re open an old bug
372 open_bug() {
373 sed -i s'/CLOSED/OPEN/' $bugdir/$id/bug.conf
374 }
376 # Get and display Gravatar image: get_gravatar email size
377 # Link to profile: <a href="http://www.gravatar.com/$md5">...</a>
378 get_gravatar() {
379 email=$1
380 size=$2
381 [ "$size" ] || size=48
382 url="http://www.gravatar.com/avatar"
383 md5=$(md5crypt $email)
384 echo "<img src='$url/$md5?d=identicon&s=$size' alt='' />"
385 }
387 # Create a new user in AUTH_FILE and PEOPLE
388 new_user_config() {
389 mail="$(GET mail)"
390 pass="$(GET pass)"
391 key=$(echo -n "$user:$mail:$pass" | md5sum | awk '{print $1}')
392 echo "$user:$pass" >> $AUTH_FILE
393 mkdir -p $PEOPLE/$user/
394 cat > $PEOPLE/$user/account.conf << EOT
395 # SliTaz user configuration
396 #
398 NAME="$(GET name)"
399 USER="$user"
400 MAIL="$mail"
401 KEY="$key"
403 COMMUNITY="$(GET scn)"
404 LOCATION="$(GET location)"
405 RELEASES="$(GET releases)"
406 PACKAGES="$(GET packages)"
407 EOT
408 chmod 0600 $PEOPLE/$user/account.conf
409 }
411 #
412 # POST actions
413 #
415 case " $(POST) " in
416 *\ auth\ *)
417 # Authenticate user. Create a session file in $sessions to be used
418 # by check_auth. We have the user login name and a peer session
419 # md5 string in the COOKIE.
420 user="$(POST auth)"
421 pass="$(md5crypt "$(POST pass)")"
422 valid=$(fgrep "${user}:" $AUTH_FILE | cut -d ":" -f 2)
423 if [ "$pass" == "$valid" ] && [ "$pass" != "" ]; then
424 md5session=$(echo -n "$$:$user:$pass:$$" | md5sum | awk '{print $1}')
425 mkdir -p $sessions
426 echo "$md5session" > $sessions/$user
427 header "Location: $WEB_URL" \
428 "Set-Cookie: auth=$user:$md5session; HttpOnly"
429 else
430 header "Location: $WEB_URL?login&error"
431 fi ;;
432 esac
434 #
435 # Plugins
436 #
437 for p in $(ls -1 $plugins)
438 do
439 [ -f "$plugins/$p/$p.conf" ] && . $plugins/$p/$p.conf
440 [ -x "$plugins/$p/$p.cgi" ] && . $plugins/$p/$p.cgi
441 done
443 #
444 # GET actions
445 #
447 case " $(GET) " in
448 *\ README\ *)
449 header
450 html_header
451 user_box
452 echo '<h2>README</h2>'
453 echo '<pre>'
454 cat /usr/share/doc/tazbug/README
455 echo '</pre>'
456 html_footer ;;
457 *\ closed\ *)
458 # Show all closed bugs.
459 header
460 html_header
461 user_box
462 list_bugs CLOSED
463 html_footer ;;
464 *\ login\ *)
465 # The login page
466 [ "$(GET error)" ] && \
467 error="<span class="error">$(gettext "Bad login or pass")</span>"
468 header
469 html_header
470 user_box
471 login_page
472 html_footer ;;
473 *\ logout\ *)
474 # Set a Cookie in the past to logout.
475 expires="Expires=Wed, 01-Jan-1980 00:00:00 GMT"
476 if check_auth; then
477 rm -f "$sessions/$user"
478 header "Location: $WEB_URL" "Set-Cookie: auth=none; $expires; HttpOnly"
479 fi ;;
480 *\ user\ *)
481 # User profile
482 header
483 html_header
484 user_box
485 . $PEOPLE/"$(GET user)"/account.conf
486 echo "<h2>$(get_gravatar $MAIL) $(GET user)</h2>"
487 if check_auth && [ "$(GET user)" == "$user" ]; then
488 auth_people
489 else
490 public_people
491 fi
492 html_footer ;;
493 *\ newbug\ *)
494 # Add a bug from web interface.
495 header
496 html_header
497 user_box
498 if check_auth; then
499 new_bug_page
500 else
501 echo "<p>$(gettext "You must be logged in to post a new bug")</p>"
502 fi
503 html_footer ;;
504 *\ addbug\ *)
505 # Add a bug from web interface.
506 if check_auth; then
507 new_bug
508 header "Location: $WEB_URL?id=$count"
509 fi ;;
510 *\ edit\ *)
511 bug="$(GET edit)"
512 header
513 html_header
514 user_box
515 edit_bug
516 html_footer ;;
517 *\ bugconf\ *)
518 if check_auth; then
519 save_bug
520 header "Location: $WEB_URL?id=$bug"
521 fi ;;
522 *\ id\ *)
523 # Empty deleted messages to keep msg count working.
524 id="$(GET id)"
525 [ "$(GET close)" ] && close_bug
526 [ "$(GET open)" ] && open_bug
527 [ "$(GET msg)" ] && new_msg
528 [ "$(GET delmsg)" ] && rm -f $bugdir/$id/msg.$(GET delmsg) && \
529 touch $bugdir/$id/msg.$(GET delmsg)
530 msgs=$(fgrep MSG= $bugdir/$id/msg.* | wc -l)
531 header
532 html_header
533 user_box
534 . $bugdir/$id/bug.conf
535 bug_page
536 html_footer ;;
537 *\ signup\ *)
538 # Signup
539 header "Content-type: text/plain;"
540 user="$(GET signup)"
541 echo "Requested user login : $user"
542 if fgrep -q "$user:" $AUTH_FILE; then
543 echo "ERROR: User already exists" && exit 1
544 else
545 echo "Creating account for : $(GET name)"
546 new_user_config
547 fi ;;
548 *\ key\ *)
549 # Let user post new bug or message with crypted key (no gettext)
550 #
551 # Testing only and is security acceptable ?
552 #
553 key="$(GET key)"
554 id="$(GET bug)"
555 header "Content-type: text/plain;"
556 echo "Checking secure key..."
557 if fgrep -qH $key $PEOPLE/*/account.conf; then
558 conf=$(fgrep -H $key $PEOPLE/*/account.conf | cut -d ":" -f 1)
559 . $conf
560 echo "Authentified: $NAME ($USER)"
561 case " $(GET) " in
562 *\ msg\ *)
563 [ ! "$id" ] && echo "Missing bug ID" && exit 0
564 echo "Posting new message to bug: $id"
565 echo "Message: $(GET msg)"
566 new_msg ;;
567 *\ bug\ *)
568 echo "Adding new bug: $(GET bug)"
569 echo "Description: $(GET desc)"
570 new_bug ;;
571 esac
572 else
573 echo "Not a valid SliTaz user key"
574 exit 0
575 fi ;;
576 *\ search\ *)
577 header
578 html_header
579 user_box
580 cat << EOT
581 <h2>$(gettext "Search")</h2>
582 <form method="get" action="./">
583 <input type="text" name="search" />
584 <input type="submit" value="$(gettext "Search")" />
585 </form>
586 <div>
587 EOT
589 #found=0 JS to notify or write results nb under the search box.
590 for bug in $bugdir/*
591 do
592 result=$(fgrep -i "$(GET search)" $bug/*)
593 if [ "$result" ]; then
594 #found=$(($found + 1))
595 id=${bug#bug/}
596 echo "<p><strong>Bug $id</strong> <a href='?id=$id'>$(gettext "Show")</a></p>"
597 echo '<pre>'
598 fgrep -i "$(GET search)" $bugdir/$id/* | \
599 sed s"/$(GET search)/<span class='ok'>$(GET search)<\/span>/"g
600 echo '</pre>'
601 else
602 gettext "<p>No result found for:"; echo " $(GET search)</p>"
603 fi
604 done
605 echo '</div>'
606 html_footer ;;
607 *)
608 # Default page.
609 bugs=$(ls -1 $bugdir | wc -l)
610 close=$(fgrep "CLOSED" $bugdir/*/bug.conf | wc -l)
611 fixme=$(fgrep "OPEN" $bugdir/*/bug.conf | wc -l)
612 msgs=$(find $bugdir -name msg.* ! -size 0 | wc -l)
613 pct=0
614 [ $bugs -gt 0 ] && pct=$(( ($close * 100) / $bugs ))
615 header
616 html_header
617 user_box
618 cat << EOT
620 <h2>$(gettext "Summary")</h2>
622 <p>
623 Bugs: $bugs in total - $close fixed - $fixme to fix - $msgs messages
624 </p>
626 <div class="pctbar">
627 <div class="pct" style="width: ${pct}%;">${pct}%</div>
628 </div>
630 <p>
631 Please read the <a href="?README">README</a> for help and more
632 information. You may also be interested by the SliTaz
633 <a href="http://roadmap.slitaz.org/">Roadmap</a> and the packages
634 <a href="http://cook.slitaz.org/">Cooker</a>. To perform a search
635 enter your term and press ENTER.
636 </p>
638 <div id="tools">
639 <a href="?closed">View closed bugs</a>
640 EOT
641 if check_auth; then
642 echo "<a href='?newbug'>$(gettext "Create a new bug")</a>"
643 fi
644 cat << EOT
645 </div>
646 EOT
647 list_bugs OPEN
648 html_footer ;;
649 esac
651 exit 0