OSDN Git Service

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