tazpanel view index.cgi @ rev 428

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