source: branches/2.0/admin/include/functions_notification_by_mail.inc.php @ 3244

Last change on this file since 3244 was 3244, checked in by rub, 15 years ago

merge -c3243 from trunk to branch 2.0

Resolved issue 0000974: Adding a link to the gallery in the notification of inscription

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