tazpanel view lib/libtazpanel @ rev 581

libtazpanel: s/noboby/root/
"fixes" user-switch on any link clicked if panel launched from new tazpkg notifier or su -c tazpanel
env problems?
------------------
boot.cgi: do not find in /run/media/username/disks
author Xander Ziiryanoff <psychomaniak@xakep.ru>
date Fri Apr 08 18:09:43 2016 +0200 (2016-04-08)
parents 6d2f77151dfa
children c8226a527542
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")" "$@"; }
17 GUI_USER=root
18 [ "${HTTP_HOST/:*/:}" == "127.0.0.1:" ] &&
19 for i in "$HTTP_COOKIE" "$QUERY_STRING" ; do
20 v="$(echo "$i" | sed '/guiuser=/!d;s/.*guiuser=\([A-Za-z0-9_]*\).*/\1/')"
21 [ "$v" ] || continue
22 busybox ps | sed '/d;/d;/127\.0\.0\.1:/!d' | grep -q " $v " || continue
23 GUI_USER="$v"
24 done
25 [ "$(id -un)" == "${REMOTE_USER:=$GUI_USER}" ] || exec su -s /bin/sh -c "$(realpath $0) $@" $REMOTE_USER
28 # Get parameters with GET, POST, COOKIE and FILE functions
30 . /usr/lib/slitaz/httphelper
33 # I18n
35 . /etc/locale.conf
36 export TEXTDOMAIN='tazpanel' LANG LC_ALL
39 # We need a config file first
41 get_config() {
42 CONFIG='/etc/slitaz/tazpanel.conf'
43 if [ -f "$CONFIG" ]; then
44 . $CONFIG
45 else
46 echo "No config file found: $CONFIG"
47 exit 1
48 fi
49 }
52 # Display < > &
54 htmlize() {
55 sed -e '/<[a-z]*>/{:a;Nba;} s|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
56 }
59 # Syntax highlighting for config file and SHell scripts
61 syntax_highlighter() {
62 case $1 in
63 conf)
64 htmlize | sed \
65 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="conf-var">\1</span><span class="conf-val">\2</span>|g' \
66 -e 's|^[ \t]*#.*|<span class="conf-comment">\0</span>|g' ;;
67 #-e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
68 #-e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
69 #-e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
70 #-e s"#[\"']\([^']*\)[\"']#<span class='conf-val'>\0</span>#"g ;;
71 sh)
72 htmlize | sed \
73 -e 's|^\([ \t]*[A-Za-z0-9_][A-Za-z0-9_]*\)\(="*.*\)|<span class="sh-var">\1</span><span class="sh-val">\2</span>|g' \
74 -e 's|^#.*|<span class="sh-comment">\0</span>|g' ;;
75 #-e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
76 #-e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
77 diff)
78 htmlize | sed \
79 -e 's|^-.*|<span class="diff-rm">\0</span>|g' \
80 -e 's|^+.*|<span class="diff-add">\0</span>|g' \
81 -e 's|^@.*|<span class="diff-at">\0</span>|g' ;;
82 activity)
83 # realize lazy quantification
84 sed -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='activity-log'>\1:</span> |" ;;
85 kernel)
86 # line with "ⓒ": realize lazy quantification
87 htmlize | sed \
88 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
89 -e "s|: |ⓒ|; s|^\(.*\)ⓒ|<span class='kernel-id'>\1:</span> |" \
90 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" \
91 -e "s|Call Trace:|<span class='kernel-trace'>\0</span>|" ;;
92 lsusb)
93 htmlize | sed \
94 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
95 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
96 lspci)
97 htmlize | sed \
98 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
99 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
100 xlog)
101 htmlize | sed \
102 -e 's|^[^]]*]|<span class="xlog xlog-timestamp">\0</span>|' \
103 -e 's|(--)|<span class="xlog xlog-probed" title="probed">\0</span>|' \
104 -e 's|(\*\*)|<span class="xlog xlog-config" title="from config file">\0</span>|' \
105 -e 's|(==)|<span class="xlog xlog-default" title="default setting">\0</span>|' \
106 -e 's|(++)|<span class="xlog xlog-cmdline" title="from command line">\0</span>|' \
107 -e 's|(!!)|<span class="xlog xlog-notice" title="notice">\0</span>|' \
108 -e 's|(II)|<span class="xlog xlog-info" title="informational">\0</span>|' \
109 -e 's|(WW)|<span class="xlog xlog-warn" title="warning">\0</span>|' \
110 -e 's|(EE)|<span class="xlog xlog-error" title="error">\0</span>|' \
111 -e 's|(NI)|<span class="xlog xlog-ni" title="not implemented">\0</span>|' \
112 -e 's|(??)|<span class="xlog xlog-unknown" title="unknown">\0</span>|' \
113 ;;
114 esac
115 }
118 # Remove status and ESC char from tazpkg/tazlito commands output
120 filter_taztools_msgs() {
121 sed \
122 -e 's|\\[0m|</span>|g' \
123 -e 's|\\[0;39m|</span>|g' \
124 -e 's|\\[0*\([0-7]*\);\([0-7]*\)m|<span class="color\1 color\2">|g' \
125 -e ':a;s/^\(.\{0,69\}\)\(\\[[0-9]*G\[\)/\1 \2/;ta' \
126 -e ':b;s|^\([^\]*\)\\[\([0-9]*\)G|<span style="display:inline-block;width:\2ex">\1</span>|;tb' \
127 -e 's/\[^Gm]*.//g'
128 }
131 # LOG activities
133 log() {
134 date=$(date "+%F %R")
135 filter_taztools_msgs | sed "s|[^']*|$date : \0|" >> $LOG_FILE
136 }
139 ok_status() {
140 echo '[<span class="diff-add"> OK </span>]'
141 }
144 # Network interface status
146 interface_status() {
147 ip="----"
148 status="----"
149 scan=""
150 if ifconfig | grep -A1 $1 | grep -q inet; then
151 ip=$(ifconfig | grep -A1 $1 | grep inet | awk '{ print $2 }' | cut -d: -f2)
152 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
153 scan='<a data-icon="@scan@" href="network.cgi?scan='$ip'&amp;back=network.cgi">'$(_ 'Scan')'</a>'
154 fi
155 [ "$(cat /sys/class/net/$1/carrier)" == "1" ] &&
156 status="$(_ 'connected')" &&
157 speed="$(cat /sys/class/net/$1/speed 2> /dev/null)" &&
158 [ "$speed" ] && status="$status ${speed}M"
159 echo "<td>$status</td><td>$ip</td><td>$scan</td>"
160 }
163 # Catch network interface (used in summary and network main page)
165 list_network_interfaces() {
166 cat <<EOT
167 <table class="wide zebra center">
168 <thead>
169 <tr>
170 <td>$(_ 'Interface')</td>
171 <td>$(_ 'Name')</td>
172 <td>$(_ 'Status')</td>
173 <td>$(_ 'IP Address')</td>
174 <td>$(_ 'Scan ports')</td>
175 </tr>
176 </thead>
177 <tbody>
178 EOT
179 for i in $(ls /sys/class/net); do
180 case $i in
181 eth*)
182 echo '<tr><td><a data-icon="@eth@" href="network.cgi?eth">'$i'</a></td>
183 <td>Ethernet</td> '$(interface_status $i)'</tr>';;
184 wlan*|ath*|ra*)
185 echo '<tr><td><a data-icon="@wifi@" href="network.cgi?wifi">'$i'</a></td>
186 <td>Wireless</td> '$(interface_status $i)'</tr>';;
187 ppp*)
188 echo '<tr><td><a data-icon="@removable@" href="ppp.cgi">'$i'</a></td>
189 <td>Point to point</td> '$(interface_status $i)'</tr>';;
190 tap[0-9]*|tun[0-9]*)
191 echo '<tr><td><span data-icon="@removable@">'$i'</span></td>
192 <td>User space</td> '$(interface_status $i)'</tr>';;
193 lo)
194 echo '<tr><td><span data-icon="@loopback@">'$i'</span></td>
195 <td>Loopback</td> '$(interface_status $i)'</tr>';;
196 *)
197 continue ;;
198 esac
199 done
200 cat <<EOT
201 </tbody>
202 </table>
203 EOT
204 }
207 # Get the list of panel styles
209 list_styles() {
210 for style in $PANEL/styles/*; do
211 style=$(basename $style)
212 echo "<option value='$style'>$style</option>"
213 done
214 }
217 # Get the list of system locales
219 list_locales() {
220 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
221 do
222 echo "<option value=\"$locale\">$locale</option>"
223 done
224 }
227 # Get the list of console keymaps
229 list_keymaps() {
230 if [ -d /usr/share/kbd ]; then
231 # kbd-base
232 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
233 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
234 else
235 # kbd-busybox
236 ls /usr/share/kmap/*.kmap | \
237 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
238 fi | sort
239 }
242 #
243 # xHTML 5 (header and footer skel are from the style)
244 #
246 loading_msg() {
247 [ -n "$(GET noheader)" ] && return
248 local MSG=${1:-$LOADING_MSG}
249 [ -z "$MSG" ] && MSG="$(_ 'Please wait')"
250 cat <<EOT
251 <script type="text/javascript">statusbar("$MSG", 1)</script>
252 EOT
253 }
256 xhtml_header() {
257 [ -n "$(GET noheader)" ] && return
259 local subtitle="$1" header="/var/cache/tazpanel/header.$(id -un)"
261 # Cache header
262 if [ ! -f "$header" ]; then
263 . ${PANEL}$HEADER > "$header"
264 cat >> "$header" <<EOT
265 <script type="text/javascript">
266 document.cookie = "guiuser=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
267 document.cookie = "guiuser=$GUI_USER";
268 </script>
269 EOT
270 fi
271 sed "s|@@TITLE@@|$TITLE|; s|@@SUBTITLE@@|$subtitle|" "$header"
273 # Show debug output
274 if [ "$DEBUG" -eq 1 ]; then
275 local i j x
276 args=''
277 for x in GET POST COOKIE; do
278 for i in $($x); do
279 if [ $($x $i count) -gt 1 ]; then
280 for j in $(seq 1 $($x $i count)); do
281 args="$args $x($i,$j)='$($x $i $j)'"
282 done
283 else
284 args="$args $x($i)='$($x $i)'"
285 fi
286 done
287 done
288 for i in $(FILE); do
289 for j in name size type tmpname; do
290 args="$args FILE($i,$j)=$(FILE $i $j)"
291 done
292 done
293 cat <<EOT
294 <pre class="debug">
295 QUERY_STRING="$QUERY_STRING"$args
296 </pre>
297 EOT
298 fi
299 }
302 xhtml_footer() {
303 [ -n "$(GET noheader)" ] && return
305 export TEXTDOMAIN='tazpkg'
306 . ${PANEL}$FOOTER
307 }
310 table_start() { echo '<table>'; }
311 table_end() { echo '</table>'; }
314 df_thead() {
315 cat <<EOT
316 <thead>
317 <tr>
318 <td>$(_ 'Disk')</td>
319 <td>$(_ 'Label')</td>
320 <td>$(_ 'Type')</td>
321 <td>$(_ 'Size')</td>
322 <td>$(_ 'Available')</td>
323 <td>$(_ 'Used')</td>
324 <td>$(_ 'Mount point')</td>
325 <td>UUID</td>
326 </tr>
327 </thead>
328 EOT
329 }
332 msg() {
333 msgtype="$1"; shift
334 case "$msgtype" in
335 tip) I='@msgtip@';;
336 warn|warning) I='@msgwarn@';;
337 err|error) I='@msgerr@';;
338 up) I='@msgup@';;
339 *) I='@msg@';;
340 esac
341 echo "<section class=\"box\"><span data-icon=\"$I\"></span>$@</section>"
342 }
345 is_installed() {
346 [ -d "$INSTALLED/$1" ]
347 }
350 blk2h() {
351 if [ "$1" ]; then
352 echo $1
353 else
354 cat
355 fi | awk '{
356 n = $0/2
357 for (i = 1; n > 1024; i++)
358 n /= 1024
359 f = "%1.0f%c"
360 if (n < 100)
361 f = "%1.1f%c"
362 printf f,n,substr("KMGT", i, 1)
363 }'
364 }
367 # Show "Back" button
369 back_button() {
370 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
371 "${1:-javascript:history.go(-1)}" "${3:-@back@}" "${2:-$(_ 'Back')}"
372 }
375 # Show "Edit" button
377 edit_button() {
378 [ -w "$1" ] && cat <<EOT
379 <form action="index.cgi">
380 <input type="hidden" name="file" value="$1"/>
381 <button name="action" value="edit" data-icon="@edit@">${2:-$(_ 'Edit')}</button>
382 </form>
383 EOT
384 }
386 # Mark select option as checked
388 selected() {
389 [ "$1" == "$2" ] && echo -n 'selected'
390 }
393 check_root_tazpanel() {
394 if [ $(id -u) -ne 0 ]; then
395 msg err "$(_ 'You must be root to show this page.')"
396 xhtml_footer; exit 0
397 fi
398 }
400 # Get system database. NSS compatible.
402 getdb() {
403 if [ -n "$(which getent)" ]; then
404 getent "$1"
405 else
406 cat "/etc/$1" | sed '/^#/d'
407 fi
408 }
410 # The html 'file' object does not return the full path. Ajax helps on localhost.
412 file_chooser() {
413 # 1: <input> name; 2: [predefined value]; 3: [button icon]; 4: [MIME type]
414 id="input$RANDOM"
415 cat <<EOT
416 <span id="$id"><input type="text" name="$1" ${2:+value="$2" }/></span>\
417 <button data-icon="${3:-@conf@}" onclick="ajax('index.cgi?do=file-selection&amp;name=$1&amp;type=$4', '1', '$id'); return false">\
418 $(_n 'Browse')</button>
419 EOT
420 }
422 dir_chooser() {
423 # 1: <input> name; 2: [predefined value]; 3: [button icon]
424 id="input$RANDOM"
425 cat <<EOT
426 <span id="$id"><input type="text" name="$1" ${2:+value="$2" }/></span>\
427 <button data-icon="${3:-@folder@}" onclick="ajax('index.cgi?do=dir-selection&amp;name=$1', '1', '$id'); return false">\
428 $(_n 'Browse')</button>
429 EOT
430 }