Source for file mapper.php
Documentation is available at mapper.php
* dbscript for PHP 4 & 5 - restful crud framework
* @version 0.1.2 -- 19-Feb-2007
* @author Brian Hendrickson <brian@dbscript.net>
* @link http://dbscript.net/
* @copyright Copyright 2007 Brian Hendrickson
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* connects the current URI to a Route,
* establishing the request variable names
* e.g. my_domain/:resource/:id would map
* values into $req->resource and $req->id
* {@link http://dbscript.net/mapper}
* @author Brian Hendrickson <brian@dbscript.net>
* unmolested regex parts of the URI
* URI parameter names and values
* list of connected Route objects
* parameters to (silently) propagate
* id of encrypted Cookie owner
* true if an error has been raised
* contents of error message
* database Record object for the current session
preg_match( "/^(https?:\/\/)([^\/]+)\/?[^\?]+?[\??]([\w\/\.]+)?/i", $this->uri, $this->values );
if ( isset ( $this->values[3] ) ) {
$this->params['error'] .= $errstr . "\n";
function url_for( $params, $altparams = NULL ) {
// first var is a route name
foreach ( $this->routes as $r ) {
foreach ( $r->patterns as $pos => $str ) {
if ( substr( $str, 0, 1 ) == ':' ) {
$vars[substr( $str, 1 )] = $pos;
if ( isset ( $routename ) ) {
if ( $routename == $r->name ) {
// a named route was found
return $r->build_url( $params, $this->base );
// every pattern in the route exists in the requested params
return $r->build_url( $params, $this->base );
foreach ( $this->params as $paramkey=> $paramval ) {
$params[$paramkey] = $paramval;
foreach ( $this->routes as $r ) {
foreach ( $r->patterns as $pos => $str ) {
if ( substr( $str, 0, 1 ) == ':' ) {
$vars[substr( $str, 1 )] = $pos;
return $r->build_url( $params, $this->base );
function link_to( $params, $altparams = NULL ) {
$url = $this->url_for( $params, $altparams );
return "<a href=\"$url\">$url</a>";
$controller = $this->params['resource'];
$links[] = '<a href="'. $this->base . '">Contents</a>';
if ( isset ( $this->resource ) && ( $this->resource != 'introspection' ))
$links[] = '<a href="'. $this->base . '?'. $this->resource. '">'. ucwords($this->resource). '</a>';
if ( ($this->id != 0) && isset ( $this->resource ) && ( $this->resource != 'introspection' ))
$links[] = '<a href="'. $this->entry_url($this->id). '">Entry '. ucwords($this->id). '</a>';
elseif ( isset ( $this->resource ) && $this->new_url())
$links[] = '<a href="'. $this->new_url(). '">New '. classify($this->resource). '</a>';
foreach ($links as $key=> $val) {
function set_filter( $name, $func, $when = 'after' ) {
$this->params[$param] = $value;
if (!isset ($this->$param))
$this->$param = & $this->params[$param];
if (isset ($this->resource))
return $this->url_for( array('resource'=> $this->resource, 'action'=> 'index.atom'));
if (isset ($this->resource))
if ($result && ($id != NULL))
return $this->url_for( array('resource'=> $this->resource, 'action'=> 'entry', 'id'=> $id));
return $this->url_for( array('resource'=> $this->resource, 'action'=> 'entry'));
if (isset ($this->resource))
return $this->url_for( array('resource'=> $this->resource, 'action'=> 'new'));
if (isset ($this->params['resource']))
$resource = $this->params['resource'] . DIRECTORY_SEPARATOR;
$template = $this->params['action'];
$template = "_" . $template;
if (isset ($this->params['client_wants']))
$ext = $this->params['client_wants'];
$view = $this->template_path . $resource . $template . "." . $ext;
if (!$partial && !(is_file($view)))
if (!$partial && !(is_file($view)))
// Create and connect a new Route to the Mapper
foreach ( $args as $idx => $arg ) {
$r->patterns = explode( '/', $arg );
if ( count( $r->patterns ) == 1 && $idx == 0 ) {
$r->name = $r->patterns[0];
foreach ( $arg as $key => $val ) {
if ( $key == 'requirements' ) {
foreach ( $r->patterns as $pos => $str ) {
if ( substr( $str, 0, 1 ) == ':' ) {
$r->requirements[$pos] = $val[$i];
$r->defaults[$key] = $val;
function generate( $controller= 'index.php', $action= 'get' ) {
// Generate a route from a set of keywords and return the url
// Match a URL against against one of the routes contained.
if ($url === NULL) $url = $this->uri;
foreach ( $this->routes as $route ) {
if ($this->match( $url, $route )) {
if ( isset ( $this->params['method'] ) ) $this->action = $this->method;
if ( isset ( $this->params['forward_to'] ) ) $this->controller = $this->forward_to;
if (!(strpos($this->action,".") === false)) { // check for period
$actionsplit = split("\.", $this->action);
$this->set_param( 'action', $actionsplit[0]);
$this->set_param( 'client_wants', $actionsplit[1] );
#print $this->resource; exit;
if (isset ($this->resource)) {
if (!(strpos($this->resource,".") === false)) { // check for period
$actionsplit = split("\.", $this->resource);
$this->set_param( 'resource', $actionsplit[0]);
$this->set_param( 'client_wants', $actionsplit[1] );
function match( $url, $r ) {
foreach ( $r->patterns as $idx => $value ) {
if ( !( isset ( $this->params[$idx] ) ) ) {
foreach ( $r->patterns as $pos => $str ) {
if ( substr( $str, 0, 1 ) == ':' ) {
if ( isset ( $r->requirements[$pos] ) ) {
$regx[] = $r->requirements[$pos];
#print "searching for ". "/\/" . implode( "\/", $regx ) . "/i". " in the string /" .implode( "/", $params )."<br><br>";
if ( count( $r->patterns ) == 0 ) {
foreach ( $this->params as $p=> $v ) {
if ( !( isset ( $this->$p ) ) )
$this->$p = & $this->params[$p];
foreach ( $r->patterns as $idx => $val ) {
if ( substr( $val, 0, 1 ) == ':' ) {
if ( isset ( $params[$idx] ) ) $this->params[$val] = $params[$idx];
// cross platform URI code by Angsuman Chakraborty
if ( isset ( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS']== 'on' ) {
$_SERVER['FULL_URL'] = 'https://';
if ( $_SERVER['SERVER_PORT']!= '443' ) {
$port = ':' . $_SERVER['SERVER_PORT'];
$_SERVER['FULL_URL'] = 'http://';
if ( $_SERVER['SERVER_PORT']!= '80' ) {
$port = ':' . $_SERVER['SERVER_PORT'];
if ( isset ( $_SERVER['REQUEST_URI'] ) ) {
$script = $_SERVER['REQUEST_URI'];
$script = $_SERVER['PHP_SELF'];
if ( $_SERVER['QUERY_STRING']> ' ' ) {
$script .= '?'. $_SERVER['QUERY_STRING'];
if ( isset ( $_SERVER['HTTP_HOST'] ) ) {
$_SERVER['FULL_URL'] .= $_SERVER['HTTP_HOST'] . $port . $script;
$_SERVER['FULL_URL'] .= $_SERVER['SERVER_NAME'] . $port . $script;
return $_SERVER['FULL_URL'];
if ( $this->error === true )
$_SESSION['params'] = array();
foreach( $this->params as $param=> $val ) {
$_SESSION['params'][$param] = $val;
if (!(isset ($_SESSION['params']))) return false;
foreach( $_SESSION['params'] as $param=> $val ) {
$this->$param = & $this->params[$param];
|