Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
FUNC4.php
Go to the documentation of this file.
1 <?php
2 
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * *
5  * XPertMailer is a PHP Mail Class that can send and read messages in MIME format. *
6  * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
7  * Copyright (C) 2007 Tanase Laurentiu Iulian *
8  * *
9  * This library is free software; you can redistribute it and/or modify it under the *
10  * terms of the GNU Lesser General Public License as published by the Free Software *
11  * Foundation; either version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY *
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
15  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public License along with *
18  * this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
19  * Fifth Floor, Boston, MA 02110-1301, USA *
20  * *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
23 if (!defined('DISPLAY_XPM4_ERRORS')) define('DISPLAY_XPM4_ERRORS', true);
24 
25 if (!function_exists('debug_backtrace')) {
26  function debug_backtrace() {
27  return array(0 => array('class' => 'unknown', 'type' => 'unknown', 'function' => 'unknown', 'file' => __FILE__, 'line' => __LINE__));
28  }
29 }
30 
31 class FUNC4 {
32 
33  function is_debug($debug) {
34  return (is_array($debug) && isset($debug[0]['class'], $debug[0]['type'], $debug[0]['function'], $debug[0]['file'], $debug[0]['line']));
35  }
36 
37  function microtime_float() {
38  list($usec, $sec) = explode(' ', microtime());
39  return ((float)$usec + (float)$sec);
40  }
41 
42  function is_win() {
43  return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
44  }
45 
46  function log_errors($msg = null, $strip = false) {
47  if (defined('LOG_XPM4_ERRORS')) {
48  if (is_string(LOG_XPM4_ERRORS) && is_string($msg) && is_bool($strip)) {
49  if (is_array($arr = unserialize(LOG_XPM4_ERRORS)) && isset($arr['type']) && is_int($arr['type']) && ($arr['type'] == 0 || $arr['type'] == 1 || $arr['type'] == 3)) {
50  $msg = "\r\n".'['.date('m-d-Y H:i:s').'] XPM4 '.($strip ? str_replace(array('<br />', '<b>', '</b>', "\r\n"), '', $msg) : $msg);
51  if ($arr['type'] == 0) error_log($msg);
52  else if ($arr['type'] == 1 && isset($arr['destination'], $arr['headers']) &&
53  is_string($arr['destination']) && strlen(trim($arr['destination'])) > 5 && count(explode('@', $arr['destination'])) == 2 &&
54  is_string($arr['headers']) && strlen(trim($arr['headers'])) > 3) {
55  error_log($msg, 1, trim($arr['destination']), trim($arr['headers']));
56  } else if ($arr['type'] == 3 && isset($arr['destination']) && is_string($arr['destination']) && strlen(trim($arr['destination'])) > 1) {
57  error_log($msg, 3, trim($arr['destination']));
58  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant value', E_USER_WARNING);
59  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant type', E_USER_WARNING);
60  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid parameter(s) type', E_USER_WARNING);
61  }
62  }
63 
64  function trace($debug, $message = null, $level = 1, $ret = false) {
65  if (FUNC4::is_debug($debug) && is_string($message) && ($level == 0 || $level == 1 || $level == 2)) {
66  if ($level == 0) $mess = 'Error';
67  else if ($level == 1) $mess = 'Warning';
68  else if ($level == 2) $mess = 'Notice';
69  $emsg = '<br /><b>'.$mess.'</b>: '.$message.
70  ' on '.strtoupper($debug[0]['class']).$debug[0]['type'].$debug[0]['function'].'()'.
71  ' in <b>'.$debug[0]['file'].'</b> on line <b>'.$debug[0]['line'].'</b><br />'."\r\n";
72  FUNC4::log_errors($emsg, true);
73  if ($level == 0) {
74  if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) die($emsg);
75  else exit;
76  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) echo $emsg;
77  } else {
78  $emsg = 'invalid debug parameters';
79  FUNC4::log_errors(': '.$emsg, true);
80  if ($level == 0) {
81  if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_ERROR);
82  else exit;
83  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_WARNING);
84  }
85  return $ret;
86  }
87 
88  function str_clear($str = null, $addrep = null, $debug = null) {
89  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
90  $err = array();
91  $rep = array("\r", "\n", "\t");
92  if (!is_string($str)) $err[] = 'invalid argument type';
93  if ($addrep == null) $addrep = array();
94  if (is_array($addrep)) {
95  if (count($addrep) > 0) {
96  foreach ($addrep as $strrep) {
97  if (is_string($strrep) && $strrep != '') $rep[] = $strrep;
98  else {
99  $err[] = 'invalid array value';
100  break;
101  }
102  }
103  }
104  } else $err[] = 'invalid array type';
105  if (count($err) == 0) return ($str == '') ? '' : str_replace($rep, '', $str);
106  else FUNC4::trace($debug, implode(', ', $err));
107  }
108 
109  function is_alpha($str = null, $num = true, $add = '', $debug = null) {
110  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
111  $err = array();
112  if (!is_string($str)) $err[] = 'invalid argument type';
113  if (!is_bool($num)) $err[] = 'invalid numeric type';
114  if (!is_string($add)) $err[] = 'invalid additional type';
115  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
116  else {
117  if ($str != '') {
118  $lst = 'abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ'.$add;
119  if ($num) $lst .= '1234567890';
120  $len1 = strlen($str);
121  $len2 = strlen($lst);
122  $match = true;
123  for ($i = 0; $i < $len1; $i++) {
124  $found = false;
125  for ($j = 0; $j < $len2; $j++) {
126  if ($lst{$j} == $str{$i}) {
127  $found = true;
128  break;
129  }
130  }
131  if (!$found) {
132  $match = false;
133  break;
134  }
135  }
136  return $match;
137  } else return false;
138  }
139  }
140 
141  function is_hostname($str = null, $addr = false, $debug = null) {
142  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
143  $err = array();
144  if (!is_string($str)) $err[] = 'invalid hostname type';
145  if (!is_bool($addr)) $err[] = 'invalid address type';
146  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
147  else {
148  $ret = false;
149  if (trim($str) != '' && FUNC4::is_alpha($str, true, '-.')) {
150  if (count($exphost1 = explode('.', $str)) > 1 && !(strstr($str, '.-') || strstr($str, '-.'))) {
151  $set = true;
152  foreach ($exphost1 as $expstr1) {
153  if ($expstr1 == '') {
154  $set = false;
155  break;
156  }
157  }
158  if ($set) {
159  foreach (($exphost2 = explode('-', $str)) as $expstr2) {
160  if ($expstr2 == '') {
161  $set = false;
162  break;
163  }
164  }
165  }
166  $ext = $exphost1[count($exphost1)-1];
167  $len = strlen($ext);
168  if ($set && $len >= 2 && $len <= 6 && FUNC4::is_alpha($ext, false)) $ret = true;
169  }
170  }
171  return ($ret && $addr && gethostbyname($str) == $str) ? false : $ret;
172  }
173  }
174 
175  function is_ipv4($str = null, $debug = null) {
176  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
177  if (is_string($str)) return (trim($str) != '' && ip2long($str) && count(explode('.', $str)) === 4);
178  else FUNC4::trace($debug, 'invalid argument type');
179  }
180 
181  function getmxrr_win($hostname = null, &$mxhosts, $debug = null) {
182  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
183  $mxhosts = array();
184  if (!is_string($hostname)) FUNC4::trace($debug, 'invalid hostname type');
185  else {
186  $hostname = strtolower($hostname);
187  if (FUNC4::is_hostname($hostname, true, $debug)) {
188  $retstr = exec('nslookup -type=mx '.$hostname, $retarr);
189  if ($retstr && count($retarr) > 0) {
190  foreach ($retarr as $line) {
191  if (preg_match('/.*mail exchanger = (.*)/', $line, $matches)) $mxhosts[] = $matches[1];
192  }
193  }
194  } else FUNC4::trace($debug, 'invalid hostname value', 1);
195  return (count($mxhosts) > 0);
196  }
197  }
198 
199  function is_mail($addr = null, $vermx = false, $debug = null) {
200  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
201  $err = array();
202  if (!is_string($addr)) $err[] = 'invalid address type';
203  if (!is_bool($vermx)) $err[] = 'invalid MX type';
204  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
205  else {
206  $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])));
207  if ($ret && $vermx) {
208  if (FUNC4::is_ipv4($exp[1])) $ret = false;
209  else $ret = FUNC4::is_win() ? FUNC4::getmxrr_win($exp[1], $mxh, $debug) : getmxrr($exp[1], $mxh);
210  }
211  return $ret;
212  }
213  }
214 
215  function mime_type($name = null, $debug = null) {
216  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
217  if (!is_string($name)) FUNC4::trace($debug, 'invalid filename type');
218  else {
219  $name = FUNC4::str_clear($name);
220  $name = trim($name);
221  if ($name == '') return FUNC4::trace($debug, 'invalid filename value', 1);
222  else {
223  $ret = 'application/octet-stream';
224  $arr = array(
225  'z' => 'application/x-compress',
226  'xls' => 'application/x-excel',
227  'gtar' => 'application/x-gtar',
228  'gz' => 'application/x-gzip',
229  'cgi' => 'application/x-httpd-cgi',
230  'php' => 'application/x-httpd-php',
231  'js' => 'application/x-javascript',
232  'swf' => 'application/x-shockwave-flash',
233  'tar' => 'application/x-tar',
234  'tgz' => 'application/x-tar',
235  'tcl' => 'application/x-tcl',
236  'src' => 'application/x-wais-source',
237  'zip' => 'application/zip',
238  'kar' => 'audio/midi',
239  'mid' => 'audio/midi',
240  'midi' => 'audio/midi',
241  'mp2' => 'audio/mpeg',
242  'mp3' => 'audio/mpeg',
243  'mpga' => 'audio/mpeg',
244  'ram' => 'audio/x-pn-realaudio',
245  'rm' => 'audio/x-pn-realaudio',
246  'rpm' => 'audio/x-pn-realaudio-plugin',
247  'wav' => 'audio/x-wav',
248  'bmp' => 'image/bmp',
249  'fif' => 'image/fif',
250  'gif' => 'image/gif',
251  'ief' => 'image/ief',
252  'jpe' => 'image/jpeg',
253  'jpeg' => 'image/jpeg',
254  'jpg' => 'image/jpeg',
255  'png' => 'image/png',
256  'tif' => 'image/tiff',
257  'tiff' => 'image/tiff',
258  'css' => 'text/css',
259  'htm' => 'text/html',
260  'html' => 'text/html',
261  'txt' => 'text/plain',
262  'rtx' => 'text/richtext',
263  'vcf' => 'text/x-vcard',
264  'xml' => 'text/xml',
265  'xsl' => 'text/xsl',
266  'mpe' => 'video/mpeg',
267  'mpeg' => 'video/mpeg',
268  'mpg' => 'video/mpeg',
269  'mov' => 'video/quicktime',
270  'qt' => 'video/quicktime',
271  'asf' => 'video/x-ms-asf',
272  'asx' => 'video/x-ms-asf',
273  'avi' => 'video/x-msvideo',
274  'vrml' => 'x-world/x-vrml',
275  'wrl' => 'x-world/x-vrml');
276  if (count($exp = explode('.', $name)) >= 2) {
277  $ext = strtolower($exp[count($exp)-1]);
278  if (trim($exp[count($exp)-2]) != '' && isset($arr[$ext])) $ret = $arr[$ext];
279  }
280  return $ret;
281  }
282  }
283  }
284 
285 }
286 
287 ?>