topical media & game development
alt-bot-lib-resolve-addresses.php / php
<?php
/*
#################################################################
Copyright 2007, Michael Schrenk
This software is designed for use with the book,
"Webbots, Spiders, and Screen Scarpers", Michael Schrenk, 2007 No Starch Press, San Francisco CA
W3CŪ SOFTWARE NOTICE AND LICENSE
This work (and included software, documentation such as READMEs, or other
related items) is being provided by the copyright holders under the following license.
By obtaining, using and/or copying this work, you (the licensee) agree that you have read,
understood, and will comply with the following terms and conditions.
Permission to copy, modify, and distribute this software and its documentation, with or
without modification, for any purpose and without fee or royalty is hereby granted, provided
that you include the following on ALL copies of the software and documentation or portions thereof,
including modifications:
1. The full text of this NOTICE in a location viewable to users of the redistributed
or derivative work.
2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions.
If none exist, the W3C Software Short Notice should be included (hypertext is preferred,
text is permitted) within the body of any redistributed or derivative code.
3. Notice of any changes or modifications to the files, including the date changes were made.
(We recommend you provide URIs to the location from which the code is derived.)
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT
OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the
software without specific, written prior permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
#################################################################
*/
#################################################################
#
# LIB_resolve_addresses
#
# This library provides routines that create fully resolved
# web addresses
#
#-----------------------------------------------------------------------
# FUNCTIONS
#
# resolve_address()
# This function returns fully resolved URLs for the
link,
link
which could be an images, css, javascript file, etc.
RETURNS:
A fully resolved URL for the
link,
link = trim(
page_base = trim(
page_base
page_base);
if( (strrpos(
page_base) )
page_base."/";
# remove unwanted characters from
link = str_replace(";", "",
link = str_replace("\"", "",
link = str_replace("'", "",
abs_address =
link;
abs_address);
abs_done==0)
{
# Use domain base address if
link, 0, 1) == "/")
{
// find the left_most "."
page_base, ".");
# Find the left-most "/" in
xx=
xx
<strlen(
xx++)
{
if( substr(
xx, 1)=="/")
break;
}
page_base);
domain_base_address.
abs_done=1;
}
}
#----------------------------------------------------------
# LOOK FOR REFERENCES TO HIGHER DIRECTORIES
#
if(
link, 0, 3) == "../")
{
page_base);
page_base, "/");
// remove slash if at end of
right_most_slash==strlen(
page_base = substr(
page_base)-1);
page_base, "/");
}
if (
unadjusted_base_address =
not_done=TRUE;
while(
page_base,
page_base,
link, 0, 3)!="../")
unadjusted_base_address))
unadjusted_base_address."/".
abs_address =
link;
abs_done==0)
{
if (substr(
link = substr(
link)-1); // remove leading "/"
page_base.
abs_done=1;
}
}
#----------------------------------------------------------
# LOOK FOR REFERENCES THAT ARE ALREADY ABSOLUTE
#
if(
link, 0, 4) == "http")
{
link;
abs_address, 0, 7)!="http://") && (substr(
abs_address = "http://".
abs_address;
}
*********************************************************************
get_base_page_address(url
RETURNS:
The base page address for
function get_base_page_address(url)
{
url, "/");
if (page_base = substr(slash_position+1); // "page_base = url is already the page base, without modification.
if(url))
page_base."/";
}
# If the page base ends with a \ replace with a \
page_base, strlen(last_two_characters=="//")
page_base, 0, strlen(page_base;
}
*********************************************************************
get_base_domain_address(page_base, (from get_base_page_address)
RETURNS:
The base page domain address for URL
*********************************************************************
function get_base_domain_address(pointer=8; page_base); page_base, domain_base=substr(pointer);
break;
}
}
page_base, strlen(last_two_characters=="//")
page_base, 0, strlen(domain_base;
}
*********************************************************************
move_address_back_one_level(object_source)
-------------------------------------------------------------
DESCRIPTION:
This function is used by the library and not intended for
external use.
-------------------------------------------------------------
function move_address_back_one_level(object_source)
{
// bring page base back one leve
page_base, "/");
page_base, 0, object_source = substr(object_source)-3);
new_page_base;
object_source;
return
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.