\n"); } // set information for navigation based on the current URL (file path) // and the navfiles in the filesystem. Returned array $levels will // contain each level of "navbar" information, starting with the top of // htdocs, down as far as we can go, along the path indicated by our URL. function where_am_i() { global $navfile, $maxrecurse; // import global $levels, $season; // export $subst_count = 1; // for use in string replacements $top = $_SERVER['DOCUMENT_ROOT']; // full path to top of site $file = $_SERVER['SCRIPT_FILENAME']; // full path to this page nav_debug("where_am_i file is '$file'"); // We're using % as a regexp delimiter below; abort if any in our path: if ( preg_match('/%/', $file ) ) { trigger_error("file path contains illegal char '%': '$file'"); return; } // strip top off our path, as well as remaining leading slash $rest = preg_replace("%^$top/?%",'',$file,$subst_count); // for comparison, strip off a trailing index.php, prepend slash: $our_location = preg_replace('%/index\.php$%','/',$rest,$subst_count); $our_location = "/$our_location"; nav_debug("where_am_i our location for comparison is '$our_location'"); // we'll walk down from the top $curdir_url = '/'; $curdir_path = $top; // recurse down our path, reading and processing navigation files, // until we run out of directories or we find one without a // navigation file. $recurse = 0; $season = 'unknown'; $levels = array(); while ( $recurse < $maxrecurse ) // expect to break out before then { $recurse++; // to avoid infinite loops, e.g. if symlinks up // figure out what the next level down will be; need it to check current $nextdir = preg_replace('%/.*%',"",$rest,$subst_count); // next component $rest = preg_replace("%^$nextdir/?%","",$rest,$subst_count); nav_debug("where_am_i ********** " ."under '$curdir_url' at dir '$nextdir' " ."with rest '$rest'"); // if $nextdir looks like a season and the season is not set, set it if ( ( $season == 'unknown' ) and preg_match('/^20\d\d(?:-20\d\d)?-[sfw]$/', $nextdir, $matches) ) { $season = $matches[0]; nav_debug("where_am_i set the season: '$season'"); } // grab the navfile data $navigation = array(); if ( file_exists("$curdir_path/$navfile") and is_readable("$curdir_path/$navfile") ) { include("$curdir_path/$navfile"); // sets array $navigation } else { nav_debug("where_am_i breaks, no navfile"); break; } if ( ! $navigation ) { nav_debug("where_am_i breaks, navfile did not set array"); break; } // okay, we have some navigation items for this level: $navitems = array(); // mark the one corresponding to us, and stack them into an array // for use by the navbar-generating code. foreach ( $navigation as $navitem ) { list( $is_active, $do_expand, $label, $item_url ) = $navitem; // if this is a foreign URL, cannot "canonify": if ( preg_match('%^https?:%', $item_url) ) { nav_debug("where_am_i: foreign URL '$item_url' cannot be current"); $is_current = false; $canon_url = $item_url; } else { // put the URL into a canonical form so we can check if it is "us": // CANON-1: if relative URLs, prepend current directory's URL, // being careful not to introduce a double leading slash $canon_url = $item_url; if ( ! preg_match('%^/%', $canon_url) ) { $canon_url = "$curdir_url/$canon_url"; $canon_url = preg_replace('%//%','/',$canon_url); } // CANON-2: remove any trailing slash $canon_url = preg_replace('%/$%','',$canon_url,$subst_count); // CANON-3: if the result is a directory then re-append slash if ( is_dir("$top$canon_url") ) { $canon_url = "$canon_url/"; } nav_debug("where_am_i checking item '$item_url' " ."canonical '$canon_url' " ."against location '$our_location'" ); // is this "us" so far? if ( preg_match("%^$canon_url%",$our_location) ) { $is_current = true; nav_debug("where_am_i: is CURRENT"); } else { $is_current = false; } } // stack this item onto the array for this level only if it is // marked as active *or* corresponds to the current URL: if ( $is_active or $is_current ) { array_push($navitems, array($is_current,$do_expand,$label,$canon_url) ); nav_debug("where_am_i: KEEPING '$canon_url' ('$label')"); } else { nav_debug("where_am_i: is DISCARDED"); } } array_push($levels, $navitems); // push this level // once we've processed all the items in the navfile, set up for // next directory down: if ( ! is_dir("$curdir_path/$nextdir") ) { nav_debug("where_am_i breaks, not a directory: '$curdir_path/$nextdir'"); break; } // ready for next iteration: $curdir_url = "$curdir_url/$nextdir"; $curdir_path = "$curdir_path/$nextdir"; } return; } function issue_nav_level($depth,$levels) { nav_debug("issue_nav_level called with depth '$depth'", $depth); print(str_repeat(' ', $depth)); // depth determines indentation if ( $depth == 0 ) { print(''."\n"); } nav_debug("issue_nav_level returned from depth '$depth'", $depth); } ?>