tazpanel view index.cgi @ rev 419

Bunch of changes. Development in progress, please note it have few known bugs.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Mar 24 03:39:08 2015 +0200 (2015-03-24)
parents 106b85c1951c
children 7488b357abd3
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 {
24 grep -l " $1$" $INSTALLED/*/md5sum | while read file; do
26 # Found, but can we do diff?
27 [ "$(grep -h " $1$" $file)" != "$(md5sum $1)" ] || break
28 orig=$(dirname $file)/volatile.cpio.gz
29 zcat $orig 2>/dev/null | cpio -t 2>/dev/null | grep -q "^${1#/}$" || break
31 case "$2" in
32 diff)
33 tmp=$(mktemp -d)
34 ( cd $tmp; zcat $orig | cpio -id ${1#/} )
35 diff -abu $tmp$1 $1 | sed "s|$tmp||"
36 rm -rf $tmp;;
37 button)
38 echo -n '<button name="action" value="diff" data-icon="diff">'$(gettext 'Differences')'</button>';;
39 esac
40 break
41 done
42 }
46 # OK status in table
48 ok_status_t() {
49 echo '<td><span class="diff-add" data-img="ok"></span></td></tr>'
50 }
54 #
55 # Things to do before displaying the page
56 #
58 [ -n "$(GET panel_pass)" ] &&
59 sed -i s@/:root:.*@/:root:$(GET panel_pass)@ $HTTPD_CONF
65 #
66 # Commands
67 #
69 case " $(GET) " in
72 *\ exec\ *)
73 # Execute command and display its result in a terminal-like window
75 header; TITLE=$(gettext 'TazPanel - exec'); xhtml_header
77 exec="$(GET exec)"
78 font="${TERM_FONT:-monospace}"
79 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
81 cat <<EOT
82 <section>
83 <header>
84 $exec
85 $(back_button "$(GET back)" "$(GET back_caption)" "$(GET back_icon)")
86 </header>
87 <div class="term_">
88 <pre class="term $palette" style="font-family: '$font'">$($exec 2>&1 | htmlize | filter_taztools_msgs)</pre>
89 </div>
90 </section>
91 EOT
92 ;;
95 *\ file\ *)
96 #
97 # Handle files
98 #
99 header
100 file="$(GET file)"
101 action="$(POST action)"; [ -z "$action" ] && action="$(GET action)" # receive 'action' both on POST or GET
103 case $file in
104 *.html)
105 cat $file; exit 0 ;;
106 *)
107 TITLE=$(gettext 'TazPanel - File'); xhtml_header ;;
108 esac
110 case "$action" in
111 edit)
112 cat <<EOT
113 <section>
114 <header>
115 <span data-icon="edit">$file</span>
116 <form id="editform" method="post" action="?file=$file" class="nogap">
117 <button data-icon="save">$(gettext 'Save')</button>
118 <button name="action" value="diff" data-icon="diff">$(gettext 'Differences')</button>
119 </form>
120 </header>
121 <textarea form="editform" name="content" class="wide" rows="30" autofocus>$(cat $file | htmlize)</textarea>
122 </section>
123 EOT
124 #The space before textarea gets muddled when the form is submitted.
125 #It prevents anything else from getting messed up
126 ;;
128 setvar)
129 data="$(. $(GET file) ;eval echo \$$(GET var))"
130 cat <<EOT
131 <section>
132 <header>$(GET var)</header>
133 <form method="post" action="?file=$file" class="nogap">
134 <input type="hidden" name="var" value="$(GET var)">
135 <input type="text" name="content" value="${data:-$(GET default)}">
136 <button type="submit" data-icon="save">$(gettext 'Save')</button>
137 </form>
138 </section>
139 EOT
140 ;;
142 diff)
143 cat <<EOT
144 <section>
145 <pre id="diff">$(file_is_modified $file diff | syntax_highlighter diff)</pre>
146 </section>
147 EOT
148 ;;
150 *)
151 R=$(echo -en '\r')
152 if [ -n "$(POST content)" ]; then
153 if [ -n "$(POST var)" ]; then
154 sed -i "s|^\\($(POST var)=\\).*|\1\"$(POST content)\"|" $file
155 else
156 sed "s/$R /\n/g;s/$R%0//g" > $file <<EOT
157 $(POST content)
158 EOT
159 fi
160 fi
162 cat <<EOT
163 <section>
164 <header><span data-icon="text">$file</span>
165 <form class="nogap">
166 <input type="hidden" name="file" value="$file"/>
167 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button><!--
168 -->$(file_is_modified $file button)
169 </form>
170 </header>
172 <div>
173 <pre>
174 EOT
175 # Handle file type by extension as a Web Server does it.
176 case "$file" in
177 *.conf|*.lst)
178 syntax_highlighter conf ;;
179 *.sh|*.cgi)
180 syntax_highlighter sh ;;
181 *Xorg.0.log)
182 syntax_highlighter xlog ;;
183 *dmesg.log)
184 syntax_highlighter kernel ;;
185 *)
186 cat | htmlize ;;
187 esac < $file
188 cat <<EOT
189 </pre>
190 </div>
191 </section>
192 EOT
193 esac
194 ;;
198 *\ terminal\ *|*\ cmd\ *)
199 # Cmdline terminal
201 TITLE=$(gettext 'TazPanel - Terminal'); header; xhtml_header
203 historyfile=/root/.ash_history
204 #commands='cat cd date du fdisk help ls ping pwd who wget'
205 cmd=$(GET cmd)
207 path="$(GET path)"; path="${path:-/tmp}"; cd "${path/~//root}"
208 [ "$path" == '/root' ] && path='~'
209 user="$REMOTE_USER"
210 [ -z "$HOME" ] && HOME=/root # for apps that modified user settings
212 font="${TERM_FONT:-monospace}"
213 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
215 [ -n "$cmd" -a "$cmd" != "$(tail -n1 $historyfile)" ] && echo "$cmd" >> $historyfile
218 # Terminal history
220 if [ "$cmd" == 'history' ]; then
221 cat <<EOT
222 <section>
223 <header>
224 $(gettext 'History')
225 <form><button name="terminal" data-icon="terminal">$(gettext 'Back')</button></form>
226 </header>
227 <form>
228 <input type="hidden" name="path" value="$path"/>
229 <pre class="term $palette" style="font-family: '$font'">
230 EOT
231 htmlize < $historyfile | awk -vrun="$(gettext 'run')" -vpath="$path" '
232 BEGIN { num=1 }
233 {
234 printf("%3d ", num);
235 cmd = $0
236 gsub("%", "%25", cmd); gsub("+", "%2B", cmd); gsub(" ", "+", cmd);
237 gsub("\"", "%22", cmd); gsub("!", "%21", cmd); gsub("'\''", "%27", cmd);
238 printf("<a data-icon=\"run\" href=\"?cmd=%s&path=%s\">%s</a> ", cmd, path, run);
239 printf("<input type=\"checkbox\" name=\"rm\" value=\"%d\" id=\"hist%d\">", num, num);
240 printf("<label for=\"hist%d\">%s</label>\n", num, $0);
241 num++
242 }'
243 cat <<EOT
244 </pre>
245 <footer>
246 <button name="rmhistory" data-icon="remove">$(gettext 'Clear')</button>
247 </footer>
248 </form>
249 </section>
250 EOT
251 xhtml_footer
252 exit 0
253 fi
256 # Terminal window
258 cat <<EOT
259 <span id="num_hist"></span>
260 <section>
261 <pre class="term $palette" style="font-family: '$font'" onclick="document.getElementById('typeField').focus()">
262 EOT
263 if [ -n "$cmd" ]; then
264 echo -n "<span class='color31'>$user@$(hostname)</span>:<span class='color33'>$path</span># "
265 echo "$cmd" | htmlize
266 fi
268 case "$cmd" in
269 usage|help)
270 gettext 'Small non-interactive terminal emulator.'; echo
271 gettext 'Run any command at your own risk, avoid interactive commands (nano, mc, ...)'; echo
272 ;;
273 wget*)
274 dl=/var/cache/downloads
275 [ ! -d "$dl" ] && mkdir -p $dl
276 eval_gettext 'Downloading to: $dl'; echo
277 cd $dl; $cmd 2>&1 ;;
278 cd|cd\ *)
279 path="${cmd#cd}"; path="${path:-/root}"; path="$(realpath $path)" ;;
280 ls|ls\ *)
281 $cmd -w80 --color=always 2>&1 | filter_taztools_msgs ;;
282 cat)
283 # Cmd must be used with an arg.
284 eval_gettext '$cmd needs an argument' ;;
285 mc|nano)
286 # List of restricted (interactive) commands
287 eval_gettext "Please, don't run interactive command \"$cmd\""; echo; echo ;;
288 *)
289 unset HTTP_REFERER # for fooling /lib/libtaz.sh formatting utils (<hr> in the terminal is so-so)
290 export DISPLAY=:0.0 # for run X applications
291 /bin/sh -c "$cmd" 2>&1 | htmlize | filter_taztools_msgs
292 ;;
293 esac
295 [ "$path" == '/root' ] && path='~'
296 cat <<EOT
297 <form id="term">
298 <div class="cmdline" id="cmdline"><span id="prompt"><span class="color31">$user@$(hostname)</span>:<span class="color33">$path</span># </span><span id="typeField"> </span></div>
299 <input type="hidden" name="path" value="$path"/>
300 <input type="hidden" name="cmd" id="cmd"/>
301 </form>
302 </pre>
303 </section>
305 <form>
306 <button name="termsettings" data-icon="settings">$(gettext 'Settings')</button>
307 <button name="cmd" value="history" data-icon="history">$(gettext 'History')</button>
308 </form>
310 <script type="text/javascript">
311 with (document.getElementById('typeField')) {
312 contentEditable=true;
313 focus();
314 }
315 document.onkeydown=keydownHandler;
316 EOT
318 # Export history as array.
319 # Escape "all \"quotes\" in quotes", and all '\'
320 # (because \u, \x, \c has special meaning and can produce syntax error and stop js)
321 sed 's|\\|\\\\|g; s|"|\\"|g' $historyfile | awk '
322 BEGIN{ i=1; printf("ash_history=[") }
323 { printf("\"%s\",", $0); i++ }
324 END{
325 printf("\"\"];")
326 i--; printf("cur_hist=\"%d\";max_hist=\"%d\";", i, i)
327 }'
328 cat <<EOT
329 </script>
330 EOT
331 ;;
334 *\ rmhistory\ *)
335 # Manage shell commandline history
336 historyfile=/root/.ash_history
338 # Return sed command for removing history lines ('8d12d' to remove 8 and 12 lines)
339 rms=$(echo $QUERY_STRING | awk 'BEGIN{RS="&";FS="="}{if($1=="rm")printf "%dd", $2}')
341 if [ -n "$rms" ]; then
342 # Actually remove lines
343 sed -i $rms $historyfile
344 # Redirects back to the history table
345 header "HTTP/1.1 301 Moved Permanently" "Location: ?terminal&cmd=history&path=$(GET path)"
346 exit 0
347 fi
348 ;;
351 *\ termsettings\ *)
352 # Terminal settings
353 TITLE=$(gettext 'TazPanel - Terminal'); header; xhtml_header;
354 user="$REMOTE_USER"
355 font="$(GET font)"; font="${font:-$TERM_FONT}"
356 palette="$(GET palette)"; palette="${palette:-$TERM_PALETTE}"
357 pal=$(echo $palette | tr A-Z a-z)
359 # Add or change settings in TazPanel config
360 if [ -z "$TERM_FONT" ]; then
361 echo -e "\n# Terminal font family\nTERM_FONT=\"$font\"" >> $CONFIG
362 else
363 sed -i "s|TERM_FONT=.*|TERM_FONT=\"$font\"|" $CONFIG
364 fi
365 if [ -z "$TERM_PALETTE" ]; then
366 echo -e "\n# Terminal color palette\nTERM_PALETTE=\"$palette\"" >> $CONFIG
367 else
368 sed -i "s|TERM_PALETTE=.*|TERM_PALETTE=\"$palette\"|" $CONFIG
369 fi
371 cat <<EOT
372 <section>
373 <header>
374 $(gettext 'Terminal settings')
375 <form>
376 <button name="terminal" data-icon="terminal">$(gettext 'Terminal')</button>
377 </form>
378 </header>
379 <pre class="term $pal" style="height: auto; font-family: '$font'">
380 <span class='color31'>$user@$(hostname)</span>:<span class='color33'>~</span># palette
382 $(
383 for i in $(seq 30 37); do
384 for b in 0 1; do
385 for j in $(seq 40 47); do
386 echo -n "<span class=\"color$b color$i color$j\">$i:$j</span>"
387 done
388 done
389 echo
390 done
391 )
392 </pre>
393 <footer>
394 <form class="wide">
395 $(gettext 'Font:')
396 <select name="font">
397 <option value="">$(gettext 'Default')</option>
398 $(fc-list :spacing=mono:lang=en family | sed '/\.pcf/d;/,/d;s|\\-|-|g' | sort -u | \
399 awk -vfont="$font" '{
400 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == font)?" selected":"", $0)
401 }')
402 </select>
403 $(gettext 'Palette:')
404 <select name="palette">
405 $(echo -e 'Tango\nLinux\nXterm\nRxvt\nEcho' | awk -vpal="$palette" '{
406 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == pal)?" selected":"", $0)
407 }')
408 </select>
409 <button name="termsettings" data-icon="ok">$(gettext 'Apply')</button>
410 </form>
411 </footer>
412 </section>
413 EOT
415 ;;
418 *\ top\ *)
419 TITLE=$(gettext 'TazPanel - Process activity'); header; xhtml_header
421 r=$(GET refresh)
422 cat <<EOT
423 <form>
424 <p>$(gettext 'Refresh:')
425 <input type="hidden" name="top"/>
426 <input type="radio" name="refresh" value="1" id="r1" $([ "$r" == 1 ] && echo checked) onchange="this.form.submit()"/>
427 <label for="r1">$(gettext '1s' )</label>
428 <input type="radio" name="refresh" value="5" id="r2" $([ "$r" == 5 ] && echo checked) onchange="this.form.submit()"/>
429 <label for="r2">$(gettext '5s' )</label>
430 <input type="radio" name="refresh" value="10" id="r3" $([ "$r" == 10 ] && echo checked) onchange="this.form.submit()"/>
431 <label for="r3">$(gettext '10s' )</label>
432 <input type="radio" name="refresh" value="" id="r4" $([ -z "$r" ] && echo checked) onchange="this.form.submit()"/>
433 <label for="r4">$(gettext 'none')</label>
434 </p>
435 </form>
436 EOT
437 [ -n "$r" ] && echo "<meta http-equiv=\"refresh\" content=\"$r\">"
439 echo '<section><div><pre class="term log">'
440 top -n1 -b | htmlize | sed \
441 -e 's|^[A-Z].*:|<span class="color1 color31">\0</span>|g' \
442 -e 's|^\ *PID|<span class="color1 color32">\0</span>|g'
443 echo '</pre></div></section>' ;;
446 *\ debug\ *)
447 TITLE=$(gettext 'TazPanel - Debug'); header; xhtml_header
449 cat <<EOT
450 <h2>$(gettext 'HTTP Environment')</h2>
452 <section>
453 <div>
454 <pre>$(httpinfo | syntax_highlighter conf)</pre>
455 </div>
456 </section>
457 EOT
458 ;;
461 *\ report\ *)
462 TITLE=$(gettext 'TazPanel - System report'); header; xhtml_header
464 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
465 output=/var/cache/slitaz/sys-report.html
467 cat <<EOT
469 <section>
470 <header>$(eval_gettext 'Reporting to: $output')</header>
471 <table class="wide zebra">
472 <tbody>
473 <tr><td>$(gettext 'Creating report header...')</td>
474 EOT
475 cat > $output <<EOT
476 <!DOCTYPE html>
477 <html xmlns="http://www.w3.org/1999/xhtml">
478 <head>
479 <meta charset="utf-8"/>
480 <title>$(gettext 'SliTaz system report')</title>
481 <style type="text/css">
482 body { padding: 20px 60px; font-size: 13px; }
483 h1, h2 { color: #444; }
484 pre { background: #f1f1f1; border: 1px solid #ddd;
485 padding: 10px; border-radius: 4px; }
486 span.diff-rm { color: red; }
487 span.diff-add { color: green; }
488 </style>
489 </head>
490 <body>
491 EOT
492 cat <<EOT
493 $(ok_status_t)
494 <tr><td>$(gettext 'Creating system summary...')</td>
495 EOT
496 cat >> $output <<EOT
497 <h1>$(gettext 'SliTaz system report')</h1>
498 $(gettext 'Date:') $(date)
499 <pre>
500 uptime : $(uptime)
501 cmdline : $(cat /proc/cmdline)
502 version : $(cat /etc/slitaz-release)
503 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
504 kernel : $(uname -r)
505 </pre>
506 EOT
507 cat <<EOT
508 $(ok_status_t)
509 <tr><td>$(gettext 'Getting hardware info...')</td>
510 EOT
511 cat >> $output <<EOT
512 <h2>free</h2>
513 <pre>$(free)</pre>
515 <h2>lspci -k</h2>
516 <pre>$(lspci -k)</pre>
518 <h2>lsusb</h2>
519 <pre>$(lsusb)</pre>
521 <h2>lsmod</h2>
522 <pre>$(lsmod)</pre>
524 EOT
525 cat <<EOT
526 $(ok_status_t)
527 <tr><td>$(gettext 'Getting networking info...')</td>
528 EOT
529 cat >> $output <<EOT
530 <h2>ifconfig -a</h2>
531 <pre>$(ifconfig -a)</pre>
533 <h2>route -n</h2>
534 <pre>$(route -n)</pre>
536 <h2>/etc/resolv.conf</h2>
537 <pre>$(cat /etc/resolv.conf)</pre>
538 EOT
539 cat <<EOT
540 $(ok_status_t)
541 <tr><td>$(gettext 'Getting filesystems info...')</td>
542 EOT
543 cat >> $output <<EOT
544 <h2>blkid</h2>
545 <pre>$(blkid)</pre>
547 <h2>fdisk -l</h2>
548 <pre>$(fdisk -l)</pre>
550 <h2>mount</h2>
551 <pre>$(mount)</pre>
553 <h2>df -h</h2>
554 <pre>$(df -h)</pre>
556 <h2>df -i</h2>
557 <pre>$(df -i)</pre>
558 EOT
559 cat <<EOT
560 $(ok_status_t)
561 <tr><td>$(gettext 'Getting boot logs...')</td>
562 EOT
563 cat >> $output <<EOT
564 <h2>$(gettext 'Kernel messages')</h2>
565 <pre>$(cat /var/log/dmesg.log)</pre>
567 <h2>$(gettext 'Boot scripts')</h2>
568 <pre>$(cat /var/log/boot.log | filter_taztools_msgs)</pre>
569 EOT
570 cat <<EOT
571 $(ok_status_t)
572 <tr><td>$(gettext 'Creating report footer...')</td>
573 EOT
574 cat cat >> $output <<EOT
575 </body>
576 </html>
577 EOT
578 cat <<EOT
579 $(ok_status_t)
580 </tbody>
581 </table>
582 <footer>
583 <form><button name="file" value="$output" data-icon="view">$(gettext 'View report')</button></form>
584 </footer>
585 </section>
588 $(msg tip "$(gettext 'This report can be attached with a bug report on:')
589 <a href="http://bugs.slitaz.org/" target="_blank">bugs.slitaz.org</a></p>")
590 EOT
591 ;;
594 *)
595 #
596 # Default xHTML content
597 #
598 header; xhtml_header
599 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
600 [ -n "$(GET rdate)" ] && echo ""
601 hostname=$(hostname)
603 cat <<EOT
604 <h2>$(eval_gettext 'Host: $hostname')</h2>
605 <p>$(gettext 'SliTaz administration and configuration Panel')<p>
607 <form class="nogap"><!--
608 --><button name="terminal" data-icon="terminal">$(gettext 'Terminal' )</button><!--
609 --><button name="top" data-icon="proc" >$(gettext 'Process activity')</button><!--
610 --><button name="report" data-icon="report" >$(gettext 'Create a report' )</button>
611 </form>
613 <section>
614 <header>$(gettext 'Summary')</header>
615 <table>
616 <tr><td>$(gettext 'Uptime:')</td>
617 <td id="uptime">$(uptime | sed 's|\([0-9.:][0-9.:]*\)|<b>\1</b>|g')</td>
618 </tr>
619 <tr><td>$(gettext 'Memory in Mb:')</td>
620 <td>$(free -m | grep Mem: | \
621 awk -vline="$(gettext 'Total: %d, Used: %d, Free: %d')" \
622 '{ printf(line, $2, $3, $4) }' | \
623 sed 's|\([0-9][0-9]*\)|<b>\1</b>|g')</td>
624 </tr>
625 <tr><td>$(gettext 'Linux kernel:')</td>
626 <td>$(uname -r)</td>
627 </tr>
628 </table>
629 </section>
632 <section>
633 <header>
634 $(gettext 'Network status')
635 <form action="network.cgi">
636 <button data-icon="wifi">$(gettext 'Network')</button>
637 </form>
638 </header>
639 $(list_network_interfaces)
640 </section>
643 <section>
644 <header>
645 $(gettext 'Filesystem usage statistics')
646 <form action="hardware.cgi">
647 <button data-icon="hdd">$(gettext 'Disks')</button>
648 </form>
649 </header>
650 <table class="wide zebra center">
651 EOT
652 # Disk stats (management is done as hardware.cgi)
653 df_thead
654 echo '<tbody>'
655 df -h | grep ^/dev | while read fs size used av pct mp; do
656 cat <<EOT
657 <tr>
658 <td><span data-icon="hdd">${fs#/dev/}</span></td>
659 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
660 <td>$(blkid $fs | sed '/TYPE=/!d;s/.*TYPE="\([^"]*\).*/\1/')</td>
661 <td>$size</td>
662 <td>$av</td>
663 <td class="meter"><meter min="0" max="100" value="$(echo $pct | cut -d% -f1)"
664 low="$DU_WARN" high="$DU_CRIT" optimum="10"></meter>
665 <span>$used - $pct</span>
666 </td>
667 <td>$mp</td>
668 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
669 </tr>
670 EOT
671 done
672 cat <<EOT
673 </tbody>
674 </table>
675 </section>
677 <section>
678 <header>
679 $(gettext 'Panel Activity')
680 <form>
681 <button name="file" value="$LOG_FILE" data-icon="view">$(gettext 'View')</button>
682 </form>
683 </header>
684 <div>
685 <pre id="panel-activity">
686 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)</pre>
687 </div>
688 </section>
689 EOT
690 ;;
691 esac
693 xhtml_footer
694 exit 0