tazpanel view lib/libtazpanel @ rev 511

Set default gui user
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jul 28 12:26:28 2015 +0200 (2015-07-28)
parents 0e464ccf165e
children f476216ae0e1
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 ps | sed '/d;/d;/127\.0\.0\.1:82/!d' | grep -q " $v " || continue
26 GUI_USER="$v"
27 done
28 [ "$REMOTE_USER" ] || REMOTE_USER="$GUI_USER"
29 [ "$(id -un)" == "$REMOTE_USER" ] || exec su -s /bin/sh -c "$(realpath $0) $@" $REMOTE_USER
32 # Get parameters with GET, POST, COOKIE and FILE functions
34 . /usr/lib/slitaz/httphelper
37 # I18n
39 . /etc/locale.conf
40 . /usr/bin/gettext.sh
41 TEXTDOMAIN='tazpanel'
42 export TEXTDOMAIN LANG LC_ALL
45 # We need a config file first
47 get_config() {
48 CONFIG='/etc/slitaz/tazpanel.conf'
49 if [ -f "$CONFIG" ]; then
50 . $CONFIG
51 else
52 echo "No config file found: $CONFIG"
53 exit 1
54 fi
55 }
58 # Display < > &
60 htmlize() {
61 sed -e 's|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
62 }
65 # Syntax highlighting for config file and SHell scripts
67 syntax_highlighter() {
68 case $1 in
69 conf)
70 htmlize | sed \
71 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="conf-var">\1</span><span class="conf-val">\2</span>|g' \
72 -e 's|^[ \t]*#.*|<span class="conf-comment">\0</span>|g' ;;
73 #-e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
74 #-e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
75 #-e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
76 #-e s"#[\"']\([^']*\)[\"']#<span class='conf-val'>\0</span>#"g ;;
77 sh)
78 htmlize | sed \
79 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="sh-var">\1</span><span class="sh-val">\2</span>|g' \
80 -e 's|^#.*|<span class="sh-comment">\0</span>|g' ;;
81 #-e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
82 #-e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
83 diff)
84 htmlize | sed \
85 -e 's|^-.*|<span class="diff-rm">\0</span>|g' \
86 -e 's|^+.*|<span class="diff-add">\0</span>|g' \
87 -e 's|^@.*|<span class="diff-at">\0</span>|g' ;;
88 activity)
89 # realize lazy quantification
90 sed -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='activity-log'>\1:</span> |" ;;
91 kernel)
92 # line with "ⓒ": realize lazy quantification
93 htmlize | sed \
94 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
95 -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='kernel-id'>\1:</span> |" \
96 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" \
97 -e "s|Call Trace:|<span class='kernel-trace'>\0</span>|" ;;
98 lsusb)
99 htmlize | sed \
100 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
101 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
102 lspci)
103 htmlize | sed \
104 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
105 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
106 xlog)
107 htmlize | sed \
108 -e 's|^[^]]*]|<span class="xlog xlog-timestamp">\0</span>|' \
109 -e 's|(--)|<span class="xlog xlog-probed" title="probed">\0</span>|' \
110 -e 's|(\*\*)|<span class="xlog xlog-config" title="from config file">\0</span>|' \
111 -e 's|(==)|<span class="xlog xlog-default" title="default setting">\0</span>|' \
112 -e 's|(++)|<span class="xlog xlog-cmdline" title="from command line">\0</span>|' \
113 -e 's|(!!)|<span class="xlog xlog-notice" title="notice">\0</span>|' \
114 -e 's|(II)|<span class="xlog xlog-info" title="informational">\0</span>|' \
115 -e 's|(WW)|<span class="xlog xlog-warn" title="warning">\0</span>|' \
116 -e 's|(EE)|<span class="xlog xlog-error" title="error">\0</span>|' \
117 -e 's|(NI)|<span class="xlog xlog-ni" title="not implemented">\0</span>|' \
118 -e 's|(??)|<span class="xlog xlog-unknown" title="unknown">\0</span>|' \
119 ;;
120 esac
121 }
124 # Remove status and ESC char from tazpkg/tazlito commands output
126 filter_taztools_msgs() {
127 sed \
128 -e 's|\\[0m|</span>|g' \
129 -e 's|\\[0;39m|</span>|g' \
130 -e 's|\\[0*\([0-7]*\);\([0-7]*\)m|<span class="color\1 color\2">|g' \
131 -e ':a;s/^\(.\{0,69\}\)\(\\[[0-9]*G\[\)/\1 \2/;ta' \
132 -e ':b;s|^\([^\]*\)\\[\([0-9]*\)G|<span style="display:inline-block;width:\2ex">\1</span>|;tb' \
133 -e 's/\[^Gm]*.//g'
134 }
137 # LOG activities
139 log() {
140 date=$(date "+%F %R")
141 filter_taztools_msgs | sed "s|[^']*|$date : \0|" >> $LOG_FILE
142 }
145 ok_status() {
146 echo '[<span class="diff-add"> OK </span>]'
147 }
150 # Network interface status
152 interface_status() {
153 ip="----"
154 status="----"
155 scan=""
156 if ifconfig | grep -A1 $1 | grep -q inet; then
157 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
158 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
159 scan="<a data-icon=\"scan\" href='network.cgi?scan=$ip&amp;back=network.cgi'>$(_ 'Scan')</a>"
160 fi
161 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
162 status="$(_ 'connected')" &&
163 speed="$(cat /sys/class/net/$1/speed 2> /dev/null)" &&
164 [ "$speed" ] && status="$status ${speed}M"
165 echo "<td>$status</td><td>$ip</td><td>$scan</td>"
166 }
169 # Catch network interface (used in summary and network main page)
171 list_network_interfaces() {
172 cat <<EOT
173 <table class="wide zebra center">
174 <thead>
175 <tr>
176 <td>$(_ 'Interface')</td>
177 <td>$(_ 'Name')</td>
178 <td>$(_ 'Status')</td>
179 <td>$(_ 'IP Address')</td>
180 <td>$(_ 'Scan ports')</td>
181 </tr>
182 </thead>
183 <tbody>
184 EOT
185 for i in $(ls /sys/class/net); do
186 case $i in
187 eth*)
188 echo " <tr><td><a data-icon='eth' href='network.cgi?eth'>$i</a></td>
189 <td>Ethernet</td> $(interface_status $i)</tr>" ;;
190 wlan*|ath*|ra*)
191 echo " <tr><td><a data-icon='wifi' href='network.cgi?wifi'>$i</a></td>
192 <td>Wireless</td> $(interface_status $i)</tr>" ;;
193 ppp*)
194 echo " <tr><td><a data-icon='removable' href='ppp.cgi'>$i</a></td>
195 <td>Point to point</td> $(interface_status $i)</tr>" ;;
196 tap[0-9]*|tun[0-9]*)
197 echo " <tr><td><span data-icon='removable'>$i</span></td>
198 <td>User space</td> $(interface_status $i)</tr>" ;;
199 lo)
200 echo " <tr><td><span data-icon='loopback'>$i</span></td>
201 <td>Loopback</td> $(interface_status $i)</tr>" ;;
202 *)
203 continue ;;
204 esac
205 done
206 cat <<EOT
207 </tbody>
208 </table>
209 EOT
210 }
213 # Get the list of panel styles
215 list_styles() {
216 for style in $PANEL/styles/*; do
217 style=$(basename $style)
218 echo "<option value='$style'>$style</option>"
219 done
220 }
223 # Get the list of system locales
225 list_locales() {
226 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
227 do
228 echo "<option value='$locale'>$locale</option>"
229 done
230 }
233 # Get the list of console keymaps
235 list_keymaps() {
236 if [ -d /usr/share/kbd ]; then # kbd-base
237 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
238 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
239 else # kbd-busybox
240 ls /usr/share/kmap/*.kmap | \
241 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
242 fi | sort
243 }
246 #
247 # xHTML 5 (header and footer skel are from the style)
248 #
250 loading_msg() {
251 local MSG=${1:-$LOADING_MSG}
252 [ -z "$MSG" ] && MSG="$(_ 'Please wait')"
253 cat <<EOT
254 <script type="text/javascript">statusbar("$MSG", 1)</script>
255 EOT
256 }
259 xhtml_header() {
260 SUBTITLE="$1"
261 . ${PANEL}$HEADER
262 cat <<EOT
263 <script type="text/javascript">
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 }