Newer
Older
s4 / s4-blog.sh
#
type cgiinit >/dev/null 2>&1 || . ./s4-funcs.sh

blog_genform() {
  #
  t=$1 
}

blog_writable() (
  # $1=articleid $2=user
  blogowner=`getvalbyid blog owner "$1"`
  [ x"$blogowner" = x"$2" ] || isuser "$blogowner" || ismember "$2" "$blogowner"
)
blog_readable() {
  # $1=articleid $2=user
  mode=`getgroupattr $grp regmode`
}
blog_notify_reply() (
  # $1=blogid $2=ReplyingUser $3=WrittenText $4(optional)=Action
  blogowner=`getvalbyid blog owner "$1"`
  [ x"$2" = x"$blogowner" ] && return # If author=blogowner, unnecessary
  blogtitle=`getvalbyid blog title "$1"`
  blogurl="$urlbase?replyblog+$1"
  action=${4:-書き込み}
  mode=`getvalbyid blog notify "$1"`
  ### EXCEPT=`sqlquote "$user"`	## User should receive to feal some annoyance
  case $mode in
    admin)
      if isgroup "$blogowner"; then
	emails=`getgroupadminmails $blogowner`
      else
	emails=`collectemail $blogowner`
      fi
      notifyto=`getpar notifyto`
      if [ -n "$notifyto" ]; then
	emails=$emails" `email4groupbyuid \"$blogowner\" $notifyto`"
      fi
	;;
    no)		return ;;
    *) team=`query "SELECT val FROM blog_s
		    WHERE id=(SELECT id FROM blog WHERE rowid=$1)
			   AND key='team';"`
       # team cannot get `getvalbyid blog team "$1"` because it's not
       # defined in blog.def.  Yes, it is Illegal USE!!
       emails=`TEAM=$team collectemail $blogowner` ;;
  esac
  err notify: user=$user Admins=`getgroupadmins $blogowner` Mode=$mode Emails="[$emails]"
  SMAIL_TO="`echo "$blogowner" | nkf -jM | tr -d '\n'` readers <$admin>" \
	  smail "$emails" "${action}通知 $urlbase"<<EOF
[$blogtitle]板に${action}がありました。
場所: $blogurl
題目: $blogtitle
筆者: `gecos $2`
内容:
`echo "$3"|sed 's/^/> /'`
EOF
)

blog_showentry() {
  # $1=table $2=rowid
  # if [ -n "$2" ]; then
  #   if [ -n "$imgcached" ]; then
  #     bstmpdir=$tmpdir/$imgcached/$thumbxy
  #   else
  #     bstmpdir=$tmpd
  #     # tmpd=`mktempd`
  #     # tmpfiles=$tmpfiles" $tmpd"
  #   fi
  # fi
  td=`getcachedir "article/$2"`
  [ -d "$td" ] || mkdir -p $td
  tbl=${1%%[!A-Z0-9a-z_]*} rowid=${2%%[!A-Z0-9a-z_]*}
err rowid=$rowid, '$2'=$2
  ts=${tbl}_s tm=${tbl}_m
  at=article as=article_s am=article_m
  serial=$(($(date +%s)-1420038000))s$$
  blog_writable $rowid $user && iswritable=true || iswritable=false
  # This function grasps blog entry definiton directly.
  # blog:   id
  # blog_s: title,ctime,heading
  # blog_m: *article

  # 2015-10-05 check readable
  if ! $iswritable; then
    blogowner=`getvalbyid blog owner "$2"`
    # err blogowner=$blogowner
    if isgroup $blogowner; then
      regmode=`getgroupattr $blogowner regmode`
      # err regmode=$regmode
      if [ x"$regmode" = x"moderated" ]; then
	if ! ismember $user $blogowner; then
	  echo "加入してからどうぞ" | html p
	  return
	fi
      fi
    fi
  fi
  case `getvalbyid blog notify "$2"` in # "all", "admin" or "no" (or NULL)
    admin)	notifyto=1 ;;
    *)		notifyto="" ;;
  esac

  err "SELECT id from $tbl where rowid=$rowid"
  id=`query "select id from $tbl where rowid=$rowid;"`
err id=$id
err "select val from $ts where key='title' and id='$id';"


  #(1)Display root article
  cat<<EOF
<form class="replyblog" action="$myname?replyblog+${rowid}#bottom" method="POST" enctype="multipart/form-data">
<table class="bloghead">
EOF

  href="<a href=\"?editheading+$rowid\"> 編集 </a>"
  if $iswritable; then
    href2="<a href=\"?lshandout+$rowid\"> 提出状況 </a>"
    href3="(<a href=\"?gethandout+$rowid\">ファイル取得</a>)"
  fi
  href4='<a href="#bottom"> 末尾へ</a>'

  query<<-EOF |
	SELECT coalesce((SELECT "yes" FROM blog
			 WHERE rowid=$rowid AND author='$user'),
			 ''),
	       max(CASE key WHEN 'ctime' THEN val END) ctime,
	       max(CASE key WHEN 'heading' THEN hex(val) END) heading,
	       CASE (SELECT val FROM $ts WHERE key="mode" AND id="$id")
	       WHEN 'report-closed' THEN 'レポート提出用(closed)'
	       WHEN 'report-open' THEN 'レポート提出用(open)'
	       ELSE ''
	       END
	FROM $ts WHERE id='$id' GROUP BY id;
	EOF
  { IFS='|' read edit ctime hexhead blogtype
    cat<<-EOF
	<tr><td>${edit:+$href }$ctime $blogtype $href2$href3 $href4</td></tr>
	<tr class="preface">
	 <td>`echo "$hexhead"|unhexize|hreflink|minitbl`</td></tr>
	</table>

	<table class="blog_replies">
	EOF
  }
  lkhome="<a href=\"$myname?home" lke='">'
  lkedit="<a href=\"$myname?editart"
  hlink="$myname?home" elink="$myname?editart"
  catlink="$myname?showattc+article_m"
  deficon="img/file-icon.png"
  # 2016-08-15 Newer flag introduced
  atime=`query "SELECT time FROM acclog
		WHERE tbl='blog' AND tblrowid=$rowid AND user='$user';"`
  sq $db<<EOF |
WITH a_s AS (
  SELECT id,
         max(CASE key WHEN 'ctime' THEN val END) TIME,
         max(CASE key WHEN 'text' THEN val END) TEXT
  FROM article_s
  GROUP by id
)
SELECT	a.id,
	CASE author
	WHEN '$user' THEN a.rowid||'+'||$rowid
	ELSE ''
	END edit,
	CASE -- 「通知送信」ボタンの有無
	WHEN '$notifyto' = '' THEN ''	-- 不要モードならなし
	WHEN '$user' = author THEN ''	-- 筆者自身ならなし
	ELSE "yes"
	END notify,
	(SELECT rowid FROM user WHERE name=author) user_rid,
	coalesce((SELECT val FROM user_s
			 WHERE name=author AND key='gecos'),
			author) uname,
	(SELECT val FROM user_s WHERE name=author AND key='$iconcachekey')
	icon,
	a.rowid,
	s.TIME,
        CASE WHEN s.TIME > '$atime' THEN 'new' ELSE '' END newer,
	hex(s.TEXT),
	(SELECT group_concat(rowid||':'||length(bin)||':'||hex(val), ' ')
	 FROM article_m
	 WHERE id=a.id AND key='image') imxgids
FROM (select rowid,id,author from article where blogid in
     (select id from blog where rowid=$rowid)) a
  LEFT JOIN
     a_s s
  ON a.id=s.id;
EOF
  while IFS='|' read id edit notify uid uname icon aid tm new hte imgids; do
    nt="<label style=\"font-size: 70%;\"><input type=\"checkbox\"
	 name=\"notifyto\" value=\"$uid\">返信通知送信</label>"
    tdcls="repatt${new:+ new}"
    imgdir=`getcachedir home/"$uid"`/main
    if [ -n "$icon" -a -s "$icon" ]; then
      icfn=`echo "$icon"|htmlescape`
      picon="<p class=\"proficon\"><a href=\"$hlink+$uid\"><img src=\"$icfn\"></a></p>"
    else
      picon=""
      query "DELETE FROM user_s WHERE key='$iconcachekey' AND
		val=`sqlquotestr \"$icon\"`;"
    fi
    
    cat<<EOF
<tr id="$id">
<td class="$tdcls">$picon${edit:+<a href="$elink+$edit">編集</a> }#$aid
<a href="$hlink+$uid">$uname</a>
$tm
${notify:+$nt}</td>
EOF
    echo -n "<td id=\"$aid\" class=\"repl\">"
    echo "$hte"|unhexize|htmlescape|hreflink|minitbl
    usecache='' tsfile=$td/$id.stamp
    for i in $imgids; do
      mrid=${i%%:*}; i=${i#*:}; sz=`size_h ${i%%:*}`
      fn=`echo "${i#*:}"|unhexize`
      fnb=$fn"(${sz})"
      # echo r=$mrid fn=$fn
      case "$fn" in
	*.[Pp][Nn][Gg]|*.[Jj][Pp][Gg]|*.[Jj][Pp][Ee][Gg]|*.[Gg][Ii][Ff])
	  #  fmt=${fn##*.}	# convert - jpg:- is slow...why
	  case "$fn" in
	    *.[Pp][Nn][Gg])	fmt=png ;;
	    *.[Gg][Ii][Ff])	fmt=gif ;;
	    *)		fmt=jpeg ;;
	  esac
	  outfile=$td/$mrid-${fn%.*}.$fmt
	  #err fn=$fn outfile=$outfile
	  #err "usecache=$usecache `ls -l $outfile`" 
	  #err tm=$tm
	  #err tsfile=$tsfile=`cat $tsfile`
	  if [ -s $outfile ] &&	# $outfile should be > 0
		 { [ "$usecache" ] ||	# And usecache flag is true, or...
		       { [ -s "$tsfile" ] && [ x"`cat $tsfile`" = x"$tm" ]
		       };}; then
	    # err Set usecache=1
	    usecache=1		# Set usecache flag on
	    cat<<-EOF
		<a href="$catlink+$mrid"><img src="$outfile">
		$fnb</a>
		EOF
	  else
	    query "SELECT hex(bin) FROM article_m WHERE rowid=$mrid;" \
		| unhexize \
		| convert -define ${fmt}:size=100x100 -resize 100x100'>' \
			  - ${fmt}:- \
		| tee "$outfile" \
		| hexize \
		| sed -e 's/\(..\)/%\1/g' \
	    	      -e "s|^|<a href=\"$catlink+$mrid\"><img src=\"data:image/$fmt,|" \
		      -e "s|\$|\">$fnb</a>|"
	    # [ -d $td ] || mkdir -p "$td"
	    echo $tm > $tsfile
	  fi
	  ;;
	*)
	  echo "<a href=\"$catlink+$mrid\"><img src=\"$deficon\">$fnb</a>"
	  ;;
      esac
    done
    echo "</td></tr>"
  done

  textform='<div class="fold">
<input type="checkbox" id="cmt" checked><label for="cmt"
>コメントする</label><div>
<table class="b">
<tr><td><textarea name="text" cols="72" rows="4"></textarea></td></tr>
<tr><td>添付ファイル:
<input type="file" name="image"'" $file_accept multiple></td></tr>"'
</table>
<input type="submit" value="送信">
<input type="reset" value="リセット"></div></div>
'
  cat<<-EOF
	</table> <!-- end of s4-blog:blog_showentry() main table -->
	<p class="update_link"><a
	 href="?reload/$rowid">再読込</a></p>
	EOF
  query<<-EOF
	WITH mrt AS (
	 SELECT max(val) maxtime FROM article_s
	 WHERE key='ctime' AND id IN
		 (SELECT id FROM article
		  WHERE blogid=(select id from blog where rowid=$id))
	)
	SELECT 'A:'||
		(SELECT count(*) FROM tblaccesses
		 WHERE tbl='blog' AND tblrowid=$id) access,
	       'C:'||
	        (SELECT count(distinct user) FROM acclog
		 WHERE tbl='blog' AND tblrowid=$id
		       AND time > (SELECT maxtime from mrt)
		       AND user IN 
			  (SELECT user FROM grp_mem
			   WHERE gname = $(sqlquote "$blogowner")))
	       ;
	EOF
  $iswritable && cat<<-EOF
	<div class="blogcomment">
	<input type="hidden" name="blogid" value="$id">
	<input type="hidden" name="id" value="`genserial`">
	<input type="hidden" name="stage" value="replyblog">
	$textform
	</div>
	</form> <!-- End of s4-blog:blog_showentry() main form -->
	<p id="bottom"> </p>
	EOF
  # Record access log
  acclog blog $rowid
}

lshandout() {
  # $1=rowid of blog
  if ! blog_writable $1 $user; then
    echo "メンバー以外は利用できません。" | html p; return
  fi
  time=`getvalbyid blog ctime $1|colrm 11`
  owner=`getvalbyid blog owner $1`
  title=`getvalbyid blog title $1`
  ge=`gecos $owner`
  lshandoutsub $owner "$@" \
      |_m4 -D_TITLE_="提出状況" \
	   -D_FORMHEAD_="$time [$title]@${ge:-$owner}" \
	   -D_FORM_="syscmd(cat)" -D_DUMPHEAD_= -D_DUMPTABLE_= \
	   $layout/html.m4.html $layout/form+dump-whead.m4.html
}
lshandoutsub() {
  # $1=owner $2=rowid of blog
  if isgroup $1; then
    sample="(select user from grp_mem where gname='$1')"
  else
    sample="(select distinct author as user from arts)"
    echo "(集計は板への投稿者のみ)" | html p
  fi
  sql="with arts as (select id,author from article \
	    where blogid=(select id from blog where rowid=$2))\
      select (select rowid from user where name=c0.user)||'	'|| \
	      coalesce((select val from user_s where name=c0.user \
			and key='gecos'),\
		      c0.user) as 'メンバー',\
	     sum(case when c1.key is not null then 1 else 0 end)\
		 as 'コメント記入',\
	     sum(case when c2.key is not null then 1 else 0 end)\
		 as 'ファイルの提出'\
	 from $sample c0 \
	        left join (select id,author from arts) a\
		on c0.user=a.author\
	        left join (select id,key from article_s where key='text') c1\
		on a.id=c1.id left join (select id,key from article_m ) c2\
		on c1.id=c2.id group by c0.user order by c0.user;"
    err ishandoutsub: sql="$sql"
    echo '<table class="b td2r td3r">'
    hrb="<a href=\"?home+"
    echo "$sql" | sq -header -html $db \
	| sed -e "s,\(<TR><TD>\)\([^	]*\)	\(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
    echo '</table>'
}
gethandout() {
  # $1=rowid of blog
  if ! blog_writable $1 $user; then
    echo "メンバー以外は利用できません。" | html p; return
  fi
  i=0
  bd=$tmpd/archive.$$
  mkdir $bd
  query "select m.rowid,author,m.val from article a join article_m m\
	 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
	 and m.key in ('image', 'document', 'binary');" \
      | while IFS='|' read rowid author filename; do
	  # err isfilereadable $user article_m $rowid
	  isfilereadable $user article_m $rowid || continue
	  # err ok
	  i=$((i+1))
	  dir=`printf $bd/%03d $i`
	  mkdir $dir
	  query "select quote(bin) from article_m where rowid=$rowid;" \
	      | unhexize > $dir/$filename
	done
  if [ ! -d $bd/001 ]; then
    contenttype; echo
    echo "取得できるファイルがありませんでした。" | html p
    return
  fi
  (cd $bd
   err cdto$bd; (pwd; ls -lFa) 1>&3
   tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
  )
  arc=$bd/archive.tar.gz
  echo "Content-type: application/x-gzip"
  echo "Content-Length: `cat $arc|wc -c`"
  echo "Content-Disposition: filename=\"archive.tar.gz\""
  echo
  cat $arc
}
lsmyfile() {	# $1(optional)=SortBy
  case "$1" in
    ""|CTIME-DESC)
      		by="CTIME"	ord="DESC" ;;
    CTIME*)	by="CTIME"	;;
    FILE*)	by="FILE"	;;
    OWNER*)	by="OWNER"	;;
    TITLE*)	by="TITLE"	;;
  esac
  case "$1" in
    *DESC)	ord="DESC" ;;
  esac
  case "$ord" in
    DESC)	lkod="" 	jord="降順" ;;
    *)		lkod="-DESC"	jord="昇順" ;;
  esac
  sql="select m.val||'/'||m.rowid FILE,
	      coalesce(
		case when (select name from user where name=bs.owner)
			 is not null
		     then (select val from user_s where name=bs.owner
				and key='gecos')
		     when (select gname from grp where gname=bs.owner)
			 is not null
		     then (select val from grp_s where gname=bs.owner
				and key='gecos')
		     else
			null
		 end,
		 bs.owner
		) OWNER,
	      a_s.val CTIME,
	      ',t,'||bs.title||':'||b.rowid||'#'||a.id TITLE
	 from (select rowid,id,val from article_m where id
		 in (select id from article where author='$user')
		and type like 'file:%')
	m left join article a on m.id=a.id
	  left join article_s a_s on a.id=a_s.id and a_s.key='ctime'
	  left join (select id,
			max(case key when 'owner' then val end) as owner,
		        max(case key when 'title' then val end) as title
			from blog_s group by id)
		     bs on a.blogid=bs.id
	  left join blog b on bs.id=b.id
	  where m.val is not null order by $by $ord;"
  err lshandoutbyauthor: sql=`echo "$sql"`
  title="個人提出ファイル"
  _m4 -D_TITLE_=$title $layout/html.m4.html
  hra="<a href=\"?lsmyfile+"
  hrb="<a href=\"?showattc+article_m+"
  hrc="<a href=\"?replyblog+"
  (echo '<table class="b">'
   echo "$sql"|sq -html -header $db ) \
      | sed -e "s|\(<TR><TD>\)\([^/]*\)/\([0-9]*\)|\1$hrb\3\">\2</a>|" \
	    -e "s|,t,\(.*\):\([^<]*\)\(</TD>\)|$hrc\2\">\1</a>\3|" \
	    -e "s|\(<TH>\)\([A-Z]*\)\(</TH>\)|\1$hra\2$lkod\">\2</a>|" \
      | _m4 -D_TITLE_=$title -D_FORM_="<p>($by$jord)</p>" \
	    -D_DUMPTABLE_="syscmd(cat)" $layout/form+dump.m4.html
  echo '</table>'
}
searchart() {
  kwd=`getpar kwd|nkf -wZ1`		# Convert Zenkaku-SPC to ASCII-SPC
  kwdgrp=""
  if [ -z "$kwd" ]; then
    echo "検索語を指定してください" | html p; return
  fi
  if expr x"$kwd" : 'x#[1-9][0-9]*$' >/dev/null 1>&2; then
    # Like '#1234', assume as artID
    rowid=$((${kwd#\#} + 0))	# Force to be a number
    kc="a.rowid = $rowid"
  else
    for k in `echo "$kwd" | sed "s/'/''/g"`; do
      if expr x"$k" : 'x@[1-9][][0-9]*-[][0-9:-]*$' >/dev/null 1>&2; then
	# Like '@2016-10-10', expands to "s.ctime GLOB '@2016-10-10'"
	ctime=${k#@}
	# Not sure GROUP BY a.blogid is comfortable for searchers...?
	kc=$kc${kc:+" AND "}"s.ctime GLOB '${ctime}*'"
	##### kwdgrp=" GROUP BY a.blogid"
      elif [ x"$k" = x"@today" -o x"$k" = x"@今日" ]; then
	ctime=`date +%F`
	kc=$kc${kc:+" AND "}"s.ctime GLOB '${ctime}*'"
      else
	kc=$kc${kc:+" AND "}"s.text LIKE '%$k%'"
      fi
    done
  fi
  kwd=`echo "$kwd"|htmlescape`
  owner=`getpar owner`
  owner=${owner:-$1}
  echo "「$kwd」による検索結果" | html p
  if [ -n "$owner" ]; then
    cond="where key='owner' and val='$owner'"
    if isuser $owner; then
      echo "(`linkhome $owner` さんの記録からの検索)" | html p
    else
      linkhome $owner 1>&3
      echo "(`linkhome $owner` グループからの検索)" | html p
    fi
  fi
  # article_s:	id=article-id, key='text', val='TEXT'
  # article:	id=article-id, blogid=blogkd
  # blog:		id=blog-id, author=LeaderAuthor
  # blog_s:		id=blog-id, key='title', val='BLOG-TITLE'
  # WANT: blog-ROWid,article-id,val(TEXT)
    sql="`sql4readableblogs`		-- Extract user-readable blogs
       SELECT b.rid||'#'||x.id as '',
		  b.title as TITLE,
		  substr(x.ctime, 0, 11) as DATE,
		  substr(x.text, 0, 78) as TEXT
       FROM (SELECT blg.rid,blg.*,bs.val as title
	     FROM readableblogs blg JOIN blog_s bs
	          ON blg.id=bs.id AND bs.key='title') b
	  JOIN
	    (SELECT a.id,
		    a.blogid,
		    s.ctime,
		    s.text
	     FROM article a
	       JOIN
		  (SELECT id,  /* Create [id, ctime, text] from article_s */
			  max(CASE key WHEN 'ctime' THEN val END) ctime,
			  max(CASE key WHEN 'text' THEN val END) text
		   FROM article_s GROUP BY id) s
	       ON a.id=s.id WHERE $kc$kwdgrp)
	     x ON b.id=x.blogid
	 WHERE b.id IN (SELECT id FROM blog_s $cond)
       ORDER by DATE DESC, TITLE, x.ctime;"
  sedopt="s,<TR><TD>\([^<]*\)</TD>,<TR><TD><a\
 href=\"?replyblog+\1\">VIEW</a></TD>,"
  
  cat<<EOF
<table class="b searchart">
`sq -header -html $db "$sql"|sed "$sedopt"`
</table>
EOF
}
listblog() (
  # $1={user,group}
  qow=`sqlquote $1`
  cond="where a.id in (select id from blog_s where key='owner' and val=$qow) order by ctime desc"
  DT_CHLD=article:blogid
  cgi_form searchart<<EOF
<label>`cgi_text kwd`という語を含む記事をこの一覧から検索</label>
`cgi_hidden owner $user`
EOF
  dumptable html blog 'ctime title heading' "$cond"
)

blog_addentry() {
  # $1=GRPname(if it is a group)
  grprowid=$1
  rowid=`getpar rowid`
  err blog_addentry0: rowid=$rowid
  if [ -n "$grprowid" ]; then
    owner=`getgroupbyid $grprowid`
  else
    owner=`getpar owner`
  fi
  err blog-add: \$1=$1 rowid=$rowid owner=$owner
  if isgroup $owner; then
    groupmode=1 listing=$owner guide="[${owner}]" GF_OWNER=$owner
  else
    usermode=1 listing=$user guide="[個人]"
  fi

  if [ -n "`getpar title`" ]; then
    if [ "$usermode" ]; then
      err usermode: user=$user owner=$owner
      if [ x"$user" != x"$owner" ]; then
	echo "他人の日記は書けません" | html p
	return 2
      fi
    elif [ "$groupmode" ]; then	# if write to group log
      grp=$owner #\`getpar grp\`
      err ismember: $user $grp
      if ! ismember "$user" "$grp"; then
	echo "(話題作成はこのグループに加入してから)" | html p
	return 3
      fi
    fi
    par2table $formdir/blog.def
    serial=`getpar serial`
    err SERIAL: $serial ROWID=$rowid listing=$listing
    id=""
    if [ -n "$rowid" ]; then
      # Here, id becomes NULL when removal of entries at par2table
      id=`query "select rowid from blog where rowid=$rowid;"`
    elif [ -n "$serial" ]; then
      # If new blog leader created, traverse to its head.
      id=`query "select rowid from blog where id='$serial';"`
      err new-Leader: "select rowid from blog where id='$serial';" id=$id
    fi
    if [ -n "$id" ]; then
      ## If new aritcle is entered, JUMP to blog_reply
      blog_reply $id
      return
    fi
  fi
  echo "${guide}新規話題作成"	> $tmpd/title.$$
  listblog $listing		> $tmpd/listblog.$$
  genform $formdir/blog.def \
      | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" \
	    -D_FORMHEAD_="序文は簡単に詳しくはコメントに" \
	    -D_DUMPHEAD_="これまでの蓄積" \
	    -D_FORM_="syscmd(\`cat')" \
	    -D_DUMPTABLE_="spaste(\`$tmpd/listblog.$$')" \
	    $layout/html.m4.html \
	    $layout/form+dump-whead.m4.html
}

blog_reply() {
  rowid=$1
  err  rowid=$1

  if [ -z "$rowid" ]; then
    echo "表示する日記番号が未指定です。" | html p
    return
  fi
  title=`getvalbyid blog title $rowid`
  owner=`getvalbyid blog owner $rowid`
  if isuser "$owner"; then
    subtitle="`gecos $owner` さんの話題"
  else
    grprowid=`query "select rowid from grp where gname=\"$owner\";"`
    subtitle="グループ <a href=\"?grp+$grprowid\">$owner</a> での話題
    	`query \"SELECT printf('(チーム:%s)', val)\
		 FROM blog_s
		 WHERE id=(SELECT id FROM blog WHERE rowid=$rowid)
		       AND key='team';
	 \"|htmlescape`"
    memclass=`grp_getbodyclass "$owner"`
  fi
  if [ -z "$title" ]; then
    echo "日記番号指定が無効です。" | html p
    return
  fi

  text=`getpar text`
  if [ -n "$text" ]; then
    if blog_writable $rowid $user; then
      par2table $formdir/article.def
      st=$?		# ; err par2t-st=$st
      case $st in
	0|4)
	  [ "$st" = "4" ] && act="書込削除"
	  blog_notify_reply $rowid $user "$text" $act ;;
      esac
    else
      title="$title(加入してないので書き込み不可)"
    fi
  fi
  def=$formdir/article.def
  echo "$title" > $tmpd/title.$$
  echo "$subtitle" > $tmpd/subtitle.$$
  ${BLOG_SHOW:-blog_showentry} blog $rowid \
      | _m4 -D_TITLE_="spaste(\`$tmpd/title.$$')" \
	    -D_BODYCLASS_=general"${memclass:+ $memclass}" \
	    -D_FORMHEAD_="spaste(\`$tmpd/subtitle.$$')" \
	    -D_FORM_='' \
	    -D_DUMPTABLE_="syscmd(cat)" -D_DUMPHEAD_="" \
	    $layout/html.m4.html $layout/form+dump-whead.m4.html
}