<?php
/* @(#)$KimmoSuominen: .www/htdocs/sw/uricrumb/uricrumb.php,v 1.4 2011-10-02 14:19:19 kim Exp $
 *
 * Copyright (c) 2005 Kimmo Suominen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in the documentation and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
Plugin Name: URI Crumb
Plugin URI: http://kimmo.suominen.com/sw/uricrumb/
Description: Create a breadcrumb based on the requested URI.
Version: 0.4
Author: Kimmo Suominen
Author URI: http://kimmo.suominen.com/
*/

class URIBreadcrumb {
    var $pathname = array();

    function URIBreadcrumb()
    {
	$this->pathname['/'] = get_settings('blogname');
    }

    function get_breadcrumb($div = 'navi', $sep = '&raquo;', $page = 'page')
    {
	$title = wp_title('', false);
	echo '<div id="', $div, '">', "\n", "  <ul>\n", "    <li>";
	$d = explode('/', preg_replace('!/(index\.\w+)?$!', '',
	    array_shift(explode('?', $_SERVER['REQUEST_URI'], 2))));
	$n = count($d) - 1;
	$p = '/';
	if ($n > 0) {
	    echo '<a href="', $p, '">',
		isset($this->pathname[$p])
		? $this->pathname[$p]
		: 'root',
		"</a></li>\n    <li>", $sep, " ";
	    if (strcmp($d[$n - 1], 'page') == 0) {
		$navipath = array(ucfirst($page) . ' ' . $d[$n]);
		$n--;
	    }
	    for ($i=1; $i < $n; $i++) {
		$p .= $d[$i] . '/';
		echo '<a href="', $p, '">',
		    isset($this->pathname[$p])
		    ? $this->pathname[$p]
		    : ucfirst($d[$i]),
		    "</a></li>\n    <li>", $sep, " ";
	    }
	}
	if (is_array($navipath) && !empty($navipath)) {
	    $npl = array_pop($navipath);
	    foreach ($navipath as $np) {
		echo $np, "</li>\n    <li>", $sep, " ";
	    }
	    echo $npl;
	} else {
	    if ($n > 0) $p .= $d[$n] . '/';
	    echo
		isset($this->pathname[$p])
		? $this->pathname[$p]
		: (empty($title) ? ucfirst($d[$n]) : $title);
	}
	echo "</li>\n", "  </ul>\n", "</div><!-- ", $div, " -->\n\n";
    }
}

$wppURIBreadcrumb = new URIBreadcrumb;

function get_breadcrumb($div = 'navi', $sep = '&raquo;', $page = 'page')
{
    global $wppURIBreadcrumb;
    $wppURIBreadcrumb->get_breadcrumb($div, $sep, $page);
}

?>