s4

view s4-blog.sh @ 72:3dc3ddd2d3e5

getcachedir argument contains rowid
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 31 Jul 2015 23:22:59 +0900
parents d10f98c2b192
children 0cbbb0874eb5
line source
1 #
2 type cgiinit >/dev/null 2>&1 || . ./s4-funcs.sh
4 blog_genform() {
5 #
6 t=$1
7 }
9 blog_writable() (
10 # $1=articleid $2=user
11 blogowner=`getvalbyid blog owner "$1"`
12 [ x"$blogowner" = x"$2" ] || isuser "$blogowner" || ismember "$2" "$blogowner"
13 )
14 blog_notify_reply() (
15 # $1=articleid $2=ReplyingUser $3=WrittenText
16 blogowner=`getvalbyid blog owner "$1"`
17 [ x"$2" = x"$blogowner" ] && return # If author=blogowner, unnecessary
18 blogtitle=`getvalbyid blog title "$1"`
19 blogurl="$urlbase?replyblog+$1"
20 mode=`getvalbyid blog notify "$1"`
21 case $mode in
22 admin)
23 if isgroup "$blogowner"; then
24 emails=`for i in $(getgroupadmins $blogowner); do\
25 [ x"$i" = x"$user" ] && continue;\
26 email4group "$blogowner" "$i" ;\
27 done`
28 else
29 emails=`collectemail $blogowner`
30 fi
31 ;;
32 no) return ;;
33 *) emails=`collectemail $blogowner` ;;
34 esac
35 err notify: user=$user Admins=`getgroupadmins $blogowner` Mode=$mode Emails="[$emails]"
36 smail "$emails" "書込通知 $urlbase"<<EOF
37 [$blogtitle]板に書き込みがありました。
38 場所: $blogurl
39 題目: $blogtitle
40 筆者: `gecos $2`
41 内容:
42 `echo "$3"|sed 's/^/> /'`
43 EOF
44 )
45 blog_showentry() {
46 # $1=table $2=rowid
47 # if [ -n "$2" ]; then
48 # if [ -n "$imgcached" ]; then
49 # bstmpdir=$tmpdir/$imgcached/$thumbxy
50 # else
51 # bstmpdir=$tmpd
52 # # tmpd=`mktempd`
53 # # tmpfiles=$tmpfiles" $tmpd"
54 # fi
55 # fi
56 td=`getcachedir "article/$2"`
57 tbl=${1%%[!A-Z0-9a-z_]*} rowid=${2%%[!A-Z0-9a-z_]*}
58 err rowid=$rowid, '$2'=$2
59 ts=${tbl}_s tm=${tbl}_m
60 at=article as=article_s am=article_m
61 serial=$(($(date +%s)-1420038000))s$$
62 blog_writable $rowid $user && iswritable=true || iswritable=false
63 # This function grasps blog entry definiton directly.
64 # blog: id
65 # blog_s: title,ctime,heading
66 # blog_m: *article
68 err "SELECT id from $tbl where rowid=$rowid"
69 id=`query "select id from $tbl where rowid=$rowid;"`
70 err id=$id
71 err "select val from $ts where key='title' and id='$id';"
74 #(1)Display root article
75 cat<<EOF
76 <form class="replyblog" action="$myname?replyblog+$rowid" method="POST" enctype="multipart/form-data">
77 <table class="bloghead">
78 EOF
80 href="<a href=\"?editheading+$rowid\"> 編集 </a>"
81 if $iswritable; then
82 href2="<a href=\"?lshandout+$rowid\"> 提出状況 </a>"
83 href3="(<a href=\"?gethandout+$rowid\">ファイル取得</a>)"
84 fi
85 cat<<EOF | sq -html $db \
86 | sed -e "s|\(<TR><TD>\),e,|\1 $href |" \
87 -e "s|,s,\(</TD>\)|$href2$href3\1|"
88 -- select val from $ts where key="title" and id="$id";
89 select
90 coalesce((select ",e," from blog where rowid=$rowid and author='$user'),'')
91 ||val||" "
92 ||case (select val from $ts where key="mode" and id="$id")
93 when 'report-closed' then "レポート提出用(自身のファイルのみ参照可),s,"
94 when 'report-open' then "レポート提出用,s,"
95 else ""
96 end
97 from $ts where key="ctime" and id="$id";
98 select val from $ts where key="heading" and id="$id";
99 EOF
100 cat<<EOF
101 </table>
102 <table class="blog_replies">
103 EOF
105 #(2)Display following articles
106 textform='<div><table class="b">
107 <tr><td><textarea name="text" cols="40" rows="4"></textarea></td></tr>
108 <tr><td>添付ファイル: <input type="file" name="image" multiple></td></tr>
109 </table>
110 <input type="submit" value="送信">
111 <input type="reset" value="リセット"></div>
112 '
113 ## 6/11の次: articleを出して行く
114 : <<EOF
115 シリアル:id:p:serial:
116 blogID:blogid:f:blog(id):
117 筆者:author:s:owner
118 時刻:ctime:s:stamp:
119 参照元:parent:s:parent:
120 パス:path:s:path:
121 本文:text:s:textarea:cols="60" rows="8"
122 画像:image:m:image:
123 stage:stage:x:hidden:value="replyblog"
125 article(id, blogid, author)
126 article_s: Visible = ctime, text Invisible = parent, path
127 article_m: image
129 article = hoge|1433812374x20849|yuuji@gentei.org
130 article_s = hoge|ctime|string|2015-06-13 12:27:34|
131 hoge|text|string|Shall we dance?|
132 EOF
133 ##
134 ##
135 err "select id from $at where blogid='$id';"
136 # arts=`sq $db "select a.rowid,a.id,
137 #-- coalesce(b.gecos, a.author)
138 # a.author from $at a
139 # LEFT JOIN (select name,val as gecos from user_s where key='gecos') b
140 # on a.author=b.name where blogid='$id'";`
141 arts=`query "select rowid,id,author from $at where blogid='$id';"`
142 # err arts="[$arts]"
143 number=0
144 hrefhome0="<a href=\"?home"
145 for a in $arts; do
146 arid=${a%%\|*} aid=`echo "$a"|cut -d'|' -f2` author=${a##*\|} imgs=""
147 err a=$a, aid=$aid, author=$author
148 # name='' # Get gecos??
149 # td=$(echo $bstmpdir/`echo $at|md5`)
150 err td=$td
151 val=`getvalbyid article image "$arid" "$td"`
152 err val="[$val]" and td as follows:
154 if [ -n "$val" ]; then
155 hrfb="$myname?showattc+article_m"
156 imgs="<br>"$(echo "$val"\
157 |while read fn; do
159 #data=`percenthex $td/$fn`
160 #ct=`cat $td/$fn.content-type`
161 sz=`cat $td/$fn.size`
162 ri=`cat $td/$fn.rowid`
163 err fn=$fn ct=$ct ri=$ri ls::: "`ls -lF $td/`"
164 iconhref $td/$fn "$hrfb+$ri" "$fn" "$fn($sz)"
165 done)
166 imgs=`echo "$imgs"|tr -d '\n'` # kill newlines for sed
167 fi
169 ### number=$((number+1))
170 # reply="<input type=\"radio\" name=\"parent\" class=\"replybtn\" \
171 # value=\"$number\">"
172 getgecos="(select rowid from user where name='$author')||':'||\
173 coalesce((select val from user_s \
174 where name='$author' and key='gecos'), '$author')||',[/a],'"
175 href="$myname?editart+$arid+$rowid"
176 link="<a href=\"$href\">編集</a>"
177 cat<<EOF | sq -html $db \
178 | sed -e "s|,n,\([0-9]*\):|,n,$hrefhome0+\1\">|" \
179 -e 's|,\[/a\],|</a>|' \
180 -e 's/,n,/<br>/g' -e "s|,i,|$imgs|" \
181 -e "s|<TR>\(<TD>\)|<TR id=\"$aid\">\1|" \
182 -e "s|^href=\([-A-Za-z0-9,.:/~_%#&+?]*\)|<a &>\1</a>|" \
183 -e "s|\(<TR.*>\)\(<TD>\),e,|\1\2$link|"
184 select
185 coalesce((select ",e," from article where id='$aid' and author='$user'),"")
186 ||
187 max(case key when 'ctime' then ",n,"||val||
188 ",n,"||$getgecos end) as TIME,
189 -- max(case key when 'parent' then val||"への返信" end) as REPLYTO,
190 max(case key when 'text' then val||",i," end) as TEXT
191 from article_s where id = '$aid'
192 group by id order by TIME;
193 EOF
194 done
195 echo "</table>"
198 $iswritable && cat<<EOF
199 <div class="blogcomment">
200 <p>コメント記入</p>
201 <input type="hidden" name="blogid" value="$id">
202 <input type="hidden" name="stage" value="replyblog">
203 <input type="hidden" name="serial" value="$serial">
204 $textform
205 </div>
206 EOF
207 echo "</form>"
209 # Record access log
210 acclog blog $rowid
211 }
213 lshandout() {
214 # $1=rowid of blog
215 if ! blog_writable $1 $user; then
216 echo "メンバー以外は利用できません。" | html p; return
217 fi
218 time=`getvalbyid blog ctime $1|colrm 11`
219 owner=`getvalbyid blog owner $1`
220 title=`getvalbyid blog title $1`
221 ge=`gecos $owner`
222 lshandoutsub $owner "$@" \
223 |m4 -D_TITLE_="提出状況" \
224 -D_FORMHEAD_="$time [$title]@${ge:-$owner}" \
225 -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
226 $layout/html.m4.html $layout/form+dump-whead.m4.html
227 }
228 lshandoutsub() {
229 # $1=owner $2=rowid of blog
230 if isgroup $1; then
231 sample="(select user from grp_mem where gname='$1')"
232 else
233 sample="(select distinct author as user from arts)"
234 echo "(集計は板への投稿者のみ)" | html p
235 fi
236 sql="with arts as (select id,author from article \
237 where blogid=(select id from blog where rowid=$2))\
238 select (select rowid from user where name=c0.user)||' '|| \
239 coalesce((select val from user_s where name=c0.user \
240 and key='gecos'),\
241 c0.user) as 'メンバー',\
242 sum(case when c1.key is not null then 1 else 0 end)\
243 as 'コメント記入',\
244 sum(case when c2.key is not null then 1 else 0 end)\
245 as 'ファイルの提出'\
246 from $sample c0 \
247 left join (select id,author from arts) a\
248 on c0.user=a.author\
249 left join (select id,key from article_s where key='text') c1\
250 on a.id=c1.id left join (select id,key from article_m ) c2\
251 on c1.id=c2.id group by c0.user order by c0.user;"
252 err ishandoutsub: sql="$sql"
253 echo '<table class="b td2r td3r">'
254 hrb="<a href=\"?home+"
255 echo "$sql" | sq -header -html $db \
256 | sed -e "s,\(<TR><TD>\)\([^ ]*\) \(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
257 echo '</table>'
258 }
259 gethandout() {
260 # $1=rowid of blog
261 if ! blog_writable $1 $user; then
262 echo "メンバー以外は利用できません。" | html p; return
263 fi
264 i=0
265 bd=$tmpd/archive.$$
266 mkdir $bd
267 query "select m.rowid,author,m.val from article a join article_m m\
268 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
269 and m.key in ('image', 'document', 'binary');" \
270 | while IFS='|' read rowid author filename; do
271 # err isfilereadable $user article_m $rowid
272 isfilereadable $user article_m $rowid || continue
273 # err ok
274 i=$((i+1))
275 dir=`printf $bd/%03d $i`
276 mkdir $dir
277 query "select quote(bin) from article_m where rowid=$rowid;" \
278 | unhexize > $dir/$filename
279 done
280 if [ ! -d $bd/001 ]; then
281 contenttype; echo
282 echo "取得できるファイルがありませんでした。" | html p
283 return
284 fi
285 (cd $bd
286 err cdto$bd; (pwd; ls -lFa) 1>&3
287 tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
288 )
289 arc=$bd/archive.tar.gz
290 echo "Content-type: application/x-gzip"
291 echo "Content-Length: `cat $arc|wc -c`"
292 echo "Content-Disposition: filename=\"archive.tar.gz\""
293 echo
294 cat $arc
295 }
297 listblog() (
298 # $1={user,group}
299 qow=`sqlquote $1`
300 cond="where a.id in (select id from blog_s where key='owner' and val=$qow) order by ctime desc"
301 DT_CHLD=article:blogid
302 cgi_form searchart<<EOF
303 <label>`cgi_text kwd`という語を含む記事を検索</label>
304 `cgi_hidden owner $user`
305 EOF
306 dumptable html blog 'ctime title heading' "$cond"
307 )
309 blog_addentry() {
310 # $1=GRPname(if it is a group)
311 grprowid=$1
312 rowid=`getpar rowid`
313 err ba: rowid=$rowid
314 #if [ -z "$rowid" ]; then
315 # When rowid is SET, it is updation of existing entry
316 if [ -z "$1" ]; then
317 listing=$user guide="[個人]"
318 #listing代入は rowid 時でもするべき
319 else
320 grp=`getgroupbyid $grprowid`
321 if [ -n "$grp" ]; then
322 listing=$grp guide="[${grp}]" GF_OWNER=$grp
323 else
324 echo "無効なグループ指定です。" | html p
325 return
326 fi
327 fi
328 #fi
329 if [ -n "`getpar title`" ]; then
330 owner=`getpar owner`
331 if isuser $owner; then
332 if [ x"$user" != x"$owner" ]; then
333 echo "他人の日記は書けません" | html p
334 return
335 fi
336 elif isgroup $owner; then # if write to group log
337 grp=$owner #\`getpar grp\`
338 err ismember: $user $grp
339 if ! ismember "$user" "$grp"; then
340 echo "(話題作成はこのグループに加入してから)" | html p
341 return
342 fi
343 fi
344 par2table $formdir/blog.def
345 serial=`getpar serial`
346 err SERIAL: $serial ROWID=$rowid listing=$listing
347 id=""
348 if [ -n "$rowid" ]; then
349 # Here, id becomes NULL when removal of entries at par2table
350 id=`query "select rowid from blog where rowid=$rowid;"`
351 elif [ -n "$serial" ]; then
352 # If new blog leader created, traverse to its head.
353 id=`query "select rowid from blog where id='$serial';"`
354 fi
355 if [ -n "$id" ]; then
356 ## If new aritcle is entered, JUMP to blog_reply
357 blog_reply $id
358 return
359 fi
360 fi
361 m4 -D_TITLE_="${guide}新規話題作成" \
362 -D_FORMHEAD_="序文は簡単に詳しくはコメントに" \
363 -D_DUMPHEAD_="これまでの蓄積" \
364 -D_FORM_="`genform $formdir/blog.def`" \
365 -D_DUMPTABLE_="`listblog $listing`" \
366 $layout/html.m4.html \
367 $layout/form+dump-whead.m4.html
368 }
370 blog_reply() {
371 rowid=$1
372 err rowid=$1
374 if [ -z "$rowid" ]; then
375 echo "表示する日記番号が未指定です。" | html p
376 return
377 fi
378 title=`getvalbyid blog title $rowid`
379 owner=`getvalbyid blog owner $rowid`
380 if isuser "$owner"; then
381 subtitle="`gecos $owner` さんの話題"
382 else
383 grprowid=`query "select rowid from grp where gname=\"$owner\";"`
384 subtitle="グループ <a href=\"?grp+$grprowid\">$owner</a> での話題"
385 fi
386 if [ -z "$title" ]; then
387 echo "日記番号指定が無効です。" | html p
388 return
389 fi
391 text=`getpar text`
392 if [ -n "$text" ]; then
393 if blog_writable $rowid $user; then
394 if par2table $formdir/article.def; then
395 blog_notify_reply $rowid $user "$text"
396 fi
397 else
398 title="$title(加入してないので書き込み不可)"
399 fi
400 fi
401 def=$formdir/article.def
402 blog_showentry blog $rowid \
403 | m4 -D_TITLE_="$title" -D_BODYCLASS_=general \
404 -D_FORMHEAD_="$subtitle" -D_FORM_="" \
405 -D_DUMPTABLE_="syscmd(cat)" -D_DUMPHEAD_="" \
406 $layout/html.m4.html $layout/form+dump-whead.m4.html
407 }