function.mailto.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {mailto} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: mailto<br>
  12. * Date: May 21, 2002
  13. * Purpose: automate mailto address link creation, and optionally
  14. * encode them.<br>
  15. * Input:<br>
  16. * - address = 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. *
  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. * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto}
  40. * (Smarty online manual)
  41. * @version 1.2
  42. * @author Monte Ohrt <monte at ohrt dot com>
  43. * @author credits to Jason Sweat (added cc, bcc and subject functionality)
  44. * @param array
  45. * @param Smarty
  46. * @return string
  47. */
  48. function smarty_function_mailto($params, &$smarty)
  49. {
  50. $extra = '';
  51. if (empty($params['address'])) {
  52. $smarty->trigger_error("mailto: missing 'address' parameter");
  53. return;
  54. } else {
  55. $address = $params['address'];
  56. }
  57. $text = $address;
  58. // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
  59. // so, don't encode it.
  60. $search = array('%40', '%2C');
  61. $replace = array('@', ',');
  62. $mail_parms = array();
  63. foreach ($params as $var=>$value) {
  64. switch ($var) {
  65. case 'cc':
  66. case 'bcc':
  67. case 'followupto':
  68. if (!empty($value))
  69. $mail_parms[] = $var.'='.str_replace($search,$replace,rawurlencode($value));
  70. break;
  71. case 'subject':
  72. case 'newsgroups':
  73. $mail_parms[] = $var.'='.rawurlencode($value);
  74. break;
  75. case 'extra':
  76. case 'text':
  77. $$var = $value;
  78. default:
  79. }
  80. }
  81. $mail_parm_vals = '';
  82. for ($i=0; $i<count($mail_parms); $i++) {
  83. $mail_parm_vals .= (0==$i) ? '?' : '&';
  84. $mail_parm_vals .= $mail_parms[$i];
  85. }
  86. $address .= $mail_parm_vals;
  87. $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
  88. if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) {
  89. $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex");
  90. return;
  91. }
  92. if ($encode == 'javascript' ) {
  93. $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');';
  94. $js_encode = '';
  95. for ($x=0; $x < strlen($string); $x++) {
  96. $js_encode .= '%' . bin2hex($string[$x]);
  97. }
  98. return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>';
  99. } elseif ($encode == 'javascript_charcode' ) {
  100. $string = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>';
  101. for($x = 0, $y = strlen($string); $x < $y; $x++ ) {
  102. $ord[] = ord($string[$x]);
  103. }
  104. $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
  105. $_ret .= "<!--\n";
  106. $_ret .= "{document.write(String.fromCharCode(";
  107. $_ret .= implode(',',$ord);
  108. $_ret .= "))";
  109. $_ret .= "}\n";
  110. $_ret .= "//-->\n";
  111. $_ret .= "</script>\n";
  112. return $_ret;
  113. } elseif ($encode == 'hex') {
  114. preg_match('!^(.*)(\?.*)$!',$address,$match);
  115. if(!empty($match[2])) {
  116. $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.");
  117. return;
  118. }
  119. $address_encode = '';
  120. for ($x=0; $x < strlen($address); $x++) {
  121. if(preg_match('!\w!',$address[$x])) {
  122. $address_encode .= '%' . bin2hex($address[$x]);
  123. } else {
  124. $address_encode .= $address[$x];
  125. }
  126. }
  127. $text_encode = '';
  128. for ($x=0; $x < strlen($text); $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. }
  138. /* vim: set expandtab: */
  139. ?>