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

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

[extensions] mail_superviseur fix compatibility with contactform)

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