tazpanel view index.cgi @ rev 437

(fix) terminal for user, history support; (fix) keymap list (again); (fix) month selected in clock setup; (feauture) add user/root panel indicator and re-login button; (feature) do not hide menu items and buttons (possibly), just disable them (to see all possibilities).
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Apr 07 00:27:17 2015 +0300 (2015-04-07)
parents 678dd43435a3
children 9aeb8fa5beaa
line source
1 #!/bin/sh
2 #
3 # Main CGI interface for TazPanel. In on word: KISS. Use the main CSS form
4 # command so we are faster and do not load unneeded functions. If necessary
5 # you can use the lib/ dir to handle external resources.
6 #
7 # Copyright (C) 2011-2015 SliTaz GNU/Linux - BSD License
8 #
11 # Common functions from libtazpanel
13 . lib/libtazpanel
14 get_config
16 TITLE="TazPanel"
20 # Check whether a configuration file has been modified after installation
22 file_is_modified() {
23 grep -l " $1$" $INSTALLED/*/md5sum | while read file; do
25 # Found, but can we do diff?
26 [ "$(grep -h " $1$" $file)" != "$(md5sum $1)" ] || break
27 orig=$(dirname $file)/volatile.cpio.gz
28 zcat $orig 2>/dev/null | cpio -t 2>/dev/null | grep -q "^${1#/}$" || break
30 case "$2" in
31 diff)
32 tmp=$(mktemp -d)
33 ( cd $tmp; zcat $orig | cpio -id ${1#/} )
34 diff -abu $tmp$1 $1 | sed "s|$tmp||"
35 rm -rf $tmp;;
36 button)
37 echo -n '<button name="action" value="diff" data-icon="diff">'$(_ 'Differences')'</button>';;
38 esac
39 break
40 done
41 }
45 # OK status in table
47 ok_status_t() {
48 echo '<td><span class="diff-add" data-img="ok"></span></td></tr>'
49 }
53 # Terminal prompt
55 term_prompt() {
56 if [ "$user" == 'root' ]; then
57 local color1='color31' sign='#'
58 else
59 local color1='color32' sign='$'
60 fi
61 echo -n "<span class='$color1'>$user@$(hostname)</span>:<span class='color33'>"
62 pwd | sed "s|^$HOME|~|" | tr -d '\n'; echo -n "</span>$sign "
63 }
67 #
68 # Things to do before displaying the page
69 #
71 [ -n "$(GET panel_pass)" ] &&
72 sed -i s@/:root:.*@/:root:$(GET panel_pass)@ $HTTPD_CONF
78 #
79 # Commands
80 #
82 case " $(GET) " in
85 *\ exec\ *)
86 # Execute command and display its result in a terminal-like window
88 header; TITLE=$(gettext 'TazPanel - exec'); xhtml_header
90 exec="$(GET exec)"
91 font="${TERM_FONT:-monospace}"
92 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
94 cat <<EOT
95 <section>
96 <header>
97 $exec
98 $(back_button "$(GET back)" "$(GET back_caption)" "$(GET back_icon)")
99 </header>
100 <div class="term_">
101 <pre class="term $palette" style="font-family: '$font'">$($exec 2>&1 | htmlize | filter_taztools_msgs)</pre>
102 </div>
103 </section>
104 EOT
105 ;;
108 *\ file\ *)
109 #
110 # Handle files
111 #
112 header
113 file="$(GET file)"
114 action="$(POST action)"; [ -z "$action" ] && action="$(GET action)" # receive 'action' both on POST or GET
116 case $file in
117 *.html)
118 cat $file; exit 0 ;;
119 *)
120 TITLE=$(gettext 'TazPanel - File'); xhtml_header ;;
121 esac
123 case "$action" in
124 edit)
125 cat <<EOT
126 <section>
127 <header>
128 <span data-icon="edit">$file</span>
129 <form id="editform" method="post" action="?file=$file" class="nogap">
130 <button data-icon="save">$(gettext 'Save')</button>
131 <button name="action" value="diff" data-icon="diff">$(gettext 'Differences')</button>
132 </form>
133 </header>
134 <textarea form="editform" name="content" class="wide" rows="30" autofocus>$(cat $file | htmlize)</textarea>
135 </section>
136 EOT
137 #The space before textarea gets muddled when the form is submitted.
138 #It prevents anything else from getting messed up
139 ;;
141 setvar)
142 data="$(. $(GET file) ;eval echo \$$(GET var))"
143 cat <<EOT
144 <section>
145 <header>$(GET var)</header>
146 <form method="post" action="?file=$file" class="nogap">
147 <input type="hidden" name="var" value="$(GET var)">
148 <input type="text" name="content" value="${data:-$(GET default)}">
149 <button type="submit" data-icon="save">$(gettext 'Save')</button>
150 </form>
151 </section>
152 EOT
153 ;;
155 diff)
156 cat <<EOT
157 <section>
158 <pre id="diff">$(file_is_modified $file diff | syntax_highlighter diff)</pre>
159 </section>
160 EOT
161 ;;
163 *)
164 R=$(echo -en '\r')
165 if [ -n "$(POST content)" ]; then
166 if [ -n "$(POST var)" ]; then
167 sed -i "s|^\\($(POST var)=\\).*|\1\"$(POST content)\"|" $file
168 else
169 sed "s/$R /\n/g;s/$R%0//g" > $file <<EOT
170 $(POST content)
171 EOT
172 fi
173 fi
175 cat <<EOT
176 <section>
177 EOT
178 [ -w "$file" ] && cat <<EOT
179 <header>
180 <span data-icon="text">$file</span>
181 <form>
182 <input type="hidden" name="file" value="$file"/>
183 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button><!--
184 -->$(file_is_modified $file button)
185 </form>
186 </header>
187 EOT
188 cat <<EOT
189 <div>
190 <pre>
191 EOT
192 # Handle file type by extension as a Web Server does it.
193 case "$file" in
194 *.conf|*.lst)
195 syntax_highlighter conf ;;
196 *.sh|*.cgi)
197 syntax_highlighter sh ;;
198 *Xorg.0.log)
199 syntax_highlighter xlog ;;
200 *dmesg.log)
201 syntax_highlighter kernel ;;
202 *)
203 cat | htmlize ;;
204 esac < $file
205 cat <<EOT
206 </pre>
207 </div>
208 </section>
209 EOT
210 esac
211 ;;
215 *\ terminal\ *|*\ cmd\ *)
216 # Cmdline terminal
218 header; TITLE=$(gettext 'TazPanel - Terminal'); xhtml_header
220 user="$REMOTE_USER"
221 HOME="$(awk -F: -vu=$user '$1==u{print $6}' /etc/passwd)"
222 historyfile="$HOME/.ash_history"
224 cmd=$(GET cmd)
225 path="$(GET path)"; path="${path:-/tmp}"; cd "$path"
227 font="${TERM_FONT:-monospace}"
228 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
230 [ -n "$cmd" -a "$cmd" != "$(tail -n1 $historyfile)" ] && echo "$cmd" >> $historyfile
233 # Terminal history
235 if [ "$cmd" == 'history' ]; then
236 cat <<EOT
237 <section>
238 <header>
239 $(gettext 'History')
240 <form><button name="terminal" data-icon="terminal">$(gettext 'Back')</button></form>
241 </header>
242 <form>
243 <input type="hidden" name="path" value="$path"/>
244 <pre class="term $palette" style="font-family: '$font'">
245 EOT
246 htmlize < $historyfile | awk -vrun="$(gettext 'run')" -vpath="$path" '
247 BEGIN { num=1 }
248 {
249 printf("%3d ", num);
250 cmd = $0
251 gsub("%", "%25", cmd); gsub("+", "%2B", cmd); gsub(" ", "+", cmd);
252 gsub("\"", "%22", cmd); gsub("!", "%21", cmd); gsub("'\''", "%27", cmd);
253 printf("<a data-icon=\"run\" href=\"?cmd=%s&path=%s\">%s</a> ", cmd, path, run);
254 printf("<input type=\"checkbox\" name=\"rm\" value=\"%d\" id=\"hist%d\">", num, num);
255 printf("<label for=\"hist%d\">%s</label>\n", num, $0);
256 num++
257 }'
258 cat <<EOT
259 </pre>
260 <footer>
261 <button name="rmhistory" data-icon="remove">$(gettext 'Clear')</button>
262 </footer>
263 </form>
264 </section>
265 EOT
266 xhtml_footer
267 exit 0
268 fi
271 # Terminal window
273 cat <<EOT
274 <span id="num_hist"></span>
275 <section>
276 <pre class="term $palette" style="font-family: '$font'" onclick="document.getElementById('typeField').focus()">
277 EOT
278 if [ -n "$cmd" ]; then
279 term_prompt
280 echo "$cmd" | htmlize
281 fi
283 case "$cmd" in
284 usage|help)
285 gettext 'Small non-interactive terminal emulator.'; echo
286 gettext 'Run any command at your own risk, avoid interactive commands (nano, mc, ...)'; echo
287 ;;
288 wget*)
289 dl=/var/cache/downloads
290 [ ! -d "$dl" ] && mkdir -p $dl
291 eval_gettext 'Downloading to: $dl'; echo
292 cd $dl; $cmd 2>&1 ;;
293 cd|cd\ *)
294 path="${cmd#cd}"; path="${path:-$HOME}";
295 path="$(realpath $path)"; cd "$path" ;;
296 ls|ls\ *)
297 $cmd -w80 --color=always 2>&1 | filter_taztools_msgs ;;
298 cat)
299 # Cmd must be used with an arg.
300 eval_gettext '$cmd needs an argument' ;;
301 mc|nano)
302 # List of restricted (interactive) commands
303 eval_gettext "Please, don't run interactive command \"$cmd\""; echo; echo ;;
304 *)
305 unset HTTP_REFERER # for fooling /lib/libtaz.sh formatting utils (<hr> in the terminal is so-so)
306 export DISPLAY=:0.0 # for run X applications
307 /bin/sh -c "$cmd" 2>&1 | htmlize | filter_taztools_msgs
308 ;;
309 esac
311 cat <<EOT
312 <form id="term">
313 <div class="cmdline" id="cmdline"><span id="prompt">$(term_prompt)</span><span id="typeField"> </span></div>
314 <input type="hidden" name="path" value="$(pwd)"/>
315 <input type="hidden" name="cmd" id="cmd"/>
316 </form>
317 </pre>
318 </section>
320 <form>
321 <button name="termsettings" data-icon="settings">$(gettext 'Settings')</button>
322 <button name="cmd" value="history" data-icon="history">$(gettext 'History')</button>
323 </form>
325 <script type="text/javascript">
326 with (document.getElementById('typeField')) {
327 contentEditable=true;
328 focus();
329 }
330 document.onkeydown=keydownHandler;
331 EOT
333 # Export history as array.
334 # Escape "all \"quotes\" in quotes", and all '\'
335 # (because \u, \x, \c has special meaning and can produce syntax error and stop js)
336 sed 's|\\|\\\\|g; s|"|\\"|g' $historyfile | awk '
337 BEGIN{ i=1; printf("ash_history=[") }
338 { printf("\"%s\",", $0); i++ }
339 END{
340 printf("\"\"];")
341 i--; printf("cur_hist=\"%d\";max_hist=\"%d\";", i, i)
342 }'
343 cat <<EOT
344 </script>
345 EOT
346 ;;
349 *\ rmhistory\ *)
350 # Manage shell commandline history
351 user="$REMOTE_USER"
352 [ -z "$HOME" ] && HOME="$(awk -F: -vu=$user '$1==u{print $6}' /etc/passwd)"
353 historyfile="$HOME/.ash_history"
355 # Return sed command for removing history lines ('8d12d' to remove 8 and 12 lines)
356 rms=$(echo $QUERY_STRING | awk 'BEGIN{RS="&";FS="="}{if($1=="rm")printf "%dd", $2}')
358 if [ -n "$rms" ]; then
359 # Actually remove lines
360 sed -i $rms $historyfile
361 # Redirects back to the history table
362 header "HTTP/1.1 301 Moved Permanently" "Location: ?terminal&cmd=history&path=$(GET path)"
363 exit 0
364 fi
365 ;;
368 *\ termsettings\ *)
369 # Terminal settings
370 TITLE=$(gettext 'TazPanel - Terminal'); header; xhtml_header;
371 user="$REMOTE_USER"
372 font="$(GET font)"; font="${font:-$TERM_FONT}"
373 palette="$(GET palette)"; palette="${palette:-$TERM_PALETTE}"
374 pal=$(echo $palette | tr A-Z a-z)
376 # Add or change settings in TazPanel config
377 if [ -z "$TERM_FONT" ]; then
378 echo -e "\n# Terminal font family\nTERM_FONT=\"$font\"" >> $CONFIG
379 else
380 sed -i "s|TERM_FONT=.*|TERM_FONT=\"$font\"|" $CONFIG
381 fi
382 if [ -z "$TERM_PALETTE" ]; then
383 echo -e "\n# Terminal color palette\nTERM_PALETTE=\"$palette\"" >> $CONFIG
384 else
385 sed -i "s|TERM_PALETTE=.*|TERM_PALETTE=\"$palette\"|" $CONFIG
386 fi
388 cat <<EOT
389 <section>
390 <header>
391 $(gettext 'Terminal settings')
392 <form>
393 <button name="terminal" data-icon="terminal">$(gettext 'Terminal')</button>
394 </form>
395 </header>
396 <pre class="term $pal" style="height: auto; font-family: '$font'">
397 <span class='color31'>$user@$(hostname)</span>:<span class='color33'>~</span># palette
399 $(
400 for i in $(seq 30 37); do
401 for b in 0 1; do
402 for j in $(seq 40 47); do
403 echo -n "<span class=\"color$b color$i color$j\">$i:$j</span>"
404 done
405 done
406 echo
407 done
408 )
409 </pre>
410 <footer>
411 <form class="wide">
412 $(gettext 'Font:')
413 <select name="font">
414 <option value="">$(gettext 'Default')</option>
415 $(fc-list :spacing=mono:lang=en family | sed '/\.pcf/d;/,/d;s|\\-|-|g' | sort -u | \
416 awk -vfont="$font" '{
417 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == font)?" selected":"", $0)
418 }')
419 </select>
420 $(gettext 'Palette:')
421 <select name="palette">
422 $(echo -e 'Tango\nLinux\nXterm\nRxvt\nEcho' | awk -vpal="$palette" '{
423 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == pal)?" selected":"", $0)
424 }')
425 </select>
426 <button name="termsettings" data-icon="ok">$(gettext 'Apply')</button>
427 </form>
428 </footer>
429 </section>
430 EOT
432 ;;
435 *\ top\ *)
436 TITLE=$(gettext 'TazPanel - Process activity'); header; xhtml_header
438 r=$(GET refresh)
439 cat <<EOT
440 <form>
441 <p>$(gettext 'Refresh:')
442 <input type="hidden" name="top"/>
443 <input type="radio" name="refresh" value="1" id="r1" $([ "$r" == 1 ] && echo checked) onchange="this.form.submit()"/>
444 <label for="r1">$(gettext '1s' )</label>
445 <input type="radio" name="refresh" value="5" id="r2" $([ "$r" == 5 ] && echo checked) onchange="this.form.submit()"/>
446 <label for="r2">$(gettext '5s' )</label>
447 <input type="radio" name="refresh" value="10" id="r3" $([ "$r" == 10 ] && echo checked) onchange="this.form.submit()"/>
448 <label for="r3">$(gettext '10s' )</label>
449 <input type="radio" name="refresh" value="" id="r4" $([ -z "$r" ] && echo checked) onchange="this.form.submit()"/>
450 <label for="r4">$(gettext 'none')</label>
451 </p>
452 </form>
453 EOT
454 [ -n "$r" ] && echo "<meta http-equiv=\"refresh\" content=\"$r\">"
456 echo '<section><div><pre class="term log">'
457 top -n1 -b | htmlize | sed \
458 -e 's|^[A-Z].*:|<span class="color1 color31">\0</span>|g' \
459 -e 's|^\ *PID|<span class="color1 color32">\0</span>|g'
460 echo '</pre></div></section>' ;;
463 *\ debug\ *)
464 TITLE=$(gettext 'TazPanel - Debug'); header; xhtml_header
466 cat <<EOT
467 <h2>$(gettext 'HTTP Environment')</h2>
469 <section>
470 <div>
471 <pre>$(httpinfo | syntax_highlighter conf)</pre>
472 </div>
473 </section>
474 EOT
475 ;;
478 *\ report\ *)
479 TITLE=$(gettext 'TazPanel - System report'); header; xhtml_header
481 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
482 output=/var/cache/slitaz/sys-report.html
484 cat <<EOT
486 <section>
487 <header>$(eval_gettext 'Reporting to: $output')</header>
488 <table class="wide zebra">
489 <tbody>
490 <tr><td>$(gettext 'Creating report header...')</td>
491 EOT
492 cat > $output <<EOT
493 <!DOCTYPE html>
494 <html xmlns="http://www.w3.org/1999/xhtml">
495 <head>
496 <meta charset="utf-8"/>
497 <title>$(gettext 'SliTaz system report')</title>
498 <style type="text/css">
499 body { padding: 20px 60px; font-size: 13px; }
500 h1, h2 { color: #444; }
501 pre { background: #f1f1f1; border: 1px solid #ddd;
502 padding: 10px; border-radius: 4px; }
503 span.diff-rm { color: red; }
504 span.diff-add { color: green; }
505 </style>
506 </head>
507 <body>
508 EOT
509 cat <<EOT
510 $(ok_status_t)
511 <tr><td>$(gettext 'Creating system summary...')</td>
512 EOT
513 cat >> $output <<EOT
514 <h1>$(gettext 'SliTaz system report')</h1>
515 $(gettext 'Date:') $(date)
516 <pre>
517 uptime : $(uptime)
518 cmdline : $(cat /proc/cmdline)
519 version : $(cat /etc/slitaz-release)
520 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
521 kernel : $(uname -r)
522 </pre>
523 EOT
524 cat <<EOT
525 $(ok_status_t)
526 <tr><td>$(gettext 'Getting hardware info...')</td>
527 EOT
528 cat >> $output <<EOT
529 <h2>free</h2>
530 <pre>$(free)</pre>
532 <h2>lspci -k</h2>
533 <pre>$(lspci -k)</pre>
535 <h2>lsusb</h2>
536 <pre>$(lsusb)</pre>
538 <h2>lsmod</h2>
539 <pre>$(lsmod)</pre>
541 EOT
542 cat <<EOT
543 $(ok_status_t)
544 <tr><td>$(gettext 'Getting networking info...')</td>
545 EOT
546 cat >> $output <<EOT
547 <h2>ifconfig -a</h2>
548 <pre>$(ifconfig -a)</pre>
550 <h2>route -n</h2>
551 <pre>$(route -n)</pre>
553 <h2>/etc/resolv.conf</h2>
554 <pre>$(cat /etc/resolv.conf)</pre>
555 EOT
556 cat <<EOT
557 $(ok_status_t)
558 <tr><td>$(gettext 'Getting filesystems info...')</td>
559 EOT
560 cat >> $output <<EOT
561 <h2>blkid</h2>
562 <pre>$(blkid)</pre>
564 <h2>fdisk -l</h2>
565 <pre>$(fdisk -l)</pre>
567 <h2>mount</h2>
568 <pre>$(mount)</pre>
570 <h2>df -h</h2>
571 <pre>$(df -h)</pre>
573 <h2>df -i</h2>
574 <pre>$(df -i)</pre>
575 EOT
576 cat <<EOT
577 $(ok_status_t)
578 <tr><td>$(gettext 'Getting boot logs...')</td>
579 EOT
580 cat >> $output <<EOT
581 <h2>$(gettext 'Kernel messages')</h2>
582 <pre>$(cat /var/log/dmesg.log)</pre>
584 <h2>$(gettext 'Boot scripts')</h2>
585 <pre>$(cat /var/log/boot.log | filter_taztools_msgs)</pre>
586 EOT
587 cat <<EOT
588 $(ok_status_t)
589 <tr><td>$(gettext 'Creating report footer...')</td>
590 EOT
591 cat cat >> $output <<EOT
592 </body>
593 </html>
594 EOT
595 cat <<EOT
596 $(ok_status_t)
597 </tbody>
598 </table>
599 <footer>
600 <form><button name="file" value="$output" data-icon="view">$(gettext 'View report')</button></form>
601 </footer>
602 </section>
605 $(msg tip "$(gettext 'This report can be attached with a bug report on:')
606 <a href="http://bugs.slitaz.org/" target="_blank">bugs.slitaz.org</a></p>")
607 EOT
608 ;;
611 *)
612 #
613 # Default xHTML content
614 #
615 header; xhtml_header
616 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
617 [ -n "$(GET rdate)" ] && echo ""
618 hostname=$(hostname)
620 cat <<EOT
621 <h2>$(eval_gettext 'Host: $hostname')</h2>
622 <p>$(gettext 'SliTaz administration and configuration Panel')<p>
624 <form class="nogap"><!--
625 --><button name="terminal" data-icon="terminal">$(gettext 'Terminal' )</button><!--
626 --><button name="top" data-icon="proc" >$(gettext 'Process activity')</button><!--
627 --><button name="report" data-icon="report" data-root>$(gettext 'Create a report' )</button><!--
628 --></form>
630 <section>
631 <header>$(gettext 'Summary')</header>
632 <table>
633 <tr><td>$(gettext 'Uptime:')</td>
634 <td id="uptime">$(uptime | sed 's|\([0-9.:][0-9.:]*\)|<b>\1</b>|g')</td>
635 </tr>
636 <tr><td>$(gettext 'Memory in Mb:')</td>
637 <td>$(free -m | grep Mem: | \
638 awk -vline="$(gettext 'Total: %d, Used: %d, Free: %d')" \
639 '{ printf(line, $2, $3, $4) }' | \
640 sed 's|\([0-9][0-9]*\)|<b>\1</b>|g')</td>
641 </tr>
642 <tr><td>$(gettext 'Linux kernel:')</td>
643 <td>$(uname -r)</td>
644 </tr>
645 </table>
646 </section>
649 <section>
650 <header>
651 $(gettext 'Network status')
652 <form action="network.cgi">
653 <button data-icon="wifi">$(gettext 'Network')</button>
654 </form>
655 </header>
656 $(list_network_interfaces)
657 </section>
660 <section>
661 <header>
662 $(gettext 'Filesystem usage statistics')
663 <form action="hardware.cgi">
664 <button data-icon="hdd">$(gettext 'Disks')</button>
665 </form>
666 </header>
667 <table class="wide zebra center">
668 EOT
669 # Disk stats (management is done as hardware.cgi)
670 df_thead
671 echo '<tbody>'
672 df -h | grep ^/dev | while read fs size used av pct mp; do
673 cat <<EOT
674 <tr>
675 <td><span data-icon="hdd">${fs#/dev/}</span></td>
676 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
677 <td>$(blkid $fs | sed '/TYPE=/!d;s/.*TYPE="\([^"]*\).*/\1/')</td>
678 <td>$size</td>
679 <td>$av</td>
680 <td class="meter"><meter min="0" max="100" value="$(echo $pct | cut -d% -f1)"
681 low="$DU_WARN" high="$DU_CRIT" optimum="10"></meter>
682 <span>$used - $pct</span>
683 </td>
684 <td>$mp</td>
685 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
686 </tr>
687 EOT
688 done
689 cat <<EOT
690 </tbody>
691 </table>
692 </section>
694 <section>
695 <header>
696 $(gettext 'Panel Activity')
697 <form>
698 <button name="file" value="$LOG_FILE" data-icon="view">$(gettext 'View')</button>
699 </form>
700 </header>
701 <div>
702 <pre id="panel-activity">
703 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)</pre>
704 </div>
705 </section>
706 EOT
707 ;;
708 esac
710 xhtml_footer
711 exit 0