PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/opentickets-community-edition/libs/phpqrcode/index.php

https://gitlab.com/leobelizquierdo/cabotsubmitter-wordpress
PHP | 149 lines | 97 code | 25 blank | 27 comment | 39 complexity | aac9f8a76eea52855651201a67153c2c MD5 | raw file
  1. <?php
  2. $debug = false;
  3. if ( $debug === true ) {
  4. ini_set( 'display_erorrs', 1 );
  5. ini_set( 'html_errors', 1 );
  6. error_reporting( E_ALL );
  7. } else {
  8. ini_set( 'display_erorrs', 0 );
  9. ini_set( 'html_errors', 0 );
  10. error_reporting( 0 );
  11. }
  12. /* old abuse protection
  13. $ref = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER']) : '';
  14. $host = strtolower(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ( isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '' ));
  15. $same_server = isset($_SERVER['SERVER_ADDR'], $_SERVER['REMOTE_ADDR']) && $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR'];
  16. if (!$same_server && !(isset($ref['host']) && $host == strtolower($ref['host']))) die();
  17. */
  18. if (!isset($_GET['d']) || empty($_GET['d'])) die();
  19. $d = strrev( @base64_decode( str_replace( array( ' ', '-', '_', '~' ), array( '+', '+', '=', '/' ), $_GET['d'] ) ) );
  20. if (empty($d)) die();
  21. // abuse protection
  22. // check that needed values are presend
  23. $d = @json_decode( $d, true );
  24. if ( !is_array( $d ) ) die( $debug ? '<!-- not array -->' : '' );
  25. $sig = isset( $d['sig'], $d['p'], $d['d'] ) ? $d['sig'] : false;
  26. if ( empty( $sig ) ) die( $debug ? '<!-- empty sig -->' : '' );
  27. unset( $d['sig'] );
  28. ksort( $d );
  29. // find defines from the wp-config
  30. function qsot_fetch_defines( $what, $p ) {
  31. // get current host name for later compare
  32. $current_host = strtolower( isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : ( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : false ) );
  33. if ( empty( $current_host ) ) die( $debug ? '<!-- empty current_host -->' : '' );
  34. // break down our indicator url
  35. $p = @parse_url( $p );
  36. // validate that the indicator url is from our domain
  37. if ( strtolower( isset( $p['host'] ) ? $p['host'] : '' ) != $current_host ) die( $debug ? '<!-- current host mismatch : ' . $p['host'] . ' / ' . $current_host . ' -->' : '' );
  38. // otce path. the starting path to look up from
  39. $otce_path = dirname( dirname( dirname( __FILE__ ) ) );
  40. // container for the path to the wp-config file
  41. $path_to_file = '';
  42. // path to the possibly present custom config, which defines where the wp-config.php file is located, so we dont have to look for it
  43. $custom_config_path = dirname( $otce_path ). DIRECTORY_SEPARATOR . 'qsot-phpqrconfig.php';
  44. $custom_config_exists = false;
  45. // first check if we have a config file added above the otce dir that defines where to look for wp-config
  46. if ( @file_exists( $custom_config_path ) && is_readable( $custom_config_path ) ) {
  47. include_once $custom_config_path;
  48. if ( defined( 'QSOT_WP_CONFIG_LOCATION' ) && @file_exists( QSOT_WP_CONFIG_LOCATION ) && is_readable( QSOT_WP_CONFIG_LOCATION ) ) {
  49. $path_to_file = QSOT_WP_CONFIG_LOCATION;
  50. $custom_config_exists = true;
  51. }
  52. }
  53. // if we do not know where the wp-config is yet, then look for it in the most common paths to check
  54. if ( ! $path_to_file ) {
  55. $search_paths = array(
  56. dirname( dirname( dirname( $otce_path ) ) ),
  57. rtrim( realpath( $_SERVER['DOCUMENT_ROOT'] ), '\\/' ),
  58. dirname( dirname( dirname( dirname( $otce_path ) ) ) ),
  59. dirname( realpath( $_SERVER['DOCUMENT_ROOT'] ) ),
  60. );
  61. // cycle through all the common paths, and check for the existence of the wp-config.php file
  62. foreach ( $search_paths as $search_path ) {
  63. $test_file_path = $search_path . DIRECTORY_SEPARATOR . 'wp-config.php';
  64. if ( @file_exists( $test_file_path ) && is_readable( $test_file_path ) ) {
  65. $path_to_file = $test_file_path;
  66. break;
  67. }
  68. }
  69. }
  70. // if we still do not have a path to wp-config.php, then try to bruteforce the location by traversing upwards until we find it or cannot go any further
  71. if ( ! $path_to_file ) {
  72. $last_path = $next_path = $otce_path;
  73. while ( ( $next_path = dirname( $next_path ) ) && $next_path != $last_path && is_readable( $next_path ) ) {
  74. $last_path = $next_path;
  75. if ( file_exists( $next_path . DIRECTORY_SEPARATOR . 'wp-config.php' ) ) {
  76. $path_to_file = $next_path . DIRECTORY_SEPARATOR . 'wp-config.php';
  77. }
  78. }
  79. }
  80. // determine where the wp-config is
  81. if ( empty( $path_to_file ) || ! file_exists( $path_to_file ) || !is_readable( $path_to_file ) ) die( $debug ? '<!-- missing wp-config -->' : '' );
  82. // at this point we have the config file. lets try to create that custom config path if we can so we can save ourselves some time later
  83. if ( ! $custom_config_exists && is_writable( dirname( $custom_config_path ) ) ) {
  84. // if the config file exists, but does not contain what we need, then just try to remove it
  85. if ( @file_exists( $custom_config_path ) && is_writable( $custom_config_path ) )
  86. unlink( $custom_config_path );
  87. // if we can create the file from scratch, do it now
  88. if ( ! @file_exists( $custom_config_path ) )
  89. file_put_contents( $custom_config_path, "<?php if ( ! defined( 'QSOT_WP_CONFIG_LOCATION' ) ) define( 'QSOT_WP_CONFIG_LOCATION', '{$path_to_file}' );" );
  90. }
  91. // search the config for the requested defines
  92. $contents = file_get_contents( $path_to_file );
  93. $out = array( $path_to_file );
  94. foreach ( $what as $define ) {
  95. preg_match_all( '#.*define.*' . preg_quote( $define, '#' ) . '(\'|")\s*,\s*(\'|")([^\2]+?)\2#s', $contents, $matches, PREG_SET_ORDER );
  96. if ( empty( $matches ) ) {
  97. $out[] = '';
  98. } else {
  99. $out[] = $matches[0][3];
  100. }
  101. }
  102. return $out;
  103. }
  104. // validate signature
  105. list( $wp_config, $key, $salt ) = qsot_fetch_defines( array( 'NONCE_KEY', 'NONCE_SALT' ), $d['p'] );
  106. $test = sha1( $key . @json_encode( $d ) . $salt );
  107. if ( $test != $sig ) die( $debug ? '<!-- hash mismatch : ' . $wp_config . ' = ' . $sig . ' / '. $test . ' -->' : '' );
  108. // end abuse protection
  109. include_once 'qrlib.php';
  110. //QRCode::png($d, false, 'L', 3, 1);
  111. $enc = QRencode::factory('L', 3, 1);
  112. $outfile = false;
  113. try {
  114. ob_start();
  115. $tab = $enc->encode($d['d']);
  116. $err = ob_get_contents();
  117. ob_end_clean();
  118. if ($err != '')
  119. QRtools::log($outfile, $err);
  120. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2 * $enc->margin));
  121. QRimage::jpg($tab, $outfile, 2.5/* min(max(1, $enc->size), $maxSize)*/, $enc->margin, 100);
  122. } catch (Exception $e) {
  123. QRtools::log($outfile, $e->getMessage());
  124. }