Advertisement
Guest User

Untitled

a guest
May 30th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?php
  2.     class AionVote{
  3.         protected $data;
  4.         protected $connection;
  5.         function AionVote(){
  6.             //theoriticly there should be database manager class to conect once and the only return instance of conection
  7.             //but you can just initialize it here
  8.             //this data should be in some configuration file
  9.             $user = "aionrenegade";
  10.             $password = "aiondevwashere";
  11.             $database = "au_server_gs3";
  12.             // Connect to the database
  13.             $this->connection = mysql_connect("74.208.218.54",$user,$password) or die ('Could not connect: ' . mysql_error());
  14.             mysql_select_db($database, $this->connection);
  15.         }
  16.         public function getData($account_id){
  17.             //check if $account_id isn't account name it should be a number id of account
  18.             $this->data = array();
  19.            
  20.            
  21.             //feeding data about account character
  22.             $query = mysql_query("SELECT id, name FROM players WHERE account_name ='$account_id'") or die("ERROR:".mysql_error());
  23.            
  24.             while($row = mysql_fetch_array( $query )){
  25.                 $this->data['account_characters'][]=$row;
  26.             }
  27.            
  28.            
  29.             //feeding data about avible prizes
  30.             $query = mysql_query("SELECT prize_id, prize_name FROM vote_prizes") or die("ERROR:".mysql_error());
  31.            
  32.             while($row = mysql_fetch_array( $query )){
  33.                 $this->data['voting_prizes'][]=$row;
  34.             }
  35.            
  36.            
  37.             //feeding data about voting pages
  38.             $query = mysql_query("SELECT id, link, img FROM vote_page_list") or die("ERROR:".mysql_error());
  39.            
  40.             while($row = mysql_fetch_array( $query )){
  41.                 $lastvoted_query = mysql_query("SELECT time,CURRENT_TIMESTAMP-time dif FROM vote_logs WHERE account_name='$account_id' AND link_id='$row[id]'") or die("ERROR:".mysql_error());
  42.                 if(mysql_num_rows($lastvoted_query)>0){
  43.                     $lastvoted = mysql_fetch_array( $lastvoted_query );
  44.                 }
  45.                 else{
  46.                     $lastvoted = array('time'=>'You may now vote.');
  47.                 }
  48.                 $this->data['voting_pages'][]=array_merge($row,$lastvoted);
  49.             }
  50.            
  51.             return $this->data;
  52.         }
  53.         public function generateLinks(){
  54.             $list='';
  55.             for($i=0;$i<count($this->data['voting_pages']);$i++){
  56.                 $list.=$this->linkSchema($this->data['voting_pages'][$i]['id'],$this->data['voting_pages'][$i]['link'],$this->data['voting_pages'][$i]['img'],$this->data['voting_pages'][$i]['time']);
  57.             }
  58.             return $list;
  59.         }
  60.         protected function linkSchema($id,$link,$img,$last){
  61.             return '<div class="vote"><a class="vote" onclick="return AionVote.open(this,'.$id.')" href="'.$link.'" target="_blank"><img width="65" height="42" src="'.$img.'"/></a><p>Last time voted:</p><span id="vote_comment_'.$id.'">'.$last.'</span></div>'."\n";
  62.         }
  63.        
  64.         public function generateAccountCharacterHTMLOptions(){
  65.             $options='';
  66.             for($i=0;$i<count($this->data['account_characters']);$i++){
  67.                 $options.='<option value="'.$this->data['account_characters'][$i]['id'].'">'.$this->data['account_characters'][$i]['name'].'</option>';
  68.             }
  69.             return $options;
  70.         }
  71.        
  72.         public function generatePrizeHTMLOptions(){
  73.             $options='';
  74.             for($i=0;$i<count($this->data['voting_prizes']);$i++){
  75.                 $options.='<option value="'.$this->data['voting_prizes'][$i]['prize_id'].'">'.$this->data['voting_prizes'][$i]['prize_name'].'</option>';
  76.             }
  77.             return $options;
  78.         }
  79.        
  80.         public function saveVote($account_id,$link_id,$prize_id){
  81.             //security here
  82.             $link_id = intval($link_id);
  83.             //$char_id = intval($char_id);
  84.             $prize_id = intval($prize_id);
  85.             if($account_id!=0&&$link_id!=0&&$prize_id!=0){
  86.                 //checking if player already voted
  87.                 $query = mysql_query("SELECT * FROM vote_logs WHERE account_name='$account_id' AND link_id='$link_id'")or die("ERROR:".mysql_error());
  88.                
  89.                 if(mysql_num_rows($query)>0)//if there is already a row with such vote we will update it if the time from last update is more than 12h
  90.                 {
  91.                     mysql_query("UPDATE vote_logs SET prize_id='$prize_id', time=CURRENT_TIMESTAMP WHERE account_name='$account_id' AND link_id='$link_id' AND CURRENT_TIMESTAMP>DATE_ADD(time,INTERVAL 12 HOUR)")or die("ERROR:".mysql_error());
  92.  
  93.                     if(mysql_affected_rows()==0){
  94.                         return "Last voting occurred less than 12h from now.";
  95.                     }
  96.                 }
  97.                 else//else we will insert new row
  98.                 {
  99.                     mysql_query("INSERT INTO vote_logs (account_name,link_id,prize_id) VALUES ('$account_id','$link_id','$prize_id')")or die("ERROR:".mysql_error());
  100.                 }
  101.             }
  102.         }
  103.     }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement