s4

view s4-blog.sh @ 133:d5afe98a8b58

debug-off in blog_showentry
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 25 Aug 2015 22:36:59 +0859
parents 42234d2d6e9c
children 12c3f2e334a1
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 $4(optional)=Action
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 action=${4:-書き込み}
21 mode=`getvalbyid blog notify "$1"`
22 case $mode in
23 admin)
24 if isgroup "$blogowner"; then
25 emails=`getgroupadminmails $blogowner`
26 else
27 emails=`collectemail $blogowner`
28 fi
29 ;;
30 no) return ;;
31 *) emails=`collectemail $blogowner` ;;
32 esac
33 err notify: user=$user Admins=`getgroupadmins $blogowner` Mode=$mode Emails="[$emails]"
34 smail "$emails" "${action}通知 $urlbase"<<EOF
35 [$blogtitle]板に${action}がありました。
36 場所: $blogurl
37 題目: $blogtitle
38 筆者: `gecos $2`
39 内容:
40 `echo "$3"|sed 's/^/> /'`
41 EOF
42 )
43 blog_showentry() {
44 # $1=table $2=rowid
45 # if [ -n "$2" ]; then
46 # if [ -n "$imgcached" ]; then
47 # bstmpdir=$tmpdir/$imgcached/$thumbxy
48 # else
49 # bstmpdir=$tmpd
50 # # tmpd=`mktempd`
51 # # tmpfiles=$tmpfiles" $tmpd"
52 # fi
53 # fi
54 td=`getcachedir "article/$2"`
55 tbl=${1%%[!A-Z0-9a-z_]*} rowid=${2%%[!A-Z0-9a-z_]*}
56 err rowid=$rowid, '$2'=$2
57 ts=${tbl}_s tm=${tbl}_m
58 at=article as=article_s am=article_m
59 serial=$(($(date +%s)-1420038000))s$$
60 blog_writable $rowid $user && iswritable=true || iswritable=false
61 # This function grasps blog entry definiton directly.
62 # blog: id
63 # blog_s: title,ctime,heading
64 # blog_m: *article
66 err "SELECT id from $tbl where rowid=$rowid"
67 id=`query "select id from $tbl where rowid=$rowid;"`
68 err id=$id
69 err "select val from $ts where key='title' and id='$id';"
72 #(1)Display root article
73 cat<<EOF
74 <form class="replyblog" action="$myname?replyblog+${rowid}#bottom" method="POST" enctype="multipart/form-data">
75 <table class="bloghead">
76 EOF
78 href="<a href=\"?editheading+$rowid\"> 編集 </a>"
79 if $iswritable; then
80 href2="<a href=\"?lshandout+$rowid\"> 提出状況 </a>"
81 href3="(<a href=\"?gethandout+$rowid\">ファイル取得</a>)"
82 fi
83 cat<<EOF | sq -html $db \
84 | sed -e "s|\(<TR><TD>\),e,|\1 $href |" \
85 -e "s|,s,\(</TD>\)|$href2$href3\1|"
86 -- select val from $ts where key="title" and id="$id";
87 select
88 coalesce((select ",e," from blog where rowid=$rowid and author='$user'),'')
89 ||val||" "
90 ||case (select val from $ts where key="mode" and id="$id")
91 when 'report-closed' then "レポート提出用(自身のファイルのみ参照可),s,"
92 when 'report-open' then "レポート提出用,s,"
93 else ""
94 end
95 from $ts where key="ctime" and id="$id";
96 select val from $ts where key="heading" and id="$id";
97 EOF
98 cat<<EOF
99 </table>
100 <table class="blog_replies">
101 EOF
103 #(2)Display following articles
104 textform='<div><table class="b">
105 <tr><td><textarea name="text" cols="40" rows="4"></textarea></td></tr>
106 <tr><td>添付ファイル: <input type="file" name="image" multiple></td></tr>
107 </table>
108 <input type="submit" value="送信">
109 <input type="reset" value="リセット"></div>
110 '
111 ## 6/11の次: articleを出して行く
112 : <<EOF
113 シリアル:id:p:serial:
114 blogID:blogid:f:blog(id):
115 筆者:author:s:owner
116 時刻:ctime:s:stamp:
117 参照元:parent:s:parent:
118 パス:path:s:path:
119 本文:text:s:textarea:cols="60" rows="8"
120 画像:image:m:image:
121 stage:stage:x:hidden:value="replyblog"
123 article(id, blogid, author)
124 article_s: Visible = ctime, text Invisible = parent, path
125 article_m: image
127 article = hoge|1433812374x20849|yuuji@gentei.org
128 article_s = hoge|ctime|string|2015-06-13 12:27:34|
129 hoge|text|string|Shall we dance?|
130 EOF
131 ##
132 ##
133 err "select id from $at where blogid='$id';"
134 # arts=`sq $db "select a.rowid,a.id,
135 #-- coalesce(b.gecos, a.author)
136 # a.author from $at a
137 # LEFT JOIN (select name,val as gecos from user_s where key='gecos') b
138 # on a.author=b.name where blogid='$id'";`
139 arts=`query "select rowid,id,author from $at where blogid='$id';"`
140 # err arts="[$arts]"
141 number=0
142 hrefhome0="<a href=\"?home"
143 for a in $arts; do
144 arid=${a%%\|*} aid=`echo "$a"|cut -d'|' -f2` author=${a##*\|} imgs=""
145 ## err a=$a, aid=$aid, author=$author
146 ## err td=$td
147 val=`getvalbyid article image "$arid" "$td"`
148 ## err val="[$val]" and td as follows:
150 if [ -n "$val" ]; then
151 hrfb="$myname?showattc+article_m"
152 imgs="<br>"$(echo "$val"\
153 |while read fn; do
155 #data=`percenthex $td/$fn`
156 #ct=`cat $td/$fn.content-type`
157 sz=`cat $td/$fn.size`
158 ri=`cat $td/$fn.rowid`
159 ## err fn=$fn ct=$ct ri=$ri ls::: "`ls -lF $td/`"
160 iconhref2 $td/$fn "$hrfb+$ri" "$fn" "$fn($sz)"
161 done)
162 imgs=`echo "$imgs"|tr -d '\n'` # kill newlines for sed
163 fi
165 ### number=$((number+1))
166 # reply="<input type=\"radio\" name=\"parent\" class=\"replybtn\" \
167 # value=\"$number\">"
168 getgecos="(select rowid from user where name='$author')||':'||\
169 coalesce((select val from user_s \
170 where name='$author' and key='gecos'), '$author')||',[/a],'"
171 href="$myname?editart+$arid+$rowid"
172 link="<a href=\"$href\">編集</a>"
173 cat<<EOF | sq -html $db \
174 | sed -e "s|,n,\([0-9]*\):|,n,$hrefhome0+\1\">|" \
175 -e 's|,\[/a\],|</a>|' \
176 -e 's/,n,/<br>/g' -e "s|,i,|$imgs|" \
177 -e "s|<TR>\(<TD>\)|<TR id=\"$aid\">\1|" \
178 -e "s|^href=\([-A-Za-z0-9,.:/~_%#&+?]*\)|<a &>\1</a>|" \
179 -e "s|\(<TR.*>\)\(<TD>\),e,|\1\2$link|"
180 select
181 coalesce((select ",e," from article where id='$aid' and author='$user'),"")
182 ||
183 max(case key when 'ctime' then ",n,"||val||
184 ",n,"||$getgecos end) as TIME,
185 -- max(case key when 'parent' then val||"への返信" end) as REPLYTO,
186 max(case key when 'text' then val||",i," end) as TEXT
187 from article_s where id = '$aid'
188 group by id order by TIME;
189 EOF
190 done
191 echo "</table>"
194 $iswritable && cat<<EOF
195 <div class="blogcomment">
196 <p>コメント記入</p>
197 <input type="hidden" name="blogid" value="$id">
198 <input type="hidden" name="stage" value="replyblog">
199 <input type="hidden" name="serial" value="$serial">
200 $textform
201 </div>
202 EOF
203 echo "</form><p id=\"bottom\"></p>"
204 # Record access log
205 acclog blog $rowid
206 }
208 lshandout() {
209 # $1=rowid of blog
210 if ! blog_writable $1 $user; then
211 echo "メンバー以外は利用できません。" | html p; return
212 fi
213 time=`getvalbyid blog ctime $1|colrm 11`
214 owner=`getvalbyid blog owner $1`
215 title=`getvalbyid blog title $1`
216 ge=`gecos $owner`
217 lshandoutsub $owner "$@" \
218 |m4 -D_TITLE_="提出状況" \
219 -D_FORMHEAD_="$time [$title]@${ge:-$owner}" \
220 -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
221 $layout/html.m4.html $layout/form+dump-whead.m4.html
222 }
223 lshandoutsub() {
224 # $1=owner $2=rowid of blog
225 if isgroup $1; then
226 sample="(select user from grp_mem where gname='$1')"
227 else
228 sample="(select distinct author as user from arts)"
229 echo "(集計は板への投稿者のみ)" | html p
230 fi
231 sql="with arts as (select id,author from article \
232 where blogid=(select id from blog where rowid=$2))\
233 select (select rowid from user where name=c0.user)||' '|| \
234 coalesce((select val from user_s where name=c0.user \
235 and key='gecos'),\
236 c0.user) as 'メンバー',\
237 sum(case when c1.key is not null then 1 else 0 end)\
238 as 'コメント記入',\
239 sum(case when c2.key is not null then 1 else 0 end)\
240 as 'ファイルの提出'\
241 from $sample c0 \
242 left join (select id,author from arts) a\
243 on c0.user=a.author\
244 left join (select id,key from article_s where key='text') c1\
245 on a.id=c1.id left join (select id,key from article_m ) c2\
246 on c1.id=c2.id group by c0.user order by c0.user;"
247 err ishandoutsub: sql="$sql"
248 echo '<table class="b td2r td3r">'
249 hrb="<a href=\"?home+"
250 echo "$sql" | sq -header -html $db \
251 | sed -e "s,\(<TR><TD>\)\([^ ]*\) \(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
252 echo '</table>'
253 }
254 gethandout() {
255 # $1=rowid of blog
256 if ! blog_writable $1 $user; then
257 echo "メンバー以外は利用できません。" | html p; return
258 fi
259 i=0
260 bd=$tmpd/archive.$$
261 mkdir $bd
262 query "select m.rowid,author,m.val from article a join article_m m\
263 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
264 and m.key in ('image', 'document', 'binary');" \
265 | while IFS='|' read rowid author filename; do
266 # err isfilereadable $user article_m $rowid
267 isfilereadable $user article_m $rowid || continue
268 # err ok
269 i=$((i+1))
270 dir=`printf $bd/%03d $i`
271 mkdir $dir
272 query "select quote(bin) from article_m where rowid=$rowid;" \
273 | unhexize > $dir/$filename
274 done
275 if [ ! -d $bd/001 ]; then
276 contenttype; echo
277 echo "取得できるファイルがありませんでした。" | html p
278 return
279 fi
280 (cd $bd
281 err cdto$bd; (pwd; ls -lFa) 1>&3
282 tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
283 )
284 arc=$bd/archive.tar.gz
285 echo "Content-type: application/x-gzip"
286 echo "Content-Length: `cat $arc|wc -c`"
287 echo "Content-Disposition: filename=\"archive.tar.gz\""
288 echo
289 cat $arc
290 }
291 lsmyfile() { # $1(optional)=SortBy
292 case "$1" in
293 ""|CTIME-DESC)
294 by="CTIME" ord="DESC" ;;
295 CTIME*) by="CTIME" ;;
296 FILE*) by="FILE" ;;
297 OWNER*) by="OWNER" ;;
298 TITLE*) by="TITLE" ;;
299 esac
300 case "$1" in
301 *DESC) ord="DESC" ;;
302 esac
303 case "$ord" in
304 DESC) lkod="" jord="降順" ;;
305 *) lkod="-DESC" jord="昇順" ;;
306 esac
307 sql="select m.val||'/'||m.rowid FILE,
308 coalesce(
309 case when (select name from user where name=bs.owner)
310 is not null
311 then (select val from user_s where name=bs.owner
312 and key='gecos')
313 when (select gname from grp where gname=bs.owner)
314 is not null
315 then (select val from grp_s where gname=bs.owner
316 and key='gecos')
317 else
318 null
319 end,
320 bs.owner
321 ) OWNER,
322 a_s.val CTIME,
323 ',t,'||bs.title||':'||b.rowid||'#'||a.id TITLE
324 from (select rowid,id,val from article_m where id
325 in (select id from article where author='$user')
326 and type like 'file:%')
327 m left join article a on m.id=a.id
328 left join article_s a_s on a.id=a_s.id and a_s.key='ctime'
329 left join (select id,
330 max(case key when 'owner' then val end) as owner,
331 max(case key when 'title' then val end) as title
332 from blog_s group by id)
333 bs on a.blogid=bs.id
334 left join blog b on bs.id=b.id
335 where m.val is not null order by $by $ord;"
336 err lshandoutbyauthor: sql=`echo "$sql"`
337 title="個人提出ファイル"
338 m4 -D_TITLE_=$title $layout/html.m4.html
339 hra="<a href=\"?lsmyfile+"
340 hrb="<a href=\"?showattc+article_m+"
341 hrc="<a href=\"?replyblog+"
342 (echo '<table class="b">'
343 echo "$sql"|sq -html -header $db ) \
344 | sed -e "s|\(<TR><TD>\)\([^/]*\)/\([0-9]*\)|\1$hrb\3\">\2</a>|" \
345 -e "s|,t,\(.*\):\([^<]*\)\(</TD>\)|$hrc\2\">\1</a>\3|" \
346 -e "s|\(<TH>\)\([A-Z]*\)\(</TH>\)|\1$hra\2$lkod\">\2</a>|" \
347 | m4 -D_TITLE_=$title -D_FORM_="<p>($by$jord)</p>" \
348 -D_DUMPTABLE_="syscmd(cat)" $layout/form+dump.m4.html
349 echo '</table>'
350 }
351 searchart() {
352 kwd=`getpar kwd`
353 if [ -z "$kwd" ]; then
354 echo "検索語を指定してください" | html p; return
355 fi
356 owner=`getpar owner`
357 owner=${owner:-$1}
358 echo "「$kwd」による検索結果" | html p
359 if [ -n "$owner" ]; then
360 cond="where key='owner' and val='$owner'"
361 if isuser $owner; then
362 echo "(`linkhome $owner` さんの記録からの検索)" | html p
363 else
364 linkhome $owner 1>&3
365 echo "(`linkhome $owner` グループからの検索)" | html p
366 fi
367 fi
368 # article_s: id=article-id, key='text', val='TEXT'
369 # article: id=article-id, blogid=blogkd
370 # blog: id=blog-id, author=LeaderAuthor
371 # blog_s: id=blog-id, key='title', val='BLOG-TITLE'
372 # WANT: blog-ROWid,article-id,val(TEXT)
373 sql="select b.rowid||'#'||x.id as '',
374 b.title as TITLE,
375 substr(x.val, 0, 80) as TEXT
376 from (select blog.rowid,blog.*,bs.val as title from blog join blog_s bs
377 on blog.id=bs.id and bs.key='title') b
378 join
379 (select a.id,a.blogid,s.val from article a join article_s s
380 on a.id=s.id where s.val like '%$kwd%')
381 x on b.id=x.blogid
382 where b.id in (select id from blog_s
383 $cond);"
384 sedopt="s,<TR><TD>\([^<]*\)</TD>,<TR><TD><a\
385 href=\"?replyblog+\1\">VIEW</a></TD>,"
386 cat<<EOF
387 <table class="b searchart">
388 `sq -header -html $db "$sql"|sed "$sedopt"`
389 </table>
390 EOF
391 }
392 listblog() (
393 # $1={user,group}
394 qow=`sqlquote $1`
395 cond="where a.id in (select id from blog_s where key='owner' and val=$qow) order by ctime desc"
396 DT_CHLD=article:blogid
397 cgi_form searchart<<EOF
398 <label>`cgi_text kwd`という語を含む記事をこの一覧から検索</label>
399 `cgi_hidden owner $user`
400 EOF
401 dumptable html blog 'ctime title heading' "$cond"
402 )
404 blog_addentry() {
405 # $1=GRPname(if it is a group)
406 grprowid=$1
407 rowid=`getpar rowid`
408 err blog_addentry0: rowid=$rowid
409 if [ -n "$grprowid" ]; then
410 owner=`getgroupbyid $grprowid`
411 else
412 owner=`getpar owner`
413 fi
414 err blog-add: \$1=$1 rowid=$rowid owner=$owner
415 if isgroup $owner; then
416 groupmode=1 listing=$owner guide="[${owner}]" GF_OWNER=$owner
417 else
418 usermode=1 listing=$user guide="[個人]"
419 fi
421 if [ -n "`getpar title`" ]; then
422 if [ "$usermode" ]; then
423 err usermode: user=$user owner=$owner
424 if [ x"$user" != x"$owner" ]; then
425 echo "他人の日記は書けません" | html p
426 return 2
427 fi
428 elif [ "$groupmode" ]; then # if write to group log
429 grp=$owner #\`getpar grp\`
430 err ismember: $user $grp
431 if ! ismember "$user" "$grp"; then
432 echo "(話題作成はこのグループに加入してから)" | html p
433 return 3
434 fi
435 fi
436 par2table $formdir/blog.def
437 serial=`getpar serial`
438 err SERIAL: $serial ROWID=$rowid listing=$listing
439 id=""
440 if [ -n "$rowid" ]; then
441 # Here, id becomes NULL when removal of entries at par2table
442 id=`query "select rowid from blog where rowid=$rowid;"`
443 elif [ -n "$serial" ]; then
444 # If new blog leader created, traverse to its head.
445 id=`query "select rowid from blog where id='$serial';"`
446 err new-Leader: "select rowid from blog where id='$serial';" id=$id
447 fi
448 if [ -n "$id" ]; then
449 ## If new aritcle is entered, JUMP to blog_reply
450 blog_reply $id
451 return
452 fi
453 fi
454 echo "${guide}新規話題作成" > $tmpd/title.$$
455 listblog $listing > $tmpd/listblog.$$
456 genform $formdir/blog.def \
457 | m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" \
458 -D_FORMHEAD_="序文は簡単に詳しくはコメントに" \
459 -D_DUMPHEAD_="これまでの蓄積" \
460 -D_FORM_="syscmd(\`cat')" \
461 -D_DUMPTABLE_="spaste(\`$tmpd/listblog.$$')" \
462 $layout/html.m4.html \
463 $layout/form+dump-whead.m4.html
464 }
466 blog_reply() {
467 rowid=$1
468 err rowid=$1
470 if [ -z "$rowid" ]; then
471 echo "表示する日記番号が未指定です。" | html p
472 return
473 fi
474 title=`getvalbyid blog title $rowid`
475 owner=`getvalbyid blog owner $rowid`
476 if isuser "$owner"; then
477 subtitle="`gecos $owner` さんの話題"
478 else
479 grprowid=`query "select rowid from grp where gname=\"$owner\";"`
480 subtitle="グループ <a href=\"?grp+$grprowid\">$owner</a> での話題"
481 fi
482 if [ -z "$title" ]; then
483 echo "日記番号指定が無効です。" | html p
484 return
485 fi
487 text=`getpar text`
488 if [ -n "$text" ]; then
489 if blog_writable $rowid $user; then
490 par2table $formdir/article.def
491 st=$? # ; err par2t-st=$st
492 case $st in
493 0|4)
494 [ "$st" = "4" ] && act="書込削除"
495 blog_notify_reply $rowid $user "$text" $act ;;
496 esac
497 else
498 title="$title(加入してないので書き込み不可)"
499 fi
500 fi
501 def=$formdir/article.def
502 echo "$title" > $tmpd/title.$$
503 echo "$subtitle" > $tmpd/subtitle.$$
504 blog_showentry blog $rowid \
505 | m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" -D_BODYCLASS_=general \
506 -D_FORMHEAD_="spaste(\`$tmpd/subtitle.$$')" -D_FORM_="" \
507 -D_DUMPTABLE_="syscmd(cat)" -D_DUMPHEAD_="" \
508 $layout/html.m4.html $layout/form+dump-whead.m4.html
509 }