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

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

Issue ID 330:

o Change NBM configuration in order to avoid lose treatment action when occurred timeout
o Add news redirect/repost functions

File size: 13.5 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-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
37
38/*
39 * Search an available check_key
40 *
41 * It's a copy of function find_available_feed_id
42 *
43 * @return string nbm identifier
44 */
45function find_available_check_key()
46{
47  while (true)
48  {
49    $key = generate_key(16);
50    $query = '
51select
52  count(*)
53from
54  '.USER_MAIL_NOTIFICATION_TABLE.'
55where
56  check_key = \''.$key.'\';';
57
58    list($count) = mysql_fetch_row(pwg_query($query));
59    if ($count == 0)
60    {
61      return $key;
62    }
63  }
64}
65
66/*
67 * Check sendmail timeout state
68 *
69 * @return true, if it's timeout
70 */
71function check_sendmail_timeout()
72{
73  global $env_nbm;
74
75  $env_nbm['is_sendmail_timeout'] = ((get_moment() - $env_nbm['start_time']) > $env_nbm['sendmail_timeout']);
76
77  return $env_nbm['is_sendmail_timeout'];
78}
79
80
81/*
82 * Add quote to all elements of check_key_list
83 *
84 * @return quoted check key list
85 */
86function quote_check_key_list($check_key_list = array())
87{
88  return array_map(create_function('$s', 'return \'\\\'\'.$s.\'\\\'\';'), $check_key_list);
89}
90
91/*
92 * Execute all main queries to get list of user
93 *
94 * Type are the type of list 'subscribe', 'send'
95 *
96 * return array of users
97 */
98function get_user_notifications($action, $check_key_list = array(), $enabled_filter_value = '')
99{
100  global $conf;
101
102  $data_users = array();
103
104  if (in_array($action, array('subscribe', 'send')))
105  {
106    $quoted_check_key_list = quote_check_key_list($check_key_list);
107    if (count($quoted_check_key_list) != 0 )
108    {
109      $query_and_check_key = ' and
110    check_key in ('.implode(",", $quoted_check_key_list).') ';
111    }
112    else
113    {
114      $query_and_check_key = '';
115    }
116
117    $query = '
118select
119  N.user_id,
120  N.check_key,
121  U.'.$conf['user_fields']['username'].' as username,
122  U.'.$conf['user_fields']['email'].' as mail_address,
123  N.enabled,
124  N.last_send
125from
126  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
127  '.USERS_TABLE.' as U
128where
129  N.user_id =  U.'.$conf['user_fields']['id'];
130 
131    if ($action == 'send')
132    {
133      // No mail empty and all users enabled
134      $query .= ' and
135  N.enabled = \'true\' and
136  U.'.$conf['user_fields']['email'].' is not null';
137    }
138
139    $query .= $query_and_check_key;
140
141    if (isset($enabled_filter_value) and ($enabled_filter_value != ''))
142    {
143      $query .= ' and
144        N.enabled = \''.boolean_to_string($enabled_filter_value).'\'';
145    }
146
147    $query .= '
148order by';
149
150    if ($action == 'send')
151    {
152      $query .= '
153  last_send, username;';
154    }
155    else
156    {
157      $query .= '
158  username;';
159    }
160
161    $query .= ';';
162
163    $result = pwg_query($query);
164    if (!empty($result))
165    {
166      while ($nbm_user = mysql_fetch_array($result))
167      {
168        array_push($data_users, $nbm_user);
169      }
170    }
171  }
172  return $data_users;
173}
174
175/*
176 * Begin of use nbm environment
177 * Prepare and save current environment and initialize data in order to send mail
178 *
179 * Return none
180 */
181function begin_users_env_nbm($is_to_send_mail = false)
182{
183  global $user, $lang, $lang_info, $conf, $env_nbm;
184
185  // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
186  $env_nbm['save_user'] = $user;
187  $env_nbm['save_lang_info'] = $lang_info;
188  $env_nbm['save_lang'] = $lang;
189  // Last Language
190  $env_nbm['last_language'] = $user['language'];
191
192  $env_nbm['is_to_send_mail'] = $is_to_send_mail;
193
194  if ($is_to_send_mail)
195  {
196    // Init mail configuration
197    $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']);
198    $env_nbm['send_as_mail_address'] = get_webmaster_mail_address();
199    $env_nbm['send_as_mail_formated'] = format_email($env_nbm['send_as_name'], $env_nbm['send_as_mail_address']);
200    // Init mail counter
201    $env_nbm['error_on_mail_count'] = 0;
202    $env_nbm['sent_mail_count'] = 0;
203    // Save sendmail message info and error in the original language
204    $env_nbm['msg_info'] = l10n('nbm_msg_mail_sent_to');
205    $env_nbm['msg_error'] = l10n('nbm_msg_error_sending_email_to');
206  }
207}
208
209/*
210 * End of use nbm environment
211 * Restore environment
212 *
213 * Return none
214 */
215function end_users_env_nbm()
216{
217  global $user, $lang, $lang_info, $env_nbm;
218
219  // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
220  $user = $env_nbm['save_user'];
221  $lang_info = $env_nbm['save_lang_info'];
222  $lang = $env_nbm['save_lang'];
223}
224
225/*
226 * Set user_id on nbm enviromnent
227 *
228 * Return none
229 */
230function set_user_id_on_env_nbm($user_id)
231{
232  global $user, $lang, $lang_info, $env_nbm;
233
234  $user = array();
235  $user['id'] = $user_id;
236  $user = array_merge($user, getuserdata($user['id'], true));
237
238  if ($env_nbm['last_language'] != $user['language'])
239  {
240    $env_nbm['last_language'] = $user['language'];
241
242    // Re-Init language arrays
243    $lang_info = array();
244    $lang  = array();
245
246    // language files
247    include(get_language_filepath('common.lang.php'));
248    // No test admin because script is checked admin (user selected no)
249    // Translations are in admin file too
250    include(get_language_filepath('admin.lang.php'));
251  }
252}
253
254/*
255 * Inc Counter success
256 *
257 * Return none
258 */
259function inc_mail_sent_success($nbm_user)
260{
261  global $page, $env_nbm;
262
263  $env_nbm['sent_mail_count'] += 1;
264  array_push($page['infos'], sprintf($env_nbm['msg_info'], $nbm_user['username'], $nbm_user['mail_address']));
265}
266
267/*
268 * Inc Counter failed
269 *
270 * Return none
271 */
272function inc_mail_sent_failed($nbm_user)
273{
274  global $page, $env_nbm;
275
276  $env_nbm['error_on_mail_count'] += 1;
277  array_push($page['errors'], sprintf($env_nbm['msg_error'], $nbm_user['username'], $nbm_user['mail_address']));
278}
279
280/*
281 * Display Counter Info
282 *
283 * Return none
284 */
285function display_counter_info()
286{
287  global $page, $env_nbm;
288
289  if ($env_nbm['error_on_mail_count'] != 0)
290  {
291    array_push($page['errors'], sprintf(l10n('nbm_msg_no_mail_to_send'), $env_nbm['error_on_mail_count']));
292    if ($env_nbm['sent_mail_count'] != 0)
293      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
294  }
295  else
296  {
297    if ($env_nbm['sent_mail_count'] == 0)
298      array_push($page['infos'], l10n('nbm_no_mail_to_send'));
299    else
300      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
301  }
302}
303
304function get_mail_content_subscribe_unsubcribe($nbm_user)
305{
306  global $page, $env_nbm;
307 
308  $content = "\n\n\n";
309 
310  if ( isset($page['root_path']) )
311  {
312    $save_root_path = $page['root_path'];
313  }
314
315  $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path();
316 
317  $content .= "___________________________________________________\n\n";
318  $content .= sprintf(l10n('nbm_content_unsubscribe_link'), add_url_params(get_root_url().'/nbm.php', array('unsubscribe' => $nbm_user['check_key'])))."\n";
319  $content .= sprintf(l10n('nbm_content_subscribe_link'), add_url_params(get_root_url().'/nbm.php', array('subscribe' => $nbm_user['check_key'])))."\n";
320  $content .= sprintf(l10n('nbm_content_subscribe_unsubscribe_contact'), $env_nbm['send_as_mail_address'])."\n";
321  $content .= "___________________________________________________\n\n\n\n";
322
323  if (isset($save_root_path))
324  {
325    $page['root_path'] = $save_root_path;
326  }
327  else
328  {
329    unset($page['root_path']);
330  }
331
332  return $content;
333}
334
335/*
336 * Subscribe or unsubscribe notification by mail
337 *
338 * is_subscribe define if action=subscribe or unsubscribe
339 * check_key list where action will be done
340 *
341 * @return check_key lisr treated
342 */
343function do_subscribe_unsubcribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array())
344{
345  global $conf, $page, $env_nbm, $conf;
346
347  $check_key_treated = array();
348  $updated_data_count = 0;
349  $error_on_updated_data_count = 0;
350
351  if ($is_subscribe)
352  {
353    $msg_info = l10n('nbm_user_change_enabled_true');
354    $msg_error = l10n('nbm_user_not_change_enabled_true');
355  }
356  else
357  {
358    $msg_info = l10n('nbm_user_change_enabled_false');
359    $msg_error = l10n('nbm_user_not_change_enabled_false');
360  }
361
362  if (count($check_key_list) != 0)
363  {
364    $updates = array();
365    $enabled_value = boolean_to_string($is_subscribe);
366    $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe);
367
368    // Prepare message after change language
369    $msg_break_timeout = l10n('nbm_nbm_break_timeout_send_mail');
370
371    // Begin nbm users environment
372    begin_users_env_nbm(true);
373
374    foreach ($data_users as $nbm_user)
375    {
376      if (check_sendmail_timeout())
377      {
378        // Stop fill list on 'send', if the quota is override
379        array_push($page['errors'], $msg_break_timeout);
380        break;
381      }
382
383      // Fill return list
384      array_push($check_key_treated, $nbm_user['check_key']);
385
386      $do_update = true;
387      if ($nbm_user['mail_address'] != '')
388      {
389        // set env nbm user
390        set_user_id_on_env_nbm($nbm_user['user_id']);
391
392        $message = '';
393
394        $subject = '['.$conf['gallery_title'].']: '.($is_subscribe ? l10n('nbm_object_subcribe'): l10n('nbm_object_unsubcribe'));
395        $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n";
396
397        if ($is_subscribe)
398        {
399          $message .= l10n($is_admin_request ? 'nbm_content_subscribe_by_admin' : 'nbm_content_subscribe_by_himself');
400        }
401        else
402        {
403          $message .= l10n($is_admin_request ? 'nbm_content_unsubscribe_by_admin' : 'nbm_content_unsubscribe_by_himself');
404        }
405
406        $message .= "\n\n";
407        $message .= l10n('nbm_content_byebye')."\n   ".$env_nbm['send_as_name']."\n\n";
408
409        $message .= get_mail_content_subscribe_unsubcribe($nbm_user);
410
411        if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message))
412        {
413          inc_mail_sent_success($nbm_user);
414        }
415        else
416        {
417          inc_mail_sent_failed($nbm_user);
418          $do_update = false;
419        }
420      }
421
422      if ($do_update)
423      {
424        array_push
425        (
426          $updates,
427          array
428          (
429            'check_key' => $nbm_user['check_key'],
430            'enabled' => $enabled_value
431          )
432        );
433        $updated_data_count += 1;
434        array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
435      }
436      else
437      {
438        $error_on_updated_data_count += 1;
439        array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
440      }
441
442    }
443
444    // Restore nbm environment
445    end_users_env_nbm();
446
447    display_counter_info();
448
449    mass_updates(
450      USER_MAIL_NOTIFICATION_TABLE,
451      array(
452        'primary' => array('check_key'),
453        'update' => array('enabled')
454      ),
455      $updates
456    );
457
458  }
459
460  array_push($page['infos'], sprintf(l10n('nbm_user_change_enabled_updated_data_count'), $updated_data_count));
461  if ($error_on_updated_data_count != 0)
462  {
463    array_push($page['errors'], sprintf(l10n('nbm_user_change_enabled_error_on_updated_data_count'), $error_on_updated_data_count));
464  }
465
466  return $check_key_treated;
467}
468
469/*
470 * Unsubscribe notification by mail
471 *
472 * check_key list where action will be done
473 *
474 * @return check_key lisr treated
475 */
476function unsubcribe_notification_by_mail($is_admin_request, $check_key_list = array())
477{
478  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, false, $check_key_list);
479}
480
481/*
482 * Subscribe notification by mail
483 *
484 * check_key list where action will be done
485 *
486 * @return check_key lisr treated
487 */
488function subcribe_notification_by_mail($is_admin_request, $check_key_list = array())
489{
490  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, true, $check_key_list);
491}
492
493?>
Note: See TracBrowser for help on using the repository browser.