s4

view s4-blog.sh @ 35:894f93ef24fe

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