tazbug view web/bugs.cgi @ rev 30

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