sup view server/sup/sup.cgi @ rev 42

Improvment and fixes after some usage/testing
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 08 00:15:04 2017 +0100 (2017-03-08)
parents 642f0fa9f0ff
children 28af8244bf8e
line source
1 #!/bin/sh
2 #
3 # TinyCM/TazBug Plugin - SliTaz User Packages
4 #
5 # This plugin is part of the SPI tools and services. More information
6 # at http://hg.slitaz.org/sup/file/ && http://scn.slitaz.org/?d=en/sup
7 # packages.sql is a SQLite table with all mirrored packages info, just
8 # for the fun of playing with SQLite :-)
9 #
10 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
11 # Author: Christophe Lincoln <pankso@slitaz.org>
12 #
14 # This script is called directly on upload: get httphelper.sh functions
15 # and set full path to move files
16 if [ "$(basename $0)" == "sup.cgi" ]; then
17 . /lib/libtaz.sh
18 . /usr/lib/slitaz/httphelper.sh
19 . paths.conf
20 fi
22 # Custom paths.conf for easier development avoid commit this file
23 # with wrong paths like Pankso does! Full paths are needed for upload
24 # to work properly.
25 [ -f "${plugins}/sup/paths.conf" ] && . ${plugins}/sup/paths.conf
27 supdb="$content/sup"
28 wok="$supdb/wok"
29 packages="$supdb/packages"
30 suplog="$cache/log/sup.log"
31 pkgsdb="$packages/packages.sql"
33 # Keep the highlighter minimal :-)
34 receip_highlighter() {
35 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
36 -e s"#^\#\([^']*\)#<span class='comment'>\0</span>#"g \
37 -e s"#\"\([^']*\)\"#<span class='value'>\0</span>#"g
38 }
40 case " $(GET sup) " in
42 *\ pkg\ *)
43 pkg="$(GET name)"
44 d="SUP: $pkg"
45 header
46 html_header
47 user_box
48 if ! . ${wok}/${pkg}/receip; then
49 echo "Missing or corrupted receip" && exit 1
50 fi
52 cat << EOT
53 <h2>$(gettext "Package:") $PACKAGE $VERSION</h2>
54 <div id="tools">
55 <a title="Right click to download link: $PACKAGE-$VERSION.sup"
56 href='$packages/$PACKAGE-$VERSION.sup'>Download</a>
57 EOT
58 # Tools for logged users
59 if check_auth; then
60 # Only package owner can update a package MAIL is set by check_auth
61 if [ "$MAINTAINER" == "$MAIL" ]; then
62 cat << EOT
63 <a href="?sup=upload">$(gettext "Update package")</a>
64 EOT
65 fi
66 fi
67 cat << EOT
68 <a href='?sup'>SUP Hub</a>
69 </div>
70 <pre>
71 ${SHORT_DESC}
72 </pre>
74 EOT
75 # Get package maintainer/user info from SCN/SliTaz user DB
76 if ! . $(fgrep -l "MAIL=\"${MAINTAINER}\"" ${PEOPLE}/*/account.conf); then
77 USER="unknow"
78 fi
79 cat << EOT
80 <div>
81 <a href="?user=$USER">$(get_gravatar $MAINTAINER 24)</a>
82 $(gettext "Maintainer:") <a href="?user=$USER">$NAME</a> -
83 $(gettext "Build date:") ${cook_date} -
84 $(gettext "License:") $LICENSE
85 </div>
86 EOT
87 # README
88 if [ -f "${wok}/${pkg}/README" ]; then
89 echo "<h3>README</h3>"
90 echo "<pre>"
91 cat ${wok}/${pkg}/README
92 echo "</pre>"
93 fi
94 # Receip
95 cat << EOT
96 <h3>$(gettext "Receip")</h3>
97 <pre>
98 $(cat ${wok}/${pkg}/receip | receip_highlighter)
99 </pre>
100 EOT
101 html_footer && exit 0 ;;
103 *\ upload\ *)
104 # HTML Form: use cloud plugin CSS style
105 header
106 html_header
107 user_box
108 if ! check_auth; then
109 header "Location: $HTTP_REFERER"
110 fi
111 d="SUP Upload"
112 cat << EOT
113 <h2>${d}</h2>
114 <p>
115 $(gettext "User:") <a href="?user=$user">$user</a> - Mail: $MAIL
116 </p>
118 <div id="cloud-upload">
120 <form method="post"
121 action="plugins/sup/sup.cgi?sup=supQA&amp;user=$user&amp;mail=$MAIL"
122 enctype="multipart/form-data">
123 <input type="file" name="supfile" size="150" />
124 <input type="submit" value="Upload" />
125 </form>
127 </div>
128 EOT
129 html_footer && exit 0 ;;
131 *\ supQA\ *)
132 # SUP Upload Quality assurance. Check package in cache before
133 # publishing. We need full path for upload to work ../../
134 #
135 # This is the geeky part for users, QA output in cmdline style
136 # and to be transparent on what is going on :-)
137 #
138 header
139 d="SUP Upload QA"
140 supfile=$(FILE supfile name)
141 tmpfile=$(FILE supfile tmpname)
142 wok="$scn/content/sup/wok"
143 packages="$scn/content/sup/packages"
144 cache="$scn/cache/sup/${supfile%.sup}"
145 pkgsdb="$scn/content/sup/packages/packages.sql"
146 suplog="$scn/cache/log/sup.log"
147 error=0
149 # clean_error "Message"
150 clean_error() {
151 echo "<span class='error'>ERROR: ${1}</span>"
152 [ -d "$cache" ] && rm -rf ${cache}
153 [ -d "$(dirname $tmpfile)" ] && rm -rf $(dirname $tmpfile)
154 }
156 # Use COOKIE to make sure user is logged in SCN/SUP Hub
157 user="$(echo $(COOKIE auth) | cut -d ':' -f 1)"
158 if [ "$(COOKIE auth)" ] && [ "$user" != "$(GET user)" ]; then
159 clean_error "user auth cookie"; exit 1
160 fi
162 # Is it a .sup file ?
163 if [ "$supfile" != "${supfile%.sup}.sup" ]; then
164 clean_error "not a .sup package: $supfile"; exit 1
165 fi
167 cat << EOT
168 <!DOCTYPE html>
169 <html lang="en">
170 <head>
171 <meta charset="utf-8" />
172 <title>$d</title>
173 <link rel="stylesheet" type="text/css" href="${host}style.css" />
174 <style type="text/css">body { margin: 40px 80px; }</style>
175 </head>
176 <body>
177 <h2>$d</h2>
178 <pre>
179 SUP Hub user : <a href="${host}?user=$user">$user</a>
180 Package file : ${supfile}
181 EOT
182 # Server sanity Check
183 if [ ! -f "$pkgsdb" ]; then
184 clean_error "missing : $(basename $pkgsdb)"
185 echo "</pre>" && exit 1
186 fi
188 mkdir -p ${cache}
189 if ! mv -f ${tmpfile} ${cache}/${supfile}; then
190 clean_error "moving: ${tmpfile} to ${supfile}"
191 echo "</pre>" && exit 1
192 fi
194 # Show MD5sum
195 echo -e "MD5sum : $(md5sum $cache/$supfile | cut -d ' ' -f 1)\n"
197 # Extract receip: sup cook already check for empty var. Make
198 # sure SCN user name match package MAINTAINER.
199 gettext "Extracting receip..."
200 cd ${cache}
201 if ! cpio -i receip --quiet < ${supfile}; then
202 echo ""
203 clean_error "Can't extract receip"
204 echo "</pre>" && exit 1
205 fi; status
207 if ! . receip; then
208 clean_error "Can't source receip"
209 echo "</pre>" && exit 1
210 fi
211 echo "Build date: <span class='float-right value'>$cook_date</span>"
213 # README
214 gettext "Checking for a README file..."
215 cpio -i README --quiet < ${supfile}
216 if [ -f "README" ]; then
217 echo " <span class='float-right color32'>$(gettext 'yes')</span>"
218 else
219 echo " <span class='float-right value'>$(gettext 'no')</span>"
220 fi
222 # Logged user is maintainer ?
223 if [ "$MAINTAINER" != "$(GET mail)" ]; then
224 error=1
225 echo -e "\n<span class='error'>WARNING: user mail not matching</span>"
226 gettext "User mail :"; echo " $(GET mail)"
227 gettext "MAINTAINER :"; echo " $MAINTAINER"
228 fi
230 # Publish and display pkg url's if no error
231 if [ "$error" == "0" ]; then
232 gettext "Moving package to mirror..."
233 mv -f ${supfile} ${packages}; status
234 gettext "Moving receip to public wok..."
235 mkdir -p ${wok}/${PACKAGE} && mv -f receip ${wok}/${PACKAGE}; status
236 [ -f "README" ] && mv -f README ${wok}/${PACKAGE}
238 # Handle packages.md5
241 # Log activity date|user|mail|pkg|version|short_desc
242 cat >> ${suplog} << EOT
243 $(date '+%Y-%m-%d %H:%M')|$user|$MAINTAINER|$PACKAGE|$VERSION|$SHORT_DESC
244 EOT
245 echo "</pre><p>
246 Package page: <a href='${host}?sup=pkg&amp;name=$PACKAGE'>$PACKAGE $VERSION</a>"
247 else
248 echo "</pre>
249 <a href='${host}?sup=upload'>SUP Upload page</a>"
250 # Show receip on error
251 echo "<h2>receip</h2>"
252 echo "<pre>"
253 cat ${cache}/receip | receip_highlighter
254 echo "</pre>"
255 fi
256 # HTML Footer
257 echo "</p>
258 </body>
259 </html>"
260 rm -rf ${cache} $(dirname $tmpfile) && exit 0 ;;
262 *\ dbsum\ *)
263 # Used by client to check for newer packages.sql
264 header "Content-Type: text/plain"
265 md5sum ${pkgsdb} | awk '{printf $1}'; exit 0 ;;
267 *\ admin\ *|*\ db\ *)
268 . ${plugins}/sup/sup-admin.cgi ;;
270 *\ sup\ *)
271 d="SUP - SliTaz User Packages"
272 header
273 html_header
274 user_box
275 echo "<h2>${d}</h2>"
276 # Tools for logged users and admins
277 if check_auth; then
278 if admin_user; then
279 tools='<a href="?sup=admin">Admin tools</a>'
280 fi
281 cat << EOT
282 <div id="tools">
283 <a href="?sup=upload">Upload package</a>
284 $tools
285 </div>
286 EOT
287 fi
288 cat << EOT
289 <p>
290 SliTaz User Packages hub: beta testing :-)
291 - Packages: $(ls $wok | wc -l)
292 - <a href="$packages/">Browse mirror</a>
293 - <a href="http://scn.slitaz.org/index.cgi?d=en/sup">Documentation</a>
294 </p>
296 <h3>$(gettext "Latest uploads")</h3>
297 <pre>
298 EOT
299 # Latest uploads from sup.log
300 IFS="|"
301 tac ${suplog} | head -n 6 | while read date user mail pkg version short_desc
302 do
303 cat << EOT
304 <a href="?user=$user">$(get_gravatar $mail 20)</a> \
305 <span class="date">$(echo $date | cut -d " " -f 1)</span> \
306 <a href="?sup=pkg&amp;name=$pkg">$pkg $version</a> - \
307 $(echo ${short_desc} | cut -c 1-34)...
308 EOT
309 done
310 unset IFS
311 echo "</pre>"
313 # Packages listing: if one day, too much packages, then this should
314 # move to ?sup=list
315 cat << EOT
316 <h3>$(gettext "SUP packages")</h3>
317 <div id="plugins">
318 <table>
319 <thead>
320 <td>$(gettext "Package name")</td>
321 <td>$(gettext "Version")</td>
322 <td>$(gettext "Short description")</td>
323 </thead>
324 EOT
325 for pkg in $(ls $wok); do
326 . ${wok}/${pkg}/receip
327 cat << EOT
328 <tr>
329 <td><a href='?sup=pkg&amp;name=$pkg'>$pkg</a></td>
330 <td>$VERSION</td>
331 <td>$SHORT_DESC</td>
332 </tr>
333 EOT
334 done
335 echo "</table></div>"
336 html_footer && exit 0 ;;
338 esac