source: extensions/EasyCaptcha/tictac/functions_tictac.inc.php @ 24215

Last change on this file since 24215 was 24215, checked in by mistic100, 11 years ago

add extension EasyCaptcha

File size: 6.8 KB
Line 
1<?php
2defined('EASYCAPTCHA_ID') or die('Hacking attempt!');
3
4function hex2rgb($hex)
5{
6  $hex = ltrim($hex, '#');
7
8  if (strlen($hex) == 3)
9  {
10    $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
11  }
12  else if (strlen($hex) != 6)
13  {
14    return array(0,0,0);
15  }
16
17  $int = hexdec($hex);
18  return array(0xFF&($int>>0x10), 0xFF&($int>>0x8), 0xFF&$int);
19}
20
21function imagecolorallocatehex(&$img, $hex)
22{
23  $rgb = hex2rgb($hex);
24  return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
25}
26
27function imagegradientrectangle(&$img, $start, $end)
28{
29  $line_numbers = imagesx($img);
30  $line_width = imagesy($img);
31
32  list($r1,$g1,$b1) = $start;
33  list($r2,$g2,$b2) = $end;
34  list($r,$g,$b) = $end;
35
36  $fill = imagecolorallocate($img, $r, $g, $b);
37
38  for ($i=0; $i<$line_numbers; $i++)
39  {
40    $old_r = $r;
41    $old_g = $g;
42    $old_b = $b;
43
44    $r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
45    $g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
46    $b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
47
48    if ("$old_r $old_g $old_b" != "$r $g $b")
49    {
50      $fill = imagecolorallocate($img, $r, $g, $b);
51    }
52    imagefilledrectangle($img, 0, $i, $line_width, $i, $fill);
53  }
54}
55
56function drawcircle(&$img, $pos, $color)
57{
58  global $conf, $props, $img_circle;
59
60  if (!is_resource($img_circle))
61  {
62    $img_circle = imagecreatetruecolor($props['box_size'], $props['box_size']);
63
64    $bg = imagecolorallocatehex($img_circle, $conf['EasyCaptcha']['tictac']['bg1']);
65    $obj = imagecolorallocatehex($img_circle, $color);
66
67    imagefilledrectangle($img_circle, 0, 0, $props['box_size'], $props['box_size'], $bg);
68    imagecolortransparent($img_circle, $bg);
69
70    $radius = $props['box_size'] - $props['pad']*2;
71    $radius2 = $radius - 2*sqrt(2*$props['pad']*$props['pad']);
72
73    imagefilledellipse($img_circle, $props['box_size']/2, $props['box_size']/2, $radius, $radius, $obj);
74    imagefilledellipse($img_circle, $props['box_size']/2, $props['box_size']/2, $radius2, $radius2, $bg);
75  }
76
77  $pos = array(
78    $pos[0]*($props['bd_size']+$props['box_size']) + $props['bd_size'],
79    $pos[1]*($props['bd_size']+$props['box_size']) + $props['bd_size'],
80    );
81
82  imagecopymerge($img, $img_circle, $pos[0], $pos[1], 0, 0, $props['box_size'], $props['box_size'], 100);
83}
84
85function drawcross(&$img, $pos, $color)
86{
87  global $conf, $props, $img_cross;
88
89  if (!is_resource($img_cross))
90  {
91    $img_cross = imagecreatetruecolor($props['box_size'], $props['box_size']);
92
93    $bg = imagecolorallocatehex($img_cross, $conf['EasyCaptcha']['tictac']['bg1']);
94    $obj = imagecolorallocatehex($img_cross, $color);
95
96    imagefilledrectangle($img_cross, 0, 0, $props['box_size'], $props['box_size'], $bg);
97    imagecolortransparent($img_cross, $bg);
98
99    $points1 = array(
100      $props['pad']*2,             $props['pad'],
101      $props['box_size'] - $props['pad'],   $props['box_size'] - $props['pad']*2,
102      $props['box_size'] - $props['pad']*2, $props['box_size'] - $props['pad'],
103      $props['pad'],               $props['pad']*2,
104      );
105
106    $points2 = array(
107      $props['box_size'] - $props['pad']*2, $props['pad'],
108      $props['box_size'] - $props['pad'],   $props['pad']*2,
109      $props['pad']*2,             $props['box_size'] - $props['pad'],
110      $props['pad'],               $props['box_size'] - $props['pad']*2,
111      );
112
113    imagefilledpolygon($img_cross, $points1, 4, $obj);
114    imagefilledpolygon($img_cross, $points2, 4, $obj);
115  }
116
117  $pos = array(
118    $pos[0]*($props['bd_size']+$props['box_size']) + $props['bd_size'],
119    $pos[1]*($props['bd_size']+$props['box_size']) + $props['bd_size'],
120    );
121
122  imagecopymerge($img, $img_cross, $pos[0], $pos[1], 0, 0, $props['box_size'], $props['box_size'], 100);
123}
124
125function checkline($pos, $existing)
126{
127  $existing[$pos[0]][$pos[1]] = true;
128
129  // check col
130  $nb = 0;
131  for ($l=0; $l<3; $l++)
132  {
133    if (isset($existing[$pos[0]][$l])) $nb++;
134  }
135  if ($nb==3) return true;
136
137  // check line
138  $nb = 0;
139  for ($c=0; $c<3; $c++)
140  {
141    if (isset($existing[$c][$pos[1]])) $nb++;
142  }
143  if ($nb==3) return true;
144
145  // check diag 1
146  $nb = 0;
147  for ($i=0; $i<3; $i++)
148  {
149    if (isset($existing[$i][$i])) $nb++;
150  }
151  if ($nb==3) return true;
152
153  // check diag 2
154  $nb = 0;
155  for ($i=0; $i<3; $i++)
156  {
157    if (isset($existing[$i][2-$i])) $nb++;
158  }
159  if ($nb==3) return true;
160
161  return false;
162}
163
164function get_configs()
165{
166  return array(
167    // line 1
168    array(
169      'checked' => array(array(0,0),array(0,1)),
170      'answer' => array(0,2),
171      ),
172    array(
173      'checked' => array(array(0,0),array(0,2)),
174      'answer' => array(0,1),
175      ),
176    array(
177      'checked' => array(array(0,1),array(0,2)),
178      'answer' => array(0,0),
179      ),
180    // line 2
181    array(
182      'checked' => array(array(1,0),array(1,1)),
183      'answer' => array(1,2),
184      ),
185    array(
186      'checked' => array(array(1,0),array(1,2)),
187      'answer' => array(1,1),
188      ),
189    array(
190      'checked' => array(array(1,1),array(1,2)),
191      'answer' => array(1,0),
192      ),
193    // line 3
194    array(
195      'checked' => array(array(2,0),array(2,1)),
196      'answer' => array(2,2),
197      ),
198    array(
199      'checked' => array(array(2,0),array(2,2)),
200      'answer' => array(2,1),
201      ),
202    array(
203      'checked' => array(array(2,1),array(2,2)),
204      'answer' => array(2,0),
205      ),
206    // col 1
207    array(
208      'checked' => array(array(0,0),array(1,0)),
209      'answer' => array(2,0),
210      ),
211    array(
212      'checked' => array(array(0,0),array(2,0)),
213      'answer' => array(1,0),
214      ),
215    array(
216      'checked' => array(array(1,0),array(2,0)),
217      'answer' => array(0,0),
218      ),
219    // col 2
220    array(
221      'checked' => array(array(0,1),array(1,1)),
222      'answer' => array(2,1),
223      ),
224    array(
225      'checked' => array(array(0,1),array(2,1)),
226      'answer' => array(1,1),
227      ),
228    array(
229      'checked' => array(array(1,1),array(2,1)),
230      'answer' => array(0,1),
231      ),
232    // col 3
233    array(
234      'checked' => array(array(0,2),array(1,2)),
235      'answer' => array(2,2),
236      ),
237    array(
238      'checked' => array(array(0,2),array(2,2)),
239      'answer' => array(1,2),
240      ),
241    array(
242      'checked' => array(array(1,2),array(2,2)),
243      'answer' => array(0,2),
244      ),
245    // diag 1
246    array(
247      'checked' => array(array(0,0),array(1,1)),
248      'answer' => array(2,2),
249      ),
250    array(
251      'checked' => array(array(0,0),array(2,2)),
252      'answer' => array(1,1),
253      ),
254    array(
255      'checked' => array(array(1,1),array(2,2)),
256      'answer' => array(0,0),
257      ),
258    // diag 2
259    array(
260      'checked' => array(array(2,0),array(1,1)),
261      'answer' => array(0,2),
262      ),
263    array(
264      'checked' => array(array(2,0),array(0,2)),
265      'answer' => array(1,1),
266      ),
267    array(
268      'checked' => array(array(1,1),array(0,2)),
269      'answer' => array(2,0),
270      ),
271    );
272}
Note: See TracBrowser for help on using the repository browser.