Files
selima-perl/lib/php/monica/email.inc.php
2026-03-10 21:31:43 +08:00

90 lines
2.3 KiB
PHP

<?php
// File name: email.inc.php
// Description: PHP subroutines for e-mail processing
// Date: 2004-02-11
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2004-2007 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/rfc822.inc.php";
// Settings
define("ISEMAIL_OK", "ok");
define("ISEMAIL_MALFORMED", "malformed");
define("ISEMAIL_DOMAIN_NOT_FOUND", "baddomain");
define("ISEMAIL_PATTERN",
"/^" . RFC822_LOCAL_PART . "@(" . RFC822_DOMAIN . ")$/");
// is_email: Check the e-mail address validity
function is_email($email)
{
// Malformed
if (!preg_match(ISEMAIL_PATTERN, $email, $m)) {
return ISEMAIL_MALFORMED;
}
$domain = $m[1];
// Domain does not exists
if (!_email_is_domain_exists($domain)) {
return ISEMAIL_DOMAIN_NOT_FOUND;
}
return ISEMAIL_OK;
}
// is_email_wellformed: Check the e-mail address format validity
function is_email_wellformed($email)
{
return preg_match(ISEMAIL_PATTERN, $email)? true: false;
}
// cmp_email: Compare 2 e-mails, to be used with usort()
function cmp_email($a, $b)
{
static $rev = array();
if (!array_key_exists($a, $rev)) {
$rev[$a] = _email_rev_email($a);
}
if (!array_key_exists($b, $rev)) {
$rev[$b] = _email_rev_email($b);
}
return strcmp($rev[$a], $rev[$b]);
}
// _email_is_domain_exists: Check the domain of an e-mail address
function _email_is_domain_exists($domain)
{
// Cache the result
static $cache = array();
// Return the cache
if (array_key_exists($domain, $cache)) {
return $cache[$domain];
}
// Check the MX records
if (getmxrr($domain, $mxhosts)) {
$cache[$domain] = true;
return $cache[$domain];
}
// Check the A or CNAME records
if (gethostbynamel($domain) !== false) {
$cache[$domain] = true;
return $cache[$domain];
}
// Not found at last
$cache[$domain] = false;
return $cache[$domain];
}
// _email_rev_email: Reverse each part of an e-mail
function _email_rev_email($email)
{
$pos = strpos($email, "@");
$local = substr($email, 0, $pos);
$domparts = explode(".", substr($email, $pos+1));
return strtolower(implode(".", array_reverse($domparts)) . "@" . $local);
}
?>