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

Last change on this file since 1876 was 1828, checked in by rub, 17 years ago
  • reduced size of clear/wipi theme.css by combining selectors with identical

content

  • don't send comment/user notification to admin/adviser
File size: 12.4 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/**
200 * sends an email, using PhpWebGallery specific informations
201 *
202 * @param:
203 *   - to: Receiver, or receivers of the mail.
204 *   - args: function params of mail function:
205 *       o from: sender [default value webmaster email]
206 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
207 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
208 *       o subject  [default value 'PhpWebGallery']
209 *       o content: content of mail    [default value '']
210 *       o content_format: format of mail content  [default value 'text/plain']
211 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
212 *       o template: template to use [default $conf['default_template']]
213 *       o theme: template to use [default $conf['default_template']]
214 */
215function pwg_mail($to, $args = array())
216{
217  global $conf, $conf_mail, $lang_info, $page;
218
219  if (!isset($conf_mail))
220  {
221    $conf_mail = get_mail_configuration();
222  }
223
224  if (empty($args['email_format']))
225  {
226    $args['email_format'] = $conf_mail['default_email_format'];
227  }
228
229  // Compute root_path in order have complete path
230  if ($args['email_format'] == 'text/html')
231  {
232    set_make_full_url();
233  }
234
235  $to = format_email('', $to);
236
237  if (empty($args['from']))
238  {
239    $args['from'] = $conf_mail['formated_email_webmaster'];
240  }
241  else
242  {
243    $args['from'] = format_email('', $args['from']);
244  }
245
246  if (empty($args['subject']))
247  {
248    $args['subject'] = 'PhpWebGallery';
249  }
250  $cvt7b_subject = str_translate_to_ascii7bits($args['subject']);
251
252  if (!isset($args['content']))
253  {
254    $args['content'] = '';
255  }
256
257  if (empty($args['content_format']))
258  {
259    $args['content_format'] = 'text/plain';
260  }
261
262  if ($conf_mail['send_bcc_mail_webmaster'])
263  {
264    $args['Bcc'][] = $conf_mail['formated_email_webmaster'];
265  }
266
267  if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain'))
268  {
269    // Todo find function to convert html text to plain text
270    return false;
271  }
272
273  $args = array_merge($args, get_array_template_theme($args));
274
275  $headers = 'From: '.$args['from']."\n";
276  $headers.= 'Reply-To: '.$args['from']."\n";
277
278  if (!empty($args['Cc']))
279  {
280    $headers.= 'Cc: '.implode(',', $args['Cc'])."\n";
281  }
282
283  if (!empty($args['Bcc']))
284  {
285    $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n";
286  }
287
288  $headers.= 'Content-Type: multipart/alternative;'."\n";
289  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
290  $headers.= '  reply-type=original'."\n";
291  $headers.= 'MIME-Version: 1.0'."\n";
292
293  $content = '';
294
295  if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']]))
296  {
297    if (!isset($mail_template))
298    {
299      $mail_template = get_mail_template($args['email_format']);
300    }
301
302    $mail_template->set_filename('mail_header', 'header.tpl');
303    $mail_template->set_filename('mail_footer', 'footer.tpl');
304
305    $mail_template->assign_vars(
306      array(
307        //Header
308        'BOUNDARY_KEY' => $conf_mail['boundary_key'],
309        'CONTENT_TYPE' => $args['email_format'],
310        'CONTENT_ENCODING' => $lang_info['charset'],
311        'LANG' => $lang_info['code'],
312        'DIR' => $lang_info['direction'],
313       
314        // Footer
315        'GALLERY_URL' =>
316          isset($page['gallery_url']) ?
317                $page['gallery_url'] : $conf['gallery_url'],
318        'GALLERY_TITLE' =>
319          isset($page['gallery_title']) ?
320                $page['gallery_title'] : $conf['gallery_title'],
321        'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
322        'PHPWG_URL' => PHPWG_URL,
323
324        'TITLE_MAIL' => urlencode(l10n('title_send_mail')),
325        'MAIL' => get_webmaster_mail_address()
326        ));
327
328    if ($args['email_format'] == 'text/html')
329    {
330      $old_root = $mail_template->root;
331
332      if (is_file($mail_template->root.'/global-mail-css.tpl'))
333      {
334        $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl');
335        $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css');
336      }
337
338      $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme'];
339      if (is_file($mail_template->root.'/mail-css.tpl'))
340      {
341        $mail_template->set_filename('mail_css', 'mail-css.tpl');
342        $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css');
343      }
344
345      $mail_template->root = PHPWG_ROOT_PATH.'template-common';
346      if (is_file($mail_template->root.'/local-mail-css.tpl'))
347      {
348        $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl');
349        $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css');
350      }
351
352      $mail_template->root = $old_root;
353    }
354
355    // what are displayed on the header of each mail ?
356    $conf_mail[$args['email_format']]
357      [$lang_info['charset']]
358      [$args['template']][$args['theme']]['header'] =
359        $mail_template->parse('mail_header', true);
360
361    // what are displayed on the footer of each mail ?
362    $conf_mail[$args['email_format']]
363      [$lang_info['charset']]
364      [$args['template']][$args['theme']]['footer'] =
365        $mail_template->parse('mail_footer', true);
366  }
367
368  // Header
369  $content.= $conf_mail[$args['email_format']]
370              [$lang_info['charset']]
371              [$args['template']][$args['theme']]['header'];
372
373  // Content
374  if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html'))
375  {
376    $content.= '<p>'.nl2br(htmlentities($args['content'])).'</p>';
377  }
378  else
379  {
380    $content.= $args['content'];
381  }
382
383  // Footer
384  $content.= $conf_mail[$args['email_format']]
385              [$lang_info['charset']]
386              [$args['template']][$args['theme']]['footer'];
387
388  // Close boundary
389  $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
390
391   // Undo Compute root_path in order have complete path
392  if ($args['email_format'] == 'text/html')
393  {
394    unset_make_full_url();
395  }
396
397  /*Testing block
398  {
399    global $user;
400    @mkdir(PHPWG_ROOT_PATH.'testmail');
401    $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username'];
402    if ($args['content_format'] == 'text/plain')
403    {
404      $filename .= '.txt';
405    }
406    else
407    {
408      $filename .= '.html';
409    }
410    $file = fopen($filename, 'w+');
411    fwrite($file, $content);
412    fclose($file);
413    return true;
414  }*/
415
416  if ($conf_mail['mail_options'])
417  {
418    $options = '-f '.$conf_mail['email_webmaster'];
419   
420    return mail($to, $cvt7b_subject, $content, $headers, $options);
421  }
422  else
423  {
424    return mail($to, $cvt7b_subject, $content, $headers);
425  }
426}
427
428?>
Note: See TracBrowser for help on using the repository browser.