s4

view s4-blog.sh @ 80:39e69daa2071

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