function.mailto.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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
  15. * encode them.<br>
  16. *
  17. * Examples:
  18. * <pre>
  19. * {mailto address="me@domain.com"}
  20. * {mailto address="me@domain.com" encode="javascript"}
  21. * {mailto address="me@domain.com" encode="hex"}
  22. * {mailto address="me@domain.com" subject="Hello to you!"}
  23. * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
  24. * {mailto address="me@domain.com" extra='class="mailto"'}
  25. * </pre>
  26. *
  27. * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto}
  28. (Smarty online manual)
  29. * @version 1.2
  30. * @author Monte Ohrt <monte at ohrt dot com>
  31. * @author credits to Jason Sweat (added cc, bcc and subject functionality)
  32. * @param array $params parameters
  33. * Input:<br>
  34. * - address = e-mail address
  35. * - text = (optional) text to display, default is address
  36. * - encode = (optional) can be one of:
  37. * * none : no encoding (default)
  38. * * javascript : encode with javascript
  39. * * javascript_charcode : encode with javascript charcode
  40. * * hex : encode with hexidecimal (no javascript)
  41. * - cc = (optional) address(es) to carbon copy
  42. * - bcc = (optional) address(es) to blind carbon copy
  43. * - subject = (optional) e-mail subject
  44. * - newsgroups = (optional) newsgroup(s) to post to
  45. * - followupto = (optional) address(es) to follow up to
  46. * - extra = (optional) extra tags for the href link
  47. * @param object $smarty Smarty object
  48. * @param object $template template object
  49. * @return string
  50. */
  51. function smarty_function_mailto($params, $smarty, $template)
  52. {
  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. $mail_parm_vals = '';
  85. for ($i = 0; $i < count($mail_parms); $i++) {
  86. $mail_parm_vals .= (0 == $i) ? '?' : '&';
  87. $mail_parm_vals .= $mail_parms[$i];
  88. }
  89. $address .= $mail_parm_vals;
  90. $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
  91. if (!in_array($encode, array('javascript', 'javascript_charcode', 'hex', 'none'))) {
  92. trigger_error("mailto: 'encode' parameter must be none, javascript or hex",E_USER_WARNING);
  93. return;
  94. }
  95. if ($encode == 'javascript') {
  96. $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
  97. $js_encode = '';
  98. for ($x = 0; $x < strlen($string); $x++) {
  99. $js_encode .= '%' . bin2hex($string[$x]);
  100. }
  101. return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
  102. } elseif ($encode == 'javascript_charcode') {
  103. $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  104. for($x = 0, $y = strlen($string); $x < $y; $x++) {
  105. $ord[] = ord($string[$x]);
  106. }
  107. $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
  108. $_ret .= "<!--\n";
  109. $_ret .= "{document.write(String.fromCharCode(";
  110. $_ret .= implode(',', $ord);
  111. $_ret .= "))";
  112. $_ret .= "}\n";
  113. $_ret .= "//-->\n";
  114. $_ret .= "</script>\n";
  115. return $_ret;
  116. } elseif ($encode == 'hex') {
  117. preg_match('!^(.*)(\?.*)$!', $address, $match);
  118. if (!empty($match[2])) {
  119. trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.",E_USER_WARNING);
  120. return;
  121. }
  122. $address_encode = '';
  123. for ($x = 0; $x < strlen($address); $x++) {
  124. if (preg_match('!\w!', $address[$x])) {
  125. $address_encode .= '%' . bin2hex($address[$x]);
  126. } else {
  127. $address_encode .= $address[$x];
  128. }
  129. }
  130. $text_encode = '';
  131. for ($x = 0; $x < strlen($text); $x++) {
  132. $text_encode .= '&#x' . bin2hex($text[$x]) . ';';
  133. }
  134. $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
  135. return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
  136. } else {
  137. // no encoding
  138. return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
  139. }
  140. }
  141. ?>