1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
<?php
/**
* Zend Framework
*
* Helper to generate mailto html
* Aide pour générer un lien mailto
*
* @category Zend
* @package Zend_View
* @subpackage Helper
*/
class My_View_Helper_Mailto extends Zend_View_Helper_Abstract {
/**
* $config est soit un array ou un Zend_Config (mailto.)
* avec les options suivantes
* 'address' string|array e-mail address - L'adresse email
* 'text' string (optional) text to display, default is address - Le texte à afficher, par défaut première adresse email
* 'cc' string|array (optional) address(es) to carbon copy - La ou Les adresses email en copie (Cc)
* 'bcc' string|array (optional) address(es) to blind carbon copy - Les adresses email en copie cachées
* 'subject' string (optional) e-mail subject - Sujet de l'email
* 'encode' string (optional) value (none | javascript | javascript_charcode) Comment encoder l'adresse email
* 'newsgroups' string|array (optional) newsgroup(s) to post to - Newsgroup où poster le message
* 'followupto' string|array (optional) address(es) to follow up to - Adresses où transmettre le message
* 'class' string|array (optional) une ou des classe css qui seront passé au lien.
* 'id' string (optional) identifiant css ID qui sera passé au lien
*/
/*
* @example
* fichier 'mail.ini'
* [production]
mailto.address = webmaster@developpez.net
mailto.cc.1 = webmaster2@developpez.net
mailto.cc.2 = webmaster3@developpez.net
mailto.text = "Envoyer un message Au Webmaster du site"
mailto.subject = "Demande d'information auprès du Webmaster"
mailto.encode = "javascript"
mailto.class = email
[development : production]
Dans la vue
<?php echo $this->mailto (new Zend_Config_Ini ('configs/mail.ini', 'development')); ?>
ou
<?php echo $this->mailto( array ( 'address' => 'webmaster@e-tuto.fr'
, 'cc' => array ( 'webmaster2@developpez.net', 'webmaster3@developpez.net' )
, 'class' => 'email'
, 'text' => 'Envoyer un message Au Webmaster du site'
, 'subject' => "Demande d'information auprès du Webmaster"
, 'encode' => 'javascript'
));
?>
*/
/**
* @param $config array|Zend_Config
* @return string html mailto
*/
public function mailto ($config) {
//
if (!is_array($config)) {
/*
* Convert Zend_Config argument to a plain array.
*/
if ($config instanceof Zend_Config) {
if (isset($config->mailto)) {
$config = $config->mailto->toArray();
}
} else {
require_once 'Zend/Exception.php';
throw new Zend_Exception('config parameters must be in an array or a Zend_Config object');
}
}
// adresse mail obligatoire
if (array_key_exists('address', $config)) {
$tabAddress = is_string($config['address']) ? array($config['address']) : $config['address'];
foreach ($tabAddress as $value) { $address .= rawurlencode($value.','); }
} else {
require_once 'Zend/Exception.php';
throw new Zend_Exception('You must supply at least one e-mail address');
}
// id
if (array_key_exists('id', $config)) {
if (is_string($config['id'])) {
$id = $config['id'];
} else {
require_once 'Zend/Exception.php';
throw new Zend_Exception('string only for id mailto');
}
}
// class
if (array_key_exists('class', $config)) {
$tabClass = is_string($config['class']) ? array($config['class']) : $config['class'];
foreach ($tabClass as $value) {
$class .= $value.' ';
}
}
// nom du lien (par défaut 1er adresse mail fournie)
if (array_key_exists('text' ,$config)) {
$text = $config['text'];
} else {
$text = $tabAddress[0];
}
// liste des paramètres
$mail_parms = array();
// netscape and mozilla do not decode %40 (@) in BCC field (bug?)
// so, don't encode it.
$search = array('%40', '%2C');
$replace = array('@', ',');
foreach ($config as $var=>$tab) {
switch ($var) {
case 'cc' :
case 'bcc':
case 'followupto':
is_string($tab) ? $tab = array ($config[$var]) : $tab;
foreach ($tab as $value) {
$mail_parms[$var] .= str_replace($search, $replace, rawurlencode($value.','));
}
$mail_parms[$var] = substr($mail_parms[$var],0 ,-1);
break;
case 'subject':
case 'newsgroups':
$mail_parms[$var] = rawurlencode($config[$var]);
default:
}
}
// concatenation des paramètres mail
$mail_parm_vals = '';
$first = true;
foreach ($mail_parms as $key => $vals) {
$mail_parm_vals .= ($first) ? '?' : '&';
$mail_parm_vals .= $key.'='.$vals;
$first = false;
}
// Chaine xHtml
$xhtml = '<a';
if (isset($id)) { $xhtml .= ' id="'. trim($id) .'"'; }
if (isset($class)) { $xhtml .= ' class="'. trim($class) .'"'; }
$xhtml .= ' href="mailto:'.$address.$mail_parm_vals;
$xhtml .= '">';
$xhtml .= $text;
$xhtml .= '</a>';
// Gestion du type d'encodage
$encode = 'none'; // par défaut
if (array_key_exists('encode', $config)) {
if (!in_array($config['encode'],array('javascript','javascript_charcode','none')) ) {
require_once 'Zend/Exception.php';
throw new Zend_Exception('You must choose an encoding following (javascript | javascript_charcode | none)');
} else {
$encode = $config['encode'];
}
}
if ($encode == 'javascript' ) {
$string = 'document.write(\''.$xhtml.'\');';
$js_encode = '';
for ($x=0; $x < strlen($string); $x++) {
$js_encode .= '%' . bin2hex($string[$x]);
}
return $mailto = '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>';
} elseif ($encode == 'javascript_charcode' ) {
for($x = 0, $y = strlen($xhtml); $x < $y; $x++ ) {
$ord[] = ord($xhtml[$x]);
}
$mailto = "<script type=\"text/javascript\" language=\"javascript\">\n";
$mailto .= "<!--\n";
$mailto .= "{document.write(String.fromCharCode(";
$mailto .= implode(',',$ord);
$mailto .= "))";
$mailto .= "}\n";
$mailto .= "//-->\n";
$mailto .= "</script>\n";
return $mailto;
} else { // Pas d'encodage
return $xhtml;
}
}//Eof:: mailto
}//Eof:: class
?> |
Partager