<<

. 117
( 132 .)



>>

$domain = preg_replace(˜/^\@/™,™™,$domain);
if (!is_domain($domain)) { return false; }
$laddr = $addr_spec;
}
return(is_addr_spec($laddr));
}

function is_addr_spec ( $eaddr = “” )
{
list($local_part,$domain) = preg_split(˜/\@/™,$eaddr);
if (!is_local_part($local_part) || !is_domain($domain))
{
#print “[$eaddr] is not valid\n”;
return false;
}
else
{
#print “[$eaddr] is valid\n”;
return true;
}
}

#local-part = word *(“.” word) ; uninterpreted
function is_local_part ( $local_part = “” )
{
if (empty($local_part)) { return false; }

$bit_array = preg_split(˜/\./™,$local_part);
686 Part V: Appendixes

while (list(,$bit) = each($bit_array))
{
if (!is_word($bit)) { return false; }
}
return true;
}

#word = atom / quoted-string
#quoted-string = <”> *(qtext/quoted-pair) <”>; Regular qtext or
# ; quoted chars.
#qtext = <any CHAR excepting <”>, ; => may be folded
# “\” & CR, and including
# linear-white-space>
#quoted-pair = “\” CHAR ; may quote any char
function is_word ( $word = “”)
{

if (preg_match(˜/^”.*”$/i™,$word))
{
return(is_quoted_string($word));
}
return(is_atom($word));
}

function is_quoted_string ( $word = “”)
{
$word = preg_replace(˜/^”/™,™™,$word); # remove leading quote
$word = preg_replace(˜/”$/™,™™,$word); # remove trailing
quote
$word = preg_replace(˜/\\+/™,™™,$word); # remove any quoted-
pairs
if (preg_match(˜/\”\\\r/™,$word)) # if “, \ or CR, it™s bad
qtext
{
return false;
}
return true;
}



#atom = 1*<any CHAR except specials, SPACE and CTLs>
#specials = “(“ / “)” / “<” / “>” / “@” ; Must be in quoted-
# / “,” / “;” / “:” / “\” / <”> ; string, to use
# / “.” / “[“ / “]” ; within a word.
#SPACE = <ASCII SP, space> ;( 40, 32.)
Appendix H: Helpful User-Defined Functions 687

#CTL = <any ASCII control ;( 0- 37, 0.- 31.)
# character and DEL> ;( 177, 127.)
function is_atom ( $atom = “”)
{

if (
(preg_match(˜/[\(\)\<\>\@\,\;\:\\\”\.\[\]]/™,$atom)) #
specials
|| (preg_match(˜/\040/™,$atom)) # SPACE
|| (preg_match(˜/[\x00-\x1F]/™,$atom)) # CTLs
)
{
return false;
}
return true;
}

#domain = sub-domain *(“.” sub-domain)
#sub-domain = domain-ref / domain-literal
#domain-ref = atom ; symbolic reference
function is_domain ( $domain = “”)
{

if (empty($domain)) { return false; }

# this is not strictly required, but is 99% likely sign of a bad
domain
if (!preg_match(˜/\./™,$domain)) { return false; }

$dbit_array = preg_split(˜/./™,$domain);
while (list(,$dbit) = each($dbit_array))
{
if (!is_sub_domain($dbit)) { return false; }
}
return true;
}
function is_sub_domain ( $subd = “”)
{
if (preg_match(˜/^\[.*\]$/™,$subd)) #domain-literal
{
return(is_domain_literal($subd));
}
return(is_atom($subd));
}
#domain-literal = “[“ *(dtext / quoted-pair) “]”
688 Part V: Appendixes

#dtext = <any CHAR excluding “[“, ; => may be folded
# “]”, “\” & CR, & including
# linear-white-space>
#quoted-pair = “\” CHAR ; may quote any char
function is_domain_literal ( $dom = “”)
{
$dom = preg_replace(˜/\\+/™,™™,$dom); # remove quoted
pairs
if (preg_match(˜/[\[\]\\\r]/™,$dom)) # bad dtext characters
{
return false;
}
return true;
}

?>

You would probably want to put all of these functions in one file and then
include it when needed. It returns 1 (for true) or nothing (for false). You™d probably
want to use it like so:

if ( !validate_email(“myaddress@mydomain.com”) )
{
echo “this is not a valid email”;
}




sitemap.php
We wrote the following code to help us take a look at all the documents installed on
the Web server. It prints every document and provides a link to these documents.

<?php include(˜book.php™); ?>
<b><a href=”.”>back to directory</a></b>
<h2>Site Map</h2>
<?php
function printdir($dir=™.™,$path=NULL, $print_ok=FALSE)
{
if ($path === NULL)
$path = $dir;
$pursue = TRUE;
$old_print_ok = $print_ok;
if ($print_ok)
{
// np
Appendix H: Helpful User-Defined Functions 689

}
elseif (strpos($path, BOOK_ROOT) === FALSE)
{
if (strpos($path, DSN_ROOT) === FALSE)
{
if (strpos(BOOK_ROOT, $path) === FALSE
&& strpos(DSN_ROOT, $path) === FALSE
)
{
$pursue = FALSE;
}
}
else
{
$print_ok = TRUE;
}
}
else
{
$print_ok = TRUE;
}
$printdir = $dir;
if ($print_ok && !$old_print_ok)
$printdir = $path;
$url = NULL;
if (strpos($path, DOC_ROOT) !== FALSE)
{
$url = str_replace(DOC_ROOT, ˜™, $path);
$printdir = “<a href=\”$url\”>$printdir</a>”;
}
if ($dh = opendir($path))
{
if ($print_ok)
print “<li>$printdir/\n<ul>\n”;
while (($file = readdir($dh)) !== FALSE)
{
if ($file == ˜.™ or $file == ˜..™ or $file == ˜CVS™ or
substr($file,-4) == ˜.swp™)
continue;

$wholefile = “{$path}/{$file}”;
if ($url)
if (substr($file,-4) == ˜.php™)
$printfile = ˜<a href=”™
. BOOK_URL_ROOT
690 Part V: Appendixes

. ˜/source™
. str_replace(BOOK_URL_ROOT, ˜/book/™,
“{$url}/{$file}”)
. ˜“>™
. $file
. ˜</a>™
;
else
$printfile = “<a
href=\”{$url}/{$file}\”>$file</a>”;
else
$printfile = $file;

if (is_link($wholefile))
{
if ($print_ok)
print “<li>@{$printfile}\n”;
}
elseif (is_dir($wholefile))
{
if ($pursue)
printdir($file, $wholefile, $print_ok);
}
elseif ($print_ok)
{
print “<li>$printfile\n”;
}
}
closedir($dh);
if ($print_ok)
print “</ul>\n”;
}
else
{
print “<li>could not open ˜$dir™\n”;
}
}
printdir(˜/usr/local/book/apache™);
?>
Appendix I

PHP and MySQL Resources
THIS APPENDIX PRESENTS SOME resources that should be extremely useful in increas-
ing your knowledge of both PHP and MySQL.



PHP Resources
Here are some sites that are great for all things PHP.

<<

. 117
( 132 .)



>>