tazpanel view lib/libtazpanel @ rev 482

libtazpanel: show ppp conections
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon May 11 16:09:05 2015 +0200 (2015-05-11)
parents 7ca14d55e705
children 16c27be73670
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 if ifconfig | grep -A1 $1 | grep -q inet; then
145 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
146 cat <<EOT
147 <td>$(_ 'connected')</td>
148 <td>$ip</td>
149 <td><a data-icon="scan" href='network.cgi?scan=$ip&amp;back=network.cgi'>$(_ 'Scan')</a></td>
150 EOT
151 else
152 echo "<td>----</td><td>----</td><td></td>"
153 fi
154 }
157 # Catch network interface (used in summary and network main page)
159 list_network_interfaces() {
160 cat <<EOT
161 <table class="wide zebra center">
162 <thead>
163 <tr>
164 <td>$(_ 'Interface')</td>
165 <td>$(_ 'Name')</td>
166 <td>$(_ 'Status')</td>
167 <td>$(_ 'IP Address')</td>
168 <td>$(_ 'Scan ports')</td>
169 </tr>
170 </thead>
171 <tbody>
172 EOT
173 for i in $(ls /sys/class/net); do
174 case $i in
175 eth*)
176 echo " <tr><td><a data-icon='eth' href='network.cgi?eth'>$i</a></td>
177 <td>Ethernet</td> $(interface_status $i)</tr>" ;;
178 wlan*|ath*|ra*)
179 echo " <tr><td><a data-icon='wifi' href='network.cgi?wifi'>$i</a></td>
180 <td>Wireless</td> $(interface_status $i)</tr>" ;;
181 ppp*)
182 echo " <tr><td><span data-icon='removable'>$i</span></td>
183 <td>Point to point</td> $(interface_status $i)</tr>" ;;
184 lo)
185 echo " <tr><td><span data-icon='loopback'>$i</span></td>
186 <td>Loopback</td> $(interface_status $i)</tr>" ;;
187 *)
188 continue ;;
189 esac
190 done
191 cat <<EOT
192 </tbody>
193 </table>
194 EOT
195 }
198 # Get the list of panel styles
200 list_styles() {
201 for style in $PANEL/styles/*
202 do
203 style=$(basename $style)
204 echo "<option value='$style'>$style</option>"
205 done
206 }
209 # Get the list of system locales
211 list_locales() {
212 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
213 do
214 echo "<option value='$locale'>$locale</option>"
215 done
216 }
219 # Get the list of console keymaps
221 list_keymaps() {
222 if [ -d /usr/share/kbd ]; then # kbd-base
223 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
224 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
225 else # kbd-busybox
226 ls /usr/share/kmap/*.kmap | \
227 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
228 fi | sort
229 }
232 #
233 # xHTML 5 (header and footer skel are from the style)
234 #
236 loading_msg() {
237 local MSG=${1:-$LOADING_MSG}
238 cat <<EOT
239 <div id="loading"><img src="/styles/default/images/loader.gif"/>$MSG</div>
240 EOT
241 }
244 xhtml_header() {
245 . ${PANEL}$HEADER
246 if [ $DEBUG == "1" ]; then
247 local i
248 local j
249 local x
250 args=""
251 for x in GET POST COOKIE; do
252 for i in $($x); do
253 if [ $($x $i count) -gt 1 ]; then
254 for j in $(seq 1 $($x $i count)); do
255 args="$args $x($i,$j)='$($x $i $j)'"
256 done
257 else
258 args="$args $x($i)='$($x $i)'"
259 fi
260 done
261 done
262 for i in $(FILE); do
263 for j in name size type tmpname; do
264 args="$args FILE($i,$j)=$(FILE $i $j)"
265 done
266 done
267 cat <<EOT
268 <pre class="debug">
269 QUERY_STRING="$QUERY_STRING"$args
270 </pre>
271 EOT
272 fi
273 }
276 xhtml_footer() {
277 . ${PANEL}$FOOTER
278 }
281 table_start() {
282 echo '<table>'
283 }
286 table_end() {
287 echo '</table>'
288 }
291 df_thead() {
292 cat <<EOT
293 <thead>
294 <tr>
295 <td>$(_ 'Disk')</td>
296 <td>$(_ 'Label')</td>
297 <td>$(_ 'Type')</td>
298 <td>$(_ 'Size')</td>
299 <td>$(_ 'Available')</td>
300 <td>$(_ 'Used')</td>
301 <td>$(_ 'Mount point')</td>
302 <td>UUID</td>
303 </tr>
304 </thead>
305 EOT
306 }
309 msg() {
310 msgtype="$1"; shift
311 case "$msgtype" in
312 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
313 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
314 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
315 up) MSG_ICON="$IMAGES/msg-up.png" ;;
316 *) MSG_ICON="$IMAGES/msg.png" ;;
317 esac
318 cat <<EOT
319 <section class="box"><img src="$MSG_ICON" alt="$msgtype"/>
320 $@
321 </section>
322 EOT
323 }
326 is_installed() {
327 [ -d "$INSTALLED/$1" ]
328 }
331 blk2h() {
332 if [ "$1" ]; then
333 echo $1
334 else
335 cat
336 fi | awk '{
337 n = $0/2
338 for (i = 1; n > 1024; i++)
339 n /= 1024
340 f = "%1.0f%c"
341 if (n < 100)
342 f = "%1.1f%c"
343 printf f,n,substr("KMGT", i, 1)
344 }'
345 }
348 # Show "Back" button
350 back_button() {
351 local URL="$1" default_caption="$(_ 'Back')"
352 local caption="${2:-$default_caption}" icon="${3:-back}"
354 if [ -n "$1" ]; then
355 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
356 "$URL" "$icon" "$caption"
357 fi
358 }
361 # Mark select option as checked
363 selected() {
364 [ "$1" == "$2" ] && echo -n 'selected'
365 }
368 check_root_tazpanel() {
369 if [ $(id -u) != '0' ]; then
370 msg err "$(_ 'You must be root to show this page.')"
371 xhtml_footer; exit 0
372 fi
373 }