OSDN Git Service

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