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