Completed
Push — master ( 929926...835aea )
by Patrick
09:00 queued 06:35
created

FlipsideCAPTCHA::get_hint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * FlipsideCAPTCHA class
4
 *
5
 * This file describes the FlipsideCAPTCHA class
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * Allow other classes to be loaded as needed
16
 */
17
require_once('Autoload.php');
18
19
/**
20
 * A class to represent a Completely Automated Public Turing test to tell Computers and Humans Apart
21
 */
22
class FlipsideCAPTCHA implements JsonSerializable
23
{
24
    /**
25
     * The ID of the CAPTCHA in the DB
26
     */
27
    public  $random_id;
28
    /**
29
     * An array of all valid IDs in the DB
30
     */
31
    private $validIDs;
32
33
    /**
34
     * Get all valid CAPTCH IDs
35
     *
36
     * @return array An array of captch IDs
37
     *
38
     * @deprecated 2.1 Will be removed in favor of self::getValidCaptchaIDs()
39
     */
40
    public static function get_valid_captcha_ids()
41
    {
42
        return self::getValidCaptchaIDs();
43
    }
44
45
    /**
46
     * Get all valid CAPTCH IDs
47
     *
48
     * @return array An array of captch IDs
49
     */
50
    public static function getValidCaptchaIDs()
51
    {
52
        $datatable = DataSetFactory::getDataTableByNames('profiles', 'captcha');
53
        $data = $datatable->read(false, array('id'));
54
        $count = count($data);
55
        for($i = 0; $i < $count; $i++)
56
        {
57
            $data[$i] = $data[$i]['id'];
58
        }
59
        return $data;
60
    }
61
62
    /**
63
     * Get an array of all CAPTCHAs
64
     *
65
     * @return array An array of captchas
66
     *
67
     * @deprecated 2.1 Will be removed in favor of self::getAll()
68
     */
69
    public static function get_all()
70
    {
71
        return self::getAll();
72
    }
73
74
    /**
75
     * Get an array of all CAPTCHAs
76
     *
77
     * @return array An array of captchas
78
     */
79
    public static function getAll()
80
    {
81
        $res = array();
82
        $ids = self::getValidCaptchaIDs();
83
        $count = count($ids);
84
        for($i = 0; $i < $count; $i++)
85
        {
86
            $captcha = new FlipsideCAPTCHA();
87
            $captcha->random_id = $ids[$i];
88
            array_push($res, $captcha);
89
        }
90
        return $res;
91
    }
92
93
    public static function save_new_captcha($question, $hint, $answer)
94
    {
95
        $dataset = DataSetFactory::getDataSetByName('profiles');
96
        $datatable = $dataset['captcha'];
97
        return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer));
98
    }
99
100
    public function __construct()
101
    {
102
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
0 ignored issues
show
Deprecated Code introduced by
The method FlipsideCAPTCHA::get_valid_captcha_ids() has been deprecated with message: 2.1 Will be removed in favor of self::getValidCaptchaIDs()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
103
        $this->random_id = mt_rand(0, count($this->validIDs) - 1);
104
        $this->random_id = $this->validIDs[$this->random_id];
105
    }
106
107
    protected function getCaptchField($fieldName)
108
    {
109
        $dataset = DataSetFactory::getDataSetByName('profiles');
110
        $datatable = $dataset['captcha'];
111
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array($fieldName));
112
        if(empty($data))
113
        {
114
            return false;
115
        }
116
        return $data[0][$fieldName];
117
    }
118
119
    public function get_question()
120
    {
121
        return $this->getCaptchField('question');
122
    }
123
124
    public function get_hint()
125
    {
126
        return $this->getCaptchField('hint');
127
    }
128
129
    private function get_answer()
130
    {
131
        return $this->getCaptchField('answer');
132
    }
133
134
    public function is_answer_right($answer)
135
    {
136
        return strcasecmp($this->get_answer(), $answer) === 0;
137
    }
138
139
    public function draw_captcha($explination = true, $return = false, $ownForm = false)
140
    {
141
        $string = '';
142
143
        if($ownForm)
144
        {
145
            $string .= '<form id="flipcaptcha" name="flipcaptcha">';
146
        }
147
148
        $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>';
149
        if($ownForm)
150
        {
151
            $string .= '</form>';
152
        }
153
        if($explination)
154
        {
155
            $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>';
156
        }
157
        
158
        if(!$return)
159
        {
160
            echo $string;
161
        }
162
        return $string;
163
    }
164
165
    public function self_json_encode()
166
    {
167
        return json_encode($this->jsonSerialize());
168
    }
169
170
    public function jsonSerialize()
171
    {
172
        $res = array();
173
        $res['id'] = $this->random_id;
174
        $res['question'] = $this->get_question();
175
        $res['hint'] = $this->get_hint();
176
        $res['answer'] = $this->get_answer();
177
        return $res;
178
    }
179
}
180
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
181