tazpkg view tazpanel/pkgs.cgi @ rev 920

Tiny edits
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Aug 03 10:28:15 2016 +0300 (2016-08-03)
parents 4ebc99c81b41
children 50421cb50644
line source
1 #!/bin/sh
2 #
3 # TazPkg CGI interface - Manage packages via a browser
4 #
5 # This CGI interface extensively uses tazpkg to manage packages and have
6 # its own code for some tasks. Please KISS, it is important and keep speed
7 # in mind. Thanks, Pankso.
8 #
9 # (C) 2011-2015 SliTaz GNU/Linux - BSD License
10 #
12 . lib/libtazpanel
14 . /etc/slitaz/slitaz.conf
15 . /etc/slitaz/tazpkg.conf
16 export BLOCKED="$PKGS_DB/blocked-packages.list" TEXTDOMAIN='tazpkg' output=html
18 get_config
20 _() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; echo; }
21 _n() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; }
22 _p() {
23 local S="$1" P="$2" N="$3"; shift 3;
24 printf "$(ngettext "$S" "$P" "$N")" "$@"; }
27 #------
28 # menu
29 #------
31 case "$1" in
32 menu)
33 TEXTDOMAIN_original=$TEXTDOMAIN
34 export TEXTDOMAIN='tazpkg'
36 cat <<EOT
37 <li tabindex="0">
38 <span>$(gettext 'Packages')</span>
39 <menu>
40 <li><a data-icon="@info@" href="pkgs.cgi">$(gettext 'Summary')</a></li>
41 <li><a data-icon="@list@" href="pkgs.cgi?list&amp;my=my&amp;cat=all&amp;repo=Any">$(gettext 'My packages')</a></li>
42 <li><a data-icon="@refresh@" href="pkgs.cgi?recharge" data-root>$(gettext 'Recharge list')</a></li>
43 <li><a data-icon="@upgrade@" href="pkgs.cgi?up" data-root>$(gettext 'Check updates')</a></li>
44 <li><a data-icon="@admin@" href="pkgs.cgi?admin" data-root>$(gettext 'Administration')</a></li>
45 </menu>
46 </li>
47 EOT
48 export TEXTDOMAIN=$TEXTDOMAIN_original
49 exit
50 esac
54 #
55 # AJAX commands
56 #
58 case " $(GET) " in
61 *\ filelist\ * )
62 # Show installed files list
63 pkg=$(GET pkg)
64 cd "$PKGS_DB"
66 header
67 echo -n '<pre>'
68 if [ -f "$INSTALLED/$pkg/files.list" ]; then
69 files=$(wc -l < "$INSTALLED/$pkg/files.list")
70 sort "$INSTALLED/$pkg/files.list"
71 echo -n '</pre><footer>'
72 _p '%s file' '%s files' "$files" "$files"
73 echo '</footer>'
74 else
75 lzcat files.list.lzma undigest/*/files.list.lzma 2>/dev/null | \
76 awk -vp="$pkg:" '$1==p{print $2}' | sort
77 echo '</pre>'
78 fi
79 exit 0 ;;
82 *\ status\ * )
83 # Show package status
85 # Space at end is flag -> do not check equivalents
86 pkg=$(GET pkg | tr -d ' ')
87 orig_pkg=''
88 data_icon="@pkgi@"
89 responce='i'
91 if ! grep -q "^$pkg"$'\t' "$PKGS_DB/installed.info"; then
92 # Package not installed
93 data_icon="@pkg@"; responce='n'
94 equivs=$(grep "^$pkg=" "$PKGS_DB/packages.equiv")
95 if [ "$(GET pkg)" == "$pkg" -a -n "$equivs" ]; then
96 # Check equivalent packages
97 for equiv in ${equivs#*=}; do
98 case $equiv in
99 *:*)
100 if grep -q "^${equiv%:*}"$'\t' "$PKGS_DB/installed.info" &&
101 grep -q "^${equiv#*:}"$'\t' "$PKGS_DB/installed.info"; then
102 # Equivalent installed
103 orig_pkg="$pkg→"; pkg="${equiv#*:}"
104 data_icon="@pkgi@"; responce='i'; break
105 fi;;
106 *)
107 if grep -q "^$equiv"$'\t' "$PKGS_DB/installed.info"; then
108 # Equivalent installed
109 orig_pkg="$pkg→"; pkg="$equiv"
110 data_icon="@pkgi@"; responce='i'; break
111 fi;;
112 esac
113 done
114 fi
115 fi
117 # Installed and blocked?
118 [ "$data_icon" == "@pkgi@" ] && grep -q "^$pkg$" "$BLOCKED" && data_icon="@pkgib@"
120 if [ $(GET web) == 'y' ]; then
121 # Request from page http://pkgs.slitaz.org/ for example:
122 # http://127.0.0.1:82/pkgs.cgi?status&web=y&pkg=nano
123 # Allow http://pkgs.slitaz.org/ to get information from tazpanel server
124 header "Access-Control-Allow-Origin: http://pkgs.slitaz.org"
125 echo -n "$responce"
126 else
127 # Local request: don't check CORS (fails for cross domain requests unconditionally)
128 header
129 echo -n "<a data-icon=\"$data_icon\" href=\"?info=${pkg//+/%2B}\">$orig_pkg$pkg</a>"
130 fi
131 exit 0
132 ;;
135 *\ app_img\ * )
136 # Show application image
137 pkg=$(GET app_img)
139 # check for icons defined with packages.icons file
140 if [ -f "$PKGS_DB/packages.icons" ]; then
141 predefined_icon="$(awk -F$'\t' -vpkg="$pkg" '$1==pkg{print $2}' "$PKGS_DB/packages.icons")"
142 fi
143 predefined_icon="${predefined_icon:-package-x-generic}.png"
145 current_user=$(id -un)
146 if [ -n "$current_user" ]; then
147 current_user_home=$(awk -F: -vu="$current_user" '$1==u{print $6}' /etc/passwd)
148 current_icon_theme=$(grep gtk-icon-theme-name "$current_user_home/.gtkrc-2.0" | cut -d'"' -f2)
149 fi
150 current_icon_theme="/usr/share/icons/$current_icon_theme"
152 # Preferred default icon is 48px package-x-generic
153 default_pkg_icon=$(find -L "$current_icon_theme" -type f -path '*48*' -name "$predefined_icon" | head -n1)
154 # ... or package-x-generic with the bigger size
155 if [ -z "$default_pkg_icon" ]; then
156 default_pkg_icon=$(find -L "$current_icon_theme" -type f -name "$predefined_icon" | sort | tail -n1)
157 fi
159 # Preferred package icon size is 48px
160 pkg_icon=$(find -L "$current_icon_theme" -type f -path '*48*' -name "$pkg.png" | head -n1)
161 # ... or just bigger one
162 if [ -z "$pkg_icon" ]; then
163 pkg_icon=$(find -L "$current_icon_theme" -type f -name "$pkg.png" | sort | tail -n1)
164 fi
165 # ... or one from pixmaps
166 if [ -z "$pkg_icon" ]; then
167 pkg_icon=$(find -L /usr/share/pixmaps -type f -name "$pkg.png" | head -n1)
168 fi
170 # Cache app icon maximum for 24h (note Cache-Control was introduced in HTTP 1.1)
171 header "HTTP/1.1 200 OK" "Content-Type: image/png" "Cache-Control: public, max-age=86400"
172 cat "${pkg_icon:-$default_pkg_icon}"
173 exit 0 ;;
176 *\ show_receipt\ * )
177 # Show package receipt
178 pkg=$(GET show_receipt)
179 if [ -f "$INSTALLED/$pkg/receipt" ]; then
180 # Redirects to the receipt view
181 header "HTTP/1.1 301 Moved Permanently" "Location: index.cgi?file=$INSTALLED/$pkg/receipt"
182 exit 0
183 else
184 temp_receipt=$(mktemp -d)
185 wget -O "$temp_receipt/receipt" -T 5 "http://hg.slitaz.org/wok/raw-file/tip/$pkg/receipt"
186 if [ -e "$temp_receipt" ]; then
187 # Redirects to the receipt view
188 header "HTTP/1.1 301 Moved Permanently" "Location: index.cgi?file=$temp_receipt/receipt"
189 exit 0
190 else
191 header; xhtml_header
192 msg err "$(_ 'Receipt for package %s unavailable' "$pkg")"
193 xhtml_footer
194 exit 0
195 fi
196 fi
197 ;;
199 esac
202 header
205 # xHTML 5 header with special side bar for categories.
206 TITLE=$(TEXTDOMAIN='tazpkg'; _ 'Packages')
209 pkg_info_link() {
210 echo "<a data-icon=\"$2\" href=\"?info=${1//+/%2B}\">$1</a>" | sed 's| data-icon=""||'
211 }
214 # Display localized short description
216 i18n_desc() {
217 for L in $LANG ${LANG%%_*}; do
218 if [ -e "$PKGS_DB/packages-desc.$L" ]; then
219 LOCDESC=$(awk -F$'\t' -vp=$1 '{if ($1 == p) print $2}' $PKGS_DB/packages-desc.$L)
220 if [ -n "$LOCDESC" ]; then
221 SHORT_DESC="$LOCDESC"
222 break
223 fi
224 fi
225 done
226 }
229 select_package_icon() {
230 if [ -d "$INSTALLED/$1" ]; then
231 if grep -q "^$1$" "$BLOCKED"
232 then echo "@pkgib@"
233 else echo "@pkgi@"
234 fi
235 else echo "@pkg@"
236 fi
237 }
240 # We need packages information for list and search
242 parse_extra() {
243 IFS='|'
244 while read PACKAGE SHORT_DESC WEB_SITE CATEGORY VERSION LICENSE; do
245 i18n_desc "$PACKAGE"
246 cat <<EOT
247 <tr>
248 <td><input type="checkbox" name="pkg" value="$PACKAGE">$(pkg_info_link "$PACKAGE" "$(select_package_icon "$PACKAGE")")</td>
249 <td>$VERSION</td>
250 <td>$SHORT_DESC</td>
251 </tr>
252 EOT
253 done
254 unset IFS
255 }
258 parse_packages_info() {
259 IFS=$'\t'
260 while read PACKAGE VERSION CATEGORY SHORT_DESC WEB_SITE TAGS SIZES DEPENDS; do
261 i18n_desc "$PACKAGE"
262 cat <<EOT
263 <tr>
264 <td><input type="checkbox" name="pkg" value="$PACKAGE">$(pkg_info_link "$PACKAGE" "$(select_package_icon "$PACKAGE")")</td>
265 <td>$VERSION</td>
266 <td>$SHORT_DESC</td>
267 </tr>
268 EOT
269 done
270 unset IFS
271 }
274 # Show button
276 show_button() {