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

Last change on this file since 289 was 289, checked in by niko, 14 years ago

fixed little bug introduced in changeset:270 (#412)

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 1.9 KB
RevLine 
[270]1<?php
2##
3##  Plugin for htmlArea, to run code through the server's HTML Tidy
4##   By Adam Wright, for The University of Western Australia
[1]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"),
[30]18                1 => array("pipe", "w")
[1]19        );
[40]20        $process = proc_open("tidy -utf8 -config html-tidy-config.cfg", $descriptorspec, $pipes);
[1]21
[40]22
[1]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?>
[40]62var ns="";
[289]63<?php echo $jsMakeSrc; ?>
[40]64editor.setHTML(ns);
Note: See TracBrowser for help on using the repository browser.