-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvote.inc.php
286 lines (277 loc) · 9.63 KB
/
tvote.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
// $Id: tvote.inc.php,v 0.25 2005/02/12 18:00:00 r.tokiwa Exp $
# ソート・追加対応PukiWiki投票プラグイン
# Note: based on vote.inc.php,v 1.14 2003/07/03 05:28:04 arino
# Copyright: Ryota Tokiwa
# License: GPL
# Uage: See README.TXT
// v0.26 PHP8対応 2021-12-17 byはいふん
# 使用目的に合わせて定数を調整してください。
# 投票前に得票数を表示する(TRUE)/しない(FALSE)
define('_TVOTE_OPENCOUNT',TRUE);
# ページのタイムスタンプを更新をしない(TRUE)/する(FALSE)。
define('_TVOTE_NOTIMESTAMP',TRUE);
# クッキーに保存する投票数の上限。100以下にしてください。
define('_TVOTE_VOTEDLIST_MAX','30');
# クッキーの保存日数。
define('_TVOTE_VALIDDAYS',180);
# ソートする(TRUE)/しない(FALSE)
define('_TVOTE_SORT',TRUE);
# その他のフォームを表示する(TRUE)/しない(FALSE)
define('_TVOTE_ADD',TRUE);
# ページを凍結すると投票は締め切る(TRUE)/継続する(FALSE)
define('_TVOTE_FREEZE_SYNC',TRUE);
# MD5ダイジェストによる更新衝突チェックを行う(TURE)/行わない(FALSE)。
define('_TVOTE_MD5_CHECK',FALSE);
########################################
# 通常の使用では、これ以降を編集する必要はありません。
#クッキー用データデリミタ
define('C_DELIM','-');
function plugin_tvote_action()
{
global $post,$vars,$script,$cols,$rows;
global $_title_collided,$_msg_collided,$_title_updated,$_title;
global $_vote_plugin_choice, $_vote_plugin_votes;
# $vars['tvote_info_'.$tvote_no] .= "debug";
$timestamp=_TVOTE_NOTIMESTAMP;
$votedlist_max=_TVOTE_VOTEDLIST_MAX;
$postdata_old = get_source($post['refer']);
$tvote_no = -1;
$title = $body = $postdata = '';
// ページデータの処理
foreach($postdata_old as $line) {
// tvote要素があるか検索し、あれば取り出す。
if (!preg_match("/^#tvote\((.*)\)\s*$/",$line,$arg)) {
$postdata .= $line;
continue;
}
// POSTされたtvoteかどうか検査
if (++$tvote_no != $post['tvote_no']) {
$postdata .= $line;
continue;
}
//プラグイン引数取り出し
$args = csv_explode(',', $arg[1]);
$items = null;
$tvoteflag=false;
$_votedlist=explode(C_DELIM, $_COOKIE['tvote_'.$post['refer'].'_'.$post['tvote_no']]);
foreach( $_votedlist as $key ) {
if (preg_match("/^([\da-f]{8})$/i",$key,$match))
$votedlist[$match[1]]=1;
}
//引数処理
foreach($args as $arg) {
$cnt = 0;
if (preg_match("/^(.+)\[([^\[]*)\]$/",$arg,$match)) {
$arg = $match[1];
$cnt = intval($match[2]);
if (!is_int($cnt) || $cnt<0) $cnt=0;
//引数の先頭文字が+ならはオプションとして $opt[$arg]=$cnt;
}
$e_arg = encode($arg);
if (!empty($post["tvote_$e_arg"]) and $post["tvote_$e_arg"] == $_vote_plugin_votes) {
//投票していないかチェック
//初投票ならカウント&クッキーセット
$hash=sprintf('%08x',crc32($arg));
$tvoteflag=true;
if (!$votedlist[$hash]) {
if ($cnt<0x7FFFFFFF) $cnt++;
$votedlist[$hash]=1;
}
}
if(!empty($arg)) {
$items[$arg] = array($cnt,$arg);
}
}
// その他の場合
if ((!empty($post['add_submit']) || !$tvoteflag) && !empty($post['tvote_add'])) {
$add = htmlsc($post['tvote_add']);
# $trans_tbl = array ('"' => '"');
# $add = strtr($post['tvote_add'],$trans_tbl);
$hash=sprintf('%08x',crc32($add));
if (is_null($items[$add])) {
$items[$add] = array(1,$add);
$votedlist[$hash]=1;
} else {
if (!$votedlist[$hash]) {
if ($items[$add][0]<0x7FFFFFFF) $items[$add][0]++;
$votedlist[$hash]=1;
}
}
}
if (count($votedlist)>$votedlist_max) array_shift($votedlist);
$new_value=@join(C_DELIM,array_keys($votedlist));
$_COOKIE['tvote_'.$post['refer'].'_'.$post['tvote_no']]=$new_value;
if (_TVOTE_SORT) {
// スコア降順ソート&キー昇順ソート
//$cmpfunc = create_function('$a, $b', 'return ($a[0]==$b[0]?strcasecmp($a[1],$b[1]):$b[0]-$a[0]);');
$cmpfunc = function($a, $b) {return ($a[0]==$b[0]?strcasecmp($a[1],$b[1]):$b[0]-$a[0]);};
uasort($items,$cmpfunc);
}
foreach ($items as $key => $value) {
$votes[] = '"'.$key.'['.$value[0].']"';
}
// オプション($opt[$arg]) を $votesのあとに追加;
$tvote_str = '#tvote('.@join(',',$votes).")\n";
$postdata_input = $tvote_str;
$postdata .= $tvote_str;
}
if (_TVOTE_MD5_CHECK && (md5(@join('',get_source($post['refer']))) != $post['digest'])) {
$title = $_title_collided;
$s_refer = htmlsc($post['refer']);
$s_digest = htmlsc($post['digest']);
$s_postdata_input = htmlsc($postdata_input);
$body = <<<EOD
$_msg_collided
<form action="$script?cmd=preview" method="post">
<div>
<input type="hidden" name="refer" value="$s_refer" />
<input type="hidden" name="digest" value="$s_digest" />
<textarea name="msg" rows="$rows" cols="$cols" id="textarea">$s_postdata_input</textarea><br />
</div>
</form>
EOD;
} else {
page_write($post['refer'],$postdata,$timestamp);
$title = $_title_updated;
}
$retvars['msg'] = $title;
$retvars['body'] = $body;
$post['page'] = $post['refer'];
$vars['page'] = $post['refer'];
return $retvars;
}
function plugin_tvote_convert()
{
global $script,$vars,$digest;
global $_vote_plugin_choice, $_vote_plugin_votes;
static $numbers = array();
$style = 'padding-left:0.5em;padding-right:0.5em';
// $vars['tvote_info_'.$tvote_no] .= "debug";
// PukiWikiのバグ?対策 action時にページ名でなしで呼ばれる。
if (empty($vars['page']))
return '';
if (!array_key_exists($vars['page'],$numbers))
$numbers[$vars['page']] = 0;
$tvote_no = $numbers[$vars['page']]++;
$args = func_get_args();
$s_page = htmlsc($vars['page']);
$s_digest = htmlsc($digest);
$_votedlist=explode(C_DELIM, $_COOKIE['tvote_'.$s_page.'_'.$tvote_no]);
foreach( $_votedlist as $key ) {
if (preg_match("/^([\da-f]{8})$/i",$key,$match))
$votedlist[$match[1]]=1;
}
$view_count=_TVOTE_OPENCOUNT || ($votedlist) || (is_freeze($vars['page']) && _TVOTE_FREEZE_SYNC);
if($view_count) {
$votecount_head='得票数';
$votepercent_head='得票率';
}
$body = <<<EOD
<a id="tvote$tvote_no"></a>
<form action="$script#tvote$tvote_no" method="post">
<table cellspacing="0" cellpadding="2" class="style_table" summary="tvote">
<tr>
<td align="left" class="vote_label" style="padding-left:0.5em;padding-right:0.5em"><strong>$_vote_plugin_choice</strong>
<input type="hidden" name="plugin" value="tvote" />
<input type="hidden" name="refer" value="$s_page" />
<input type="hidden" name="tvote_no" value="$tvote_no" />
<input type="hidden" name="digest" value="$s_digest" />
</td>
<td align="right" class="vote_label"><strong>$votecount_head</strong></td>
<td align="right" class="vote_label"><strong>$votepercent_head</strong></td>
<td align="center" class="vote_label"><strong>
EOD;
if(!(is_freeze($vars['page']) && _TVOTE_FREEZE_SYNC))
$body .= $_vote_plugin_votes;
$body .= '</strong></td></tr>';
$tdcnt = 0;
$pollcnt = 0;
$itemlist=array();
foreach($args as $arg) {
$cnt = 0;
if (preg_match("/^(.+)\[([^\[]*)\]$/",$arg,$match)) {
$arg = $match[1];
$cnt = intval($match[2]);
if (!is_int($cnt) || $cnt<0) $cnt=0;
$polltotal+=$cnt;
//引数の先頭文字が+ならはオプションとして $opt[$arg]=$cnt;
}
$itemlist[$arg]=$cnt;
}
foreach($itemlist as $key => $cnt) {
$e_arg = encode($key);
# $trans_tbl = array ('"' => '"');
# $item = strtr($key,$trans_tbl);
$trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
$item = strtr($key, $trans_tbl);
$html = make_link($item);
if ($polltotal != 0)
$cntp=sprintf("%.1f",$cnt*100/$polltotal).'%';
else
$cntp = 0 . "%";
if(!$view_count) {
unset($cnt);
unset($cntp);
}
$cls = ($tdcnt++ % 2) ? 'vote_td1' : 'vote_td2';
$body .= <<<EOD
<tr>
<td align="left" class="$cls" style="$style">$html</td>
<td align="right" class="$cls" style="$style">$cnt</td>
<td align="right" class="$cls" style="$style">$cntp</td>
<td align="right" class="$cls" style="$style">
EOD;
$hash=sprintf('%08x',crc32($key));
$itemshash[$hash] = 1;
// 凍結判定
if(!(is_freeze($vars['page']) && _TVOTE_FREEZE_SYNC)) {
if (!$votedlist[$hash])
//投票してないならボタン表示
$body .= "<input type=\"submit\" name=\"tvote_$e_arg\" value=\"$_vote_plugin_votes\" class=\"submit\" />";
else
//投票済なら表示しない
$body .= '投票済';
} //else $body .= '締切';
$body .= '</td></tr>';
}
# 凍結判定
if(!(is_freeze($vars['page']) && _TVOTE_FREEZE_SYNC) && _TVOTE_ADD) {
$cls = ($tdcnt++ % 2) ? 'vote_td1' : 'vote_td2';
$body .= <<<EOD
<tr>
<td align="left" class="$cls" colspan="3" style="$style">その他
<input type="text" size="40" name="tvote_add" value="" />
</td>
<td align="right" class="$cls" style="$style">
<input type="submit" name="add_submit" value="投票" class="submit" />
</td>
</tr>
EOD;
}
if($view_count) {
$cls = ($tdcnt++ % 2) ? 'vote_td1' : 'vote_td2';
$body .= <<<EOD
<tr>
<td align="left" class="$cls" style="$style">投票総数</td>
<td align="right" class="$cls" style="$style">$polltotal</td>
<td align="right" class="$cls" style="$style"></td>
<td align="right" class="$cls" style="$style"></td>
</tr>
EOD;
}
$body .= '</table></form>';
# クッキー掃除
if (!empty($votedlist)) {
foreach( array_keys($votedlist) as $key )
if (!$itemshash[$key])
unset($votedlist[$key]);
}
# クッキーセット
if (!empty($votedlist)) {
$new_value=@join(C_DELIM,array_keys($votedlist));
setcookie('tvote_'.$s_page.'_'.$tvote_no,$new_value,time()+3600*24*_TVOTE_VALIDDAYS);
}
return $vars['tvote_info_'.$tvote_no].$body;
}
?>