s4

view s4-blog.sh @ 43:7defa2416356

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