function.mailto.php 5.3 KB

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