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

Last change on this file since 2530 was 2530, checked in by vdigital, 16 years ago

Wigo becomes "goto".
Admin tpl files are moved.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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( $section_action_by, true );
419
420        if (pwg_mail
421            (
422              format_email($nbm_user['username'], $nbm_user['mail_address']),
423              array
424              (
425                'from' => $env_nbm['send_as_mail_formated'],
426                'subject' => $subject,
427                'email_format' => $env_nbm['email_format'],
428                'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
429                'content_format' => $env_nbm['email_format'],
430                'template' => $nbm_user['template'],
431                'theme' => $nbm_user['theme']
432              )
433            ))
434        {
435          inc_mail_sent_success($nbm_user);
436        }
437        else
438        {
439          inc_mail_sent_failed($nbm_user);
440          $do_update = false;
441        }
442
443        // unset env nbm user
444        unset_user_on_env_nbm();
445
446      }
447
448      if ($do_update)
449      {
450        array_push
451        (
452          $updates,
453          array
454          (
455            'check_key' => $nbm_user['check_key'],
456            'enabled' => $enabled_value
457          )
458        );
459        $updated_data_count += 1;
460        array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address']));
461      }
462      else
463      {
464        $error_on_updated_data_count += 1;
465        array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address']));
466      }
467
468    }
469
470    // Restore nbm environment
471    end_users_env_nbm();
472
473    display_counter_info();
474
475    mass_updates(
476      USER_MAIL_NOTIFICATION_TABLE,
477      array(
478        'primary' => array('check_key'),
479        'update' => array('enabled')
480      ),
481      $updates
482    );
483
484  }
485
486  array_push($page['infos'], l10n_dec('nbm_user_change_enabled_updated_data_count', 'nbm_users_change_enabled_updated_data_count', $updated_data_count));
487  if ($error_on_updated_data_count != 0)
488  {
489    array_push($page['errors'],
490      l10n_dec('nbm_user_change_enabled_error_on_updated_data_count',
491               'nbm_users_change_enabled_error_on_updated_data_count',
492               $error_on_updated_data_count));
493  }
494
495  return $check_key_treated;
496}
497
498/*
499 * Unsubscribe notification by mail
500 *
501 * check_key list where action will be done
502 *
503 * @return check_key list treated
504 */
505function unsubscribe_notification_by_mail($is_admin_request, $check_key_list = array())
506{
507  return do_subscribe_unsubscribe_notification_by_mail($is_admin_request, false, $check_key_list);
508}
509
510/*
511 * Subscribe notification by mail
512 *
513 * check_key list where action will be done
514 *
515 * @return check_key list treated
516 */
517function subscribe_notification_by_mail($is_admin_request, $check_key_list = array())
518{
519  return do_subscribe_unsubscribe_notification_by_mail($is_admin_request, true, $check_key_list);
520}
521
522?>
Note: See TracBrowser for help on using the repository browser.