tazpanel view lib/libtazpanel @ rev 411

boot.cgi: display log files in tabs
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jan 18 18:59:01 2015 +0100 (2015-01-18)
parents aa22bfbb483c
children f3a7fb3d92ec
line source
1 #!/bin/sh
2 #
3 # Common functions for TazPanel CGI and cmdline interface
4 #
6 [ "$(id -un)" == "${REMOTE_USER:-root}" ] || exec su -c "$(realpath $0) $@" $REMOTE_USER
8 # Get parameters with GET, POST and FILE functions
9 . /usr/lib/slitaz/httphelper
11 # I18n
12 . /etc/locale.conf
13 . /usr/bin/gettext.sh
14 TEXTDOMAIN='tazpanel'
15 export TEXTDOMAIN LANG LC_ALL
17 # We need a config file first
18 get_config() {
19 CONFIG="/etc/slitaz/tazpanel.conf"
20 [ -f data/tazpanel.conf ] && CONFIG="data/tazpanel.conf"
21 [ -f "$CONFIG" ] && . $CONFIG
22 [ ! -f "$PANEL/lib/libtazpanel" ] && \
23 echo "No config file or libtazpanel found: $CONFIG" && \
24 exit 1
25 }
27 # Display < > &
28 htmlize() {
29 sed -e 's|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
30 }
32 # Syntax highlighting for config file and SHell scripts
33 syntax_highlighter() {
34 case $1 in
35 conf)
36 htmlize | sed \
37 -e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
38 -e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
39 -e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
40 -e s"#\"\([^']*\)\"#<span class='conf-val'>\0</span>#"g ;;
41 sh)
42 htmlize | sed \
43 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
44 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
45 diff)
46 htmlize | sed \
47 -e s"#^-\(.*\).#<span class='diff-rm'>\0</span>#"g \
48 -e s"#^+\(.*\).#<span class='diff-add'>\0</span>#"g \
49 -e s"#@@\(.*\)@@#<span class='diff-at'>@@\1@@</span>#"g ;;
50 activity)
51 sed -e s"#^\([^']*:\)#<span class='activity-log'>\0</span>#"g ;;
52 kernel)
53 htmlize | sed \
54 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
55 -e "s|^\([^(,\[]\+: \)|<span class='kernel-id'>\0</span>|g" \
56 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" ;;
57 lsusb)
58 htmlize | sed \
59 -e 's|^[^:]*:[ x0-9a-f^:]*$|<span class="lsusb-t">\0</span>|g' \
60 -e 's|^Bus.*$|<span class="lsusb-h">\0</span>|g' ;;
61 lspci)
62 htmlize | sed \
63 -e 's|^[0-9a-f].*$|<span class="lspci-t">\0</span>|g' \
64 -e 's|^ \([^:]*:\)| <span class="lspci-h">\1</span>|g' ;;
66 esac
67 }
69 # Remove status and ESC char from tazpkg/tazlito commands output
70 filter_taztools_msgs() {
71 sed -e s'|.\[1;32m|<span style="color: green">|g' \
72 -e s'|.\[1;31m|<span style="color: red">|g' \
73 -e s'|.\[1;33m|<span style="color: yellow">|g' \
74 -e s'|.\[1;34m|<span style="color: blue">|g' \
75 -e s'|.\[1;36m|<span style="color: lightblue">|g' \
76 -e s'|.\[0;39m|</span>|g' \
77 -e s'/\[^Gm]*.//g' \
78 -e ':a;s/^\(.\{1,68\}\)\(\[ *<span style="color\)/\1 \2/;ta'
79 }
81 # LOG activities
82 log() {
83 date=$(date "+%Y-%m-%d %H:%M")
84 filter_taztools_msgs | \
85 sed s"#[^']*#$date : \0#" >> $LOG_FILE
86 }
88 ok_status() {
89 echo "[<span class='diff-add'> OK </span>]"
90 }
92 # Network interface status
93 interface_status() {
94 if ifconfig | grep -A 1 $i | grep -q inet; then
95 ip=`ifconfig | grep -A 1 $i | grep inet | \
96 awk '{ print $2 }' | cut -d ":" -f 2`
97 echo "<td>$(gettext 'connected')</td><td>$ip</td>"
98 echo "<td><a href='/network.cgi?scan=$ip'>"
99 echo "<img src='$IMAGES/recharge.png' /></a></td>"
100 else
101 echo "<td>----</td><td>----</td><td></td>"
102 fi
103 }
105 # Catch network interface (used in summary and network main page)
106 list_network_interfaces() {
107 cat << EOT
108 <table class="zebra outbox">
109 <thead>
110 <tr>
111 <td>$(gettext 'Interface')</td>
112 <td>$(gettext 'Name')</td>
113 <td>$(gettext 'Status')</td>
114 <td>$(gettext 'IP Address')</td>
115 <td>$(gettext 'Scan ports')</td>
116 </tr>
117 </thead>
118 <tbody>
119 EOT
120 for i in `ls /sys/class/net`
121 do
122 case $i in
123 eth*)
124 echo " <tr><td><a href='/network.cgi?eth'>
125 <img src='$IMAGES/ethernet.png' />$i</a></td>
126 <td>Ethernet</td> $(interface_status)</tr>" ;;
127 wlan*|ath*|ra*)
128 echo " <tr><td><a href='/network.cgi?wifi'>
129 <img src='$IMAGES/wireless.png' />$i</a></td>
130 <td>Wireless</td> $(interface_status)</tr>" ;;
131 lo)
132 echo " <tr><td><img src='$IMAGES/loopback.png' />$i</td>
133 <td>Loopback</td> $(interface_status)</tr>" ;;
134 *)
135 continue ;;
136 esac
137 done
138 cat << EOT
139 </tbody>
140 </table>
141 EOT
142 }
144 # Get the list of panel styles
145 list_styles() {
146 for style in $PANEL/styles/*
147 do
148 style=$(basename $style)
149 echo "<option value='$style'>$style</option>"
150 done
151 }
153 # Get the list of system locales
154 list_locales() {
155 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
156 do
157 echo "<option value='$locale'>$locale</option>"
158 done
159 }
161 # Get the list of console keymaps
162 list_keymaps() {
163 for keymap in /usr/share/kmap/*.kmap
164 do
165 basename $keymap .kmap | sed "s|.*|<option value='&'>&</option>|"
166 done
167 }
169 #
170 # xHTML 5 (header and footer skel are from the style)
171 #
173 loading_msg() {
174 cat << EOT
175 <script type="text/javascript">
176 document.write('<div id="loading"><img src="/styles/default/images/loader.gif" />$LOADING_MSG</div>');
177 </script>
178 EOT
179 }
181 xhtml_header() {
182 . ${PANEL}$HEADER
183 if [ $DEBUG == "1" ]; then
184 local i
185 local j
186 local x
187 args=""
188 for x in GET POST COOKIE ; do
189 for i in $($x) ; do
190 if [ $($x $i count) -gt 1 ]; then
191 for j in $(seq 1 $($x $i count)); do
192 args="$args $x($i,$j)='$($x $i $j)'"
193 done
194 else
195 args="$args $x($i)='$($x $i)'"
196 fi
197 done
198 done
199 for i in $(FILE); do
200 for j in name size type tmpname ; do
201 args="$args FILE($i,$j)=$(FILE $i $j)"
202 done
203 done
204 cat << EOT
205 <pre class='debug'>
206 QUERY_STRING="$QUERY_STRING"$args
207 </pre>
208 EOT
209 fi
210 }
212 xhtml_footer() {
213 . ${PANEL}$FOOTER
214 }
216 table_start() {
217 echo '<table>'
218 }
220 table_end() {
221 echo '</table>'
222 }
224 df_thead() {
225 cat << EOT
226 <thead>
227 <tr>
228 <td>$(gettext 'Disk')</td>
229 <td>$(gettext 'Label')</td>
230 <td>$(gettext 'Type')</td>
231 <td>$(gettext 'Size')</td>
232 <td>$(gettext 'Available')</td>
233 <td>$(gettext 'Used')</td>
234 <td>$(gettext 'Mount point')</td>
235 <td>UUID</td>
236 </tr>
237 </thead>
238 EOT
239 }
241 msg() {
242 msgtype="$1"; shift
243 case "$msgtype" in
244 tip) MSG_ICON="$IMAGES/msg-tip.png" ;;
245 warn|warning) MSG_ICON="$IMAGES/msg-warn.png" ;;
246 err|error) MSG_ICON="$IMAGES/msg-err.png" ;;
247 up) MSG_ICON="$IMAGES/msg-up.png" ;;
248 *) MSG_ICON="$IMAGES/msg.png" ;;
249 esac
250 cat << EOT
251 <section class="box" style="width:50%;margin:0.5em auto;">
252 <image src="$MSG_ICON" alt="$msgtype" class="float-left" />
253 $@
254 </section>
255 EOT
256 }
258 is_installed() {
259 [ -d "$INSTALLED/$1" ]
260 }
262 blk2h() {
263 echo $1 | awk '{
264 n = $0/2
265 for (i = 1; n > 1024; i++)
266 n /= 1024
267 f = "%1.0f%c"
268 if (n < 100)
269 f = "%1.1f%c"
270 printf f,n,substr("KMGT", i, 1)
271 }'
272 }