source: trunk/include/functions_mail.inc.php @ 1901

Last change on this file since 1901 was 1901, checked in by rub, 18 years ago

Add notification when a picture is loaded.
Change little translations.
Improve mail sent.

File size: 12.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// | Copyright (C) 2006-2007 Ruben ARNAUD - team@phpwebgallery.net         |
7// +-----------------------------------------------------------------------+
8// | branch        : BSF (Best So Far)
9// | file          : $RCSfile$
10// | last update   : $Date: 2005-11-26 21:15:50 +0100 (sam., 26 nov. 2005) $
11// | last modifier : $Author: plg $
12// | revision      : $Revision: 958 $
13// +-----------------------------------------------------------------------+
14// | This program is free software; you can redistribute it and/or modify  |
15// | it under the terms of the GNU General Public License as published by  |
16// | the Free Software Foundation                                          |
17// |                                                                       |
18// | This program is distributed in the hope that it will be useful, but   |
19// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
20// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
21// | General Public License for more details.                              |
22// |                                                                       |
23// | You should have received a copy of the GNU General Public License     |
24// | along with this program; if not, write to the Free Software           |
25// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
26// | USA.                                                                  |
27// +-----------------------------------------------------------------------+
28
29// +-----------------------------------------------------------------------+
30// |                               functions                               |
31// +-----------------------------------------------------------------------+
32
33/*
34 * Returns an array of mail configuration parameters :
35 *
36 * - mail_options: see $conf['mail_options']
37 * - send_bcc_mail_webmaster: see $conf['send_bcc_mail_webmaster']
38 * - email_webmaster: mail corresponding to $conf['webmaster_id']
39 * - formated_email_webmaster: the name of webmaster is $conf['gallery_title']
40 * - text_footer: PhpWebGallery and version
41 *
42 * @return array
43 */
44function get_mail_configuration()
45{
46  global $conf;
47
48  $conf_mail = array(
49    'mail_options' => $conf['mail_options'],
50    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
51    'default_email_format' => $conf['default_email_format']
52    );
53
54  // we have webmaster id among user list, what's his email address ?
55  $conf_mail['email_webmaster'] = get_webmaster_mail_address();
56
57  // name of the webmaster is the title of the gallery
58  $conf_mail['formated_email_webmaster'] =
59    format_email($conf['gallery_title'], $conf_mail['email_webmaster']);
60
61  $conf_mail['boundary_key'] = generate_key(32);
62
63  return $conf_mail;
64}
65
66/**
67 * Returns an email address with an associated real name
68 *
69 * @param string name
70 * @param string email
71 */
72function format_email($name, $email)
73{
74  global $conf;
75
76  if ($conf['enabled_format_email'])
77  {
78    $cvt7b_name = str_translate_to_ascii7bits($name);
79
80    if (strpos($email, '<') === false)
81    {
82      return $cvt7b_name.' <'.$email.'>';
83    }
84    else
85    {
86      return $cvt7b_name.$email;
87    }
88  }
89  else
90  {
91    return $email;
92  }
93}
94
95/**
96 * Returns an completed array template/theme
97 * completed with $conf['default_template']
98 *
99 * @params:
100 *   - args: incompleted array of template/theme
101 *       o template: template to use [default $conf['default_template']]
102 *       o theme: template to use [default $conf['default_template']]
103 */
104function get_array_template_theme($args = array())
105{
106  global $conf;
107
108  $res = array();
109 
110  if (empty($args['template']) or empty($args['theme']))
111  {
112    list($res['template'], $res['theme']) = explode('/', $conf['default_template']);
113  }
114
115  if (!empty($args['template']))
116  {
117    $res['template'] = $args['template'];
118  }
119
120  if (!empty($args['theme']))
121  {
122    $res['theme'] = $args['theme'];
123  }
124
125  return $res;
126}
127
128/**
129 * Return an new mail template
130 *
131 * @params:
132 *   - email_format: mail format
133 *   - args: function params of mail function:
134 *       o template: template to use [default $conf['default_template']]
135 *       o theme: template to use [default $conf['default_template']]
136 */
137function get_mail_template($email_format, $args = array())
138{
139  $args = get_array_template_theme($args);
140
141  $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']);
142  $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format);
143
144  return $mail_template;
145}
146
147/**
148 * Return string email format (html or not)
149 *
150 * @param string format
151 */
152function get_str_email_format($is_html)
153{
154  return ($is_html ? 'text/html' : 'text/plain');
155}
156
157/**
158 * Returns email of all administrator
159 *
160 * @return string
161 */
162function get_administrators_email()
163{
164  global $conf;
165
166  $result = array();
167
168  $query = '
169select
170  U.'.$conf['user_fields']['username'].' as username,
171  U.'.$conf['user_fields']['email'].' as mail_address
172from
173  '.USERS_TABLE.' as U,
174  '.USER_INFOS_TABLE.' as I
175where
176  I.user_id =  U.'.$conf['user_fields']['id'].' and
177  I.status in (\'webmaster\',  \'admin\') and
178  I.adviser = \'false\' and
179  '.$conf['user_fields']['email'].' is not null
180order by
181  username
182';
183
184  $datas = pwg_query($query);
185  if (!empty($datas))
186  {
187    while ($admin = mysql_fetch_array($datas))
188    {
189      if (!empty($admin['mail_address']))
190      {
191        array_push($result, format_email($admin['username'], $admin['mail_address']));
192      }
193    }
194  }
195
196  return $result;
197}
198
199/* Return a standard block useful for admin mail */
200function get_block_mail_admin_info()
201{
202  global $user;
203
204  return 
205    "\n"
206    .'Connected user: '.$user['username']."\n"
207    .'IP: '.$_SERVER['REMOTE_ADDR']."\n"
208    .'Browser: '.$_SERVER['HTTP_USER_AGENT']."\n"
209    ."\n";
210}
211
212/**
213 * sends an email, using PhpWebGallery specific informations
214 *
215 * @param:
216 *   - to: Receiver, or receivers of the mail.
217 *   - args: function params of mail function:
218 *       o from: sender [default value webmaster email]
219 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
220 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
221 *       o subject  [default value 'PhpWebGallery']
222 *       o content: content of mail    [default value '']
223 *       o content_format: format of mail content  [default value 'text/plain']
224 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
225 *       o template: template to use [default $conf['default_template']]
226 *       o theme: template to use [default $conf['default_template']]
227 */
228function pwg_mail($to, $args = array())
229{
230  global $conf, $conf_mail, $lang_info, $page;
231
232  if (!isset($conf_mail))
233  {
234    $conf_mail = get_mail_configuration();
235  }
236
237  if (empty($args['email_format']))
238  {
239    $args['email_format'] = $conf_mail['default_email_format'];
240  }
241
242  // Compute root_path in order have complete path
243  if ($args['email_format'] == 'text/html')
244  {
245    set_make_full_url();
246  }
247
248  $to = format_email('', $to);
249
250  if (empty($args['from']))
251  {
252    $args['from'] = $conf_mail['formated_email_webmaster'];
253  }
254  else
255  {
256    $args['from'] = format_email('', $args['from']);
257  }
258
259  if (empty($args['subject']))
260  {
261    $args['subject'] = 'PhpWebGallery';
262  }
263  $cvt7b_subject = str_translate_to_ascii7bits($args['subject']);
264
265  if (!isset($args['content']))
266  {
267    $args['content'] = '';
268  }
269
270  if (empty($args['content_format']))
271  {
272    $args['content_format'] = 'text/plain';
273  }
274
275  if ($conf_mail['send_bcc_mail_webmaster'])
276  {
277    $args['Bcc'][] = $conf_mail['formated_email_webmaster'];
278  }
279
280  if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain'))
281  {
282    // Todo find function to convert html text to plain text
283    return false;
284  }
285
286  $args = array_merge($args, get_array_template_theme($args));
287
288  $headers = 'From: '.$args['from']."\n";
289  $headers.= 'Reply-To: '.$args['from']."\n";
290
291  if (!empty($args['Cc']))
292  {
293    $headers.= 'Cc: '.implode(',', $args['Cc'])."\n";
294  }
295
296  if (!empty($args['Bcc']))
297  {
298    $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n";
299  }
300
301  $headers.= 'Content-Type: multipart/alternative;'."\n";
302  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
303  $headers.= '  reply-type=original'."\n";
304  $headers.= 'MIME-Version: 1.0'."\n";
305
306  $content = '';
307
308  if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']]))
309  {
310    if (!isset($mail_template))
311    {
312      $mail_template = get_mail_template($args['email_format']);
313    }
314
315    $mail_template->set_filename('mail_header', 'header.tpl');
316    $mail_template->set_filename('mail_footer', 'footer.tpl');
317
318    $mail_template->assign_vars(
319      array(
320        //Header
321        'BOUNDARY_KEY' => $conf_mail['boundary_key'],
322        'CONTENT_TYPE' => $args['email_format'],
323        'CONTENT_ENCODING' => $lang_info['charset'],
324        'LANG' => $lang_info['code'],
325        'DIR' => $lang_info['direction'],
326       
327        // Footer
328        'GALLERY_URL' =>
329          isset($page['gallery_url']) ?
330                $page['gallery_url'] : $conf['gallery_url'],
331        'GALLERY_TITLE' =>
332          isset($page['gallery_title']) ?
333                $page['gallery_title'] : $conf['gallery_title'],
334        'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
335        'PHPWG_URL' => PHPWG_URL,
336
337        'TITLE_MAIL' => urlencode(l10n('title_send_mail')),
338        'MAIL' => get_webmaster_mail_address()
339        ));
340
341    if ($args['email_format'] == 'text/html')
342    {
343      $old_root = $mail_template->root;
344
345      if (is_file($mail_template->root.'/global-mail-css.tpl'))
346      {
347        $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl');
348        $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css');
349      }
350
351      $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme'];
352      if (is_file($mail_template->root.'/mail-css.tpl'))
353      {
354        $mail_template->set_filename('mail_css', 'mail-css.tpl');
355        $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css');
356      }
357
358      $mail_template->root = PHPWG_ROOT_PATH.'template-common';
359      if (is_file($mail_template->root.'/local-mail-css.tpl'))
360      {
361        $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl');
362        $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css');
363      }
364
365      $mail_template->root = $old_root;
366    }
367
368    // what are displayed on the header of each mail ?
369    $conf_mail[$args['email_format']]
370      [$lang_info['charset']]
371      [$args['template']][$args['theme']]['header'] =
372        $mail_template->parse('mail_header', true);
373
374    // what are displayed on the footer of each mail ?
375    $conf_mail[$args['email_format']]
376      [$lang_info['charset']]
377      [$args['template']][$args['theme']]['footer'] =
378        $mail_template->parse('mail_footer', true);
379  }
380
381  // Header
382  $content.= $conf_mail[$args['email_format']]
383              [$lang_info['charset']]
384              [$args['template']][$args['theme']]['header'];
385
386  // Content
387  if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html'))
388  {
389    $content.= '<p>'.
390                nl2br(
391                  preg_replace("/(http:\/\/)([^\s,]*)/i",
392                               "<a href='$1$2'>$1$2</a>",
393                               htmlentities($args['content']))).
394                '</p>';
395  }
396  else
397  {
398    $content.= $args['content'];
399  }
400
401  // Footer
402  $content.= $conf_mail[$args['email_format']]
403              [$lang_info['charset']]
404              [$args['template']][$args['theme']]['footer'];
405
406  // Close boundary
407  $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
408
409   // Undo Compute root_path in order have complete path
410  if ($args['email_format'] == 'text/html')
411  {
412    unset_make_full_url();
413  }
414
415  /*Testing block
416  {
417    global $user;
418    @mkdir(PHPWG_ROOT_PATH.'testmail');
419    $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username'];
420    if ($args['content_format'] == 'text/plain')
421    {
422      $filename .= '.txt';
423    }
424    else
425    {
426      $filename .= '.html';
427    }
428    $file = fopen($filename, 'w+');
429    fwrite($file, $content);
430    fclose($file);
431    return true;
432  }*/
433
434  if ($conf_mail['mail_options'])
435  {
436    $options = '-f '.$conf_mail['email_webmaster'];
437   
438    return mail($to, $cvt7b_subject, $content, $headers, $options);
439  }
440  else
441  {
442    return mail($to, $cvt7b_subject, $content, $headers);
443  }
444}
445
446?>
Note: See TracBrowser for help on using the repository browser.