tazbug view web/bugs.cgi @ rev 26

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