PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/WeBid/trunk/includes/functions_email.php

https://github.com/cipiceuca25/webid
PHP | 375 lines | 292 code | 55 blank | 28 comment | 35 complexity | d903fb0a6c80a34c246b3d23bba2dafb MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /***************************************************************************
  3. * copyright : (C) 2008, 2009 WeBid
  4. * site : http://www.webidsupport.com/
  5. ***************************************************************************/
  6. /***************************************************************************
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version. Although none of the code may be
  11. * sold. If you have been sold this script, get a refund.
  12. ***************************************************************************/
  13. if (!defined('InWeBid')) exit('Access denied');
  14. class email_class
  15. {
  16. var $from, $message, $subject, $headers, $email_uid, $userlang;
  17. function build_header()
  18. {
  19. global $system, $CHARSET;
  20. $headers = array();
  21. if (!isset($this->from) || empty($this->from))
  22. {
  23. $this->from = $system->SETTINGS['adminmail'];
  24. }
  25. $headers[] = 'From: ' . $this->from;
  26. $headers[] = 'Reply-To: ' . $this->from;
  27. $headers[] = 'Return-Path: <' . $this->from . '>';
  28. $headers[] = 'Sender: <' . $system->SETTINGS['adminmail'] . '>';
  29. $headers[] = 'MIME-Version: 1.0';
  30. $headers[] = 'Date: ' . gmdate('r', time());
  31. //$headers[] = 'Content-Type: text/plain; charset=' . $CHARSET;
  32. $headers[] = 'Content-Type: text/html; charset=' . $CHARSET;
  33. $headers[] = 'Content-Transfer-Encoding: 8bit';
  34. $this->headers = implode("\n", $headers);
  35. }
  36. function buildmessage($file)
  37. {
  38. global $main_path, $include_path;
  39. $buffer = file($main_path . 'language/' . $this->getuserlang() . '/emails/' . $this->getusermailtype() . '/' . $file);
  40. $i = 0;
  41. $j = 0;
  42. while ($i < count($buffer))
  43. {
  44. if (!preg_match('/^#(.)*$/', $buffer[$i]))
  45. {
  46. $skipped_buffer[$j] = $buffer[$i];
  47. $j++;
  48. }
  49. $i++;
  50. }
  51. $this->message = implode($skipped_buffer, '');
  52. $this->message = str_replace("'", "\'", $this->message);
  53. $this->message = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->message);
  54. preg_match_all('#<!-- ([^<].*?) (.*?)? ?-->#', $this->message, $blocks, PREG_SET_ORDER);
  55. $text_blocks = preg_split('#<!-- [^<].*? (?:.*?)? ?-->#', $this->message);
  56. $compile_blocks = array();
  57. for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++)
  58. {
  59. $block_val = &$blocks[$curr_tb];
  60. switch ($block_val[1])
  61. {
  62. case 'IF':
  63. $compile_blocks[] = "'; " . $this->compile_tag_if (str_replace("\'", "'", $block_val[2]), false) . " \$this->message .= '";
  64. break;
  65. case 'ELSE':
  66. $compile_blocks[] = "'; } else { \$this->message .= '";
  67. break;
  68. case 'ELSEIF':
  69. $compile_blocks[] = "'; " . $this->compile_tag_if (str_replace("\'", "'", $block_val[2]), true) . " \$this->message .= '";
  70. break;
  71. case 'ENDIF':
  72. $compile_blocks[] = "'; } \$this->message .= '";
  73. break;
  74. }
  75. }
  76. $template_php = '';
  77. for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++)
  78. {
  79. $trim_check_text = trim($text_blocks[$i]);
  80. $template_php .= (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '');
  81. }
  82. eval("\$this->message = '$template_php';");
  83. }
  84. function compile_tag_if ($tag_args, $elseif)
  85. {
  86. // Tokenize args for 'if' tag.
  87. preg_match_all('/(?:
  88. "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
  89. \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' |
  90. [(),] |
  91. [^\s(),]+)/x', $tag_args, $match);
  92. $tokens = $match[0];
  93. $is_arg_stack = array();
  94. for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
  95. {
  96. $token = &$tokens[$i];
  97. switch ($token)
  98. {
  99. case '!==':
  100. case '===':
  101. case '<<':
  102. case '>>':
  103. case '|':
  104. case '^':
  105. case '&':
  106. case '~':
  107. case ')':
  108. case ',':
  109. case '+':
  110. case '-':
  111. case '*':
  112. case '/':
  113. case '@':
  114. break;
  115. case '==':
  116. case 'eq':
  117. $token = '==';
  118. break;
  119. case '!=':
  120. case '<>':
  121. case 'ne':
  122. case 'neq':
  123. $token = '!=';
  124. break;
  125. case '<':
  126. case 'lt':
  127. $token = '<';
  128. break;
  129. case '<=':
  130. case 'le':
  131. case 'lte':
  132. $token = '<=';
  133. break;
  134. case '>':
  135. case 'gt':
  136. $token = '>';
  137. break;
  138. case '>=':
  139. case 'ge':
  140. case 'gte':
  141. $token = '>=';
  142. break;
  143. case '&&':
  144. case 'and':
  145. $token = '&&';
  146. break;
  147. case '||':
  148. case 'or':
  149. $token = '||';
  150. break;
  151. case '!':
  152. case 'not':
  153. $token = '!';
  154. break;
  155. case '%':
  156. case 'mod':
  157. $token = '%';
  158. break;
  159. case '(':
  160. array_push($is_arg_stack, $i);
  161. break;
  162. case 'is':
  163. $is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
  164. $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  165. $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
  166. array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
  167. $i = $is_arg_start;
  168. // no break
  169. default:
  170. if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
  171. {
  172. $token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$this->vars[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$this->vars[\'' . $varrefs[3] . '\']');
  173. }
  174. elseif (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
  175. {
  176. // Allow checking if loops are set with .loopname
  177. // It is also possible to check the loop count by doing <!-- IF .loopname > 1 --> for example
  178. $blocks = explode('.', $varrefs[1]);
  179. // If the block is nested, we have a reference that we can grab.
  180. // If the block is not nested, we just go and grab the block from _tpldata
  181. if (sizeof($blocks) > 1)
  182. {
  183. $block = array_pop($blocks);
  184. $namespace = implode('.', $blocks);
  185. $varref = $this->generate_block_data_ref($namespace, true);
  186. // Add the block reference for the last child.
  187. $varref .= "['" . $block . "']";
  188. }
  189. else
  190. {
  191. $varref = '$this->_tpldata';
  192. // Add the block reference for the last child.
  193. $varref .= "['" . $blocks[0] . "']";
  194. }
  195. $token = "sizeof($varref)";
  196. }
  197. elseif (!empty($token))
  198. {
  199. $token = '(' . $token . ')';
  200. }
  201. break;
  202. }
  203. }
  204. // If there are no valid tokens left or only control/compare characters left, we do skip this statement
  205. if (!sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '')
  206. {
  207. $tokens = array('false');
  208. }
  209. return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
  210. }
  211. function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
  212. {
  213. // Get an array of the blocks involved.
  214. $blocks = explode('.', $blockname);
  215. $blockcount = sizeof($blocks) - 1;
  216. // DEFINE is not an element of any referenced variable, we must use _tpldata to access it
  217. if ($defop)
  218. {
  219. $varref = '$this->_tpldata[\'DEFINE\']';
  220. // Build up the string with everything but the last child.
  221. for ($i = 0; $i < $blockcount; $i++)
  222. {
  223. $varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
  224. }
  225. // Add the block reference for the last child.
  226. $varref .= "['" . $blocks[$blockcount] . "']";
  227. // Add the iterator for the last child if requried.
  228. if ($include_last_iterator)
  229. {
  230. $varref .= '[$_' . $blocks[$blockcount] . '_i]';
  231. }
  232. return $varref;
  233. }
  234. else if ($include_last_iterator)
  235. {
  236. return '$_'. $blocks[$blockcount] . '_val';
  237. }
  238. else
  239. {
  240. return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']';
  241. }
  242. }
  243. function assign_vars($vars)
  244. {
  245. $this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars;
  246. }
  247. function getuserlang()
  248. {
  249. global $system, $DBPrefix, $language;
  250. if (isset($this->email_uid) && $this->email_uid > 0)
  251. {
  252. // Retrieve user's prefered language
  253. $query = "SELECT language FROM " . $DBPrefix . "users WHERE id = " . intval($this->email_uid);
  254. $res = mysql_query($query);
  255. $system->check_mysql($res, $query, __LINE__, __FILE__);
  256. if (mysql_num_rows($res) > 0)
  257. {
  258. $USERLANG = mysql_result($res, 0);
  259. if (isset($USERLANG) && !empty($USERLANG)) return $USERLANG;
  260. }
  261. }
  262. elseif(isset($this->userlang))
  263. {
  264. $language = $this->userlang;
  265. }
  266. return $language;
  267. }
  268. function getusermailtype()
  269. {
  270. global $system, $DBPrefix;
  271. if (isset($this->email_uid) && $this->email_uid > 0)
  272. {
  273. // Retrieve user's prefered language
  274. $query = "SELECT emailtype FROM " . $DBPrefix . "users WHERE id = " . intval($this->email_uid);
  275. $res = mysql_query($query);
  276. $system->check_mysql($res, $query, __LINE__, __FILE__);
  277. if (mysql_num_rows($res) > 0)
  278. {
  279. $emailtype = mysql_result($res, 0);
  280. if (isset($emailtype) && !empty($emailtype)) return $emailtype;
  281. }
  282. }
  283. return 'html';
  284. }
  285. function sendmail()
  286. {
  287. if (is_array($this->to))
  288. {
  289. for ($i = 0; $i < count($this->to); $i++)
  290. {
  291. mail($this->to[$i], $this->subject, $this->message, $this->headers);
  292. }
  293. }
  294. else
  295. {
  296. mail($this->to, $this->subject, $this->message, $this->headers);
  297. }
  298. }
  299. function email_basic($subject, $to, $message, $from = '')
  300. {
  301. $this->to = $to;
  302. $this->subject = $subject;
  303. $this->from = $from;
  304. $this->message = $message;
  305. $this->build_header();
  306. $this->sendmail();
  307. }
  308. function email_sender($to, $file, $subject)
  309. {
  310. $this->to = $to;
  311. $this->subject = $subject;
  312. $this->build_header();
  313. $this->buildmessage($file);
  314. $this->sendmail();
  315. }
  316. }
  317. ?>