OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / vote.inc.php
1 <?php
2 /**
3  * voteプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: vote.inc.php 1601 2009-03-21 05:51:06Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 function plugin_vote_action()
17 {
18         //global $vars, $script, $cols,$rows;
19         global $script, $cols,$rows;
20         global $_title_collided, $_msg_collided, $_title_updated;
21         global $_vote_plugin_votes;
22
23         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
24
25         //$postdata_old  = get_source($vars['refer']);
26         $postdata_old  = get_source(WikiParam::getRefer());
27
28         $vote_no = 0;
29         $title = $body = $postdata = $postdata_input = $vote_str = '';
30         $matches = array();
31         foreach($postdata_old as $line) {
32                 //if (! preg_match('/^#vote(?:\((.*)\)(.*))?$/i', $line, $matches) || $vote_no++ != $vars['vote_no']) {
33                 if (!preg_match('/^#vote(?:\((.*)\)(.*))?$/i', $line, $matches) || $vote_no++ != WikiParam::getVar('vote_no')){
34                         $postdata .= $line;
35                         continue;
36                 }
37                 $args  = explode(',', $matches[1]);
38                 $lefts = isset($matches[2]) ? $matches[2] : '';
39
40                 foreach($args as $arg) {
41                         $cnt = 0;
42                         if (preg_match('/^(.+)\[(\d+)\]$/', $arg, $matches)) {
43                                 $arg = $matches[1];
44                                 $cnt = $matches[2];
45                         }
46                         $e_arg = encode($arg);
47                         //if (! empty($vars['vote_' . $e_arg]) && $vars['vote_' . $e_arg] == $_vote_plugin_votes)
48                         if (WikiParam::getVar('vote_' . $e_arg) == $_vote_plugin_votes)
49                                 ++$cnt;
50
51                         $votes[] = $arg . '[' . $cnt . ']';
52                 }
53
54                 $vote_str       = '#vote(' . @join(',', $votes) . ')' . $lefts . "\n";
55                 $postdata_input = $vote_str;
56                 $postdata      .= $vote_str;
57         }
58
59         //if (md5(@join('', get_source($vars['refer']))) != $vars['digest']) {
60         if (md5(get_source(WikiParam::getRefer(), true)) != WikiParam::getVar('digest')) {
61                 $title = $_title_collided;
62
63         /*      $s_refer          = htmlspecialchars($vars['refer']);
64                 $s_digest         = htmlspecialchars($vars['digest']);*/
65                 $s_refer          = htmlspecialchars(WikiParam::getRefer());
66                 $s_digest         = htmlspecialchars(WikiParam::getVar('digest'));
67                 
68                 $postScript = $script . WikiParam::convQuery("?cmd=preview");
69                 $s_postdata_input = htmlspecialchars($postdata_input);
70                 $body = <<<EOD
71 $_msg_collided
72 <form action="$postScript" method="post" class="form">
73  <div>
74   <input type="hidden" name="refer"  value="$s_refer" />
75   <input type="hidden" name="digest" value="$s_digest" />
76   <textarea name="msg" class="wiki_edit" rows="$rows" cols="$cols">$s_postdata_input</textarea><br />
77  </div>
78 </form>
79 EOD;
80         } else {
81                 //page_write($vars['refer'], $postdata);
82                 page_write(WikiParam::getRefer(), $postdata);
83                 $title = $_title_updated;
84         }
85
86         //$vars['page'] = $vars['refer'];
87         WikiParam::setPage(WikiParam::getRefer());
88
89         return array('msg'=>$title, 'body'=>$body);
90 }
91
92 function plugin_vote_convert()
93 {
94         //global $script, $vars,  $digest;
95         global $script;
96         global $_vote_plugin_choice, $_vote_plugin_votes;
97         static $number = array();
98
99         //$page = isset($vars['page']) ? $vars['page'] : '';
100         $page = WikiParam::getPage();
101         
102         // Vote-box-id in the page
103         if (! isset($number[$page])) $number[$page] = 0; // Init
104         $vote_no = $number[$page]++;
105
106         if (! func_num_args()) return '#vote(): No arguments<br />' . "\n";
107
108         if (PKWK_READONLY) {
109                 $_script = '';
110                 $_submit = 'hidden';
111         } else {
112                 //$_script = $script;
113                 $_script = $script . WikiParam::convQuery("?");
114                 $_submit = 'submit';
115         }
116
117         $args     = func_get_args();
118         $s_page   = htmlspecialchars($page);
119         //$s_digest = htmlspecialchars($digest);
120         $s_digest = htmlspecialchars(WikiParam::getDigest());
121
122         $body = <<<EOD
123 <form action="$_script" method="post" class="form">
124  <table cellspacing="0" cellpadding="2" class="style_table" summary="vote">
125   <tr>
126    <td align="left" class="vote_label" style="padding-left:1em;padding-right:1em"><strong>$_vote_plugin_choice</strong>
127     <input type="hidden" name="plugin"  value="vote" />
128     <input type="hidden" name="refer"   value="$s_page" />
129     <input type="hidden" name="vote_no" value="$vote_no" />
130     <input type="hidden" name="digest"  value="$s_digest" />
131    </td>
132    <td align="center" class="vote_label"><strong>$_vote_plugin_votes</strong></td>
133   </tr>
134 EOD;
135
136         $tdcnt = 0;
137         $matches = array();
138         foreach($args as $arg) {
139                 $cnt = 0;
140
141                 if (preg_match('/^(.+)\[(\d+)\]$/', $arg, $matches)) {
142                         $arg = $matches[1];
143                         $cnt = $matches[2];
144                 }
145                 $e_arg = encode($arg);
146
147                 $link = make_link($arg);
148
149                 $cls = ($tdcnt++ % 2)  ? 'vote_td1' : 'vote_td2';
150
151                 $body .= <<<EOD
152   <tr>
153    <td align="left"  class="$cls" style="padding-left:1em;padding-right:1em;">$link</td>
154    <td align="right" class="$cls">$cnt&nbsp;&nbsp;
155     <input type="$_submit" name="vote_$e_arg" class="button" value="$_vote_plugin_votes" />
156    </td>
157   </tr>
158 EOD;
159         }
160
161         $body .= <<<EOD
162  </table>
163 </form>
164 EOD;
165
166         return $body;
167 }
168 ?>