Completed
Push — develop ( 9a1311...2592a6 )
by Patrick
04:52 queued 02:48
created

FlipsideCAPTCHA.php (1 issue)

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
namespace Flipside;
3
/**
4
 * FlipsideCAPTCHA class
5
 *
6
 * This file describes the FlipsideCAPTCHA class
7
 *
8
 * PHP version 5 and 7
9
 *
10
 * @author Patrick Boyd / [email protected]
11
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
12
 * @license http://www.apache.org/licenses/ Apache 2.0 License
13
 */
14
15
/**
16
 * Allow other classes to be loaded as needed
17
 */
18
require_once('Autoload.php');
19
20
/**
21
 * A class to represent a Completely Automated Public Turing test to tell Computers and Humans Apart
22
 */
23
class FlipsideCAPTCHA implements \JsonSerializable
24
{
25
    /**
26
     * The ID of the CAPTCHA in the DB
27
     */
28
    public  $random_id;
29
    /**
30
     * An array of all valid IDs in the DB
31
     */
32
    private $validIDs;
33
34
    public  $wwwUrl;
35
36
    /**
37
     * Get all valid CAPTCH IDs
38
     *
39
     * @return array An array of captch IDs
40
     *
41
     * @deprecated 2.1 Will be removed in favor of self::getValidCaptchaIDs()
42
     */
43
    public static function get_valid_captcha_ids()
44
    {
45
        return self::getValidCaptchaIDs();
46
    }
47
48
    /**
49
     * Get all valid CAPTCH IDs
50
     *
51
     * @return array An array of captch IDs
52
     */
53
    public static function getValidCaptchaIDs()
54
    {
55
        $datatable = DataSetFactory::getDataTableByNames('profiles', 'captcha');
56
        $data = $datatable->read(false, array('id'));
57
        $count = count($data);
58
        for($i = 0; $i < $count; $i++)
59
        {
60
            $data[$i] = $data[$i]['id'];
61
        }
62
        return $data;
63
    }
64
65
    /**
66
     * Get an array of all CAPTCHAs
67
     *
68
     * @return array An array of captchas
69
     *
70
     * @deprecated 2.1 Will be removed in favor of self::getAll()
71
     */
72
    public static function get_all()
73
    {
74
        return self::getAll();
75
    }
76
77
    /**
78
     * Get an array of all CAPTCHAs
79
     *
80
     * @return array An array of captchas
81
     */
82
    public static function getAll()
83
    {
84
        $res = array();
85
        $ids = self::getValidCaptchaIDs();
86
        $count = count($ids);
87
        for($i = 0; $i < $count; $i++)
88
        {
89
            $captcha = new FlipsideCAPTCHA();
90
            $captcha->random_id = $ids[$i];
91
            array_push($res, $captcha);
92
        }
93
        return $res;
94
    }
95
96
    public static function save_new_captcha($question, $hint, $answer)
97
    {
98
        $dataset = DataSetFactory::getDataSetByName('profiles');
99
        $datatable = $dataset['captcha'];
100
        return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer));
101
    }
102
103
    public function __construct()
104
    {
105
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
0 ignored issues
show
Deprecated Code introduced by
The method Flipside\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...
106
        $this->random_id = mt_rand(0, count($this->validIDs) - 1);
107
        $this->random_id = $this->validIDs[$this->random_id];
108
        $settings = Settings::getInstance();
109
        $this->wwwUrl = $settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
110
    }
111
112
    protected function getCaptchField($fieldName)
113
    {
114
        $dataset = DataSetFactory::getDataSetByName('profiles');
115
        $datatable = $dataset['captcha'];
116
        $data = $datatable->read(new \Flipside\Data\Filter('id eq '.$this->random_id), array($fieldName));
117
        if(empty($data))
118
        {
119
            return false;
120
        }
121
        return $data[0][$fieldName];
122
    }
123
124
    public function get_question()
125
    {
126
        return $this->getCaptchField('question');
127
    }
128
129
    public function get_hint()
130
    {
131
        return $this->getCaptchField('hint');
132
    }
133
134
    private function get_answer()
135
    {
136
        return $this->getCaptchField('answer');
137
    }
138
139
    public function is_answer_right($answer)
140
    {
141
        return strcasecmp($this->get_answer(), $answer) === 0;
142
    }
143
144
    public function draw_captcha($explination = true, $return = false, $ownForm = false)
145
    {
146
        $string = '';
147
148
        if($ownForm)
149
        {
150
            $string .= '<form id="flipcaptcha" name="flipcaptcha">';
151
        }
152
153
        $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>';
154
        if($ownForm)
155
        {
156
            $string .= '</form>';
157
        }
158
        if($explination)
159
        {
160
            $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="'.$this->wwwUrl.'/sg">here</a>.</div>';
161
        }
162
        
163
        if(!$return)
164
        {
165
            echo $string;
166
        }
167
        return $string;
168
    }
169
170
    public function self_json_encode()
171
    {
172
        return json_encode($this->jsonSerialize());
173
    }
174
175
    public function jsonSerialize()
176
    {
177
        $res = array();
178
        $res['id'] = $this->random_id;
179
        $res['question'] = $this->get_question();
180
        $res['hint'] = $this->get_hint();
181
        $res['answer'] = $this->get_answer();
182
        return $res;
183
    }
184
}
185
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
186