source: extensions/Mail_supervisor/mail_send.inc.php @ 31988

Last change on this file since 31988 was 19018, checked in by cljosse, 11 years ago

[extensions] mail_superviseur fix compatibility with contactform (option text/html)

File size: 16.4 KB
Line 
1<?php
2
3class clj {
4static public function send_mail($result, $mailto, $subject, $content, $headers, $args) { 
5global $conf, $conf_mail, $lang_info, $page;
6  global  $user;
7  global $mails_options,$mails_donnees ;
8  global  $template,$erreur_message,$infos_message;
9  global $errors;
10  $result=true;
11
12  include_once (MAIL_SUPERV_PATH.'include/fonctions.php');
13  load_language('plugin.lang', MAIL_SUPERV_PATH);
14   $clj = new clj();
15    $infos=  $clj->pwg_mail($mailto,$args) ;   
16    $mailto=$infos->mailto;
17    $subject=$infos->subject;
18    $content=$infos->content;
19    $headers=$infos->headers;
20    $result=$infos->result;
21    $args=$infos->args;
22    $nb_destinataires=$infos->nb_destinataires;
23
24$ret =clj_send_mail($result, $mailto, $subject, $content, $headers, $args,$nb_destinataires);
25return  $ret;
26
27//================================
28// Photographie
29//================================
30    $ret="false"; 
31     $message ="";
32
33      ob_start();
34      $ret = mail($mailto, $subject,$content, $headers); 
35      $message = ob_get_contents();
36      ob_end_clean();   
37      Gestion_erreurs($message,$ret,$nb_destinataires,$headers,$mailto, $subject,$content);
38
39 
40     return  $ret;
41 return true ;
42
43
44//===========================
45//return false; // envoie
46//return true ; // pas d'envoie
47//===========================
48}
49/************************************
50*
51*************************************/
52static public function send_mail_to( $to){
53 
54
55 
56return $to;
57}
58/************************************
59*
60*************************************/
61static public function send_mail_subject($cvt_subject){
62global $cl_cvt_subject;
63$cl_cvt_subject=$cvt_subject;
64return $cvt_subject;
65}
66/************************************
67*
68*************************************/
69static public function send_mail_content($content){
70global $myclContent,$cl_externe_content;
71$myclContent=$content ;
72$cl_externe_content=true ;
73return $content;
74}
75/************************************
76*
77*************************************/
78static public function send_mail_headers($headers_init){
79
80 
81
82return $headers_init;
83}
84
85/*
86 * sends an email, using Piwigo specific informations
87 *
88 * @param:
89 *   - to: receiver(s) of the mail (list separated by comma).
90 *   - args: function params of mail function:
91 *       o from: sender [default value webmaster email]
92 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
93 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
94 *       o subject  [default value 'Piwigo']
95 *       o content: content of mail    [default value '']
96 *       o content_format: format of mail content  [default value 'text/plain']
97 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
98 *       o theme: template to use [default get_default_theme()]
99 *
100 * @return boolean (Ok or not)
101 * pour mise au point .
102 * http://www.faqs.org/rfcs/rfc2822.html
103 */
104function pwg_mail($to, $args = array()){
105  global $conf, $conf_mail, $lang_info, $page;
106  global $mails_options ;
107     remove_event_handler('send_mail', 'pwg_send_mail');
108
109  if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) {
110    $retour->mailto= "";
111    $retour->subject= "";
112    $retour->content= "";
113    $retour->headers=  "";
114    $retour->args= $args ;     
115    $retour->nb_destinataires=0  ; 
116    $retour->$result=true  ; 
117    return $retour ;     
118  }
119
120  if (!isset($conf_mail))  {
121    $conf_mail = get_mail_configuration();
122  }
123  $args['email_format']="text/html";
124
125  if (empty($args['email_format'])) {
126    $args['email_format'] = $conf_mail['default_email_format'];
127  }
128  // Compute root_path in order have complete path
129  set_make_full_url();
130  if (empty($args['from']))  {
131    $args['from'] = $conf_mail['formated_email_webmaster'];
132  }  else  {
133    $args['from'] = format_email('', $args['from']);
134  }
135  if (empty($args['subject']))  {
136    $args['subject'] = 'Piwigo';
137  }
138
139  global $cl_cvt_subject;
140  if($cl_cvt_subject)
141$args['subject']=$cl_cvt_subject;
142
143  // Spring cleaning
144  $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
145  // Ascii convertion
146  $cvt_subject = encode_mime_header($cvt_subject); 
147
148 
149
150  if (empty($args['content_format']))  {
151    $args['content_format'] = 'text/plain';
152  } 
153
154  if ($conf_mail['send_bcc_mail_webmaster'] || $mails_options['check_send_webmaster']=="on")  {
155    $args['Bcc'][] = $conf_mail['formated_email_webmaster'];   
156 }
157    $webmaster = $conf_mail['formated_email_webmaster'];   
158//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159    $Cc="";$Bcc="";
160   if (empty($mailto)) $mailto="";
161 $nb_destinataires=0;
162if( isset($args['Bcc'])){ 
163  $nb_destinataires=count($args['Bcc']);
164  $Bcc=  implode(',', $args['Bcc']) ;
165}
166if( isset($args['Cc'])){ 
167  $nb_destinataires +=count($args['Cc']);
168  $Cc= implode(',', $args['Cc']) ;
169}
170if(!empty($to)) $nb_destinataires +=count(explode(",",$to));
171
172
173if($nb_destinataires==1 && empty($to)){
174    if (isset($args['Bcc']) && count($args['Bcc'])==1) $to = array_pop($args['Bcc']);
175    if (isset($args['Cc']) && count($args['Cc'])==1){ $to = array_pop($args['Cc']);
176  }
177}
178$mailto=$to ;
179//=============================================
180// Test Si Mailto vide
181//=============================================
182if ($mails_options['check_mailto'] == 'on' && empty($mailto) ) { 
183    //=== suppression webmaster adresse dans les copies .
184    $match_email = $webmaster;
185    $new_args=array();
186    if( isset($args['Bcc']))
187      foreach($args['Bcc'] as $key => $val){
188        if (!preg_match('/'. $conf_mail['email_webmaster'] .'/', $val)){
189            array_push($new_args, $val);
190        }
191      }
192    $args['Bcc']=$new_args;
193    //=======================================================================
194    $new_args=array();
195    if( isset($args['Cc']))
196      foreach($args['Cc'] as $key => $val){
197        if (!preg_match('/'. $conf_mail['email_webmaster'] .'/', $val)){
198            array_push($new_args, $val);
199        }
200      }
201    $args['Cc']=$new_args;
202    $nb_destinataires =count($args['Bcc'])+count($args['Cc']) ;
203    //=======================================================================   
204    $mailto = $webmaster;
205    $nb_destinataires++;
206}
207  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
208
209
210  if (empty($args['theme']))  {
211    $args['theme'] = get_default_theme();
212  }
213  //============================================
214  // TEST EOL
215  //
216  //===========================================
217  //    Note:
218  // Si le message n'est pas reçu, essayez d'utiliser uniquement un caractère LF (\n).
219  // Quelques agents de transfert de mail Unix de mauvaise qualité remplacent le caractère LF
220  // par le caractère CRLF automatiquement
221  //(ce qui revient à doubler le caractère CR si le caractère CRLF est utilisé).
222  // Ceci doit être un dernier recours car cela ne correspond pas à la » RFC 2822.
223      //======================================================
224      // drop LF barre 
225      //======================================================
226      $Eol="\n"; 
227      $pattern="/free.fr|club/";
228       if(  preg_match($pattern, $mailto) ||  preg_match($pattern, $Cc) ||  preg_match($pattern, $Bcc)){
229         $Eol="\r\n"; 
230       } 
231//====================================================
232// Headers
233//====================================================
234  $headers = 'From: '.$args['from'].$Eol;
235  $headers.= 'Reply-To: '.$args['from'].$Eol;
236    //=============================================================================
237    // Note:
238    // L'implémentation de la fonction mail() sous Windows diffère de manière significative avec l'implémentation
239    // Unix.
240    // Tout d'abord, il n'utilise pas un programme local pour composer les messages,
241    // mais opère uniquement et directement sur les sockets,
242    // ce qui signifie qu'un MTA est nécessairement à l'écoute sur un socket du réseau
243    // (qui peut être soit sur le réseau local ou sur une machine distante).
244    //
245    // Deuxièmement, les en-têtes personnalisés comme From:, Cc:, Bcc: et Date: ne sont pas interprétés
246    // par le MTA dans un premier temps, mais sont analysés par PHP.
247    //
248    // De plus, le paramètre to ne doit pas être une adresse sous la forme
249    // "Quelque chose <quelqu_un@example.com>".
250    // La commande mail n'analysera pas correctement ceci lors de la discussion avec le MTA.
251    //=============================================================================
252    $Cc="";$Bcc="";
253   if (!empty($args['Cc']))  {
254     $Cc=implode(",",$args['Cc']);
255     if ($mails_options['check_header_carbon'] == 'on') $Cc=  get_strict_email_list( $Cc); 
256        $headers.= 'Cc: '.$Cc.$Eol;
257        $args['Cc']=explode(", ",$Cc);
258    }
259    if (!empty($args['Bcc']))  {
260     $Bcc=implode(",",$args['Bcc']);
261      if ($mails_options['check_header_carbon'] == 'on') $Bcc=  get_strict_email_list( $Bcc);
262        $headers.= 'Bcc: '.$Bcc.$Eol;
263        $args['Bcc']=explode(", ",$Bcc);
264    }
265    if (!empty($mailto))  {
266      if ($mails_options['check_header_carbon'] == 'on') $mailto=  get_strict_email_list($mailto);
267       
268    }
269   
270  //=====================================
271  // List on content-type test si doublé
272  //=====================================
273  $content_type_list[] = $args['email_format'];
274  if (!empty($conf_mail['alternative_email_format'])  &&
275  $conf_mail['alternative_email_format']!= $args['email_format']  )  {
276      $content_type_list[] = $conf_mail['alternative_email_format'];
277      $headers.= 'Content-Type: multipart/alternative;'.$Eol;
278
279  }else{
280   //   $headers.= 'Content-Type: '.$args['email_format'].';'.$Eol; 
281   $headers .= 'Content-type: '.$args['email_format'].'; charset='.get_pwg_charset(). ' ' .$Eol;
282 } 
283  //=====================================
284  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'.$Eol;
285  $headers.= '  reply-type=original'.$Eol;
286  $headers.= 'MIME-Version: 1.0'.$Eol;
287  $headers.= 'X-Mailer: Piwigo Mailer'.$Eol;
288  //=====================================
289  $content = '';
290  foreach (array_unique($content_type_list) as $content_type)  {
291    // key compose of indexes witch allow ti cache mail data
292    $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme'];
293    if (!isset($conf_mail[$cache_key]))    {
294        $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']);
295
296      $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl');
297      $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl');   
298     
299      $conf_mail[$cache_key]['theme']->assign(
300        array(
301          //Header
302          'BOUNDARY_KEY' => $conf_mail['boundary_key'],
303          'CONTENT_TYPE' => $content_type,
304          'CONTENT_ENCODING' => get_pwg_charset(),
305         
306          // Footer
307          'GALLERY_URL' => get_gallery_home_url(),
308          'GALLERY_TITLE' =>
309            isset($page['gallery_title']) ?
310                  $page['gallery_title'] : $conf['gallery_title'],
311          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
312          'PHPWG_URL' => PHPWG_URL,
313
314          'TITLE_MAIL' => urlencode(l10n('A comment on your site')),
315          'MAIL' => get_webmaster_mail_address()
316          ));
317
318      if ($content_type == 'text/html')     {
319        if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl'))
320        {
321          $conf_mail[$cache_key]['theme']->set_filename('css', 'global-mail-css.tpl');
322          $conf_mail[$cache_key]['theme']->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css');
323        }
324
325        $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl';
326        if (is_file($file))   {
327          $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file));
328          $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css');
329        }
330      }
331
332      // what are displayed on the header of each mail ?
333      $conf_mail[$cache_key]['header'] =
334        $conf_mail[$cache_key]['theme']->parse('mail_header', true);
335
336      // what are displayed on the footer of each mail ?
337      $conf_mail[$cache_key]['footer'] =
338        $conf_mail[$cache_key]['theme']->parse('mail_footer', true);
339    } 
340   
341    // Header
342     if(count($content_type_list)>1) 
343                 $content.= $conf_mail[$cache_key]['header'];
344    global $myclContent,$cl_externe_content;   
345             if (!isset($args['content']))  {
346                  $args['content'] = '';
347             if($cl_externe_content && $myclContent!="" ){
348                  $args['content'] = $myclContent;
349                  $myclContent="";
350                  if ($conf['ContactForm']['cf_mail_type'] == 'text/plain'){
351
352                      }else if ($conf['ContactForm']['cf_mail_type'] == 'text/html'){
353                      $content_type =""; 
354                                     };
355                }
356     }             
357 
358    // Content
359    if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html'))    {
360      $content.= '<p>'.
361                  nl2br(
362                    preg_replace("/(http:\/\/)([^\s,]*)/i",
363                                 "<a href='$1$2' class='thumblnk'>$1$2</a>",
364                                 htmlspecialchars($args['content']))).
365                  '</p>';
366    } else if (($args['content_format'] == 'text/html') and ($content_type == 'text/plain')) {
367      // convert html text to plain text
368      $content.= strip_tags($args['content']);
369    }    else    {
370      $content.= $args['content'];
371    }
372    if(!$cl_externe_content){
373    // Footer
374    $content.= $conf_mail[$cache_key]['footer'];
375    }
376  // Close boundary
377  if(count($content_type_list)>1)
378     $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'.$Eol;
379
380  }// fin each
381  //==================================================================
382  //~ // Close boundary
383  //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
384
385
386  //===========================================
387  //    Note:
388  //Si le message n'est pas reçu, essayez d'utiliser uniquement un caractère LF (\n).
389  // Quelques agents de transfert de mail Unix de mauvaise qualité remplacent le caractère LF
390  // par le caractère CRLF automatiquement
391  //(ce qui revient à doubler le caractère CR si le caractère CRLF est utilisé).
392  // Ceci doit être un dernier recours car cela ne correspond pas à la » RFC 2822.
393      //======================================================
394      // drop LF barre 
395      //======================================================
396      //  $pattern ="/" . preg_quote($pattern, '/')."/" ;
397      $pattern="/free.fr|club/";
398            if(  preg_match($pattern, $mailto) ||  preg_match($pattern, $headers) ){
399             $content= preg_replace('#(?<!\r)\n#si', "\r\n", $content); //drop LF barre   
400        }
401
402  //=========================================
403  // Correction pour windows
404  //=========================================
405    $content = str_replace("\n.", "\n..", $content);
406     //======================================================
407     // si adresse avec ip , certaine boite aux lettres traite ces messages
408     //  en spam (orange+windowsmail)
409     //======================================================
410      if ( $mails_options['no_ipaddress'] == 'on' ){   
411        $pattern ="/(http:\/\/)(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9])\.){1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9])\.){2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))/" ;       
412        if(preg_match($pattern, $content)){
413          $replacement = "http://localhost"; 
414          $content = preg_replace( $pattern, $replacement,  $content );     
415        }
416      }
417
418
419//===========================================
420
421
422   // Undo Compute root_path in order have complete path
423  unset_make_full_url();
424     $clj = new clj();
425 
426   if (empty($retour))
427  $retour = (object) '';
428    $retour->mailto= $mailto;
429    $retour->subject= $cvt_subject;
430    $retour->content= $content;
431    $retour->headers=  $headers;
432    $retour->args= $args ;     
433    $retour->nb_destinataires=$nb_destinataires  ; 
434    $retour->result=false  ; 
435               
436  return $retour ;
437
438}
439       
440 
441 }
442?>
Note: See TracBrowser for help on using the repository browser.