OSDN Git Service

BugTrack/2176 showrss: Fix character encoding issues etc.
[pukiwiki/pukiwiki.git] / plugin / vote.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // vote.inc.php
4 // Copyright 2002-2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // Vote box plugin
8
9 function plugin_vote_action()
10 {
11         global $vars, $cols,$rows;
12         global $_title_collided, $_msg_collided, $_title_updated;
13         global $_vote_plugin_votes;
14
15         $script = get_base_uri();
16         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
17
18         $postdata_old  = get_source($vars['refer']);
19
20         $vote_no = 0;
21         $title = $body = $postdata = $postdata_input = $vote_str = '';
22         $matches = array();
23         foreach($postdata_old as $line) {
24
25                 if (! preg_match('/^#vote(?:\((.*)\)(.*))?$/i', $line, $matches) ||
26                     $vote_no++ != $vars['vote_no']) {
27                         $postdata .= $line;
28                         continue;
29                 }
30                 $args  = explode(',', $matches[1]);
31                 $lefts = isset($matches[2]) ? $matches[2] : '';
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]) && $vars['vote_' . $e_arg] == $_vote_plugin_votes)
41                                 ++$cnt;
42
43                         $votes[] = $arg . '[' . $cnt . ']';
44                 }
45
46                 $vote_str       = '#vote(' . @join(',', $votes) . ')' . $lefts . "\n";
47                 $postdata_input = $vote_str;
48                 $postdata      .= $vote_str;
49         }
50
51         if (md5(get_source($vars['refer'], TRUE, TRUE)) !== $vars['digest']) {
52                 $title = $_title_collided;
53
54                 $s_refer          = htmlsc($vars['refer']);
55                 $s_digest         = htmlsc($vars['digest']);
56                 $s_postdata_input = htmlsc($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 $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<br />' . "\n";
91
92         if (PKWK_READONLY) {
93                 $_script = '';
94                 $_submit = 'hidden';
95         } else {
96                 $_script = get_base_uri();
97                 $_submit = 'submit';
98         }
99
100         $args     = func_get_args();
101         $s_page   = htmlsc($page);
102         $s_digest = htmlsc($digest);
103
104         $body = <<<EOD
105 <form action="$_script" method="post">
106  <table cellspacing="0" cellpadding="2" class="style_table" summary="vote">
107   <tr>
108    <td align="left" class="vote_label" style="padding-left:1em;padding-right:1em"><strong>$_vote_plugin_choice</strong>
109     <input type="hidden" name="plugin"  value="vote" />
110     <input type="hidden" name="refer"   value="$s_page" />
111     <input type="hidden" name="vote_no" value="$vote_no" />
112     <input type="hidden" name="digest"  value="$s_digest" />
113    </td>
114    <td align="center" class="vote_label"><strong>$_vote_plugin_votes</strong></td>
115   </tr>
116
117 EOD;
118
119         $tdcnt = 0;
120         $matches = array();
121         foreach($args as $arg) {
122                 $cnt = 0;
123
124                 if (preg_match('/^(.+)\[(\d+)\]$/', $arg, $matches)) {
125                         $arg = $matches[1];
126                         $cnt = $matches[2];
127                 }
128                 $e_arg = encode($arg);
129
130                 $link = make_link($arg);
131
132                 $cls = ($tdcnt++ % 2)  ? 'vote_td1' : 'vote_td2';
133
134                 $body .= <<<EOD
135   <tr>
136    <td align="left"  class="$cls" style="padding-left:1em;padding-right:1em;">$link</td>
137    <td align="right" class="$cls">$cnt&nbsp;&nbsp;
138     <input type="$_submit" name="vote_$e_arg" value="$_vote_plugin_votes" class="submit" />
139    </td>
140   </tr>
141
142 EOD;
143         }
144
145         $body .= <<<EOD
146  </table>
147 </form>
148
149 EOD;
150
151         return $body;
152 }