source: trunk/admin/include/functions_notification_by_mail.inc.php @ 1792

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

Issue 0000639: Force selected page on index.php
Continue the Radu idea, restrict more use of random list

Issue 0000598: NBM: Add new informations
Add css on mail
Use user template for mail (on next commit, best control)

Comment mass_inserts because don't with MySQL 4.1.9

File size: 15.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// | Copyright (C) 2006-2007 Ruben ARNAUD - team@phpwebgallery.net         |
7// +-----------------------------------------------------------------------+
8// | branch        : BSF (Best So Far)
9// | file          : $RCSfile$
10// | last update   : $Date: 2006-03-23 02:49:04 +0100 (jeu., 23 mars 2006) $
11// | last modifier : $Author: rvelices $
12// | revision      : $Revision: 1094 $
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/* nbm_global_var */
30$env_nbm = array
31          (
32            'start_time' => get_moment(),
33            'sendmail_timeout' => (intval(ini_get('max_execution_time')) * $conf['nbm_max_treatment_timeout_percent']),
34            'is_sendmail_timeout' => false
35          );
36
37if
38  (
39    (!isset($env_nbm['sendmail_timeout'])) or
40    (!is_numeric($env_nbm['sendmail_timeout'])) or
41    ($env_nbm['sendmail_timeout'] <= 0)
42  )
43{
44  $env_nbm['sendmail_timeout'] = $conf['nbm_treatment_timeout_default'];
45}
46
47/*
48 * Search an available check_key
49 *
50 * It's a copy of function find_available_feed_id
51 *
52 * @return string nbm identifier
53 */
54function find_available_check_key()
55{
56  while (true)
57  {
58    $key = generate_key(16);
59    $query = '
60select
61  count(*)
62from
63  '.USER_MAIL_NOTIFICATION_TABLE.'
64where
65  check_key = \''.$key.'\';';
66
67    list($count) = mysql_fetch_row(pwg_query($query));
68    if ($count == 0)
69    {
70      return $key;
71    }
72  }
73}
74
75/*
76 * Check sendmail timeout state
77 *
78 * @return true, if it's timeout
79 */
80function check_sendmail_timeout()
81{
82  global $env_nbm;
83
84  $env_nbm['is_sendmail_timeout'] = ((get_moment() - $env_nbm['start_time']) > $env_nbm['sendmail_timeout']);
85
86  return $env_nbm['is_sendmail_timeout'];
87}
88
89
90/*
91 * Add quote to all elements of check_key_list
92 *
93 * @return quoted check key list
94 */
95function quote_check_key_list($check_key_list = array())
96{
97  return array_map(create_function('$s', 'return \'\\\'\'.$s.\'\\\'\';'), $check_key_list);
98}
99
100/*
101 * Execute all main queries to get list of user
102 *
103 * Type are the type of list 'subscribe', 'send'
104 *
105 * return array of users
106 */
107function get_user_notifications($action, $check_key_list = array(), $enabled_filter_value = '')
108{
109  global $conf;
110
111  $data_users = array();
112
113  if (in_array($action, array('subscribe', 'send')))
114  {
115    $quoted_check_key_list = quote_check_key_list($check_key_list);
116    if (count($quoted_check_key_list) != 0 )
117    {
118      $query_and_check_key = ' and
119    check_key in ('.implode(",", $quoted_check_key_list).') ';
120    }
121    else
122    {
123      $query_and_check_key = '';
124    }
125
126    $query = '
127select
128  N.user_id,
129  N.check_key,
130  U.'.$conf['user_fields']['username'].' as username,
131  U.'.$conf['user_fields']['email'].' as mail_address,
132  N.enabled,
133  N.last_send
134from
135  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
136  '.USERS_TABLE.' as U
137where
138  N.user_id =  U.'.$conf['user_fields']['id'];
139 
140    if ($action == 'send')
141    {
142      // No mail empty and all users enabled
143      $query .= ' and
144  N.enabled = \'true\' and
145  U.'.$conf['user_fields']['email'].' is not null';
146    }
147
148    $query .= $query_and_check_key;
149
150    if (isset($enabled_filter_value) and ($enabled_filter_value != ''))
151    {
152      $query .= ' and
153        N.enabled = \''.boolean_to_string($enabled_filter_value).'\'';
154    }
155
156    $query .= '
157order by';
158
159    if ($action == 'send')
160    {
161      $query .= '
162  last_send, username;';
163    }
164    else
165    {
166      $query .= '
167  username;';
168    }
169
170    $query .= ';';
171
172    $result = pwg_query($query);
173    if (!empty($result))
174    {
175      while ($nbm_user = mysql_fetch_array($result))
176      {
177        array_push($data_users, $nbm_user);
178      }
179    }
180  }
181  return $data_users;
182}
183
184/*
185 * Begin of use nbm environment
186 * Prepare and save current environment and initialize data in order to send mail
187 *
188 * Return none
189 */
190function begin_users_env_nbm($is_to_send_mail = false)
191{
192  global $user, $lang, $lang_info, $conf, $env_nbm;
193
194  // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
195  $env_nbm['save_user'] = $user;
196  $env_nbm['save_lang_info'] = $lang_info;
197  $env_nbm['save_lang'] = $lang;
198  // Last Language
199  $env_nbm['last_language'] = $user['language'];
200
201  $env_nbm['is_to_send_mail'] = $is_to_send_mail;
202
203  if ($is_to_send_mail)
204  {
205    // Init mail configuration
206    $env_nbm['email_format'] = get_str_email_format($conf['nbm_send_html_mail']);
207    $env_nbm['send_as_name'] = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']);
208    $env_nbm['send_as_mail_address'] = get_webmaster_mail_address();
209    $env_nbm['send_as_mail_formated'] = format_email($env_nbm['send_as_name'], $env_nbm['send_as_mail_address']);
210    // Init mail counter
211    $env_nbm['error_on_mail_count'] = 0;
212    $env_nbm['sent_mail_count'] = 0;
213    // Save sendmail message info and error in the original language
214    $env_nbm['msg_info'] = l10n('nbm_msg_mail_sent_to');
215    $env_nbm['msg_error'] = l10n('nbm_msg_error_sending_email_to');
216  }
217}
218
219/*
220 * End of use nbm environment
221 * Restore environment
222 *
223 * Return none
224 */
225function end_users_env_nbm()
226{
227  global $user, $lang, $lang_info, $env_nbm;
228
229  // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
230  $user = $env_nbm['save_user'];
231  $lang_info = $env_nbm['save_lang_info'];
232  $lang = $env_nbm['save_lang'];
233
234  if ($env_nbm['is_to_send_mail'])
235  {
236    unset($env_nbm['email_format']);
237    unset($env_nbm['send_as_name']);
238    unset($env_nbm['send_as_mail_address']);
239    unset($env_nbm['send_as_mail_formated']);
240    // Don t unset counter
241    //unset($env_nbm['error_on_mail_count']);
242    //unset($env_nbm['sent_mail_count']);
243    unset($env_nbm['msg_info']);
244    unset($env_nbm['msg_error']);
245  }
246
247  unset($env_nbm['save_user']);
248  unset($env_nbm['save_lang_info']);
249  unset($env_nbm['save_lang']);
250  unset($env_nbm['last_language']);
251  unset($env_nbm['is_to_send_mail']);
252
253}
254
255/*
256 * Set user on nbm enviromnent
257 *
258 * Return none
259 */
260function set_user_on_env_nbm($user_id, $is_action_send)
261{
262  global $user, $lang, $lang_info, $env_nbm;
263
264  $user = array();
265  $user['id'] = $user_id;
266  $user = array_merge($user, getuserdata($user['id'], true));
267
268  list($user['template'], $user['theme']) = explode('/', $user['template']);
269
270  if ($env_nbm['last_language'] != $user['language'])
271  {
272    $env_nbm['last_language'] = $user['language'];
273
274    // Re-Init language arrays
275    $lang_info = array();
276    $lang  = array();
277
278    // language files
279    include(get_language_filepath('common.lang.php'));
280    // No test admin because script is checked admin (user selected no)
281    // Translations are in admin file too
282    include(get_language_filepath('admin.lang.php'));
283    trigger_action('loading_lang');
284    @include(get_language_filepath('local.lang.php'));
285  }
286 
287  if ($is_action_send)
288  {
289    $env_nbm['mail_template'] = get_mail_template($env_nbm['email_format']);
290    $env_nbm['mail_template']->set_filename('notification_by_mail', 'admin/notification_by_mail.tpl');
291  }
292}
293
294/*
295 * Unset user on nbm enviromnent
296 *
297 * Return none
298 */
299function unset_user_on_env_nbm()
300{
301  global $env_nbm;
302
303  unset($env_nbm['mail_template']);
304}
305
306/*
307 * Inc Counter success
308 *
309 * Return none
310 */
311function inc_mail_sent_success($nbm_user)
312{
313  global $page, $env_nbm;
314
315  $env_nbm['sent_mail_count'] += 1;
316  array_push($page['infos'], sprintf($env_nbm['msg_info'], $nbm_user['username'], $nbm_user['mail_address']));
317}
318
319/*
320 * Inc Counter failed
321 *
322 * Return none
323 */
324function inc_mail_sent_failed($nbm_user)
325{
326  global $page, $env_nbm;
327
328  $env_nbm['error_on_mail_count'] += 1;
329  array_push($page['errors'], sprintf($env_nbm['msg_error'], $nbm_user['username'], $nbm_user['mail_address']));
330}
331
332/*
333 * Display Counter Info
334 *
335 * Return none
336 */
337function display_counter_info()
338{
339  global $page, $env_nbm;
340
341  if ($env_nbm['error_on_mail_count'] != 0)
342  {
343    array_push($page['errors'], l10n_dec('nbm_msg_n_mail_not_send', 'nbm_msg_n_mails_not_send', $env_nbm['error_on_mail_count']));
344    if ($env_nbm['sent_mail_count'] != 0)
345      array_push($page['infos'], l10n_dec('nbm_msg_n_mail_sent', 'nbm_msg_n_mails_sent', $env_nbm['sent_mail_count']));
346  }
347  else
348  {
349    if ($env_nbm['sent_mail_count'] == 0)
350      array_push($page['infos'], l10n('nbm_no_mail_to_send'));
351    else
352      array_push($page['infos'], l10n_dec('nbm_msg_n_mail_sent', 'nbm_msg_n_mails_sent', $env_nbm['sent_mail_count']));
353  }
354}
355
356function assign_vars_nbm_mail_content($nbm_user)
357{
358  global $env_nbm;
359
360  set_make_full_url();
361
362  $env_nbm['mail_template']->assign_vars
363  (
364    array
365    (
366      'USERNAME' => $nbm_user['username'],
367
368      'SEND_AS_NAME' => $env_nbm['send_as_name'],
369
370      'UNSUBSCRIBE_LINK' => add_url_params(get_root_url().'nbm.php', array('unsubscribe' => $nbm_user['check_key'])),
371      'SUBSCRIBE_LINK' => add_url_params(get_root_url().'nbm.php', array('subscribe' => $nbm_user['check_key'])),
372      'CONTACT_EMAIL' => $env_nbm['send_as_mail_address']
373    )
374  );
375
376  unset_make_full_url();
377}
378
379/*
380 * Subscribe or unsubscribe notification by mail
381 *
382 * is_subscribe define if action=subscribe or unsubscribe
383 * check_key list where action will be done
384 *
385 * @return check_key list treated
386 */
387function do_subscribe_unsubcribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array())
388{
389  global $conf, $page, $env_nbm, $conf;
390
391  $check_key_treated = array();
392  $updated_data_count = 0;
393  $error_on_updated_data_count = 0;
394
395  if ($is_subscribe)
396  {
397    $msg_info = l10n('nbm_user_change_enabled_true');
398    $msg_error = l10n('nbm_user_not_change_enabled_true');
399  }
400  else
401  {
402    $msg_info = l10n('nbm_user_change_enabled_false');
403    $msg_error = l10n('nbm_user_not_change_enabled_false');
404  }
405
406  if (count($check_key_list) != 0)
407  {
408    $updates = array();
409    $enabled_value = boolean_to_string($is_subscribe);
410    $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe);
411
412    // Prepare message after change language
413    $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
414
415    // Begin nbm users environment
416    begin_users_env_nbm(true);
417
418    foreach ($data_users as $nbm_user)
419    {
420      if (check_sendmail_timeout())
421      {
422        // Stop fill list on 'send', if the quota is override
423        array_push($page['errors'], $msg_break_timeout);
424        break;
425      }
426
427      // Fill return list
428      array_push($check_key_treated, $nbm_user['check_key']);
429
430      $do_update = true;
431      if ($nbm_user['mail_address'] != '')
432      {
433        // set env nbm user
434        set_user_on_env_nbm($nbm_user['user_id'], true);
435
436        $subject = '['.$conf['gallery_title'].']: '.($is_subscribe ? l10n('nbm_object_subcribe'): l10n('nbm_object_unsubcribe'));
437
438        // Assign current var for nbm mail
439        assign_vars_nbm_mail_content($nbm_user);
440
441        $section_action_by = ($is_subscribe ? 'subscribe_by_' : 'unsubscribe_by_');
442        $section_action_by .= ($is_admin_request ? 'admin' : 'himself');
443        $env_nbm['mail_template']->assign_block_vars
444        (
445          $section_action_by, array('DUMMY' => 'dummy')
446        );
447
448        if (pwg_mail(
449              format_email($nbm_user['username'], $nbm_user['mail_address']),
450              $env_nbm['send_as_mail_formated'],
451              $subject,
452              $env_nbm['mail_template']->parse('notification_by_mail', true),
453              $env_nbm['email_format'], $env_nbm['email_format']
454              ))
455        {
456          inc_mail_sent_success($nbm_user);
457        }
458        else
459        {
460          inc_mail_sent_failed($nbm_user);
461          $do_update = false;
462        }
463
464        // unset env nbm user
465        unset_user_on_env_nbm();
466
467      }
468
469      if ($do_update)
470      {
471        array_push
472        (
473          $updates,
474          array
475          (
476            'check_key' => $nbm_user['check_key'],
477            'enabled' => $enabled_value
478          )
479        );
480        $updated_data_count += 1;
481        array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
482      }
483      else
484      {
485        $error_on_updated_data_count += 1;
486        array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
487      }
488
489    }
490
491    // Restore nbm environment
492    end_users_env_nbm();
493
494    display_counter_info();
495
496    mass_updates(
497      USER_MAIL_NOTIFICATION_TABLE,
498      array(
499        'primary' => array('check_key'),
500        'update' => array('enabled')
501      ),
502      $updates
503    );
504
505  }
506
507  array_push($page['infos'], l10n_dec('nbm_user_change_enabled_updated_data_count', 'nbm_users_change_enabled_updated_data_count', $updated_data_count));
508  if ($error_on_updated_data_count != 0)
509  {
510    array_push($page['errors'],
511      l10n_dec('nbm_user_change_enabled_error_on_updated_data_count',
512               'nbm_users_change_enabled_error_on_updated_data_count',
513               $error_on_updated_data_count));
514  }
515
516  return $check_key_treated;
517}
518
519/*
520 * Unsubscribe notification by mail
521 *
522 * check_key list where action will be done
523 *
524 * @return check_key list treated
525 */
526function unsubcribe_notification_by_mail($is_admin_request, $check_key_list = array())
527{
528  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, false, $check_key_list);
529}
530
531/*
532 * Subscribe notification by mail
533 *
534 * check_key list where action will be done
535 *
536 * @return check_key list treated
537 */
538function subcribe_notification_by_mail($is_admin_request, $check_key_list = array())
539{
540  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, true, $check_key_list);
541}
542
543?>
Note: See TracBrowser for help on using the repository browser.