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

Small fix to path in sup.cgi
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 27 23:01:43 2017 +0100 (2017-02-27)
parents 38d6569ce70f
children 7f0dd18bdb66
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 witg 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 fi
21 host="http://scn.slitaz.org/"
22 scn="/home/slitaz/www/scn"
23 #host="http://localhost/~pankso/cgi-bin/scn/"
24 #scn="/home/pankso/Public/cgi-bin/scn"
26 supdb="$content/sup"
27 wok="$supdb/wok"
28 packages="$supdb/packages"
29 suplog="$cache/log/sup.log"
30 pkgsdb="$packages/packages.sql"
32 # Keep the highlighter minimal :-)
33 receip_highlighter() {
34 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
35 -e s"#^\#\([^']*\)#<span class='comment'>\0</span>#"g \
36 -e s"#\"\([^']*\)\"#<span class='value'>\0</span>#"g
37 }
39 case " $(GET sup) " in
41 *\ pkg\ *)
42 pkg="$(GET name)"
43 d="SUP: $pkg"
44 header
45 html_header
46 user_box
47 if ! . ${wok}/${pkg}/receip; then
48 echo "Missing or corrupted receip" && exit 1
49 fi
51 cat << EOT
52 <h2>$(gettext "Package:") $PACKAGE $VERSION</h2>
53 <div id="tools">
54 <a title="Right click to download link: $PACKAGE-$VERSION.sup"
55 href='$packages/$PACKAGE-$VERSION.sup'>Download</a>
56 EOT
57 # Tools for logged users
58 if check_auth; then
59 # Only package owner can update a package MAIL is set by check_auth
60 if [ "$MAINTAINER" == "$MAIL" ]; then
61 cat << EOT
62 <a href="?sup=upload">$(gettext "Update package")</a>
63 EOT
64 fi
65 fi
66 cat << EOT
67 <a href='?sup'>SUP Hub</a>
68 </div>
69 <pre>
70 ${SHORT_DESC}
71 </pre>
73 EOT
74 # Get package maintainer/user info from SCN/SliTaz user DB
75 if ! . $(fgrep -l "MAIL=\"${MAINTAINER}\"" ${PEOPLE}/*/account.conf); then
76 USER="unknow"
77 fi
78 cat << EOT
79 <div>
80 <a href="?user=$USER">$(get_gravatar $MAINTAINER 24)</a>
81 $(gettext "Maintainer:") <a href="?user=$USER">$NAME</a> -
82 $(gettext "Build date:") ${build_date} -
83 $(gettext "License:") $LICENSE
84 </div>
85 EOT
86 # README
87 if [ -f "${wok}/${pkg}/README" ]; then
88 echo "<h3>README</h3>"
89 echo "<pre>"
90 cat ${wok}/${pkg}/README
91 echo "</pre>"
92 fi
93 # Receip
94 cat << EOT
95 <h3>$(gettext "Receip")</h3>
96 <pre>
97 $(cat ${wok}/${pkg}/receip | receip_highlighter)
98 </pre>
99 EOT
100 html_footer && exit 0 ;;
102 *\ upload\ *)
103 # HTML Form: use cloud plugin CSS style
104 header
105 html_header
106 user_box
107 if ! check_auth; then
108 header "Location: $HTTP_REFERER"
109 fi
110 d="SUP Upload"
111 cat << EOT
112 <h2>${d}</h2>
113 <p>
114 $(gettext "User:") <a href="?user=$user">$user</a> - Mail: $MAIL
115 </p>
117 <div id="cloud-upload">
119 <form method="post"
120 action="plugins/sup/sup.cgi?sup=supQA&amp;user=$user&amp;mail=$MAIL"
121 enctype="multipart/form-data">
122 <input type="file" name="supfile" size="150" />
123 <input type="submit" value="Upload" />
124 </form>
126 </div>
127 EOT
128 html_footer && exit 0 ;;
130 *\ supQA\ *)
131 # SUP Upload Quality assurance. Check package in cache before
132 # publishing. We need full path for upload to work ../../
133 #
134 # This is the geeky part for users, QA output in cmdline style
135 # and to be transparent on what is going on :-)
136 #
137 header
138 d="SUP Upload QA"
139 supfile=$(FILE supfile name)
140 tmpfile=$(FILE supfile tmpname)
141 wok="$scn/content/sup/wok"
142 packages="$scn/content/sup/packages"
143 cache="$scn/cache/sup/${supfile%.sup}"
144 pkgsdb="$scn/content/sup/packages/packages.sql"
145 suplog="$scn/cache/log/sup.log"
146 error=0
148 # clean_error "Message"
149 clean_error() {
150 echo "<span class='error'>ERROR: ${1}</span>"
151 [ -d "$cache" ] && rm -rf ${cache}
152 [ -d "$(dirname $tmpfile)" ] && rm -rf $(dirname $tmpfile)
153 }
155 # Use COOKIE to make sure user is logged in SCN/SUP Hub
156 user="$(echo $(COOKIE auth) | cut -d ':' -f 1)"
157 if [ "$(COOKIE auth)" ] && [ "$user" != "$(GET user)" ]; then
158 clean_error "user auth cookie"; exit 1
159 fi
161 # Is it a .sup file ?
162 if [ "$supfile" != "${supfile%.sup}.sup" ]; then
163 clean_error "not a .sup package: $supfile"; exit 1
164 fi
166 cat << EOT
167 <!DOCTYPE html>
168 <html lang="en">
169 <head>
170 <meta charset="utf-8" />
171 <title>$d</title>
172 <link rel="stylesheet" type="text/css" href="${host}style.css" />
173 <style type="text/css">body { margin: 40px 80px; }</style>
174 </head>
175 <body>
176 <h2>$d</h2>
177 <pre>
178 SUP Hub user : <a href="${host}?user=$user">$user</a>
179 Package file : ${supfile}
180 EOT
181 # Server sanity Check
182 if [ ! -f "$pkgsdb" ]; then
183 clean_error "missing : $(basename $pkgsdb)"
184 echo "</pre>" && exit 1
185 fi
187 mkdir -p ${cache}
188 if ! mv -f ${tmpfile} ${cache}/${supfile}; then
189 clean_error "moving: ${tmpfile} to ${supfile}"
190 echo "</pre>" && exit 1
191 fi
193 # Show MD5sum
194 echo -e "MD5sum : $(md5sum $cache/$supfile | cut -d ' ' -f 1)\n"
196 # Extract receip: sup cook already check vor empty var. Make
197 # sure SCN user name match package MAINTAINER.
198 gettext "Extracting receip..."
199 cd ${cache}
200 if ! cpio -i receip --quiet < ${supfile}; then
201 echo ""
202 clean_error "Can't extract receip"
203 echo "</pre>" && exit 1
204 fi; status
206 if ! . receip; then
207 clean_error "Can't source receip"
208 echo "</pre>" && exit 1
209 fi
210 echo "Build date: <span class='float-right value'>$build_date</span>"
212 # README
213 gettext "Checking for a README file..."
214 cpio -i README --quiet < ${supfile}
215 if [ -f "README" ]; then
216 echo " <span class='float-right color32'>$(gettext 'yes')</span>"
217 else
218 echo " <span class='float-right value'>$(gettext 'no')</span>"
219 fi
221 # Logged user is maintainer ?
222 if [ "$MAINTAINER" != "$(GET mail)" ]; then
223 error=1
224 echo -e "\n<span class='error'>WARNING: user mail not matching</span>"
225 gettext "User mail :"; echo " $(GET mail)"
226 gettext "MAINTAINER :"; echo " $MAINTAINER"
227 fi
229 # Publish and display pkg url's if no error
230 if [ "$error" == "0" ]; then
231 gettext "Moving package to mirror..."
232 mv -f ${supfile} ${packages}; status
233 gettext "Moving receip to public wok..."
234 mkdir -p ${wok}/${PACKAGE} && mv -f receip ${wok}/${PACKAGE}; status
235 [ -f "README" ] && mv -f README ${wok}/${PACKAGE}
237 # Handle packages.md5
240 # Log activity date|user|mail|pkg|version|short_desc
241 cat >> ${suplog} << EOT
242 $(date '+%Y-%m-%d %H:%M')|$user|$MAINTAINER|$PACKAGE|$VERSION|$SHORT_DESC
243 EOT
244 echo "</pre><p>
245 Package page: <a href='${host}?sup=pkg&amp;name=$PACKAGE'>$PACKAGE $VERSION</a>"
246 else
247 echo "</pre>
248 <a href='${host}?sup=upload'>SUP Upload page</a>"
249 # Show receip on error
250 echo "<h2>receip</h2>"
251 echo "<pre>"
252 cat ${cache}/receip | receip_highlighter
253 echo "</pre>"
254 fi
255 # HTML Footer
256 echo "</p>
257 </body>
258 </html>"
259 rm -rf ${cache} $(dirname $tmpfile) && exit 0 ;;
261 *\ admin\ *|*\ db\ *)
262 . ${plugins}/sup/sup-admin.cgi ;;
264 *\ sup\ *)
265 d="SUP - SliTaz User Packages"
266 header
267 html_header
268 user_box
269 echo "<h2>${d}</h2>"
270 # Tools for logged users and admins
271 if check_auth; then
272 if admin_user; then
273 tools='<a href="?sup=admin">Admin tools</a>'
274 fi
275 cat << EOT
276 <div id="tools">
277 <a href="?sup=upload">Upload package</a>
278 $tools
279 </div>
280 EOT
281 fi
282 cat << EOT
283 <p>
284 SliTaz User Packages hub: beta testing :-)
285 - Packages: $(ls $wok | wc -l)
286 - <a href="$packages/">Browse mirror</a>
287 - <a href="http://scn.slitaz.org/index.cgi?d=en/sup">Documentation</a>
288 </p>
290 <h3>$(gettext "Latest uploads")</h3>
291 <pre>
292 EOT
293 # Latest uploads from sup.log
294 IFS="|"
295 tac ${suplog} | head -n 6 | while read date user mail pkg version short_desc
296 do
297 cat << EOT
298 <a href="?user=$user">$(get_gravatar $mail 20)</a> \
299 <span class="date">$(echo $date | cut -d " " -f 1)</span> \
300 <a href="?sup=pkg&amp;name=$pkg">$pkg $version</a> - \
301 $(echo ${short_desc} | cut -c 1-34)...
302 EOT
303 done
304 unset IFS
305 echo "</pre>"
307 # Packages listing: if one day, too much packages, then this should
308 # move to ?sup=list
309 cat << EOT
310 <h3>$(gettext "SUP packages")</h3>
311 <div id="plugins">
312 <table>
313 <thead>
314 <td>$(gettext "Package name")</td>
315 <td>$(gettext "Version")</td>
316 <td>$(gettext "Short description")</td>
317 </thead>
318 EOT
319 for pkg in $(ls $wok); do
320 . ${wok}/${pkg}/receip
321 cat << EOT
322 <tr>
323 <td><a href='?sup=pkg&amp;name=$pkg'>$pkg</a></td>
324 <td>$VERSION</td>
325 <td>$SHORT_DESC</td>
326 </tr>
327 EOT
328 done
329 echo "</table></div>"
330 html_footer && exit 0 ;;
332 esac