1<?php
2/******************************************************************************
3**
4**  action script related to Rater
5**  Action to count votes and display message
6*/
7/******************************************************************************
8**  must run within Dokuwiki
9**/
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14/******************************************************************************/
15class action_plugin_rater extends DokuWiki_Action_Plugin {
16
17    var $parameter = "";
18
19  /**
20   * return some info
21   */
22  function getInfo(){
23    return array(
24         'author' => 'Taggic',
25         'email'  => 'Taggic@t-online.de',
26         'date'   => '2016-06-02',
27         'name'   => 'rater (action plugin component)',
28         'desc'   => 'to store votes and display feedback.',
29         'url'    => 'http://www.dokuwiki.org/plugin:rater',
30         );
31  }
32/******************************************************************************
33**  Register its handlers with the dokuwiki's event controller
34*/
35     function register(Doku_Event_Handler &$controller) {
36         $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
37         $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'output', array());
38     }
39
40/******************************************************************************
41**  Handle the action
42*/
43     function _handle_act(&$event, $param) {
44         if ($event->data === 'rate_voteup') {
45             $this->raterfile = $_GET['rater_file'];
46             $this->rater_id = $_GET['rater_id'];
47             $this->rater_name = $_GET['rater_name'];
48             $this->rater_ip = $_GET['rater_ip'];
49             $this->rater_end = $_GET['rater_end'];
50             $this->anker_id = $_GET['anker'];
51             $this->vote = 1;
52         }
53         elseif ($event->data === 'rate_votedown') {
54             $this->raterfile = $_GET['rater_file'];
55             $this->rater_id = $_GET['rater_id'];
56             $this->rater_name = $_GET['rater_name'];
57             $this->rater_ip = $_GET['rater_ip'];
58             $this->rater_end = $_GET['rater_end'];
59             $this->anker_id = $_GET['anker'];
60             $this->vote = 2;
61         }
62         else return;
63
64         $event->preventDefault(); // https://www.dokuwiki.org/devel:events#event_object
65     }
66/******************************************************************************
67**  Generate output
68*/
69    function output(&$data) {
70      if (($data->data == 'rate_voteup') && ($this->vote == 1)) {
71              $data->preventDefault();
72              $rater_rating = 1;
73      }
74      elseif (($data->data == 'rate_votedown') && ($this->vote == 2)) {
75              $data->preventDefault();
76              $rater_rating = 2;
77      }
78      else { return; }
79
80      global $ID;
81      global $lang;
82      global $conf;
83
84      $rater_type = "vote";
85      $rater_id   = $this->rater_id;
86      $rater_name = $this->rater_name;
87      $rater_ip   = $this->rater_ip;
88      $rater_end  = $this->rater_end;
89      $anker_id   = $this->anker_id;
90
91          // Config settings
92          $rater_ip_voting_restriction = $this->getConf('voting_restriction'); // restrict ip address voting (true or false)
93          $rater_ip_vote_qty           = $this->getConf('vote_qty');           // how many times an ip address can vote
94          $rater_already_rated_msg     = sprintf($this->getConf('already_rated_msg'),$rater_ip_vote_qty);
95          $rater_not_selected_msg      = $this->getConf('not_selected_msg');
96          $rater_thankyou_msg          = $this->getConf('thankyou_msg');
97          $rater_generic_text          = $this->getConf('generic_text');       // generic item text
98          $rater_end_of_line_char      = $this->getConf('eol_char');           // to separate the records
99//          msg("eol_char = |".$rater_end_of_line_char."|",0);
100          if($rater_end_of_line_char == '') $rater_end_of_line_char = '\n';
101
102          $msg_votended                = $this->getLang('msg_votended');
103          $alink_Back                  = $this->getLang('alink_Back');
104          $today                       = date('d.m.Y');
105
106          //check if vote period already ended
107          if (($rater_end!='never') && (strtotime($today) > strtotime($rater_end)))
108              {$rater_endmsg =sprintf($msg_votended,date('d.m.Y',strtotime($rater_end))).'<br>';
109
110               echo '<meta http-equiv="refresh" content="1; URL=doku.php?id='.$ID.'#'.$anker_id.'"><div class="thumb__negative_feedback">'.$rater_endmsg.
111                    '<a href="doku.php?id='.$ID.'" />'.$alink_Back.'</a></div>';
112               return;
113               }
114
115
116    //        save vote
117            $rater_filename = metaFN('rater_'.$rater_id.'_'.$rater_name.'_'.$rater_type, '.rating');
118           // trace ip or login
119            $rater_file=fopen($rater_filename,"a+");
120            $rater_str="";
121            $rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
122            if($rater_str!=""){
123               if($rater_ip_voting_restriction){
124                  $rater_data=explode($rater_end_of_line_char,$rater_str);
125              	  $rater_ip_vote_count=0;
126                  foreach($rater_data as $d){
127                  	 $rater_tmp=explode("|",$d);
128                  	 $rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
129                  	 if($rater_ip==$rater_oldip){
130                  	  $rater_ip_vote_count++;
131                  	 }
132                  }
133                	if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
134                     $rater_msg=$rater_already_rated_msg;
135                     $addMXG = "&info=ppp";
136                	}else{
137                     fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
138                     $rater_msg=$rater_thankyou_msg;
139                     if($rater_rating===2) {
140                        $rater_msg .= $this->getLang('msg_why');
141                         $addMXG = '';
142                         echo '<div class="thumb__positive_feedback">'.$rater_ip.' : '.$rater_msg.'<br />'.
143                              '<a href="doku.php?id='.$ID.'#'.$anker_id.'" />'.$alink_Back.'</a></div>';
144                         return;
145                      }
146                	}
147               }
148               else {
149                fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
150                $rater_msg=$rater_thankyou_msg;
151               }
152            }
153            else{
154               fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
155               $rater_msg=$rater_thankyou_msg;
156            }
157            fclose($rater_file);
158
159//            msg($rater_rating."|".$rater_ip.$rater_end_of_line_char,0);
160
161      // reload original page
162      echo '<meta http-equiv="refresh" content="2; URL=doku.php?id='.$ID.'#'.$anker_id.'"><div class="thumb__positive_feedback">'.$rater_ip.' : '.$rater_msg.'<br />'.
163                    '<a href="doku.php?id='.$ID.'#'.$anker_id.'" />'.$alink_Back.'</a></div>';
164
165    }
166/******************************************************************************/
167}