tazbug view web/bugs.cgi @ rev 17

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