source: trunk/admin/notification_by_mail.php @ 1232

Last change on this file since 1232 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
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-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$
10// | last update   : $Date: 2006-04-21 17:18:50 +0000 (Fri, 21 Apr 2006) $
11// | last modifier : $Author: rub $
12// | revision      : $Revision: 1232 $
13// +-----------------------------------------------------------------------+
14// | This program is free software; you can redistribute it and/or modify  |
15// | it under the terms of the GNU General Public License as published by  |
16// | the Free Software Foundation                                          |
17// |                                                                       |
18// | This program is distributed in the hope that it will be useful, but   |
19// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
20// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
21// | General Public License for more details.                              |
22// |                                                                       |
23// | You should have received a copy of the GNU General Public License     |
24// | along with this program; if not, write to the Free Software           |
25// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
26// | USA.                                                                  |
27// +-----------------------------------------------------------------------+
28
29// +-----------------------------------------------------------------------+
30// | include                                                               |
31// +-----------------------------------------------------------------------+
32
33if (!defined('PHPWG_ROOT_PATH'))
34{
35  die ("Hacking attempt!");
36}
37
38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
39include_once(PHPWG_ROOT_PATH.'admin/include/functions_notification_by_mail.inc.php');
40include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
41include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
42include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
43
44// +-----------------------------------------------------------------------+
45// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49// +-----------------------------------------------------------------------+
50// | Initialization                                                        |
51// +-----------------------------------------------------------------------+
52$base_url = get_root_url().'admin.php';
53$must_repost = false;
54
55// +-----------------------------------------------------------------------+
56// | functions                                                             |
57// +-----------------------------------------------------------------------+
58
59/*
60 * Do timeout treatment in order to finish to send mails
61 *
62 * @param $post_keyname: key of check_key post array
63 * @param check_key_treated: array of check_key treated
64 * @return none
65 */
66function do_timeout_treatment($post_keyname, $check_key_treated = array())
67{
68  global $env_nbm, $base_url, $page, $must_repost;
69
70  if ($env_nbm['is_sendmail_timeout'])
71  {
72    if (isset($_POST[$post_keyname]))
73    {
74      $post_count = count($_POST[$post_keyname]);
75      $treated_count = count($check_key_treated);
76      if ($treated_count != 0)
77      {
78        $time_refresh = ceil((get_moment() - $env_nbm['start_time']) * $post_count / $treated_count);
79      }
80      else
81      {
82        $time_refresh = 0;
83      }
84      $_POST[$post_keyname] = array_diff($_POST[$post_keyname], $check_key_treated);
85
86      $must_repost = true;
87      array_push($page['errors'], sprintf(l10n('nbm_background_treatment_redirect'), $time_refresh));
88    }
89  }
90
91}
92
93/*
94 * Get the authorized_status for each tab
95 * return corresponding status
96 */
97function get_tab_status($mode)
98{
99  $result = ACCESS_WEBMASTER;
100  switch ($mode)
101  {
102    case 'param':
103    case 'subscribe':
104      $result = ACCESS_WEBMASTER;
105      break;
106    case 'send':
107      $result = ACCESS_ADMINISTRATOR;
108      break;
109    default:
110      $result = ACCESS_WEBMASTER;
111      break;
112  }
113  return $result;
114}
115
116/*
117 * Inserting News users
118 */
119function insert_new_data_user_mail_notification()
120{
121  global $conf, $page, $env_nbm;
122
123  // Set null mail_address empty
124  $query = '
125update
126  '.USERS_TABLE.'
127set
128  '.$conf['user_fields']['email'].' = null
129where
130  trim('.$conf['user_fields']['email'].') = \'\';';
131  pwg_query($query);
132
133  // null mail_address are not selected in the list
134  $query = '
135select
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
139from
140  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.'.$conf['user_fields']['id'].' = m.user_id
141where
142  u.'.$conf['user_fields']['email'].' is not null and
143  m.user_id is null
144order by
145  user_id;';
146
147  $result = pwg_query($query);
148
149  if (mysql_num_rows($result) > 0)
150  {
151    $inserts = array();
152    $check_key_list = array();
153
154    while ($nbm_user = mysql_fetch_array($result))
155    {
156      // Calculate key
157      $nbm_user['check_key'] = find_available_check_key();
158
159      // Save key
160      array_push($check_key_list, $nbm_user['check_key']);
161
162      // Insert new nbm_users
163      array_push
164      (
165        $inserts, 
166        array
167        (
168          'user_id' => $nbm_user['user_id'],
169          'check_key' => $nbm_user['check_key'],
170          'enabled' => 'false' // By default if false, set to true with specific functions
171        )
172      );
173
174      array_push($page['infos'], sprintf(l10n('nbm_user_x_added'), $nbm_user['username'], $nbm_user['mail_address']));
175    }
176
177    // Insert new nbm_users
178    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
179    // Update field enabled with specific function
180    $check_key_treated = do_subscribe_unsubcribe_notification_by_mail
181    (
182      true,
183      $conf['nbm_default_value_user_enabled'],
184      $check_key_list
185    );
186
187     // On timeout simulate like tabsheet send
188    if ($env_nbm['is_sendmail_timeout'])
189    {
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 )
192      {
193        $query = 'delete from '.USER_MAIL_NOTIFICATION_TABLE.' where check_key in ('.implode(",", $quoted_check_key_list).');';
194        $result = pwg_query($query);
195
196        redirect($base_url.get_query_string_diff(array()), l10n('nbm_redirect_msg'));
197      }
198    }
199  }
200}
201
202/*
203 * Send mail for notification to all users
204 * Return list of "selected" users for 'list_to_send'
205 * Return list of "treated" check_key for 'send'
206 */
207function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
208{
209  global $conf, $page, $user, $lang_info, $lang, $env_nbm;
210  $return_list = array();
211 
212  if (in_array($action, array('list_to_send', 'send')))
213  {
214    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
215
216    $is_action_send = ($action == 'send');
217
218    if (isset($customize_mail_content))
219    {
220      $customize_mail_content = $conf['nbm_complementary_mail_content'];
221    }
222
223    // disabled and null mail_address are not selected in the list
224    $data_users = get_user_notifications('send', $check_key_list);
225
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
229    // Check if exist news to list user or send mails
230    if ((!$is_list_all_without_test) or ($is_action_send))
231    {
232      if (count($data_users) > 0)
233      {
234        $datas = array();
235
236        // Prepare message after change language
237        if ($is_action_send)
238        {
239          $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
240        }
241        else
242        {
243          $msg_break_timeout = l10n('nbm_break_timeout_list_user');
244        }
245
246        // Begin nbm users environment
247        begin_users_env_nbm($is_action_send);
248
249        foreach ($data_users as $nbm_user)
250        {
251          if ((!$is_action_send) and check_sendmail_timeout())
252          {
253            // Stop fill list on 'list_to_send', if the quota is override
254            array_push($page['infos'], $msg_break_timeout);
255            break;
256          }
257          if (($is_action_send) and check_sendmail_timeout())
258          {
259            // Stop fill list on 'send', if the quota is override
260            array_push($page['errors'], $msg_break_timeout);
261            break;
262          }
263
264          // set env nbm user
265          set_user_id_on_env_nbm($nbm_user['user_id']);
266
267          if ($is_action_send)
268          {
269            // Fill return list of "treated" check_key for 'send'
270            array_push($return_list, $nbm_user['check_key']);
271            $message = '';
272
273            if ($conf['nbm_send_detailed_content'])
274            {
275               $news = news($nbm_user['last_send'], $dbnow);
276               $exist_data = count($news) > 0;
277            }
278            else
279            {
280              $exist_data = news_exists($nbm_user['last_send'], $dbnow);
281            }
282
283            if ($exist_data)
284            {
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              }
326            }
327          }
328          else
329          {
330            if (news_exists($nbm_user['last_send'], $dbnow))
331            {
332              // Fill return list of "selected" users for 'list_to_send'
333              array_push($return_list, $nbm_user);
334            }
335          }
336        }
337
338        // Restore nbm environment
339        end_users_env_nbm();
340
341        if ($is_action_send)
342        {
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();
353        }
354      }
355      else
356      {
357        if ($is_action_send)
358        {
359          array_push($page['errors'], l10n('nbm_no_user_to send_notifications_by_mail'));
360        }
361      }
362    }
363    else
364    {
365      // Quick List, don't check news
366      // Fill return list of "selected" users for 'list_to_send'
367      $return_list = $data_users;
368    }
369  }
370
371  // Return list of "selected" users for 'list_to_send'
372  // Return list of "treated" check_key for 'send'
373  return $return_list;
374}
375
376// +-----------------------------------------------------------------------+
377// | Main                                                                  |
378// +-----------------------------------------------------------------------+
379if (!isset($_GET['mode']))
380{
381  $page['mode'] = 'send';
382}
383else
384{
385  $page['mode'] = $_GET['mode'];
386}
387
388// +-----------------------------------------------------------------------+
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\\_%\'');
412    while ($nbm_user = mysql_fetch_array($result))
413    {
414      if (isset($_POST['param_submit']))
415      {
416        if (isset($_POST[$nbm_user['param']]))
417        {
418          $value = $_POST[$nbm_user['param']];
419
420          $query = '
421update
422  '.CONFIG_TABLE.'
423set
424  value = \''. str_replace("\'", "''", $value).'\'
425where
426  param = \''.$nbm_user['param'].'\';';
427          pwg_query($query);
428          $updated_param_count += 1;
429        }
430      }
431
432      $conf[$nbm_user['param']] = $nbm_user['value'];
433
434      // if the parameter is present in $_POST array (if a form is submited), we
435      // override it with the submited value
436      if (isset($_POST[$nbm_user['param']]))
437      {
438        $conf[$nbm_user['param']] = stripslashes($_POST[$nbm_user['param']]);
439      }
440
441      // If the field is true or false, the variable is transformed into a
442      // boolean value.
443      if ($conf[$nbm_user['param']] == 'true' or $conf[$nbm_user['param']] == 'false')
444      {
445        $conf[$nbm_user['param']] = get_boolean($conf[$nbm_user['param']]);
446      }
447    }
448   
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    {
458      $check_key_treated = unsubcribe_notification_by_mail(true, $_POST['cat_true']);
459      do_timeout_treatment('cat_true', $check_key_treated);
460    }
461    else
462    if (isset($_POST['trueify']) and isset($_POST['cat_false']))
463    {
464      $check_key_treated = subcribe_notification_by_mail(true, $_POST['cat_false']);
465      do_timeout_treatment('cat_false', $check_key_treated);
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    {
474      $check_key_treated = do_action_send_mail_notification('send', $_POST['send_selection'], $_POST['send_customize_mail_content']);
475      do_timeout_treatment('send_selection', $check_key_treated);
476    }
477  }
478}
479
480// +-----------------------------------------------------------------------+
481// | template initialization                                               |
482// +-----------------------------------------------------------------------+
483$template->set_filenames
484(
485  array
486  (
487    'double_select' => 'admin/double_select.tpl',
488    'notification_by_mail'=>'admin/notification_by_mail.tpl'
489  )
490);
491
492$template->assign_vars
493(
494  array
495  (
496    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
497    'U_HELP' => add_url_params(get_root_url().'popuphelp.php', array('page' => 'notification_by_mail')),
498    'F_ACTION'=> $base_url.get_query_string_diff(array())
499  )
500);
501
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
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
543switch ($page['mode'])
544{
545  case 'param' :
546  {
547    $template->assign_block_vars(
548      $page['mode'],
549      array(
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']
554        ));
555    break;
556  }
557
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
572    $data_users = get_user_notifications('subscribe');
573    foreach ($data_users as $nbm_user)
574    {
575      $template->assign_block_vars(
576        (get_boolean($nbm_user['enabled']) ? 'category_option_true' : 'category_option_false'),
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"' : '',
581              'VALUE' => $nbm_user['check_key'],
582              'OPTION' => $nbm_user['username'].'['.$nbm_user['mail_address'].']'
583          ));
584    }
585
586    break;
587  }
588
589  case 'send' :
590  {
591    $template->assign_block_vars($page['mode'], array());
592
593    $data_users = do_action_send_mail_notification('list_to_send');
594
595    if  (count($data_users) == 0)
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(
604          'CUSTOMIZE_MAIL_CONTENT' => isset($_POST['send_customize_mail_content']) ? stripslashes($_POST['send_customize_mail_content']) : $conf['nbm_complementary_mail_content']
605          ));
606
607      foreach ($data_users as $num => $nbm_user)
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        {
614          $template->assign_block_vars(
615            $page['mode'].'.send_data.user_send_mail',
616            array(
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
621                              !in_array($nbm_user['check_key'], $_POST['send_selection']) // not selected
622                            )   ? '' : 'checked="checked"',
623              'USERNAME'=> $nbm_user['username'],
624              'EMAIL' => $nbm_user['mail_address'],
625              'LAST_SEND'=> $nbm_user['last_send']
626              ));
627        }
628      }
629    }
630
631    break;
632  }
633}
634
635// +-----------------------------------------------------------------------+
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
641?>
Note: See TracBrowser for help on using the repository browser.