Advertisement
Guest User

Untitled

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