Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
FUNC5.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 class FUNC5 {
26 
27  static public function is_debug($debug) {
28  return (is_array($debug) && isset($debug[0]['class'], $debug[0]['type'], $debug[0]['function'], $debug[0]['file'], $debug[0]['line']));
29  }
30 
31  static public function microtime_float() {
32  return microtime(true);
33  }
34 
35  static public function is_win() {
36  return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
37  }
38 
39  static public function log_errors($msg = null, $strip = false) {
40  if (defined('LOG_XPM4_ERRORS')) {
41  if (is_string(LOG_XPM4_ERRORS) && is_string($msg) && is_bool($strip)) {
42  if (is_array($arr = unserialize(LOG_XPM4_ERRORS)) && isset($arr['type']) && is_int($arr['type']) && ($arr['type'] == 0 || $arr['type'] == 1 || $arr['type'] == 3)) {
43  $msg = "\r\n".'['.date('m-d-Y H:i:s').'] XPM4 '.($strip ? str_replace(array('<br />', '<b>', '</b>', "\r\n"), '', $msg) : $msg);
44  if ($arr['type'] == 0) error_log($msg);
45  else if ($arr['type'] == 1 && isset($arr['destination'], $arr['headers']) &&
46  is_string($arr['destination']) && strlen(trim($arr['destination'])) > 5 && count(explode('@', $arr['destination'])) == 2 &&
47  is_string($arr['headers']) && strlen(trim($arr['headers'])) > 3) {
48  error_log($msg, 1, trim($arr['destination']), trim($arr['headers']));
49  } else if ($arr['type'] == 3 && isset($arr['destination']) && is_string($arr['destination']) && strlen(trim($arr['destination'])) > 1) {
50  error_log($msg, 3, trim($arr['destination']));
51  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant value', E_USER_WARNING);
52  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid LOG_XPM4_ERRORS constant type', E_USER_WARNING);
53  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error('invalid parameter(s) type', E_USER_WARNING);
54  }
55  }
56 
57  static public function trace($debug, $message = null, $level = 1, $ret = false) {
58  if (self::is_debug($debug) && is_string($message) && ($level == 0 || $level == 1 || $level == 2)) {
59  if ($level == 0) $mess = 'Error';
60  else if ($level == 1) $mess = 'Warning';
61  else if ($level == 2) $mess = 'Notice';
62  $emsg = '<br /><b>'.$mess.'</b>: '.$message.
63  ' on '.strtoupper($debug[0]['class']).$debug[0]['type'].$debug[0]['function'].'()'.
64  ' in <b>'.$debug[0]['file'].'</b> on line <b>'.$debug[0]['line'].'</b><br />'."\r\n";
65  self::log_errors($emsg, true);
66  if ($level == 0) {
67  if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) die($emsg);
68  else exit;
69  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) echo $emsg;
70  } else {
71  $emsg = 'invalid debug parameters';
72  self::log_errors(': '.$emsg, true);
73  if ($level == 0) {
74  if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_ERROR);
75  else exit;
76  } else if (defined('DISPLAY_XPM4_ERRORS') && DISPLAY_XPM4_ERRORS == true) trigger_error($emsg, E_USER_WARNING);
77  }
78  return $ret;
79  }
80 
81  static public function str_clear($str = null, $addrep = null, $debug = null) {
82  if (!self::is_debug($debug)) $debug = debug_backtrace();
83  $err = array();
84  $rep = array("\r", "\n", "\t");
85  if (!is_string($str)) $err[] = 'invalid argument type';
86  if ($addrep == null) $addrep = array();
87  if (is_array($addrep)) {
88  if (count($addrep) > 0) {
89  foreach ($addrep as $strrep) {
90  if (is_string($strrep) && $strrep != '') $rep[] = $strrep;
91  else {
92  $err[] = 'invalid array value';
93  break;
94  }
95  }
96  }
97  } else $err[] = 'invalid array type';
98  if (count($err) == 0) return ($str == '') ? '' : str_replace($rep, '', $str);
99  else self::trace($debug, implode(', ', $err));
100  }
101 
102  static public function is_alpha($str = null, $num = true, $add = '', $debug = null) {
103  if (!self::is_debug($debug)) $debug = debug_backtrace();
104  $err = array();
105  if (!is_string($str)) $err[] = 'invalid argument type';
106  if (!is_bool($num)) $err[] = 'invalid numeric type';
107  if (!is_string($add)) $err[] = 'invalid additional type';
108  if (count($err) > 0) self::trace($debug, implode(', ', $err));
109  else {
110  if ($str != '') {
111  $lst = 'abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ'.$add;
112  if ($num) $lst .= '1234567890';
113  $len1 = strlen($str);
114  $len2 = strlen($lst);
115  $match = true;
116  for ($i = 0; $i < $len1; $i++) {
117  $found = false;
118  for ($j = 0; $j < $len2; $j++) {
119  if ($lst{$j} == $str{$i}) {
120  $found = true;
121  break;
122  }
123  }
124  if (!$found) {
125  $match = false;
126  break;
127  }
128  }
129  return $match;
130  } else return false;
131  }
132  }
133 
134  static public function is_hostname($str = null, $addr = false, $debug = null) {
135  if (!self::is_debug($debug)) $debug = debug_backtrace();
136  $err = array();
137  if (!is_string($str)) $err[] = 'invalid hostname type';
138  if (!is_bool($addr)) $err[] = 'invalid address type';
139  if (count($err) > 0) self::trace($debug, implode(', ', $err));
140  else {
141  $ret = false;
142  if (trim($str) != '' && self::is_alpha($str, true, '-.')) {
143  if (count($exphost1 = explode('.', $str)) > 1 && !(strstr($str, '.-') || strstr($str, '-.'))) {
144  $set = true;
145  foreach ($exphost1 as $expstr1) {
146  if ($expstr1 == '') {
147  $set = false;
148  break;
149  }
150  }
151  if ($set) {
152  foreach (($exphost2 = explode('-', $str)) as $expstr2) {
153  if ($expstr2 == '') {
154  $set = false;
155  break;
156  }
157  }
158  }
159  $ext = $exphost1[count($exphost1)-1];
160  $len = strlen($ext);
161  if ($set && $len >= 2 && $len <= 6 && self::is_alpha($ext, false)) $ret = true;
162  }
163  }
164  return ($ret && $addr && gethostbyname($str) == $str) ? false : $ret;
165  }
166  }
167 
168  static public function is_ipv4($str = null, $debug = null) {
169  if (!self::is_debug($debug)) $debug = debug_backtrace();
170  if (is_string($str)) return (trim($str) != '' && ip2long($str) && count(explode('.', $str)) === 4);
171  else self::trace($debug, 'invalid argument type');
172  }
173 
174  static public function getmxrr_win($hostname = null, &$mxhosts, $debug = null) {
175  if (!self::is_debug($debug)) $debug = debug_backtrace();
176  $mxhosts = array();
177  if (!is_string($hostname)) self::trace($debug, 'invalid hostname type');
178  else {
179  $hostname = strtolower($hostname);
180  if (self::is_hostname($hostname, true, $debug)) {
181  $retstr = exec('nslookup -type=mx '.$hostname, $retarr);
182  if ($retstr && count($retarr) > 0) {
183  foreach ($retarr as $line) {
184  if (preg_match('/.*mail exchanger = (.*)/', $line, $matches)) $mxhosts[] = $matches[1];
185  }
186  }
187  } else self::trace($debug, 'invalid hostname value', 1);
188  return (count($mxhosts) > 0);
189  }
190  }
191 
192  static public function is_mail($addr = null, $vermx = false, $debug = null) {
193  if (!self::is_debug($debug)) $debug = debug_backtrace();
194  $err = array();
195  if (!is_string($addr)) $err[] = 'invalid address type';
196  if (!is_bool($vermx)) $err[] = 'invalid MX type';
197  if (count($err) > 0) self::trace($debug, implode(', ', $err));
198  else {
199  $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])));
200  if ($ret && $vermx) {
201  if (self::is_ipv4($exp[1])) $ret = false;
202  else $ret = self::is_win() ? self::getmxrr_win($exp[1], $mxh, $debug) : getmxrr($exp[1], $mxh);
203  }
204  return $ret;
205  }
206  }
207 
208  static public function mime_type($name = null, $debug = null) {
209  if (!self::is_debug($debug)) $debug = debug_backtrace();
210  if (!is_string($name)) self::trace($debug, 'invalid filename type');
211  else {
212  $name = self::str_clear($name);
213  $name = trim($name);
214  if ($name == '') return self::trace($debug, 'invalid filename value', 1);
215  else {
216  $ret = 'application/octet-stream';
217  $arr = array(
218  'z' => 'application/x-compress',
219  'xls' => 'application/x-excel',
220  'gtar' => 'application/x-gtar',
221  'gz' => 'application/x-gzip',
222  'cgi' => 'application/x-httpd-cgi',
223  'php' => 'application/x-httpd-php',
224  'js' => 'application/x-javascript',
225  'swf' => 'application/x-shockwave-flash',
226  'tar' => 'application/x-tar',
227  'tgz' => 'application/x-tar',
228  'tcl' => 'application/x-tcl',
229  'src' => 'application/x-wais-source',
230  'zip' => 'application/zip',
231  'kar' => 'audio/midi',
232  'mid' => 'audio/midi',
233  'midi' => 'audio/midi',
234  'mp2' => 'audio/mpeg',
235  'mp3' => 'audio/mpeg',
236  'mpga' => 'audio/mpeg',
237  'ram' => 'audio/x-pn-realaudio',
238  'rm' => 'audio/x-pn-realaudio',
239  'rpm' => 'audio/x-pn-realaudio-plugin',
240  'wav' => 'audio/x-wav',
241  'bmp' => 'image/bmp',
242  'fif' => 'image/fif',
243  'gif' => 'image/gif',
244  'ief' => 'image/ief',
245  'jpe' => 'image/jpeg',
246  'jpeg' => 'image/jpeg',
247  'jpg' => 'image/jpeg',
248  'png' => 'image/png',
249  'tif' => 'image/tiff',
250  'tiff' => 'image/tiff',
251  'css' => 'text/css',
252  'htm' => 'text/html',
253  'html' => 'text/html',
254  'txt' => 'text/plain',
255  'rtx' => 'text/richtext',
256  'vcf' => 'text/x-vcard',
257  'xml' => 'text/xml',
258  'xsl' => 'text/xsl',
259  'mpe' => 'video/mpeg',
260  'mpeg' => 'video/mpeg',
261  'mpg' => 'video/mpeg',
262  'mov' => 'video/quicktime',
263  'qt' => 'video/quicktime',
264  'asf' => 'video/x-ms-asf',
265  'asx' => 'video/x-ms-asf',
266  'avi' => 'video/x-msvideo',
267  'vrml' => 'x-world/x-vrml',
268  'wrl' => 'x-world/x-vrml');
269  if (count($exp = explode('.', $name)) >= 2) {
270  $ext = strtolower($exp[count($exp)-1]);
271  if (trim($exp[count($exp)-2]) != '' && isset($arr[$ext])) $ret = $arr[$ext];
272  }
273  return $ret;
274  }
275  }
276  }
277 
278 }
279 
280 ?>