You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.mailto.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {mailto} function plugin
  10. * Type: function<br>
  11. * Name: mailto<br>
  12. * Date: May 21, 2002
  13. * Purpose: automate mailto address link creation, and optionally encode them.<br>
  14. * Params:
  15. * <pre>
  16. * - address - (required) - e-mail address
  17. * - text - (optional) - text to display, default is address
  18. * - encode - (optional) - can be one of:
  19. * * none : no encoding (default)
  20. * * javascript : encode with javascript
  21. * * javascript_charcode : encode with javascript charcode
  22. * * hex : encode with hexidecimal (no javascript)
  23. * - cc - (optional) - address(es) to carbon copy
  24. * - bcc - (optional) - address(es) to blind carbon copy
  25. * - subject - (optional) - e-mail subject
  26. * - newsgroups - (optional) - newsgroup(s) to post to
  27. * - followupto - (optional) - address(es) to follow up to
  28. * - extra - (optional) - extra tags for the href link
  29. * </pre>
  30. * Examples:
  31. * <pre>
  32. * {mailto address="me@domain.com"}
  33. * {mailto address="me@domain.com" encode="javascript"}
  34. * {mailto address="me@domain.com" encode="hex"}
  35. * {mailto address="me@domain.com" subject="Hello to you!"}
  36. * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
  37. * {mailto address="me@domain.com" extra='class="mailto"'}
  38. * </pre>
  39. *
  40. * @link http://www.smarty.net/manual/en/language.function.mailto.php {mailto}
  41. * (Smarty online manual)
  42. * @version 1.2
  43. * @author Monte Ohrt <monte at ohrt dot com>
  44. * @author credits to Jason Sweat (added cc, bcc and subject functionality)
  45. *
  46. * @param array $params parameters
  47. *
  48. * @return string
  49. */
  50. function smarty_function_mailto($params)
  51. {
  52. static $_allowed_encoding = array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true);
  53. $extra = '';
  54. if (empty($params['address'])) {
  55. trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
  56. return;
  57. } else {
  58. $address = $params['address'];
  59. }
  60. $text = $address;
  61. // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
  62. // so, don't encode it.
  63. $search = array('%40', '%2C');
  64. $replace = array('@', ',');
  65. $mail_parms = array();
  66. foreach ($params as $var => $value) {
  67. switch ($var) {
  68. case 'cc':
  69. case 'bcc':
  70. case 'followupto':
  71. if (!empty($value)) {
  72. $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
  73. }
  74. break;
  75. case 'subject':
  76. case 'newsgroups':
  77. $mail_parms[] = $var . '=' . rawurlencode($value);
  78. break;
  79. case 'extra':
  80. case 'text':
  81. $$var = $value;
  82. default:
  83. }
  84. }
  85. if ($mail_parms) {
  86. $address .= '?' . join('&', $mail_parms);
  87. }
  88. $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
  89. if (!isset($_allowed_encoding[$encode])) {
  90. trigger_error("mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex", E_USER_WARNING);
  91. return;
  92. }
  93. // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
  94. if ($encode == 'javascript') {
  95. $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
  96. $js_encode = '';
  97. for ($x = 0, $_length = strlen($string); $x < $_length; $x ++) {
  98. $js_encode .= '%' . bin2hex($string[$x]);
  99. }
  100. return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
  101. } elseif ($encode == 'javascript_charcode') {
  102. $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  103. for ($x = 0, $y = strlen($string); $x < $y; $x ++) {
  104. $ord[] = ord($string[$x]);
  105. }
  106. $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"
  107. . "{document.write(String.fromCharCode("
  108. . implode(',', $ord)
  109. . "))"
  110. . "}\n"
  111. . "</script>\n";
  112. return $_ret;
  113. } elseif ($encode == 'hex') {
  114. preg_match('!^(.*)(\?.*)$!', $address, $match);
  115. if (!empty($match[2])) {
  116. trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
  117. return;
  118. }
  119. $address_encode = '';
  120. for ($x = 0, $_length = strlen($address); $x < $_length; $x ++) {
  121. if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[$x])) {
  122. $address_encode .= '%' . bin2hex($address[$x]);
  123. } else {
  124. $address_encode .= $address[$x];
  125. }
  126. }
  127. $text_encode = '';
  128. for ($x = 0, $_length = strlen($text); $x < $_length; $x ++) {
  129. $text_encode .= '&#x' . bin2hex($text[$x]) . ';';
  130. }
  131. $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
  132. return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
  133. } else {
  134. // no encoding
  135. return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  136. }
  137. }