/mzz/system/template/drivers/native/mailNativePlugin.php

https://github.com/greevex/mzz-framework-blank-application · PHP · 131 lines · 92 code · 15 blank · 24 comment · 19 complexity · d9d20a233a8fb01cfe6e764a76595211 MD5 · raw file

  1. <?php
  2. /**
  3. * $URL: svn://svn.mzz.ru/mzz/trunk/system/template/drivers/native/mailNativePlugin.php $
  4. *
  5. * MZZ Content Management System (c) 2010
  6. * Website : http://www.mzz.ru
  7. *
  8. * This program is free software and released under
  9. * the GNU/GPL License (See /docs/GPL.txt).
  10. *
  11. * @link http://www.mzz.ru
  12. * @package system
  13. * @subpackage template
  14. * @version $Id: mailNativePlugin.php 4222 2010-05-17 04:50:56Z striker $
  15. */
  16. /**
  17. * Порт {mail} функции для нативных шаблонов
  18. *
  19. * @package system
  20. * @subpackage template
  21. * @version 0.0.1
  22. */
  23. class mailNativePlugin extends aNativePlugin
  24. {
  25. public function run(Array $params = array())
  26. {
  27. $extra = '';
  28. if (empty($params['address'])) {
  29. throw new Exception ("mailto: missing 'address' parameter");
  30. return;
  31. } else {
  32. $address = $params['address'];
  33. }
  34. $text = $address;
  35. // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
  36. // so, don't encode it.
  37. $search = array('%40', '%2C');
  38. $replace = array('@', ',');
  39. $mail_parms = array();
  40. foreach ($params as $var => $value) {
  41. switch ($var) {
  42. case 'cc':
  43. case 'bcc':
  44. case 'followupto':
  45. if (!empty($value))
  46. $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
  47. break;
  48. case 'subject':
  49. case 'newsgroups':
  50. $mail_parms[] = $var . '=' . rawurlencode($value);
  51. break;
  52. case 'extra':
  53. case 'text':
  54. $$var = $value;
  55. default:
  56. }
  57. }
  58. $mail_parm_vals = '';
  59. for ($i = 0; $i < count($mail_parms); $i++) {
  60. $mail_parm_vals .= (0 == $i) ? '?' : '&';
  61. $mail_parm_vals .= $mail_parms[$i];
  62. }
  63. $address .= $mail_parm_vals;
  64. $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
  65. if (!in_array($encode, array('javascript', 'javascript_charcode', 'hex', 'none'))) {
  66. throw new Exception ("mailto: 'encode' parameter must be none, javascript or hex");
  67. return;
  68. }
  69. if ($encode == 'javascript') {
  70. $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
  71. $js_encode = '';
  72. for ($x = 0; $x < strlen($string); $x++) {
  73. $js_encode .= '%' . bin2hex($string[$x]);
  74. }
  75. return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
  76. } elseif ($encode == 'javascript_charcode') {
  77. $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  78. for($x = 0, $y = strlen($string); $x < $y; $x++) {
  79. $ord[] = ord($string[$x]);
  80. }
  81. $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
  82. $_ret .= "<!--\n";
  83. $_ret .= "{document.write(String.fromCharCode(";
  84. $_ret .= implode(',', $ord);
  85. $_ret .= "))";
  86. $_ret .= "}\n";
  87. $_ret .= "//-->\n";
  88. $_ret .= "</script>\n";
  89. return $_ret;
  90. } elseif ($encode == 'hex') {
  91. preg_match('!^(.*)(\?.*)$!', $address, $match);
  92. if (!empty($match[2])) {
  93. throw new Exception ("mailto: hex encoding does not work with extra attributes. Try javascript.");
  94. return;
  95. }
  96. $address_encode = '';
  97. for ($x = 0; $x < strlen($address); $x++) {
  98. if (preg_match('!\w!', $address[$x])) {
  99. $address_encode .= '%' . bin2hex($address[$x]);
  100. } else {
  101. $address_encode .= $address[$x];
  102. }
  103. }
  104. $text_encode = '';
  105. for ($x = 0; $x < strlen($text); $x++) {
  106. $text_encode .= '&#x' . bin2hex($text[$x]) . ';';
  107. }
  108. $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
  109. return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
  110. } else {
  111. // no encoding
  112. return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  113. }
  114. }
  115. }
  116. ?>