tazpanel view lib/libtazpanel @ rev 460
lib/libtazpanel: fix blk2h
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Apr 13 12:44:30 2015 +0200 (2015-04-13) |
parents | 5d33b8aaf36d |
children | 7ca14d55e705 |
line source
1 #!/bin/sh
2 #
3 # Common functions for TazPanel CGI and cmdline interface
4 #
5 # Copyright (C) 2011-2015 SliTaz GNU/Linux - BSD License
6 #
8 . /lib/libtaz.sh
10 [ "$(id -un)" == "${REMOTE_USER:=nobody}" ] || exec su -s /bin/sh -c "$(realpath $0) $@" $REMOTE_USER
13 # Get parameters with GET, POST and FILE functions
15 . /usr/lib/slitaz/httphelper
18 # I18n
20 . /etc/locale.conf
21 . /usr/bin/gettext.sh
22 TEXTDOMAIN='tazpanel'
23 export TEXTDOMAIN LANG LC_ALL
26 # We need a config file first
28 get_config() {
29 CONFIG='/etc/slitaz/tazpanel.conf'
30 if [ -f "$CONFIG" ]; then
31 . $CONFIG
32 else
33 echo "No config file found: $CONFIG"
34 exit 1
35 fi
36 }
39 # Display < > &
41 htmlize() {
42 sed -e 's|\&|\&|g; s|<|\<|g; s|>|\>|g'
43 }
46 # Syntax highlighting for config file and SHell scripts
48 syntax_highlighter() {
49 case $1 in
50 conf)
51 htmlize | sed \
52 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="conf-var">\1</span><span class="conf-val">\2</span>|g' \
53 -e 's|^[ \t]*#.*|<span class="conf-comment">\0</span>|g' ;;
54 #-e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
55 #-e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
56 #-e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
57 #-e s"#[\"']\([^']*\)[\"']#<span class='conf-val'>\0</span>#"g ;;
58 sh)
59 htmlize | sed \
60 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="sh-var">\1</span><span class="sh-val">\2</span>|g' \
61 -e 's|^#.*|<span class="sh-comment">\0</span>|g' ;;
62 #-e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
63 #-e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
64 diff)
65 htmlize | sed \
66 -e 's|^-.*|<span class="diff-rm">\0</span>|g' \
67 -e 's|^+.*|<span class="diff-add">\0</span>|g' \
68 -e 's|^@.*|<span class="diff-at">\0</span>|g' ;;
69 activity)
70 # realize lazy quantification
71 sed -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='activity-log'>\1:</span> |" ;;
72 kernel)
73 # line with "ⓒ": realize lazy quantification
74 htmlize | sed \
75 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
76 -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='kernel-id'>\1:</span> |" \
77 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" \
78 -e "s|Call Trace:|<span class='kernel-trace'>\0</span>|" ;;
79 lsusb)
80 htmlize | sed \
81 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
82 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
83 lspci)
84 htmlize | sed \
85 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
86 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
87 xlog)
88 htmlize | sed \
89 -e 's|^[^]]*]|<span class="xlog xlog-timestamp">\0</span>|' \
90 -e 's|(--)|<span class="xlog xlog-probed" title="probed">\0</span>|' \
91 -e 's|(\*\*)|<span class="xlog xlog-config" title="from config file">\0</span>|' \
92 -e 's|(==)|<span class="xlog xlog-default" title="default setting">\0</span>|' \
93 -e 's|(++)|<span class="xlog xlog-cmdline" title="from command line">\0</span>|' \
94 -e 's|(!!)|<span class="xlog xlog-notice" title="notice">\0</span>|' \
95 -e 's|(II)|<span class="xlog xlog-info" title="informational">\0</span>|' \
96 -e 's|(WW)|<span class="xlog xlog-warn" title="warning">\0</span>|' \
97 -e 's|(EE)|<span class="xlog xlog-error" title="error">\0</span>|' \
98 -e 's|(NI)|<span class="xlog xlog-ni" title="not implemented">\0</span>|' \
99 -e 's|(??)|<span class="xlog xlog-unknown" title="unknown">\0</span>|' \
100 ;;
101 esac
102 }
105 # Remove status and ESC char from tazpkg/tazlito commands output
107 filter_taztools_msgs() {
108 sed \
109 -e 's|\\[0m|</span>|g' \
110 -e 's|\\[0;39m|</span>|g' \
111 -e 's|\\[0*\([0-7]*\);\([0-7]*\)m|<span class="color\1 color\2">|g' \
112 -e ':a;s/^\(.\{0,69\}\)\(\\[[0-9]*G\[\)/\1 \2/;ta' \
113 -e ':b;s|^\([^\]*\)\\[\([0-9]*\)G|<span style="display:inline-block;width:\2ex">\1</span>|;tb' \
114 -e 's/\[^Gm]*.//g'
115 }
118 # LOG activities
120 log() {
121 date=$(date "+%F %R")
122 filter_taztools_msgs | sed "s|[^']*|$date : \0|" >> $LOG_FILE
123 }
126 ok_status() {
127 echo '[<span class="diff-add"> OK </span>]'
128 }
131 # Network interface status
133 interface_status() {
134 if ifconfig | grep -A1 $1 | grep -q inet; then
135 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
136 cat <<EOT
137 <td>$(_ 'connected')</td>
138 <td>$ip</td>
139 <td><a data-icon="scan" href='network.cgi?scan=$ip&back=network.cgi'>$(_ 'Scan')</a></td>
140 EOT
141 else
142 echo "<td>----</td><td>----</td><td></td>"
143 fi
144 }
147 # Catch network interface (used in summary and network main page)
149 list_network_interfaces() {
150 cat <<EOT
151 <table class="wide zebra center">
152 <thead>
153 <tr>
154 <td>$(_ 'Interface')</td>
155 <td>$(_ 'Name')</td>
156 <td>$(_ 'Status')</td>
157 <td>$(_ 'IP Address')</td>
158 <td>$(_ 'Scan ports')</td>
159 </tr>
160 </thead>
161 <tbody>
162 EOT
163 for i in $(ls /sys/class/net); do
164 case $i in
165 eth*)
166 echo " <tr><td><a data-icon='eth' href='network.cgi?eth'>$i</a></td>
167 <td>Ethernet</td> $(interface_status $i)</tr>" ;;
168 wlan*|ath*|ra*)
169 echo " <tr><td><a data-icon='wifi' href='network.cgi?wifi'>$i</a></td>
170 <td>Wireless</td> $(interface_status $i)</tr>" ;;
171 lo)
172 echo " <tr><td><span data-icon='loopback'>$i</span></td>
173 <td>Loopback</td> $(interface_status $i)</tr>" ;;
174 *)
175 continue ;;
176 esac
177 done
178 cat <<EOT
179 </tbody>
180 </table>
181 EOT
182 }
185 # Get the list of panel styles
187 list_styles() {
188 for style in $PANEL/styles/*
189 do
190 style=$(basename $style)
191 echo "<option value='$style'>$style</option>"
192 done
193 }
196 # Get the list of system locales
198 list_locales() {
199 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
200 do
201 echo "<option value='$locale'>$locale</option>"
202 done
203 }
206 # Get the list of console keymaps
208 list_keymaps() {
209 if [ -d /usr/share/kbd ]; then # kbd-base
210 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
211 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
212 else # kbd-busybox
213 ls /usr/share/kmap/*.kmap | \
214 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
215 fi | sort
216 }
219 #
220 # xHTML 5 (header and footer skel are from the style)
221 #
223 loading_msg() {
224 local MSG=${1:-$LOADING_MSG}
225 cat <<EOT
226 <div id="loading"><img src="/styles/default/images/loader.gif"/>$MSG</div>
227 EOT
228 }
231 xhtml_header() {
232 . ${PANEL}$HEADER
233 if [ $DEBUG == "1" ]; then
234 local i
235 local j
236 local x
237 args=""
238 for x in GET POST COOKIE; do
239 for i in $($x); do
240 if [ $($x $i count) -gt 1 ]; then
241 for j in $(seq 1 $($x $i count)); do
242 args="$args $x($i,$j)='$($x $i $j)'"
243 done
244 else
245 args="$args $x($i)='$($x $i)'"
246 fi
247 done
248 done
249 for i in $(FILE); do
250 for j in name size type tmpname; do
251 args="$args FILE($i,$j)=$(FILE $i $j)"
252 done
253 done
254 cat <<EOT
255 <pre class="debug">
256 QUERY_STRING="$QUERY_STRING"$args
257 </pre>
258 EOT
259 fi
260 }
263 xhtml_footer() {
264 . ${PANEL}$FOOTER
265 }
268 table_start() {
269 echo '<table>'
270 }
273 table_end() {
274 echo '</table>'
275 }
278 df_thead() {
279 cat <<EOT
280 <thead>
281 <tr>
282 <td>$(_ 'Disk')</td>
283 <td>$(_ 'Label')</td>
284 <td>$(_ 'Type')</td>
285 <td>$(_ 'Size')</td>
286 <td>$(_ 'Available')</td>
287 <td>$(_ 'Used')</td>
288 <td>$(_ 'Mount point')</td>
289 <td>UUID</td>
290 </tr>
291 </thead>
292 EOT
293 }
296 msg() {
297 msgtype="$1"; shift
298 case "$msgtype" in
299 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
300 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
301 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
302 up) MSG_ICON="$IMAGES/msg-up.png" ;;
303 *) MSG_ICON="$IMAGES/msg.png" ;;
304 esac
305 cat <<EOT
306 <section class="box"><img src="$MSG_ICON" alt="$msgtype"/>
307 $@
308 </section>
309 EOT
310 }
313 is_installed() {
314 [ -d "$INSTALLED/$1" ]
315 }
318 blk2h() {
319 if [ "$1" ]; then
320 echo $1
321 else
322 cat
323 fi | awk '{
324 n = $0/2
325 for (i = 1; n > 1024; i++)
326 n /= 1024
327 f = "%1.0f%c"
328 if (n < 100)
329 f = "%1.1f%c"
330 printf f,n,substr("KMGT", i, 1)
331 }'
332 }
335 # Show "Back" button
337 back_button() {
338 local URL="$1" default_caption="$(_ 'Back')"
339 local caption="${2:-$default_caption}" icon="${3:-back}"
341 if [ -n "$1" ]; then
342 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
343 "$URL" "$icon" "$caption"
344 fi
345 }
348 # Mark select option as checked
350 selected() {
351 [ "$1" == "$2" ] && echo -n 'selected'
352 }
355 check_root_tazpanel() {
356 if [ $(id -u) != '0' ]; then
357 msg err "$(_ 'You must be root to show this page.')"
358 xhtml_footer; exit 0
359 fi
360 }