s4

view s4-blog.sh @ 153:f7b4f7e5df2a

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