really-simple-captcha.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /**
  3. ** A base module for [captchac] and [captchar]
  4. **/
  5. /* form_tag handler */
  6. add_action( 'wpcf7_init', 'wpcf7_add_form_tag_captcha', 10, 0 );
  7. function wpcf7_add_form_tag_captcha() {
  8. // CAPTCHA-Challenge (image)
  9. wpcf7_add_form_tag( 'captchac',
  10. 'wpcf7_captchac_form_tag_handler',
  11. array(
  12. 'name-attr' => true,
  13. 'zero-controls-container' => true,
  14. 'not-for-mail' => true,
  15. )
  16. );
  17. // CAPTCHA-Response (input)
  18. wpcf7_add_form_tag( 'captchar',
  19. 'wpcf7_captchar_form_tag_handler',
  20. array(
  21. 'name-attr' => true,
  22. 'do-not-store' => true,
  23. 'not-for-mail' => true,
  24. )
  25. );
  26. }
  27. function wpcf7_captchac_form_tag_handler( $tag ) {
  28. if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
  29. $error = sprintf(
  30. /* translators: %s: link labeled 'Really Simple CAPTCHA' */
  31. esc_html( __( "To use CAPTCHA, you need %s plugin installed.", 'contact-form-7' ) ),
  32. wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' )
  33. );
  34. return sprintf( '<em>%s</em>', $error );
  35. }
  36. if ( empty( $tag->name ) ) {
  37. return '';
  38. }
  39. $class = wpcf7_form_controls_class( $tag->type );
  40. $class .= ' wpcf7-captcha-' . $tag->name;
  41. $atts = array();
  42. $atts['class'] = $tag->get_class_option( $class );
  43. $atts['id'] = $tag->get_id_option();
  44. $op = array( // Default
  45. 'img_size' => array( 72, 24 ),
  46. 'base' => array( 6, 18 ),
  47. 'font_size' => 14,
  48. 'font_char_width' => 15,
  49. );
  50. $op = array_merge( $op, wpcf7_captchac_options( $tag->options ) );
  51. if ( ! $filename = wpcf7_generate_captcha( $op ) ) {
  52. return '';
  53. }
  54. if ( ! empty( $op['img_size'] ) ) {
  55. if ( isset( $op['img_size'][0] ) ) {
  56. $atts['width'] = $op['img_size'][0];
  57. }
  58. if ( isset( $op['img_size'][1] ) ) {
  59. $atts['height'] = $op['img_size'][1];
  60. }
  61. }
  62. $atts['alt'] = 'captcha';
  63. $atts['src'] = wpcf7_captcha_url( $filename );
  64. $atts = wpcf7_format_atts( $atts );
  65. $prefix = substr( $filename, 0, strrpos( $filename, '.' ) );
  66. $html = sprintf(
  67. '<input type="hidden" name="_wpcf7_captcha_challenge_%1$s" value="%2$s" /><img %3$s />',
  68. $tag->name, esc_attr( $prefix ), $atts );
  69. return $html;
  70. }
  71. function wpcf7_captchar_form_tag_handler( $tag ) {
  72. if ( empty( $tag->name ) ) {
  73. return '';
  74. }
  75. $validation_error = wpcf7_get_validation_error( $tag->name );
  76. $class = wpcf7_form_controls_class( $tag->type );
  77. if ( $validation_error ) {
  78. $class .= ' wpcf7-not-valid';
  79. }
  80. $atts = array();
  81. $atts['size'] = $tag->get_size_option( '40' );
  82. $atts['maxlength'] = $tag->get_maxlength_option();
  83. $atts['minlength'] = $tag->get_minlength_option();
  84. if ( $atts['maxlength'] and $atts['minlength']
  85. and $atts['maxlength'] < $atts['minlength'] ) {
  86. unset( $atts['maxlength'], $atts['minlength'] );
  87. }
  88. $atts['class'] = $tag->get_class_option( $class );
  89. $atts['id'] = $tag->get_id_option();
  90. $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
  91. $atts['autocomplete'] = 'off';
  92. $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
  93. $value = (string) reset( $tag->values );
  94. if ( wpcf7_is_posted() ) {
  95. $value = '';
  96. }
  97. if ( $tag->has_option( 'placeholder' )
  98. or $tag->has_option( 'watermark' ) ) {
  99. $atts['placeholder'] = $value;
  100. $value = '';
  101. }
  102. $atts['value'] = $value;
  103. $atts['type'] = 'text';
  104. $atts['name'] = $tag->name;
  105. $atts = wpcf7_format_atts( $atts );
  106. $html = sprintf(
  107. '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
  108. sanitize_html_class( $tag->name ), $atts, $validation_error );
  109. return $html;
  110. }
  111. /* Validation filter */
  112. add_filter( 'wpcf7_validate_captchar',
  113. 'wpcf7_captcha_validation_filter', 10, 2 );
  114. function wpcf7_captcha_validation_filter( $result, $tag ) {
  115. $type = $tag->type;
  116. $name = $tag->name;
  117. $captchac = '_wpcf7_captcha_challenge_' . $name;
  118. $prefix = isset( $_POST[$captchac] ) ? (string) $_POST[$captchac] : '';
  119. $response = isset( $_POST[$name] ) ? (string) $_POST[$name] : '';
  120. $response = wpcf7_canonicalize( $response );
  121. if ( 0 == strlen( $prefix )
  122. or ! wpcf7_check_captcha( $prefix, $response ) ) {
  123. $result->invalidate( $tag, wpcf7_get_message( 'captcha_not_match' ) );
  124. }
  125. if ( 0 != strlen( $prefix ) ) {
  126. wpcf7_remove_captcha( $prefix );
  127. }
  128. return $result;
  129. }
  130. /* Ajax echo filter */
  131. add_filter( 'wpcf7_ajax_onload', 'wpcf7_captcha_ajax_refill', 10, 1 );
  132. add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_captcha_ajax_refill', 10, 1 );
  133. function wpcf7_captcha_ajax_refill( $items ) {
  134. if ( ! is_array( $items ) ) {
  135. return $items;
  136. }
  137. $tags = wpcf7_scan_form_tags( array( 'type' => 'captchac' ) );
  138. if ( empty( $tags ) ) {
  139. return $items;
  140. }
  141. $refill = array();
  142. foreach ( $tags as $tag ) {
  143. $name = $tag->name;
  144. $options = $tag->options;
  145. if ( empty( $name ) ) {
  146. continue;
  147. }
  148. $op = wpcf7_captchac_options( $options );
  149. if ( $filename = wpcf7_generate_captcha( $op ) ) {
  150. $captcha_url = wpcf7_captcha_url( $filename );
  151. $refill[$name] = $captcha_url;
  152. }
  153. }
  154. if ( ! empty( $refill ) ) {
  155. $items['captcha'] = $refill;
  156. }
  157. return $items;
  158. }
  159. /* Messages */
  160. add_filter( 'wpcf7_messages', 'wpcf7_captcha_messages', 10, 1 );
  161. function wpcf7_captcha_messages( $messages ) {
  162. $messages = array_merge( $messages, array(
  163. 'captcha_not_match' => array(
  164. 'description' =>
  165. __( "The code that sender entered does not match the CAPTCHA", 'contact-form-7' ),
  166. 'default' =>
  167. __( 'Your entered code is incorrect.', 'contact-form-7' ),
  168. ),
  169. ) );
  170. return $messages;
  171. }
  172. /* Tag generator */
  173. add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_captcha', 46, 0 );
  174. function wpcf7_add_tag_generator_captcha() {
  175. if ( ! wpcf7_use_really_simple_captcha() ) {
  176. return;
  177. }
  178. $tag_generator = WPCF7_TagGenerator::get_instance();
  179. $tag_generator->add( 'captcha',
  180. __( 'CAPTCHA (Really Simple CAPTCHA)', 'contact-form-7' ),
  181. 'wpcf7_tag_generator_captcha' );
  182. }
  183. function wpcf7_tag_generator_captcha( $contact_form, $args = '' ) {
  184. $args = wp_parse_args( $args, array() );
  185. if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
  186. ?>
  187. <div class="control-box">
  188. <fieldset>
  189. <legend><?php
  190. echo sprintf(
  191. /* translators: %s: link labeled 'Really Simple CAPTCHA' */
  192. esc_html( __( "To use CAPTCHA, you first need to install and activate %s plugin.", 'contact-form-7' ) ),
  193. wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' )
  194. );
  195. ?></legend>
  196. </fieldset>
  197. </div>
  198. <?php
  199. return;
  200. }
  201. $description = __( "Generate form-tags for a CAPTCHA image and corresponding response input field. For more details, see %s.", 'contact-form-7' );
  202. $desc_link = wpcf7_link( __( 'https://contactform7.com/captcha/', 'contact-form-7' ), __( 'CAPTCHA', 'contact-form-7' ) );
  203. ?>
  204. <div class="control-box">
  205. <fieldset>
  206. <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
  207. <table class="form-table">
  208. <tbody>
  209. <tr>
  210. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
  211. <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
  212. </tr>
  213. </tbody>
  214. </table>
  215. <table class="form-table scope captchac">
  216. <caption><?php echo esc_html( __( "Image settings", 'contact-form-7' ) ); ?></caption>
  217. <tbody>
  218. <tr>
  219. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  220. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>" /></td>
  221. </tr>
  222. <tr>
  223. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  224. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>" /></td>
  225. </tr>
  226. </tbody>
  227. </table>
  228. <table class="form-table scope captchar">
  229. <caption><?php echo esc_html( __( "Input field settings", 'contact-form-7' ) ); ?></caption>
  230. <tbody>
  231. <tr>
  232. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
  233. <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>" /></td>
  234. </tr>
  235. <tr>
  236. <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
  237. <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>" /></td>
  238. </tr>
  239. </tbody>
  240. </table>
  241. </fieldset>
  242. </div>
  243. <div class="insert-box">
  244. <input type="text" name="captcha" class="tag code" readonly="readonly" onfocus="this.select()" />
  245. <div class="submitbox">
  246. <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
  247. </div>
  248. </div>
  249. <?php
  250. }
  251. /* Warning message */
  252. add_action( 'wpcf7_admin_warnings',
  253. 'wpcf7_captcha_display_warning_message', 10, 3 );
  254. function wpcf7_captcha_display_warning_message( $page, $action, $object ) {
  255. if ( $object instanceof WPCF7_ContactForm ) {
  256. $contact_form = $object;
  257. } else {
  258. return;
  259. }
  260. $has_tags = (bool) $contact_form->scan_form_tags(
  261. array( 'type' => array( 'captchac' ) ) );
  262. if ( ! $has_tags ) {
  263. return;
  264. }
  265. if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
  266. return;
  267. }
  268. $uploads_dir = wpcf7_captcha_tmp_dir();
  269. wpcf7_init_captcha();
  270. if ( ! is_dir( $uploads_dir )
  271. or ! wp_is_writable( $uploads_dir ) ) {
  272. $message = sprintf( __( 'This contact form contains CAPTCHA fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.', 'contact-form-7' ), $uploads_dir );
  273. echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>';
  274. }
  275. if ( ! function_exists( 'imagecreatetruecolor' )
  276. or ! function_exists( 'imagettftext' ) ) {
  277. $message = __( "This contact form contains CAPTCHA fields, but the necessary libraries (GD and FreeType) are not available on your server.", 'contact-form-7' );
  278. echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>';
  279. }
  280. }
  281. /* CAPTCHA functions */
  282. function wpcf7_init_captcha() {
  283. static $captcha = null;
  284. if ( $captcha ) {
  285. return $captcha;
  286. }
  287. if ( class_exists( 'ReallySimpleCaptcha' ) ) {
  288. $captcha = new ReallySimpleCaptcha();
  289. } else {
  290. return false;
  291. }
  292. $dir = trailingslashit( wpcf7_captcha_tmp_dir() );
  293. $captcha->tmp_dir = $dir;
  294. if ( is_callable( array( $captcha, 'make_tmp_dir' ) ) ) {
  295. $result = $captcha->make_tmp_dir();
  296. if ( ! $result ) {
  297. return false;
  298. }
  299. return $captcha;
  300. }
  301. if ( wp_mkdir_p( $dir ) ) {
  302. $htaccess_file = path_join( $dir, '.htaccess' );
  303. if ( file_exists( $htaccess_file ) ) {
  304. return $captcha;
  305. }
  306. if ( $handle = fopen( $htaccess_file, 'w' ) ) {
  307. fwrite( $handle, 'Order deny,allow' . "\n" );
  308. fwrite( $handle, 'Deny from all' . "\n" );
  309. fwrite( $handle, '<Files ~ "^[0-9A-Za-z]+\\.(jpeg|gif|png)$">' . "\n" );
  310. fwrite( $handle, ' Allow from all' . "\n" );
  311. fwrite( $handle, '</Files>' . "\n" );
  312. fclose( $handle );
  313. }
  314. } else {
  315. return false;
  316. }
  317. return $captcha;
  318. }
  319. function wpcf7_captcha_tmp_dir() {
  320. if ( defined( 'WPCF7_CAPTCHA_TMP_DIR' ) ) {
  321. return WPCF7_CAPTCHA_TMP_DIR;
  322. } else {
  323. return path_join( wpcf7_upload_dir( 'dir' ), 'wpcf7_captcha' );
  324. }
  325. }
  326. function wpcf7_captcha_tmp_url() {
  327. if ( defined( 'WPCF7_CAPTCHA_TMP_URL' ) ) {
  328. return WPCF7_CAPTCHA_TMP_URL;
  329. } else {
  330. return path_join( wpcf7_upload_dir( 'url' ), 'wpcf7_captcha' );
  331. }
  332. }
  333. function wpcf7_captcha_url( $filename ) {
  334. $url = path_join( wpcf7_captcha_tmp_url(), $filename );
  335. if ( is_ssl()
  336. and 'http:' == substr( $url, 0, 5 ) ) {
  337. $url = 'https:' . substr( $url, 5 );
  338. }
  339. return apply_filters( 'wpcf7_captcha_url', esc_url_raw( $url ) );
  340. }
  341. function wpcf7_generate_captcha( $options = null ) {
  342. if ( ! $captcha = wpcf7_init_captcha() ) {
  343. return false;
  344. }
  345. if ( ! is_dir( $captcha->tmp_dir )
  346. or ! wp_is_writable( $captcha->tmp_dir ) ) {
  347. return false;
  348. }
  349. $img_type = imagetypes();
  350. if ( $img_type & IMG_PNG ) {
  351. $captcha->img_type = 'png';
  352. } elseif ( $img_type & IMG_GIF ) {
  353. $captcha->img_type = 'gif';
  354. } elseif ( $img_type & IMG_JPG ) {
  355. $captcha->img_type = 'jpeg';
  356. } else {
  357. return false;
  358. }
  359. if ( is_array( $options ) ) {
  360. if ( isset( $options['img_size'] ) ) {
  361. $captcha->img_size = $options['img_size'];
  362. }
  363. if ( isset( $options['base'] ) ) {
  364. $captcha->base = $options['base'];
  365. }
  366. if ( isset( $options['font_size'] ) ) {
  367. $captcha->font_size = $options['font_size'];
  368. }
  369. if ( isset( $options['font_char_width'] ) ) {
  370. $captcha->font_char_width = $options['font_char_width'];
  371. }
  372. if ( isset( $options['fg'] ) ) {
  373. $captcha->fg = $options['fg'];
  374. }
  375. if ( isset( $options['bg'] ) ) {
  376. $captcha->bg = $options['bg'];
  377. }
  378. }
  379. $prefix = wp_rand();
  380. $captcha_word = $captcha->generate_random_word();
  381. return $captcha->generate_image( $prefix, $captcha_word );
  382. }
  383. function wpcf7_check_captcha( $prefix, $response ) {
  384. if ( ! $captcha = wpcf7_init_captcha() ) {
  385. return false;
  386. }
  387. return $captcha->check( $prefix, $response );
  388. }
  389. function wpcf7_remove_captcha( $prefix ) {
  390. if ( ! $captcha = wpcf7_init_captcha() ) {
  391. return false;
  392. }
  393. // Contact Form 7 generates $prefix with wp_rand()
  394. if ( preg_match( '/[^0-9]/', $prefix ) ) {
  395. return false;
  396. }
  397. $captcha->remove( $prefix );
  398. }
  399. add_action( 'template_redirect', 'wpcf7_cleanup_captcha_files', 20, 0 );
  400. function wpcf7_cleanup_captcha_files() {
  401. if ( ! $captcha = wpcf7_init_captcha() ) {
  402. return false;
  403. }
  404. if ( is_callable( array( $captcha, 'cleanup' ) ) ) {
  405. return $captcha->cleanup();
  406. }
  407. $dir = trailingslashit( wpcf7_captcha_tmp_dir() );
  408. if ( ! is_dir( $dir )
  409. or ! is_readable( $dir )
  410. or ! wp_is_writable( $dir ) ) {
  411. return false;
  412. }
  413. if ( $handle = opendir( $dir ) ) {
  414. while ( false !== ( $file = readdir( $handle ) ) ) {
  415. if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $file ) ) {
  416. continue;
  417. }
  418. $stat = stat( path_join( $dir, $file ) );
  419. if ( $stat['mtime'] + 3600 < time() ) { // 3600 secs == 1 hour
  420. @unlink( path_join( $dir, $file ) );
  421. }
  422. }
  423. closedir( $handle );
  424. }
  425. }
  426. function wpcf7_captchac_options( $options ) {
  427. if ( ! is_array( $options ) ) {
  428. return array();
  429. }
  430. $op = array();
  431. $image_size_array = preg_grep( '%^size:[smlSML]$%', $options );
  432. if ( $image_size = array_shift( $image_size_array ) ) {
  433. preg_match( '%^size:([smlSML])$%', $image_size, $is_matches );
  434. switch ( strtolower( $is_matches[1] ) ) {
  435. case 's':
  436. $op['img_size'] = array( 60, 20 );
  437. $op['base'] = array( 6, 15 );
  438. $op['font_size'] = 11;
  439. $op['font_char_width'] = 13;
  440. break;
  441. case 'l':
  442. $op['img_size'] = array( 84, 28 );
  443. $op['base'] = array( 6, 20 );
  444. $op['font_size'] = 17;
  445. $op['font_char_width'] = 19;
  446. break;
  447. case 'm':
  448. default:
  449. $op['img_size'] = array( 72, 24 );
  450. $op['base'] = array( 6, 18 );
  451. $op['font_size'] = 14;
  452. $op['font_char_width'] = 15;
  453. }
  454. }
  455. $fg_color_array = preg_grep(
  456. '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
  457. if ( $fg_color = array_shift( $fg_color_array ) ) {
  458. preg_match( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%',
  459. $fg_color, $fc_matches );
  460. if ( 3 == strlen( $fc_matches[1] ) ) {
  461. $r = substr( $fc_matches[1], 0, 1 );
  462. $g = substr( $fc_matches[1], 1, 1 );
  463. $b = substr( $fc_matches[1], 2, 1 );
  464. $op['fg'] = array(
  465. hexdec( $r . $r ),
  466. hexdec( $g . $g ),
  467. hexdec( $b . $b ),
  468. );
  469. } elseif ( 6 == strlen( $fc_matches[1] ) ) {
  470. $r = substr( $fc_matches[1], 0, 2 );
  471. $g = substr( $fc_matches[1], 2, 2 );
  472. $b = substr( $fc_matches[1], 4, 2 );
  473. $op['fg'] = array(
  474. hexdec( $r ),
  475. hexdec( $g ),
  476. hexdec( $b ),
  477. );
  478. }
  479. }
  480. $bg_color_array = preg_grep(
  481. '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
  482. if ( $bg_color = array_shift( $bg_color_array ) ) {
  483. preg_match( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%',
  484. $bg_color, $bc_matches );
  485. if ( 3 == strlen( $bc_matches[1] ) ) {
  486. $r = substr( $bc_matches[1], 0, 1 );
  487. $g = substr( $bc_matches[1], 1, 1 );
  488. $b = substr( $bc_matches[1], 2, 1 );
  489. $op['bg'] = array(
  490. hexdec( $r . $r ),
  491. hexdec( $g . $g ),
  492. hexdec( $b . $b ),
  493. );
  494. } elseif ( 6 == strlen( $bc_matches[1] ) ) {
  495. $r = substr( $bc_matches[1], 0, 2 );
  496. $g = substr( $bc_matches[1], 2, 2 );
  497. $b = substr( $bc_matches[1], 4, 2 );
  498. $op['bg'] = array(
  499. hexdec( $r ),
  500. hexdec( $g ),
  501. hexdec( $b ),
  502. );
  503. }
  504. }
  505. return $op;
  506. }