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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {mailto} function plugin
  10. * Type: function
  11. * Name: mailto
  12. * Date: May 21, 2002
  13. * Purpose: automate mailto address link creation, and optionally encode them.
  14. * Params:
  15. *
  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 hexadecimal (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. *
  30. * Examples:
  31. *
  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. *
  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 =
  53. array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true);
  54. $extra = '';
  55. if (empty($params[ 'address' ])) {
  56. trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
  57. return;
  58. } else {
  59. $address = $params[ 'address' ];
  60. }
  61. $text = $address;
  62. // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
  63. // so, don't encode it.
  64. $search = array('%40', '%2C');
  65. $replace = array('@', ',');
  66. $mail_parms = array();
  67. foreach ($params as $var => $value) {
  68. switch ($var) {
  69. case 'cc':
  70. case 'bcc':
  71. case 'followupto':
  72. if (!empty($value)) {
  73. $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
  74. }
  75. break;
  76. case 'subject':
  77. case 'newsgroups':
  78. $mail_parms[] = $var . '=' . rawurlencode($value);
  79. break;
  80. case 'extra':
  81. case 'text':
  82. $$var = $value;
  83. default:
  84. }
  85. }
  86. if ($mail_parms) {
  87. $address .= '?' . join('&', $mail_parms);
  88. }
  89. $encode = (empty($params[ 'encode' ])) ? 'none' : $params[ 'encode' ];
  90. if (!isset($_allowed_encoding[ $encode ])) {
  91. trigger_error("mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex",
  92. E_USER_WARNING);
  93. return;
  94. }
  95. // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
  96. if ($encode === 'javascript') {
  97. $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
  98. $js_encode = '';
  99. for ($x = 0, $_length = strlen($string); $x < $_length; $x ++) {
  100. $js_encode .= '%' . bin2hex($string[ $x ]);
  101. }
  102. return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
  103. } elseif ($encode === 'javascript_charcode') {
  104. $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  105. for ($x = 0, $y = strlen($string); $x < $y; $x ++) {
  106. $ord[] = ord($string[ $x ]);
  107. }
  108. $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n" . "{document.write(String.fromCharCode(" .
  109. implode(',', $ord) . "))" . "}\n" . "</script>\n";
  110. return $_ret;
  111. } elseif ($encode === 'hex') {
  112. preg_match('!^(.*)(\?.*)$!', $address, $match);
  113. if (!empty($match[ 2 ])) {
  114. trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
  115. return;
  116. }
  117. $address_encode = '';
  118. for ($x = 0, $_length = strlen($address); $x < $_length; $x ++) {
  119. if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[ $x ])) {
  120. $address_encode .= '%' . bin2hex($address[ $x ]);
  121. } else {
  122. $address_encode .= $address[ $x ];
  123. }
  124. }
  125. $text_encode = '';
  126. for ($x = 0, $_length = strlen($text); $x < $_length; $x ++) {
  127. $text_encode .= '&#x' . bin2hex($text[ $x ]) . ';';
  128. }
  129. $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
  130. return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
  131. } else {
  132. // no encoding
  133. return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  134. }
  135. }