OSDN Git Service

BugTrack/281: separate sequence number for each page.
[pukiwiki/pukiwiki.git] / plugin / vote.inc.php
1 <?php
2 // $Id: vote.inc.php,v 1.12 2003/04/13 06:28:52 arino Exp $
3
4 function plugin_vote_init()
5 {
6         if (LANG == 'ja')
7                 $messages = array(
8                         '_vote_plugin_choice' => 'ÁªÂò»è',
9                         '_vote_plugin_votes' => 'Åêɼ',
10                 );
11         else
12                 $messages = array(
13                         '_vote_plugin_choice' => 'Selection',
14                         '_vote_plugin_votes' => 'Vote',
15                 );
16         set_plugin_messages($messages);
17 }
18
19 function plugin_vote_action()
20 {
21         global $post,$vars,$script,$cols,$rows;
22         global $_title_collided,$_msg_collided,$_title_updated;
23         global $_vote_plugin_choice, $_vote_plugin_votes;
24
25         $postdata_old  = get_source($post['refer']);
26         $vote_no = 0;
27         $title = $body = $postdata = '';
28
29         foreach($postdata_old as $line) {
30                 if (!preg_match("/^#vote\((.*)\)\s*$/",$line,$arg)) {
31                         $postdata .= $line;
32                         continue;
33                 }
34                 
35                 if ($vote_no++ != $post['vote_no']) {
36                         $postdata .= $line;
37                         continue;
38                 }
39                 $args = explode(',',$arg[1]);
40                 
41                 foreach($args as $arg) {
42                         $cnt = 0;
43                         if (preg_match("/^(.+)\[(\d+)\]$/",$arg,$match)) {
44                                 $arg = $match[1];
45                                 $cnt = $match[2];
46                         }
47                         $e_arg = encode($arg);
48                         if (!empty($post["vote_$e_arg"]) and $post["vote_$e_arg"] == $_vote_plugin_votes) {
49                                 $cnt++;
50                         }
51                         
52                         $votes[] = $arg.'['.$cnt.']';
53                 }
54                 
55                 $vote_str = '#vote('.@join(',',$votes).")\n";
56                 
57                 $postdata_input = $vote_str;
58                 $postdata .= $vote_str;
59         }
60
61         if (md5(@join('',get_source($post['refer']))) != $post['digest']) {
62                 $title = $_title_collided;
63                 
64                 $s_refer = htmlspecialchars($post['refer']);
65                 $s_digest = htmlspecialchars($post['digest']);
66                 $s_postdata_input = htmlspecialchars($postdata_input);
67                 $body = <<<EOD
68 $_msg_collided
69 <form action="$script?cmd=preview" method="post">
70  <div>
71   <input type="hidden" name="refer" value="$s_refer" />
72   <input type="hidden" name="digest" value="$s_digest" />
73   <textarea name="msg" rows="$rows" cols="$cols" id="textarea">$s_postdata_input</textarea><br />
74  </div>
75 </form>
76
77 EOD;
78         }
79         else
80         {
81                 page_write($post['refer'],$postdata);
82                 
83                 $title = $_title_updated;
84         }
85
86         $retvars['msg'] = $title;
87         $retvars['body'] = $body;
88
89         $post['page'] = $post['refer'];
90         $vars['page'] = $post['refer'];
91
92         return $retvars;
93 }
94 function plugin_vote_convert()
95 {
96         global $script,$vars,$digest;
97         global $_vote_plugin_choice, $_vote_plugin_votes;
98         static $numbers = array();
99         
100         if (!array_key_exists($vars['page'],$numbers))
101         {
102                 $numbers[$vars['page']] = 0;
103         }
104         $vote_no = $numbers[$vars['page']]++;
105         
106         if (!func_num_args()) {
107                 return '';
108         }
109
110         $args = func_get_args();
111         $s_page = htmlspecialchars($vars['page']);
112         $s_digest = htmlspecialchars($digest);
113
114         $body = <<<EOD
115 <form action="$script" method="post">
116  <table cellspacing="0" cellpadding="2" class="style_table" summary="vote">
117   <tr>
118    <td align="left" class="vote_label" style="padding-left:1em;padding-right:1em"><strong>$_vote_plugin_choice</strong>
119     <input type="hidden" name="plugin" value="vote" />
120     <input type="hidden" name="refer" value="$s_page" />
121     <input type="hidden" name="vote_no" value="$vote_no" />
122     <input type="hidden" name="digest" value="$s_digest" />
123    </td>
124    <td align="center" class="vote_label"><strong>$_vote_plugin_votes</strong></td>
125   </tr>
126
127 EOD;
128         
129         $tdcnt = 0;
130         foreach($args as $arg) {
131                 $cnt = 0;
132                 
133                 if (preg_match("/^(.+)\[(\d+)\]$/",$arg,$match)) {
134                         $arg = $match[1];
135                         $cnt = $match[2];
136                 }
137                 $e_arg = encode($arg);
138                 
139                 $link = make_link($arg);
140                 
141                 $cls = ($tdcnt++ % 2)  ? 'vote_td1' : 'vote_td2';
142                 
143                 $body .= <<<EOD
144   <tr>
145    <td align="left" class="$cls" style="padding-left:1em;padding-right:1em;">$link</td>
146    <td align="right" class="$cls">$cnt&nbsp;&nbsp;
147     <input type="submit" name="vote_$e_arg" value="$_vote_plugin_votes" class="submit" />
148    </td>
149   </tr>
150
151 EOD;
152         }
153         
154         $body .= <<<EOD
155  </table>
156 </form>
157
158 EOD;
159         
160         return $body;
161 }
162 ?>