Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.22 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 = 0;
  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.         $size += fpassthru($handle);
  437.         logline("proxy, binary passthru() finished. closing handle");
  438.         fclose($handle);
  439.         logline("proxy, total binary tranfer size = ", $size);
  440.         if ($size > 0) {
  441.             logline("proxy, registering bandwidth usage of binary content to");
  442.             register_bandwidth_usage($size);
  443.         }
  444.         //exit;
  445.  
  446.     }
  447.  
  448.     // Proxy rewrite resource
  449.  
  450.     proxyhelp_init_for_repress($rewritebase, $abbreviation, $remoteservers, $useHashes, $secret);
  451.  
  452.     $contents = proxify_html_resource($contents);
  453.  
  454.     /* Push text. */
  455.  
  456.     $size = strlen($contents);
  457.     if ($size > 0) {
  458.         logline("proxy, registering bandwidth usage of text content");
  459.         register_bandwidth_usage($size);
  460.     }
  461.     echo $contents;
  462.  
  463.     /* Done. */
  464.  
  465.     exit;
  466.  
  467. }
  468.  
  469. }
  470.  
  471. // This function takes a plain hostname and determines if RePress is authorized to proxy it, and obliged to rewrite it
  472. // returns either TRUE or FALSE
  473. function repress_recognize_host($stripped) {
  474.  
  475.     logline("repress_recognize_host, host = ", $stripped);
  476.  
  477.     $remoteservers = json_decode( get_option('repress_remoteservers'), TRUE );
  478.  
  479.     /** First check if this hostname is legal **/
  480.  
  481.     // split the domain into subdomains
  482.     $subdomains = explode('.', $stripped);
  483.  
  484.     foreach ($subdomains as $subdom) {
  485.         if (!is_legal_domain_part($subdom)) {
  486.             logline("repress_recognize_host, illegal domain part ", $subdom);
  487.             return FALSE;       // will not work with this object
  488.         }
  489.     }
  490.  
  491.     /** Now search to find if this hostname is one of the (sub)domains we proxy **/
  492.  
  493.     foreach ($remoteservers as $hostname) {
  494.  
  495.         logline("repress_recognize_host, match checking on ", $hostname);
  496.  
  497.         // split the proxied domain into subdomains
  498.  
  499.         $proxiedSubdomains = explode('.', $hostname);
  500.         $numParts = count($proxiedSubdomains);
  501.  
  502.         if (!($numParts > count($subdomains))) {
  503.  
  504.             $okMatch = true;
  505.  
  506.             for ($i = $numParts - 1, $b = count($subdomains) - 1; $i >= 0; $i--,$b--) {
  507.                 logline("repress_recognize_host, proxied domain subpart is ", $proxiedSubdomains[$i]);
  508.                 if ($proxiedSubdomains[$i] !== $subdomains[$b]) {
  509.                     logline("repress_recognize_host, proxied domain subpart does not match ", $subdomains[$b]);
  510.                     $okMatch = false;
  511.                     break;
  512.                 } else {
  513.                     logline("repress_recognize_host, proxied domain subpart matches ", $subdomains[$b]);
  514.                 }
  515.             }
  516.  
  517.             if ($okMatch) {
  518.                 return TRUE;
  519.             }
  520.  
  521.         }
  522.  
  523.     }
  524.  
  525.     return FALSE;       // no match was found
  526.  
  527. }
  528.  
  529.  
  530. // Build the url string with GET query if neccessary, todo force HTTPS if neccesary */
  531. function repress_build_query_url($url) {
  532.  
  533.     logline("repress_build_query_url, url = ", $url);
  534.  
  535.         $parsed = parse_url($url);
  536.  
  537.         if (isset($parsed['query'])) {
  538.  
  539.                 return $parsed['scheme'] . '://' . $parsed['host'] . $parsed['path'] . '?' . repress_rawqueryencode($parsed['query']);
  540.  
  541.         } elseif (isset($parsed['path'])) {
  542.  
  543.                 return $parsed['scheme'] . '://' . $parsed['host'] . $parsed['path'];
  544.  
  545.         } else {
  546.  
  547.                 return $parsed['scheme'] . '://' . $parsed['host'];
  548.  
  549.         }
  550.  
  551. }
  552.  
  553. function repress_rawqueryencode($urlPart) {
  554.  
  555.     logline("repress_rawqueryencode, urlpart = ", $urlPart);
  556.  
  557.     $queryString = '';
  558.  
  559.     $assignments = explode('&', $urlPart);
  560.  
  561.     $first = true;
  562.  
  563.     foreach ($assignments as $assignment) {
  564.  
  565.         if ($first) {
  566.             $first = false;
  567.         } else {
  568.             $queryString .= '&';
  569.         }
  570.  
  571.         // raw url encode of value
  572.  
  573.         if (!preg_match("/\=/", $assignment)) {
  574.             logline("repress_rawqueryencode, this does not look like an assignment: ",  $assignment);
  575.             logline("repress_rawqueryencode, adding to query string as literal");
  576.             $queryString .= $assignment;
  577.         } else {
  578.             $dec = explode("=", $assignment);
  579.             $queryString .= $dec[0] .= '=';
  580.             if (isset($dec[1])) {
  581.                 $queryString .= rawurlencode($dec[1]);
  582.             }
  583.         }
  584.  
  585.     }
  586.  
  587.     return $queryString;
  588. }
  589.  
  590. function register_bandwidth_usage($size) {
  591.  
  592.     logline("register_bandwidth_usage, size = ", $size);
  593.  
  594.     if ( function_exists('repress_register_bandwidth_usage') ) {
  595.         repress_register_bandwidth_usage($size);
  596.     }
  597.  
  598. }
  599.  
  600. /** Log and debug functions **/
  601.  
  602. // dump a log line to debug file
  603. function logline($line, $data = '') {
  604.     if (!$GLOBALS['proxydebug'] || !$GLOBALS['proxydebugfile']) { return; }
  605.  
  606.     if (!is_link($GLOBALS['proxydebugfile'])) {
  607.  
  608.         // prepare printing of data
  609.  
  610.         $printData = $data;
  611.  
  612.         if (is_nonprintable_string($data)) {
  613.             $printData = '[nonprintable. hex dump] ' . hexdump_with_spaces( bin2hex($data) );
  614.         }
  615.  
  616.         $printLine = $line . $printData;
  617.  
  618.         if (strlen($printLine > 2048)) {
  619.             // truncate
  620.             $printLine = substr($printLine, 0, 2048);
  621.         }
  622.  
  623.         // write log line to file
  624.  
  625.         $now = date("Ymd H:i:s");
  626.         $f = fopen($GLOBALS['proxydebugfile'], "a");
  627.  
  628.         if (flock($f, LOCK_EX)) {
  629.             fputs($f, '[' . $now . '] ' . $printLine . "\n");
  630.             fflush($f);
  631.             flock($f, LOCK_UN); // release the lock
  632.         }
  633.  
  634.         fclose($f);
  635.     }
  636. }
  637.  
  638. // this function determines if string is easy printable asci, like hostnames, otherwise we hex dump the data string
  639. // utf8 not supported
  640. function is_nonprintable_string($str) {
  641.  
  642.     for ($i = 0; $i < strlen($str); $i++) {
  643.         $ascii = ord($str[$i]);
  644.         if ($ascii < 32 || $ascii > 126) {
  645.             return TRUE;
  646.         }
  647.     }
  648.  
  649.     return FALSE;
  650.  
  651. }
  652.  
  653. // format hex output with spaces
  654. function hexdump_with_spaces($hexstr) {
  655.     $r = '';
  656.     for ($i = 0; $i < strlen($hexstr); $i++) {
  657.         if ($i > 0 && $i % 2 == 0) {
  658.             $r .= ' ';
  659.         }
  660.         $r .= $hexstr[$i];
  661.     }
  662.     return $r;
  663. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement