These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | public $wwwUrl; |
||
34 | |||
35 | /** |
||
36 | * Get all valid CAPTCH IDs |
||
37 | * |
||
38 | * @return array An array of captch IDs |
||
39 | * |
||
40 | * @deprecated 2.1 Will be removed in favor of self::getValidCaptchaIDs() |
||
41 | */ |
||
42 | public static function get_valid_captcha_ids() |
||
43 | { |
||
44 | return self::getValidCaptchaIDs(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get all valid CAPTCH IDs |
||
49 | * |
||
50 | * @return array An array of captch IDs |
||
51 | */ |
||
52 | public static function getValidCaptchaIDs() |
||
53 | { |
||
54 | $datatable = DataSetFactory::getDataTableByNames('profiles', 'captcha'); |
||
55 | $data = $datatable->read(false, array('id')); |
||
56 | $count = count($data); |
||
57 | for($i = 0; $i < $count; $i++) |
||
58 | { |
||
59 | $data[$i] = $data[$i]['id']; |
||
60 | } |
||
61 | return $data; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get an array of all CAPTCHAs |
||
66 | * |
||
67 | * @return array An array of captchas |
||
68 | * |
||
69 | * @deprecated 2.1 Will be removed in favor of self::getAll() |
||
70 | */ |
||
71 | public static function get_all() |
||
72 | { |
||
73 | return self::getAll(); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Get an array of all CAPTCHAs |
||
78 | * |
||
79 | * @return array An array of captchas |
||
80 | */ |
||
81 | public static function getAll() |
||
82 | { |
||
83 | $res = array(); |
||
84 | $ids = self::getValidCaptchaIDs(); |
||
85 | $count = count($ids); |
||
86 | for($i = 0; $i < $count; $i++) |
||
87 | { |
||
88 | $captcha = new FlipsideCAPTCHA(); |
||
89 | $captcha->random_id = $ids[$i]; |
||
90 | array_push($res, $captcha); |
||
91 | } |
||
92 | return $res; |
||
93 | } |
||
94 | |||
95 | public static function save_new_captcha($question, $hint, $answer) |
||
96 | { |
||
97 | $dataset = DataSetFactory::getDataSetByName('profiles'); |
||
98 | $datatable = $dataset['captcha']; |
||
99 | return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer)); |
||
100 | } |
||
101 | |||
102 | public function __construct() |
||
103 | { |
||
104 | $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids(); |
||
105 | $this->random_id = mt_rand(0, count($this->validIDs) - 1); |
||
106 | $this->random_id = $this->validIDs[$this->random_id]; |
||
107 | $this->settings = \Settings::getInstance(); |
||
0 ignored issues
–
show
|
|||
108 | $this->wwwUrl = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com/'); |
||
109 | } |
||
110 | |||
111 | protected function getCaptchField($fieldName) |
||
112 | { |
||
113 | $dataset = DataSetFactory::getDataSetByName('profiles'); |
||
114 | $datatable = $dataset['captcha']; |
||
115 | $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array($fieldName)); |
||
116 | if(empty($data)) |
||
117 | { |
||
118 | return false; |
||
119 | } |
||
120 | return $data[0][$fieldName]; |
||
121 | } |
||
122 | |||
123 | public function get_question() |
||
124 | { |
||
125 | return $this->getCaptchField('question'); |
||
126 | } |
||
127 | |||
128 | public function get_hint() |
||
129 | { |
||
130 | return $this->getCaptchField('hint'); |
||
131 | } |
||
132 | |||
133 | private function get_answer() |
||
134 | { |
||
135 | return $this->getCaptchField('answer'); |
||
136 | } |
||
137 | |||
138 | public function is_answer_right($answer) |
||
139 | { |
||
140 | return strcasecmp($this->get_answer(), $answer) === 0; |
||
141 | } |
||
142 | |||
143 | public function draw_captcha($explination = true, $return = false, $ownForm = false) |
||
144 | { |
||
145 | $string = ''; |
||
146 | |||
147 | if($ownForm) |
||
148 | { |
||
149 | $string .= '<form id="flipcaptcha" name="flipcaptcha">'; |
||
150 | } |
||
151 | |||
152 | $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>'; |
||
153 | if($ownForm) |
||
154 | { |
||
155 | $string .= '</form>'; |
||
156 | } |
||
157 | if($explination) |
||
158 | { |
||
159 | $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>'; |
||
160 | } |
||
161 | |||
162 | if(!$return) |
||
163 | { |
||
164 | echo $string; |
||
165 | } |
||
166 | return $string; |
||
167 | } |
||
168 | |||
169 | public function self_json_encode() |
||
170 | { |
||
171 | return json_encode($this->jsonSerialize()); |
||
172 | } |
||
173 | |||
174 | public function jsonSerialize() |
||
175 | { |
||
176 | $res = array(); |
||
177 | $res['id'] = $this->random_id; |
||
178 | $res['question'] = $this->get_question(); |
||
179 | $res['hint'] = $this->get_hint(); |
||
180 | $res['answer'] = $this->get_answer(); |
||
181 | return $res; |
||
182 | } |
||
183 | } |
||
184 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
185 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: