tazpanel view index.cgi @ rev 456

boot.cgi: ISO mine: "table"izing input form.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Apr 13 01:44:08 2015 +0300 (2015-04-13)
parents 169f1ccfb613
children d0dbe11a2060
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=$(_ '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=$(_ '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">$(_ 'Save')</button>
131 <button name="action" value="diff" data-icon="diff">$(_ '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">$(_ '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 <header>
178 <span data-icon="text">$file</span>
179 EOT
180 if [ -w "$file" ]; then
181 cat <<EOT
182 <form>
183 <input type="hidden" name="file" value="$file"/>
184 <button name="action" value="edit" data-icon="edit">$(_ 'Edit')</button><!--
185 -->$(file_is_modified $file button)
186 </form>
187 EOT
188 elif [ -r "$file" ]; then
189 cat <<EOT
190 <form>
191 <input type="hidden" name="file" value="$file"/>
192 $(file_is_modified $file button)
193 </form>
194 EOT
195 fi
196 cat <<EOT
197 </header>
199 <div>
200 <pre>
201 EOT
202 # Handle file type by extension as a Web Server does it.
203 case "$file" in
204 *.conf|*.lst)
205 syntax_highlighter conf ;;
206 *.sh|*.cgi)
207 syntax_highlighter sh ;;
208 *Xorg.0.log)
209 syntax_highlighter xlog ;;
210 *dmesg.log)
211 syntax_highlighter kernel ;;
212 *)
213 cat | htmlize ;;
214 esac < $file
215 cat <<EOT
216 </pre>
217 </div>
218 </section>
219 EOT
220 esac
221 ;;
225 *\ terminal\ *|*\ cmd\ *)
226 # Cmdline terminal
228 header; TITLE=$(_ 'TazPanel - Terminal'); xhtml_header
230 user="$REMOTE_USER"
231 HOME="$(awk -F: -vu=$user '$1==u{print $6}' /etc/passwd)"
232 historyfile="$HOME/.ash_history"
234 cmd=$(GET cmd)
235 path="$(GET path)"; path="${path:-/tmp}"; cd "$path"
237 font="${TERM_FONT:-monospace}"
238 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
240 [ -n "$cmd" -a "$cmd" != "$(tail -n1 $historyfile)" ] && echo "$cmd" >> $historyfile
243 # Terminal history
245 if [ "$cmd" == 'history' ]; then
246 cat <<EOT
247 <section>
248 <header>
249 $(_ 'History')
250 <form><button name="terminal" data-icon="terminal">$(_ 'Back')</button></form>
251 </header>
252 <form>
253 <input type="hidden" name="path" value="$path"/>
254 <pre class="term $palette" style="font-family: '$font'">
255 EOT
256 htmlize < $historyfile | awk -vrun="$(_ 'run')" -vpath="$path" '
257 BEGIN { num=1 }
258 {
259 printf("%3d ", num);
260 cmd = $0
261 gsub("%", "%25", cmd); gsub("+", "%2B", cmd); gsub(" ", "+", cmd);
262 gsub("\"", "%22", cmd); gsub("!", "%21", cmd); gsub("'\''", "%27", cmd);
263 printf("<a data-icon=\"run\" href=\"?cmd=%s&path=%s\">%s</a> ", cmd, path, run);
264 printf("<input type=\"checkbox\" name=\"rm\" value=\"%d\" id=\"hist%d\">", num, num);
265 printf("<label for=\"hist%d\">%s</label>\n", num, $0);
266 num++
267 }'
268 cat <<EOT
269 </pre>
270 <footer>
271 <button name="rmhistory" data-icon="remove">$(_ 'Clear')</button>
272 </footer>
273 </form>
274 </section>
275 EOT
276 xhtml_footer
277 exit 0
278 fi
281 # Terminal window
283 cat <<EOT
284 <span id="num_hist"></span>
285 <section>
286 <pre class="term $palette" style="font-family: '$font'" onclick="document.getElementById('typeField').focus()">
287 EOT
288 if [ -n "$cmd" ]; then
289 term_prompt
290 echo "$cmd" | htmlize
291 fi
293 case "$cmd" in
294 usage|help)
295 _ 'Small non-interactive terminal emulator.'; echo
296 _ 'Run any command at your own risk, avoid interactive commands (%s)' "nano, mc, ..."; echo
297 ;;
298 wget*)
299 dl=/var/cache/downloads
300 [ ! -d "$dl" ] && mkdir -p $dl
301 _ 'Downloading to: %s' $dl; echo
302 cd $dl; $cmd 2>&1 ;;
303 cd|cd\ *)
304 path="${cmd#cd}"; path="${path:-$HOME}";
305 path="$(realpath $path)"; cd "$path" ;;
306 ls|ls\ *)
307 $cmd -w80 --color=always 2>&1 | filter_taztools_msgs ;;
308 cat)
309 # Cmd must be used with an arg.
310 _ '%s needs an argument' "$cmd" ;;
311 mc|nano|su)
312 # List of restricted (interactive) commands
313 _ "Please, don't run interactive command \"%s\"" "$cmd"; echo; echo ;;
314 *)
315 unset HTTP_REFERER # for fooling /lib/libtaz.sh formatting utils (<hr> in the terminal is so-so)
316 export DISPLAY=:0.0 # for run X applications
317 /bin/sh -c "$cmd" 2>&1 | htmlize | filter_taztools_msgs
318 ;;
319 esac
321 cat <<EOT
322 <form id="term">
323 <div class="cmdline" id="cmdline"><span id="prompt">$(term_prompt)</span><span id="typeField"> </span></div>
324 <input type="hidden" name="path" value="$(pwd)"/>
325 <input type="hidden" name="cmd" id="cmd"/>
326 </form>
327 </pre>
328 </section>
330 <form>
331 <button name="termsettings" data-icon="settings">$(_ 'Settings')</button>
332 <button name="cmd" value="history" data-icon="history">$(_ 'History')</button>
333 </form>
335 <script type="text/javascript">
336 with (document.getElementById('typeField')) {
337 contentEditable=true;
338 focus();
339 }
340 document.onkeydown=keydownHandler;
341 EOT
343 # Export history as array.
344 # Escape "all \"quotes\" in quotes", and all '\'
345 # (because \u, \x, \c has special meaning and can produce syntax error and stop js)
346 sed 's|\\|\\\\|g; s|"|\\"|g' $historyfile | awk '
347 BEGIN{ i=1; printf("ash_history=[") }
348 { printf("\"%s\",", $0); i++ }
349 END{
350 printf("\"\"];")
351 i--; printf("cur_hist=\"%d\";max_hist=\"%d\";", i, i)
352 }'
353 cat <<EOT
354 </script>
355 EOT
356 ;;
359 *\ rmhistory\ *)
360 # Manage shell commandline history
361 user="$REMOTE_USER"
362 [ -z "$HOME" ] && HOME="$(awk -F: -vu=$user '$1==u{print $6}' /etc/passwd)"
363 historyfile="$HOME/.ash_history"
365 # Return sed command for removing history lines ('8d12d' to remove 8 and 12 lines)
366 rms=$(echo $QUERY_STRING | awk 'BEGIN{RS="&";FS="="}{if($1=="rm")printf "%dd", $2}')
368 if [ -n "$rms" ]; then
369 # Actually remove lines
370 sed -i $rms $historyfile
371 # Redirects back to the history table
372 header "HTTP/1.1 301 Moved Permanently" "Location: ?terminal&cmd=history&path=$(GET path)"
373 exit 0
374 fi
375 ;;
378 *\ termsettings\ *)
379 # Terminal settings
380 TITLE=$(_ 'TazPanel - Terminal'); header; xhtml_header;
381 user="$REMOTE_USER"
382 font="$(GET font)"; font="${font:-$TERM_FONT}"
383 palette="$(GET palette)"; palette="${palette:-$TERM_PALETTE}"
384 pal=$(echo $palette | tr A-Z a-z)
386 # Add or change settings in TazPanel config
387 if [ -z "$TERM_FONT" ]; then
388 echo -e "\n# Terminal font family\nTERM_FONT=\"$font\"" >> $CONFIG
389 else
390 sed -i "s|TERM_FONT=.*|TERM_FONT=\"$font\"|" $CONFIG
391 fi
392 if [ -z "$TERM_PALETTE" ]; then
393 echo -e "\n# Terminal color palette\nTERM_PALETTE=\"$palette\"" >> $CONFIG
394 else
395 sed -i "s|TERM_PALETTE=.*|TERM_PALETTE=\"$palette\"|" $CONFIG
396 fi
398 cat <<EOT
399 <section>
400 <header>
401 $(_ 'Terminal settings')
402 <form>
403 <button name="terminal" data-icon="terminal">$(_ 'Terminal')</button>
404 </form>
405 </header>
406 <pre class="term $pal" style="height: auto; font-family: '$font'">
407 <span class='color31'>$user@$(hostname)</span>:<span class='color33'>~</span># palette
409 $(
410 for i in $(seq 30 37); do
411 for b in 0 1; do
412 for j in $(seq 40 47); do
413 echo -n "<span class=\"color$b color$i color$j\">$i:$j</span>"
414 done
415 done
416 echo
417 done
418 )
419 </pre>
420 <footer>
421 <form class="wide">
422 $(_ 'Font:')
423 <select name="font">
424 <option value="">$(_ 'Default')</option>
425 $(fc-list :spacing=mono:lang=en family | sed '/\.pcf/d;/,/d;s|\\-|-|g' | sort -u | \
426 awk -vfont="$font" '{
427 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == font)?" selected":"", $0)
428 }')
429 </select>
430 $(_ 'Palette:')
431 <select name="palette">
432 $(echo -e 'Tango\nLinux\nXterm\nRxvt\nEcho' | awk -vpal="$palette" '{
433 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == pal)?" selected":"", $0)
434 }')
435 </select>
436 <button name="termsettings" data-icon="ok">$(_ 'Apply')</button>
437 </form>
438 </footer>
439 </section>
440 EOT
442 ;;
445 *\ top\ *)
446 header; TITLE=$(_ 'TazPanel - Process activity'); xhtml_header
448 r=$(GET refresh)
449 cat <<EOT
450 <form>
451 <p>$(_ 'Refresh:')
452 <input type="hidden" name="top"/>
453 <input type="radio" name="refresh" value="1" id="r1" $([ "$r" == 1 ] && echo checked) onchange="this.form.submit()"/>
454 <label for="r1">$(_ '1s' )</label>
455 <input type="radio" name="refresh" value="5" id="r2" $([ "$r" == 5 ] && echo checked) onchange="this.form.submit()"/>
456 <label for="r2">$(_ '5s' )</label>
457 <input type="radio" name="refresh" value="10" id="r3" $([ "$r" == 10 ] && echo checked) onchange="this.form.submit()"/>
458 <label for="r3">$(_ '10s' )</label>
459 <input type="radio" name="refresh" value="" id="r4" $([ -z "$r" ] && echo checked) onchange="this.form.submit()"/>
460 <label for="r4">$(_ 'none')</label>
461 </p>
462 </form>
463 EOT
464 [ -n "$r" ] && echo "<meta http-equiv=\"refresh\" content=\"$r\">"
466 echo '<section><div><pre class="term log">'
467 top -n1 -b | htmlize | sed \
468 -e 's|^[A-Z].*:|<span class="color1 color31">\0</span>|g' \
469 -e 's|^\ *PID|<span class="color1 color32">\0</span>|g'
470 echo '</pre></div></section>' ;;
473 *\ debug\ *)
474 header; TITLE=$(_ 'TazPanel - Debug'); xhtml_header
476 cat <<EOT
477 <h2>$(_ 'HTTP Environment')</h2>
479 <section>
480 <div>
481 <pre>$(httpinfo | syntax_highlighter conf)</pre>
482 </div>
483 </section>
484 EOT
485 ;;
488 *\ report\ *)
489 header; TITLE=$(_ 'TazPanel - System report'); xhtml_header
491 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
492 output=/var/cache/slitaz/sys-report.html
494 cat <<EOT
496 <section>
497 <header>$(_ 'Reporting to: %s' "$output")</header>
498 <table class="wide zebra">
499 <tbody>
500 <tr><td>$(_ 'Creating report header...')</td>
501 EOT
502 cat > $output <<EOT
503 <!DOCTYPE html>
504 <html xmlns="http://www.w3.org/1999/xhtml">
505 <head>
506 <meta charset="utf-8"/>
507 <title>$(_ 'SliTaz system report')</title>
508 <style type="text/css">
509 body { padding: 20px 60px; font-size: 13px; }
510 h1, h2 { color: #444; }
511 pre { background: #f1f1f1; border: 1px solid #ddd;
512 padding: 10px; border-radius: 4px; }
513 span.diff-rm { color: red; }
514 span.diff-add { color: green; }
515 </style>
516 </head>
517 <body>
518 EOT
519 cat <<EOT
520 $(ok_status_t)
521 <tr><td>$(_ 'Creating system summary...')</td>
522 EOT
523 cat >> $output <<EOT
524 <h1>$(_ 'SliTaz system report')</h1>
525 $(_ 'Date:') $(date)
526 <pre>
527 uptime : $(uptime)
528 cmdline : $(cat /proc/cmdline)
529 version : $(cat /etc/slitaz-release)
530 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
531 kernel : $(uname -r)
532 </pre>
533 EOT
534 cat <<EOT
535 $(ok_status_t)
536 <tr><td>$(_ 'Getting hardware info...')</td>
537 EOT
538 cat >> $output <<EOT
539 <h2>free</h2>
540 <pre>$(free)</pre>
542 <h2>lspci -k</h2>
543 <pre>$(lspci -k)</pre>
545 <h2>lsusb</h2>
546 <pre>$(lsusb)</pre>
548 <h2>lsmod</h2>
549 <pre>$(lsmod)</pre>
551 EOT
552 cat <<EOT
553 $(ok_status_t)
554 <tr><td>$(_ 'Getting networking info...')</td>
555 EOT
556 cat >> $output <<EOT
557 <h2>ifconfig -a</h2>
558 <pre>$(ifconfig -a)</pre>
560 <h2>route -n</h2>
561 <pre>$(route -n)</pre>
563 <h2>/etc/resolv.conf</h2>
564 <pre>$(cat /etc/resolv.conf)</pre>
565 EOT
566 cat <<EOT
567 $(ok_status_t)
568 <tr><td>$(_ 'Getting filesystems info...')</td>
569 EOT
570 cat >> $output <<EOT
571 <h2>blkid</h2>
572 <pre>$(blkid)</pre>
574 <h2>fdisk -l</h2>
575 <pre>$(fdisk -l)</pre>
577 <h2>mount</h2>
578 <pre>$(mount)</pre>
580 <h2>df -h</h2>
581 <pre>$(df -h)</pre>
583 <h2>df -i</h2>
584 <pre>$(df -i)</pre>
585 EOT
586 cat <<EOT
587 $(ok_status_t)
588 <tr><td>$(_ 'Getting boot logs...')</td>
589 EOT
590 cat >> $output <<EOT
591 <h2>$(_ 'Kernel messages')</h2>
592 <pre>$(cat /var/log/dmesg.log)</pre>
594 <h2>$(_ 'Boot scripts')</h2>
595 <pre>$(cat /var/log/boot.log | filter_taztools_msgs)</pre>
596 EOT
597 cat <<EOT
598 $(ok_status_t)
599 <tr><td>$(_ 'Creating report footer...')</td>
600 EOT
601 cat cat >> $output <<EOT
602 </body>
603 </html>
604 EOT
605 cat <<EOT
606 $(ok_status_t)
607 </tbody>
608 </table>
609 <footer>
610 <form><button name="file" value="$output" data-icon="view">$(_ 'View')</button></form>
611 </footer>
612 </section>
615 $(msg tip "$(_ 'This report can be attached with a bug report on:')
616 <a href="http://bugs.slitaz.org/" target="_blank">bugs.slitaz.org</a></p>")
617 EOT
618 ;;
621 *)
622 #
623 # Default xHTML content
624 #
625 header; xhtml_header
626 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
627 [ -n "$(GET rdate)" ] && echo ""
628 hostname=$(hostname)
630 cat <<EOT
631 <h2>$(_ 'Host: %s' $hostname)</h2>
632 <p>$(_ 'SliTaz administration and configuration Panel')<p>
634 <form class="nogap"><!--
635 --><button name="terminal" data-icon="terminal">$(_ 'Terminal')</button><!--
636 --><button name="top" data-icon="proc" >$(_ 'Process activity')</button><!--
637 --><button name="report" data-icon="report" data-root>$(_ 'Create a report')</button><!--
638 --></form>
640 <section>
641 <header>$(_ 'Summary')</header>
642 <table>
643 <tr><td>$(_ 'Uptime:')</td>
644 <td id="uptime">$(uptime | sed 's|\([0-9.:][0-9.:]*\)|<b>\1</b>|g')</td>
645 </tr>
646 <tr><td>$(_ 'Memory in Mb:')</td>
647 <td>$(free -m | grep Mem: | \
648 awk -vline="$(gettext 'Total: %d, Used: %d, Free: %d')" \
649 '{ printf(line, $2, $3, $4) }' | \
650 sed 's|\([0-9][0-9]*\)|<b>\1</b>|g')</td>
651 </tr>
652 <tr><td>$(_ 'Linux kernel:')</td>
653 <td>$(uname -r)</td>
654 </tr>
655 </table>
656 </section>
659 <section>
660 <header>
661 $(_ 'Network status')
662 <form action="network.cgi">
663 <button data-icon="wifi">$(_ 'Network')</button>
664 </form>
665 </header>
666 $(list_network_interfaces)
667 </section>
670 <section>
671 <header>
672 $(_ 'Filesystem usage statistics')
673 <form action="hardware.cgi">
674 <button data-icon="hdd">$(_ 'Disks')</button>
675 </form>
676 </header>
677 <table class="wide zebra center">
678 EOT
679 # Disk stats (management is done as hardware.cgi)
680 df_thead
681 echo '<tbody>'
682 df -h | grep ^/dev | while read fs size used av pct mp; do
683 cat <<EOT
684 <tr>
685 <td><span data-icon="hdd">${fs#/dev/}</span></td>
686 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
687 <td>$(blkid $fs | sed '/TYPE=/!d;s/.*TYPE="\([^"]*\).*/\1/')</td>
688 <td>$size</td>
689 <td>$av</td>
690 <td class="meter"><meter min="0" max="100" value="$(echo $pct | cut -d% -f1)"
691 low="$DU_WARN" high="$DU_CRIT" optimum="10"></meter>
692 <span>$used - $pct</span>
693 </td>
694 <td>$mp</td>
695 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
696 </tr>
697 EOT
698 done
699 cat <<EOT
700 </tbody>
701 </table>
702 </section>
704 <section>
705 <header>
706 $(_ 'Panel Activity')
707 <form>
708 <button name="file" value="$LOG_FILE" data-icon="view">$(_ 'View')</button>
709 </form>
710 </header>
711 <div>
712 <pre id="panel-activity">
713 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)</pre>
714 </div>
715 </section>
716 EOT
717 ;;
718 esac
720 xhtml_footer
721 exit 0