s4

view s4-blog.sh @ 64:54bbd7ce3766

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