s4

view s4-blog.sh @ 206:c761ce8ff963

Quoting changed
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 25 Apr 2016 19:46:25 +0859
parents 6c63ea1b65d5
children 36b6354de5cb
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>添付ファイル:
249 <input type="file" name="image"'" $file_accept multiple></td></tr>"'
250 </table>
251 <input type="submit" value="送信">
252 <input type="reset" value="リセット"></div></div>
253 '
254 echo '</table>'
256 $iswritable && cat<<EOF
257 <div class="blogcomment">
258 <!-- <p>コメント記入</p> -->
259 <input type="hidden" name="blogid" value="$id">
260 <input type="hidden" name="stage" value="replyblog">
261 <input type="hidden" name="serial" value="$serial">
262 $textform
263 </div>
264 EOF
265 echo "</form><p id=\"bottom\"></p>"
266 # Record access log
267 acclog blog $rowid
268 }
270 lshandout() {
271 # $1=rowid of blog
272 if ! blog_writable $1 $user; then
273 echo "メンバー以外は利用できません。" | html p; return
274 fi
275 time=`getvalbyid blog ctime $1|colrm 11`
276 owner=`getvalbyid blog owner $1`
277 title=`getvalbyid blog title $1`
278 ge=`gecos $owner`
279 lshandoutsub $owner "$@" \
280 |_m4 -D_TITLE_="提出状況" \
281 -D_FORMHEAD_="$time [$title]@${ge:-$owner}" \
282 -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
283 $layout/html.m4.html $layout/form+dump-whead.m4.html
284 }
285 lshandoutsub() {
286 # $1=owner $2=rowid of blog
287 if isgroup $1; then
288 sample="(select user from grp_mem where gname='$1')"
289 else
290 sample="(select distinct author as user from arts)"
291 echo "(集計は板への投稿者のみ)" | html p
292 fi
293 sql="with arts as (select id,author from article \
294 where blogid=(select id from blog where rowid=$2))\
295 select (select rowid from user where name=c0.user)||' '|| \
296 coalesce((select val from user_s where name=c0.user \
297 and key='gecos'),\
298 c0.user) as 'メンバー',\
299 sum(case when c1.key is not null then 1 else 0 end)\
300 as 'コメント記入',\
301 sum(case when c2.key is not null then 1 else 0 end)\
302 as 'ファイルの提出'\
303 from $sample c0 \
304 left join (select id,author from arts) a\
305 on c0.user=a.author\
306 left join (select id,key from article_s where key='text') c1\
307 on a.id=c1.id left join (select id,key from article_m ) c2\
308 on c1.id=c2.id group by c0.user order by c0.user;"
309 err ishandoutsub: sql="$sql"
310 echo '<table class="b td2r td3r">'
311 hrb="<a href=\"?home+"
312 echo "$sql" | sq -header -html $db \
313 | sed -e "s,\(<TR><TD>\)\([^ ]*\) \(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
314 echo '</table>'
315 }
316 gethandout() {
317 # $1=rowid of blog
318 if ! blog_writable $1 $user; then
319 echo "メンバー以外は利用できません。" | html p; return
320 fi
321 i=0
322 bd=$tmpd/archive.$$
323 mkdir $bd
324 query "select m.rowid,author,m.val from article a join article_m m\
325 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
326 and m.key in ('image', 'document', 'binary');" \
327 | while IFS='|' read rowid author filename; do
328 # err isfilereadable $user article_m $rowid
329 isfilereadable $user article_m $rowid || continue
330 # err ok
331 i=$((i+1))
332 dir=`printf $bd/%03d $i`
333 mkdir $dir
334 query "select quote(bin) from article_m where rowid=$rowid;" \
335 | unhexize > $dir/$filename
336 done
337 if [ ! -d $bd/001 ]; then
338 contenttype; echo
339 echo "取得できるファイルがありませんでした。" | html p
340 return
341 fi
342 (cd $bd
343 err cdto$bd; (pwd; ls -lFa) 1>&3
344 tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
345 )
346 arc=$bd/archive.tar.gz
347 echo "Content-type: application/x-gzip"
348 echo "Content-Length: `cat $arc|wc -c`"
349 echo "Content-Disposition: filename=\"archive.tar.gz\""
350 echo
351 cat $arc
352 }
353 lsmyfile() { # $1(optional)=SortBy
354 case "$1" in
355 ""|CTIME-DESC)
356 by="CTIME" ord="DESC" ;;
357 CTIME*) by="CTIME" ;;
358 FILE*) by="FILE" ;;
359 OWNER*) by="OWNER" ;;
360 TITLE*) by="TITLE" ;;
361 esac
362 case "$1" in
363 *DESC) ord="DESC" ;;
364 esac
365 case "$ord" in
366 DESC) lkod="" jord="降順" ;;
367 *) lkod="-DESC" jord="昇順" ;;
368 esac
369 sql="select m.val||'/'||m.rowid FILE,
370 coalesce(
371 case when (select name from user where name=bs.owner)
372 is not null
373 then (select val from user_s where name=bs.owner
374 and key='gecos')
375 when (select gname from grp where gname=bs.owner)
376 is not null
377 then (select val from grp_s where gname=bs.owner
378 and key='gecos')
379 else
380 null
381 end,
382 bs.owner
383 ) OWNER,
384 a_s.val CTIME,
385 ',t,'||bs.title||':'||b.rowid||'#'||a.id TITLE
386 from (select rowid,id,val from article_m where id
387 in (select id from article where author='$user')
388 and type like 'file:%')
389 m left join article a on m.id=a.id
390 left join article_s a_s on a.id=a_s.id and a_s.key='ctime'
391 left join (select id,
392 max(case key when 'owner' then val end) as owner,
393 max(case key when 'title' then val end) as title
394 from blog_s group by id)
395 bs on a.blogid=bs.id
396 left join blog b on bs.id=b.id
397 where m.val is not null order by $by $ord;"
398 err lshandoutbyauthor: sql=`echo "$sql"`
399 title="個人提出ファイル"
400 _m4 -D_TITLE_=$title $layout/html.m4.html
401 hra="<a href=\"?lsmyfile+"
402 hrb="<a href=\"?showattc+article_m+"
403 hrc="<a href=\"?replyblog+"
404 (echo '<table class="b">'
405 echo "$sql"|sq -html -header $db ) \
406 | sed -e "s|\(<TR><TD>\)\([^/]*\)/\([0-9]*\)|\1$hrb\3\">\2</a>|" \
407 -e "s|,t,\(.*\):\([^<]*\)\(</TD>\)|$hrc\2\">\1</a>\3|" \
408 -e "s|\(<TH>\)\([A-Z]*\)\(</TH>\)|\1$hra\2$lkod\">\2</a>|" \
409 | _m4 -D_TITLE_=$title -D_FORM_="<p>($by$jord)</p>" \
410 -D_DUMPTABLE_="syscmd(cat)" $layout/form+dump.m4.html
411 echo '</table>'
412 }
413 searchart() {
414 kwd=`getpar kwd`
415 if [ -z "$kwd" ]; then
416 echo "検索語を指定してください" | html p; return
417 fi
418 for k in `echo "$kwd" | sed "s/'/''/g"`; do
419 kc=$kc${kc:+" AND "}"s.val LIKE '%$k%'"
420 done
421 kwd=`echo "$kwd"|htmlescape`
422 owner=`getpar owner`
423 owner=${owner:-$1}
424 echo "「$kwd」による検索結果" | html p
425 if [ -n "$owner" ]; then
426 cond="where key='owner' and val='$owner'"
427 if isuser $owner; then
428 echo "(`linkhome $owner` さんの記録からの検索)" | html p
429 else
430 linkhome $owner 1>&3
431 echo "(`linkhome $owner` グループからの検索)" | html p
432 fi
433 fi
434 # article_s: id=article-id, key='text', val='TEXT'
435 # article: id=article-id, blogid=blogkd
436 # blog: id=blog-id, author=LeaderAuthor
437 # blog_s: id=blog-id, key='title', val='BLOG-TITLE'
438 # WANT: blog-ROWid,article-id,val(TEXT)
439 sql="select b.rowid||'#'||x.id as '',
440 b.title as TITLE,
441 substr(x.val, 0, 80) as TEXT
442 from (select blog.rowid,blog.*,bs.val as title from blog join blog_s bs
443 on blog.id=bs.id and bs.key='title') b
444 join
445 (select a.id,a.blogid,s.val from article a join article_s s
446 on a.id=s.id where $kc)
447 x on b.id=x.blogid
448 where b.id in (select id from blog_s
449 $cond);"
450 sedopt="s,<TR><TD>\([^<]*\)</TD>,<TR><TD><a\
451 href=\"?replyblog+\1\">VIEW</a></TD>,"
452 cat<<EOF
453 <table class="b searchart">
454 `sq -header -html $db "$sql"|sed "$sedopt"`
455 </table>
456 EOF
457 }
458 listblog() (
459 # $1={user,group}
460 qow=`sqlquote $1`
461 cond="where a.id in (select id from blog_s where key='owner' and val=$qow) order by ctime desc"
462 DT_CHLD=article:blogid
463 cgi_form searchart<<EOF
464 <label>`cgi_text kwd`という語を含む記事をこの一覧から検索</label>
465 `cgi_hidden owner $user`
466 EOF
467 dumptable html blog 'ctime title heading' "$cond"
468 )
470 blog_addentry() {
471 # $1=GRPname(if it is a group)
472 grprowid=$1
473 rowid=`getpar rowid`
474 err blog_addentry0: rowid=$rowid
475 if [ -n "$grprowid" ]; then
476 owner=`getgroupbyid $grprowid`
477 else
478 owner=`getpar owner`
479 fi
480 err blog-add: \$1=$1 rowid=$rowid owner=$owner
481 if isgroup $owner; then
482 groupmode=1 listing=$owner guide="[${owner}]" GF_OWNER=$owner
483 else
484 usermode=1 listing=$user guide="[個人]"
485 fi
487 if [ -n "`getpar title`" ]; then
488 if [ "$usermode" ]; then
489 err usermode: user=$user owner=$owner
490 if [ x"$user" != x"$owner" ]; then
491 echo "他人の日記は書けません" | html p
492 return 2
493 fi
494 elif [ "$groupmode" ]; then # if write to group log
495 grp=$owner #\`getpar grp\`
496 err ismember: $user $grp
497 if ! ismember "$user" "$grp"; then
498 echo "(話題作成はこのグループに加入してから)" | html p
499 return 3
500 fi
501 fi
502 par2table $formdir/blog.def
503 serial=`getpar serial`
504 err SERIAL: $serial ROWID=$rowid listing=$listing
505 id=""
506 if [ -n "$rowid" ]; then
507 # Here, id becomes NULL when removal of entries at par2table
508 id=`query "select rowid from blog where rowid=$rowid;"`
509 elif [ -n "$serial" ]; then
510 # If new blog leader created, traverse to its head.
511 id=`query "select rowid from blog where id='$serial';"`
512 err new-Leader: "select rowid from blog where id='$serial';" id=$id
513 fi
514 if [ -n "$id" ]; then
515 ## If new aritcle is entered, JUMP to blog_reply
516 blog_reply $id
517 return
518 fi
519 fi
520 echo "${guide}新規話題作成" > $tmpd/title.$$
521 listblog $listing > $tmpd/listblog.$$
522 genform $formdir/blog.def \
523 | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" \
524 -D_FORMHEAD_="序文は簡単に詳しくはコメントに" \
525 -D_DUMPHEAD_="これまでの蓄積" \
526 -D_FORM_="syscmd(\`cat')" \
527 -D_DUMPTABLE_="spaste(\`$tmpd/listblog.$$')" \
528 $layout/html.m4.html \
529 $layout/form+dump-whead.m4.html
530 }
532 blog_reply() {
533 rowid=$1
534 err rowid=$1
536 if [ -z "$rowid" ]; then
537 echo "表示する日記番号が未指定です。" | html p
538 return
539 fi
540 title=`getvalbyid blog title $rowid`
541 owner=`getvalbyid blog owner $rowid`
542 if isuser "$owner"; then
543 subtitle="`gecos $owner` さんの話題"
544 else
545 grprowid=`query "select rowid from grp where gname=\"$owner\";"`
546 subtitle="グループ <a href=\"?grp+$grprowid\">$owner</a> での話題"
547 fi
548 if [ -z "$title" ]; then
549 echo "日記番号指定が無効です。" | html p
550 return
551 fi
553 text=`getpar text`
554 if [ -n "$text" ]; then
555 if blog_writable $rowid $user; then
556 par2table $formdir/article.def
557 st=$? # ; err par2t-st=$st
558 case $st in
559 0|4)
560 [ "$st" = "4" ] && act="書込削除"
561 blog_notify_reply $rowid $user "$text" $act ;;
562 esac
563 else
564 title="$title(加入してないので書き込み不可)"
565 fi
566 fi
567 def=$formdir/article.def
568 echo "$title" > $tmpd/title.$$
569 echo "$subtitle" > $tmpd/subtitle.$$
570 ${BLOG_SHOW:-blog_showentry} blog $rowid \
571 | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" -D_BODYCLASS_=general \
572 -D_FORMHEAD_="spaste(\`$tmpd/subtitle.$$')" \
573 -D_FORM_='' \
574 -D_DUMPTABLE_="syscmd(cat)" -D_DUMPHEAD_="" \
575 $layout/html.m4.html $layout/form+dump-whead.m4.html
576 }