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