source: trunk/plugins/HtmlTidy/html-tidy-logic.php @ 30

Last change on this file since 30 was 30, checked in by gogo, 15 years ago

Apply patch from ticket #52 to allow safe mode operation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 2.2 KB
Line 
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        );
20        $process = proc_open("tidy -config html-tidy-config.cfg", $descriptorspec, $pipes);
21
22        // Make sure the program started and we got the hooks...
23        // Either way, get some source code into $source
24        if (is_resource($process)) {
25
26                // Feed untidy source into the stdin
27                fwrite($pipes[0], $source);
28                fclose($pipes[0]);
29
30                // Read clean source out to the browser
31                while (!feof($pipes[1])) {
32                        //echo fgets($pipes[1], 1024);
33                        $newsrc .= fgets($pipes[1], 1024);
34                }
35                fclose($pipes[1]);
36
37                // Clean up after ourselves
38                proc_close($process);
39
40        } else {
41                // Better give them back what they came with, so they don't lose it all...
42                $newsrc = "<body>\n" .$source. "\n</body>";
43        }
44
45        // Split our source into an array by lines
46        $srcLines = explode("\n",$newsrc);
47
48        // Get only the lines between the body tags
49        $startLn = 0;
50        while ( strpos( $srcLines[$startLn++], '<body' ) === false && $startLn < sizeof($srcLines) );
51        $endLn = $startLn;
52        while ( strpos( $srcLines[$endLn++], '</body' ) === false && $endLn < sizeof($srcLines) );
53
54        $srcLines = array_slice( $srcLines, $startLn, ($endLn - $startLn - 1) );
55
56        // Create a set of javascript code to compile a new source string
57        foreach ($srcLines as $line) {
58                $jsMakeSrc .= "\tns += '" . str_replace("'","\'",$line) . "\\n';\n";
59        }
60?>
61
62
63<html>
64  <head>
65    <script type="text/javascript">
66
67function setNewHtml() {
68        var htRef = window.parent._editorRef.plugins['HtmlTidy'];
69        htRef.instance.processTidied(tidyString());
70}
71function tidyString() {
72        var ns = '\n';
73        <?=$jsMakeSrc;?>
74        return ns;
75}
76
77    </script>
78  </head>
79
80  <body id="htiNewBody" onload="setNewHtml()">
81  </body>
82</html>
Note: See TracBrowser for help on using the repository browser.