plugin_comment.patch

サマリcomment.inc.phpの機能を拡張する
リビジョン2.0
対応バージョン1.5 (branch_r1_5)
投稿者NoName?
投稿日2016-01-16 (土) 14:06:49

標準のcomment.inc.phpの機能を拡張します diff形式なのでpatchコマンドもしくは手動で書き換えてください。

commentプラグインに名前の入力を強制する forcename オプションを追加します ついでにコメント無しで投稿した場合にもエラー文を出すようにしました

従来の noname オプションと併用した場合は noname の方を優先して名前は必要ありません

--- ./plugin.org/comment.inc.php	2016-01-11 01:22:46.000000000 +0900
+++ ./plugin/comment.inc.php	2016-01-20 16:35:39.744467900 +0900
@@ -25,20 +25,37 @@ function plugin_comment_action()
 
 	if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
 
-	if (! isset($vars['msg'])) return array('msg'=>'', 'body'=>''); // Do nothing
+	$vars['page'] = $vars['refer'];
+	$_num = '#'.$vars['comment_no'];
+	$vars['_error'][$_num] = 'コメントがありません';
+	$vars['_name'][$_num] = $vars['name'];
+	$vars['_msg'][$_num] = $vars['msg'];
+
 
-	$vars['msg'] = str_replace("\n", '', $vars['msg']); // Cut LFs
+	if (! isset($vars['_msg'][$_num])) return array('msg'=>'', 'body'=>''); // Do nothing
+
+	$vars['_msg'][$_num] = str_replace("\n", '', $vars['_msg'][$_num]); // Cut LFs
 	$head = '';
 	$match = array();
-	if (preg_match('/^(-{1,2})-*\s*(.*)/', $vars['msg'], $match)) {
+	if (preg_match('/^(-{1,2})-*\s*(.*)/', $vars['_msg'][$_num], $match)) {
 		$head        = & $match[1];
-		$vars['msg'] = & $match[2];
+		$vars['_msg'][$_num] = & $match[2];
 	}
-	if ($vars['msg'] == '') return array('msg'=>'', 'body'=>''); // Do nothing
+	if ($vars['_msg'][$_num] == '') return array('msg'=>'', 'body'=>''); // Do nothing
+
+	$vars['_error'][$_num] = '「お名前」の入力をおねがいします';
 
-	$comment  = str_replace('$msg', $vars['msg'], PLUGIN_COMMENT_FORMAT_MSG);
-	if(isset($vars['name']) || ($vars['nodate'] != '1')) {
-		$_name = (! isset($vars['name']) || $vars['name'] == '') ? $_no_name : $vars['name'];
+	$comment  = str_replace('$msg', $vars['_msg'][$_num], PLUGIN_COMMENT_FORMAT_MSG);
+	if(isset($vars['_name'][$_num]) || ($vars['nodate'] != '1')) {
+		$_name = (! isset($vars['_name'][$_num]) || $vars['_name'][$_num] == '') ? $_no_name : $vars['name'];
+		if ($vars['forcename'] && $_name == $_no_name) {
+			$vars['_msg'][$_num] = htmlsc($vars['_msg'][$_num]);
+			return array('msg'=>'', 'body'=>'');
+		} else {
+			$vars['_error'][$_num] = '';
+			$vars['_msg'][$_num] = '';
+			$vars['_name'][$_num] = $vars['forcename'] ? $_name : '';
+		}
 		$_name = ($_name == '') ? '' : str_replace('$name', $_name, PLUGIN_COMMENT_FORMAT_NAME);
 		$_now  = ($vars['nodate'] == '1') ? '' :
 			str_replace('$now', $now, PLUGIN_COMMENT_FORMAT_NOW);
@@ -78,8 +95,6 @@ function plugin_comment_action()
 	$retvars['msg']  = $title;
 	$retvars['body'] = $body;
 
-	$vars['page'] = $vars['refer'];
-
 	return $retvars;
 }
 
@@ -94,6 +109,10 @@ function plugin_comment_convert()
 	if (! isset($numbers[$vars['page']])) $numbers[$vars['page']] = 0;
 	$comment_no = $numbers[$vars['page']]++;
 
+	$_name = $vars['_name']['#'.$comment_no];
+	$_msg = $vars['_msg']['#'.$comment_no];
+	$_error = $vars['_error']['#'.$comment_no];
+
 	$options = func_num_args() ? func_get_args() : array();
 	if (in_array('noname', $options)) {
 		$nametags = '<label for="_p_comment_comment_' . $comment_no . '">' .
@@ -103,31 +122,39 @@ function plugin_comment_convert()
 			$_btn_name . '</label>' .
 			'<input type="text" name="name" id="_p_comment_name_' .
 			$comment_no .  '" size="' . PLUGIN_COMMENT_SIZE_NAME .
+			'" value="'.htmlsc($_name).
 			'" />' . "\n";
 	}
+	$forcename = (in_array('forcename', $options) && !in_array('noname', $options))? '1' : '0';
 	$nodate = in_array('nodate', $options) ? '1' : '0';
 	$above  = in_array('above',  $options) ? '1' :
 		(in_array('below', $options) ? '0' : PLUGIN_COMMENT_DIRECTION_DEFAULT);
 
 	$script = get_script_uri();
 	$s_page = htmlsc($vars['page']);
+	$_error = $_error ? "<p> ERROR: ".htmlsc($_error)."</p>" : "";
 	$string = <<<EOD
 <br />
-<form action="$script" method="post">
+<form action="$script?cmd=comment&refer=$s_page" method="post">
  <div>
+  <!--
   <input type="hidden" name="plugin" value="comment" />
   <input type="hidden" name="refer"  value="$s_page" />
+  -->
   <input type="hidden" name="comment_no" value="$comment_no" />
+  <input type="hidden" name="forcename"  value="$forcename" />
   <input type="hidden" name="nodate" value="$nodate" />
   <input type="hidden" name="above"  value="$above" />
   <input type="hidden" name="digest" value="$digest" />
+  $_error
   $nametags
-  <input type="text"   name="msg" id="_p_comment_comment_{$comment_no}" size="$comment_cols" />
+  <input type="text"   name="msg" id="_p_comment_comment_{$comment_no}" size="$comment_cols" value="{$_msg}" />
   <input type="submit" name="comment" value="$_btn_comment" />
  </div>
 </form>
 EOD;
 
+	$_name = $_msg = $_error = '';
 	return $string;
 }
 ?>

バグなど

  • 複数実行したときにエラー文がおかしくなる問題を修正 -- 2016-01-20 (水) 16:40:21


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-01-20 (水) 16:40:22
Site admin: PukiWiki Development Team

PukiWiki 1.5.2+ © 2001-2019 PukiWiki Development Team. Powered by PHP 5.6.40-0+deb8u7. HTML convert time: 0.184 sec.

OSDN