[6779] | 1 | <? ################################################################### |
---|
| 2 | ## |
---|
| 3 | ## Plugin for htmlArea, to run code through the server's HTML Tidy |
---|
| 4 | ## By Adam Wright, for The University of Western Australia |
---|
| 5 | ## This is the server-side script, which dirty code is run through. |
---|
| 6 | ## |
---|
| 7 | ## Distributed under the same terms as HTMLArea itself. |
---|
| 8 | ## This notice MUST stay intact for use (see license.txt). |
---|
| 9 | ## |
---|
| 10 | |
---|
| 11 | // Get the original source |
---|
| 12 | $source = $_POST['htisource_name']; |
---|
| 13 | $source = stripslashes($source); |
---|
| 14 | |
---|
| 15 | // Open a tidy process - I hope it's installed! |
---|
| 16 | $descriptorspec = array( |
---|
| 17 | 0 => array("pipe", "r"), |
---|
| 18 | 1 => array("pipe", "w"), |
---|
| 19 | 2 => array("file", "/dev/null", "a") |
---|
| 20 | ); |
---|
| 21 | $process = proc_open("tidy -config html-tidy-config.cfg", $descriptorspec, $pipes); |
---|
| 22 | |
---|
| 23 | // Make sure the program started and we got the hooks... |
---|
| 24 | // Either way, get some source code into $source |
---|
| 25 | if (is_resource($process)) { |
---|
| 26 | |
---|
| 27 | // Feed untidy source into the stdin |
---|
| 28 | fwrite($pipes[0], $source); |
---|
| 29 | fclose($pipes[0]); |
---|
| 30 | |
---|
| 31 | // Read clean source out to the browser |
---|
| 32 | while (!feof($pipes[1])) { |
---|
| 33 | //echo fgets($pipes[1], 1024); |
---|
| 34 | $newsrc .= fgets($pipes[1], 1024); |
---|
| 35 | } |
---|
| 36 | fclose($pipes[1]); |
---|
| 37 | |
---|
| 38 | // Clean up after ourselves |
---|
| 39 | proc_close($process); |
---|
| 40 | |
---|
| 41 | } else { |
---|
| 42 | // Better give them back what they came with, so they don't lose it all... |
---|
| 43 | $newsrc = "<body>\n" .$source. "\n</body>"; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | // Split our source into an array by lines |
---|
| 47 | $srcLines = explode("\n",$newsrc); |
---|
| 48 | |
---|
| 49 | // Get only the lines between the body tags |
---|
| 50 | $startLn = 0; |
---|
| 51 | while ( strpos( $srcLines[$startLn++], '<body' ) === false && $startLn < sizeof($srcLines) ); |
---|
| 52 | $endLn = $startLn; |
---|
| 53 | while ( strpos( $srcLines[$endLn++], '</body' ) === false && $endLn < sizeof($srcLines) ); |
---|
| 54 | |
---|
| 55 | $srcLines = array_slice( $srcLines, $startLn, ($endLn - $startLn - 1) ); |
---|
| 56 | |
---|
| 57 | // Create a set of javascript code to compile a new source string |
---|
| 58 | foreach ($srcLines as $line) { |
---|
| 59 | $jsMakeSrc .= "\tns += '" . str_replace("'","\'",$line) . "\\n';\n"; |
---|
| 60 | } |
---|
| 61 | ?> |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | <html> |
---|
| 65 | <head> |
---|
| 66 | <script type="text/javascript"> |
---|
| 67 | |
---|
| 68 | function setNewHtml() { |
---|
| 69 | var htRef = window.parent._editorRef.plugins['HtmlTidy']; |
---|
| 70 | htRef.instance.processTidied(tidyString()); |
---|
| 71 | } |
---|
| 72 | function tidyString() { |
---|
| 73 | var ns = '\n'; |
---|
| 74 | <?=$jsMakeSrc;?> |
---|
| 75 | return ns; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | </script> |
---|
| 79 | </head> |
---|
| 80 | |
---|
| 81 | <body id="htiNewBody" onload="setNewHtml()"> |
---|
| 82 | </body> |
---|
| 83 | </html> |
---|