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

Last change on this file since 394 was 394, checked in by gogo, 14 years ago

Apply #501

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 2.2 KB
Line 
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
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 -utf8 -config html-tidy-config.cfg", $descriptorspec, $pipes);
21
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    /* Use tidy if it's available from PECL */
43    if( function_exists('tidy_parse_string') )
44    {
45      $tempsrc = tidy_parse_string($source);
46      tidy_clean_repair();
47      $newsrc = tidy_get_output();
48    }
49    else
50    {
51      // Better give them back what they came with, so they don't lose it all...
52      $newsrc = "<body>\n" .$source. "\n</body>";
53    }
54        }
55
56        // Split our source into an array by lines
57        $srcLines = explode("\n",$newsrc);
58
59        // Get only the lines between the body tags
60        $startLn = 0;
61        while ( strpos( $srcLines[$startLn++], '<body' ) === false && $startLn < sizeof($srcLines) );
62        $endLn = $startLn;
63        while ( strpos( $srcLines[$endLn++], '</body' ) === false && $endLn < sizeof($srcLines) );
64
65        $srcLines = array_slice( $srcLines, $startLn, ($endLn - $startLn - 1) );
66
67        // Create a set of javascript code to compile a new source string
68        foreach ($srcLines as $line) {
69                $jsMakeSrc .= "\tns += '" . str_replace("'","\'",$line) . "\\n';\n";
70        }
71if(!sizeof($srcLines)) {
72    echo "alert(HTMLArea._lc('Tidy failed.  Check your HTML for syntax errors.', 'HtmlTidy'));\n";
73} else {
74?>
75var ns="";
76<?php echo $jsMakeSrc; ?>
77editor.setHTML(ns);
78<? } ?>
Note: See TracBrowser for help on using the repository browser.