s4

view s4-blog.sh @ 197:d0a5ffc2cabf

div.dumptable within div.fold should have scroll bar
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 24 Apr 2016 17:06:34 +0859
parents 50e6195130b3
children 71a2c840cfd7
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_readable() {
15 # $1=articleid $2=user
16 mode=`getgroupattr $grp regmode`
17 }
18 blog_notify_reply() (
19 # $1=articleid $2=ReplyingUser $3=WrittenText $4(optional)=Action
20 blogowner=`getvalbyid blog owner "$1"`
21 [ x"$2" = x"$blogowner" ] && return # If author=blogowner, unnecessary
22 blogtitle=`getvalbyid blog title "$1"`
23 blogurl="$urlbase?replyblog+$1"
24 action=${4:-書き込み}
25 mode=`getvalbyid blog notify "$1"`
26 case $mode in
27 admin)
28 if isgroup "$blogowner"; then
29 emails=`getgroupadminmails $blogowner`
30 else
31 emails=`collectemail $blogowner`
32 fi
33 notifyto=`getpar notifyto`
34 if [ -n "$notifyto" ]; then
35 emails=$emails" `email4groupbyuid \"$blogowner\" $notifyto`"
36 fi
37 ;;
38 no) return ;;
39 *) emails=`collectemail $blogowner` ;;
40 esac
41 err notify: user=$user Admins=`getgroupadmins $blogowner` Mode=$mode Emails="[$emails]"
42 SMAIL_TO="`echo "$blogowner" | nkf -jM | tr -d '\n'` readers <$admin>" \
43 smail "$emails" "${action}通知 $urlbase"<<EOF
44 [$blogtitle]板に${action}がありました。
45 場所: $blogurl
46 題目: $blogtitle
47 筆者: `gecos $2`
48 内容:
49 `echo "$3"|sed 's/^/> /'`
50 EOF
51 )
53 blog_showentry() {
54 # $1=table $2=rowid
55 # if [ -n "$2" ]; then
56 # if [ -n "$imgcached" ]; then
57 # bstmpdir=$tmpdir/$imgcached/$thumbxy
58 # else
59 # bstmpdir=$tmpd
60 # # tmpd=`mktempd`
61 # # tmpfiles=$tmpfiles" $tmpd"
62 # fi
63 # fi
64 td=`getcachedir "article/$2"`
65 tbl=${1%%[!A-Z0-9a-z_]*} rowid=${2%%[!A-Z0-9a-z_]*}
66 err rowid=$rowid, '$2'=$2
67 ts=${tbl}_s tm=${tbl}_m
68 at=article as=article_s am=article_m
69 serial=$(($(date +%s)-1420038000))s$$
70 blog_writable $rowid $user && iswritable=true || iswritable=false
71 # This function grasps blog entry definiton directly.
72 # blog: id
73 # blog_s: title,ctime,heading
74 # blog_m: *article
76 # 2015-10-05 check readable
77 if ! $iswritable; then
78 blogowner=`getvalbyid blog owner "$2"`
79 # err blogowner=$blogowner
80 if isgroup $blogowner; then
81 regmode=`getgroupattr $blogowner regmode`
82 # err regmode=$regmode
83 if [ x"$regmode" = x"moderated" ]; then
84 if ! ismember $user $blogowner; then
85 echo "加入してからどうぞ" | html p
86 return
87 fi
88 fi
89 fi
90 fi
91 case `getvalbyid blog notify "$2"` in # "all", "admin" or "no" (or NULL)
92 admin) notifyto=1 ;;
93 *) notifyto="" ;;
94 esac
96 err "SELECT id from $tbl where rowid=$rowid"
97 id=`query "select id from $tbl where rowid=$rowid;"`
98 err id=$id
99 err "select val from $ts where key='title' and id='$id';"
102 #(1)Display root article
103 cat<<EOF
104 <form class="replyblog" action="$myname?replyblog+${rowid}#bottom" method="POST" enctype="multipart/form-data">
105 <table class="bloghead">
106 EOF
108 href="<a href=\"?editheading+$rowid\"> 編集 </a>"
109 if $iswritable; then
110 href2="<a href=\"?lshandout+$rowid\"> 提出状況 </a>"
111 href3="(<a href=\"?gethandout+$rowid\">ファイル取得</a>)"
112 fi
113 href4='<a href="#bottom"> 末尾へ</a>'
114 cat<<EOF | sq -html $db \
115 | sed -e "s|\(<TR><TD>\),e,|\1 $href |" \
116 -e "s|,s,|$href2$href3|" \
117 -e "s|,t,\(</TD\)>|$href4\1|"
118 -- select val from $ts where key="title" and id="$id";
119 select
120 coalesce((select ",e," from blog where rowid=$rowid and author='$user'),'')
121 ||val||" "
122 ||case (select val from $ts where key="mode" and id="$id")
123 when 'report-closed' then "レポート提出用(自身のファイルのみ参照可),s,"
124 when 'report-open' then "レポート提出用,s,"
125 else ""
126 end || ',t,'
127 from $ts where key="ctime" and id="$id";
128 select val from $ts where key="heading" and id="$id";
129 EOF
130 cat<<EOF
131 </table>
132 <table class="blog_replies">
133 EOF
135 lkhome="<a href=\"$myname?home" lke='">'
136 lkedit="<a href=\"$myname?editart"
137 if false; then
138 sq -html $db<<EOF |
139 WITH a_s AS (
140 SELECT id,
141 max(CASE key WHEN 'ctime' THEN val END) TIME,
142 max(CASE key WHEN 'text' THEN val END) TEXT
143 FROM article_s
144 GROUP by id
145 )
146 SELECT 'id:'||a.id,
147 CASE author
148 WHEN '$user' THEN 'ed:'||a.rowid||':'||$rowid
149 ELSE 'e0:'
150 END edit,
151 'at:'||(SELECT rowid FROM user WHERE name=author)||':'
152 ||coalesce((SELECT val FROM user_s
153 WHERE name=author AND key='gecos'),
154 author),
155 'ti:'||s.TIME,
156 'te:'||s.TEXT,
157 'im:'||(SELECT group_concat(hex(bin)
158 FROM (select rowid,id,author from article where blogid in
159 (select id from blog where rowid=$rowid)) a
160 LEFT JOIN
161 a_s s
162 ON a.id=s.id;
163 EOF
164 sed -e "s|^<TR><TD>id:\(.*\)</TD>|<TR id=\"\1\"><TD>|" \
165 -e "/^<TR/N" -e 's/\n//' \
166 -e "s|<TD>ed:\([0-9]*\):\(.*\)</TD>|$lkedit+\1+\2${lke}編集</a>|" \
167 -e "s|<TD>e0:</TD>||" \
168 -e "s|^<TD>at:\([0-9]*\):\(.*\)</TD>|$lkhome+\1$lke\2</a>|" \
169 -e "s|^<TD>ti:\(.*\)</TD>|\1</TD>|" \
170 -e "s|^<TD>te:\(.*\)|<TD>\1|"
171 else
172 hlink="$myname?home" elink="$myname?editart"
173 catlink="$myname?showattc+article_m"
174 deficon="img/file-icon.png"
175 sq $db<<EOF |
176 WITH a_s AS (
177 SELECT id,
178 max(CASE key WHEN 'ctime' THEN val END) TIME,
179 max(CASE key WHEN 'text' THEN val END) TEXT
180 FROM article_s
181 GROUP by id
182 )
183 SELECT a.id,
184 CASE author
185 WHEN '$user' THEN a.rowid||'+'||$rowid
186 ELSE ''
187 END edit,
188 CASE -- 「通知送信」ボタンの有無
189 WHEN '$notifyto' = '' THEN '' -- 不要モードならなし
190 WHEN '$user' = author THEN '' -- 筆者自身ならなし
191 ELSE "yes"
192 END notify,
193 (SELECT rowid FROM user WHERE name=author) user_rid,
194 coalesce((SELECT val FROM user_s
195 WHERE name=author AND key='gecos'),
196 author) uname,
197 a.rowid,
198 s.TIME,
199 hex(s.TEXT),
200 (SELECT group_concat(rowid||':'||length(bin)||':'||hex(val), ' ')
201 FROM article_m
202 WHERE id=a.id AND key='image') imxgids
203 FROM (select rowid,id,author from article where blogid in
204 (select id from blog where rowid=$rowid)) a
205 LEFT JOIN
206 a_s s
207 ON a.id=s.id;
208 EOF
209 while IFS='|' read id edit notify uid uname aid tm hte imgids; do
210 nt="<label style=\"font-size: 70%;\"><input type=\"checkbox\"
211 name=\"notifyto\" value=\"$uid\">返信通知送信</label>"
212 cat<<EOF |
213 <tr id="$id">
214 <td>${edit:+<a href="$elink+$edit">編集</a> }#$aid
215 <a href="$hlink+$uid">$uname</a>
216 $tm
217 ${notify:+$nt}</td><td>`echo "$hte"|unhexize|htmlescape`
218 EOF
219 sed -e "s|^href=\([-A-Za-z0-9,.:/~_%#&+?]*\)|<a &>\1</a>|"
220 for i in $imgids; do
221 mrid=${i%%:*}; i=${i#*:}; sz=`size_h ${i%%:*}`
222 fn=`echo "${i#*:}"|unhexize`
223 fnb=$fn"(${sz})"
224 # echo r=$mrid fn=$fn
225 case "$fn" in
226 *.[Pp][Nn][Gg]|*.[Jj][Pp][Gg]|*.[Jj][Pp][Ee][Gg])
227 sq $db "SELECT hex(bin) FROM article_m WHERE rowid=$mrid" \
228 | unhexize \
229 | convert -define jpeg:size=100x100 -resize 100x100'>' - jpeg:- \
230 | hexize \
231 | sed -e 's/\(..\)/%\1/g' \
232 -e "s|^|<a href=\"$catlink+$mrid\"><img src=\"data:image/jpeg,|" \
233 -e "s|\$|\">$fnb</a>|"
234 ;;
235 *)
236 echo "<a href=\"$catlink+$mrid\"><img src=\"$deficon\">$fnb</a>"
237 ;;
238 esac
239 done
240 done
241 fi
243 textform='<div class="fold">
244 <input type="checkbox" id="cmt" checked><label for="cmt"
245 >コメントする</label><div>
246 <table class="b">
247 <tr><td><textarea name="text" cols="72" rows="4"></textarea></td></tr>
248 <tr><td>添付ファイル: <input type="file" name="image" multiple></td></tr>
249 </table>
250 <input type="submit" value="送信">
251 <input type="reset" value="リセット"></div></div>
252 '
253 echo '</table>'
255 $iswritable && cat<<EOF
256 <div class="blogcomment">
257 <!-- <p>コメント記入</p> -->
258 <input type="hidden" name="blogid" value="$id">
259 <input type="hidden" name="stage" value="replyblog">
260 <input type="hidden" name="serial" value="$serial">
261 $textform
262 </div>
263 EOF
264 echo "</form><p id=\"bottom\"></p>"
265 # Record access log
266 acclog blog $rowid
267 }
269 lshandout() {
270 # $1=rowid of blog
271 if ! blog_writable $1 $user; then
272 echo "メンバー以外は利用できません。" | html p; return
273 fi
274 time=`getvalbyid blog ctime $1|colrm 11`
275 owner=`getvalbyid blog owner $1`
276 title=`getvalbyid blog title $1`
277 ge=`gecos $owner`
278 lshandoutsub $owner "$@" \
279 |_m4 -D_TITLE_="提出状況" \
280 -D_FORMHEAD_="$time [$title]@${ge:-$owner}" \
281 -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
282 $layout/html.m4.html $layout/form+dump-whead.m4.html
283 }
284 lshandoutsub() {
285 # $1=owner $2=rowid of blog
286 if isgroup $1; then
287 sample="(select user from grp_mem where gname='$1')"
288 else
289 sample="(select distinct author as user from arts)"
290 echo "(集計は板への投稿者のみ)" | html p
291 fi
292 sql="with arts as (select id,author from article \
293 where blogid=(select id from blog where rowid=$2))\
294 select (select rowid from user where name=c0.user)||' '|| \
295 coalesce((select val from user_s where name=c0.user \
296 and key='gecos'),\
297 c0.user) as 'メンバー',\
298 sum(case when c1.key is not null then 1 else 0 end)\
299 as 'コメント記入',\
300 sum(case when c2.key is not null then 1 else 0 end)\
301 as 'ファイルの提出'\
302 from $sample c0 \
303 left join (select id,author from arts) a\
304 on c0.user=a.author\
305 left join (select id,key from article_s where key='text') c1\
306 on a.id=c1.id left join (select id,key from article_m ) c2\
307 on c1.id=c2.id group by c0.user order by c0.user;"
308 err ishandoutsub: sql="$sql"
309 echo '<table class="b td2r td3r">'
310 hrb="<a href=\"?home+"
311 echo "$sql" | sq -header -html $db \
312 | sed -e "s,\(<TR><TD>\)\([^ ]*\) \(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
313 echo '</table>'
314 }
315 gethandout() {
316 # $1=rowid of blog
317 if ! blog_writable $1 $user; then
318 echo "メンバー以外は利用できません。" | html p; return
319 fi
320 i=0
321 bd=$tmpd/archive.$$
322 mkdir $bd
323 query "select m.rowid,author,m.val from article a join article_m m\
324 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
325 and m.key in ('image', 'document', 'binary');" \
326 | while IFS='|' read rowid author filename; do
327 # err isfilereadable $user article_m $rowid
328 isfilereadable $user article_m $rowid || continue
329 # err ok
330 i=$((i+1))
331 dir=`printf $bd/%03d $i`
332 mkdir $dir
333 query "select quote(bin) from article_m where rowid=$rowid;" \
334 | unhexize > $dir/$filename
335 done
336 if [ ! -d $bd/001 ]; then
337 contenttype; echo
338 echo "取得できるファイルがありませんでした。" | html p
339 return
340 fi
341 (cd $bd
342 err cdto$bd; (pwd; ls -lFa) 1>&3
343 tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
344 )
345 arc=$bd/archive.tar.gz
346 echo "Content-type: application/x-gzip"
347 echo "Content-Length: `cat $arc|wc -c`"
348 echo "Content-Disposition: filename=\"archive.tar.gz\""
349 echo
350 cat $arc
351 }
352 lsmyfile() { # $1(optional)=SortBy
353 case "$1" in
354 ""|CTIME-DESC)
355 by="CTIME" ord="DESC" ;;
356 CTIME*) by="CTIME" ;;
357 FILE*) by="FILE" ;;
358 OWNER*) by="OWNER" ;;
359 TITLE*) by="TITLE" ;;
360 esac
361 case "$1" in
362 *DESC) ord="DESC" ;;
363 esac
364 case "$ord" in
365 DESC) lkod="" jord="降順" ;;
366 *) lkod="-DESC" jord="昇順" ;;
367 esac
368 sql="select m.val||'/'||m.rowid FILE,
369 coalesce(
370 case when (select name from user where name=bs.owner)
371 is not null
372 then (select val from user_s where name=bs.owner
373 and key='gecos')
374 when (select gname from grp where gname=bs.owner)
375 is not null
376 then (select val from grp_s where gname=bs.owner
377 and key='gecos')
378 else
379 null
380 end,
381 bs.owner
382 ) OWNER,
383 a_s.val CTIME,
384 ',t,'||bs.title||':'||b.rowid||'#'||a.id TITLE
385 from (select rowid,id,val from article_m where id
386 in (select id from article where author='$user')
387 and type like 'file:%')
388 m left join article a on m.id=a.id
389 left join article_s a_s on a.id=a_s.id and a_s.key='ctime'
390 left join (select id,
391 max(case key when 'owner' then val end) as owner,
392 max(case key when 'title' then val end) as title
393 from blog_s group by id)
394 bs on a.blogid=bs.id
395 left join blog b on bs.id=b.id
396 where m.val is not null order by $by $ord;"
397 err lshandoutbyauthor: sql=`echo "$sql"`
398 title="個人提出ファイル"
399 _m4 -D_TITLE_=$title $layout/html.m4.html
400 hra="<a href=\"?lsmyfile+"
401 hrb="<a href=\"?showattc+article_m+"
402 hrc="<a href=\"?replyblog+"
403 (echo '<table class="b">'
404 echo "$sql"|sq -html -header $db ) \
405 | sed -e "s|\(<TR><TD>\)\([^/]*\)/\([0-9]*\)|\1$hrb\3\">\2</a>|" \
406 -e "s|,t,\(.*\):\([^<]*\)\(</TD>\)|$hrc\2\">\1</a>\3|" \
407 -e "s|\(<TH>\)\([A-Z]*\)\(</TH>\)|\1$hra\2$lkod\">\2</a>|" \
408 | _m4 -D_TITLE_=$title -D_FORM_="<p>($by$jord)</p>" \
409 -D_DUMPTABLE_="syscmd(cat)" $layout/form+dump.m4.html
410 echo '</table>'
411 }
412 searchart() {
413 kwd=`getpar kwd`
414 if [ -z "$kwd" ]; then
415 echo "検索語を指定してください" | html p; return
416 fi
417 for k in `echo "$kwd" | sed "s/'/''/g"`; do
418 kc=$kc${kc:+" AND "}"s.val LIKE '%$k%'"
419 done
420 kwd=`echo "$kwd"|htmlescape`
421 owner=`getpar owner`
422 owner=${owner:-$1}
423 echo "「$kwd」による検索結果" | html p
424 if [ -n "$owner" ]; then
425 cond="where key='owner' and val='$owner'"
426 if isuser $owner; then
427 echo "(`linkhome $owner` さんの記録からの検索)" | html p
428 else
429 linkhome $owner 1>&3
430 echo "(`linkhome $owner` グループからの検索)" | html p
431 fi
432 fi
433 # article_s: id=article-id, key='text', val='TEXT'
434 # article: id=article-id, blogid=blogkd
435 # blog: id=blog-id, author=LeaderAuthor
436 # blog_s: id=blog-id, key='title', val='BLOG-TITLE'
437 # WANT: blog-ROWid,article-id,val(TEXT)
438 sql="select b.rowid||'#'||x.id as '',
439 b.title as TITLE,
440 substr(x.val, 0, 80) as TEXT
441 from (select blog.rowid,blog.*,bs.val as title from blog join blog_s bs
442 on blog.id=bs.id and bs.key='title') b
443 join
444 (select a.id,a.blogid,s.val from article a join article_s s
445 on a.id=s.id where $kc)
446 x on b.id=x.blogid
447 where b.id in (select id from blog_s
448 $cond);"
449 sedopt="s,<TR><TD>\([^<]*\)</TD>,<TR><TD><a\
450 href=\"?replyblog+\1\">VIEW</a></TD>,"
451 cat<<EOF
452 <table class="b searchart">
453 `sq -header -html $db "$sql"|sed "$sedopt"`
454 </table>
455 EOF
456 }
457 listblog() (
458 # $1={user,group}
459 qow=`sqlquote $1`
460 cond="where a.id in (select id from blog_s where key='owner' and val=$qow) order by ctime desc"
461 DT_CHLD=article:blogid
462 cgi_form searchart<<EOF
463 <label>`cgi_text kwd`という語を含む記事をこの一覧から検索</label>
464 `cgi_hidden owner $user`
465 EOF
466 dumptable html blog 'ctime title heading' "$cond"
467 )
469 blog_addentry() {
470 # $1=GRPname(if it is a group)
471 grprowid=$1
472 rowid=`getpar rowid`
473 err blog_addentry0: rowid=$rowid
474 if [ -n "$grprowid" ]; then
475 owner=`getgroupbyid $grprowid`
476 else
477 owner=`getpar owner`
478 fi
479 err blog-add: \$1=$1 rowid=$rowid owner=$owner
480 if isgroup $owner; then
481 groupmode=1 listing=$owner guide="[${owner}]" GF_OWNER=$owner
482 else
483 usermode=1 listing=$user guide="[個人]"
484 fi
486 if [ -n "`getpar title`" ]; then
487 if [ "$usermode" ]; then
488 err usermode: user=$user owner=$owner
489 if [ x"$user" != x"$owner" ]; then
490 echo "他人の日記は書けません" | html p
491 return 2
492 fi
493 elif [ "$groupmode" ]; then # if write to group log
494 grp=$owner #\`getpar grp\`
495 err ismember: $user $grp
496 if ! ismember "$user" "$grp"; then
497 echo "(話題作成はこのグループに加入してから)" | html p
498 return 3
499 fi
500 fi
501 par2table $formdir/blog.def
502 serial=`getpar serial`
503 err SERIAL: $serial ROWID=$rowid listing=$listing
504 id=""
505 if [ -n "$rowid" ]; then
506 # Here, id becomes NULL when removal of entries at par2table
507 id=`query "select rowid from blog where rowid=$rowid;"`
508 elif [ -n "$serial" ]; then
509 # If new blog leader created, traverse to its head.
510 id=`query "select rowid from blog where id='$serial';"`
511 err new-Leader: "select rowid from blog where id='$serial';" id=$id
512 fi
513 if [ -n "$id" ]; then
514 ## If new aritcle is entered, JUMP to blog_reply
515 blog_reply $id
516 return
517 fi
518 fi
519 echo "${guide}新規話題作成" > $tmpd/title.$$
520 listblog $listing > $tmpd/listblog.$$
521 genform $formdir/blog.def \
522 | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" \
523 -D_FORMHEAD_="序文は簡単に詳しくはコメントに" \
524 -D_DUMPHEAD_="これまでの蓄積" \
525 -D_FORM_="syscmd(\`cat')" \
526 -D_DUMPTABLE_="spaste(\`$tmpd/listblog.$$')" \
527 $layout/html.m4.html \
528 $layout/form+dump-whead.m4.html
529 }
531 blog_reply() {
532 rowid=$1
533 err rowid=$1
535 if [ -z "$rowid" ]; then
536 echo "表示する日記番号が未指定です。" | html p
537 return
538 fi
539 title=`getvalbyid blog title $rowid`
540 owner=`getvalbyid blog owner $rowid`
541 if isuser "$owner"; then
542 subtitle="`gecos $owner` さんの話題"
543 else
544 grprowid=`query "select rowid from grp where gname=\"$owner\";"`
545 subtitle="グループ <a href=\"?grp+$grprowid\">$owner</a> での話題"
546 fi
547 if [ -z "$title" ]; then
548 echo "日記番号指定が無効です。" | html p
549 return
550 fi
552 text=`getpar text`
553 if [ -n "$text" ]; then
554 if blog_writable $rowid $user; then
555 par2table $formdir/article.def
556 st=$? # ; err par2t-st=$st
557 case $st in
558 0|4)
559 [ "$st" = "4" ] && act="書込削除"
560 blog_notify_reply $rowid $user "$text" $act ;;
561 esac
562 else
563 title="$title(加入してないので書き込み不可)"
564 fi
565 fi
566 def=$formdir/article.def
567 echo "$title" > $tmpd/title.$$
568 echo "$subtitle" > $tmpd/subtitle.$$
569 ${BLOG_SHOW:-blog_showentry} blog $rowid \
570 | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" -D_BODYCLASS_=general \
571 -D_FORMHEAD_="spaste(\`$tmpd/subtitle.$$')" \
572 -D_FORM_='' \
573 -D_DUMPTABLE_="syscmd(cat)" -D_DUMPHEAD_="" \
574 $layout/html.m4.html $layout/form+dump-whead.m4.html
575 }