tazpanel view lib/libtazpanel @ rev 432

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