Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: class Quform_Element_Captcha extends Quform_Element_Field
  7: {
  8:     /**
  9:      * @var Quform_Session
 10:      */
 11:     protected $session;
 12: 
 13:     /**
 14:      * @param  int             $id
 15:      * @param  Quform_Form     $form
 16:      * @param  Quform_Session  $session
 17:      */
 18:     public function __construct($id, Quform_Form $form, Quform_Session $session)
 19:     {
 20:         parent::__construct($id, $form);
 21: 
 22:         $this->session = $session;
 23:     }
 24: 
 25:     /**
 26:      * Get the HTML attributes for the image tag
 27:      *
 28:      * @return array
 29:      */
 30:     protected function getImageAttributes()
 31:     {
 32:         $attributes = array(
 33:             'class' => 'quform-captcha-image-img',
 34:             'src' => $this->generateImageData(),
 35:             'alt' => __('CAPTCHA image', 'quform'),
 36:             'data-element-id' => $this->getId()
 37:         );
 38: 
 39:         $imageWidth = $this->config('captchaWidth');
 40:         $imageHeight = $this->config('captchaHeight');
 41: 
 42:         if ( ! is_numeric($imageWidth)) {
 43:             $imageWidth = self::getDefaultConfig('captchaWidth');
 44:         }
 45: 
 46:         if ( ! is_numeric($imageHeight)) {
 47:             $imageHeight = self::getDefaultConfig('captchaHeight');
 48:         }
 49: 
 50:         $attributes['width'] = $imageWidth;
 51:         $attributes['height'] = $imageHeight;
 52: 
 53:         $attributes = apply_filters('quform_captcha_image_attributes', $attributes, $this, $this->form);
 54:         $attributes = apply_filters('quform_captcha_image_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form);
 55: 
 56:         return $attributes;
 57:     }
 58: 
 59:     /**
 60:      * Get the HTML for the captcha image
 61:      *
 62:      * @return string
 63:      */
 64:     protected function getImageHtml()
 65:     {
 66:         $output = '<div class="quform-captcha quform-cf">';
 67:         $output .= sprintf('<div class="quform-captcha-image quform-captcha-image-%s">', $this->getIdentifier());
 68:         $output .= Quform::getHtmlTag('img', $this->getImageAttributes());
 69:         $output .= '</div></div>';
 70: 
 71:         return $output;
 72:     }
 73: 
 74:     /**
 75:      * Get the HTML attributes for the field
 76:      *
 77:      * @param  array  $context
 78:      * @return array
 79:      */
 80:     protected function getFieldAttributes(array $context = array())
 81:     {
 82:         $attributes = array(
 83:             'type' => 'text',
 84:             'id' => $this->getUniqueId(),
 85:             'name' => $this->getFullyQualifiedName(),
 86:             'class' => Quform::sanitizeClass($this->getFieldClasses($context))
 87:         );
 88: 
 89:         if ( ! $this->isEmpty()) {
 90:             $attributes['value'] = $this->getValue();
 91:         }
 92: 
 93:         $placeholder = $this->form->replaceVariablesPreProcess($this->config('placeholder'));
 94:         if (Quform::isNonEmptyString($placeholder)) {
 95:             $attributes['placeholder'] = $placeholder;
 96:         }
 97:         $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
 98:         $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
 99: 
100:         return $attributes;
101:     }
102: 
103:     /**
104:      * Get the classes for the field
105:      *
106:      * @param   array  $context
107:      * @return  array
108:      */
109:     protected function getFieldClasses(array $context = array())
110:     {
111:         $classes = array(
112:             'quform-field',
113:             'quform-field-captcha',
114:             sprintf('quform-field-%s', $this->getIdentifier())
115:         );
116: 
117:         if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($this->config('tooltip')) && Quform::get($context, 'tooltipType') == 'field') {
118:             $classes[] = sprintf('quform-tooltip-%s', Quform::get($context, 'tooltipEvent'));
119:         }
120: 
121:         if (Quform::isNonEmptyString($this->config('customClass'))) {
122:             $classes[] = $this->config('customClass');
123:         }
124: 
125:         $classes = apply_filters('quform_field_classes', $classes, $this, $this->form, $context);
126:         $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $this, $this->form, $context);
127: 
128:         return $classes;
129:     }
130: 
131:     /**
132:      * Get the HTML for the field
133:      *
134:      * @param   array   $context
135:      * @return  string
136:      */
137:     protected function getFieldHtml(array $context = array())
138:     {
139:         return Quform::getHtmlTag('input', $this->getFieldAttributes($context));
140:     }
141: 
142:     /**
143:      * Get the HTML for the element input wrapper
144:      *
145:      * @param   array   $context
146:      * @return  string
147:      */
148:     protected function getInputHtml(array $context = array())
149:     {
150:         $output = sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getInputClasses($context))));
151:         $output .= $this->getFieldHtml($context);
152:         $output .= $this->getFieldIconsHtml();
153: 
154:         if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($this->config('tooltip')) && Quform::get($context, 'tooltipType') == 'field') {
155:             $output .= sprintf('<div class="quform-tooltip-content">%s</div>', $this->config('tooltip'));
156:         }
157: 
158:         $output .= '</div>';
159:         $output .= $this->getImageHtml();
160: 
161:         return $output;
162:     }
163: 
164:     /**
165:      * Generates a string of characters of the given length
166:      *
167:      * @param   int     $length
168:      * @return  string
169:      */
170:     protected function generateCode($length)
171:     {
172:         // The character pool, similar looking characters removed
173:         $characters = '23456789bcdfghjkmnpqrstvwxyz';
174:         $code = '';
175: 
176:         for ($i = 0; $i < $length; $i++) {
177:             $code .= substr($characters, mt_rand(0, strlen($characters) - 1), 1);
178:         }
179: 
180:         return $code;
181:     }
182: 
183:     /**
184:      * Get the complete base64 encoded image data for use in an &lt;img&gt; tag and set generated code in session
185:      *
186:      * @return string
187:      */
188:     public function generateImageData()
189:     {
190:         if ($this->supportsDynamicImageGeneration()) {
191:             $code = $this->generateCode((int) $this->config('captchaLength'));
192:             $data = $this->generateDynamicImage($code);
193:         } else {
194:             $code = 'catch';
195:             $data = $this->getStaticImageData();
196:         }
197: 
198:         $this->session->set($this->form->getSessionKey() . '.captcha.' . $this->getName(), $code);
199: 
200:         return 'data:image/png;base64,' . $data;
201:     }
202: 
203:     /**
204:      * Get the path to the given font
205:      *
206:      * Copies the font file to a temporary folder it is a Windows server
207:      *
208:      * @param   string  $font  The filename of the font
209:      * @return  string         The full path to the font
210:      */
211:     protected function getFontPath($font)
212:     {
213:         $originalFontPath = QUFORM_LIBRARY_PATH . '/fonts/' . $font;
214:         $path = $originalFontPath;
215: 
216:         if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
217:             // Copy the fonts to the temp dir or they become locked on Windows servers when used and prevent plugin upgrades
218:             $tmpDir = Quform::getTempDir('quform/fonts');
219:             $cachedFontPath = $tmpDir . '/' . $font;
220: 
221:             if (file_exists($cachedFontPath)) {
222:                 $path = $cachedFontPath;
223:             } else {
224:                 if ( ! is_dir($tmpDir)) {
225:                     wp_mkdir_p($tmpDir);
226:                 }
227: 
228:                 if (wp_is_writable($tmpDir)) {
229:                     copy($originalFontPath, $cachedFontPath);
230:                     $path = $cachedFontPath;
231:                 }
232: 
233:                 if ( ! file_exists($cachedFontPath)) {
234:                     $path = $originalFontPath;
235:                 }
236:             }
237:         }
238: 
239:         return $path;
240:     }
241: 
242:     /**
243:      * @return bool
244:      */
245:     protected function supportsDynamicImageGeneration()
246:     {
247:         return function_exists('imagecreate') &&
248:                function_exists('imagecolorallocate') &&
249:                function_exists('imagettftext') &&
250:                function_exists('imagepng') &&
251:                function_exists('imagedestroy');
252: 
253:     }
254: 
255:     /**
256:      * Generate the captcha image from the given code
257:      *
258:      * @param   string  $code  The code to generate
259:      * @return  string
260:      */
261:     protected function generateDynamicImage($code)
262:     {
263:         $yOffsetAdjustMin = 5;
264:         $yOffsetAdjustMax = 10;
265:         $width = (int) $this->config('captchaWidth');
266:         $height = (int) $this->config('captchaHeight');
267:         $minFontSize = (int) $this->config('captchaMinFontSize');
268:         $maxFontSize = (int) $this->config('captchaMaxFontSize');
269:         $bgAlpha = (int) ((1 - $this->config('captchaBgColorRgba.a', 1)) * 127);
270:         $textAlpha = (int) ((1 - $this->config('captchaTextColorRgba.a', 1)) * 127);
271: 
272:         if ($this->config('captchaRetina')) {
273:             $width = $width * 2;
274:             $height = $height * 2;
275:             $minFontSize = $minFontSize * 2;
276:             $maxFontSize = $maxFontSize * 2;
277:             $yOffsetAdjustMin = $yOffsetAdjustMin * 2;
278:             $yOffsetAdjustMax = $yOffsetAdjustMax * 2;
279:         }
280: 
281:         $image = imagecreate($width, $height);
282: 
283:         imagecolorallocatealpha(
284:             $image,
285:             $this->config('captchaBgColorRgba.r'),
286:             $this->config('captchaBgColorRgba.g'),
287:             $this->config('captchaBgColorRgba.b'),
288:             $bgAlpha
289:         );
290: 
291:         $textColor = imagecolorallocatealpha(
292:             $image,
293:             $this->config('captchaTextColorRgba.r'),
294:             $this->config('captchaTextColorRgba.g'),
295:             $this->config('captchaTextColorRgba.b'),
296:             $textAlpha
297:         );
298: 
299:         for($i = 0; $i < $this->config('captchaLength'); $i++) {
300:             $counter = mt_rand(0, 1);
301: 
302:             if ($counter == 0) {
303:                 $angle = mt_rand($this->config('captchaMinAngle'), $this->config('captchaMaxAngle'));
304:             }
305: 
306:             if ($counter == 1) {
307:                 $angle = mt_rand(360 - $this->config('captchaMaxAngle'), 360 - $this->config('captchaMinAngle'));
308:             }
309: 
310:             imagettftext(
311:                 $image,
312:                 mt_rand($minFontSize, $maxFontSize),
313:                 $angle,
314:                 (($i + 1) * $maxFontSize) - ($maxFontSize / 2),
315:                 mt_rand($maxFontSize + $yOffsetAdjustMin, $maxFontSize + $yOffsetAdjustMax),
316:                 $textColor,
317:                 $this->getFontPath($this->config('captchaFont')),
318:                 substr($code, $i, 1)
319:             );
320:         }
321: 
322:         ob_start();
323:         imagepng($image);
324:         $data = base64_encode(ob_get_clean());
325:         imagedestroy($image);
326: 
327:         return $data;
328:     }
329: 
330:     /**
331:      * Base 64 encoded captcha image when dynamic image generation is not available
332:      *
333:      * Displays the string 'catch'
334:      *
335:      * @return  string            The image data as base 64
336:      */
337:     protected function getStaticImageData()
338:     {
339:         // TODO these are the same, make seperate retina/non-retina versions
340:         if ($this->config('captchaRetina')) {
341:             return 'iVBORw0KGgoAAAANSUhEUgAAAJsAAAAoBAMAAAAbEZVkAAAAG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OiXFkhPpdrJxMmRJaDGacxH2Cafzwb124NPFqzarxw85eG+YBJoLNeZwSDi64kIGkIKyErchsMnGHeRdr2N0CTcHhecSI31WBXCM3UsBGlsC7Zk2YMpAp40Yg6scY02J9waRudOlj8L/+69aF/VSqlSpE9oD+HU3KouM3WcAAAAASUVORK5CYII=';
344:         }
345:     }
346: 
347:     /**
348:      * Render the CSS for this element
349:      *
350:      * @param   array   $context
351:      * @return  string
352:      */
353:     protected function renderCss(array $context = array())
354:     {
355:         $css = parent::renderCss($context);
356: 
357:         if ($context['fieldWidth'] == 'custom' && Quform::isNonEmptyString($context['fieldWidthCustom'])) {
358:             $css .= sprintf('.quform-input-captcha.quform-input-%s { width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
359:             $css .= sprintf('.quform-inner-%s > .quform-error > .quform-error-inner { float: left; min-width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
360:         }
361: 
362:         return $css;
363:     }
364: 
365:     /**
366:      * Get the default element configuration
367:      *
368:      * @param   string|null  $key  Get the config by key, if omitted the full config is returned
369:      * @return  array
370:      */
371:     public static function getDefaultConfig($key = null)
372:     {
373:         $config = apply_filters('quform_default_config_captcha', array(
374:             // Basic
375:             'label' => __('Please type the characters', 'quform'),
376:             'description' => __('This helps us prevent spam, thank you.', 'quform'),
377:             'descriptionAbove' => '',
378: 
379:             // Styles
380:             'labelIcon' => '',
381:             'fieldIconLeft' => '',
382:             'fieldIconRight' => '',
383:             'fieldSize' => 'inherit',
384:             'fieldWidth' => 'inherit',
385:             'fieldWidthCustom' => '',
386:             'captchaLength' => '5',
387:             'captchaWidth' => '115',
388:             'captchaHeight' => '40',
389:             'captchaBgColor' => '#FFFFFF',
390:             'captchaBgColorRgba' => array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 1),
391:             'captchaTextColor' => '#222222',
392:             'captchaTextColorRgba' => array('r' => 34, 'g' => 34, 'b' => 34, 'a' => 1),
393:             'captchaFont' => 'Typist.ttf',
394:             'captchaMinFontSize' => '12',
395:             'captchaMaxFontSize' => '19',
396:             'captchaMinAngle' => '0',
397:             'captchaMaxAngle' => '20',
398:             'captchaRetina' => true,
399:             'customClass' => '',
400:             'customElementClass' => '',
401:             'styles' => array(),
402: 
403:             // Labels
404:             'placeholder' => '',
405:             'subLabel' => '',
406:             'subLabelAbove' => '',
407:             'tooltip' => '',
408:             'tooltipType' => 'inherit',
409:             'tooltipEvent' => 'inherit',
410:             'labelPosition' => 'inherit',
411:             'labelWidth' => '',
412: 
413:             // Logic
414:             'logicEnabled' => false,
415:             'logicAction' => true,
416:             'logicMatch' => 'all',
417:             'logicRules' => array(),
418: 
419:             // Advanced
420:             'visibility' => '',
421: 
422:             // Translations
423:             'messageRequired' => '',
424:             'messageCaptchaNotMatch' => ''
425:         ));
426: 
427:         $config['type'] = 'captcha';
428: 
429:         if (Quform::isNonEmptyString($key)) {
430:             return Quform::get($config, $key);
431:         }
432: 
433:         return $config;
434:     }
435: }
436: 
API documentation generated by ApiGen