tazpanel view lib/libtazpanel @ rev 591

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