tazpanel view lib/libtazpanel @ rev 517
tazpanel: typos
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Aug 03 10:11:25 2015 +0200 (2015-08-03) |
parents | f476216ae0e1 |
children | c0997cc494ed |
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 GUI_USER=nobody
21 [ "$HTTP_HOST" == "127.0.0.1:82" ] &&
22 for i in "$HTTP_COOKIE" "$QUERY_STRING" ; do
23 v="$(echo "$i" | sed '/guiuser=/!d;s/.*guiuser=\([A-Za-z0-9_]*\).*/\1/')"
24 [ "$v" ] || continue
25 busybox ps | sed '/d;/d;/127\.0\.0\.1:82/!d' | grep -q " $v " || continue
26 GUI_USER="$v"
27 done
28 [ "$(id -un)" == "${REMOTE_USER:=$GUI_USER}" ] || exec su -s /bin/sh -c "$(realpath $0) $@" $REMOTE_USER
31 # Get parameters with GET, POST, COOKIE and FILE functions
33 . /usr/lib/slitaz/httphelper
36 # I18n
38 . /etc/locale.conf
39 . /usr/bin/gettext.sh
40 TEXTDOMAIN='tazpanel'
41 export TEXTDOMAIN LANG LC_ALL
44 # We need a config file first
46 get_config() {
47 CONFIG='/etc/slitaz/tazpanel.conf'
48 if [ -f "$CONFIG" ]; then
49 . $CONFIG
50 else
51 echo "No config file found: $CONFIG"
52 exit 1
53 fi
54 }
57 # Display < > &
59 htmlize() {
60 sed -e 's|\&|\&|g; s|<|\<|g; s|>|\>|g'
61 }
64 # Syntax highlighting for config file and SHell scripts
66 syntax_highlighter() {
67 case $1 in
68 conf)
69 htmlize | sed \
70 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="conf-var">\1</span><span class="conf-val">\2</span>|g' \
71 -e 's|^[ \t]*#.*|<span class="conf-comment">\0</span>|g' ;;
72 #-e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
73 #-e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
74 #-e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
75 #-e s"#[\"']\([^']*\)[\"']#<span class='conf-val'>\0</span>#"g ;;
76 sh)
77 htmlize | sed \
78 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="sh-var">\1</span><span class="sh-val">\2</span>|g' \
79 -e 's|^#.*|<span class="sh-comment">\0</span>|g' ;;
80 #-e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
81 #-e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
82 diff)
83 htmlize | sed \
84 -e 's|^-.*|<span class="diff-rm">\0</span>|g' \
85 -e 's|^+.*|<span class="diff-add">\0</span>|g' \
86 -e 's|^@.*|<span class="diff-at">\0</span>|g' ;;
87 activity)
88 # realize lazy quantification
89 sed -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='activity-log'>\1:</span> |" ;;
90 kernel)
91 # line with "ⓒ": realize lazy quantification
92 htmlize | sed \
93 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
94 -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='kernel-id'>\1:</span> |" \
95 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" \
96 -e "s|Call Trace:|<span class='kernel-trace'>\0</span>|" ;;
97 lsusb)
98 htmlize | sed \
99 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
100 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
101 lspci)
102 htmlize | sed \
103 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
104 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
105 xlog)
106 htmlize | sed \
107 -e 's|^[^]]*]|<span class="xlog xlog-timestamp">\0</span>|' \
108 -e 's|(--)|<span class="xlog xlog-probed" title="probed">\0</span>|' \
109 -e 's|(\*\*)|<span class="xlog xlog-config" title="from config file">\0</span>|' \
110 -e 's|(==)|<span class="xlog xlog-default" title="default setting">\0</span>|' \
111 -e 's|(++)|<span class="xlog xlog-cmdline" title="from command line">\0</span>|' \
112 -e 's|(!!)|<span class="xlog xlog-notice" title="notice">\0</span>|' \
113 -e 's|(II)|<span class="xlog xlog-info" title="informational">\0</span>|' \
114 -e 's|(WW)|<span class="xlog xlog-warn" title="warning">\0</span>|' \
115 -e 's|(EE)|<span class="xlog xlog-error" title="error">\0</span>|' \
116 -e 's|(NI)|<span class="xlog xlog-ni" title="not implemented">\0</span>|' \
117 -e 's|(??)|<span class="xlog xlog-unknown" title="unknown">\0</span>|' \
118 ;;
119 esac
120 }
123 # Remove status and ESC char from tazpkg/tazlito commands output
125 filter_taztools_msgs() {
126 sed \
127 -e 's|\\[0m|</span>|g' \
128 -e 's|\\[0;39m|</span>|g' \
129 -e 's|\\[0*\([0-7]*\);\([0-7]*\)m|<span class="color\1 color\2">|g' \
130 -e ':a;s/^\(.\{0,69\}\)\(\\[[0-9]*G\[\)/\1 \2/;ta' \
131 -e ':b;s|^\([^\]*\)\\[\([0-9]*\)G|<span style="display:inline-block;width:\2ex">\1</span>|;tb' \
132 -e 's/\[^Gm]*.//g'
133 }
136 # LOG activities
138 log() {
139 date=$(date "+%F %R")
140 filter_taztools_msgs | sed "s|[^']*|$date : \0|" >> $LOG_FILE
141 }
144 ok_status() {
145 echo '[<span class="diff-add"> OK </span>]'
146 }
149 # Network interface status
151 interface_status() {
152 ip="----"
153 status="----"
154 scan=""
155 if ifconfig | grep -A1 $1 | grep -q inet; then
156 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
157 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
158 scan="<a data-icon=\"scan\" href='network.cgi?scan=$ip&back=network.cgi'>$(_ 'Scan')</a>"
159 fi
160 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
161 status="$(_ 'connected')" &&
162 speed="$(cat /sys/class/net/$1/speed 2> /dev/null)" &&
163 [ "$speed" ] && status="$status ${speed}M"
164 echo "<td>$status</td><td>$ip</td><td>$scan</td>"
165 }
168 # Catch network interface (used in summary and network main page)
170 list_network_interfaces() {
171 cat <<EOT
172 <table class="wide zebra center">
173 <thead>
174 <tr>
175 <td>$(_ 'Interface')</td>
176 <td>$(_ 'Name')</td>
177 <td>$(_ 'Status')</td>
178 <td>$(_ 'IP Address')</td>
179 <td>$(_ 'Scan ports')</td>
180 </tr>
181 </thead>
182 <tbody>
183 EOT
184 for i in $(ls /sys/class/net); do
185 case $i in
186 eth*)
187 echo " <tr><td><a data-icon='eth' href='network.cgi?eth'>$i</a></td>
188 <td>Ethernet</td> $(interface_status $i)</tr>" ;;
189 wlan*|ath*|ra*)
190 echo " <tr><td><a data-icon='wifi' href='network.cgi?wifi'>$i</a></td>
191 <td>Wireless</td> $(interface_status $i)</tr>" ;;
192 ppp*)
193 echo " <tr><td><a data-icon='removable' href='ppp.cgi'>$i</a></td>
194 <td>Point to point</td> $(interface_status $i)</tr>" ;;
195 tap[0-9]*|tun[0-9]*)
196 echo " <tr><td><span data-icon='removable'>$i</span></td>
197 <td>User space</td> $(interface_status $i)</tr>" ;;
198 lo)
199 echo " <tr><td><span data-icon='loopback'>$i</span></td>
200 <td>Loopback</td> $(interface_status $i)</tr>" ;;
201 *)
202 continue ;;
203 esac
204 done
205 cat <<EOT
206 </tbody>
207 </table>
208 EOT
209 }
212 # Get the list of panel styles
214 list_styles() {
215 for style in $PANEL/styles/*; do
216 style=$(basename $style)
217 echo "<option value='$style'>$style</option>"
218 done
219 }
222 # Get the list of system locales
224 list_locales() {
225 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
226 do
227 echo "<option value='$locale'>$locale</option>"
228 done
229 }
232 # Get the list of console keymaps
234 list_keymaps() {
235 if [ -d /usr/share/kbd ]; then # kbd-base
236 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
237 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
238 else # kbd-busybox
239 ls /usr/share/kmap/*.kmap | \
240 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
241 fi | sort
242 }
245 #
246 # xHTML 5 (header and footer skel are from the style)
247 #
249 loading_msg() {
250 local MSG=${1:-$LOADING_MSG}
251 [ -z "$MSG" ] && MSG="$(_ 'Please wait')"
252 cat <<EOT
253 <script type="text/javascript">statusbar("$MSG", 1)</script>
254 EOT
255 }
258 xhtml_header() {
259 SUBTITLE="$1"
260 . ${PANEL}$HEADER
261 cat <<EOT
262 <script type="text/javascript">
263 document.cookie = "guiuser=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
264 document.cookie = "guiuser=$GUI_USER";
265 </script>
266 EOT
267 if [ "$DEBUG" -eq 1 ]; then
268 local i j x
269 args=''
270 for x in GET POST COOKIE; do
271 for i in $($x); do
272 if [ $($x $i count) -gt 1 ]; then
273 for j in $(seq 1 $($x $i count)); do
274 args="$args $x($i,$j)='$($x $i $j)'"
275 done
276 else
277 args="$args $x($i)='$($x $i)'"
278 fi
279 done
280 done
281 for i in $(FILE); do
282 for j in name size type tmpname; do
283 args="$args FILE($i,$j)=$(FILE $i $j)"
284 done
285 done
286 cat <<EOT
287 <pre class="debug">
288 QUERY_STRING="$QUERY_STRING"$args
289 </pre>
290 EOT
291 fi
292 }
295 xhtml_footer() {
296 . ${PANEL}$FOOTER
297 }
300 table_start() { echo '<table>'; }
301 table_end() { echo '</table>'; }
304 df_thead() {
305 cat <<EOT
306 <thead>
307 <tr>
308 <td>$(_ 'Disk')</td>
309 <td>$(_ 'Label')</td>
310 <td>$(_ 'Type')</td>
311 <td>$(_ 'Size')</td>
312 <td>$(_ 'Available')</td>
313 <td>$(_ 'Used')</td>
314 <td>$(_ 'Mount point')</td>
315 <td>UUID</td>
316 </tr>
317 </thead>
318 EOT
319 }
322 msg() {
323 msgtype="$1"; shift
324 case "$msgtype" in
325 tip) MSG_ICON='msgtip';;
326 warn|warning) MSG_ICON='msgwarn';;
327 err|error) MSG_ICON='msgerr';;
328 up) MSG_ICON='msgup';;
329 *) MSG_ICON='msg';;
330 esac
331 cat <<EOT
332 <section class="box"><span data-icon="$MSG_ICON"></span>$@</section>
333 EOT
334 }
337 is_installed() {
338 [ -d "$INSTALLED/$1" ]
339 }
342 blk2h() {
343 if [ "$1" ]; then
344 echo $1
345 else
346 cat
347 fi | awk '{
348 n = $0/2
349 for (i = 1; n > 1024; i++)
350 n /= 1024
351 f = "%1.0f%c"
352 if (n < 100)
353 f = "%1.1f%c"
354 printf f,n,substr("KMGT", i, 1)
355 }'
356 }
359 # Show "Back" button
361 back_button() {
362 local URL="$1" default_caption="$(_ 'Back')"
363 local caption="${2:-$default_caption}" icon="${3:-back}"
365 if [ -n "$1" ]; then
366 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
367 "$URL" "$icon" "$caption"
368 fi
369 }
372 # Mark select option as checked
374 selected() {
375 [ "$1" == "$2" ] && echo -n 'selected'
376 }
379 check_root_tazpanel() {
380 if [ $(id -u) != '0' ]; then
381 msg err "$(_ 'You must be root to show this page.')"
382 xhtml_footer; exit 0
383 fi
384 }
386 # Get system database. NSS compatible.
388 getdb() {
389 getent $1 2>/dev/null || cat /etc/$1
390 }