tazpanel view lib/libtazpanel @ rev 437

(fix) terminal for user, history support; (fix) keymap list (again); (fix) month selected in clock setup; (feauture) add user/root panel indicator and re-login button; (feature) do not hide menu items and buttons (possibly), just disable them (to see all possibilities).
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Apr 07 00:27:17 2015 +0300 (2015-04-07)
parents 2ce8e949aef9
children f09ef19be78d
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:-root}" ] || exec su -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|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|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>$(gettext 'connected')</td>
138 <td>$ip</td>
139 <td><a data-icon="scan" href='network.cgi?scan=$ip&amp;back=network.cgi'>$(gettext '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>$(gettext 'Interface')</td>
155 <td>$(gettext 'Name')</td>
156 <td>$(gettext 'Status')</td>
157 <td>$(gettext 'IP Address')</td>
158 <td>$(gettext '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 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
210 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}' | \
211 sort
212 }
215 #
216 # xHTML 5 (header and footer skel are from the style)
217 #
219 loading_msg() {
220 local MSG=${1:-$LOADING_MSG}
221 cat <<EOT
222 <div id="loading"><img src="/styles/default/images/loader.gif"/>$MSG</div>
223 EOT
224 }
227 xhtml_header() {
228 . ${PANEL}$HEADER
229 if [ $DEBUG == "1" ]; then
230 local i
231 local j
232 local x
233 args=""
234 for x in GET POST COOKIE; do
235 for i in $($x); do
236 if [ $($x $i count) -gt 1 ]; then
237 for j in $(seq 1 $($x $i count)); do
238 args="$args $x($i,$j)='$($x $i $j)'"
239 done
240 else
241 args="$args $x($i)='$($x $i)'"
242 fi
243 done
244 done
245 for i in $(FILE); do
246 for j in name size type tmpname; do
247 args="$args FILE($i,$j)=$(FILE $i $j)"
248 done
249 done
250 cat <<EOT
251 <pre class="debug">
252 QUERY_STRING="$QUERY_STRING"$args
253 </pre>
254 EOT
255 fi
256 }
259 xhtml_footer() {
260 . ${PANEL}$FOOTER
261 }
264 table_start() {
265 echo '<table>'
266 }
269 table_end() {
270 echo '</table>'
271 }
274 df_thead() {
275 cat <<EOT
276 <thead>
277 <tr>
278 <td>$(gettext 'Disk')</td>
279 <td>$(gettext 'Label')</td>
280 <td>$(gettext 'Type')</td>
281 <td>$(gettext 'Size')</td>
282 <td>$(gettext 'Available')</td>
283 <td>$(gettext 'Used')</td>
284 <td>$(gettext 'Mount point')</td>
285 <td>UUID</td>
286 </tr>
287 </thead>
288 EOT
289 }
292 msg() {
293 msgtype="$1"; shift
294 case "$msgtype" in
295 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
296 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
297 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
298 up) MSG_ICON="$IMAGES/msg-up.png" ;;
299 *) MSG_ICON="$IMAGES/msg.png" ;;
300 esac
301 cat <<EOT
302 <section class="box"><img src="$MSG_ICON" alt="$msgtype"/>
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>' \
335 "$URL" "$icon" "$caption"
336 fi
337 }
340 # Mark select option as checked
342 selected() {
343 [ "$1" == "$2" ] && echo -n 'selected'
344 }
347 check_root_tazpanel() {
348 if [ $(id -u) != '0' ]; then
349 msg err "$(_ 'You must be root to show this page.')"
350 xhtml_footer; exit 0
351 fi
352 }