OSDN Git Service

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