78ae357ef23557440930d714b51f8f932f15d734
[atutor.git] / mods / wiki / fragments / 404finder.php
1 <?php
2 /*
3    This is an convinience wrapper around the ewiki PowerSearch page. It
4    should be used to catch 404 errors ("page not found") and let the
5    wiki try to find the correct page.
6
7    To set it up, link this wrapper (or move it) to the docroot or where your
8    ewiki wrapper script is located. Then edit the .htaccess and add
9    following (for Apache webservers):
10
11           ErrorDocument  404  404finder.php
12    
13    This wrapper should then call your ewiki wrapper/layout script, this is
14    what the $include variable is for:
15 */
16
17 $include = "example-1.php";
18
19
20 #-- try to guess the 'search string' from the requested URL path
21 if (isset($_REQUEST["id"])) {
22    # fine, do nothing
23 }
24 else {
25    #-- get CGI env var
26    ($url = $_SERVER["REDIRECT_URL"])
27    or
28    ($url = $_SERVER["PATH_INFO"])
29    or
30    ($url = $_SERVER["REQUEST_URI"]);
31
32 #echo "using $url";
33
34    #-- extract $_REQUEST, strip query_string
35    if ($p = strpos($url, "?")) {
36       if (empty($_GET)) {
37          parse_str(substr($url, $p), $_GET);
38          $_REQUEST = array_merge($_REQUEST, $_GET);
39       }
40       $url = substr($url, 0, $p);
41    }
42
43    #-- strip existing parts out of the URL
44    $pwd = getcwd();
45    chdir($_SERVER["DOCUMENT_ROOT"]);
46    $url = trim($url, "/");
47    $new_url = false;
48    $l = 0;
49    while ($l = strpos($url, "/", $l+1)) {
50 #echo ",$url;$l, ";
51       if (file_exists("./" . substr($url, 0, $l))) {
52 #echo "NURL";
53          $new_url = substr($url, $l+1);
54       }
55    }
56    if ($new_url) {
57       $url = $new_url;
58    }
59    chdir($pwd);
60
61 #echo " as '$url' ";
62
63    #-- now use it as search string
64    $_REQUEST["id"] = "PowerSearch";
65    $_REQUEST["where"] = "content";
66    $_REQUEST["q"] = strtr("$url", "/", " ");
67    unset($_SERVER["QUERY_STRING"]);
68 }
69
70 #print_r($_SERVER);
71 #print_r($_REQUEST);
72
73
74
75 foreach (array($include,"example-1.php","index.php") as $include) {
76    for ($subdir=0; $subdir<3; $subdir++) {
77       $dir = str_repeat("../",$subdir);
78       if (file_exists($dir.$include)) {
79          chdir($dir);
80          include($include);
81          break 2;
82       }
83    }
84 }
85
86 ?>