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

Last change on this file since 410 was 410, checked in by gogo, 8 years ago

Apply parts of #417 - namely add cwd and regexp splitting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 2.3 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  $cwd = str_replace("\\","/",getcwd())."/";
15 
16        // Open a tidy process - I hope it's installed!
17        $descriptorspec = array(
18                0 => array("pipe", "r"),
19                1 => array("pipe", "w")
20        );
21        $process = @proc_open("tidy -utf8 -config {$cwd}html-tidy-config.cfg", $descriptorspec, $pipes);
22
23
24        // Make sure the program started and we got the hooks...
25        // Either way, get some source code into $source
26        if (is_resource($process)) {
27
28                // Feed untidy source into the stdin
29                fwrite($pipes[0], $source);
30                fclose($pipes[0]);
31
32                // Read clean source out to the browser
33                while (!feof($pipes[1])) {
34                        //echo fgets($pipes[1], 1024);
35                        $newsrc .= fgets($pipes[1], 1024);
36                }
37                fclose($pipes[1]);
38
39                // Clean up after ourselves
40                proc_close($process);
41
42        } else {
43    /* Use tidy if it's available from PECL */
44    if( function_exists('tidy_parse_string') )
45    {
46      $tempsrc = tidy_parse_string($source);
47      tidy_clean_repair();
48      $newsrc = tidy_get_output();
49    }
50    else
51    {
52      // Better give them back what they came with, so they don't lose it all...
53      $newsrc = "<body>\n" .$source. "\n</body>";
54    }
55        }
56
57        // Split our source into an array by lines
58        $srcLines = preg_split("/\n/",$newsrc,-1,PREG_SPLIT_NO_EMPTY);
59
60        // Get only the lines between the body tags
61        $startLn = 0;
62        while ( strpos( $srcLines[$startLn++], '<body' ) === false && $startLn < sizeof($srcLines) );
63        $endLn = $startLn;
64        while ( strpos( $srcLines[$endLn++], '</body' ) === false && $endLn < sizeof($srcLines) );
65
66        $srcLines = array_slice( $srcLines, $startLn, ($endLn - $startLn - 1) );
67
68        // Create a set of javascript code to compile a new source string
69        foreach ($srcLines as $line) {
70                $jsMakeSrc .= "\tns += '" . str_replace("'","\'",$line) . "\\n';\n";
71        }
72if(!sizeof($srcLines)) {
73    echo "alert(HTMLArea._lc('Tidy failed.  Check your HTML for syntax errors.', 'HtmlTidy'));\n";
74} else {
75?>
76var ns="";
77<?php echo $jsMakeSrc; ?>
78editor.setHTML(ns);
79<? } ?>
Note: See TracBrowser for help on using the repository browser.