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

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

Resolved Issue ID 0000507:

o format of email
o max_execution_time equal to 0
o -f with only adress mail
o use of standard function get_webmaster_mail_address

2 news $conf parameters.

Merge branch-1_6 r1529:1530 into BSF

File size: 13.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-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['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
234/*
235 * Set user_id on nbm enviromnent
236 *
237 * Return none
238 */
239function set_user_id_on_env_nbm($user_id)
240{
241  global $user, $lang, $lang_info, $env_nbm;
242
243  $user = array();
244  $user['id'] = $user_id;
245  $user = array_merge($user, getuserdata($user['id'], true));
246
247  if ($env_nbm['last_language'] != $user['language'])
248  {
249    $env_nbm['last_language'] = $user['language'];
250
251    // Re-Init language arrays
252    $lang_info = array();
253    $lang  = array();
254
255    // language files
256    include(get_language_filepath('common.lang.php'));
257    // No test admin because script is checked admin (user selected no)
258    // Translations are in admin file too
259    include(get_language_filepath('admin.lang.php'));
260  }
261}
262
263/*
264 * Inc Counter success
265 *
266 * Return none
267 */
268function inc_mail_sent_success($nbm_user)
269{
270  global $page, $env_nbm;
271
272  $env_nbm['sent_mail_count'] += 1;
273  array_push($page['infos'], sprintf($env_nbm['msg_info'], $nbm_user['username'], $nbm_user['mail_address']));
274}
275
276/*
277 * Inc Counter failed
278 *
279 * Return none
280 */
281function inc_mail_sent_failed($nbm_user)
282{
283  global $page, $env_nbm;
284
285  $env_nbm['error_on_mail_count'] += 1;
286  array_push($page['errors'], sprintf($env_nbm['msg_error'], $nbm_user['username'], $nbm_user['mail_address']));
287}
288
289/*
290 * Display Counter Info
291 *
292 * Return none
293 */
294function display_counter_info()
295{
296  global $page, $env_nbm;
297
298  if ($env_nbm['error_on_mail_count'] != 0)
299  {
300    array_push($page['errors'], sprintf(l10n('nbm_msg_no_mail_to_send'), $env_nbm['error_on_mail_count']));
301    if ($env_nbm['sent_mail_count'] != 0)
302      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
303  }
304  else
305  {
306    if ($env_nbm['sent_mail_count'] == 0)
307      array_push($page['infos'], l10n('nbm_no_mail_to_send'));
308    else
309      array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count']));
310  }
311}
312
313function get_mail_content_subscribe_unsubcribe($nbm_user)
314{
315  global $page, $env_nbm;
316 
317  $content = "\n\n\n";
318 
319  if ( isset($page['root_path']) )
320  {
321    $save_root_path = $page['root_path'];
322  }
323
324  $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path();
325 
326  $content .= "___________________________________________________\n\n";
327  $content .= sprintf(l10n('nbm_content_unsubscribe_link'), add_url_params(get_root_url().'/nbm.php', array('unsubscribe' => $nbm_user['check_key'])))."\n";
328  $content .= sprintf(l10n('nbm_content_subscribe_link'), add_url_params(get_root_url().'/nbm.php', array('subscribe' => $nbm_user['check_key'])))."\n";
329  $content .= sprintf(l10n('nbm_content_subscribe_unsubscribe_contact'), $env_nbm['send_as_mail_address'])."\n";
330  $content .= "___________________________________________________\n\n\n\n";
331
332  if (isset($save_root_path))
333  {
334    $page['root_path'] = $save_root_path;
335  }
336  else
337  {
338    unset($page['root_path']);
339  }
340
341  return $content;
342}
343
344/*
345 * Subscribe or unsubscribe notification by mail
346 *
347 * is_subscribe define if action=subscribe or unsubscribe
348 * check_key list where action will be done
349 *
350 * @return check_key list treated
351 */
352function do_subscribe_unsubcribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array())
353{
354  global $conf, $page, $env_nbm, $conf;
355
356  $check_key_treated = array();
357  $updated_data_count = 0;
358  $error_on_updated_data_count = 0;
359
360  if ($is_subscribe)
361  {
362    $msg_info = l10n('nbm_user_change_enabled_true');
363    $msg_error = l10n('nbm_user_not_change_enabled_true');
364  }
365  else
366  {
367    $msg_info = l10n('nbm_user_change_enabled_false');
368    $msg_error = l10n('nbm_user_not_change_enabled_false');
369  }
370
371  if (count($check_key_list) != 0)
372  {
373    $updates = array();
374    $enabled_value = boolean_to_string($is_subscribe);
375    $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe);
376
377    // Prepare message after change language
378    $msg_break_timeout = l10n('nbm_break_timeout_send_mail');
379
380    // Begin nbm users environment
381    begin_users_env_nbm(true);
382
383    foreach ($data_users as $nbm_user)
384    {
385      if (check_sendmail_timeout())
386      {
387        // Stop fill list on 'send', if the quota is override
388        array_push($page['errors'], $msg_break_timeout);
389        break;
390      }
391
392      // Fill return list
393      array_push($check_key_treated, $nbm_user['check_key']);
394
395      $do_update = true;
396      if ($nbm_user['mail_address'] != '')
397      {
398        // set env nbm user
399        set_user_id_on_env_nbm($nbm_user['user_id']);
400
401        $message = '';
402
403        $subject = '['.$conf['gallery_title'].']: '.($is_subscribe ? l10n('nbm_object_subcribe'): l10n('nbm_object_unsubcribe'));
404        $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n";
405
406        if ($is_subscribe)
407        {
408          $message .= l10n($is_admin_request ? 'nbm_content_subscribe_by_admin' : 'nbm_content_subscribe_by_himself');
409        }
410        else
411        {
412          $message .= l10n($is_admin_request ? 'nbm_content_unsubscribe_by_admin' : 'nbm_content_unsubscribe_by_himself');
413        }
414
415        $message .= "\n\n";
416        $message .= l10n('nbm_content_byebye')."\n   ".$env_nbm['send_as_name']."\n\n";
417
418        $message .= get_mail_content_subscribe_unsubcribe($nbm_user);
419
420        if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message))
421        {
422          inc_mail_sent_success($nbm_user);
423        }
424        else
425        {
426          inc_mail_sent_failed($nbm_user);
427          $do_update = false;
428        }
429      }
430
431      if ($do_update)
432      {
433        array_push
434        (
435          $updates,
436          array
437          (
438            'check_key' => $nbm_user['check_key'],
439            'enabled' => $enabled_value
440          )
441        );
442        $updated_data_count += 1;
443        array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
444      }
445      else
446      {
447        $error_on_updated_data_count += 1;
448        array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
449      }
450
451    }
452
453    // Restore nbm environment
454    end_users_env_nbm();
455
456    display_counter_info();
457
458    mass_updates(
459      USER_MAIL_NOTIFICATION_TABLE,
460      array(
461        'primary' => array('check_key'),
462        'update' => array('enabled')
463      ),
464      $updates
465    );
466
467  }
468
469  array_push($page['infos'], sprintf(l10n('nbm_user_change_enabled_updated_data_count'), $updated_data_count));
470  if ($error_on_updated_data_count != 0)
471  {
472    array_push($page['errors'], sprintf(l10n('nbm_user_change_enabled_error_on_updated_data_count'), $error_on_updated_data_count));
473  }
474
475  return $check_key_treated;
476}
477
478/*
479 * Unsubscribe notification by mail
480 *
481 * check_key list where action will be done
482 *
483 * @return check_key list treated
484 */
485function unsubcribe_notification_by_mail($is_admin_request, $check_key_list = array())
486{
487  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, false, $check_key_list);
488}
489
490/*
491 * Subscribe notification by mail
492 *
493 * check_key list where action will be done
494 *
495 * @return check_key list treated
496 */
497function subcribe_notification_by_mail($is_admin_request, $check_key_list = array())
498{
499  return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, true, $check_key_list);
500}
501
502?>
Note: See TracBrowser for help on using the repository browser.