source: trunk/admin/notification_by_mail.php @ 1857

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

Reduce number of images on NBM for more visibility. (Next parameters for RSS & NBM on $conf in order to be more easy to modify)
Use $id$ on php headers

  • 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// | file          : $Id: notification_by_mail.php 1857 2007-02-25 22:26:52Z rub $
9// | last update   : $Date: 2007-02-25 22:26:52 +0000 (Sun, 25 Feb 2007) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1857 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28// +-----------------------------------------------------------------------+
29// | include                                                               |
30// +-----------------------------------------------------------------------+
31
32if (!defined('PHPWG_ROOT_PATH'))
33{
34  die ("Hacking attempt!");
35}
36
37include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
38include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.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(7, 3, 9);
357                foreach ($recent_post_dates as $date_detail)
358                {
359                  $env_nbm['mail_template']->assign_block_vars
360                  (
361                    'recent_post.recent_post_block',
362                    array
363                    (
364                      'TITLE' => get_title_recent_post_date($date_detail),
365                      'HTML_DATA' => get_html_description_recent_post_date($date_detail)
366                    )
367                  );
368                }
369              }
370
371              $env_nbm['mail_template']->assign_block_vars
372              (
373                'goto',
374                array
375                (
376                  'GALLERY_TITLE' => $conf['gallery_title'],
377                  'GALLERY_URL' => $conf['gallery_url']
378                )
379              );
380
381              $env_nbm['mail_template']->assign_block_vars
382              (
383                'byebye', array('SEND_AS_NAME' => $env_nbm['send_as_name'])
384              );
385
386              if (pwg_mail
387                  (
388                    format_email($nbm_user['username'], $nbm_user['mail_address']),
389                    array
390                    (
391                      'from' => $env_nbm['send_as_mail_formated'],
392                      'subject' => $subject,
393                      'email_format' => $env_nbm['email_format'],
394                      'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
395                      'content_format' => $env_nbm['email_format'],
396                      'template' => $nbm_user['template'],
397                      'theme' => $nbm_user['theme']
398                    )
399                  ))
400              {
401                inc_mail_sent_success($nbm_user);
402
403                $data = array('user_id' => $nbm_user['user_id'],
404                              'last_send' => $dbnow);
405                array_push($datas, $data);
406              }
407              else
408              {
409                inc_mail_sent_failed($nbm_user);
410              }
411
412              unset_make_full_url();
413            }
414          }
415          else
416          {
417            if (news_exists($nbm_user['last_send'], $dbnow))
418            {
419              // Fill return list of "selected" users for 'list_to_send'
420              array_push($return_list, $nbm_user);
421            }
422          }
423         
424          // unset env nbm user
425          unset_user_on_env_nbm();
426        }
427
428        // Restore nbm environment
429        end_users_env_nbm();
430
431        if ($is_action_send)
432        {
433          mass_updates(
434            USER_MAIL_NOTIFICATION_TABLE,
435            array(
436              'primary' => array('user_id'),
437              'update' => array('last_send')
438             ),
439             $datas
440             );
441
442          display_counter_info();
443        }
444      }
445      else
446      {
447        if ($is_action_send)
448        {
449          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
450        }
451      }
452    }
453    else
454    {
455      // Quick List, don't check news
456      // Fill return list of "selected" users for 'list_to_send'
457      $return_list = $data_users;
458    }
459  }
460
461  // Return list of "selected" users for 'list_to_send'
462  // Return list of "treated" check_key for 'send'
463  return $return_list;
464}
465
466// +-----------------------------------------------------------------------+
467// | Main                                                                  |
468// +-----------------------------------------------------------------------+
469if (!isset($_GET['mode']))
470{
471  $page['mode'] = 'send';
472}
473else
474{
475  $page['mode'] = $_GET['mode'];
476}
477
478// +-----------------------------------------------------------------------+
479// | Check Access and exit when user status is not ok                      |
480// +-----------------------------------------------------------------------+
481check_status(get_tab_status($page['mode']));
482
483// +-----------------------------------------------------------------------+
484// | Insert new users with mails                                           |
485// +-----------------------------------------------------------------------+
486if (!isset($_POST) or (count($_POST) ==0))
487{
488  // No insert data in post mode
489  insert_new_data_user_mail_notification();
490}
491
492// +-----------------------------------------------------------------------+
493// | Treatment of tab post                                                 |
494// +-----------------------------------------------------------------------+
495switch ($page['mode'])
496{
497  case 'param' :
498  {
499    if (isset($_POST['param_submit']) and !is_adviser())
500    {
501      $updated_param_count = 0;
502      // Update param
503      $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
504      while ($nbm_user = mysql_fetch_array($result))
505      {
506        if (isset($_POST[$nbm_user['param']]))
507        {
508          $value = $_POST[$nbm_user['param']];
509
510          $query = '
511update
512'.CONFIG_TABLE.'
513set
514  value = \''. str_replace("\'", "''", $value).'\'
515where
516  param = \''.$nbm_user['param'].'\';';
517          pwg_query($query);
518          $updated_param_count += 1;
519        }
520      }
521   
522      array_push($page['infos'],
523        l10n_dec('nbm_updated_param_count', 'nbm_updated_params_count',
524          $updated_param_count));
525
526      // Reload conf with new values
527      load_conf_from_db('param like \'nbm\\_%\'');
528    }
529  }
530  case 'subscribe' :
531  {
532    if (!is_adviser())
533    {
534      if (isset($_POST['falsify']) and isset($_POST['cat_true']))
535      {
536        $check_key_treated = unsubscribe_notification_by_mail(true, $_POST['cat_true']);
537        do_timeout_treatment('cat_true', $check_key_treated);
538      }
539      else
540      if (isset($_POST['trueify']) and isset($_POST['cat_false']))
541      {
542        $check_key_treated = subscribe_notification_by_mail(true, $_POST['cat_false']);
543        do_timeout_treatment('cat_false', $check_key_treated);
544      }
545    }
546    break;
547  }
548
549  case 'send' :
550  {
551    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']) and !is_adviser())
552    {
553      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], stripslashes($_POST['send_customize_mail_content']));
554      do_timeout_treatment('send_selection', $check_key_treated);
555    }
556  }
557}
558
559// +-----------------------------------------------------------------------+
560// | template initialization                                               |
561// +-----------------------------------------------------------------------+
562$template->set_filenames
563(
564  array
565  (
566    'double_select' => 'admin/double_select.tpl',
567    'notification_by_mail'=>'admin/notification_by_mail.tpl'
568  )
569);
570
571$template->assign_vars
572(
573  array
574  (
575    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
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  $template->assign_block_vars
584  (
585    'header_link',
586    array
587    (
588      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
589      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
590      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
591    )
592  );
593}
594
595if ($must_repost)
596{
597  // Get name of submit button
598  $repost_submit_name = '';
599  if (isset($_POST['falsify']))
600  {
601    $repost_submit_name = 'falsify';
602  }
603  elseif (isset($_POST['trueify']))
604  {
605    $repost_submit_name = 'trueify';
606  }
607  elseif (isset($_POST['send_submit']))
608  {
609    $repost_submit_name = 'send_submit';
610  }
611
612  $template->assign_block_vars
613  (
614    'repost', 
615      array
616      (
617        'REPOST_SUBMIT_NAME' => $repost_submit_name
618      )
619    );
620}
621
622switch ($page['mode'])
623{
624  case 'param' :
625  {
626    $template->assign_block_vars(
627      $page['mode'],
628      array(
629        'SEND_HTML_MAIL_YES' => ($conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
630        'SEND_HTML_MAIL_NO' => (!$conf['nbm_send_html_mail'] ? 'checked="checked"' : ''),
631        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
632        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
633        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
634        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
635        'SEND_RECENT_POST_DATES_YES' => ($conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : ''),
636        'SEND_RECENT_POST_DATES_NO' => (!$conf['nbm_send_recent_post_dates'] ? 'checked="checked"' : '')
637        ));
638    break;
639  }
640
641  case 'subscribe' :
642  {
643    $template->assign_block_vars(
644      $page['mode'],
645      array(
646        ));
647
648    $template->assign_vars(
649      array(
650        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
651        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
652        )
653      );
654
655    $data_users = get_user_notifications('subscribe');
656    foreach ($data_users as $nbm_user)
657    {
658      $template->assign_block_vars(
659        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
660        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
661                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
662                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
663                            ) ? 'selected="selected"' : '',
664              'VALUE' => $nbm_user['check_key'],
665              'OPTION' => $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']'
666          ));
667    }
668
669    break;
670  }
671
672  case 'send' :
673  {
674    $template->assign_block_vars($page['mode'], array());
675
676    $data_users = do_action_send_mail_notification('list_to_send');
677
678    if  (count($data_users) == 0)
679    {
680      $template->assign_block_vars($page['mode'].'.send_empty', array());
681    }
682    else
683    {
684      $template->assign_block_vars(
685        $page['mode'].'.send_data',
686        array(
687          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
688          ));
689
690      foreach ($data_users as $num => $nbm_user)
691      {
692        if (
693            (!$must_repost) or // Not timeout, normal treatment
694            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
695            )
696        {
697          $template->assign_block_vars(
698            $page['mode'].'.send_data.user_send_mail',
699            array(
700              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
701              'ID' => $nbm_user['check_key'],
702              'CHECKED' =>  ( // not check if not selected,  on init select<all
703                              isset($_POST['send_selection']) and // not init
704                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
705                            )   ? '' : 'checked="checked"',
706              'USERNAME'=> $nbm_user['username'],
707              'EMAIL' => get_email_address_as_display_text($nbm_user['mail_address']),
708              'LAST_SEND'=> $nbm_user['last_send']
709              ));
710        }
711      }
712    }
713
714    break;
715  }
716}
717
718// +-----------------------------------------------------------------------+
719// | Sending html code                                                     |
720// +-----------------------------------------------------------------------+
721$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
722$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
723
724?>
Note: See TracBrowser for help on using the repository browser.