source: trunk/admin/notification_by_mail.php @ 1809

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

When not template are selected for mail, PWG uses default template..

Sent multi-part message in MIME format. (With only one part for the moment).

Improvement pwg_mail function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.3 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-12 23:21:23 +0000 (Mon, 12 Feb 2007) $
11// | last modifier : $Author: rub $
12// | revision      : $Revision: 1809 $
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    if (!isset($customize_mail_content))
230    {
231      $customize_mail_content = $conf['nbm_complementary_mail_content'];
232    }
233
234    // disabled and null mail_address are not selected in the list
235    $data_users = get_user_notifications('send', $check_key_list);
236
237    // List all if it's define on options or on timeout
238    $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
239
240    // Check if exist news to list user or send mails
241    if ((!$is_list_all_without_test) or ($is_action_send))
242    {
243      if (count($data_users) > 0)
244      {
245        $datas = array();
246
247        // Prepare message after change language
248        if ($is_action_send)
249        {
250          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
251        }
252        else
253        {
254          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
255        }
256
257        // Begin nbm users environment
258        begin_users_env_nbm($is_action_send);
259
260        foreach ($data_users as $nbm_user)
261        {
262          if ((!$is_action_send) and check_sendmail_timeout())
263          {
264            // Stop fill list on 'list_to_send', if the quota is override
265            array_push($page['infos'], $msg_break_timeout);
266            break;
267          }
268          if (($is_action_send) and check_sendmail_timeout())
269          {
270            // Stop fill list on 'send', if the quota is override
271            array_push($page['errors'], $msg_break_timeout);
272            break;
273          }
274
275          // set env nbm user
276          set_user_on_env_nbm($nbm_user, $is_action_send);
277
278          if ($is_action_send)
279          {
280            set_make_full_url();
281            // Fill return list of "treated" check_key for 'send'
282            array_push($return_list, $nbm_user['check_key']);
283
284            if ($conf['nbm_send_detailed_content'])
285            {
286               $news = news($nbm_user['last_send'], $dbnow, false, $conf['nbm_send_html_mail']);
287               $exist_data = count($news) > 0;
288            }
289            else
290            {
291              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
292            }
293
294            if ($exist_data)
295            {
296              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
297
298              // Assign current var for nbm mail
299              assign_vars_nbm_mail_content($nbm_user);
300
301              $end_punct = ($conf['nbm_send_detailed_content'] ? ':' : '.');
302
303              if (!is_null($nbm_user['last_send']))
304              {
305                $env_nbm['mail_template']->assign_block_vars
306                (
307                  'content_new_elements_between',
308                  array
309                  (
310                    'DATE_BETWEEN_1' => $nbm_user['last_send'], 
311                    'DATE_BETWEEN_2' => $dbnow,
312                    'END_PUNCT' => $end_punct
313                  )
314                );
315              }
316              else
317              {
318                $env_nbm['mail_template']->assign_block_vars
319                (
320                  'content_new_elements_single',
321                  array
322                  (
323                    'DATE_SINGLE' => $dbnow,
324                    'END_PUNCT' => $end_punct
325                  )
326                );
327              }
328
329              if ($conf['nbm_send_detailed_content'])
330              {
331                foreach ($news as $data)
332                {
333                  $env_nbm['mail_template']->assign_block_vars
334                  (
335                    'global_new_line.new_line', array('DATA' => $data)
336                  );
337                }
338              }
339
340              if (!empty($customize_mail_content))
341              {
342                $env_nbm['mail_template']->assign_block_vars
343                (
344                  'custom', array('CUSTOMIZE_MAIL_CONTENT' => $customize_mail_content)
345                );
346              }
347
348              if ($conf['nbm_send_html_mail'] and $conf['nbm_send_recent_post_dates'])
349              {
350                $recent_post_dates = get_recent_post_dates(7, 5, 9);
351                foreach ($recent_post_dates as $date_detail)
352                {
353                  $env_nbm['mail_template']->assign_block_vars
354                  (
355                    'recent_post.recent_post_block',
356                    array
357                    (
358                      'TITLE' => get_title_recent_post_date($date_detail),
359                      'HTML_DATA' => get_html_description_recent_post_date($date_detail)
360                    )
361                  );
362                }
363              }
364
365              $env_nbm['mail_template']->assign_block_vars
366              (
367                'goto',
368                array
369                (
370                  'GALLERY_TITLE' => $conf['gallery_title'],
371                  'GALLERY_URL' => $conf['gallery_url']
372                )
373              );
374
375              $env_nbm['mail_template']->assign_block_vars
376              (
377                'byebye', array('SEND_AS_NAME' => $env_nbm['send_as_name'])
378              );
379
380              if (pwg_mail
381                  (
382                    format_email($nbm_user['username'], $nbm_user['mail_address']),
383                    array
384                    (
385                      'from' => $env_nbm['send_as_mail_formated'],
386                      'subject' => $subject,
387                      'email_format' => $env_nbm['email_format'],
388                      'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
389                      'content_format' => $env_nbm['email_format'],
390                      'template' => $nbm_user['template'],
391                      'theme' => $nbm_user['theme']
392                    )
393                  ))
394              {
395                inc_mail_sent_success($nbm_user);
396
397                $data = array('user_id' => $nbm_user['user_id'],
398                              'last_send' => $dbnow);
399                array_push($datas, $data);
400              }
401              else
402              {
403                inc_mail_sent_failed($nbm_user);
404              }
405
406              unset_make_full_url();
407            }
408          }
409          else
410          {
411            if (news_exists($nbm_user['last_send'], $dbnow))
412            {
413              // Fill return list of "selected" users for 'list_to_send'
414              array_push($return_list, $nbm_user);
415            }
416          }
417         
418          // unset env nbm user
419          unset_user_on_env_nbm();
420        }
421
422        // Restore nbm environment
423        end_users_env_nbm();
424
425        if ($is_action_send)
426        {
427          mass_updates(
428            USER_MAIL_NOTIFICATION_TABLE,
429            array(
430              'primary' => array('user_id'),
431              'update' => array('last_send')
432             ),
433             $datas
434             );
435
436          display_counter_info();
437        }
438      }
439      else
440      {
441        if ($is_action_send)
442        {
443          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
444        }
445      }
446    }
447    else
448    {
449      // Quick List, don't check news
450      // Fill return list of "selected" users for 'list_to_send'
451      $return_list = $data_users;
452    }
453  }
454
455  // Return list of "selected" users for 'list_to_send'
456  // Return list of "treated" check_key for 'send'
457  return $return_list;
458}
459
460// +-----------------------------------------------------------------------+
461// | Main                                                                  |
462// +-----------------------------------------------------------------------+
463if (!isset($_GET['mode']))
464{
465  $page['mode'] = 'send';
466}
467else
468{
469  $page['mode'] = $_GET['mode'];
470}
471
472// +-----------------------------------------------------------------------+
473// | Check Access and exit when user status is not ok                      |
474// +-----------------------------------------------------------------------+
475check_status(get_tab_status($page['mode']));
476
477// +-----------------------------------------------------------------------+
478// | Insert new users with mails                                           |
479// +-----------------------------------------------------------------------+
480if (!isset($_POST) or (count($_POST) ==0))
481{
482  // No insert data in post mode
483  insert_new_data_user_mail_notification();
484}
485
486// +-----------------------------------------------------------------------+
487// | Treatment of tab post                                                 |
488// +-----------------------------------------------------------------------+
489switch ($page['mode'])
490{
491  case 'param' :
492  {
493    if (isset($_POST['param_submit']) and !is_adviser())
494    {
495      $updated_param_count = 0;
496      // Update param
497      $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
498      while ($nbm_user = mysql_fetch_array($result))
499      {
500        if (isset($_POST[$nbm_user['param']]))
501        {
502          $value = $_POST[$nbm_user['param']];
503
504          $query = '
505update
506'.CONFIG_TABLE.'
507set
508  value = \''. str_replace("\'", "''", $value).'\'
509where
510  param = \''.$nbm_user['param'].'\';';
511          pwg_query($query);
512          $updated_param_count += 1;
513        }
514      }
515   
516      array_push($page['infos'],
517        l10n_dec('nbm_updated_param_count', 'nbm_updated_params_count',
518          $updated_param_count));
519
520      // Reload conf with new values
521      load_conf_from_db('param like \'nbm\\_%\'');
522    }
523  }
524  case 'subscribe' :
525  {
526    if (!is_adviser())
527    {
528      if (isset($_POST['falsify']) and isset($_POST['cat_true']))
529      {
530        $check_key_treated = unsubscribe_notification_by_mail(true, $_POST['cat_true']);
531        do_timeout_treatment('cat_true', $check_key_treated);
532      }
533      else
534      if (isset($_POST['trueify']) and isset($_POST['cat_false']))
535      {
536        $check_key_treated = subscribe_notification_by_mail(true, $_POST['cat_false']);
537        do_timeout_treatment('cat_false', $check_key_treated);
538      }
539    }
540    break;
541  }
542
543  case 'send' :
544  {
545    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']) and !is_adviser())
546    {
547      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], stripslashes($_POST['send_customize_mail_content']));
548      do_timeout_treatment('send_selection', $check_key_treated);
549    }
550  }
551}
552
553// +-----------------------------------------------------------------------+
554// | template initialization                                               |
555// +-----------------------------------------------------------------------+
556$template->set_filenames
557(
558  array
559  (
560    'double_select' => 'admin/double_select.tpl',
561    'notification_by_mail'=>'admin/notification_by_mail.tpl'
562  )
563);
564
565$template->assign_vars
566(
567  array
568  (
569    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
570    'U_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
571    'F_ACTION'=> $base_url.get_query_string_diff(array())
572  )
573);
574
575if (is_autorize_status(ACCESS_WEBMASTER))
576{
577  $template->assign_block_vars
578  (
579    'header_link',
580    array
581    (
582      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
583      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
584      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
585    )
586  );
587}
588
589if ($must_repost)
590{
591  // Get name of submit button
592  $repost_submit_name = '';
593  if (isset($_POST['falsify']))
594  {
595    $repost_submit_name = 'falsify';
596  }
597  elseif (isset($_POST['trueify']))
598  {
599    $repost_submit_name = 'trueify';
600  }
601  elseif (isset($_POST['send_submit']))
602  {
603    $repost_submit_name = 'send_submit';
604  }
605
606  $template->assign_block_vars
607  (
608    'repost', 
609      array
610      (
611        'REPOST_SUBMIT_NAME' => $repost_submit_name
612      )
613    );
614}
615
616switch ($page['mode'])
617{
618  case 'param' :
619  {
620    $template->assign_block_vars(
621      $page['mode'],
622      array(
623        'SEND_HTML_MAIL_YES' => ($conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
624        'SEND_HTML_MAIL_NO' => (!$conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
625        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
626        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
627        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
628        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
629        'SEND_RECENT_POST_DATES_YES' => ($conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : ''),
630        'SEND_RECENT_POST_DATES_NO' => (!$conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : '')
631        ));
632    break;
633  }
634
635  case 'subscribe' :
636  {
637    $template->assign_block_vars(
638      $page['mode'],
639      array(
640        ));
641
642    $template->assign_vars(
643      array(
644        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
645        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
646        )
647      );
648
649    $data_users = get_user_notifications('subscribe');
650    foreach ($data_users as $nbm_user)
651    {
652      $template->assign_block_vars(
653        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
654        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
655                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
656                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
657                            ) ? 'selected="selected"' : '',
658              'VALUE' => $nbm_user['check_key'],
659              'OPTION' => $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']'
660          ));
661    }
662
663    break;
664  }
665
666  case 'send' :
667  {
668    $template->assign_block_vars($page['mode'], array());
669
670    $data_users = do_action_send_mail_notification('list_to_send');
671
672    if  (count($data_users) == 0)
673    {
674      $template->assign_block_vars($page['mode'].'.send_empty', array());
675    }
676    else
677    {
678      $template->assign_block_vars(
679        $page['mode'].'.send_data',
680        array(
681          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
682          ));
683
684      foreach ($data_users as $num => $nbm_user)
685      {
686        if (
687            (!$must_repost) or // Not timeout, normal treatment
688            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
689            )
690        {
691          $template->assign_block_vars(
692            $page['mode'].'.send_data.user_send_mail',
693            array(
694              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
695              'ID' => $nbm_user['check_key'],
696              'CHECKED' =>  ( // not check if not selected,  on init select<all
697                              isset($_POST['send_selection']) and // not init
698                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
699                            )   ? '' : 'checked="checked"',
700              'USERNAME'=> $nbm_user['username'],
701              'EMAIL' => get_email_address_as_display_text($nbm_user['mail_address']),
702              'LAST_SEND'=> $nbm_user['last_send']
703              ));
704        }
705      }
706    }
707
708    break;
709  }
710}
711
712// +-----------------------------------------------------------------------+
713// | Sending html code                                                     |
714// +-----------------------------------------------------------------------+
715$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
716$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
717
718?>
Note: See TracBrowser for help on using the repository browser.