tazpanel view lib/libtazpanel @ rev 487

libtazpanel: show carrier (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue May 12 13:25:48 2015 +0200 (2015-05-12)
parents 9309ccae1a59
children b9a33d89d025
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
11 # Redefine gettext functions
13 _() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; echo; }
14 _n() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; }
15 _p() {
16 local S="$1" P="$2" N="$3"; shift 3;
17 printf "$(ngettext "$S" "$P" "$N")" "$@"; }
20 [ "$(id -un)" == "${REMOTE_USER:=nobody}" ] || exec su -s /bin/sh -c "$(realpath $0) $@" $REMOTE_USER
23 # Get parameters with GET, POST and FILE functions
25 . /usr/lib/slitaz/httphelper
28 # I18n
30 . /etc/locale.conf
31 . /usr/bin/gettext.sh
32 TEXTDOMAIN='tazpanel'
33 export TEXTDOMAIN LANG LC_ALL
36 # We need a config file first
38 get_config() {
39 CONFIG='/etc/slitaz/tazpanel.conf'
40 if [ -f "$CONFIG" ]; then
41 . $CONFIG
42 else
43 echo "No config file found: $CONFIG"
44 exit 1
45 fi
46 }
49 # Display < > &
51 htmlize() {
52 sed -e 's|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
53 }
56 # Syntax highlighting for config file and SHell scripts
58 syntax_highlighter() {
59 case $1 in
60 conf)
61 htmlize | sed \
62 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="conf-var">\1</span><span class="conf-val">\2</span>|g' \
63 -e 's|^[ \t]*#.*|<span class="conf-comment">\0</span>|g' ;;
64 #-e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
65 #-e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
66 #-e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
67 #-e s"#[\"']\([^']*\)[\"']#<span class='conf-val'>\0</span>#"g ;;
68 sh)
69 htmlize | sed \
70 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="sh-var">\1</span><span class="sh-val">\2</span>|g' \
71 -e 's|^#.*|<span class="sh-comment">\0</span>|g' ;;
72 #-e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
73 #-e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
74 diff)
75 htmlize | sed \
76 -e 's|^-.*|<span class="diff-rm">\0</span>|g' \
77 -e 's|^+.*|<span class="diff-add">\0</span>|g' \
78 -e 's|^@.*|<span class="diff-at">\0</span>|g' ;;
79 activity)
80 # realize lazy quantification
81 sed -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='activity-log'>\1:</span> |" ;;
82 kernel)
83 # line with "ⓒ": realize lazy quantification
84 htmlize | sed \
85 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
86 -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='kernel-id'>\1:</span> |" \
87 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" \
88 -e "s|Call Trace:|<span class='kernel-trace'>\0</span>|" ;;
89 lsusb)
90 htmlize | sed \
91 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
92 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
93 lspci)
94 htmlize | sed \
95 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
96 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
97 xlog)
98 htmlize | sed \
99 -e 's|^[^]]*]|<span class="xlog xlog-timestamp">\0</span>|' \
100 -e 's|(--)|<span class="xlog xlog-probed" title="probed">\0</span>|' \
101 -e 's|(\*\*)|<span class="xlog xlog-config" title="from config file">\0</span>|' \
102 -e 's|(==)|<span class="xlog xlog-default" title="default setting">\0</span>|' \
103 -e 's|(++)|<span class="xlog xlog-cmdline" title="from command line">\0</span>|' \
104 -e 's|(!!)|<span class="xlog xlog-notice" title="notice">\0</span>|' \
105 -e 's|(II)|<span class="xlog xlog-info" title="informational">\0</span>|' \
106 -e 's|(WW)|<span class="xlog xlog-warn" title="warning">\0</span>|' \
107 -e 's|(EE)|<span class="xlog xlog-error" title="error">\0</span>|' \
108 -e 's|(NI)|<span class="xlog xlog-ni" title="not implemented">\0</span>|' \
109 -e 's|(??)|<span class="xlog xlog-unknown" title="unknown">\0</span>|' \
110 ;;
111 esac
112 }
115 # Remove status and ESC char from tazpkg/tazlito commands output
117 filter_taztools_msgs() {
118 sed \
119 -e 's|\\[0m|</span>|g' \
120 -e 's|\\[0;39m|</span>|g' \
121 -e 's|\\[0*\([0-7]*\);\([0-7]*\)m|<span class="color\1 color\2">|g' \
122 -e ':a;s/^\(.\{0,69\}\)\(\\[[0-9]*G\[\)/\1 \2/;ta' \
123 -e ':b;s|^\([^\]*\)\\[\([0-9]*\)G|<span style="display:inline-block;width:\2ex">\1</span>|;tb' \
124 -e 's/\[^Gm]*.//g'
125 }
128 # LOG activities
130 log() {
131 date=$(date "+%F %R")
132 filter_taztools_msgs | sed "s|[^']*|$date : \0|" >> $LOG_FILE
133 }
136 ok_status() {
137 echo '[<span class="diff-add"> OK </span>]'
138 }
141 # Network interface status
143 interface_status() {
144 ip="----"
145 status="----"
146 scan=""
147 if ifconfig | grep -A1 $1 | grep -q inet; then
148 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
149 if [ "$(cat /sys/class/net/$1/carrier)" == "1" ]; then
150 status="$(_ 'connected')"
151 scan="<a data-icon=\"scan\" href='network.cgi?scan=$ip&amp;back=network.cgi'>$(_ 'Scan')</a>"
152 fi
153 fi
154 echo "<td>$status</td><td>$ip</td><td>$scan</td>"
155 }
158 # Catch network interface (used in summary and network main page)
160 list_network_interfaces() {
161 cat <<EOT
162 <table class="wide zebra center">
163 <thead>
164 <tr>
165 <td>$(_ 'Interface')</td>
166 <td>$(_ 'Name')</td>
167 <td>$(_ 'Status')</td>
168 <td>$(_ 'IP Address')</td>
169 <td>$(_ 'Scan ports')</td>
170 </tr>
171 </thead>
172 <tbody>
173 EOT
174 for i in $(ls /sys/class/net); do
175 case $i in
176 eth*)
177 echo " <tr><td><a data-icon='eth' href='network.cgi?eth'>$i</a></td>
178 <td>Ethernet</td> $(interface_status $i)</tr>" ;;
179 wlan*|ath*|ra*)
180 echo " <tr><td><a data-icon='wifi' href='network.cgi?wifi'>$i</a></td>
181 <td>Wireless</td> $(interface_status $i)</tr>" ;;
182 ppp*)
183 echo " <tr><td><a data-icon='removable' href='ppp.cgi'>$i</a></td>
184 <td>Point to point</td> $(interface_status $i)</tr>" ;;
185 tap[0-9]*|tun[0-9]*)
186 echo " <tr><td><span data-icon='removable'>$i</span></td>
187 <td>User space</td> $(interface_status $i)</tr>" ;;
188 lo)
189 echo " <tr><td><span data-icon='loopback'>$i</span></td>
190 <td>Loopback</td> $(interface_status $i)</tr>" ;;
191 *)
192 continue ;;
193 esac
194 done
195 cat <<EOT
196 </tbody>
197 </table>
198 EOT
199 }
202 # Get the list of panel styles
204 list_styles() {
205 for style in $PANEL/styles/*
206 do
207 style=$(basename $style)
208 echo "<option value='$style'>$style</option>"
209 done
210 }
213 # Get the list of system locales
215 list_locales() {
216 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
217 do
218 echo "<option value='$locale'>$locale</option>"
219 done
220 }
223 # Get the list of console keymaps
225 list_keymaps() {
226 if [ -d /usr/share/kbd ]; then # kbd-base
227 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
228 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
229 else # kbd-busybox
230 ls /usr/share/kmap/*.kmap | \
231 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
232 fi | sort
233 }
236 #
237 # xHTML 5 (header and footer skel are from the style)
238 #
240 loading_msg() {
241 local MSG=${1:-$LOADING_MSG}
242 cat <<EOT
243 <div id="loading"><img src="/styles/default/images/loader.gif"/>$MSG</div>
244 EOT
245 }
248 xhtml_header() {
249 . ${PANEL}$HEADER
250 if [ $DEBUG == "1" ]; then
251 local i
252 local j
253 local x
254 args=""
255 for x in GET POST COOKIE; do
256 for i in $($x); do
257 if [ $($x $i count) -gt 1 ]; then
258 for j in $(seq 1 $($x $i count)); do
259 args="$args $x($i,$j)='$($x $i $j)'"
260 done
261 else
262 args="$args $x($i)='$($x $i)'"
263 fi
264 done
265 done
266 for i in $(FILE); do
267 for j in name size type tmpname; do
268 args="$args FILE($i,$j)=$(FILE $i $j)"
269 done
270 done
271 cat <<EOT
272 <pre class="debug">
273 QUERY_STRING="$QUERY_STRING"$args
274 </pre>
275 EOT
276 fi
277 }
280 xhtml_footer() {
281 . ${PANEL}$FOOTER
282 }
285 table_start() {
286 echo '<table>'
287 }
290 table_end() {
291 echo '</table>'
292 }
295 df_thead() {
296 cat <<EOT
297 <thead>
298 <tr>
299 <td>$(_ 'Disk')</td>
300 <td>$(_ 'Label')</td>
301 <td>$(_ 'Type')</td>
302 <td>$(_ 'Size')</td>
303 <td>$(_ 'Available')</td>
304 <td>$(_ 'Used')</td>
305 <td>$(_ 'Mount point')</td>
306 <td>UUID</td>
307 </tr>
308 </thead>
309 EOT
310 }
313 msg() {
314 msgtype="$1"; shift
315 case "$msgtype" in
316 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
317 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
318 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
319 up) MSG_ICON="$IMAGES/msg-up.png" ;;
320 *) MSG_ICON="$IMAGES/msg.png" ;;
321 esac
322 cat <<EOT
323 <section class="box"><img src="$MSG_ICON" alt="$msgtype"/>
324 $@
325 </section>
326 EOT
327 }
330 is_installed() {
331 [ -d "$INSTALLED/$1" ]
332 }
335 blk2h() {
336 if [ "$1" ]; then
337 echo $1
338 else
339 cat
340 fi | awk '{
341 n = $0/2
342 for (i = 1; n > 1024; i++)
343 n /= 1024
344 f = "%1.0f%c"
345 if (n < 100)
346 f = "%1.1f%c"
347 printf f,n,substr("KMGT", i, 1)
348 }'
349 }
352 # Show "Back" button
354 back_button() {
355 local URL="$1" default_caption="$(_ 'Back')"
356 local caption="${2:-$default_caption}" icon="${3:-back}"
358 if [ -n "$1" ]; then
359 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
360 "$URL" "$icon" "$caption"
361 fi
362 }
365 # Mark select option as checked
367 selected() {
368 [ "$1" == "$2" ] && echo -n 'selected'
369 }
372 check_root_tazpanel() {
373 if [ $(id -u) != '0' ]; then
374 msg err "$(_ 'You must be root to show this page.')"
375 xhtml_footer; exit 0
376 fi
377 }