Révision ac1bc5de
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/captcha/image_captcha/image_captcha.module | ||
---|---|---|
16 | 16 |
define('IMAGE_CAPTCHA_FILE_FORMAT_PNG', 2); |
17 | 17 |
define('IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG', 3); |
18 | 18 |
|
19 |
|
|
20 | 19 |
/** |
21 | 20 |
* Implements hook_help(). |
22 | 21 |
*/ |
... | ... | |
33 | 32 |
*/ |
34 | 33 |
function image_captcha_menu() { |
35 | 34 |
$items = array(); |
36 |
// add an administration tab for image_captcha
|
|
35 |
// Add an administration tab for image_captcha.
|
|
37 | 36 |
$items['admin/config/people/captcha/image_captcha'] = array( |
38 | 37 |
'title' => 'Image CAPTCHA', |
39 | 38 |
'file' => 'image_captcha.admin.inc', |
... | ... | |
50 | 49 |
'access arguments' => array('administer CAPTCHA settings'), |
51 | 50 |
'type' => MENU_CALLBACK, |
52 | 51 |
); |
53 |
// callback for generating an image
|
|
52 |
// Callback for generating an image.
|
|
54 | 53 |
$items['image_captcha'] = array( |
55 | 54 |
'file' => 'image_captcha.user.inc', |
56 | 55 |
'page callback' => 'image_captcha_image', |
... | ... | |
63 | 62 |
/** |
64 | 63 |
* Helper function for getting the fonts to use in the image CAPTCHA. |
65 | 64 |
* |
66 |
* @return a list of font paths. |
|
65 |
* @return array |
|
66 |
* a list of font paths. |
|
67 | 67 |
*/ |
68 | 68 |
function _image_captcha_get_enabled_fonts() { |
69 | 69 |
if (IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT & _image_captcha_check_setup(FALSE)) { |
... | ... | |
81 | 81 |
/** |
82 | 82 |
* Helper function for checking if the specified fonts are available. |
83 | 83 |
* |
84 |
* @param $fonts paths of fonts to check. |
|
85 |
* @return list($readable_fonts, $problem_fonts) |
|
84 |
* @param array $fonts |
|
85 |
* paths of fonts to check. |
|
86 |
* |
|
87 |
* @return array |
|
88 |
* list($readable_fonts, $problem_fonts) |
|
86 | 89 |
*/ |
87 | 90 |
function _image_captcha_check_fonts($fonts) { |
88 | 91 |
$readable_fonts = array(); |
... | ... | |
100 | 103 |
|
101 | 104 |
/** |
102 | 105 |
* Helper function for splitting an utf8 string correctly in characters. |
106 |
* |
|
103 | 107 |
* Assumes the given utf8 string is well formed. |
104 | 108 |
* See http://en.wikipedia.org/wiki/Utf8 for more info |
105 | 109 |
*/ |
106 | 110 |
function _image_captcha_utf8_split($str) { |
107 | 111 |
$characters = array(); |
108 | 112 |
$len = strlen($str); |
109 |
for ($i=0; $i < $len; ) {
|
|
113 |
for ($i = 0; $i < $len;) {
|
|
110 | 114 |
$chr = ord($str[$i]); |
111 |
if (($chr & 0x80) == 0x00) { // one byte character (0zzzzzzz) |
|
115 |
// One byte character (0zzzzzzz) |
|
116 |
if (($chr & 0x80) == 0x00) { |
|
112 | 117 |
$width = 1; |
113 | 118 |
} |
114 | 119 |
else { |
115 |
if (($chr & 0xE0) == 0xC0) { // two byte character (first byte: 110yyyyy) |
|
120 |
// Two byte character (first byte: 110yyyyy) |
|
121 |
if (($chr & 0xE0) == 0xC0) { |
|
116 | 122 |
$width = 2; |
117 | 123 |
} |
118 |
elseif (($chr & 0xF0) == 0xE0) { // three byte character (first byte: 1110xxxx) |
|
124 |
// Three byte character (first byte: 1110xxxx) |
|
125 |
elseif (($chr & 0xF0) == 0xE0) { |
|
119 | 126 |
$width = 3; |
120 | 127 |
} |
121 |
elseif (($chr & 0xF8) == 0xF0) { // four byte character (first byte: 11110www) |
|
128 |
// Four byte character (first byte: 11110www) |
|
129 |
elseif (($chr & 0xF8) == 0xF0) { |
|
122 | 130 |
$width = 4; |
123 | 131 |
} |
124 | 132 |
else { |
... | ... | |
140 | 148 |
* font files should be readable. |
141 | 149 |
* This functions checks these things. |
142 | 150 |
* |
143 |
* @param $check_fonts whether or not the enabled fonts should be checked. |
|
151 |
* @param bool $check_fonts |
|
152 |
* whether or not the enabled fonts should be checked. |
|
144 | 153 |
* |
145 |
* @return status code: bitwise 'OR' of status flags like |
|
154 |
* @return int |
|
155 |
* status code: bitwise 'OR' of status flags like |
|
146 | 156 |
* IMAGE_CAPTCHA_ERROR_NO_GDLIB, IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT, |
147 | 157 |
* IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM. |
148 | 158 |
*/ |
149 |
function _image_captcha_check_setup($check_fonts=TRUE) {
|
|
159 |
function _image_captcha_check_setup($check_fonts = TRUE) {
|
|
150 | 160 |
// Start clean. |
151 | 161 |
$status = 0; |
152 | 162 |
// Check if we can use the GD library. |
... | ... | |
170 | 180 |
} |
171 | 181 |
|
172 | 182 |
/** |
173 |
* Helper function for calculating image height and width |
|
174 |
* based on given code and current font/spacing settings. |
|
183 |
* Helper function for calculating image height and width based on given code and current font/spacing settings. |
|
175 | 184 |
* |
176 |
* @return array($width, $heigh) |
|
185 |
* @return array |
|
186 |
* array($width, $heigh) |
|
177 | 187 |
*/ |
178 | 188 |
function _image_captcha_image_size($code) { |
179 |
// Get settings |
|
189 |
// Get settings.
|
|
180 | 190 |
$font_size = (int) variable_get('image_captcha_font_size', 30); |
181 | 191 |
$character_spacing = (float) variable_get('image_captcha_character_spacing', '1.2'); |
182 | 192 |
$characters = _image_captcha_utf8_split($code); |
183 | 193 |
$character_quantity = count($characters); |
184 | 194 |
|
185 |
// Calculate height and width |
|
195 |
// Calculate height and width.
|
|
186 | 196 |
$width = $character_spacing * $font_size * $character_quantity; |
187 | 197 |
$height = 2 * $font_size; |
188 | 198 |
|
189 | 199 |
return array($width, $height); |
190 | 200 |
} |
191 | 201 |
|
192 |
|
|
193 | 202 |
/** |
194 | 203 |
* Implements hook_captcha(). |
195 | 204 |
*/ |
196 |
function image_captcha_captcha($op, $captcha_type='', $captcha_sid=NULL) {
|
|
205 |
function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
|
|
197 | 206 |
switch ($op) { |
198 | 207 |
case 'list': |
199 | 208 |
// Only offer the image CAPTCHA if it is possible to generate an image on this setup. |
... | ... | |
214 | 223 |
if (variable_get('maintenance_mode', 0) && $user->uid == 0) { |
215 | 224 |
return captcha_captcha('generate', 'Math'); |
216 | 225 |
} |
217 |
// generate a CAPTCHA code
|
|
226 |
// Generate a CAPTCHA code.
|
|
218 | 227 |
$allowed_chars = _image_captcha_utf8_split(variable_get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS)); |
219 |
$code_length = (int)variable_get('image_captcha_code_length', 5); |
|
228 |
$code_length = (int) variable_get('image_captcha_code_length', 5);
|
|
220 | 229 |
$code = ''; |
221 | 230 |
for ($i = 0; $i < $code_length; $i++) { |
222 | 231 |
$code .= $allowed_chars[array_rand($allowed_chars)]; |
223 | 232 |
} |
224 | 233 |
|
225 |
// build the result to return
|
|
234 |
// Build the result to return.
|
|
226 | 235 |
$result = array(); |
227 | 236 |
|
228 | 237 |
$result['solution'] = $code; |
... | ... | |
241 | 250 |
$result['form']['captcha_image'] = array( |
242 | 251 |
'#type' => 'markup', |
243 | 252 |
'#markup' => '<img src="' . $img_src |
244 |
. '" width="'. $width . '" height="' . $height
|
|
245 |
. '" alt="' . t('Image CAPTCHA') . '" title="' . t('Image CAPTCHA') . '" />',
|
|
253 |
. '" width="' . $width . '" height="' . $height
|
|
254 |
. '" alt="' . t('Image CAPTCHA') . '" title="' . t('Image CAPTCHA') . '" />', |
|
246 | 255 |
'#weight' => -2, |
247 | 256 |
); |
248 | 257 |
$result['form']['captcha_response'] = array( |
... | ... | |
259 | 268 |
case CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE: |
260 | 269 |
$result['captcha_validate'] = 'captcha_validate_ignore_spaces'; |
261 | 270 |
break; |
271 |
|
|
262 | 272 |
case CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE: |
263 | 273 |
$result['captcha_validate'] = 'captcha_validate_case_insensitive_ignore_spaces'; |
264 | 274 |
break; |
Formats disponibles : Unified diff
Weekly update of contrib modules