source: trunk/admin/notification_by_mail.php @ 1246

Last change on this file since 1246 was 1232, checked in by rub, 18 years ago

Issue ID 0000342:

o Improved NBM messages

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.7 KB
RevLine 
[1049]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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// | Copyright (C) 2006 Ruben ARNAUD - team@phpwebgallery.net              |
7// +-----------------------------------------------------------------------+
8// | branch        : BSF (Best So Far)
9// | file          : $RCSfile$
[1094]10// | last update   : $Date: 2006-04-21 17:18:50 +0000 (Fri, 21 Apr 2006) $
11// | last modifier : $Author: rub $
12// | revision      : $Revision: 1232 $
[1049]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// +-----------------------------------------------------------------------+
[1091]30// | include                                                               |
[1049]31// +-----------------------------------------------------------------------+
32
33if (!defined('PHPWG_ROOT_PATH'))
34{
35  die ("Hacking attempt!");
36}
[1072]37
[1049]38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1105]39include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
[1049]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// +-----------------------------------------------------------------------+
[1072]45// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49// +-----------------------------------------------------------------------+
[1156]50// | Initialization                                                        |
51// +-----------------------------------------------------------------------+
52$base_url = get_root_url().'admin.php';
[1177]53$must_repost = false;
[1156]54
55// +-----------------------------------------------------------------------+
[1091]56// | functions                                                             |
[1049]57// +-----------------------------------------------------------------------+
[1105]58
[1049]59/*
[1177]60 * Do timeout treatment in order to finish to send mails
[1156]61 *
62 * @param $post_keyname: key of check_key post array
[1177]63 * @param check_key_treated: array of check_key treated
[1156]64 * @return none
65 */
[1177]66function do_timeout_treatment($post_keyname, $check_key_treated = array())
[1156]67{
[1177]68  global $env_nbm, $base_url, $page, $must_repost;
[1156]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      {
[1177]82        $time_refresh = 0;
[1156]83      }
84      $_POST[$post_keyname] = array_diff($_POST[$post_keyname], $check_key_treated);
[1214]85
[1177]86      $must_repost = true;
87      array_push($page['errors'], sprintf(l10n('nbm_background_treatment_redirect'), $time_refresh));
[1156]88    }
89  }
90
91}
92
93/*
[1105]94 * Get the authorized_status for each tab
95 * return corresponding status
[1115]96 */
[1105]97function get_tab_status($mode)
[1049]98{
[1105]99  $result = ACCESS_WEBMASTER;
100  switch ($mode)
[1049]101  {
[1105]102    case 'param':
[1115]103    case 'subscribe':
[1105]104      $result = ACCESS_WEBMASTER;
105      break;
[1115]106    case 'send':
[1105]107      $result = ACCESS_ADMINISTRATOR;
108      break;
109    default:
110      $result = ACCESS_WEBMASTER;
111      break;
[1049]112  }
[1105]113  return $result;
[1049]114}
115
[1115]116/*
[1105]117 * Inserting News users
[1049]118 */
[1105]119function insert_new_data_user_mail_notification()
[1049]120{
[1156]121  global $conf, $page, $env_nbm;
[1049]122
[1070]123  // Set null mail_address empty
[1049]124  $query = '
[1094]125update
[1070]126  '.USERS_TABLE.'
127set
[1115]128  '.$conf['user_fields']['email'].' = null
[1070]129where
[1115]130  trim('.$conf['user_fields']['email'].') = \'\';';
[1070]131  pwg_query($query);
132
[1105]133  // null mail_address are not selected in the list
[1070]134  $query = '
[1049]135select
[1115]136  u.'.$conf['user_fields']['id'].' as user_id,
137  u.'.$conf['user_fields']['username'].' as username,
138  u.'.$conf['user_fields']['email'].' as mail_address
[1049]139from
[1115]140  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.'.$conf['user_fields']['id'].' = m.user_id
[1049]141where
[1115]142  u.'.$conf['user_fields']['email'].' is not null and
[1074]143  m.user_id is null
[1049]144order by
[1115]145  user_id;';
[1049]146
147  $result = pwg_query($query);
148
149  if (mysql_num_rows($result) > 0)
150  {
151    $inserts = array();
[1105]152    $check_key_list = array();
[1049]153
[1115]154    while ($nbm_user = mysql_fetch_array($result))
[1049]155    {
[1105]156      // Calculate key
[1115]157      $nbm_user['check_key'] = find_available_check_key();
[1105]158
159      // Save key
[1115]160      array_push($check_key_list, $nbm_user['check_key']);
[1105]161
[1115]162      // Insert new nbm_users
[1105]163      array_push
164      (
[1115]165        $inserts, 
[1105]166        array
167        (
[1115]168          'user_id' => $nbm_user['user_id'],
169          'check_key' => $nbm_user['check_key'],
[1105]170          'enabled' => 'false' // By default if false, set to true with specific functions
171        )
172      );
173
[1116]174      array_push($page['infos'], sprintf(l10n('nbm_user_x_added'), $nbm_user['username'], $nbm_user['mail_address']));
[1049]175    }
176
[1115]177    // Insert new nbm_users
[1049]178    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
[1105]179    // Update field enabled with specific function
[1156]180    $check_key_treated = do_subscribe_unsubcribe_notification_by_mail
[1105]181    (
[1116]182      true,
183      $conf['nbm_default_value_user_enabled'],
[1105]184      $check_key_list
185    );
[1156]186
187     // On timeout simulate like tabsheet send
188    if ($env_nbm['is_sendmail_timeout'])
189    {
[1177]190      $quoted_check_key_list = quote_check_key_list(array_diff($check_key_list, $check_key_treated));
191      if (count($quoted_check_key_list) != 0 )
[1156]192      {
[1177]193        $query = 'delete from '.USER_MAIL_NOTIFICATION_TABLE.' where check_key in ('.implode(",", $quoted_check_key_list).');';
194        $result = pwg_query($query);
195
[1232]196        redirect($base_url.get_query_string_diff(array()), l10n('nbm_redirect_msg'));
[1156]197      }
198    }
[1049]199  }
200}
201
202/*
[1105]203 * Send mail for notification to all users
[1156]204 * Return list of "selected" users for 'list_to_send'
205 * Return list of "treated" check_key for 'send'
[1049]206 */
[1116]207function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
[1049]208{
[1116]209  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
[1115]210  $return_list = array();
211 
[1116]212  if (in_array($action, array('list_to_send', 'send')))
[1105]213  {
[1115]214    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
[1105]215
[1115]216    $is_action_send = ($action == 'send');
[1049]217
[1115]218    if (isset($customize_mail_content))
219    {
220      $customize_mail_content = $conf['nbm_complementary_mail_content'];
221    }
[1049]222
[1115]223    // disabled and null mail_address are not selected in the list
224    $data_users = get_user_notifications('send', $check_key_list);
[1114]225
[1177]226    // List all if it's define on options or on timeout
227    $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
228
[1116]229    // Check if exist news to list user or send mails
[1214]230    if ((!$is_list_all_without_test) or ($is_action_send))
[1105]231    {
[1116]232      if (count($data_users) > 0)
[1115]233      {
[1116]234        $datas = array();
[1114]235
[1156]236        // Prepare message after change language
237        if ($is_action_send)
238        {
[1177]239          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
[1156]240        }
241        else
242        {
243          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
244        }
245
[1116]246        // Begin nbm users environment
247        begin_users_env_nbm($is_action_send);
[1105]248
[1116]249        foreach ($data_users as $nbm_user)
[1115]250        {
[1156]251          if ((!$is_action_send) and check_sendmail_timeout())
[1115]252          {
[1116]253            // Stop fill list on 'list_to_send', if the quota is override
[1156]254            array_push($page['infos'], $msg_break_timeout);
[1116]255            break;
[1115]256          }
[1156]257          if (($is_action_send) and check_sendmail_timeout())
[1115]258          {
[1116]259            // Stop fill list on 'send', if the quota is override
[1156]260            array_push($page['errors'], $msg_break_timeout);
[1116]261            break;
[1115]262          }
263
[1116]264          // set env nbm user
265          set_user_id_on_env_nbm($nbm_user['user_id']);
266
267          if ($is_action_send)
[1115]268          {
[1156]269            // Fill return list of "treated" check_key for 'send'
270            array_push($return_list, $nbm_user['check_key']);
[1116]271            $message = '';
[1115]272
273            if ($conf['nbm_send_detailed_content'])
274            {
[1116]275               $news = news($nbm_user['last_send'], $dbnow);
276               $exist_data = count($news) > 0;
[1115]277            }
278            else
279            {
[1116]280              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
[1115]281            }
282
[1116]283            if ($exist_data)
[1115]284            {
[1116]285              $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_object_news');
286              $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n";
287
288              if (!is_null($nbm_user['last_send']))
289                $message .= sprintf(l10n('nbm_content_new_elements_between'), $nbm_user['last_send'], $dbnow);
290              else
291                $message .= sprintf(l10n('nbm_content_new_elements'), $dbnow);
292
293              if ($conf['nbm_send_detailed_content'])
294              {
295                $message .= ":\n";
296
297                foreach ($news as $line)
298                {
299                  $message .= '  o '.$line."\n";
300                }
301                $message .= "\n";
302              }
303              else
304              {
305                $message .= ".\n";
306              }
307
308              $message .= sprintf(l10n('nbm_content_goto'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
309              $message .= $customize_mail_content."\n\n";
310              $message .= l10n('nbm_content_byebye')."\n   ".$env_nbm['send_as_name']."\n\n";
311
312              $message .= get_mail_content_subscribe_unsubcribe($nbm_user);
313
314              if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message))
315              {
316                inc_mail_sent_success($nbm_user);
317
318                $data = array('user_id' => $nbm_user['user_id'],
319                              'last_send' => $dbnow);
320                array_push($datas, $data);
321              }
322              else
323              {
324                inc_mail_sent_failed($nbm_user);
325              }
[1115]326            }
[1116]327          }
328          else
329          {
330            if (news_exists($nbm_user['last_send'], $dbnow))
[1115]331            {
[1156]332              // Fill return list of "selected" users for 'list_to_send'
[1116]333              array_push($return_list, $nbm_user);
[1115]334            }
335          }
[1049]336        }
[1116]337
338        // Restore nbm environment
339        end_users_env_nbm();
340
341        if ($is_action_send)
[1115]342        {
[1116]343          mass_updates(
344            USER_MAIL_NOTIFICATION_TABLE,
345            array(
346              'primary' => array('user_id'),
347              'update' => array('last_send')
348             ),
349             $datas
350             );
351
352          display_counter_info();
[1115]353        }
[1049]354      }
[1116]355      else
[1105]356      {
[1116]357        if ($is_action_send)
[1115]358        {
[1116]359          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
[1115]360        }
[1105]361      }
[1115]362    }
363    else
364    {
[1116]365      // Quick List, don't check news
[1156]366      // Fill return list of "selected" users for 'list_to_send'
[1116]367      $return_list = $data_users;
[1049]368    }
369  }
[1156]370
371  // Return list of "selected" users for 'list_to_send'
372  // Return list of "treated" check_key for 'send'
[1115]373  return $return_list;
[1049]374}
375
376// +-----------------------------------------------------------------------+
[1091]377// | Main                                                                  |
[1049]378// +-----------------------------------------------------------------------+
[1091]379if (!isset($_GET['mode']))
380{
381  $page['mode'] = 'send';
382}
383else
384{
385  $page['mode'] = $_GET['mode'];
386}
[1049]387
388// +-----------------------------------------------------------------------+
[1105]389// | Check Access and exit when user status is not ok                      |
390// +-----------------------------------------------------------------------+
391check_status(get_tab_status($page['mode']));
392
393// +-----------------------------------------------------------------------+
394// | Insert new users with mails                                           |
395// +-----------------------------------------------------------------------+
396if (!isset($_POST) or (count($_POST) ==0))
397{
398  // No insert data in post mode
399  insert_new_data_user_mail_notification();
400}
401
402// +-----------------------------------------------------------------------+
403// | Treatment of tab post                                                 |
404// +-----------------------------------------------------------------------+
405switch ($page['mode'])
406{
407  case 'param' :
408  {
409    $updated_param_count = 0;
410    // Update param
411    $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
[1115]412    while ($nbm_user = mysql_fetch_array($result))
[1105]413    {
414      if (isset($_POST['param_submit']))
415      {
[1115]416        if (isset($_POST[$nbm_user['param']]))
[1105]417        {
[1115]418          $value = $_POST[$nbm_user['param']];
[1105]419
420          $query = '
421update
422  '.CONFIG_TABLE.'
[1115]423set
[1105]424  value = \''. str_replace("\'", "''", $value).'\'
425where
[1115]426  param = \''.$nbm_user['param'].'\';';
[1105]427          pwg_query($query);
428          $updated_param_count += 1;
429        }
430      }
431
[1115]432      $conf[$nbm_user['param']] = $nbm_user['value'];
[1105]433
434      // if the parameter is present in $_POST array (if a form is submited), we
435      // override it with the submited value
[1115]436      if (isset($_POST[$nbm_user['param']]))
[1105]437      {
[1115]438        $conf[$nbm_user['param']] = stripslashes($_POST[$nbm_user['param']]);
[1105]439      }
440
441      // If the field is true or false, the variable is transformed into a
442      // boolean value.
[1115]443      if ($conf[$nbm_user['param']] == 'true' or $conf[$nbm_user['param']] == 'false')
[1105]444      {
[1115]445        $conf[$nbm_user['param']] = get_boolean($conf[$nbm_user['param']]);
[1105]446      }
447    }
[1115]448   
[1105]449    if ($updated_param_count != 0)
450    {
451      array_push($page['infos'], sprintf(l10n('nbm_updated_param_count'), $updated_param_count));
452    }
453  }
454  case 'subscribe' :
455  {
456    if (isset($_POST['falsify']) and isset($_POST['cat_true']))
457    {
[1156]458      $check_key_treated = unsubcribe_notification_by_mail(true, $_POST['cat_true']);
[1177]459      do_timeout_treatment('cat_true', $check_key_treated);
[1105]460    }
461    else
462    if (isset($_POST['trueify']) and isset($_POST['cat_false']))
463    {
[1156]464      $check_key_treated = subcribe_notification_by_mail(true, $_POST['cat_false']);
[1177]465      do_timeout_treatment('cat_false', $check_key_treated);
[1105]466    }
467    break;
468  }
469
470  case 'send' :
471  {
472    if (isset($_POST['send_submit']) and isset($_POST['send_selection']) and isset($_POST['send_customize_mail_content']))
473    {
[1156]474      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], $_POST['send_customize_mail_content']);
[1177]475      do_timeout_treatment('send_selection', $check_key_treated);
[1105]476    }
477  }
478}
479
480// +-----------------------------------------------------------------------+
[1091]481// | template initialization                                               |
[1049]482// +-----------------------------------------------------------------------+
[1105]483$template->set_filenames
484(
485  array
486  (
[1091]487    'double_select' => 'admin/double_select.tpl',
488    'notification_by_mail'=>'admin/notification_by_mail.tpl'
[1105]489  )
490);
[1049]491
[1105]492$template->assign_vars
493(
494  array
495  (
[1091]496    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
[1156]497    'U_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
[1091]498    'F_ACTION'=> $base_url.get_query_string_diff(array())
[1105]499  )
500);
[1091]501
[1105]502if (is_autorize_status(ACCESS_WEBMASTER))
503{
504  $template->assign_block_vars
505  (
506    'header_link',
507    array
508    (
509      'PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'param')),
510      'SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'subscribe')),
511      'SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode', 'select')), array('mode' => 'send'))
512    )
513  );
514}
515
[1177]516if ($must_repost)
517{
518  // Get name of submit button
519  $repost_submit_name = '';
520  if (isset($_POST['falsify']))
521  {
522    $repost_submit_name = 'falsify';
523  }
524  elseif (isset($_POST['trueify']))
525  {
526    $repost_submit_name = 'trueify';
527  }
528  elseif (isset($_POST['send_submit']))
529  {
530    $repost_submit_name = 'send_submit';
531  }
532
533  $template->assign_block_vars
534  (
535    'repost', 
536      array
537      (
538        'REPOST_SUBMIT_NAME' => $repost_submit_name
539      )
540    );
541}
542
[1091]543switch ($page['mode'])
544{
545  case 'param' :
546  {
547    $template->assign_block_vars(
548      $page['mode'],
549      array(
[1105]550        'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
551        'SEND_DETAILED_CONTENT_YES' => ($conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
552        'SEND_DETAILED_CONTENT_NO' => (!$conf['nbm_send_detailed_content'] ? 'checked="checked"' : ''),
553        'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content']
[1091]554        ));
555    break;
556  }
[1105]557
[1091]558  case 'subscribe' :
559  {
560    $template->assign_block_vars(
561      $page['mode'],
562      array(
563        ));
564
565    $template->assign_vars(
566      array(
567        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
568        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
569        )
570      );
571
[1115]572    $data_users = get_user_notifications('subscribe');
573    foreach ($data_users as $nbm_user)
[1105]574    {
[1114]575      $template->assign_block_vars(
[1115]576        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
[1116]577        array('SELECTED' => ( // Keep selected user where enabled are not changed when change has been notify
578                              get_boolean($nbm_user['enabled']) ? (isset($_POST['falsify']) and isset($_POST['cat_true']) and in_array($nbm_user['check_key'], $_POST['cat_true']))
579                                                                : (isset($_POST['trueify']) and isset($_POST['cat_false']) and in_array($nbm_user['check_key'], $_POST['cat_false']))
580                            ) ? 'selected="selected"' : '',
[1115]581              'VALUE' => $nbm_user['check_key'],
582              'OPTION' => $nbm_user['username'].'['.$nbm_user['mail_address'].']'
[1114]583          ));
[1105]584    }
[1115]585
[1091]586    break;
587  }
[1105]588
[1091]589  case 'send' :
590  {
[1105]591    $template->assign_block_vars($page['mode'], array());
[1091]592
[1116]593    $data_users = do_action_send_mail_notification('list_to_send');
[1091]594
[1115]595    if  (count($data_users) == 0)
[1105]596    {
597      $template->assign_block_vars($page['mode'].'.send_empty', array());
598    }
599    else
600    {
601      $template->assign_block_vars(
602        $page['mode'].'.send_data',
603        array(
[1160]604          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
[1105]605          ));
[1115]606
607      foreach ($data_users as $num => $nbm_user)
[1214]608      {
609        if (
610            (!$must_repost) or // Not timeout, normal treatment
611            (($must_repost) and in_array($nbm_user['check_key'], $_POST['send_selection']))  // Must be repost, show only user to send
612            )
613        {
[1115]614          $template->assign_block_vars(
[1105]615            $page['mode'].'.send_data.user_send_mail',
616            array(
[1115]617              'CLASS' => ($num % 2 == 1) ? 'nbm_user2' : 'nbm_user1',
618              'ID' => $nbm_user['check_key'],
619              'CHECKED' =>  ( // not check if not selected,  on init select<all
620                              isset($_POST['send_selection']) and // not init
[1177]621                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
[1115]622                            )   ? '' : 'checked="checked"',
623              'USERNAME'=> $nbm_user['username'],
624              'EMAIL' => $nbm_user['mail_address'],
625              'LAST_SEND'=> $nbm_user['last_send']
[1105]626              ));
[1214]627        }
628      }
[1105]629    }
[1091]630
631    break;
632  }
633}
634
[1049]635// +-----------------------------------------------------------------------+
[1091]636// | Sending html code                                                     |
637// +-----------------------------------------------------------------------+
638$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
639$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
640
[1049]641?>
Note: See TracBrowser for help on using the repository browser.