source: trunk/admin/notification_by_mail.php @ 2059

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

Change headers with team email

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 23.9 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// +-----------------------------------------------------------------------+
7// | file          : $Id: notification_by_mail.php 2059 2007-07-10 05:30:01Z rub $
8// | last update   : $Date: 2007-07-10 05:30:01 +0000 (Tue, 10 Jul 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2059 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27// +-----------------------------------------------------------------------+
28// | include                                                               |
29// +-----------------------------------------------------------------------+
30
31if (!defined('PHPWG_ROOT_PATH'))
32{
33  die ("Hacking attempt!");
34}
35
36include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
37include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
38include_once(PHPWG_ROOT_PATH.'admin/include/functions_tabsheet.inc.php');
39include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
40include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
41include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
42
43// +-----------------------------------------------------------------------+
44// | Check Access and exit when user status is not ok                      |
45// +-----------------------------------------------------------------------+
46check_status(ACCESS_ADMINISTRATOR);
47
48// +-----------------------------------------------------------------------+
49// | Initialization                                                        |
50// +-----------------------------------------------------------------------+
51$base_url = get_root_url().'admin.php';
52$must_repost = false;
53
54// +-----------------------------------------------------------------------+
55// | functions                                                             |
56// +-----------------------------------------------------------------------+
57
58/*
59 * Do timeout treatment in order to finish to send mails
60 *
61 * @param $post_keyname: key of check_key post array
62 * @param check_key_treated: array of check_key treated
63 * @return none
64 */
65function do_timeout_treatment($post_keyname, $check_key_treated = array())
66{
67  global $env_nbm, $base_url, $page, $must_repost;
68
69  if ($env_nbm['is_sendmail_timeout'])
70  {
71    if (isset($_POST[$post_keyname]))
72    {
73      $post_count = count($_POST[$post_keyname]);
74      $treated_count = count($check_key_treated);
75      if ($treated_count != 0)
76      {
77        $time_refresh = ceil((get_moment() - $env_nbm['start_time']) * $post_count / $treated_count);
78      }
79      else
80      {
81        $time_refresh = 0;
82      }
83      $_POST[$post_keyname] = array_diff($_POST[$post_keyname], $check_key_treated);
84
85      $must_repost = true;
86      array_push($page['errors'],
87        l10n_dec('nbm_background_treatment_redirect_second', 
88                 'nbm_background_treatment_redirect_seconds',
89                  $time_refresh));
90    }
91  }
92
93}
94
95/*
96 * Get the authorized_status for each tab
97 * return corresponding status
98 */
99function get_tab_status($mode)
100{
101  $result = ACCESS_WEBMASTER;
102  switch ($mode)
103  {
104    case 'param':
105    case 'subscribe':
106      $result = ACCESS_WEBMASTER;
107      break;
108    case 'send':
109      $result = ACCESS_ADMINISTRATOR;
110      break;
111    default:
112      $result = ACCESS_WEBMASTER;
113      break;
114  }
115  return $result;
116}
117
118/*
119 * Inserting News users
120 */
121function insert_new_data_user_mail_notification()
122{
123  global $conf, $page, $env_nbm;
124
125  // Set null mail_address empty
126  $query = '
127update
128  '.USERS_TABLE.'
129set
130  '.$conf['user_fields']['email'].' = null
131where
132  trim('.$conf['user_fields']['email'].') = \'\';';
133  pwg_query($query);
134
135  // null mail_address are not selected in the list
136  $query = '
137select
138  u.'.$conf['user_fields']['id'].' as user_id,
139  u.'.$conf['user_fields']['username'].' as username,
140  u.'.$conf['user_fields']['email'].' as mail_address
141from
142  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.'.$conf['user_fields']['id'].' = m.user_id
143where
144  u.'.$conf['user_fields']['email'].' is not null and
145  m.user_id is null
146order by
147  user_id;';
148
149  $result = pwg_query($query);
150
151  if (mysql_num_rows($result) > 0)
152  {
153    $inserts = array();
154    $check_key_list = array();
155
156    while ($nbm_user = mysql_fetch_array($result))
157    {
158      // Calculate key
159      $nbm_user['check_key'] = find_available_check_key();
160
161      // Save key
162      array_push($check_key_list, $nbm_user['check_key']);
163
164      // Insert new nbm_users
165      array_push
166      (
167        $inserts, 
168        array
169        (
170          'user_id' => $nbm_user['user_id'],
171          'check_key' => $nbm_user['check_key'],
172          'enabled' => 'false' // By default if false, set to true with specific functions
173        )
174      );
175
176      array_push
177      (
178        $page['infos'], 
179        sprintf(
180          l10n('nbm_user_x_added'), 
181          $nbm_user['username'], 
182          get_email_address_as_display_text($nbm_user['mail_address'])
183        )
184      );
185    }
186
187    // Insert new nbm_users
188    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
189    // Update field enabled with specific function
190    $check_key_treated = do_subscribe_unsubscribe_notification_by_mail
191    (
192      true,
193      $conf['nbm_default_value_user_enabled'],
194      $check_key_list
195    );
196
197     // On timeout simulate like tabsheet send
198    if ($env_nbm['is_sendmail_timeout'])
199    {
200      $quoted_check_key_list = quote_check_key_list(array_diff($check_key_list, $check_key_treated));
201      if (count($quoted_check_key_list) != 0 )
202      {
203        $query = 'delete from '.USER_MAIL_NOTIFICATION_TABLE.' where check_key in ('.implode(",", $quoted_check_key_list).');';
204        $result = pwg_query($query);
205
206        redirect($base_url.get_query_string_diff(array()), l10n('nbm_redirect_msg'));
207      }
208    }
209  }
210}
211
212/*
213 * Send mail for notification to all users
214 * Return list of "selected" users for 'list_to_send'
215 * Return list of "treated" check_key for 'send'
216 */
217function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
218{
219  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
220  $return_list = array();
221 
222  if (in_array($action, array('list_to_send', 'send')))
223  {
224    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
225
226    $is_action_send = ($action == 'send');
227
228    // disabled and null mail_address are not selected in the list
229    $data_users = get_user_notifications('send', $check_key_list);
230
231    // List all if it's define on options or on timeout
232    $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
233
234    // Check if exist news to list user or send mails
235    if ((!$is_list_all_without_test) or ($is_action_send))
236    {
237      if (count($data_users) > 0)
238      {
239        $datas = array();
240
241        if (!isset($customize_mail_content))
242        {
243          $customize_mail_content = $conf['nbm_complementary_mail_content'];
244        }
245
246        if ($conf['nbm_send_html_mail'] and !(strpos($customize_mail_content, '<') === 0))
247        {
248          // On HTML mail, detects if the content are HTML format.
249          // If it's plain text format, convert content to readable HTML
250          $customize_mail_content = nl2br(htmlentities($customize_mail_content));
251        }
252
253        // Prepare message after change language
254        if ($is_action_send)
255        {
256          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
257        }
258        else
259        {
260          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
261        }
262
263        // Begin nbm users environment
264        begin_users_env_nbm($is_action_send);
265
266        foreach ($data_users as $nbm_user)
267        {
268          if ((!$is_action_send) and check_sendmail_timeout())
269          {
270            // Stop fill list on 'list_to_send', if the quota is override
271            array_push($page['infos'], $msg_break_timeout);
272            break;
273          }
274          if (($is_action_send) and check_sendmail_timeout())
275          {
276            // Stop fill list on 'send', if the quota is override
277            array_push($page['errors'], $msg_break_timeout);
278            break;
279          }
280
281          // set env nbm user
282          set_user_on_env_nbm($nbm_user, $is_action_send);
283
284          if ($is_action_send)
285          {
286            set_make_full_url();
287            // Fill return list of "treated" check_key for 'send'
288            array_push($return_list, $nbm_user['check_key']);
289
290            if ($conf['nbm_send_detailed_content'])
291            {
292               $news = news($nbm_user['last_send'], $dbnow, false, $conf['nbm_send_html_mail']);
293               $exist_data = count($news) > 0;
294            }
295            else
296            {
297              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
298            }
299
300            if ($exist_data)
301            {
302              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
303
304              // Assign current var for nbm mail
305              assign_vars_nbm_mail_content($nbm_user);
306
307              $end_punct = ($conf['nbm_send_detailed_content'] ? ':' : '.');
308
309              if (!is_null($nbm_user['last_send']))
310              {
311                $env_nbm['mail_template']->assign_block_vars
312                (
313                  'content_new_elements_between',
314                  array
315                  (
316                    'DATE_BETWEEN_1' => $nbm_user['last_send'], 
317                    'DATE_BETWEEN_2' => $dbnow,
318                    'END_PUNCT' => $end_punct
319                  )
320                );
321              }
322              else
323              {
324                $env_nbm['mail_template']->assign_block_vars
325                (
326                  'content_new_elements_single',
327                  array
328                  (
329                    'DATE_SINGLE' => $dbnow,
330                    'END_PUNCT' => $end_punct
331                  )
332                );
333              }
334
335              if ($conf['nbm_send_detailed_content'])
336              {
337                foreach ($news as $data)
338                {
339                  $env_nbm['mail_template']->assign_block_vars
340                  (
341                    'global_new_line.new_line', array('DATA' => $data)
342                  );
343                }
344              }
345
346              if (!empty($customize_mail_content))
347              {
348                $env_nbm['mail_template']->assign_block_vars
349                (
350                  'custom', array('CUSTOMIZE_MAIL_CONTENT' => $customize_mail_content)
351                );
352              }
353
354              if ($conf['nbm_send_html_mail'] and $conf['nbm_send_recent_post_dates'])
355              {
356                $recent_post_dates = get_recent_post_dates_array(
357                  $conf['recent_post_dates']['NBM']);
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_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
577    'F_ACTION'=> $base_url.get_query_string_diff(array())
578  )
579);
580
581if (is_autorize_status(ACCESS_WEBMASTER))
582{
583  // TabSheet initialization
584  $page['tabsheet'] = array
585  (
586    'param' => array
587     (
588      'caption' => l10n('nbm_param_mode'),
589      'url' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), 
590        array('mode' => 'param'))
591     ),
592    'subscribe' => array
593     (
594      'caption' => l10n('nbm_subscribe_mode'),
595      'url' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
596     ),
597    'send' => array
598     (
599      'caption' => l10n('nbm_send_mode'),
600      'url' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
601     )
602  );
603
604  $page['tabsheet'][$page['mode']]['selected'] = true;
605
606  // Assign tabsheet to template
607  template_assign_tabsheet();
608}
609
610if ($must_repost)
611{
612  // Get name of submit button
613  $repost_submit_name = '';
614  if (isset($_POST['falsify']))
615  {
616    $repost_submit_name = 'falsify';
617  }
618  elseif (isset($_POST['trueify']))
619  {
620    $repost_submit_name = 'trueify';
621  }
622  elseif (isset($_POST['send_submit']))
623  {
624    $repost_submit_name = 'send_submit';
625  }
626
627  $template->assign_block_vars
628  (
629    'repost', 
630      array
631      (
632        'REPOST_SUBMIT_NAME' => $repost_submit_name
633      )
634    );
635}
636
637switch ($page['mode'])
638{
639  case 'param' :
640  {
641    $template->assign_block_vars(
642      $page['mode'],
643      array(
644        'SEND_HTML_MAIL_YES' => ($conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
645        'SEND_HTML_MAIL_NO' => (!$conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
646        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
647        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
648        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
649        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
650        'SEND_RECENT_POST_DATES_YES' => ($conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : ''),
651        'SEND_RECENT_POST_DATES_NO' => (!$conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : '')
652        ));
653    break;
654  }
655
656  case 'subscribe' :
657  {
658    $template->assign_block_vars(
659      $page['mode'],
660      array(
661        ));
662
663    $template->assign_vars(
664      array(
665        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
666        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
667        )
668      );
669
670    $data_users = get_user_notifications('subscribe');
671    foreach ($data_users as $nbm_user)
672    {
673      $template->assign_block_vars(
674        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
675        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
676                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
677                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
678                            ) ? 'selected="selected"' : '',
679              'VALUE' => $nbm_user['check_key'],
680              'OPTION' => $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']'
681          ));
682    }
683
684    break;
685  }
686
687  case 'send' :
688  {
689    $template->assign_block_vars($page['mode'], array());
690
691    $data_users = do_action_send_mail_notification('list_to_send');
692
693    if  (count($data_users) == 0)
694    {
695      $template->assign_block_vars($page['mode'].'.send_empty', array());
696    }
697    else
698    {
699      $template->assign_block_vars(
700        $page['mode'].'.send_data',
701        array(
702          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
703          ));
704
705      foreach ($data_users as $num => $nbm_user)
706      {
707        if (
708            (!$must_repost) or // Not timeout, normal treatment
709            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
710            )
711        {
712          $template->assign_block_vars(
713            $page['mode'].'.send_data.user_send_mail',
714            array(
715              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
716              'ID' => $nbm_user['check_key'],
717              'CHECKED' =>  ( // not check if not selected,  on init select<all
718                              isset($_POST['send_selection']) and // not init
719                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
720                            )   ? '' : 'checked="checked"',
721              'USERNAME'=> $nbm_user['username'],
722              'EMAIL' => get_email_address_as_display_text($nbm_user['mail_address']),
723              'LAST_SEND'=> $nbm_user['last_send']
724              ));
725        }
726      }
727    }
728
729    break;
730  }
731}
732
733// +-----------------------------------------------------------------------+
734// | Sending html code                                                     |
735// +-----------------------------------------------------------------------+
736$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
737$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
738
739?>
Note: See TracBrowser for help on using the repository browser.