source: trunk/admin/notification_by_mail.php @ 1160

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

Issue ID 330:

o Remove Apache functions
o Show redirect message on 2 lines

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