Welcome Guest, Not a member yet? Register   Sign In
is making the CI super object a global varible instead of referencing it with $this a security hole
#1

[eluser]Sally D[/eluser]
I was fooling around and I made a code igniter library that evaluates a 5 card poker hand. Should I be using the $this->CI instead. What kind of security hole happens if I make it global to the function that needs it? It's a lot easier then typing $this->Ci all the that's why



Code:
// or should I reference the ci super object that I create in my library functions with the $this-> //key word
    function score_hand(){
        global $CI;
        if($CI->eval_hand->royal_flush()) {
                echo "royal flush";
                $this->cash+=1000;
                return;
            } else if($CI->eval_hand->straight_flush()) {
                echo "straight flush";
                $this->cash+=500;
                return;
            } else if($CI->eval_hand->four_of_a_kind()) {
                echo "four of a kind";
                $this->cash+=400;
                return;
            } else if($CI->eval_hand->full_house()){
                echo "full house";
                $this->cash+=300;
                return;
            } else if($CI->eval_hand->flush()) {
                echo "flush";
                $this->cash+=275;
                return;
            } else if($CI->eval_hand->straight()){
                echo "straight";
                $this->cash+=250;
                return;
            } else if($CI->eval_hand->three_of_a_kind()){
                echo "three of a kind";
                $this->cash+=120;
                return;
            } else if($CI->eval_hand->two_pair()){
                echo "two pair";
                $this->cash+=40;
                return;
                
                
            } else if($CI->eval_hand->pair()){
                
                echo "pair";
                $this->cash+=20;
                //echo $this->cash;
                return;
            } else {
                               echo "make a hi card algorithm";
                               return;
                        }
            
    }
#2

[eluser]nmweb[/eluser]
I'm not sure what you're trying to accomplish here. Also, probably no security hole with globalizing $CI but I don't quite understand your approach.
#3

[eluser]Sally D[/eluser]
I was board today so I wrote this code to try to make a poker card game I use this library to check and array of cards array('c1','h13','s1','d1') would produce a three_of a kind if that array was passed to it's constructor

I use this library along with another library that controls user events along with a controller to feed all this to a view file

it's not perfected yet but I can see all the pieces and I know I can make them fit

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*    welcome to my card algorithm
*      This program evaulates a 5 card poker hand
*      c=clubs, d=diamonds, h=hearts, s=spades, 1=ace
*
*/


// my poker hand
class Eval_hand  {
    var $hand;
    var $suits;
    var $ranks;
    var $counted_up_ranks;
    // x is the poker hand which is sorted
    function Eval_hand($x){
        $CI =& get_instance();
        if(count($x) == 5) {$this->hand=$x;}else{$this->hand=null;}
        $this->suits=array('c'=>array(), 'd'=>array(), 'h'=>array(), 's'=>array());
        $this->ranks=array();
    
        foreach($this->hand as $key=>$value) {
            if(strlen($value) == 3)  {
                $suit=substr($value, -3,1);
                $rank=(int)substr($value, -2);
                $this->ranks[] = $rank;
                $this->suits[$suit][] = $rank;
            } else if(strlen($value) == 2){
                $suit=substr($value, -2, 1) ;
                $rank=substr($value,-1);
                $this->ranks[] = $rank;
                $this->suits[$suit][]=$rank;
            }
        }
        $this->counted_up_ranks=array_count_values($this->ranks);
    }
    function test(){
        global $CI;
        print_r($this->hand);
        echo "<br>";
        print_r($this->counted_up_ranks);
    }
    function royal_flush()
    {
        if(in_array('1', $this->ranks, true))
        {
            $aces_hi=$this->ranks;
            sort($aces_hi);
            $aces_hi[0] = 14;
            sort($aces_hi);
            if(   ($aces_hi[0]+1) == ($aces_hi[1])
               && ($aces_hi[1]+1) == ($aces_hi[2])
               && ($aces_hi[2]+1) == ($aces_hi[3])
               && ($aces_hi[3]+1) == ($aces_hi[4]))
            {    
                 if( (count($this->suits['h'])==5)
                   || (count($this->suits['c']) == 5)
                   || (count($this->suits['s']) == 5)
                   || (count($this->suits['d']) == 5))
                {
                     return 1;    
                }
             }
        }
        else
        {
            return 0;
        }
    }        
    function straight_flush(){
        // code to test for a straight flush    
        if(   ($this->ranks[0]+1) == ($this->ranks[1])
           && ($this->ranks[1]+1) == ($this->ranks[2])
           && ($this->ranks[2]+1) == ($this->ranks[3])
           && ($this->ranks[3]+1) == ($this->ranks[4]))
        {
            if(   (count($this->suits['h'])==5) || (count($this->suits['c']) == 5)
               || (count($this->suits['s']) == 5)
               || (count($this->suits['d']) == 5))
            {
                return 1;    
            }
        }
        else
        {
            return 0;
        }
        
    }
    function four_of_a_kind(){
        if( (count($this->counted_up_ranks) == 2)
            && (in_array('4',$this->counted_up_ranks)))
        {
               return 1;
        }
        else
        {
            return 0;
        }
    }
    function full_house(){
        // code to test for a full house
        if((count($this->counted_up_ranks) == 2)
            && (in_array('3',$this->counted_up_ranks))
            && (in_array('2',$this->counted_up_ranks)))
        {
               return 1;
        }
        else
        {
            return 0;
        }
    }
    function flush(){
        // code to test for a flush
        if( (count($this->suits['h'])==5)
            || (count($this->suits['c']) == 5)
            || (count($this->suits['s']) == 5)
            || (count($this->suits['d']) == 5))
        {
            return 1;
        }    
        else
        {
            return 0;
        }
    }
    function straight() {
            if( ($this->ranks[0]+1) == ($this->ranks[1])
                &&  ($this->ranks[1]+1) == ($this->ranks[2])
                &&  ($this->ranks[2]+1) == ($this->ranks[3])
                &&  ($this->ranks[3]+1) == ($this->ranks[4]))
        {
            // return 5 for just a straight
            return 1;
        }
        else
        {
            return 0;
        }
    }
    function three_of_a_kind() {
        // code to
            if( (count($this->counted_up_ranks) == 3)
            && (in_array('3',$this->counted_up_ranks)))
        {
               return 1;
        }
        else
        {
            return 0;
        }
    }
    function two_pair(){
            if( (count($this->counted_up_ranks) == 3)
            && (in_array('2',$this->counted_up_ranks)))
        {
               return 1;
        }
        else
        {
            return 0;
        }
        
    }
    function pair(){
            if( (count($this->counted_up_ranks) == 4)
            && (in_array('2',$this->counted_up_ranks)))
        {
               return 1;
        }
        else
        {
            return 0;
        }
        
    }
    function high_card(){
        return "not yet implemented";
    }
    
}    
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB