source: trunk/admin/notification_by_mail.php @ 2286

Last change on this file since 2286 was 2286, checked in by rvelices, 16 years ago
  • admin/notification_by_mail goes smarty - THE LAST ONE :-) :-)
  • get rid of user_list warnings
  • some code reorganisation in template class + explode modifier
  • minor template and language changes in search.tpl and cat_modify.tpl
  • Property svn:eol-style set to LF
  • 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-2008 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: notification_by_mail.php 2286 2008-03-20 00:35:36Z rvelices $
8// | last update   : $Date: 2008-03-20 00:35:36 +0000 (Thu, 20 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2286 $
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/tabsheet.class.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(), false), l10n('nbm_redirect_msg'));
207      }
208    }
209  }
210}
211
212/*
213 * Apply global functions to mail content
214 * return customize mail content rendered
215 */
216function render_global_customize_mail_content($customize_mail_content)
217{
218  global $conf;
219
220  if ($conf['nbm_send_html_mail'] and !(strpos($customize_mail_content, '<') === 0))
221  {
222    // On HTML mail, detects if the content are HTML format.
223    // If it's plain text format, convert content to readable HTML
224    return nl2br(htmlspecialchars($customize_mail_content));
225  }
226  else
227  {
228    return $customize_mail_content;
229  }
230}
231
232/*
233 * Send mail for notification to all users
234 * Return list of "selected" users for 'list_to_send'
235 * Return list of "treated" check_key for 'send'
236 */
237function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
238{
239  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
240  $return_list = array();
241
242  if (in_array($action, array('list_to_send', 'send')))
243  {
244    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
245
246    $is_action_send = ($action == 'send');
247
248    // disabled and null mail_address are not selected in the list
249    $data_users = get_user_notifications('send', $check_key_list);
250
251    // List all if it's define on options or on timeout
252    $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
253
254    // Check if exist news to list user or send mails
255    if ((!$is_list_all_without_test) or ($is_action_send))
256    {
257      if (count($data_users) > 0)
258      {
259        $datas = array();
260
261        if (!isset($customize_mail_content))
262        {
263          $customize_mail_content = $conf['nbm_complementary_mail_content'];
264        }
265
266        $customize_mail_content = 
267          trigger_event('nbm_render_global_customize_mail_content', $customize_mail_content);
268
269
270        // Prepare message after change language
271        if ($is_action_send)
272        {
273          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
274        }
275        else
276        {
277          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
278        }
279
280        // Begin nbm users environment
281        begin_users_env_nbm($is_action_send);
282
283        foreach ($data_users as $nbm_user)
284        {
285          if ((!$is_action_send) and check_sendmail_timeout())
286          {
287            // Stop fill list on 'list_to_send', if the quota is override
288            array_push($page['infos'], $msg_break_timeout);
289            break;
290          }
291          if (($is_action_send) and check_sendmail_timeout())
292          {
293            // Stop fill list on 'send', if the quota is override
294            array_push($page['errors'], $msg_break_timeout);
295            break;
296          }
297
298          // set env nbm user
299          set_user_on_env_nbm($nbm_user, $is_action_send);
300
301          if ($is_action_send)
302          {
303            set_make_full_url();
304            // Fill return list of "treated" check_key for 'send'
305            array_push($return_list, $nbm_user['check_key']);
306
307            if ($conf['nbm_send_detailed_content'])
308            {
309               $news = news($nbm_user['last_send'], $dbnow, false, $conf['nbm_send_html_mail']);
310               $exist_data = count($news) > 0;
311            }
312            else
313            {
314              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
315            }
316
317            if ($exist_data)
318            {
319              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
320
321              // Assign current var for nbm mail
322              assign_vars_nbm_mail_content($nbm_user);
323
324              if (!is_null($nbm_user['last_send']))
325              {
326                $env_nbm['mail_template']->assign
327                (
328                  'content_new_elements_between',
329                  array
330                  (
331                    'DATE_BETWEEN_1' => $nbm_user['last_send'],
332                    'DATE_BETWEEN_2' => $dbnow,
333                  )
334                );
335              }
336              else
337              {
338                $env_nbm['mail_template']->assign
339                (
340                  'content_new_elements_single',
341                  array
342                  (
343                    'DATE_SINGLE' => $dbnow,
344                  )
345                );
346              }
347
348              if ($conf['nbm_send_detailed_content'])
349              {
350                $env_nbm['mail_template']->assign('global_new_lines', $news);
351              }
352
353              $nbm_user_customize_mail_content = 
354                trigger_event('nbm_render_user_customize_mail_content',
355                  $customize_mail_content, $nbm_user);
356              if (!empty($nbm_user_customize_mail_content))
357              {
358                $env_nbm['mail_template']->assign
359                (
360                  'custom_mail_content', $nbm_user_customize_mail_content
361                );
362              }
363
364              if ($conf['nbm_send_html_mail'] and $conf['nbm_send_recent_post_dates'])
365              {
366                $recent_post_dates = get_recent_post_dates_array(
367                  $conf['recent_post_dates']['NBM']);
368                foreach ($recent_post_dates as $date_detail)
369                {
370                  $env_nbm['mail_template']->append
371                  (
372                    'recent_posts',
373                    array
374                    (
375                      'TITLE' => get_title_recent_post_date($date_detail),
376                      'HTML_DATA' => get_html_description_recent_post_date($date_detail)
377                    )
378                  );
379                }
380              }
381
382              $env_nbm['mail_template']->assign
383              (
384                array
385                (
386                  'GOTO_GALLERY_TITLE' => $conf['gallery_title'],
387                  'GOTO_GALLERY_URL' => $conf['gallery_url'],
388                  'SEND_AS_NAME'      => $env_nbm['send_as_name'],
389                )
390              );
391
392              if (pwg_mail
393                  (
394                    format_email($nbm_user['username'], $nbm_user['mail_address']),
395                    array
396                    (
397                      'from' => $env_nbm['send_as_mail_formated'],
398                      'subject' => $subject,
399                      'email_format' => $env_nbm['email_format'],
400                      'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
401                      'content_format' => $env_nbm['email_format'],
402                      'template' => $nbm_user['template'],
403                      'theme' => $nbm_user['theme']
404                    )
405                  ))
406              {
407                inc_mail_sent_success($nbm_user);
408
409                $data = array('user_id' => $nbm_user['user_id'],
410                              'last_send' => $dbnow);
411                array_push($datas, $data);
412              }
413              else
414              {
415                inc_mail_sent_failed($nbm_user);
416              }
417
418              unset_make_full_url();
419            }
420          }
421          else
422          {
423            if (news_exists($nbm_user['last_send'], $dbnow))
424            {
425              // Fill return list of "selected" users for 'list_to_send'
426              array_push($return_list, $nbm_user);
427            }
428          }
429
430          // unset env nbm user
431          unset_user_on_env_nbm();
432        }
433
434        // Restore nbm environment
435        end_users_env_nbm();
436
437        if ($is_action_send)
438        {
439          mass_updates(
440            USER_MAIL_NOTIFICATION_TABLE,
441            array(
442              'primary' => array('user_id'),
443              'update' => array('last_send')
444             ),
445             $datas
446             );
447
448          display_counter_info();
449        }
450      }
451      else
452      {
453        if ($is_action_send)
454        {
455          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
456        }
457      }
458    }
459    else
460    {
461      // Quick List, don't check news
462      // Fill return list of "selected" users for 'list_to_send'
463      $return_list = $data_users;
464    }
465  }
466
467  // Return list of "selected" users for 'list_to_send'
468  // Return list of "treated" check_key for 'send'
469  return $return_list;
470}
471
472// +-----------------------------------------------------------------------+
473// | Main                                                                  |
474// +-----------------------------------------------------------------------+
475if (!isset($_GET['mode']))
476{
477  $page['mode'] = 'send';
478}
479else
480{
481  $page['mode'] = $_GET['mode'];
482}
483
484// +-----------------------------------------------------------------------+
485// | Check Access and exit when user status is not ok                      |
486// +-----------------------------------------------------------------------+
487check_status(get_tab_status($page['mode']));
488
489
490// +-----------------------------------------------------------------------+
491// | Add event handler                                                     |
492// +-----------------------------------------------------------------------+
493add_event_handler('nbm_render_global_customize_mail_content', 'render_global_customize_mail_content');
494trigger_action('nbm_event_handler_added');
495
496
497// +-----------------------------------------------------------------------+
498// | Insert new users with mails                                           |
499// +-----------------------------------------------------------------------+
500if (!isset($_POST) or (count($_POST) ==0))
501{
502  // No insert data in post mode
503  insert_new_data_user_mail_notification();
504}
505
506// +-----------------------------------------------------------------------+
507// | Treatment of tab post                                                 |
508// +-----------------------------------------------------------------------+
509switch ($page['mode'])
510{
511  case 'param' :
512  {
513    if (isset($_POST['param_submit']) and !is_adviser())
514    {
515      $updated_param_count = 0;
516      // Update param
517      $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
518      while ($nbm_user = mysql_fetch_array($result))
519      {
520        if (isset($_POST[$nbm_user['param']]))
521        {
522          $value = $_POST[$nbm_user['param']];
523
524          $query = '
525update
526'.CONFIG_TABLE.'
527set
528  value = \''. str_replace("\'", "''", $value).'\'
529where
530  param = \''.$nbm_user['param'].'\';';
531          pwg_query($query);
532          $updated_param_count += 1;
533        }
534      }
535
536      array_push($page['infos'],
537        l10n_dec('nbm_updated_param_count', 'nbm_updated_params_count',
538          $updated_param_count));
539
540      // Reload conf with new values
541      load_conf_from_db('param like \'nbm\\_%\'');
542    }
543  }
544  case 'subscribe' :
545  {
546    if (!is_adviser())
547    {
548      if (isset($_POST['falsify']) and isset($_POST['cat_true']))
549      {
550        $check_key_treated = unsubscribe_notification_by_mail(true, $_POST['cat_true']);
551        do_timeout_treatment('cat_true', $check_key_treated);
552      }
553      else
554      if (isset($_POST['trueify']) and isset($_POST['cat_false']))
555      {
556        $check_key_treated = subscribe_notification_by_mail(true, $_POST['cat_false']);
557        do_timeout_treatment('cat_false', $check_key_treated);
558      }
559    }
560    break;
561  }
562
563  case 'send' :
564  {
565    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']) and !is_adviser())
566    {
567      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], stripslashes($_POST['send_customize_mail_content']));
568      do_timeout_treatment('send_selection', $check_key_treated);
569    }
570  }
571}
572
573// +-----------------------------------------------------------------------+
574// | template initialization                                               |
575// +-----------------------------------------------------------------------+
576$template->set_filenames
577(
578  array
579  (
580    'double_select' => 'admin/double_select.tpl',
581    'notification_by_mail'=>'admin/notification_by_mail.tpl'
582  )
583);
584
585$template->assign
586(
587  array
588  (
589    'U_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
590    'F_ACTION'=> $base_url.get_query_string_diff(array())
591  )
592);
593
594if (is_autorize_status(ACCESS_WEBMASTER))
595{
596  // TabSheet
597  $tabsheet = new tabsheet();
598  // TabSheet initialization
599  $tabsheet->add('param', l10n('nbm_param_mode'),
600    add_url_params($base_url.get_query_string_diff(array('mode', 'select')),
601        array('mode' => 'param')));
602  $tabsheet->add('subscribe', l10n('nbm_subscribe_mode'),
603    add_url_params($base_url.get_query_string_diff(array('mode', 'select')),
604      array('mode' => 'subscribe')));
605  $tabsheet->add('send', l10n('nbm_send_mode'),
606    add_url_params($base_url.get_query_string_diff(array('mode', 'select')),
607      array('mode' => 'send')));
608  // TabSheet selection
609  $tabsheet->select($page['mode']);
610  // Assign tabsheet to template
611  $tabsheet->assign();
612}
613
614if ($must_repost)
615{
616  // Get name of submit button
617  $repost_submit_name = '';
618  if (isset($_POST['falsify']))
619  {
620    $repost_submit_name = 'falsify';
621  }
622  elseif (isset($_POST['trueify']))
623  {
624    $repost_submit_name = 'trueify';
625  }
626  elseif (isset($_POST['send_submit']))
627  {
628    $repost_submit_name = 'send_submit';
629  }
630
631  $template->assign('REPOST_SUBMIT_NAME', $repost_submit_name);
632}
633
634switch ($page['mode'])
635{
636  case 'param' :
637  {
638    $template->assign(
639      $page['mode'],
640      array(
641        'SEND_HTML_MAIL' => $conf['nbm_send_html_mail'],
642        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
643        'SEND_DETAILED_CONTENT' => $conf['nbm_send_detailed_content'],
644        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
645        'SEND_RECENT_POST_DATES' => $conf['nbm_send_recent_post_dates'],
646        ));
647    break;
648  }
649
650  case 'subscribe' :
651  {
652    $template->assign( $page['mode'], true );
653
654    $template->assign(
655      array(
656        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
657        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
658        )
659      );
660
661    $data_users = get_user_notifications('subscribe');
662   
663    $opt_true = array();
664    $opt_true_selected = array();
665    $opt_false = array();
666    $opt_false_selected = array();
667    foreach ($data_users as $nbm_user)
668    {
669      if (get_boolean($nbm_user['enabled']))
670      {
671        $opt_true[ $nbm_user['check_key'] ] = $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']';
672        if ((isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true'])))
673        {
674          $opt_true_selected[] = $nbm_user['check_key'];
675        }
676      }
677      else
678      {
679        $opt_false[ $nbm_user['check_key'] ] = $nbm_user['username'].'['.get_email_address_as_display_text($nbm_user['mail_address']).']';
680        if (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
681        {
682          $opt_false_selected[] = $nbm_user['check_key'];
683        }
684      }
685    }
686    $template->assign( array(
687        'category_option_true'          => $opt_true,
688        'category_option_true_selected' => $opt_true_selected,
689        'category_option_false'         => $opt_false,
690        'category_option_false_selected' => $opt_false_selected,
691        )
692    );
693    $template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
694    break;
695  }
696
697  case 'send' :
698  {
699    $tpl_var = array('users'=> array() );
700
701    $data_users = do_action_send_mail_notification('list_to_send');
702
703    $tpl_var['CUSTOMIZE_MAIL_CONTENT'] = 
704      isset($_POST['send_customize_mail_content']) 
705        ? stripslashes($_POST['send_customize_mail_content']) 
706        : $conf['nbm_complementary_mail_content'];
707
708    if  (count($data_users))
709    {
710      foreach ($data_users as $nbm_user)
711      {
712        if (
713            (!$must_repost) or // Not timeout, normal treatment
714            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
715            )
716        {
717          $tpl_var['users'][] = 
718            array(
719              'ID' => $nbm_user['check_key'],
720              'CHECKED' =>  ( // not check if not selected,  on init select<all
721                              isset($_POST['send_selection']) and // not init
722                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
723                            )   ? '' : 'checked="checked"',
724              'USERNAME'=> $nbm_user['username'],
725              'EMAIL' => get_email_address_as_display_text($nbm_user['mail_address']),
726              'LAST_SEND'=> $nbm_user['last_send']
727              );
728        }
729      }
730    }
731    $template->assign($page['mode'], $tpl_var);
732    break;
733  }
734}
735
736// +-----------------------------------------------------------------------+
737// | Sending html code                                                     |
738// +-----------------------------------------------------------------------+
739$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
740
741?>
Note: See TracBrowser for help on using the repository browser.