s4

view s4-blog.sh @ 58:ccd0c6736215

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