Xataface Email Module 0.2
Email/Mailmerge Module for Xataface
lib/XPM/PHP4/FUNC4.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 if (!function_exists('debug_backtrace')) {
00026         function debug_backtrace() {
00027                 return array(0 => array('class' => 'unknown', 'type' => 'unknown', 'function' => 'unknown', 'file' => __FILE__, 'line' => __LINE__));
00028         }
00029 }
00030 
00031 class FUNC4 {
00032 
00033         function is_debug($debug) {
00034                 return (is_array($debug) && isset($debug[0]['class'], $debug[0]['type'], $debug[0]['function'], $debug[0]['file'], $debug[0]['line']));
00035         }
00036 
00037         function microtime_float() {
00038                 list($usec, $sec) = explode(' ', microtime());
00039                 return ((float)$usec + (float)$sec);
00040         }
00041 
00042         function is_win() {
00043                 return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
00044         }
00045 
00046         function log_errors($msg = null, $strip = false) {
00047                 if (defined('LOG_XPM4_ERRORS')) {
00048                         if (is_string(LOG_XPM4_ERRORS) && is_string($msg) && is_bool($strip)) {
00049                                 if (is_array($arr = unserialize(LOG_XPM4_ERRORS)) && isset($arr['type']) && is_int($arr['type']) && ($arr['type'] == 0 || $arr['type'] == 1 || $arr['type'] == 3)) {
00050                                         $msg = "\r\n".'['.date('m-d-Y H:i:s').'] XPM4 '.($strip ? str_replace(array('<br />', '<b>', '</b>', "\r\n"), '', $msg) : $msg);
00051                                         if ($arr['type'] == 0) error_log($msg);
00052                                         else if ($arr['type'] == 1 && isset($arr['destination'], $arr['headers']) && 
00053                                                 is_string($arr['destination']) && strlen(trim($arr['destination'])) > 5 && count(explode('@', $arr['destination'])) == 2 && 
00054                                                 is_string($arr['headers']) && strlen(trim($arr['headers'])) > 3) {
00055                                                 error_log($msg, 1, trim($arr['destination']), trim($arr['headers']));
00056                                         } else if ($arr['type'] == 3 && isset($arr['destination']) && is_string($arr['destination']) && strlen(trim($arr['destination'])) > 1) {
00057                                                 error_log($msg, 3, trim($arr['destination']));
00058                                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant value', E_USER_WARNING);
00059                                 } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant type', E_USER_WARNING);
00060                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid parameter(s) type', E_USER_WARNING);
00061                 }
00062         }
00063 
00064         function trace($debug, $message = null, $level = 1, $ret = false) {
00065                 if (FUNC4::is_debug($debug) && is_string($message) && ($level == 0 || $level == 1 || $level == 2)) {
00066                         if ($level == 0) $mess = 'Error';
00067                         else if ($level == 1) $mess = 'Warning';
00068                         else if ($level == 2) $mess = 'Notice';
00069                         $emsg = '<br /><b>'.$mess.'</b>: '.$message.
00070                                 ' on '.strtoupper($debug[0]['class']).$debug[0]['type'].$debug[0]['function'].'()'.
00071                                 ' in <b>'.$debug[0]['file'].'</b> on line <b>'.$debug[0]['line'].'</b><br />'."\r\n";
00072                         FUNC4::log_errors($emsg, true);
00073                         if ($level == 0) {
00074                                 if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) die($emsg);
00075                                 else exit;
00076                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) echo $emsg;
00077                 } else {
00078                         $emsg = 'invalid debug parameters';
00079                         FUNC4::log_errors(': '.$emsg, true);
00080                         if ($level == 0) {
00081                                 if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_ERROR);
00082                                 else exit;
00083                         } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_WARNING);
00084                 }
00085                 return $ret;
00086         }
00087 
00088         function str_clear($str = null, $addrep = null, $debug = null) {
00089                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00090                 $err = array();
00091                 $rep = array("\r", "\n", "\t");
00092                 if (!is_string($str)) $err[] = 'invalid argument type';
00093                 if ($addrep == null) $addrep = array();
00094                 if (is_array($addrep)) {
00095                         if (count($addrep) > 0) {
00096                                 foreach ($addrep as $strrep) {
00097                                         if (is_string($strrep) && $strrep != '') $rep[] = $strrep;
00098                                         else {
00099                                                 $err[] = 'invalid array value';
00100                                                 break;
00101                                         }
00102                                 }
00103                         }
00104                 } else $err[] = 'invalid array type';
00105                 if (count($err) == 0) return ($str == '') ? '' : str_replace($rep, '', $str);
00106                 else FUNC4::trace($debug, implode(', ', $err));
00107         }
00108 
00109         function is_alpha($str = null, $num = true, $add = '', $debug = null) {
00110                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00111                 $err = array();
00112                 if (!is_string($str)) $err[] = 'invalid argument type';
00113                 if (!is_bool($num)) $err[] = 'invalid numeric type';
00114                 if (!is_string($add)) $err[] = 'invalid additional type';
00115                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00116                 else {
00117                         if ($str != '') {
00118                                 $lst = 'abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ'.$add;
00119                                 if ($num) $lst .= '1234567890';
00120                                 $len1 = strlen($str);
00121                                 $len2 = strlen($lst);
00122                                 $match = true;
00123                                 for ($i = 0; $i < $len1; $i++) {
00124                                         $found = false;
00125                                         for ($j = 0; $j < $len2; $j++) {
00126                                                 if ($lst{$j} == $str{$i}) {
00127                                                         $found = true;
00128                                                         break;
00129                                                 }
00130                                         }
00131                                         if (!$found) {
00132                                                 $match = false;
00133                                                 break;
00134                                         }
00135                                 }
00136                                 return $match;
00137                         } else return false;
00138                 }
00139         }
00140 
00141         function is_hostname($str = null, $addr = false, $debug = null) {
00142                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00143                 $err = array();
00144                 if (!is_string($str)) $err[] = 'invalid hostname type';
00145                 if (!is_bool($addr)) $err[] = 'invalid address type';
00146                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00147                 else {
00148                         $ret = false;
00149                         if (trim($str) != '' && FUNC4::is_alpha($str, true, '-.')) {
00150                                 if (count($exphost1 = explode('.', $str)) > 1 && !(strstr($str, '.-') || strstr($str, '-.'))) {
00151                                         $set = true;
00152                                         foreach ($exphost1 as $expstr1) {
00153                                                 if ($expstr1 == '') {
00154                                                         $set = false;
00155                                                         break;
00156                                                 }
00157                                         }
00158                                         if ($set) {
00159                                                 foreach (($exphost2 = explode('-', $str)) as $expstr2) {
00160                                                         if ($expstr2 == '') {
00161                                                                 $set = false;
00162                                                                 break;
00163                                                         }
00164                                                 }
00165                                         }
00166                                         $ext = $exphost1[count($exphost1)-1];
00167                                         $len = strlen($ext);
00168                                         if ($set && $len >= 2 && $len <= 6 && FUNC4::is_alpha($ext, false)) $ret = true;
00169                                 }
00170                         }
00171                         return ($ret && $addr && gethostbyname($str) == $str) ? false : $ret;
00172                 }
00173         }
00174 
00175         function is_ipv4($str = null, $debug = null) {
00176                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00177                 if (is_string($str)) return (trim($str) != '' && ip2long($str) && count(explode('.', $str)) === 4);
00178                 else FUNC4::trace($debug, 'invalid argument type');
00179         }
00180 
00181         function getmxrr_win($hostname = null, &$mxhosts, $debug = null) {
00182                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00183                 $mxhosts = array();
00184                 if (!is_string($hostname)) FUNC4::trace($debug, 'invalid hostname type');
00185                 else {
00186                         $hostname = strtolower($hostname);
00187                         if (FUNC4::is_hostname($hostname, true, $debug)) {
00188                                 $retstr = exec('nslookup -type=mx '.$hostname, $retarr);
00189                                 if ($retstr && count($retarr) > 0) {
00190                                         foreach ($retarr as $line) {
00191                                                 if (preg_match('/.*mail exchanger = (.*)/', $line, $matches)) $mxhosts[] = $matches[1];
00192                                         }
00193                                 }
00194                         } else FUNC4::trace($debug, 'invalid hostname value', 1);
00195                         return (count($mxhosts) > 0);
00196                 }
00197         }
00198 
00199         function is_mail($addr = null, $vermx = false, $debug = null) {
00200                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00201                 $err = array();
00202                 if (!is_string($addr)) $err[] = 'invalid address type';
00203                 if (!is_bool($vermx)) $err[] = 'invalid MX type';
00204                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00205                 else {
00206                         $ret = (count($exp = explode('@', $addr)) === 2 && $exp[0] != '' && $exp[1] != '' && FUNC4::is_alpha($exp[0], true, '_-.+') && (FUNC4::is_hostname($exp[1]) || FUNC4::is_ipv4($exp[1])));
00207                         if ($ret && $vermx) {
00208                                 if (FUNC4::is_ipv4($exp[1])) $ret = false;
00209                                 else $ret = FUNC4::is_win() ? FUNC4::getmxrr_win($exp[1], $mxh, $debug) : getmxrr($exp[1], $mxh);
00210                         }
00211                         return $ret;
00212                 }
00213         }
00214 
00215         function mime_type($name = null, $debug = null) {
00216                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00217                 if (!is_string($name)) FUNC4::trace($debug, 'invalid filename type');
00218                 else {
00219                         $name = FUNC4::str_clear($name);
00220                         $name = trim($name);
00221                         if ($name == '') return FUNC4::trace($debug, 'invalid filename value', 1);
00222                         else {
00223                                 $ret = 'application/octet-stream';
00224                                 $arr = array(
00225                                         'z'    => 'application/x-compress', 
00226                                         'xls'  => 'application/x-excel', 
00227                                         'gtar' => 'application/x-gtar', 
00228                                         'gz'   => 'application/x-gzip', 
00229                                         'cgi'  => 'application/x-httpd-cgi', 
00230                                         'php'  => 'application/x-httpd-php', 
00231                                         'js'   => 'application/x-javascript', 
00232                                         'swf'  => 'application/x-shockwave-flash', 
00233                                         'tar'  => 'application/x-tar', 
00234                                         'tgz'  => 'application/x-tar', 
00235                                         'tcl'  => 'application/x-tcl', 
00236                                         'src'  => 'application/x-wais-source', 
00237                                         'zip'  => 'application/zip', 
00238                                         'kar'  => 'audio/midi', 
00239                                         'mid'  => 'audio/midi', 
00240                                         'midi' => 'audio/midi', 
00241                                         'mp2'  => 'audio/mpeg', 
00242                                         'mp3'  => 'audio/mpeg', 
00243                                         'mpga' => 'audio/mpeg', 
00244                                         'ram'  => 'audio/x-pn-realaudio', 
00245                                         'rm'   => 'audio/x-pn-realaudio', 
00246                                         'rpm'  => 'audio/x-pn-realaudio-plugin', 
00247                                         'wav'  => 'audio/x-wav', 
00248                                         'bmp'  => 'image/bmp', 
00249                                         'fif'  => 'image/fif', 
00250                                         'gif'  => 'image/gif', 
00251                                         'ief'  => 'image/ief', 
00252                                         'jpe'  => 'image/jpeg', 
00253                                         'jpeg' => 'image/jpeg', 
00254                                         'jpg'  => 'image/jpeg', 
00255                                         'png'  => 'image/png', 
00256                                         'tif'  => 'image/tiff', 
00257                                         'tiff' => 'image/tiff', 
00258                                         'css'  => 'text/css', 
00259                                         'htm'  => 'text/html', 
00260                                         'html' => 'text/html', 
00261                                         'txt'  => 'text/plain', 
00262                                         'rtx'  => 'text/richtext', 
00263                                         'vcf'  => 'text/x-vcard', 
00264                                         'xml'  => 'text/xml', 
00265                                         'xsl'  => 'text/xsl', 
00266                                         'mpe'  => 'video/mpeg', 
00267                                         'mpeg' => 'video/mpeg', 
00268                                         'mpg'  => 'video/mpeg', 
00269                                         'mov'  => 'video/quicktime', 
00270                                         'qt'   => 'video/quicktime', 
00271                                         'asf'  => 'video/x-ms-asf', 
00272                                         'asx'  => 'video/x-ms-asf', 
00273                                         'avi'  => 'video/x-msvideo', 
00274                                         'vrml' => 'x-world/x-vrml', 
00275                                         'wrl'  => 'x-world/x-vrml');
00276                                 if (count($exp = explode('.', $name)) >= 2) {
00277                                         $ext = strtolower($exp[count($exp)-1]);
00278                                         if (trim($exp[count($exp)-2]) != '' && isset($arr[$ext])) $ret = $arr[$ext];
00279                                 }
00280                                 return $ret;
00281                         }
00282                 }
00283         }
00284 
00285 }
00286 
00287 ?>
 All Data Structures Files Functions Variables Enumerations