tazpanel view lib/libtazpanel @ rev 303

3 weeks forging ;) Some bugs fixed, maybe all i18n improved (please, recheck 'po's!), couple of new features added... Enjoy!
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri May 04 13:00:43 2012 +0300 (2012-05-04)
parents 72e9955ebdff
children b3c93e4a8acc
line source
1 #!/bin/sh
2 #
3 # Common functions for TazPanel CGI and cmdline interface
4 #
6 # Get parameters with GET, POST and FILE functions
7 . /usr/bin/httpd_helper.sh
9 # I18n
10 . /etc/locale.conf
11 . /usr/bin/gettext.sh
12 TEXTDOMAIN='tazpanel'
13 export TEXTDOMAIN LANG LC_ALL
15 # We need a config file first
16 get_config() {
17 CONFIG="/etc/slitaz/tazpanel.conf"
18 [ -f data/tazpanel.conf ] && CONFIG="data/tazpanel.conf"
19 [ -f "$CONFIG" ] && . $CONFIG
20 [ ! -f "$PANEL/lib/libtazpanel" ] && \
21 echo "No config file or libtazpanel found: $CONFIG" && \
22 exit 1
23 }
25 # Display < > &
26 htmlize() {
27 sed -e 's|\&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g'
28 }
30 # Syntax highlighting for config file and SHell scripts
31 syntax_highlighter() {
32 case $1 in
33 conf)
34 htmlize | sed \
35 -e s"#^\#\([^']*\)#<span class='conf-comment'>\0</span>#"g \
36 -e s"#^[A-Z]\([^']*\)=#<span class='conf-var'>\0</span>#"g \
37 -e s"#^[a-z]\([^']*\)#<span class='conf-var'>\0</span>#"g \
38 -e s"#\"\([^']*\)\"#<span class='conf-val'>\0</span>#"g ;;
39 sh)
40 htmlize | sed \
41 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
42 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
43 diff)
44 htmlize | sed \
45 -e s"#^-\(.*\).#<span class='diff-rm'>\0</span>#"g \
46 -e s"#^+\(.*\).#<span class='diff-add'>\0</span>#"g \
47 -e s"#@@\(.*\)@@#<span class='diff-at'>@@\1@@</span>#"g ;;
48 activity)
49 sed -e s"#^\([^']*:\)#<span class='activity-log'>\0</span>#"g ;;
50 kernel)
51 htmlize | sed \
52 -e "s|\([^0-9]\)\(0x[0-9a-f]\+\)|\1<span class='kernel-hex'>\2</span>|g" \
53 -e "s|^\([^(,\[]\+: \)|<span class='kernel-id'>\0</span>|g" \
54 -e "s|\(\[[^ ]\+\]\)|<span class='kernel-id2'>\0</span>|g" ;;
56 esac
57 }
59 # Remove status and ESC char from tazpkg/tazlito commands output
60 filter_taztools_msgs() {
61 sed -e s'/\[^Gm]*.//g' \
62 -e ':a;s/^\(.\{1,68\}\)\(\[ [A-Za-z]* \]\)/\1 \2/;ta' \
63 -e 's#\[ OK \]#[ <span class="diff-add">OK</span> ]#' \
64 -e 's#\[ Failed \]#[ <span class="diff-rm">Failed</span> ]#'
65 }
67 # LOG activities
68 log() {
69 date=$(date "+%Y-%m-%d %H:%M")
70 filter_taztools_msgs | \
71 sed s"#[^']*#$date : \0#" >> $LOG_FILE
72 }
74 ok_status() {
75 echo "[<span class='diff-add'> OK </span>]"
76 }
78 # Network interface status
79 interface_status() {
80 if ifconfig | grep -A 1 $i | grep -q inet; then
81 ip=`ifconfig | grep -A 1 $i | grep inet | \
82 awk '{ print $2 }' | cut -d ":" -f 2`
83 echo "<td>$(gettext 'connected')</td><td>$ip</td>"
84 echo "<td><a href='/network.cgi?scan=$ip'>"
85 echo "<img src='$IMAGES/recharge.png' /></a></td>"
86 else
87 echo "<td>----</td><td>----</td><td></td>"
88 fi
89 }
91 # Catch network interface (used in summary and network main page)
92 list_network_interfaces() {
93 cat << EOT
94 <table class="zebra">
95 <thead>
96 <tr>
97 <td>$(gettext 'Interface')</td>
98 <td>$(gettext 'Name')</td>
99 <td>$(gettext 'Status')</td>
100 <td>$(gettext 'IP Address')</td>
101 <td>$(gettext 'Scan ports')</td>
102 </tr>
103 </thead>
104 <tbody>
105 EOT
106 for i in `ls /sys/class/net`
107 do
108 case $i in
109 eth*)
110 echo " <tr><td><a href='/network.cgi?eth'>
111 <img src='$IMAGES/ethernet.png' />$i</a></td>
112 <td>Ethernet</td> $(interface_status)</tr>" ;;
113 wlan*|ath*|ra*)
114 echo " <tr><td><a href='/network.cgi?wifi'>
115 <img src='$IMAGES/wireless.png' />$i</a></td>
116 <td>Wireless</td> $(interface_status)</tr>" ;;
117 lo)
118 echo " <tr><td><img src='$IMAGES/loopback.png' />$i</td>
119 <td>Loopback</td> $(interface_status)</tr>" ;;
120 *)
121 continue ;;
122 esac
123 done
124 cat << EOT
125 </tbody>
126 </table>
127 EOT
128 }
130 # Get the list of panel styles
131 list_styles() {
132 for style in $PANEL/styles/*
133 do
134 style=$(basename $style)
135 echo "<option value='$style'>$style</option>"
136 done
137 }
139 # Get the list of system locales
140 list_locales() {
141 for locale in $(find /usr/share/i18n/locales -type f -name "[a-z][a-z]_[A-Z][A-Z]")
142 do
143 echo "<option value='$locale'>$locale</option>"
144 done
145 }
147 # Get the list of console keymaps
148 list_keymaps() {
149 for keymap in /usr/share/kmap/*.kmap
150 do
151 basename $keymap .kmap | sed "s|.*|<option value='&'>&</option>|"
152 done
153 }
155 #
156 # xHTML 5 (header and footer skel are from the style)
157 #
159 loading_msg() {
160 cat << EOT
161 <script type="text/javascript">
162 document.write('<div id="loading"><img src="/styles/default/images/loader.gif" />$LOADING_MSG</div>');
163 </script>
164 EOT
165 }
167 xhtml_header() {
168 . ${PANEL}$HEADER
169 if [ $DEBUG == "1" ]; then
170 local i
171 local j
172 local x
173 args=""
174 for x in GET POST COOKIE ; do
175 for i in $($x) ; do
176 if [ $($x $i count) -gt 1 ]; then
177 for j in $(seq 1 $($x $i count)); do
178 args="$args $x($i,$j)='$($x $i $j)'"
179 done
180 else
181 args="$args $x($i)='$($x $i)'"
182 fi
183 done
184 done
185 for i in $(FILE); do
186 for j in name size type tmpname ; do
187 args="$args FILE($i,$j)=$(FILE $i $j)"
188 done
189 done
190 cat << EOT
191 <pre class='debug'>
192 QUERY_STRING="$QUERY_STRING"$args
193 </pre>
194 EOT
195 fi
196 }
198 xhtml_footer() {
199 . ${PANEL}$FOOTER
200 }
202 table_start() {
203 echo '<table>'
204 }
206 table_end() {
207 echo '</table>'
208 }
210 df_thead() {
211 cat << EOT
212 <thead>
213 <tr>
214 <td>$(gettext 'Disk')</td>
215 <td>$(gettext 'Label')</td>
216 <td>$(gettext 'Type')</td>
217 <td>$(gettext 'Size')</td>
218 <td>$(gettext 'Available')</td>
219 <td>$(gettext 'Used')</td>
220 <td>$(gettext 'Mount point')</td>
221 </tr>
222 </thead>
223 EOT
224 }