Xataface Email Module 0.3
Email/Mailmerge Module for Xataface
lib/XPM/PHP5/FUNC5.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  *                                                                                         *
00005  *  XPertMailer is a PHP Mail Class that can send and read messages in MIME format.        *
00006  *  This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/)     *
00007  *  Copyright (C) 2007 Tanase Laurentiu Iulian                                             *
00008  *                                                                                         *
00009  *  This library is free software; you can redistribute it and/or modify it under the      *
00010  *  terms of the GNU Lesser General Public License as published by the Free Software       *
00011  *  Foundation; either version 2.1 of the License, or (at your option) any later version.  *
00012  *                                                                                         *
00013  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A        *
00015  *  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.        *
00016  *                                                                                         *
00017  *  You should have received a copy of the GNU Lesser General Public License along with    *
00018  *  this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  *  Fifth Floor, Boston, MA 02110-1301, USA                                                *
00020  *                                                                                         *
00021  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00022 
00023 if (!defined('DISPLAY_XPM4_ERRORS')) define('DISPLAY_XPM4_ERRORS', true);
00024 
00025 class FUNC5 {
00026 
00027         static public function is_debug($debug) {
00028                 return (is_array($debug) && isset($debug[0]['class'], $debug[0]['type'], $debug[0]['function'], $debug[0]['file'], $debug[0]['line']));
00029         }
00030 
00031         static public function microtime_float() {
00032                 return microtime(true);
00033         }
00034 
00035         static public function is_win() {
00036                 return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
00037         }
00038 
00039         static public function log_errors($msg = null, $strip = false) {
00040                 if (defined('LOG_XPM4_ERRORS')) {
00041                         if (is_string(LOG_XPM4_ERRORS) && is_string($msg) && is_bool($strip)) {
00042                                 if (is_array($arr = unserialize(LOG_XPM4_ERRORS)) && isset($arr['type']) && is_int($arr['type']) && ($arr['type'] == 0 || $arr['type'] == 1 || $arr['type'] == 3)) {
00043                                         $msg = "\r\n".'['.date('m-d-Y H:i:s').'] XPM4 '.($strip ? str_replace(array('<br />', '<b>', '</b>', "\r\n"), '', $msg) : $msg);
00044                                         if ($arr['type'] == 0) error_log($msg);
00045                                         else if ($arr['type'] == 1 && isset($arr['destination'], $arr['headers']) && 
00046                                                 is_string($arr['destination']) && strlen(trim($arr['destination'])) > 5 && count(explode('@', $arr['destination'])) == 2 && 
00047                                                 is_string($arr['headers']) && strlen(trim($arr['headers'])) > 3) {
00048                                                 error_log($msg, 1, trim($arr['destination']), trim($arr['headers']));
00049                                         } else if ($arr['type'] == 3 && isset($arr['destination']) && is_string($arr['destination']) && strlen(trim($arr['destination'])) > 1) {
00050                                                 error_log($msg, 3, trim($arr['destination']));
00051                                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant value', E_USER_WARNING);
00052                                 } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant type', E_USER_WARNING);
00053                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid parameter(s) type', E_USER_WARNING);
00054                 }
00055         }
00056 
00057         static public function trace($debug, $message = null, $level = 1, $ret = false) {
00058                 if (self::is_debug($debug) && is_string($message) && ($level == 0 || $level == 1 || $level == 2)) {
00059                         if ($level == 0) $mess = 'Error';
00060                         else if ($level == 1) $mess = 'Warning';
00061                         else if ($level == 2) $mess = 'Notice';
00062                         $emsg = '<br /><b>'.$mess.'</b>: '.$message.
00063                                 ' on '.strtoupper($debug[0]['class']).$debug[0]['type'].$debug[0]['function'].'()'.
00064                                 ' in <b>'.$debug[0]['file'].'</b> on line <b>'.$debug[0]['line'].'</b><br />'."\r\n";
00065                         self::log_errors($emsg, true);
00066                         if ($level == 0) {
00067                                 if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) die($emsg);
00068                                 else exit;
00069                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) echo $emsg;
00070                 } else {
00071                         $emsg = 'invalid debug parameters';
00072                         self::log_errors(': '.$emsg, true);
00073                         if ($level == 0) {
00074                                 if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_ERROR);
00075                                 else exit;
00076                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_WARNING);
00077                 }
00078                 return $ret;
00079         }
00080 
00081         static public function str_clear($str = null, $addrep = null, $debug = null) {
00082                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00083                 $err = array();
00084                 $rep = array("\r", "\n", "\t");
00085                 if (!is_string($str)) $err[] = 'invalid argument type';
00086                 if ($addrep == null) $addrep = array();
00087                 if (is_array($addrep)) {
00088                         if (count($addrep) > 0) {
00089                                 foreach ($addrep as $strrep) {
00090                                         if (is_string($strrep) && $strrep != '') $rep[] = $strrep;
00091                                         else {
00092                                                 $err[] = 'invalid array value';
00093                                                 break;
00094                                         }
00095                                 }
00096                         }
00097                 } else $err[] = 'invalid array type';
00098                 if (count($err) == 0) return ($str == '') ? '' : str_replace($rep, '', $str);
00099                 else self::trace($debug, implode(', ', $err));
00100         }
00101 
00102         static public function is_alpha($str = null, $num = true, $add = '', $debug = null) {
00103                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00104                 $err = array();
00105                 if (!is_string($str)) $err[] = 'invalid argument type';
00106                 if (!is_bool($num)) $err[] = 'invalid numeric type';
00107                 if (!is_string($add)) $err[] = 'invalid additional type';
00108                 if (count($err) > 0) self::trace($debug, implode(', ', $err));
00109                 else {
00110                         if ($str != '') {
00111                                 $lst = 'abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ'.$add;
00112                                 if ($num) $lst .= '1234567890';
00113                                 $len1 = strlen($str);
00114                                 $len2 = strlen($lst);
00115                                 $match = true;
00116                                 for ($i = 0; $i < $len1; $i++) {
00117                                         $found = false;
00118                                         for ($j = 0; $j < $len2; $j++) {
00119                                                 if ($lst{$j} == $str{$i}) {
00120                                                         $found = true;
00121                                                         break;
00122                                                 }
00123                                         }
00124                                         if (!$found) {
00125                                                 $match = false;
00126                                                 break;
00127                                         }
00128                                 }
00129                                 return $match;
00130                         } else return false;
00131                 }
00132         }
00133 
00134         static public function is_hostname($str = null, $addr = false, $debug = null) {
00135                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00136                 $err = array();
00137                 if (!is_string($str)) $err[] = 'invalid hostname type';
00138                 if (!is_bool($addr)) $err[] = 'invalid address type';
00139                 if (count($err) > 0) self::trace($debug, implode(', ', $err));
00140                 else {
00141                         $ret = false;
00142                         if (trim($str) != '' && self::is_alpha($str, true, '-.')) {
00143                                 if (count($exphost1 = explode('.', $str)) > 1 && !(strstr($str, '.-') || strstr($str, '-.'))) {
00144                                         $set = true;
00145                                         foreach ($exphost1 as $expstr1) {
00146                                                 if ($expstr1 == '') {
00147                                                         $set = false;
00148                                                         break;
00149                                                 }
00150                                         }
00151                                         if ($set) {
00152                                                 foreach (($exphost2 = explode('-', $str)) as $expstr2) {
00153                                                         if ($expstr2 == '') {
00154                                                                 $set = false;
00155                                                                 break;
00156                                                         }
00157                                                 }
00158                                         }
00159                                         $ext = $exphost1[count($exphost1)-1];
00160                                         $len = strlen($ext);
00161                                         if ($set && $len >= 2 && $len <= 6 && self::is_alpha($ext, false)) $ret = true;
00162                                 }
00163                         }
00164                         return ($ret && $addr && gethostbyname($str) == $str) ? false : $ret;
00165                 }
00166         }
00167 
00168         static public function is_ipv4($str = null, $debug = null) {
00169                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00170                 if (is_string($str)) return (trim($str) != '' && ip2long($str) && count(explode('.', $str)) === 4);
00171                 else self::trace($debug, 'invalid argument type');
00172         }
00173 
00174         static public function getmxrr_win($hostname = null, &$mxhosts, $debug = null) {
00175                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00176                 $mxhosts = array();
00177                 if (!is_string($hostname)) self::trace($debug, 'invalid hostname type');
00178                 else {
00179                         $hostname = strtolower($hostname);
00180                         if (self::is_hostname($hostname, true, $debug)) {
00181                                 $retstr = exec('nslookup -type=mx '.$hostname, $retarr);
00182                                 if ($retstr && count($retarr) > 0) {
00183                                         foreach ($retarr as $line) {
00184                                                 if (preg_match('/.*mail exchanger = (.*)/', $line, $matches)) $mxhosts[] = $matches[1];
00185                                         }
00186                                 }
00187                         } else self::trace($debug, 'invalid hostname value', 1);
00188                         return (count($mxhosts) > 0);
00189                 }
00190         }
00191 
00192         static public function is_mail($addr = null, $vermx = false, $debug = null) {
00193                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00194                 $err = array();
00195                 if (!is_string($addr)) $err[] = 'invalid address type';
00196                 if (!is_bool($vermx)) $err[] = 'invalid MX type';
00197                 if (count($err) > 0) self::trace($debug, implode(', ', $err));
00198                 else {
00199                         $ret = (count($exp = explode('@', $addr)) === 2 && $exp[0] != '' && $exp[1] != '' && self::is_alpha($exp[0], true, '_-.+') && (self::is_hostname($exp[1]) || self::is_ipv4($exp[1])));
00200                         if ($ret && $vermx) {
00201                                 if (self::is_ipv4($exp[1])) $ret = false;
00202                                 else $ret = self::is_win() ? self::getmxrr_win($exp[1], $mxh, $debug) : getmxrr($exp[1], $mxh);
00203                         }
00204                         return $ret;
00205                 }
00206         }
00207 
00208         static public function mime_type($name = null, $debug = null) {
00209                 if (!self::is_debug($debug)) $debug = debug_backtrace();
00210                 if (!is_string($name)) self::trace($debug, 'invalid filename type');
00211                 else {
00212                         $name = self::str_clear($name);
00213                         $name = trim($name);
00214                         if ($name == '') return self::trace($debug, 'invalid filename value', 1);
00215                         else {
00216                                 $ret = 'application/octet-stream';
00217                                 $arr = array(
00218                                         'z'    => 'application/x-compress', 
00219                                         'xls'  => 'application/x-excel', 
00220                                         'gtar' => 'application/x-gtar', 
00221                                         'gz'   => 'application/x-gzip', 
00222                                         'cgi'  => 'application/x-httpd-cgi', 
00223                                         'php'  => 'application/x-httpd-php', 
00224                                         'js'   => 'application/x-javascript', 
00225                                         'swf'  => 'application/x-shockwave-flash', 
00226                                         'tar'  => 'application/x-tar', 
00227                                         'tgz'  => 'application/x-tar', 
00228                                         'tcl'  => 'application/x-tcl', 
00229                                         'src'  => 'application/x-wais-source', 
00230                                         'zip'  => 'application/zip', 
00231                                         'kar'  => 'audio/midi', 
00232                                         'mid'  => 'audio/midi', 
00233                                         'midi' => 'audio/midi', 
00234                                         'mp2'  => 'audio/mpeg', 
00235                                         'mp3'  => 'audio/mpeg', 
00236                                         'mpga' => 'audio/mpeg', 
00237                                         'ram'  => 'audio/x-pn-realaudio', 
00238                                         'rm'   => 'audio/x-pn-realaudio', 
00239                                         'rpm'  => 'audio/x-pn-realaudio-plugin', 
00240                                         'wav'  => 'audio/x-wav', 
00241                                         'bmp'  => 'image/bmp', 
00242                                         'fif'  => 'image/fif', 
00243                                         'gif'  => 'image/gif', 
00244                                         'ief'  => 'image/ief', 
00245                                         'jpe'  => 'image/jpeg', 
00246                                         'jpeg' => 'image/jpeg', 
00247                                         'jpg'  => 'image/jpeg', 
00248                                         'png'  => 'image/png', 
00249                                         'tif'  => 'image/tiff', 
00250                                         'tiff' => 'image/tiff', 
00251                                         'css'  => 'text/css', 
00252                                         'htm'  => 'text/html', 
00253                                         'html' => 'text/html', 
00254                                         'txt'  => 'text/plain', 
00255                                         'rtx'  => 'text/richtext', 
00256                                         'vcf'  => 'text/x-vcard', 
00257                                         'xml'  => 'text/xml', 
00258                                         'xsl'  => 'text/xsl', 
00259                                         'mpe'  => 'video/mpeg', 
00260                                         'mpeg' => 'video/mpeg', 
00261                                         'mpg'  => 'video/mpeg', 
00262                                         'mov'  => 'video/quicktime', 
00263                                         'qt'   => 'video/quicktime', 
00264                                         'asf'  => 'video/x-ms-asf', 
00265                                         'asx'  => 'video/x-ms-asf', 
00266                                         'avi'  => 'video/x-msvideo', 
00267                                         'vrml' => 'x-world/x-vrml', 
00268                                         'wrl'  => 'x-world/x-vrml');
00269                                 if (count($exp = explode('.', $name)) >= 2) {
00270                                         $ext = strtolower($exp[count($exp)-1]);
00271                                         if (trim($exp[count($exp)-2]) != '' && isset($arr[$ext])) $ret = $arr[$ext];
00272                                 }
00273                                 return $ret;
00274                         }
00275                 }
00276         }
00277 
00278 }
00279 
00280 ?>
 All Data Structures Files Functions Variables Enumerations