Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.20 KB | None | 0 0
  1. <?php
  2.  
  3. /** Options from Wordpress (stored in database) **/
  4.  
  5. // You should normally not change any of these values. Use the Wordpress backend instead.
  6.  
  7. $rewritebase = repress_make_rewrite_base();
  8. $remoteservers = json_decode( get_option('repress_remoteservers'), TRUE );
  9. $agentstring = "RePress plugin/" . plugin_get_version() . " (PHP " . phpversion() . " streamer) - please read https://all4xs.net";
  10. if (get_option('repress_push_post') == 'yes') {
  11.     $doPosts = true;
  12. } else {
  13.     $doPosts = false;
  14. }
  15. if (get_option('repress_push_cookie') == 'yes') {
  16.     $doCookies = true;
  17. } else {
  18.     $doCookies = false;
  19. }
  20. if (get_option('repress_secret') !== '') {
  21.     $secret = get_option('repress_secret');
  22. } else {
  23.     $secret = 'repress';        // make something up if you want to support hashed cookies
  24. }
  25. if (get_option('repress_use_hashes') == 'yes') {
  26.     $useHashes = true;
  27. } else {
  28.     $useHashes = false;
  29. }
  30. if (get_option('repress_curl_wrapper') == 'yes') { // mh_edit get the stored value of the proxy wrapper type
  31.     $curlWrapper = true;
  32. } else {
  33.     $curlWrapper = false;
  34. }
  35. $acceptCookies = array ( '' );
  36.  
  37. /** Debugging opions **/
  38.  
  39. $GLOBALS['proxydebug'] = false;     // debugging? will dump all output to flat file.
  40. $GLOBALS['proxydebugfile'] = '/tmp/repress.module.debug.log';
  41. error_reporting(0);
  42.  
  43. /** Detect WordPress permalink structure **/
  44. if ( get_option('permalink_structure') == '' ) {
  45.  
  46.     // There is no permalink structure. We will do nothing.
  47.  
  48. } else {
  49.  
  50. /*** Proxy code begins here. ***/
  51.  
  52. // Include dependencies
  53.  
  54. // proxyhelp requirement
  55. require_once('proxyhelp/proxify_html_resource.php');
  56. // domain functions
  57. require_once('domains.php');
  58.  
  59. /** Attempt to destroy cookies above RePress base **/
  60.  
  61. require_once("emptycookieredirect.php");
  62.  
  63. /** Process request **/
  64.  
  65. if (!isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == '') {
  66.     logline("proxy, called without request. exiting");
  67.     exit;
  68. }
  69.  
  70. logline("proxy, intercepted request: ", $_SERVER['REQUEST_URI']);
  71.  
  72. $hit = false; $extra = ''; $strippedRepress = ''; $remoteserver = ''; $abbreviation = ''; $text = false;
  73.  
  74. $len = strlen($rewritebase);
  75. if (strncmp($_SERVER['REQUEST_URI'], $rewritebase, $len) == 0) {
  76.  
  77.     logline("proxy, our rewritebase / in url. Seems promising at first sight.");
  78.  
  79.     // strip repress/ part
  80.  
  81.     if (preg_match("/#\//", $rewritebase)) { continue; }
  82.     $strippedRepress = preg_replace("#^" . $rewritebase . "[\/]?#", "", $_SERVER['REQUEST_URI']);
  83.  
  84.     // strip everything after first slash
  85.  
  86.     $stripped = preg_replace("/\/.*$/", "", $strippedRepress);
  87.  
  88.     // domain name should be left over, this could be a hash (obfuscated domain name)
  89.  
  90.     if ($useHashes) {
  91.         logline("proxy, trying to decode the domain name from its hashed value");
  92.         $stripped = rawurldecode($stripped);        // now we have Base64 encoded hash
  93.         logline("proxy, rawurldecode() returns ", $stripped);
  94.         $stripped = repress_get_host_from_hash($stripped, $secret);
  95.         $stripped = rtrim($stripped);
  96.         logline("proxy, decode output = ", $stripped);
  97.     }
  98.  
  99.     /** Possible hit. Is the domain one we proxy? **/
  100.  
  101.     if (repress_recognize_host($stripped)) {
  102.  
  103.         $hit = true; $remoteserver = $stripped;
  104.  
  105.     }
  106.  
  107.     if ($hit == false) {
  108.         header('HTTP/1.0 404 Not Found');
  109.  
  110.         exit;       // close, but no cigar.
  111.     }
  112. }
  113.  
  114. if ($hit) {
  115.  
  116.     /// Store the 'abbreviation' of this host, ie. either the hostname or its hash
  117.  
  118.     if ($useHashes) {
  119.         $abbreviation = repress_obfuscate($remoteserver, $secret);
  120.     } else {
  121.         $abbreviation = $remoteserver;
  122.     }
  123.  
  124.     logline("proxy, looking for something extra, a path, or query.");
  125.  
  126.     // now strippedRepress looks something like: wikileaks.org[/][....]
  127.  
  128.     // if no slashes, no extra
  129.     if (strpos($strippedRepress,'/') == FALSE) {
  130.         $extra = '';
  131.     } else {
  132.         // extra is everything after the first slash
  133.         $extra = preg_replace("/^.*?\//", "", $strippedRepress);
  134.  
  135.         // and may be obfuscated ..
  136.         // but to make it even more complex. part of this url string can contain GET parameters that were added by the browser
  137.         // as part of a form field for instance, and these will _not_ be obfuscated because we do not rewrite form field IDs in pages
  138.  
  139.         if ($useHashes && $extra !== '') {
  140.  
  141.             logline("proxy, we got an extra and we are going to de-obfuscate it, extra = ", $extra);
  142.  
  143.             logline("proxy, first looking for GET parameters in this string");
  144.             if (preg_match("/^(.*)\?(.*)$/", $extra, $matches)) {
  145.                 logline("proxy, it does have 'em, rip apart");
  146.                 $extra = rawurldecode($matches[1]);
  147.                 logline("proxy, raw decode: ", $extra);
  148.                 $extra = repress_get_host_from_hash($extra, $secret);
  149.                 logline("proxy, hash decode: ", $extra);
  150.                 $extra = rtrim($extra);
  151.                 $extra .= '?' . $matches[2];
  152.  
  153.             } else {
  154.                 logline("proxy, nope. Totally obfuscated.");
  155.                 $extra = rawurldecode($extra);
  156.                 $extra = repress_get_host_from_hash($extra, $secret);
  157.                 $extra = rtrim($extra);
  158.             }
  159.             logline("proxy, now it has become ", $extra);
  160.         }
  161.     }
  162.  
  163.     // try to protect against XSS attacks on Wordpress sessions if running as plugin
  164.     if (function_exists('repress_logout_wordpress_user')) {
  165.         repress_logout_wordpress_user();
  166.     }
  167.  
  168.     logline("proxy, interpreted a hit as follows:");
  169.     logline("proxy, remoteserver = ", $remoteserver);
  170.     logline("proxy, abbreviation = ", $abbreviation);
  171.     logline("proxy, extra = ", $extra);
  172.  
  173.     // do we do a POST?
  174.  
  175.     if ($doPosts && is_array($_POST) && count($_POST) > 0) {
  176.         $requestType = 'POST';
  177.     } else {
  178.         $requestType = 'GET';
  179.     }
  180.  
  181.     // do we do cookies?
  182.  
  183.     $cookieHeaders = '';
  184.  
  185.     if ($doCookies && is_array($_COOKIE) && count($_COOKIE) > 0) {
  186.         foreach ($_COOKIE as $name => $val) {
  187.             $encodeName = rawurlencode($name);
  188.             $encodeVal = rawurlencode($val);
  189.             $cookieHeaders .= "Cookie: $encodeName=$encodeVal\r\n";
  190.         }
  191.     }
  192.  
  193.     /* Establish streaming connection to the remote server */
  194.  
  195.     if ($requestType == 'GET') {
  196.         $streamOptions = array(
  197.                       'http'=>array(
  198.                                   'method'=>"GET",
  199.                           'host'=>$remoteserver,
  200.                           'user_agent'=>$agentstring,
  201.                           'header'=>$cookieHeaders
  202.                            )
  203.                       );
  204.     } else {
  205.         $postData = http_build_query($_POST);
  206.  
  207.         $streamOptions = array(
  208.                       'http'=>array(
  209.                                       'method'=>"POST",
  210.                           'host'=>$remoteserver,
  211.                           'user_agent'=>$agentstring,
  212.                           'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
  213.                                     "Content-Length: " . strlen($postData) . "\r\n" .
  214.                                 $cookieHeaders,
  215.                           'content'=>$postData
  216.                            )
  217.                       );
  218.     }
  219.  
  220.     $context = stream_context_create($streamOptions);
  221.  
  222.     logline("proxy, calling build_query function");
  223.  
  224.     $urlOpen = repress_build_query_url("http://$remoteserver/$extra");
  225.  
  226.     logline("proxy, open to location ", "'http://$remoteserver/$extra' as '$urlOpen'");
  227.  
  228.     $handle = fopen($urlOpen, "rb", false, $context);
  229.     if ($handle == FALSE) {
  230.         if (isset($GLOBALS['proxydebug']) && $GLOBALS['proxydebug']) {
  231.             echo("Proxy error to: http://$remoteserver/$extra");
  232.         }
  233.         exit;
  234.     }
  235.  
  236.     /* Start working on headers */
  237.     if($curlWrapper)
  238.         $one_byte = fread($handle, 1); // mh_edit read one byte to populate header data if the curl wrapper is used
  239.     $meta = stream_get_meta_data($handle);
  240.  
  241.     if($meta['wrapper_type']=='cURL' ^ $curlWrapper){ // mh_edit if the wrapper type differs from that recorded in the db
  242.         update_option('repress_curl_wrapper', (($meta['wrapper_type']=='cURL') ? 'yes' : 'no')); // mh_edit set the db val
  243.         echo 'Proxy wrapper type set to `' . $meta['wrapper_type'] . '`.<br/>Please refresh your browser to continue.<br/><br/><small><strong>fix by mh @ monkeyr.com</strong></small>'; // mh_edit notify the user
  244.         exit;
  245.     }
  246.  
  247.     $headerData = $meta['wrapper_data'];
  248.     if($curlWrapper && is_array($headerData) && is_array(@$headerData['headers']))
  249.         $headerData = $headerData['headers']; // mh_edit this is added to get to the actual response headers into $headerData when the curl wrapper is used
  250.  
  251.     if (preg_match("/HTTP.... 30./", $headerData[0])) $redirect = true;
  252.     else {
  253.         $redirect = false;
  254.     }
  255.  
  256.     $i = 0;
  257.     foreach ($headerData as $line) {
  258.         $i++;
  259.  
  260.         if ($i > 1 && preg_match("/^HTTP\/1../", $line)) {
  261.             // no multiple HTTP headers will be replied
  262.             exit;
  263.         }
  264.  
  265.         logline("proxy, got header: ", $line);
  266.  
  267.         // make 302 redirects work
  268.         if ($redirect && !preg_match("#$rewritebase#", $line)) {
  269.  
  270.             // a redirect to root is a redirect to main page here
  271.             $line = preg_replace("#^Location: /$#i", "Location: $rewritebase/$abbreviation/", $line);
  272.  
  273.             // a relative redirect
  274.             if (preg_match("#^Location: /(.+)#i", $line, $matches)) {
  275.  
  276.                 if ($useHashes) {
  277.  
  278.                     // will have to be rewritten to obfuscated path if hashes are used
  279.  
  280.                     $relativePath = repress_obfuscate($matches[1], $secret);
  281.                     rtrim($relativePath);
  282.                     $line = "Location: $rewritebase/$abbreviation/$relativePath";
  283.  
  284.                 } else {
  285.  
  286.                     $line = preg_replace("#^Location: /(.+)#i", "Location: $rewritebase/$abbreviation/$1", $line);
  287.  
  288.                 }
  289.  
  290.             }
  291.  
  292.             // we localize redirects to all known hosts, and subdomains of those hosts
  293.             foreach ($remoteservers as $knownhost) {
  294.                 if ($useHashes) {
  295.                     $knownhostAbbr = repress_obfuscate($knownhost, $secret);
  296.                 } else {
  297.                     $knownhostAbbr = $knownhost;
  298.                 }
  299.  
  300.                 // todo: Use something similar to repress_recognize_host() mechanism here!
  301.  
  302.                 // an absolute redirect to a main page
  303.                 $line = preg_replace("#^Location: http://$knownhost$#", "Location: $rewritebase/$knownhostAbbr", $line);
  304.                 $line = preg_replace("#^Location: http://$knownhost/$#", "Location: $rewritebase/$knownhostAbbr", $line);
  305.                 // an absolute redirect to a lower page, which may be an obfuscated path
  306.                 if ($useHashes) {
  307.                     if (preg_match("#^Location: http://$knownhost/(.*)$#", $line, $matches)) {
  308.                         $obfsPath = repress_obfuscate($matches[1], $secret);
  309.                         $obfsPath = rtrim($obfsPath);
  310.                         $line = preg_replace("#^Location: http://$knownhost/(.*)$#", "Location: $rewritebase/$knownhostAbbr/$obfsPath", $line);
  311.                     }
  312.                 } else {
  313.                     $line = preg_replace("#^Location: http://$knownhost/(.*)$#", "Location: $rewritebase/$knownhostAbbr/$1", $line);
  314.                 }
  315.  
  316.                 // Support subdomains
  317.  
  318.                 if ($useHashes) {
  319.  
  320.                     // an absolute redirect to a main page
  321.                     if (preg_match("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost$#", $line, $matches)) {
  322.                         $abbrSub = repress_obfuscate($matches[1] . '.' . $knownhost, $secret);
  323.                         $abbrSub = rtrim($abbrSub);
  324.                         $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost#", "Location: $rewritebase/$abbrSub", $line);
  325.                     }
  326.                     if (preg_match("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/$#", $line, $matches)) {
  327.                         $abbrSub = repress_obfuscate($matches[1] . '.' . $knownhost, $secret);
  328.                         $abbrSub = rtrim($abbrSub);
  329.                         $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/#", "Location: $rewritebase/$abbrSub", $line);
  330.                     }
  331.                     // an absolute redirect to a lower page, an obfuscated path
  332.                     if (preg_match("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/(.*)$#", $line, $matches)) {
  333.                         $abbrSub = repress_obfuscate($matches[1] . '.' . $knownhost, $secret);
  334.                         $abbrSub = rtrim($abbrSub);
  335.                         $obfsPath = repress_obfuscate($matches[2], $secret);
  336.                         $obfsPath = rtrim($obfsPath);
  337.                         $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/(.*)$#", "Location: $rewritebase/$abbrSub/$obfsPath", $line);
  338.                     }
  339.  
  340.                 } else {
  341.                     // an absolute redirect to a main page
  342.                     $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost$#", "Location: $rewritebase/$1.$knownhostAbbr", $line);
  343.                     $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/$#", "Location: $rewritebase/$1.$knownhostAbbr", $line);
  344.                     // an absolute redirect to a lower page, not an obfuscated path
  345.                     $line = preg_replace("#^Location: http://([a-zA-Z0-9\.\-]*?)\.$knownhost/(.*)$#", "Location: $rewritebase/$1.$knownhostAbbr/$2", $line);
  346.                 }
  347.             }
  348.         }
  349.  
  350.         // cookie handling
  351.         if (preg_match("/^Set-Cookie: /i", $line)) {
  352.  
  353.             if ($doCookies == false) {
  354.  
  355.                 continue;
  356.  
  357.             }
  358.  
  359.             logline("proxy, found cookie header ", $line);
  360.  
  361.             // rewrite cookie paths
  362.  
  363.             if (preg_match("#; path=([^;]*)#", $line, $matches)) {
  364.  
  365.                 $pathNow = $matches[1];
  366.  
  367.                 logline("proxy, cookie path found ", $pathNow);
  368.  
  369.                 if (preg_match("/^/", $pathNow)) {
  370.  
  371.                     $pathNew = "$rewritebase/$abbreviation" . $pathNow;
  372.                     $line = preg_replace("#; path=([^;]*)#", "; path=$pathNew", $line);
  373.  
  374.                     logline("proxy, insecure path. setting to ", $pathNew);
  375.                 }
  376.  
  377.             }
  378.  
  379.  
  380.             // set all cookies to be httponly
  381.  
  382.             if (!preg_match("/HttpOnly/i", $line)) {
  383.                 logline("proxy, setting this cookie to httponly");
  384.  
  385.                 $line .= "; httpOnly";
  386.             }
  387.  
  388.         }
  389.  
  390.         // get content type
  391.         if (preg_match("/^Content-Type: text\/.*$/i", $line)) {
  392.             $text = true;
  393.         }
  394.  
  395.         // ignore robots header. we have our own
  396.  
  397.         if (preg_match("/^X-Robots-Tag: /i", $line)) {
  398.             continue;
  399.         }
  400.  
  401.         // pass through headers
  402.  
  403.         logline("proxy, give header: ", $line);
  404.         header($line);
  405.  
  406.     }
  407.  
  408.     // After a 30x relocation response, exit immediatly
  409.     if ($redirect) {
  410.         logline("proxy, exit because relocate");
  411.         exit;
  412.     }
  413.  
  414.     // add no robots header
  415.     header("X-Robots-Tag: noindex, nofollow", true);
  416.  
  417.     if ($text) {
  418.  
  419.         /* Read text page and close handle */
  420.         $contents = '';
  421.         if($curlWrapper) // mh_edit add the one byte read already to $contents when the curl wrapper is used
  422.             $contents = $one_byte;
  423.         $contents .= stream_get_contents($handle);
  424.         fclose($handle);
  425.  
  426.     } else {
  427.  
  428.         /* Pass through binary content or non-parsed content */
  429.  
  430.         logline("proxy, starting passthru() of binary data from ", "http://$remoteserver/$extra");
  431.         $size = fpassthru($handle);
  432.         if($curlWrapper){ // mh_edit when the curl wrapper is used
  433.             echo $one_byte; // mh_edit add the one byte read already to the output stream
  434.             $size++; // mh_edit add the one byte read already to the size of the data read
  435.         }
  436.         logline("proxy, binary passthru() finished. closing handle");
  437.         fclose($handle);
  438.         logline("proxy, total binary tranfer size = ", $size);
  439.         if ($size > 0) {
  440.             logline("proxy, registering bandwidth usage of binary content to");
  441.             register_bandwidth_usage($size);
  442.         }
  443.         exit;
  444.  
  445.     }
  446.  
  447.     // Proxy rewrite resource
  448.  
  449.     proxyhelp_init_for_repress($rewritebase, $abbreviation, $remoteservers, $useHashes, $secret);
  450.  
  451.     $contents = proxify_html_resource($contents);
  452.  
  453.     /* Push text. */
  454.  
  455.     $size = strlen($contents);
  456.     if ($size > 0) {
  457.         logline("proxy, registering bandwidth usage of text content");
  458.         register_bandwidth_usage($size);
  459.     }
  460.     echo $contents;
  461.  
  462.     /* Done. */
  463.  
  464.     exit;
  465.  
  466. }
  467.  
  468. }
  469.  
  470. // This function takes a plain hostname and determines if RePress is authorized to proxy it, and obliged to rewrite it
  471. // returns either TRUE or FALSE
  472. function repress_recognize_host($stripped) {
  473.  
  474.     logline("repress_recognize_host, host = ", $stripped);
  475.  
  476.     $remoteservers = json_decode( get_option('repress_remoteservers'), TRUE );
  477.  
  478.     /** First check if this hostname is legal **/
  479.  
  480.     // split the domain into subdomains
  481.     $subdomains = explode('.', $stripped);
  482.  
  483.     foreach ($subdomains as $subdom) {
  484.         if (!is_legal_domain_part($subdom)) {
  485.             logline("repress_recognize_host, illegal domain part ", $subdom);
  486.             return FALSE;       // will not work with this object
  487.         }
  488.     }
  489.  
  490.     /** Now search to find if this hostname is one of the (sub)domains we proxy **/
  491.  
  492.     foreach ($remoteservers as $hostname) {
  493.  
  494.         logline("repress_recognize_host, match checking on ", $hostname);
  495.  
  496.         // split the proxied domain into subdomains
  497.  
  498.         $proxiedSubdomains = explode('.', $hostname);
  499.         $numParts = count($proxiedSubdomains);
  500.  
  501.         if (!($numParts > count($subdomains))) {
  502.  
  503.             $okMatch = true;
  504.  
  505.             for ($i = $numParts - 1, $b = count($subdomains) - 1; $i >= 0; $i--,$b--) {
  506.                 logline("repress_recognize_host, proxied domain subpart is ", $proxiedSubdomains[$i]);
  507.                 if ($proxiedSubdomains[$i] !== $subdomains[$b]) {
  508.                     logline("repress_recognize_host, proxied domain subpart does not match ", $subdomains[$b]);
  509.                     $okMatch = false;
  510.                     break;
  511.                 } else {
  512.                     logline("repress_recognize_host, proxied domain subpart matches ", $subdomains[$b]);
  513.                 }
  514.             }
  515.  
  516.             if ($okMatch) {
  517.                 return TRUE;
  518.             }
  519.  
  520.         }
  521.  
  522.     }
  523.  
  524.     return FALSE;       // no match was found
  525.  
  526. }
  527.  
  528.  
  529. // Build the url string with GET query if neccessary, todo force HTTPS if neccesary */
  530. function repress_build_query_url($url) {
  531.  
  532.     logline("repress_build_query_url, url = ", $url);
  533.  
  534.         $parsed = parse_url($url);
  535.  
  536.         if (isset($parsed['query'])) {
  537.  
  538.                 return $parsed['scheme'] . '://' . $parsed['host'] . $parsed['path'] . '?' . repress_rawqueryencode($parsed['query']);
  539.  
  540.         } elseif (isset($parsed['path'])) {
  541.  
  542.                 return $parsed['scheme'] . '://' . $parsed['host'] . $parsed['path'];
  543.  
  544.         } else {
  545.  
  546.                 return $parsed['scheme'] . '://' . $parsed['host'];
  547.  
  548.         }
  549.  
  550. }
  551.  
  552. function repress_rawqueryencode($urlPart) {
  553.  
  554.     logline("repress_rawqueryencode, urlpart = ", $urlPart);
  555.  
  556.     $queryString = '';
  557.  
  558.     $assignments = explode('&', $urlPart);
  559.  
  560.     $first = true;
  561.  
  562.     foreach ($assignments as $assignment) {
  563.  
  564.         if ($first) {
  565.             $first = false;
  566.         } else {
  567.             $queryString .= '&';
  568.         }
  569.  
  570.         // raw url encode of value
  571.  
  572.         if (!preg_match("/\=/", $assignment)) {
  573.             logline("repress_rawqueryencode, this does not look like an assignment: ",  $assignment);
  574.             logline("repress_rawqueryencode, adding to query string as literal");
  575.             $queryString .= $assignment;
  576.         } else {
  577.             $dec = explode("=", $assignment);
  578.             $queryString .= $dec[0] .= '=';
  579.             if (isset($dec[1])) {
  580.                 $queryString .= rawurlencode($dec[1]);
  581.             }
  582.         }
  583.  
  584.     }
  585.  
  586.     return $queryString;
  587. }
  588.  
  589. function register_bandwidth_usage($size) {
  590.  
  591.     logline("register_bandwidth_usage, size = ", $size);
  592.  
  593.     if ( function_exists('repress_register_bandwidth_usage') ) {
  594.         repress_register_bandwidth_usage($size);
  595.     }
  596.  
  597. }
  598.  
  599. /** Log and debug functions **/
  600.  
  601. // dump a log line to debug file
  602. function logline($line, $data = '') {
  603.     if (!$GLOBALS['proxydebug'] || !$GLOBALS['proxydebugfile']) { return; }
  604.  
  605.     if (!is_link($GLOBALS['proxydebugfile'])) {
  606.  
  607.         // prepare printing of data
  608.  
  609.         $printData = $data;
  610.  
  611.         if (is_nonprintable_string($data)) {
  612.             $printData = '[nonprintable. hex dump] ' . hexdump_with_spaces( bin2hex($data) );
  613.         }
  614.  
  615.         $printLine = $line . $printData;
  616.  
  617.         if (strlen($printLine > 2048)) {
  618.             // truncate
  619.             $printLine = substr($printLine, 0, 2048);
  620.         }
  621.  
  622.         // write log line to file
  623.  
  624.         $now = date("Ymd H:i:s");
  625.         $f = fopen($GLOBALS['proxydebugfile'], "a");
  626.  
  627.         if (flock($f, LOCK_EX)) {
  628.             fputs($f, '[' . $now . '] ' . $printLine . "\n");
  629.             fflush($f);
  630.             flock($f, LOCK_UN); // release the lock
  631.         }
  632.  
  633.         fclose($f);
  634.     }
  635. }
  636.  
  637. // this function determines if string is easy printable asci, like hostnames, otherwise we hex dump the data string
  638. // utf8 not supported
  639. function is_nonprintable_string($str) {
  640.  
  641.     for ($i = 0; $i < strlen($str); $i++) {
  642.         $ascii = ord($str[$i]);
  643.         if ($ascii < 32 || $ascii > 126) {
  644.             return TRUE;
  645.         }
  646.     }
  647.  
  648.     return FALSE;
  649.  
  650. }
  651.  
  652. // format hex output with spaces
  653. function hexdump_with_spaces($hexstr) {
  654.     $r = '';
  655.     for ($i = 0; $i < strlen($hexstr); $i++) {
  656.         if ($i > 0 && $i % 2 == 0) {
  657.             $r .= ' ';
  658.         }
  659.         $r .= $hexstr[$i];
  660.     }
  661.     return $r;
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement