source: trunk/plugins/SuperClean/tidy.php @ 1

Last change on this file since 1 was 1, checked in by xinha, 15 years ago

Bootstrap

File size: 4.0 KB
Line 
1<?php
2  /** This PHP file is intended for use with XMLHTTPRequest from an HTMLArea
3   * it requrns javascript to set the htmlarea html with tidied html that is
4   * submitted in a $_POST parameter called 'content'
5   */
6
7  if(get_magic_quotes_gpc())
8  {
9    // trigger_error('Magic Quotes GPC is on, cleaning GPC.', E_USER_NOTICE);
10    $to_clean = array(&$_GET, &$_POST, &$_REQUEST, &$_COOKIE);
11    while(count($to_clean))
12    {
13      $cleaning =& $to_clean[array_pop(array_keys($to_clean))];
14      unset($to_clean[array_pop(array_keys($to_clean))]);
15      foreach(array_keys($cleaning) as $k)
16      {
17        if(is_array($cleaning[$k]))
18        {
19          $to_clean[] =& $cleaning[$k];
20        }
21        else
22        {
23          $cleaning[$k] = stripslashes($cleaning[$k]);
24        }
25      }
26    }
27  }
28
29  /** Function to POST some data to a URL */
30  function PostIt($DataStream, $URL)
31  {
32
33//  Strip http:// from the URL if present
34    $URL = ereg_replace("^http://", "", $URL);
35
36//  Separate into Host and URI
37    $Host = substr($URL, 0, strpos($URL, "/"));
38    $URI = strstr($URL, "/");
39
40//  Form up the request body
41    $ReqBody = "";
42    while (list($key, $val) = each($DataStream)) {
43      if ($ReqBody) $ReqBody.= "&";
44      $ReqBody.= $key."=".urlencode($val);
45    }
46    $ContentLength = strlen($ReqBody);
47
48//  Generate the request header
49    $ReqHeader =
50      "POST $URI HTTP/1.0\n".
51      "Host: $Host\n".
52      "User-Agent: PostIt\n".
53      "Content-Type: application/x-www-form-urlencoded\n".
54      "Content-Length: $ContentLength\n\n".
55      "$ReqBody\n";
56
57//     echo $ReqHeader;
58
59
60//  Open the connection to the host
61    $socket = fsockopen($Host, 80, &$errno, &$errstr);
62    if (!$socket) {
63      $result = "($errno) $errstr";
64      return $Result;
65    }
66
67    fputs($socket, $ReqHeader);
68
69    $result = '';
70    while(!feof($socket))
71    {
72      $result .= fgets($socket);
73    }
74    return $result;
75  }
76
77
78  function js_encode($string)
79  {
80    static $strings = "\\,\",',%,&,<,>,{,},@,\n,\r";
81
82    if(!is_array($strings))
83    {
84      $tr = array();
85      foreach(explode(',', $strings) as $chr)
86      {
87        $tr[$chr] = sprintf('\x%02X', ord($chr));
88      }
89      $strings = $tr;
90    }
91
92    return strtr($string, $strings);
93  }
94
95  // Any errors would screq up our javascript
96  error_reporting(E_NONE);
97  ini_set('display_errors', false);
98
99  if(trim(@$_REQUEST['content']))
100  {
101    ob_start();
102      passthru("echo " .  escapeshellarg($_REQUEST['content']) . " | tidy -q -i -wrap 9999 2>/dev/null", $result);
103      $content = ob_get_contents();
104    ob_end_clean();
105
106    if(!strlen($content))
107    {
108      // Tidy on the local machine failed, try a post
109      $res_1
110        = PostIt(
111          array
112            (
113              '_function' => 'tidy',
114              '_html'   => $_REQUEST['content'],
115              'char-encoding' => 'utf8',
116              '_output'       => 'warn',
117              'indent'        => 'auto',
118              'wrap'          => 9999,
119              'break-before-br' => 'y',
120              'bare'          => 'n',
121              'word-2000'     => 'n',
122              'drop-empty-paras' => 'y',
123              'drop-font-tags' => 'n',
124
125            ),
126          'http://infohound.net/tidy/tidy.pl');
127
128      if(preg_match('/<a href="([^"]+)" title="Save the tidied HTML/', $res_1, $m))
129      {
130        $tgt = strtr($m[1], array_flip(get_html_translation_table(HTML_ENTITIES)));
131        $content = implode('', file('http://infohound.net/tidy/' . $tgt));
132      }
133    }
134
135    if(strlen($content) && ! preg_match('/<\/body>/i', $_REQUEST['content']))
136    {
137      if( preg_match('/<body[^>]*>(.*)<\/body>/is', $content, $matches) )
138      {
139        $content = $matches[1];
140      }
141    }
142    elseif(!strlen($content))
143    {
144      $content = $_REQUEST['content'];
145    }
146
147    if($content)
148    {
149      ?>
150      editor.setHTML('<?php echo js_encode($content) ?>');
151      <?php
152    }
153    else
154    {
155      ?>
156      alert(SuperClean.I18N.failed);
157      <?php
158    }
159  }
160  else
161  {
162    ?>
163    alert(SuperClean.I18N.nothingtodo);
164    <?php
165  }
166
167?>
Note: See TracBrowser for help on using the repository browser.