Completed
Pull Request — develop (#134)
by Patrick
03:00 queued 44s
created

FlipsideCAPTCHA::get_all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    public static function getValidCaptchaIDs()
42
    {
43
        $datatable = DataSetFactory::getDataTableByNames('profiles', 'captcha');
44
        $data = $datatable->read(false, array('id'));
45
        $count = count($data);
46
        for($i = 0; $i < $count; $i++)
47
        {
48
            $data[$i] = $data[$i]['id'];
49
        }
50
        return $data;
51
    }
52
53
    /**
54
     * Get an array of all CAPTCHAs
55
     *
56
     * @return array An array of captchas
57
     */
58
    public static function getAll()
59
    {
60
        $res = array();
61
        $ids = self::getValidCaptchaIDs();
62
        $count = count($ids);
63
        for($i = 0; $i < $count; $i++)
64
        {
65
            $captcha = new FlipsideCAPTCHA();
66
            $captcha->random_id = $ids[$i];
67
            array_push($res, $captcha);
68
        }
69
        return $res;
70
    }
71
72
    public static function save_new_captcha($question, $hint, $answer)
73
    {
74
        $dataset = DataSetFactory::getDataSetByName('profiles');
75
        $datatable = $dataset['captcha'];
76
        return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer));
77
    }
78
79
    public function __construct()
80
    {
81
        $this->validIDs = FlipsideCAPTCHA::getValidCaptchaIDs();
82
        $this->random_id = mt_rand(0, count($this->validIDs) - 1);
83
        $this->random_id = $this->validIDs[$this->random_id];
84
        $settings = Settings::getInstance();
85
        $this->wwwUrl = $settings->getGlobalSetting('www_url', 'https://www.burningflipside.com');
86
    }
87
88
    protected function getCaptchField($fieldName)
89
    {
90
        $dataset = DataSetFactory::getDataSetByName('profiles');
91
        $datatable = $dataset['captcha'];
92
        $data = $datatable->read(new \Flipside\Data\Filter('id eq '.$this->random_id), array($fieldName));
93
        if(empty($data))
94
        {
95
            return false;
96
        }
97
        return $data[0][$fieldName];
98
    }
99
100
    public function get_question()
101
    {
102
        return $this->getCaptchField('question');
103
    }
104
105
    public function get_hint()
106
    {
107
        return $this->getCaptchField('hint');
108
    }
109
110
    private function get_answer()
111
    {
112
        return $this->getCaptchField('answer');
113
    }
114
115
    public function is_answer_right($answer)
116
    {
117
        return strcasecmp($this->get_answer(), $answer) === 0;
118
    }
119
120
    public function draw_captcha($explination = true, $return = false, $ownForm = false)
121
    {
122
        $string = '';
123
124
        if($ownForm)
125
        {
126
            $string .= '<form id="flipcaptcha" name="flipcaptcha">';
127
        }
128
129
        $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>';
130
        if($ownForm)
131
        {
132
            $string .= '</form>';
133
        }
134
        if($explination)
135
        {
136
            $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>';
137
        }
138
        
139
        if(!$return)
140
        {
141
            echo $string;
142
        }
143
        return $string;
144
    }
145
146
    public function self_json_encode()
147
    {
148
        return json_encode($this->jsonSerialize());
149
    }
150
151
    public function jsonSerialize()
152
    {
153
        $res = array();
154
        $res['id'] = $this->random_id;
155
        $res['question'] = $this->get_question();
156
        $res['hint'] = $this->get_hint();
157
        $res['answer'] = $this->get_answer();
158
        return $res;
159
    }
160
}
161
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
162