ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
index.php
Go to the documentation of this file.
1 <?php
2 /*
3  +----------------------------------------------------------------------+
4  | HTML/XML Validator |
5  +----------------------------------------------------------------------+
6  | Copyright (c) 2004-2005 Nuno Lopes |
7  +----------------------------------------------------------------------+
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU Lesser General Public |
10  | License as published by the Free Software Foundation; either |
11  | version 2.1 of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16  | Lesser General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU Lesser General Public |
19  | License along with this library; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
21  | 02111-1307 USA. |
22  +----------------------------------------------------------------------+
23  | http://validator.aborla.net/ |
24  +----------------------------------------------------------------------+
25 
26 vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
27 
28  $Id: index.php 8377 2005-08-15 09:27:49Z akill $
29 */
30 
31 require './include.inc';
32 require './validator.inc';
33 
34 // while php 5.1 isn't released we must keep compatibility with php 5.0
35 if (!defined('UPLOAD_ERR_CANT_WRITE'))
36  define('UPLOAD_ERR_CANT_WRITE', 7);
37 
38 
39 /* check if the client browser sent a language */
40 function valid_http_lang() {
41  global $valid_langs;
42 
43  if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
44  return false;
45 
46  $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
47  foreach ($langs as $lang) {
48  list($lang) = explode(';', $lang);
49  list($lang) = explode('-', $lang);
50  $lang = trim($lang);
51 
52  if (isset($valid_langs[$lang]))
53  return $lang;
54  }
55 
56  return false;
57 }
58 
59 
60 /*
61  Mozilla, Firefox and friends have an error that lock radio buttons.
62  We use this to disable some feature for these browsers
63  More info at: http://bugzilla.mozilla.org/show_bug.cgi?id=229925
64 */
65 define('MOZILLA', (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko/') !== false));
66 
67 function mozilla($txt) {
68  return MOZILLA ? '' : $txt;
69 }
70 
71 
72 /* the default 'variables_order' isn't good for us. As we can't change it
73  at runtime, we do this little 'hack' to give _GET precendence */
74 if (isset($_GET['lang'])) {
75  $_REQUEST['lang'] = &$_GET['lang'];
76 } elseif (isset($_POST['lang'])) {
77  $_REQUEST['lang'] = &$_POST['lang'];
78 }
79 
80 
81 /* Check for valid language and include the local file */
82 if (!isset($_GET['langchooser']) &&
83  (
84  (isset($_REQUEST['lang']) && isset($valid_langs[(string)$_REQUEST['lang']])) ||
85  ($lang = valid_http_lang()) ||
86  !empty($_REQUEST['url'])
87  )
88  ) {
89 
90  if (empty($lang)) {
91  $lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'en';
92  } else {
93  $redir = true;
94  }
95 
96  if (empty($_COOKIE['lang']) || $lang != $_COOKIE['lang']) {
97  setcookie('lang', $lang, time()+60*60*24*90, '/', 'validator.aborla.net');
98  }
99 
100  output_add_rewrite_var('lang', $lang);
101 
102  require './local/en.inc'; // fallback for not translated messages
103 
104  if ($lang != 'en')
105  require "./local/$lang.inc"; //load localized messages
106 
107  common_header();
108 
109  if (isset($redir)) {
110  echo '<p id="redir">We have automatically choosen the "' . $valid_langs[$lang] .
111  '" language for you. <a href="/?langchooser">Click here to change</a>.</p>';
112  }
113 
114 
115 /*************** LANGUAGE CHOOSER ***************/
116 } else {
117  require "./local/en.inc";
118  common_header();
119 
120  echo "<p>Choose a language, please:</p>\n" .
121  "<form method=\"get\" action=\"index.php\">\n" .
122  "<p><select name=\"lang\">\n";
123 
124  foreach($valid_langs as $k => $v) {
125  echo "<option value=\"$k\">$v</option>\n";
126  }
127 
128  echo "</select></p>\n<p><input type=\"submit\" value=\" Submit \" /></p>\n</form>\n";
129 
130  die(common_footer());
131 }
132 /*********** END OF LANGUAGE CHOOSER ************/
133 
134 
135 
136 /**************** DOCUMENTATION ****************/
137 
138 if (isset($_GET['docs'])) {
139  // docs
140  echo "<h2>$message[5]</h2>\n".
141  "<p>$message[99]</p><p>&nbsp;</p>\n";
142 
143  // explanation of each option
144  echo "<h2>$message[0]</h2>";
145  foreach($opts as $v) {
146  echo "<p><strong>$v[name]</strong>: $v[explain]</p>\n";
147  }
148 
149  die(common_footer());
150 }
151 
152 /***************** END OF DOCS ****************/
153 
154 
155 
156 /***************** FIRST PAGE *****************/
157 
158 if (empty($_REQUEST['url']) && (!isset($_FILES['file']) || $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE)) {
159 
160  echo '<form method="post" action="index.php" enctype="multipart/form-data"><p>' . $message[3].
161  '<input type="hidden" name="MAX_FILE_SIZE" value="' . validator::FILE_LIMIT . '" />' .
162  ': <input type="text" name="url" size="50" value="http://" /><br />'.
163  $message[19] . ': <input name="file" type="file" /></p>';
164 
165  $aditional = "$message[8]:\n" . mozilla("<label title=\"$message[1]\">\n") .
166  "<input type=\"radio\" name=\"errors\" checked=\"checked\" value=\"along\" /> $message[6]\n" .
167  "<input type=\"radio\" name=\"errors\" value=\"alone\" /> $message[7]\n" .
168  mozilla("</label>\n") .
169 
170  "<br />$message[9]:\n" . mozilla("<label title=\"$message[2]\">\n") .
171  "<input type=\"radio\" name=\"repair\" checked=\"checked\" value=\"full\" /> $message[10]\n" .
172  "<input type=\"radio\" name=\"repair\" value=\"diff\" /> $message[11]\n" .
173  "<input type=\"radio\" name=\"repair\" value=\"none\" /> $message[12]\n" .
174  mozilla("</label>\n") . "<br />\n";
175 
176 
177  echo validator::html_opts($aditional);
178  echo <<< HTML
179 
180 <p><input type="submit" value=" $message[4] " /></p>
181 </form>
182 <p><a href="index.php?docs">$message[5]</a></p>
183 
184 HTML;
185 
186  die(common_footer());
187 }
188 /************ END OF FIRST PAGE **************/
189 
190 
191 
192 /****************** START OF THE VALIDATOR ITSELF *******************/
193 
194 $validator = new validator($_REQUEST);
195 
196 
197 /* File upload */
198 if(isset($_FILES['file']) && $_FILES['file']['error'] != UPLOAD_ERR_NO_FILE) {
199 
200  switch($_FILES['file']['error']) {
201  case UPLOAD_ERR_OK:
202  $result = $validator->parse_string(file_get_contents($_FILES['file']['tmp_name']));
203  break;
204 
205  case UPLOAD_ERR_INI_SIZE:
206  case UPLOAD_ERR_FORM_SIZE:
207  $result = false;
208  $int_error = $validator->_error(8, validator::FILE_LIMIT);
209  break;
210 
211  case UPLOAD_ERR_PARTIAL:
212  case UPLOAD_ERR_NO_TMP_DIR:
213  case UPLOAD_ERR_CANT_WRITE:
214  $result = false;
215  $int_error = $validator->_error(9);
216  break;
217  }
218 
219 /* URL validator */
220 } else {
221  $result = $validator->parse_url((string)$_REQUEST['url']);
222 }
223 
224 
225 /* no fatal errors. continue */
226 if ($result){
227 
228  if ($validator->internal_error()) {
229  echo "<p>$message[13]:<br />\n";
230  echo nl2br($validator->internal_errors_string()) . "</p>\n";
231  }
232 
233  echo '<p>';
234 
235  /* file upload */
236  if(isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
237  echo isset($_FILES['file']['name']) ? "<strong>$message[18]</strong>: {$_FILES['file']['name']}<br />\n" : '';
238 
239  /* URL validator */
240  } else {
241  echo '<strong>URL</strong>: ' . htmlspecialchars($_REQUEST['url'], ENT_QUOTES, 'UTF-8') . "<br />\n";
242  }
243 
244  /* If the document contains too many errors, the version may not be found */
245  if ($detected_version = $validator->detected_version()) {
246  echo "<strong>$message[14]</strong>: $detected_version<br />\n";
247  }
248 
249  // enconding in use
250  echo "<strong>$message[20]</strong>: {$validator->readable_charset()}";
251 
252  // language
253  if ($validator->lang)
254  echo "<br />\n<strong>$message[24]</strong>: {$langs[$validator->lang]}";
255 
256 
257  /* Show HTML/XML errors */
258  if ($errors = $validator->error()) {
259 
260  echo "<br />\n<strong>$message[21]</strong>: $errors</p>\n".
261  "<h2>$message[16]</h2>\n";
262 
263  if (empty($_REQUEST['errors']) || $_REQUEST['errors'] == 'along') {
264  echo '<code>' . $validator->errors_source() . "</code>\n\n";
265 
266  } else { //errors alone
267  echo '<p>' . $validator->errors_string() . "</p>\n\n";
268  }
269 
270  /* no errors found in the document */
271  } else {
272  echo "</p><p>$message[15]</p>\n";
273  }
274 
275 
276  /* show repaired document */
277  if (!isset($_REQUEST['repair']) || $_REQUEST['repair'] == 'full') {
278  $html = strtr(nl2br(htmlspecialchars($validator->repaired_source(), ENT_QUOTES, 'UTF-8')), array(' ' => ' &nbsp;'));
279 
280  echo "\n<p>&nbsp;</p>\n<h2>$message[17]</h2>\n";
281  echo '<div class="code"><code>' . $html . "</code></div>\n";
282 
283  /* Diff */
284  } elseif ($_REQUEST['repair'] == 'diff' && $diff = $validator->repaired_diff()) {
285  $diff = strtr($diff, array(' ' => ' &nbsp;'));
286 
287  echo "\n<p>&nbsp;</p>\n<h2>$message[11]</h2>\n";
288  echo '<div class="code"><code>' . $diff . "</code></div>\n";
289  }
290 
291 
292 /* there was some error handling the URL/file upload */
293 } else {
294  echo "<p>$message[13]:<br />\n";
295  echo nl2br($validator->internal_errors_string()) . "</p>\n";
296 }
297 
298 
299 if (isset($_GET['dump_debug'])) {
300  echo '<pre>' . htmlspecialchars($validator->debug()) . '</pre>';
301 }
302 
303 
304 common_footer();
305 ?>