Completed
Push — master ( 448bfe...be88f9 )
by Patrick
03:11
created

class.FlipsideCAPTCHA.php (3 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
require_once('Autoload.php');
3
class FlipsideCAPTCHA implements JsonSerializable
4
{
5
    public  $random_id;
6
    private $validIDs;
7
8
    public static function get_valid_captcha_ids()
9
    {
10
        $dataset = DataSetFactory::getDataSetByName('profiles');
11
        $datatable = $dataset['captcha'];
12
        $data = $datatable->read(false, array('id'));
13
        $count = count($data);
14
        for($i = 0; $i < $count; $i++)
15
        {
16
            $data[$i] = $data[$i]['id'];
17
        }
18
        return $data;
19
    }
20
21
    public static function get_all()
22
    {
23
        $res = array();
24
        $ids = FlipsideCAPTCHA::get_valid_captcha_ids();
25
        $count = count($ids);
26
        for($i = 0; $i < $count; $i++)
27
        {
28
            $captcha = new FlipsideCAPTCHA();
29
            $captcha->random_id = $ids[$i]; 
30
            array_push($res, $captcha);
31
        }
32
        return $res;
33
    }
34
35
    public static function save_new_captcha($question, $hint, $answer)
36
    {
37
        $dataset = DataSetFactory::getDataSetByName('profiles');
38
        $datatable = $dataset['captcha'];
39
        return $datatable->create(array('question'=>$question,'hint'=>$hint,'answer'=>$answer));
40
    }
41
42
    public function __construct()
43
    {
44
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
45
        $this->random_id = mt_rand(0, count($this->validIDs)-1);
46
        $this->random_id = $this->validIDs[$this->random_id];
47
    }
48
49 View Code Duplication
    public function get_question()
50
    {
51
        $dataset = DataSetFactory::getDataSetByName('profiles');
52
        $datatable = $dataset['captcha'];
53
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('question'));
0 ignored issues
show
'id eq ' . $this->random_id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        if($data === false)
55
        {
56
            return false;
57
        }
58
        return $data[0]['question'];
59
    }
60
61 View Code Duplication
    public function get_hint()
62
    {
63
        $dataset = DataSetFactory::getDataSetByName('profiles');
64
        $datatable = $dataset['captcha'];
65
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('hint'));
0 ignored issues
show
'id eq ' . $this->random_id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        if($data === false)
67
        {
68
            return false;
69
        }
70
        return $data[0]['hint'];
71
    }
72
73 View Code Duplication
    private function get_answer()
74
    {
75
        $dataset = DataSetFactory::getDataSetByName('profiles');
76
        $datatable = $dataset['captcha'];
77
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('answer'));
0 ignored issues
show
'id eq ' . $this->random_id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        if($data === false)
79
        {
80
            return false;
81
        }
82
        return $data[0]['answer'];
83
    }
84
85
    public function is_answer_right($answer)
86
    {
87
        return strcasecmp($this->get_answer(),$answer) == 0;
88
    }
89
90
    public function draw_captcha($explination=true, $return=false, $ownForm=false)
91
    {
92
        $string = '';
93
94
        if($ownForm)
95
        {
96
            $string.= '<form id="flipcaptcha" name="flipcaptcha">';
97
        }
98
99
        $string .= '<label for="captcha" class="col-sm-2 control-label">'.$this->get_question().'</label><div class="col-sm-10"><input class="form-control" type="text" id="captcha" name="captcha" placeholder="'.$this->get_hint().'" required/></div>';
100
        if($ownForm)
101
        {
102
            $string.='</form>';
103
        }
104
        if($explination)
105
        {
106
            $string .= '<div class="col-sm-10">The answer to this question may be found in the Burning Flipside Survival Guide. It may be found <a href="http://www.burningflipside.com/sg">here</a>.</div>';
107
        }
108
        
109
        if(!$return)
110
        {
111
            echo $string;
112
        }
113
        return $string;
114
    }
115
116
    public function self_json_encode()
117
    {
118
        return json_encode($this->jsonSerialize());
119
    }
120
121
    public function jsonSerialize()
122
    {
123
        $res = array();
124
        $res['id'] = $this->random_id;
125
        $res['question'] = $this->get_question();
126
        $res['hint'] = $this->get_hint();
127
        $res['answer'] = $this->get_answer();
128
        return $res;
129
    }
130
}
131
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
132
?>
133