tazpanel view lib/libtazpanel @ rev 463

Add prism.js syntax highlighter; now files can be edited "in place" without page reloading; allow ANY characters in the Wi-Fi password (bug 126); change web-app layout: main window isn't scrollable, with scrollable contents.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Apr 24 16:00:14 2015 +0300 (2015-04-24)
parents 57d6b49df2a2
children b9e3c8ad454b
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 lo)
182 echo " <tr><td><span data-icon='loopback'>$i</span></td>
183 <td>Loopback</td> $(interface_status $i)</tr>" ;;
184 *)
185 continue ;;
186 esac
187 done
188 cat <<EOT
189 </tbody>
190 </table>
191 EOT
192 }
195 # Get the list of panel styles
197 list_styles() {
198 for style in $PANEL/styles/*
199 do
200 style=$(basename $style)
201 echo "<option value='$style'>$style</option>"
202 done
203 }
206 # Get the list of system locales
208 list_locales() {
209 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
210 do
211 echo "<option value='$locale'>$locale</option>"
212 done
213 }
216 # Get the list of console keymaps
218 list_keymaps() {
219 if [ -d /usr/share/kbd ]; then # kbd-base
220 find /usr/share/kbd/keymaps/i386 -type f -name '*.map*' ! -path '*include*' | \
221 awk -F/ '{gsub(/.map.gz/, ""); printf "<option value=\"%s\">%s (%s)\n", $8, $8, $7}'
222 else # kbd-busybox
223 ls /usr/share/kmap/*.kmap | \
224 sed 's|.*kmap/\(.*\).kmap|<option value="\1">\1|'
225 fi | sort
226 }
229 #
230 # xHTML 5 (header and footer skel are from the style)
231 #
233 loading_msg() {
234 local MSG=${1:-$LOADING_MSG}
235 cat <<EOT
236 <div id="loading"><img src="/styles/default/images/loader.gif"/>$MSG</div>
237 EOT
238 }
241 xhtml_header() {
242 . ${PANEL}$HEADER
243 if [ $DEBUG == "1" ]; then
244 local i
245 local j
246 local x
247 args=""
248 for x in GET POST COOKIE; do
249 for i in $($x); do
250 if [ $($x $i count) -gt 1 ]; then
251 for j in $(seq 1 $($x $i count)); do
252 args="$args $x($i,$j)='$($x $i $j)'"
253 done
254 else
255 args="$args $x($i)='$($x $i)'"
256 fi
257 done
258 done
259 for i in $(FILE); do
260 for j in name size type tmpname; do
261 args="$args FILE($i,$j)=$(FILE $i $j)"
262 done
263 done
264 cat <<EOT
265 <pre class="debug">
266 QUERY_STRING="$QUERY_STRING"$args
267 </pre>
268 EOT
269 fi
270 }
273 xhtml_footer() {
274 . ${PANEL}$FOOTER
275 }
278 table_start() {
279 echo '<table>'
280 }
283 table_end() {
284 echo '</table>'
285 }
288 df_thead() {
289 cat <<EOT
290 <thead>
291 <tr>
292 <td>$(_ 'Disk')</td>
293 <td>$(_ 'Label')</td>
294 <td>$(_ 'Type')</td>
295 <td>$(_ 'Size')</td>
296 <td>$(_ 'Available')</td>
297 <td>$(_ 'Used')</td>
298 <td>$(_ 'Mount point')</td>
299 <td>UUID</td>
300 </tr>
301 </thead>
302 EOT
303 }
306 msg() {
307 msgtype="$1"; shift
308 case "$msgtype" in
309 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
310 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
311 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
312 up) MSG_ICON="$IMAGES/msg-up.png" ;;
313 *) MSG_ICON="$IMAGES/msg.png" ;;
314 esac
315 cat <<EOT
316 <section class="box"><img src="$MSG_ICON" alt="$msgtype"/>
317 $@
318 </section>
319 EOT
320 }
323 is_installed() {
324 [ -d "$INSTALLED/$1" ]
325 }
328 blk2h() {
329 if [ "$1" ]; then
330 echo $1
331 else
332 cat
333 fi | awk '{
334 n = $0/2
335 for (i = 1; n > 1024; i++)
336 n /= 1024
337 f = "%1.0f%c"
338 if (n < 100)
339 f = "%1.1f%c"
340 printf f,n,substr("KMGT", i, 1)
341 }'
342 }
345 # Show "Back" button
347 back_button() {
348 local URL="$1" default_caption="$(_ 'Back')"
349 local caption="${2:-$default_caption}" icon="${3:-back}"
351 if [ -n "$1" ]; then
352 printf '<form action="%s" method="post"><button data-icon="%s">%s</button></form>' \
353 "$URL" "$icon" "$caption"
354 fi
355 }
358 # Mark select option as checked
360 selected() {
361 [ "$1" == "$2" ] && echo -n 'selected'
362 }
365 check_root_tazpanel() {
366 if [ $(id -u) != '0' ]; then
367 msg err "$(_ 'You must be root to show this page.')"
368 xhtml_footer; exit 0
369 fi
370 }