tazpanel view index.cgi @ rev 462

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