PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/data/xinha/plugins/SuperClean/tidy.php

https://github.com/esetera/BookiPublisher
PHP | 179 lines | 153 code | 14 blank | 12 comment | 5 complexity | 1f98d159499326718c9a9bdfecc2a1c3 MD5 | raw file
  1. <?php
  2. /** This PHP file is intended for use with XMLHTTPRequest from Xinha
  3. * it requrns javascript to set the Xinha html with tidied html that is
  4. * submitted in a $_POST parameter called 'content'
  5. */
  6. if(get_magic_quotes_gpc())
  7. {
  8. // trigger_error('Magic Quotes GPC is on, cleaning GPC.', E_USER_NOTICE);
  9. $to_clean = array(&$_GET, &$_POST, &$_REQUEST, &$_COOKIE);
  10. while(count($to_clean))
  11. {
  12. $cleaning =& $to_clean[array_pop(array_keys($to_clean))];
  13. unset($to_clean[array_pop(array_keys($to_clean))]);
  14. foreach(array_keys($cleaning) as $k)
  15. {
  16. if(is_array($cleaning[$k]))
  17. {
  18. $to_clean[] =& $cleaning[$k];
  19. }
  20. else
  21. {
  22. $cleaning[$k] = stripslashes($cleaning[$k]);
  23. }
  24. }
  25. }
  26. }
  27. header('Content-Type: text/javascript; charset=utf-8');
  28. /** Function to POST some data to a URL */
  29. function PostIt($DataStream, $URL)
  30. {
  31. // Strip http:// from the URL if present
  32. $URL = ereg_replace("^http://", "", $URL);
  33. // Separate into Host and URI
  34. $Host = substr($URL, 0, strpos($URL, "/"));
  35. $URI = strstr($URL, "/");
  36. // Form up the request body
  37. $ReqBody = "";
  38. while (list($key, $val) = each($DataStream)) {
  39. if ($ReqBody) $ReqBody.= "&";
  40. $ReqBody.= $key."=".urlencode($val);
  41. }
  42. $ContentLength = strlen($ReqBody);
  43. // Generate the request header
  44. $ReqHeader =
  45. "POST $URI HTTP/1.0\n".
  46. "Host: $Host\n".
  47. "User-Agent: PostIt\n".
  48. "Content-Type: application/x-www-form-urlencoded\n".
  49. "Content-Length: $ContentLength\n\n".
  50. "$ReqBody\n";
  51. // echo $ReqHeader;
  52. // Open the connection to the host
  53. $socket = fsockopen($Host, 80, $errno, $errstr);
  54. if (!$socket) {
  55. $result = "($errno) $errstr";
  56. return $Result;
  57. }
  58. fputs($socket, $ReqHeader);
  59. $result = '';
  60. while(!feof($socket))
  61. {
  62. $result .= fgets($socket);
  63. }
  64. return $result;
  65. }
  66. function js_encode($string)
  67. {
  68. static $strings = "\\,\",',%,&,<,>,{,},@,\n,\r";
  69. if(!is_array($strings))
  70. {
  71. $tr = array();
  72. foreach(explode(',', $strings) as $chr)
  73. {
  74. $tr[$chr] = sprintf('\x%02X', ord($chr));
  75. }
  76. $strings = $tr;
  77. }
  78. return strtr($string, $strings);
  79. }
  80. // Any errors would screq up our javascript
  81. error_reporting(0);
  82. ini_set('display_errors', false);
  83. if(trim(@$_REQUEST['content']))
  84. {
  85. // PHP's urldecode doesn't understand %uHHHH for unicode
  86. $_REQUEST['content'] = preg_replace('/%u([a-f0-9]{4,4})/ei', 'utf8_chr(0x$1)', $_REQUEST['content']);
  87. function utf8_chr($num)
  88. {
  89. if($num<128)return chr($num);
  90. if($num<1024)return chr(($num>>6)+192).chr(($num&63)+128);
  91. if($num<32768)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  92. if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
  93. return '';
  94. }
  95. ob_start();
  96. passthru("echo " . escapeshellarg($_REQUEST['content']) . " | tidy -q -i -u -wrap 9999 -utf8 -bare -asxhtml 2>/dev/null", $result);
  97. $content = ob_get_contents();
  98. ob_end_clean();
  99. if(strlen($content) < 4)
  100. {
  101. // Tidy on the local machine failed, try a post
  102. $res_1
  103. = PostIt(
  104. array
  105. (
  106. '_function' => 'tidy',
  107. '_html' => $_REQUEST['content'],
  108. 'char-encoding' => 'utf8',
  109. '_output' => 'warn',
  110. 'indent' => 'auto',
  111. 'wrap' => 9999,
  112. 'break-before-br' => 'y',
  113. 'bare' => 'n',
  114. 'word-2000' => 'n',
  115. 'drop-empty-paras' => 'y',
  116. 'drop-font-tags' => 'n',
  117. ),
  118. 'http://infohound.net/tidy/tidy.pl');
  119. if(preg_match('/<a href="([^"]+)" title="Save the tidied HTML/', $res_1, $m))
  120. {
  121. $tgt = strtr($m[1], array_flip(get_html_translation_table(HTML_ENTITIES)));
  122. $content = implode('', file('http://infohound.net/tidy/' . $tgt));
  123. }
  124. }
  125. if(strlen($content) && ! preg_match('/<\/body>/i', $_REQUEST['content']))
  126. {
  127. if( preg_match('/<body[^>]*>(.*)<\/body>/is', $content, $matches) )
  128. {
  129. $content = $matches[1];
  130. }
  131. }
  132. elseif(!strlen($content))
  133. {
  134. $content = $_REQUEST['content'];
  135. }
  136. if($content)
  137. {
  138. ?>
  139. {action:'setHTML',value:'<?php echo js_encode($content) ?>'};
  140. <?php
  141. }
  142. else
  143. {
  144. ?>
  145. {action:'alert',value:'Tidy failed. Check your HTML for syntax errors.'};
  146. <?php
  147. }
  148. }
  149. else
  150. {
  151. ?>
  152. {action:'alert',value:"You don't have anything to tidy!"}
  153. <?php
  154. }
  155. ?>