source: trunk/admin/notification_by_mail.php @ 1818

Last change on this file since 1818 was 1818, checked in by rub, 17 years ago

My last improvements before 1.7.0RC1.

Can include Cc & Bcc on mail.
Send mail to all administrators on new comment or new users.
Add validate link on new comment mail.
Try to detect if the NBM complementary content is HTML or plain text. With plain text, this content is convert to readable HTML.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.6 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: 2007-02-14 22:53:04 +0000 (Wed, 14 Feb 2007) $
11// | last modifier : $Author: rub $
12// | revision      : $Revision: 1818 $
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// | include                                                               |
31// +-----------------------------------------------------------------------+
32
33if (!defined('PHPWG_ROOT_PATH'))
34{
35  die ("Hacking attempt!");
36}
37
38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
39include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
40include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
41include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
42include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
43
44// +-----------------------------------------------------------------------+
45// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49// +-----------------------------------------------------------------------+
50// | Initialization                                                        |
51// +-----------------------------------------------------------------------+
52$base_url = get_root_url().'admin.php';
53$must_repost = false;
54
55// +-----------------------------------------------------------------------+
56// | functions                                                             |
57// +-----------------------------------------------------------------------+
58
59/*
60 * Do timeout treatment in order to finish to send mails
61 *
62 * @param $post_keyname: key of check_key post array
63 * @param check_key_treated: array of check_key treated
64 * @return none
65 */
66function do_timeout_treatment($post_keyname, $check_key_treated = array())
67{
68  global $env_nbm, $base_url, $page, $must_repost;
69
70  if ($env_nbm['is_sendmail_timeout'])
71  {
72    if (isset($_POST[$post_keyname]))
73    {
74      $post_count = count($_POST[$post_keyname]);
75      $treated_count = count($check_key_treated);
76      if ($treated_count != 0)
77      {
78        $time_refresh = ceil((get_moment() - $env_nbm['start_time']) * $post_count / $treated_count);
79      }
80      else
81      {
82        $time_refresh = 0;
83      }
84      $_POST[$post_keyname] = array_diff($_POST[$post_keyname], $check_key_treated);
85
86      $must_repost = true;
87      array_push($page['errors'],
88        l10n_dec('nbm_background_treatment_redirect_second', 
89                 'nbm_background_treatment_redirect_seconds',
90                  $time_refresh));
91    }
92  }
93
94}
95
96/*
97 * Get the authorized_status for each tab
98 * return corresponding status
99 */
100function get_tab_status($mode)
101{
102  $result = ACCESS_WEBMASTER;
103  switch ($mode)
104  {
105    case 'param':
106    case 'subscribe':
107      $result = ACCESS_WEBMASTER;
108      break;
109    case 'send':
110      $result = ACCESS_ADMINISTRATOR;
111      break;
112    default:
113      $result = ACCESS_WEBMASTER;
114      break;
115  }
116  return $result;
117}
118
119/*
120 * Inserting News users
121 */
122function insert_new_data_user_mail_notification()
123{
124  global $conf, $page, $env_nbm;
125
126  // Set null mail_address empty
127  $query = '
128update
129  '.USERS_TABLE.'
130set
131  '.$conf['user_fields']['email'].' = null
132where
133  trim('.$conf['user_fields']['email'].') = \'\';';
134  pwg_query($query);
135
136  // null mail_address are not selected in the list
137  $query = '
138select
139  u.'.$conf['user_fields']['id'].' as user_id,
140  u.'.$conf['user_fields']['username'].' as username,
141  u.'.$conf['user_fields']['email'].' as mail_address
142from
143  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.'.$conf['user_fields']['id'].' = m.user_id
144where
145  u.'.$conf['user_fields']['email'].' is not null and
146  m.user_id is null
147order by
148  user_id;';
149
150  $result = pwg_query($query);
151
152  if (mysql_num_rows($result) > 0)
153  {
154    $inserts = array();
155    $check_key_list = array();
156
157    while ($nbm_user = mysql_fetch_array($result))
158    {
159      // Calculate key
160      $nbm_user['check_key'] = find_available_check_key();
161
162      // Save key
163      array_push($check_key_list, $nbm_user['check_key']);
164
165      // Insert new nbm_users
166      array_push
167      (
168        $inserts, 
169        array
170        (
171          'user_id' => $nbm_user['user_id'],
172          'check_key' => $nbm_user['check_key'],
173          'enabled' => 'false' // By default if false, set to true with specific functions
174        )
175      );
176
177      array_push
178      (
179        $page['infos'], 
180        sprintf(
181          l10n('nbm_user_x_added'), 
182          $nbm_user['username'], 
183          get_email_address_as_display_text($nbm_user['mail_address'])
184        )
185      );
186    }
187
188    // Insert new nbm_users
189    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
190    // Update field enabled with specific function
191    $check_key_treated = do_subscribe_unsubscribe_notification_by_mail
192    (
193      true,
194      $conf['nbm_default_value_user_enabled'],
195      $check_key_list
196    );
197
198     // On timeout simulate like tabsheet send
199    if ($env_nbm['is_sendmail_timeout'])
200    {
201      $quoted_check_key_list = quote_check_key_list(array_diff($check_key_list, $check_key_treated));
202      if (count($quoted_check_key_list) != 0 )
203      {
204        $query = 'delete from '.USER_MAIL_NOTIFICATION_TABLE.' where check_key in ('.implode(",", $quoted_check_key_list).');';
205        $result = pwg_query($query);
206
207        redirect($base_url.get_query_string_diff(array()), l10n('nbm_redirect_msg'));
208      }
209    }
210  }
211}
212
213/*
214 * Send mail for notification to all users
215 * Return list of "selected" users for 'list_to_send'
216 * Return list of "treated" check_key for 'send'
217 */
218function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
219{
220  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
221  $return_list = array();
222 
223  if (in_array($action, array('list_to_send', 'send')))
224  {
225    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
226
227    $is_action_send = ($action == 'send');
228
229    // disabled and null mail_address are not selected in the list
230    $data_users = get_user_notifications('send', $check_key_list);
231
232    // List all if it's define on options or on timeout
233    $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
234
235    // Check if exist news to list user or send mails
236    if ((!$is_list_all_without_test) or ($is_action_send))
237    {
238      if (count($data_users) > 0)
239      {
240        $datas = array();
241
242        if (!isset($customize_mail_content))
243        {
244          $customize_mail_content = $conf['nbm_complementary_mail_content'];
245        }
246
247        if ($conf['nbm_send_html_mail'] and !(strpos($customize_mail_content, '<') === 0))
248        {
249          // On HTML mail, detects if the content are HTML format.
250          // If it's plain text format, convert content to readable HTML
251          $customize_mail_content = nl2br(htmlentities($customize_mail_content));
252        }
253
254        // Prepare message after change language
255        if ($is_action_send)
256        {
257          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
258        }
259        else
260        {
261          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
262        }
263
264        // Begin nbm users environment
265        begin_users_env_nbm($is_action_send);
266
267        foreach ($data_users as $nbm_user)
268        {
269          if ((!$is_action_send) and check_sendmail_timeout())
270          {
271            // Stop fill list on 'list_to_send', if the quota is override
272            array_push($page['infos'], $msg_break_timeout);
273            break;
274          }
275          if (($is_action_send) and check_sendmail_timeout())
276          {
277            // Stop fill list on 'send', if the quota is override
278            array_push($page['errors'], $msg_break_timeout);
279            break;
280          }
281
282          // set env nbm user
283          set_user_on_env_nbm($nbm_user, $is_action_send);
284
285          if ($is_action_send)
286          {
287            set_make_full_url();
288            // Fill return list of "treated" check_key for 'send'
289            array_push($return_list, $nbm_user['check_key']);
290
291            if ($conf['nbm_send_detailed_content'])
292            {
293               $news = news($nbm_user['last_send'], $dbnow, false, $conf['nbm_send_html_mail']);
294               $exist_data = count($news) > 0;
295            }
296            else
297            {
298              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
299            }
300
301            if ($exist_data)
302            {
303              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
304
305              // Assign current var for nbm mail
306              assign_vars_nbm_mail_content($nbm_user);
307
308              $end_punct = ($conf['nbm_send_detailed_content'] ? ':' : '.');
309
310              if (!is_null($nbm_user['last_send']))
311              {
312                $env_nbm['mail_template']->assign_block_vars
313                (
314                  'content_new_elements_between',
315                  array
316                  (
317                    'DATE_BETWEEN_1' => $nbm_user['last_send'], 
318                    'DATE_BETWEEN_2' => $dbnow,
319                    'END_PUNCT' => $end_punct
320                  )
321                );
322              }
323              else
324              {
325                $env_nbm['mail_template']->assign_block_vars
326                (
327                  'content_new_elements_single',
328                  array
329                  (
330                    'DATE_SINGLE' => $dbnow,
331                    'END_PUNCT' => $end_punct
332                  )
333                );
334              }
335
336              if ($conf['nbm_send_detailed_content'])
337              {
338                foreach ($news as $data)
339                {
340                  $env_nbm['mail_template']->assign_block_vars
341                  (
342                    'global_new_line.new_line', array('DATA' => $data)
343                  );
344                }
345              }
346
347              if (!empty($customize_mail_content))
348              {
349                $env_nbm['mail_template']->assign_block_vars
350                (
351                  'custom', array('CUSTOMIZE_MAIL_CONTENT' => $customize_mail_content)
352                );
353              }
354
355              if ($conf['nbm_send_html_mail'] and $conf['nbm_send_recent_post_dates'])
356              {
357                $recent_post_dates = get_recent_post_dates(7, 5, 9);
358                foreach ($recent_post_dates as $date_detail)
359                {
360                  $env_nbm['mail_template']->assign_block_vars
361                  (
362                    'recent_post.recent_post_block',
363                    array
364                    (
365                      'TITLE' => get_title_recent_post_date($date_detail),
366                      'HTML_DATA' => get_html_description_recent_post_date($date_detail)
367                    )
368                  );
369                }
370              }
371
372              $env_nbm['mail_template']->assign_block_vars
373              (
374                'goto',
375                array
376                (
377                  'GALLERY_TITLE' => $conf['gallery_title'],
378                  'GALLERY_URL' => $conf['gallery_url']
379                )
380              );
381
382              $env_nbm['mail_template']->assign_block_vars
383              (
384                'byebye', array('SEND_AS_NAME' => $env_nbm['send_as_name'])
385              );
386
387              if (pwg_mail
388                  (
389                    format_email($nbm_user['username'], $nbm_user['mail_address']),
390                    array
391                    (
392                      'from' => $env_nbm['send_as_mail_formated'],
393                      'subject' => $subject,
394                      'email_format' => $env_nbm['email_format'],
395                      'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
396                      'content_format' => $env_nbm['email_format'],
397                      'template' => $nbm_user['template'],
398                      'theme' => $nbm_user['theme']
399                    )
400                  ))
401              {
402                inc_mail_sent_success($nbm_user);
403
404                $data = array('user_id' => $nbm_user['user_id'],
405                              'last_send' => $dbnow);
406                array_push($datas, $data);
407              }
408              else
409              {
410                inc_mail_sent_failed($nbm_user);
411              }
412
413              unset_make_full_url();
414            }
415          }
416          else
417          {
418            if (news_exists($nbm_user['last_send'], $dbnow))
419            {
420              // Fill return list of "selected" users for 'list_to_send'
421              array_push($return_list, $nbm_user);
422            }
423          }
424         
425          // unset env nbm user
426          unset_user_on_env_nbm();
427        }
428
429        // Restore nbm environment
430        end_users_env_nbm();
431
432        if ($is_action_send)
433        {
434          mass_updates(
435            USER_MAIL_NOTIFICATION_TABLE,
436            array(
437              'primary' => array('user_id'),
438              'update' => array('last_send')
439             ),
440             $datas
441             );
442
443          display_counter_info();
444        }
445      }
446      else
447      {
448        if ($is_action_send)
449        {
450          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
451        }
452      }
453    }
454    else
455    {
456      // Quick List, don't check news
457      // Fill return list of "selected" users for 'list_to_send'
458      $return_list = $data_users;
459    }
460  }
461
462  // Return list of "selected" users for 'list_to_send'
463  // Return list of "treated" check_key for 'send'
464  return $return_list;
465}
466
467// +-----------------------------------------------------------------------+
468// | Main                                                                  |
469// +-----------------------------------------------------------------------+
470if (!isset($_GET['mode']))
471{
472  $page['mode'] = 'send';
473}
474else
475{
476  $page['mode'] = $_GET['mode'];
477}
478
479// +-----------------------------------------------------------------------+
480// | Check Access and exit when user status is not ok                      |
481// +-----------------------------------------------------------------------+
482check_status(get_tab_status($page['mode']));
483
484// +-----------------------------------------------------------------------+
485// | Insert new users with mails                                           |
486// +-----------------------------------------------------------------------+
487if (!isset($_POST) or (count($_POST) ==0))
488{
489  // No insert data in post mode
490  insert_new_data_user_mail_notification();
491}
492
493// +-----------------------------------------------------------------------+
494// | Treatment of tab post                                                 |
495// +-----------------------------------------------------------------------+
496switch ($page['mode'])
497{
498  case 'param' :
499  {
500    if (isset($_POST['param_submit']) and !is_adviser())
501    {
502      $updated_param_count = 0;
503      // Update param
504      $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
505      while ($nbm_user = mysql_fetch_array($result))
506      {
507        if (isset($_POST[$nbm_user['param']]))
508        {
509          $value = $_POST[$nbm_user['param']];
510
511          $query = '
512update
513'.CONFIG_TABLE.'
514set
515  value = \''. str_replace("\'", "''", $value).'\'
516where
517  param = \''.$nbm_user['param'].'\';';
518          pwg_query($query);
519          $updated_param_count += 1;
520        }
521      }
522   
523      array_push($page['infos'],
524        l10n_dec('nbm_updated_param_count', 'nbm_updated_params_count',
525          $updated_param_count));
526
527      // Reload conf with new values
528      load_conf_from_db('param like \'nbm\\_%\'');
529    }
530  }
531  case 'subscribe' :
532  {
533    if (!is_adviser())
534    {
535      if (isset($_POST['falsify']) and isset($_POST['cat_true']))
536      {
537        $check_key_treated = unsubscribe_notification_by_mail(true, $_POST['cat_true']);
538        do_timeout_treatment('cat_true', $check_key_treated);
539      }
540      else
541      if (isset($_POST['trueify']) and isset($_POST['cat_false']))
542      {
543        $check_key_treated = subscribe_notification_by_mail(true, $_POST['cat_false']);
544        do_timeout_treatment('cat_false', $check_key_treated);
545      }
546    }
547    break;
548  }
549
550  case 'send' :
551  {
552    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']) and !is_adviser())
553    {
554      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], stripslashes($_POST['send_customize_mail_content']));
555      do_timeout_treatment('send_selection', $check_key_treated);
556    }
557  }
558}
559
560// +-----------------------------------------------------------------------+
561// | template initialization                                               |
562// +-----------------------------------------------------------------------+
563$template->set_filenames
564(
565  array
566  (
567    'double_select' => 'admin/double_select.tpl',
568    'notification_by_mail'=>'admin/notification_by_mail.tpl'
569  )
570);
571
572$template->assign_vars
573(
574  array
575  (
576    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
577    'U_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
578    'F_ACTION'=> $base_url.get_query_string_diff(array())
579  )
580);
581
582if (is_autorize_status(ACCESS_WEBMASTER))
583{
584  $template->assign_block_vars
585  (
586    'header_link',
587    array
588    (
589      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
590      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
591      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
592    )
593  );
594}
595
596if ($must_repost)
597{
598  // Get name of submit button
599  $repost_submit_name = '';
600  if (isset($_POST['falsify']))
601  {
602    $repost_submit_name = 'falsify';
603  }
604  elseif (isset($_POST['trueify']))
605  {
606    $repost_submit_name = 'trueify';
607  }
608  elseif (isset($_POST['send_submit']))
609  {
610    $repost_submit_name = 'send_submit';
611  }
612
613  $template->assign_block_vars
614  (
615    'repost', 
616      array
617      (
618        'REPOST_SUBMIT_NAME' => $repost_submit_name
619      )
620    );
621}
622
623switch ($page['mode'])
624{
625  case 'param' :
626  {
627    $template->assign_block_vars(
628      $page['mode'],
629      array(
630        'SEND_HTML_MAIL_YES' => ($conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
631        'SEND_HTML_MAIL_NO' => (!$conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
632        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
633        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
634        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
635        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
636        'SEND_RECENT_POST_DATES_YES' => ($conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : ''),
637        'SEND_RECENT_POST_DATES_NO' => (!$conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : '')
638        ));
639    break;
640  }
641
642  case 'subscribe' :
643  {
644    $template->assign_block_vars(
645      $page['mode'],
646      array(
647        ));
648
649    $template->assign_vars(
650      array(
651        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
652        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
653        )
654      );
655
656    $data_users = get_user_notifications('subscribe');
657    foreach ($data_users as $nbm_user)
658    {
659      $template->assign_block_vars(
660        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
661        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
662                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
663                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
664                            ) ? 'selected="selected"' : '',
665              'VALUE' => $nbm_user['check_key'],
666              'OPTION' => $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']'
667          ));
668    }
669
670    break;
671  }
672
673  case 'send' :
674  {
675    $template->assign_block_vars($page['mode'], array());
676
677    $data_users = do_action_send_mail_notification('list_to_send');
678
679    if  (count($data_users) == 0)
680    {
681      $template->assign_block_vars($page['mode'].'.send_empty', array());
682    }
683    else
684    {
685      $template->assign_block_vars(
686        $page['mode'].'.send_data',
687        array(
688          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
689          ));
690
691      foreach ($data_users as $num => $nbm_user)
692      {
693        if (
694            (!$must_repost) or // Not timeout, normal treatment
695            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
696            )
697        {
698          $template->assign_block_vars(
699            $page['mode'].'.send_data.user_send_mail',
700            array(
701              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
702              'ID' => $nbm_user['check_key'],
703              'CHECKED' =>  ( // not check if not selected,  on init select<all
704                              isset($_POST['send_selection']) and // not init
705                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
706                            )   ? '' : 'checked="checked"',
707              'USERNAME'=> $nbm_user['username'],
708              'EMAIL' => get_email_address_as_display_text($nbm_user['mail_address']),
709              'LAST_SEND'=> $nbm_user['last_send']
710              ));
711        }
712      }
713    }
714
715    break;
716  }
717}
718
719// +-----------------------------------------------------------------------+
720// | Sending html code                                                     |
721// +-----------------------------------------------------------------------+
722$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
723$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
724
725?>
Note: See TracBrowser for help on using the repository browser.