Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class VoterController extends VoteAppController {
  3.  
  4.     public function index() {
  5.       $this->loadModel('Vote.VoteConfiguration');
  6.       $config = $this->VoteConfiguration->getConfig();
  7.  
  8.       $this->set('websites', $config['websites']);
  9.       $this->set('rewards', $config['rewards']);
  10.  
  11.       $this->loadModel('User');
  12.       $ranking = $this->User->find('all', array('limit' => '10', 'order' => 'vote desc'));
  13.       $this->set(compact('ranking'));
  14.     }
  15.  
  16.     public function setWebsite() {
  17.       $this->autoRender = false;
  18.       if($this->request->is('ajax')) {
  19.         if(!empty($this->request->data['website']) OR $this->request->data['website'] == 0) {
  20.           $this->Session->write('vote.website', $this->request->data['website']);
  21.  
  22.           $this->loadModel('Vote.VoteConfiguration');
  23.           $config = $this->VoteConfiguration->getConfig();
  24.           $websites = $config['websites'];
  25.  
  26.           if($websites[$this->request->data['website']]['website_type'] == 'rpg') {
  27.               $this->response->body(json_encode(array('page' => 'http://rpg-paradize.com/?page=vote&vote='.@$websites[$this->request->data['website']]['rpg_id'], 'website_type' => @$websites[$this->request->data['website']]['website_type'])));
  28.           } else {
  29.              $this->response->body(json_encode(array('page' => @$websites[$this->request->data['website']]['page_vote'], 'website_type' => @$websites[$this->request->data['website']]['website_type'])));
  30.           }
  31.         }
  32.       }
  33.     }
  34.  
  35.   public function setPseudo() {
  36.     $this->autoRender = false;
  37.     $this->response->type('json');
  38.     if($this->request->is('ajax')) {
  39.       $this->loadModel('User');
  40.       if(!empty($this->request->data['pseudo'])) {
  41.         if($this->User->exist($this->request->data['pseudo'])) {
  42.  
  43.                     $user_id = $this->User->getFromUser('id', $this->request->data['pseudo']);
  44.  
  45.           $this->loadModel('Vote.Vote');
  46.           $get_last_vote = $this->Vote->find('first',
  47.                         array('conditions' =>
  48.                             array(
  49.                                 'OR' => array(
  50.                                         'user_id' => $user_id,
  51.                                         'ip' => $this->Util->getIP()
  52.                                     ),
  53.                                 'website' => $this->Session->read('vote.website')
  54.                                 )
  55.                             )
  56.                         );
  57.  
  58.             if(!empty($get_last_vote['Vote']['created'])) {
  59.                 $now = time();
  60.                 $last_vote = ($now - strtotime($get_last_vote['Vote']['created']))/60;
  61.             } else {
  62.                 $last_vote = null;
  63.             }
  64.  
  65.             $this->loadModel('Vote.VoteConfiguration');
  66.             $config = $this->VoteConfiguration->getConfig();
  67.  
  68.  
  69.             $websites = $config['websites'];
  70.             if(isset($websites[$this->Session->read('vote.website')])) {
  71.               $time_vote = $websites[$this->Session->read('vote.website')]['time_vote'];
  72.  
  73.               if(empty($last_vote) OR $last_vote > $time_vote) {
  74.  
  75.                 $this->Session->write('vote.pseudo', $this->request->data['pseudo']);
  76.                 echo json_encode(array('statut' => true , 'msg' => $this->Lang->get('VOTE__STEP_1_SUCCESS')));
  77.  
  78.               } else {
  79.  
  80.                 $calcul_wait_time = ($time_vote - $last_vote)*60;
  81.                 $calcul_wait_time = $this->Util->secondsToTime($calcul_wait_time); //On le sort en jolie
  82.  
  83.                 $wait_time = array();
  84.                 if($calcul_wait_time['d'] > 0) {
  85.                   $wait_time[] = $calcul_wait_time['d'].' '.$this->Lang->get('GLOBAL__DATE_R_DAYS');
  86.                 }
  87.                 if($calcul_wait_time['h'] > 0) {
  88.                   $wait_time[] = $calcul_wait_time['h'].' '.$this->Lang->get('GLOBAL__DATE_R_HOURS');
  89.                 }
  90.                 if($calcul_wait_time['m'] > 0) {
  91.                   $wait_time[] = $calcul_wait_time['m'].' '.$this->Lang->get('GLOBAL__DATE_R_MINUTES');
  92.                 }
  93.                 if($calcul_wait_time['s'] > 0) {
  94.                   $wait_time[] = $calcul_wait_time['s'].' '.$this->Lang->get('GLOBAL__DATE_R_SECONDS');
  95.                 }
  96.  
  97.                 $wait_time = implode(', ', $wait_time);
  98.  
  99.                 $this->response->body(json_encode(array('statut' => false , 'msg' => $this->Lang->get('VOTE__VOTE_ERROR_WAIT', array('{WAIT_TIME}' => $wait_time)))));
  100.               }
  101.             } else {
  102.                 $this->response->body(json_encode(array('statut' => false , 'msg' => $this->Lang->get('VOTE__VOTE_ERROR_UNKNOWN_WEBSITE'))));
  103.             }
  104.           } else {
  105.             $this->response->body(json_encode(array('statut' => false , 'msg' =>$this->Lang->get('VOTE__VOTE_ERROR_USER_UNKNOWN'))));
  106.           }
  107.         }
  108.       } else {
  109.         throw new InternalErrorException();
  110.       }
  111.     }
  112.  
  113.     public function checkOut() {
  114.       $this->autoRender = false;
  115.     $this->response->type('json');
  116.       if($this->request->is('ajax')) {
  117.         if(!empty($this->request->data['out']) && $this->Session->check('vote.website') && $this->Session->check('vote.pseudo')) {
  118.  
  119.           $this->loadModel('Vote.VoteConfiguration');
  120.           $config = $this->VoteConfiguration->getConfig();
  121.           $websites = $config['websites'];
  122.           $url = $websites[$this->Session->read('vote.website')]['page_vote'];
  123.           // exemple : http://rpg-paradize.com/site-+FR+++RESET++ObsiFight+Serveur+PvP+Faction+2424+1.8-44835
  124.  
  125.           $user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'; // simule Firefox 4.
  126.             $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
  127.             $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  128.             $header[] = "Cache-Control: max-age=0";
  129.             $header[] = "Connection: keep-alive";
  130.             $header[] = "Keep-Alive: 300";
  131.             $header[] = "Accept-Charset: utf-8";
  132.             $header[] = "Accept-Language: fr"; // langue fr.
  133.             $header[] = "Pragma: "; // Simule un navigateur
  134.  
  135.           $ch = curl_init();
  136.           curl_setopt($ch, CURLOPT_URL, $url); // l'url visité
  137.           curl_setopt($ch, CURLOPT_FAILONERROR, 1);// Gestion d'erreur
  138.           curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // autorise la redirection
  139.           curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // stock la response dans une variable
  140.           curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  141.           curl_setopt($ch, CURLOPT_PORT, 80); // set port 80
  142.           curl_setopt($ch, CURLOPT_TIMEOUT, 2); //  timeout curl à 15 secondes.
  143.  
  144.           curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  145.  
  146.  
  147.           $result=curl_exec($ch);
  148.           $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  149.           curl_close($ch);
  150.  
  151.           if($code != 200) {
  152.             $this->log('RPG-Paradize unavailable (CODE: '.$code.')');
  153.           }
  154.  
  155.           $str = substr($result, strpos($result, 'Clic Sortant'), 20);
  156.           $out = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
  157.           $array = array($out, $out-1, $out-2, $out-3, $out+1, $out+2, $out+3);
  158.  
  159.           if(in_array($this->request->data['out'], $array) || $code != 200) {
  160.  
  161.             $this->Session->write('vote.out', true);
  162.             $this->response->body(json_encode(array('statut' => true, 'msg' =>$this->Lang->get('VOTE__STEP_3_SUCCESS'))));
  163.  
  164.           } else {
  165.             $this->response->body(json_encode(array('statut' => false, 'msg' =>$this->Lang->get('VOTE__STEP_3_ERROR'))));
  166.           }
  167.         }
  168.       } else {
  169.           throw new InternalErrorException();
  170.       }
  171.     }
  172.  
  173.     public function getRewards() {
  174.         $when = (isset($this->request->data['when'])) ? $this->request->data['when'] : 'now';
  175.         $this->autoRender = false;
  176.         $this->response->type('json');
  177.         if($this->request->is('ajax')) {
  178.             if($this->Session->check('vote.website') && $this->Session->check('vote.pseudo')) {
  179.                 $this->loadModel('Vote.VoteConfiguration');
  180.                 $config = $this->VoteConfiguration->getConfig();
  181.                 $websites = $config['websites'];
  182.                 $website = $websites[$this->Session->read('vote.website')];
  183.                 if($this->Session->check('vote.out') || $website['website_type'] == "other") {
  184.  
  185.                     // check si il a pas déjà voté sur ce site
  186.                     $this->loadModel('Vote.Vote');
  187.                                         $user_id = $this->User->getFromUser('id', $this->Session->read('vote.pseudo'));
  188.                     $get_last_vote = $this->Vote->find('first', array('conditions' => array('OR' => array('user_id' => $user_id, 'ip' => $this->Util->getIP()), 'website' => $this->Session->read('vote.website'))));
  189.  
  190.                     if(!empty($get_last_vote['Vote']['created'])) {
  191.                         $now = time();
  192.                         $last_vote = ($now - strtotime($get_last_vote['Vote']['created']))/60;
  193.                     } else {
  194.                         $last_vote = null;
  195.                     }
  196.  
  197.                     if(isset($websites[$this->Session->read('vote.website')])) {
  198.                         $time_vote = $websites[$this->Session->read('vote.website')]['time_vote'];
  199.  
  200.                         if(empty($last_vote) OR $last_vote > $time_vote) {
  201.  
  202.                             // on incrémente le vote
  203.                             if(empty($get_last_vote)) {
  204.                               $this->Vote->read(null, null);
  205.                               $this->Vote->set(array(
  206.                                   'user_id' => $user_id,
  207.                                   'ip' => $this->Util->getIP(),
  208.                                   'website' => $this->Session->read('vote.website')
  209.                               ));
  210.                             } else {
  211.                               $this->Vote->read(null, $get_last_vote['Vote']['id']);
  212.                               $this->Vote->set(array(
  213.                                   'user_id' => $user_id,
  214.                                   'ip' => $this->Util->getIP(),
  215.                                   'website' => $this->Session->read('vote.website'),
  216.                                   'created' => date('Y-m-d H:i:s')
  217.                               ));
  218.                             }
  219.                             $this->loadModel('User');
  220.                             $userData = $this->User->find('first', array('conditions' => array('pseudo' => $this->Session->read('vote.pseudo'))));
  221.                             $vote_nbr = $userData['User']['vote'] + 1;
  222.  
  223.                             if($when != 'now') {
  224.                               $rewards_waited = intval($userData['User']['rewards_waited']) + 1;
  225.                             }
  226.  
  227.                             // on cast l'event
  228.                             $event = new CakeEvent('onVote', $this, array('when' => $when, 'website' => $this->Session->read('vote.website'), 'config' => $config, 'user' => $userData));
  229.                                     $this->getEventManager()->dispatch($event);
  230.                                     if($event->isStopped()) {
  231.                                         return $event->result;
  232.                                     }
  233.  
  234.  
  235.                             if($when == 'now') { // si c'est maintenant
  236.  
  237.                                                             $rewardStatus = $this->processRewards($config, $userData['User']);
  238.  
  239.                                                             if(!$rewardStatus['status']) {
  240.                                                                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get($rewardStatus['msg']))));
  241.                                 return;
  242.                                                             }
  243.  
  244.                               /*
  245.                                   On vérifie sa position pour le notifier
  246.                               */
  247.                                 // On récupère le top 3
  248.                                 $getTOP = $this->User->find('all', array('fields' => array('User.id', 'User.vote'), 'order' => 'vote DESC', 'limit' => 3));
  249.                                 //Si il en fait partie
  250.                                 if(in_array($userData['User']['id'], $getTOP)) {
  251.  
  252.                                   // Si il est deuxième
  253.                                   if($getTOP[1]['User']['id'] == $userData['User']['id']) {
  254.                                     // Si il va être premier
  255.                                     if($vote_nbr > $getTOP[1]['User']['vote']) {
  256.  
  257.                                       //On notifie qu'il est 1er
  258.                                       $this->loadModel('Notification');
  259.                                       $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOW_FIRST'), $userData['User']['id']);
  260.                                       //On notifie l'autre qu'il n'est plus 1er
  261.                                       $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOT_NOW_FIRST'), $getTOP[0]['User']['id']);
  262.  
  263.                                     }
  264.                                   }
  265.  
  266.                                 } else {
  267.                                   // Si il en fait pas partie, mais que dans 1 vote, il y sera
  268.                                   $top3Votes = $getTOP[2]['User']['vote'];
  269.                                   if($vote_nbr > $top3Votes) {
  270.  
  271.                                     //On notifie qu'il est dans le top 3
  272.                                     $this->loadModel('Notification');
  273.                                     $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOW_IN_TOP'), $userData['User']['id']);
  274.                                     //On notifie l'autre qu'il n'est plus dans le top 3
  275.                                     $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOT_NOW_IN_TOP'), $getTOP[2]['User']['id']);
  276.  
  277.                                   }
  278.                                 }
  279.  
  280.                               // on sauvegarde le vote
  281.                               $this->User->setToUser('vote', $vote_nbr, $userData['User']['id']);
  282.                               $this->Vote->save();
  283.  
  284.                                                             $this->response->body(json_encode(array('statut' => true, 'msg' => $rewardStatus['msg'])));
  285.  
  286.                             } else { // si c'est plus tard
  287.  
  288.                               /*
  289.                                   On vérifie sa position pour le notifier
  290.                               */
  291.                                 // On récupère le top 3
  292.                                 $getTOP = $this->User->find('all', array('fields' => array('User.id', 'User.pseudo', 'User.vote'), 'order' => 'vote DESC', 'limit' => 3));
  293.  
  294.                                 //Si il en fait partie
  295.                                 if($getTOP[0]['User']['id'] == $userData['User']['id'] || $getTOP[1]['User']['id'] == $userData['User']['id'] || $getTOP[2]['User']['id'] == $userData['User']['id']) {
  296.  
  297.                                   // Si il est deuxième
  298.                                   if($getTOP[1]['User']['id'] == $userData['User']['id']) {
  299.                                     // Si il va être premier
  300.                                     if($vote_nbr > $getTOP[0]['User']['vote']) {
  301.  
  302.                                       //On notifie qu'il est 1er
  303.                                       $this->loadModel('Notification');
  304.                                       $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOW_FIRST'), $userData['User']['id']);
  305.                                       //On notifie l'autre qu'il n'est plus 1er
  306.                                       $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOT_NOW_FIRST'), $getTOP[0]['User']['id']);
  307.  
  308.                                     }
  309.                                   }
  310.  
  311.                                 } else {
  312.                                   // Si il en fait pas partie, mais que dans 1 vote, il y sera
  313.                                   $top3Votes = $getTOP[2]['User']['vote'];
  314.                                   if($vote_nbr > $top3Votes) {
  315.  
  316.                                     //On notifie qu'il est dans le top 3
  317.                                     $this->loadModel('Notification');
  318.                                     $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOW_IN_TOP'), $userData['User']['id']);
  319.                                     //On notifie l'autre qu'il n'est plus dans le top 3
  320.                                     $this->Notification->setToUser($this->Lang->get('VOTE__NOTIFICATION_NOT_NOW_IN_TOP'), $getTOP[2]['User']['id']);
  321.  
  322.                                   }
  323.                                 }
  324.  
  325.                               // on sauvegarde le vote
  326.                               $this->User->setToUser('vote', $vote_nbr, $userData['User']['id']);
  327.                               $this->User->setToUser('rewards_waited', $rewards_waited, $userData['User']['id']);
  328.                               $this->Vote->save();
  329.  
  330.                               $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('VOTE__STEP_4_REWARD_SUCCESS_SAVE'))));
  331.                             }
  332.  
  333.                             $this->Session->delete('vote');
  334.  
  335.                         } else {
  336.  
  337.                           $calcul_wait_time = ($time_vote - $last_vote)*60;
  338.                           $calcul_wait_time = $this->Util->secondsToTime($calcul_wait_time); //On le sort en jolie
  339.  
  340.                           $wait_time = array();
  341.                           if($calcul_wait_time['d'] > 0) {
  342.                             $wait_time[] = $calcul_wait_time['d'].' '.$this->Lang->get('GLOBAL__DATE_R_DAYS');
  343.                           }
  344.                           if($calcul_wait_time['h'] > 0) {
  345.                             $wait_time[] = $calcul_wait_time['h'].' '.$this->Lang->get('GLOBAL__DATE_R_HOURS');
  346.                           }
  347.                           if($calcul_wait_time['m'] > 0) {
  348.                             $wait_time[] = $calcul_wait_time['m'].' '.$this->Lang->get('GLOBAL__DATE_R_MINUTES');
  349.                           }
  350.                           if($calcul_wait_time['s'] > 0) {
  351.                             $wait_time[] = $calcul_wait_time['s'].' '.$this->Lang->get('GLOBAL__DATE_R_SECONDS');
  352.                           }
  353.  
  354.                           $wait_time = implode(', ', $wait_time);
  355.  
  356.                           $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('VOTE__VOTE_ERROR_WAIT', array('{WAIT_TIME}' => $wait_time)))));
  357.  
  358.                           $this->Session->delete('vote');
  359.                         }
  360.                     } else {
  361.                       throw new NotFoundException();
  362.                     }
  363.  
  364.                 } else {
  365.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('VOTE__STEP_3_ERROR'))));
  366.                 }
  367.             } else {
  368.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('VOTE__VOTE_ERROR_USER_UNKNOWN'))));
  369.             }
  370.         } else {
  371.             throw new InternalErrorException();
  372.         }
  373.     }
  374.  
  375.         private function processRewards($config, $user) { // Donne les récompenses au user passé selon la configuration donnée
  376.  
  377.             /* ====
  378.                     Toutes les récompenses
  379.                  ==== */
  380.             if($config['rewards_type'] == 1) { // toutes les récompenses
  381.  
  382.                 $rewards = $config['rewards']; // on récupére la liste
  383.  
  384.         $event = new CakeEvent('beforeReceiveRewards', $this, array('rewards' => $rewards, 'type' => 'all', 'user' => $user));
  385.         $this->getEventManager()->dispatch($event);
  386.         if($event->isStopped()) {
  387.           return $event->result;
  388.         }
  389.  
  390.                 foreach ($rewards as $key => $value) { // on parcoure les récompenses
  391.  
  392.                     if($value['type'] == 'server') { // si c'est une commande serveur
  393.  
  394.                         $server = $this->executeServerReward($config, $user, $value);
  395.                         if($server === true) {
  396.  
  397.                             $rewardsSended[] = $value['name'];
  398.  
  399.                         } else {
  400.                             return array('status' => false, 'msg' => $server);
  401.                         }
  402.  
  403.                     } elseif($value['type'] == 'money') { // si c'est des points boutique
  404.  
  405.                         $money = intval($user['money']) + intval($value['how']);
  406.                         $this->loadModel('User');
  407.                         $this->User->setToUser('money', $money, $user['pseudo']);
  408.  
  409.                         $rewardsSended[] = $value['how'].' '.$this->Configuration->getMoneyName();
  410.  
  411.                     } else {
  412.                         return array('status' => false, 'msg' => 'VOTE__UNKNOWN_REWARD_TYPE');
  413.                     }
  414.  
  415.                 }
  416.  
  417.                 $return = $this->Lang->get('VOTE__VOTE_SUCCESS').' ! ';
  418.                 if(!empty($rewardsSended)) {
  419.                     $return .= $this->Lang->get('VOTE__REWARDS_TITLE').' : ';
  420.                     $return .= '<b>'.implode('</b>, <b>', $rewardsSended).'</b>.';
  421.              }
  422.  
  423.              return array('status' =>true, 'msg' => $return);
  424.  
  425.         } else { // récompenses aléatoire selon probabilité
  426.  
  427.             $rewards = $config['rewards']; // on récupère la liste des récompenses
  428.             $probability_all = 0; // on met la probabilité totale à 0 de base
  429.             foreach ($rewards as $key => $value) {
  430.                 $probability_all = $probability_all + $value['proba'];   // Puis on la calcule
  431.                 $rewards_rand[$key] = $value['proba'];
  432.             }
  433.  
  434.             $reward = $this->Util->random($rewards_rand, $probability_all); // on récupère la reward tiré au sort
  435.  
  436.       $event = new CakeEvent('beforeReceiveRewards', $this, array('rewards' => $reward, 'type' => 'random', 'user' => $user));
  437.       $this->getEventManager()->dispatch($event);
  438.       if($event->isStopped()) {
  439.         return $event->result;
  440.       }
  441.  
  442.             if($rewards[$reward]['type'] == 'server') { // si c'est une commande serveur
  443.  
  444.                 $server = $this->executeServerReward($config, $user, $rewards[$reward]);
  445.                 if($server === true) {
  446.  
  447.                     return array('status' => true, 'msg' => $this->Lang->get('VOTE__VOTE_SUCCESS').' ! '.$this->Lang->get('VOTE__MESSAGE_VOTE_SUCCESS_REWARD').' : <b>'.$rewards[$reward]['name'].'</b>.');
  448.  
  449.                 } else {
  450.                     return array('status' => false, 'msg' => $server);
  451.                 }
  452.  
  453.              } elseif($rewards[$reward]['type'] == 'money') { // si c'est des points boutique
  454.  
  455.                      $money = intval($user['money']) + intval($rewards[$reward]['how']);
  456.                      $this->loadModel('User');
  457.                      $this->User->setToUser('money', $money, $user['pseudo']);
  458.  
  459.                      return array('status' => true, 'msg' =>$this->Lang->get('VOTE__VOTE_SUCCESS').' ! '.$this->Lang->get('VOTE__REWARDS_TITLE').' : <b>'.$rewards[$reward]['how'].' '.$this->Configuration->getMoneyName().'</b>.');
  460.  
  461.              } else {
  462.                      return array('status' => false, 'msg' => 'ERROR__INTERNAL_ERROR');
  463.              }
  464.  
  465.             }
  466.  
  467.         }
  468.  
  469.         private function executeServerReward($config, $user, $reward) { // execute la commande d'une récompense si les serveurs de la config sont ouverts
  470.             $config['servers'] = $config['servers']; // on récupére la liste des serveurs configurés
  471.             if(!empty($config['servers'])) { // si la liste n'est pas vide
  472.                 foreach ($config['servers'] as $k => $v) { //on parcours les serveurs pour voir si ils sont tous allumés
  473.                     $servers_online[] = $this->Server->online($v);
  474.                 }
  475.             } else { // sinon on demande au component de prendre celui par défaut, le premier de ceux liés
  476.                 $servers_online = array($this->Server->online());
  477.             }
  478.             if(!in_array(false, $servers_online)) { // si tous les serveurs sont allumés
  479.  
  480.         $cmd = $reward['command'];
  481.         $cmd = str_replace('{PLAYER}', $user['pseudo'], $cmd);
  482.         $cmd = str_replace('{PROBA}', $reward['proba'], $cmd);
  483.         $cmd = str_replace('{REWARD}', $reward['name'], $cmd);
  484.  
  485.                 if(empty($config['servers'])) { // si on a pas de liste, on prend celui par défaut
  486.  
  487.           if($reward['need_connect_on_server'] == "true") {
  488.             $call = $this->Server->call(array('isConnected' => $user['pseudo']), true, false);
  489.             if($call['isConnected'] != 'true') {
  490.               return 'VOTE__ERROR_NEED_CONNECT_ON_SERVER';
  491.             }
  492.           }
  493.  
  494.                     $this->Server->commands($cmd); // on envoie la commande puis enregistre le vote
  495.                 } else {
  496.           $continue = false;
  497.           if($reward['need_connect_on_server'] == "true") {
  498.                     foreach ($config['servers'] as $k2 => $v2) { // on parcours les serveurs
  499.  
  500.               $call = $this->Server->call(array('isConnected' => $user['pseudo']), true, $v2);
  501.               if($call['isConnected'] == 'true') {
  502.                 $continue = true;
  503.                 break;
  504.               }
  505.  
  506.                     }
  507.           } else {
  508.             $continue = true;
  509.           }
  510.           if($continue) {
  511.             unset($k2);
  512.             unset($v2);
  513.             foreach ($config['servers'] as $k2 => $v2) {
  514.               $this->Server->commands($cmd, $v2); // on envoie la commande puis enregistre le vote
  515.             }
  516.           } else {
  517.             return 'VOTE__ERROR_NEED_CONNECT_ON_SERVER';
  518.           }
  519.                 }
  520.             } else { //le serveur est éteint
  521.                 return 'SERVER__MUST_BE_ON';
  522.             }
  523.             return true;
  524.         }
  525.  
  526.     public function get_reward() {
  527.         $this->autoRender = false;
  528.         if($this->isConnected && $this->User->getKey('rewards_waited') > 0) {
  529.             $this->loadModel('Vote.VoteConfiguration');
  530.             $config = $this->VoteConfiguration->getConfig();
  531.  
  532.             $event = new CakeEvent('beforeGetWaitingReward', $this, array('user' => $this->User->getAllFromCurrentUser()));
  533.             $this->getEventManager()->dispatch($event);
  534.             if($event->isStopped()) {
  535.               return $event->result;
  536.             }
  537.  
  538.                         $rewardStatus = $this->processRewards($config, $this->User->getAllFromCurrentUser());
  539.  
  540.                         if(!$rewardStatus['status']) {
  541.                             $this->Session->setFlash($this->Lang->get($rewardStatus['msg']), 'default.error');
  542.                 $this->redirect(array('controller' => 'user', 'action' => 'profile', 'plugin' => false));
  543.                         }
  544.  
  545.                         $this->User->setKey('rewards_waited', (intval($this->User->getKey('rewards_waited')) - 1));
  546.                         $this->Session->setFlash($rewardStatus['msg'], 'default.success');
  547.             $this->redirect(array('controller' => 'user', 'action' => 'profile', 'plugin' => false));
  548.  
  549.         } else {
  550.             $this->redirect('/');
  551.         }
  552.     }
  553.  
  554.     public function admin_index() {
  555.         if($this->isConnected AND $this->User->isAdmin()) {
  556.             $this->layout = "admin";
  557.  
  558.             $this->loadModel('Vote.VoteConfiguration');
  559.             $vote = $config = $this->VoteConfiguration->getConfig();
  560.             if(!empty($vote)) {
  561.                 $vote['rewards'] = $vote['rewards'];
  562.                 $vote['websites'] = $vote['websites'];
  563.             } else {
  564.                 $vote = array();
  565.             }
  566.             //debug($vote['rewards']);
  567.             $this->set(compact('vote'));
  568.  
  569.             $this->loadModel('Server');
  570.  
  571.             $servers = $this->Server->findSelectableServers(true);
  572.                         $this->set(compact('servers'));
  573.  
  574.                         $vote['servers'] = (isset($vote['servers'])) ? $vote['servers'] : array();
  575.             if(!empty($vote['servers'])) {
  576.                 $selected_server = array();
  577.                 foreach ($vote['servers'] as $key => $value) {
  578.                     if(isset($servers[$value])) {
  579.                         $selected_server[] = $value;
  580.                     }
  581.                 }
  582.             } else {
  583.                 $selected_server = array();
  584.             }
  585.             $this->set(compact('selected_server'));
  586.  
  587.             $this->loadModel('User');
  588.             $ranking = $this->User->find('all', array('limit' => '15', 'order' => 'vote desc'));
  589.             $this->set(compact('ranking'));
  590.  
  591.             $this->set('title_for_layout',$this->Lang->get('VOTE__TITLE'));
  592.         } else {
  593.             $this->redirect('/');
  594.         }
  595.     }
  596.  
  597.     public function admin_reset() {
  598.             $this->autoRender = false;
  599.         if($this->isConnected AND $this->User->isAdmin()) {
  600.  
  601.           $event = new CakeEvent('beforeResetVotes', $this, array('user' => $this->User->getAllFromCurrentUser()));
  602.           $this->getEventManager()->dispatch($event);
  603.           if($event->isStopped()) {
  604.             return $event->result;
  605.           }
  606.  
  607.           $this->loadModel('Notification');
  608.           $this->Notification->setToAll($this->Lang->get('VOTE__NOTIFICATION_RESET_VOTES'));
  609.  
  610.           $this->loadModel('Vote.Vote');
  611.           $this->Vote->deleteAll(array('1' => '1'));
  612.           $this->loadModel('User');
  613.           $this->User->updateAll(array('vote' => 0));
  614.           $this->History->set('RESET', 'vote');
  615.           $this->Session->setFlash($this->Lang->get('VOTE__RESET_SUCCESS'), 'default.success');
  616.           $this->redirect(array('controller' => 'voter', 'action' => 'index', 'admin' => true));
  617.         }
  618.     }
  619.  
  620.     public function admin_add_ajax() {
  621.             $this->autoRender = false;
  622.         if($this->isConnected AND $this->User->isAdmin()) {
  623.  
  624.             if($this->request->is('post')) {
  625.                 if(!empty($this->request->data['servers']) AND !empty($this->request->data['website'][0]['page_vote']) AND !empty($this->request->data['website'][0]['time_vote']) AND !empty($this->request->data['website'][0]['website_type']) AND $this->request->data['rewards_type'] == '0' OR $this->request->data['rewards_type'] == '1') {
  626.  
  627.                     $firstReward = @key($this->request->data['rewards']);
  628.                     if(isset($this->request->data['rewards']) && is_array($this->request->data['rewards']) && !empty($this->request->data['rewards']) && !empty($this->request->data['rewards'][$firstReward]['name']) && $this->request->data['rewards'][$firstReward]['name'] != "undefined" && !empty($this->request->data['rewards'][$firstReward]['type']) && $this->request->data['rewards'][$firstReward]['type'] != "undefined") {
  629.                         $this->loadModel('Vote.VoteConfiguration');
  630.                         /*
  631.                         REWARDS -> serialize();
  632.  
  633.                         Structure = array(
  634.                             array(
  635.                                 'type' => money/server
  636.                                 - 'how' => 10 - pour la money
  637.                                 - 'command' => say e - pour le server
  638.                             )
  639.  
  640.                         )
  641.  
  642.                         */
  643.  
  644.                         if($this->request->data['rewards_type'] == 0) {
  645.                             foreach ($this->request->data['rewards'] as $key => $value) {
  646.                                 if(!isset($value['proba']) || empty($value['proba'])) {
  647.                                     echo json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')));
  648.                                     return;
  649.                                 }
  650.                             }
  651.                         }
  652.  
  653.                         $rewards = serialize($this->request->data['rewards']);
  654.  
  655.                         $vote = $this->VoteConfiguration->getConfig();
  656.                         if(!empty($vote)) {
  657.                             $this->VoteConfiguration->read(null, 1);
  658.                         } else {
  659.                             $this->VoteConfiguration->create();
  660.                         }
  661.                         $this->VoteConfiguration->set(array(
  662.                             'rewards_type' => $this->request->data['rewards_type'],
  663.                             'rewards' => $rewards,
  664.                             'websites' => serialize($this->request->data['website']),
  665.                             'servers' => serialize($this->request->data['servers'])
  666.                         ));
  667.                         $this->VoteConfiguration->save();
  668.                         $this->History->set('EDIT_CONFIG', 'vote');
  669.                         $this->Session->setFlash($this->Lang->get('VOTE__CONFIGURATION_SUCCESS'), 'default.success');
  670.                         echo json_encode(array('statut' => true, 'msg' => $this->Lang->get('VOTE__CONFIGURATION_SUCCESS')));
  671.                     } else {
  672.                         echo json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')));
  673.                     }
  674.                 } else {
  675.                     echo json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')));
  676.                 }
  677.             } else {
  678.                 echo json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST')));
  679.             }
  680.         } else {
  681.             throw new ForbiddenException();
  682.         }
  683.     }
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement