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

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

$Id$ are wrong on my last commit

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