tazbug view web/bugs.cgi @ rev 39

bug 43 again...
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 20 10:19:35 2012 +0100 (2012-12-20)
parents 2ccd2cd18ef5
children e404b4141804
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/bugs.conf" ] && . /etc/slitaz/bugs.conf
10 # Internal variable
11 bugdir="$TAZBUG/bug"
12 plugins="plugins"
13 sessions="/tmp/bugs/sessions"
14 po=""
16 # Content negotiation for Gettext
17 IFS=","
18 for lang in $HTTP_ACCEPT_LANGUAGE
19 do
20 lang=${lang%;*} lang=${lang# } lang=${lang%-*}
21 case "$lang" in
22 en) LANG="C" ;;
23 de) LANG="de_DE" ;;
24 es) LANG="es_ES" ;;
25 fr) LANG="fr_FR" ;;
26 it) LANG="it_IT" ;;
27 pt) LANG="pt_BR" ;;
28 ru) LANG="ru_RU" ;;
29 zh) LANG="zh_TW" ;;
30 esac
31 if echo "$po" | fgrep -q "$lang"; then
32 break
33 fi
34 done
35 unset IFS
36 export LANG LC_ALL=$LANG
38 # Internationalization: $(gettext "")
39 . /usr/bin/gettext.sh
40 TEXTDOMAIN='tazbug'
41 export TEXTDOMAIN
48 #
49 # Functions
50 #
52 # HTML 5 header.
53 html_header() {
54 cat lib/header.html
55 }
57 # HTML 5 footer.
58 html_footer() {
59 cat << EOT
60 </div>
62 <div id="footer">
63 <a href="$WEB_URL">SliTaz Bugs</a> -
64 <a href="$WEB_URL?README">README</a>
65 </div>
67 </body>
68 </html>
69 EOT
70 }
72 GETfiltered()
73 {
74 GET $1 | sed -e "s/'/\&#39;/g; s|\n|<br/>|g; s/\t/\&#09;/g;s/\%22/\"/g"
75 }
77 js_redirection_to()
78 {
79 js_log "Redirecting to $1"
80 echo "<script type=\"text/javascript\"> document.location = \"$1\"; </script>"
81 }
84 js_log()
85 {
86 echo "<script type=\"text/javascript\">console.log('$1')</script>";
87 }
90 js_set_cookie()
91 {
92 name=$1
93 value=$2
95 js_log 'Setting cookie.'
96 echo "<script type=\"text/javascript\">"
97 echo "document.cookie = \"$name=$value; expires=0; path=/\"";
98 echo "</script>"
99 }
102 js_unset_cookie()
103 {
104 name=$1
106 js_log 'Unsetting cookie.'
107 echo "<script type=\"text/javascript\">"
108 echo "document.cookie = \"$1=\"\"; expires=-1; path=/";
109 echo "</script>"
110 }
113 # Check if user is auth
114 check_auth() {
115 auth="$(COOKIE auth)"
116 user="$(echo $auth | cut -d ":" -f 1)"
117 md5cookie="$(echo $auth | cut -d ":" -f 2)"
118 [ -f "$sessions/$user" ] && md5session="$(cat $sessions/$user)"
119 if [ "$md5cookie" == "$md5session" ] && [ "$auth" ]; then
120 return 0
121 else
122 return 1
123 fi
124 }
127 # Authentified or not
128 user_box() {
130 IDLOC=""
131 if [[ "$(GET id)" ]] ;then
132 IDLOC="&id=$(GET id)"
133 fi
135 if check_auth; then
136 . $PEOPLE/$user/account.conf
137 cat << EOT
138 <div id="user">
139 <a href="?user=$user">$(get_gravatar $MAIL 20)</a>
140 <a href="?logout">$(gettext 'Log out')</a>
141 </div>
142 EOT
143 else
144 cat << EOT
145 <div id="user">
146 <a href="?login$IDLOC"><img src="images/avatar.png" alt="[ User ]" /></a>
147 <a href="?login$IDLOC">$(gettext 'Log in')</a>
148 </div>
149 EOT
150 fi
151 cat << EOT
153 <div id="search">
154 <form method="get" action="$WEB_URL">
155 <input type="text" name="search" placeholder="$(gettext 'Search')" />
156 <!-- <input type="submit" value="$(gettext 'Search')" /> -->
157 </form>
158 </div>
160 <!-- Content -->
161 <div id="content">
163 EOT
164 }
167 # Login page
168 login_page() {
169 IDLOC=""
170 if [[ "$(GET id)" ]] ;then
171 IDLOC="?id=$(GET id)"
172 fi
174 cat << EOT
175 <h2>$(gettext 'Login')</h2>
177 <div id="account-info">
178 <p>$(gettext "No account yet? Please signup using the SliTaz Bugs reporter \
179 on your SliTaz system.")</p>
180 <p>$(gettext "Tip: to attach big files or images, you can use SliTaz Paste \
181 services:") <a href="http://paste.slitaz.org/">paste.slitaz.org</a></p>
182 </div>
184 <div id="login">
185 <form method="post" action="$SCRIPT_NAME">
186 <input type="text" name="auth" placeholder="$(gettext 'User name')" />
187 <input type="password" name="pass" placeholder="$(gettext 'Password')" />
188 <div>
189 <input type="submit" value="$(gettext 'Log in')" />
190 $error
191 </div>
192 </form>
193 </div>
195 <div style="clear: both;"></div>
196 EOT
197 }
200 # Display user public profile.
201 public_people() {
202 cat << EOT
203 <pre>
204 $(eval_gettext 'Real name : $NAME')
205 </pre>
206 EOT
207 }
210 # Display authentified user profile. TODO: change password
211 auth_people() {
212 cat << EOT
213 <pre>
214 $(eval_gettext 'Real name : $NAME')
215 $(eval_gettext 'Email : $MAIL')
216 $(eval_gettext 'Secure key : $KEY')
217 </pre>
218 EOT
219 }
222 # Usage: list_bugs STATUS
223 list_bugs() {
224 bug="$1"
225 echo "<h3>$(eval_gettext '$bug Bug')</h3>"
226 for pr in critical standard
227 do
228 for bug in $(fgrep -H "$1" $bugdir/*/bug.conf | cut -d ":" -f 1)
229 do
230 . $bug
231 id=$(basename $(dirname $bug))
232 if [ "$PRIORITY" == "$pr" ]; then
233 cat << EOT
234 <pre>
235 $(gettext 'Bug title :') <strong>$BUG</strong> <a href="?id=$id">$(gettext 'Show')</a>
236 $(gettext 'ID - Date :') $id - $DATE
237 $(gettext 'Creator :') <a href="?user=$CREATOR">$CREATOR</a>
238 </pre>
239 EOT
240 fi
241 done
242 done
243 }
246 # Stripped down Wiki parser for bug desc and messages which are simply
247 # displayed in <pre>
248 wiki_parser() {
249 sed \
250 -e s"#http://\([^']*\).png#<img src='\0' alt='[ Image ]' />#"g \
251 -e s"#http://\([^']*\).*# <a href='\0'>\1</a>#"g
252 }
255 # Bug page
256 bug_page() {
257 if [ -f "$PEOPLE/$CREATOR/account.conf" ]; then
258 . $PEOPLE/$CREATOR/account.conf
259 else
260 MAIL="default"
261 fi
262 cat << EOT
263 <h2>$(eval_gettext 'Bug $id')</h2>
264 <form method="get" action="$WEB_URL">
266 <p>
267 $(get_gravatar $MAIL 32)
268 <strong>$STATUS</strong>
269 $BUG - $DATE -
270 $(eval_gettext 'Priority $PRIORITY') -
271 $(eval_ngettext '$msgs message' '$msgs messages' $msgs)
272 </p>
274 <pre>
275 $(echo "$DESC" | wiki_parser)
276 </pre>
278 <div id="tools">
279 EOT
280 if check_auth; then
281 if [ "$STATUS" == "OPEN" ]; then
282 cat << EOT
283 <a href="?id=$id&amp;close">$(gettext "Close bug")</a>
284 <a href="?edit=$id">$(gettext "Edit bug")</a>
285 EOT
286 else
287 cat << EOT
288 <a href="?id=$id&amp;open">$(gettext "Re open bug")</a>
289 EOT
290 fi
291 fi
292 cat << EOT
293 </div>
295 <h3>$(gettext "Messages")</h3>
296 EOT
297 [ "$msgs" == "0" ] && gettext "No messages"
298 for msg in $(ls -1tr $bugdir/$id/msg.*)
299 do
300 . $msg
301 if [ "$MSG" ]; then
302 msgid=$(echo $msg | cut -d "." -f 2)
303 del=""
304 # User can delete his post.
305 [ "$user" == "$USER" ] && \
306 del="<a href=\"?id=$id&amp;delmsg=$msgid\">delete</a>"
307 cat << EOT
308 <p><strong>$USER</strong> $DATE $del</p>
309 <pre>
310 $(echo "$MSG" | wiki_parser)
311 </pre>
312 EOT
313 fi
314 unset NAME DATE MSG
315 done
316 if check_auth; then
317 cat << EOT
318 <div>
319 <h3>$(gettext "New message")</h3>
321 <input type="hidden" name="id" value="$id" />
322 <textarea name="msg" rows="8"></textarea>
323 <p><input type="submit" value="$(gettext 'Send message')" /></p>
324 </form>
325 </div>
326 EOT
327 fi
328 }
331 # Write a new message
332 new_msg() {
333 date=$(date "+%Y-%m-%d %H:%M")
334 msgs=$(ls -1 $bugdir/$id/msg.* | wc -l)
335 count=$(($msgs + 1))
336 if check_auth; then
337 USER="$user"
338 fi
339 js_log "Will write message in $bugdir/$id/msg.$count "
340 sed "s/$(echo -en '\r') /\n/g" > $bugdir/$id/msg.$count << EOT
341 USER="$USER"
342 DATE="$date"
343 MSG="$(GETfiltered msg)"
344 EOT
345 }
348 # Create a new Bug
349 new_bug() {
350 count=$(ls -1 $bugdir | wc -l)
351 date=$(date "+%Y-%m-%d %H:%M")
352 # Sanity check, JS may be disabled.
353 [ ! "$(GET bug)" ] && echo "Missing bug title" && exit 1
354 [ ! "$(GET desc)" ] && echo "Missing bug description" && exit 1
355 if check_auth; then
356 USER="$user"
357 fi
358 mkdir -p $bugdir/$count
359 sed "s/$(echo -en '\r') /\n/g" > $bugdir/$count/bug.conf << EOT
360 # SliTaz Bug configuration
362 BUG="$(GETfiltered bug)"
363 STATUS="OPEN"
364 PRIORITY="$(GET priority)"
365 CREATOR="$USER"
366 DATE="$date"
367 PKGS="$(GETfiltered pkgs)"
369 DESC="$(GETfiltered desc)"
370 EOT
371 }
374 # New bug page for the web interface
375 new_bug_page() {
376 cat << EOT
377 <h2>$(gettext "New Bug")</h2>
378 <div id="newbug">
380 <form method="get" action="$WEB_URL" onsubmit="return checkNewBug();">
381 <input type="hidden" name="addbug" />
382 <table>
383 <tbody>
384 <tr>
385 <td>$(gettext "Bug title")*</td>
386 <td><input type="text" name="bug" /></td>
387 </tr>
388 <tr>
389 <td>$(gettext "Description")*</td>
390 <td><textarea name="desc"></textarea></td>
391 </tr>
392 <tr>
393 <td>$(gettext "Packages")</td>
394 <td><input type="text" name="pkgs" /></td>
395 </tr>
396 <tr>
397 <td>$(gettext "Priority")</td>
398 <td>
399 <select name="priority">
400 <option value="standard">$(gettext "Standard")</option>
401 <option value="critical">$(gettext "Critical")</option>
402 </select>
403 <input type="submit" value="$(gettext "Create Bug")" />
404 </td>
405 </tr>
406 </tbody>
407 </table>
408 </form>
410 <p>
411 $(gettext "* field is obligatory. You can also specify affected packages.")
412 </p>
414 </div>
415 EOT
416 }
419 # Edit/Save a bug configuration file
420 edit_bug() {
421 cat << EOT
422 <h2>$(eval_gettext 'Edit Bug $bug')</h2>
423 <div id="edit">
425 <form method="get" action="$WEB_URL">
426 <textarea name="bugconf">$(cat $bugdir/$bug/bug.conf)</textarea>
427 <input type="hidden" name="bug" value="$bug" />
428 <input type="submit" value="$(gettext 'Save configuration')" />
429 </form>
431 </div>
432 EOT
433 }
436 save_bug() {
437 bug="$(GET bug)"
438 content="$(GET bugconf)"
439 sed "s|\"|'|" | sed "s/$(echo -en '\r') /\n/g" > $bugdir/$bug/bug.conf << EOT
440 $content
441 EOT
442 }
445 # Close a fixed bug
446 close_bug() {
447 sed -i s'/OPEN/CLOSED/' $bugdir/$id/bug.conf
448 }
451 # Re open an old bug
452 open_bug() {
453 sed -i s'/CLOSED/OPEN/' $bugdir/$id/bug.conf
454 }
457 # Get and display Gravatar image: get_gravatar email size
458 # Link to profile: <a href="http://www.gravatar.com/$md5">...</a>
459 get_gravatar() {
460 email=$1
461 size=$2
462 [ "$size" ] || size=48
463 url="http://www.gravatar.com/avatar"
464 md5=$(md5crypt $email)
465 echo "<img src=\"$url/$md5?d=identicon&amp;s=$size\" alt=\"\" />"
466 }
469 # Create a new user in AUTH_FILE and PEOPLE
470 new_user_config() {
471 mail="$(GET mail)"
472 pass="$(GET pass)"
473 key=$(echo -n "$user:$mail:$pass" | md5sum | awk '{print $1}')
474 echo "Server Key generated"
475 echo "$user:$pass" >> $AUTH_FILE
476 mkdir -pm0700 $PEOPLE/$user/
477 cat > $PEOPLE/$user/account.conf << EOT
478 # SliTaz user configuration
479 #
481 NAME="$(GET name)"
482 USER="$user"
483 MAIL="$mail"
484 KEY="$key"
486 COMMUNITY="$(GET scn)"
487 LOCATION="$(GET location)"
488 RELEASES="$(GET releases)"
489 PACKAGES="$(GET packages)"
490 EOT
491 chmod 0600 $PEOPLE/$user/account.conf
492 if [ ! -f $PEOPLE/$user/account.conf ]; then
493 echo "ERROR: User creation failed!"
494 fi;
495 }
500 ###################################################
501 # POST actions
502 ###################################################
504 case " $(POST) " in
505 *\ auth\ *)
506 header
507 html_header
508 # Authenticate user. Create a session file in $sessions to be used
509 # by check_auth. We have the user login name and a peer session
510 # md5 string in the COOKIE.
511 user="$(POST auth)"
512 pass="$(echo -n "$(POST pass)" | md5sum | awk '{print $1}')"
514 IDLOC=""
515 if [[ "$(GET id)" ]] ;then
516 IDLOC="&id=$(GET id)"
517 fi
519 if [ ! -f $AUTH_FILE ] ; then
520 js_log "$AUTH_FILE (defined in \$AUTH_FILE) have not been found."
521 js_redirection_to "$WEB_URL?login$IDLOC"
522 fi;
524 valid=$(fgrep "${user}:" $AUTH_FILE | cut -d ":" -f 2)
525 if [ "$pass" == "$valid" ] && [ "$pass" != "" ]; then
526 if [[ "$(GET id)" ]] ;then
527 IDLOC="?id=$(GET id)"
528 fi
529 md5session=$(echo -n "$$:$user:$pass:$$" | md5sum | awk '{print $1}')
530 mkdir -p $sessions
531 echo "$md5session" > $sessions/$user
532 js_set_cookie 'auth' "$user:$md5session"
533 js_log "Login authentification have been executed & accepted :)"
534 js_redirection_to "$WEB_URL$IDLOC"
535 else
536 js_log "Login authentification have been executed & refused"
537 js_redirection_to "$WEB_URL?login&error$IDLOC"
538 fi
540 html_footer
541 ;;
542 esac
544 #
545 # Plugins
546 #
547 for p in $(ls -1 $plugins)
548 do
549 [ -f "$plugins/$p/$p.conf" ] && . $plugins/$p/$p.conf
550 [ -x "$plugins/$p/$p.cgi" ] && . $plugins/$p/$p.cgi
551 done
556 ###################################################
557 # GET actions
558 ###################################################
560 case " $(GET) " in
561 *\ README\ *)
562 header
563 html_header
564 user_box
565 echo '<h2>README</h2>'
566 echo '<pre>'
567 cat /usr/share/doc/tazbug/README
568 echo '</pre>'
569 html_footer ;;
570 *\ closed\ *)
571 # Show all closed bugs.
572 header
573 html_header
574 user_box
575 list_bugs CLOSED
576 html_footer ;;
577 *\ login\ *)
578 # The login page
579 [ "$(GET error)" ] && \
580 error="<span class='error'>$(gettext 'Bad login or pass')</span>"
581 header
582 html_header
583 user_box
584 login_page
585 html_footer ;;
586 *\ logout\ *)
587 header
588 html_header
589 if check_auth; then
590 rm -f "$sessions/$user"
591 js_unset_cookie 'auth'
592 js_redirection_to "$WEB_URL"
594 fi ;;
595 *\ user\ *)
596 # User profile
597 header
598 html_header
599 user_box
600 . $PEOPLE/"$(GET user)"/account.conf
601 echo "<h2>$(get_gravatar $MAIL) $(GET user)</h2>"
602 if check_auth && [ "$(GET user)" == "$user" ]; then
603 auth_people
604 else
605 public_people
606 fi
607 html_footer ;;
608 *\ newbug\ *)
609 # Add a bug from web interface.
610 header
611 html_header
612 user_box
613 if check_auth; then
614 new_bug_page
615 else
616 echo "<p>$(gettext 'You must be logged in to post a new bug')</p>"
617 fi
618 html_footer ;;
619 *\ addbug\ *)
620 # Add a bug from web interface.
621 header
622 html_header
623 if check_auth; then
624 new_bug
625 js_redirection_to "$WEB_URL?id=$count"
626 fi ;;
627 *\ edit\ *)
628 bug="$(GET edit)"
629 header
630 html_header
631 user_box
632 edit_bug
633 html_footer ;;
634 *\ bugconf\ *)
635 header
636 html_header
637 if check_auth; then
638 save_bug
639 js_redirection_to "$WEB_URL?id=$bug"
640 fi ;;
641 *\ id\ *)
642 # Empty deleted messages to keep msg count working.
643 header
644 html_header
645 id="$(GET id)"
646 [ "$(GET close)" ] && close_bug
647 [ "$(GET open)" ] && open_bug
648 [ "$(GET msg)" ] && new_msg
649 [ "$(GET delmsg)" ] && rm -f $bugdir/$id/msg.$(GET delmsg) && \
650 touch $bugdir/$id/msg.$(GET delmsg)
651 msgs=$(fgrep MSG= $bugdir/$id/msg.* | wc -l)
652 user_box
653 . $bugdir/$id/bug.conf
654 bug_page
655 html_footer ;;
656 *\ signup\ *)
657 # Signup
658 header "Content-type: text/plain;"
659 user="$(GET signup)"
660 echo "Requested user login : $user"
661 if fgrep -q "$user:" $AUTH_FILE; then
662 echo "ERROR: User already exists" && exit 1
663 else
665 echo "Creating account for : $(GET name)"
666 new_user_config
667 fi ;;
668 *\ key\ *)
669 # Let user post new bug or message with crypted key (no gettext)
670 #
671 # Testing only and is security acceptable ?
672 #
673 key="$(GET key)"
674 id="$(GET bug)"
675 header "Content-type: text/plain;"
676 echo "Checking secure key..."
677 if fgrep -qH $key $PEOPLE/*/account.conf; then
678 conf=$(fgrep -H $key $PEOPLE/*/account.conf | cut -d ":" -f 1)
679 . $conf
680 echo "Authentified: $NAME ($USER)"
681 case " $(GET) " in
682 *\ msg\ *)
683 [ ! "$id" ] && echo "Missing bug ID" && exit 0
684 echo "Posting new message to bug: $id"
685 echo "Message: $(GET msg)"
686 new_msg ;;
687 *\ bug\ *)
688 echo "Adding new bug: $(GET bug)"
689 echo "Description: $(GET desc)"
690 new_bug ;;
691 esac
692 else
693 echo "Not a valid SliTaz user key"
694 exit 0
695 fi ;;
696 *\ search\ *)
697 header
698 html_header
699 user_box
700 cat << EOT
701 <h2>$(gettext "Search")</h2>
702 <form method="get" action="$WEB_URL">
703 <input type="text" name="search" />
704 <input type="submit" value="$(gettext 'Search')" />
705 </form>
706 <div>
707 EOT
709 #found=0 JS to notify or write results nb under the search box.
710 for bug in $bugdir/*
711 do
712 result=$(fgrep -i "$(GET search)" $bug/*)
713 if [ "$result" ]; then
714 #found=$(($found + 1))
715 id=${bug#bug/}
716 echo "<p><strong>Bug $id</strong> <a href=\"?id=$id\">"$(gettext 'Show')"</a></p>"
717 echo '<pre>'
718 fgrep -i "$(GET search)" $bugdir/$id/* | \
719 sed s"/$(GET search)/<span class='ok'>$(GET search)<\/span>/"g
720 echo '</pre>'
721 else
722 get_search=$(GET search)
723 echo "<p>$(eval_gettext 'No result found for: $get_search')</p>"
724 fi
725 done
726 echo '</div>'
727 html_footer ;;
728 *)
729 # Default page.
730 bugs=$(ls -1 $bugdir | wc -l)
731 close=$(fgrep "CLOSED" $bugdir/*/bug.conf | wc -l)
732 fixme=$(fgrep "OPEN" $bugdir/*/bug.conf | wc -l)
733 msgs=$(find $bugdir -name msg.* ! -size 0 | wc -l)
734 pct=0
735 [ $bugs -gt 0 ] && pct=$(( ($close * 100) / $bugs ))
736 header
737 html_header
738 user_box
739 cat << EOT
741 <h2>$(gettext "Summary")</h2>
743 <p>
744 $(eval_ngettext 'Bug: $bugs in total -' 'Bugs: $bugs in total -' $bugs)
745 $(eval_ngettext '$close fixed -' '$close fixed -' $close)
746 $(eval_ngettext '$fixme to fix -' '$fixme to fix -' $fixme)
747 $(eval_ngettext '$msgs message' '$msgs messages' $msgs)
748 </p>
750 <div class="pctbar">
751 <div class="pct" style="width: ${pct}%;">${pct}%</div>
752 </div>
754 <p>$(gettext "Please read the <a href=\"?README\">README</a> for help and more \
755 information. You may also be interested by the SliTaz \
756 <a href=\"http://roadmap.slitaz.org/\">Roadmap</a> and the packages \
757 <a href=\"http://cook.slitaz.org/\">Cooker</a>. To perform a search \
758 enter your term and press ENTER.")
759 </p>
761 <div id="tools">
762 <a href="?closed">$(gettext 'View closed bugs')</a>
763 EOT
764 if check_auth; then
765 echo "<a href='?newbug'>$(gettext 'Create a new bug')</a>"
766 fi
767 cat << EOT
768 </div>
769 EOT
770 list_bugs OPEN
771 html_footer ;;
772 esac
774 exit 0