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

Last change on this file since 27429 was 26461, checked in by mistic100, 11 years ago

Update headers to 2014. Happy new year!!

  • Property svn:eol-style set to LF
File size: 14.1 KB
RevLine 
[1900]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1900]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
[4325]62    list($count) = pwg_db_fetch_row(pwg_query($query));
[1900]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/*
[6411]96 * Execute all main queries to get list of user
[1900]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    {
[6411]113      $query_and_check_key = ' and
[1900]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'];
[6411]134
[1900]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    {
[4325]170      while ($nbm_user = pwg_db_fetch_assoc($result))
[1900]171      {
[25018]172        $data_users[] = $nbm_user;
[1900]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;
[1904]191  // Save current language to stack, necessary because $user change during NBM
192  switch_lang_to($user['language']);
[1900]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']);
[2284]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());
[1900]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
[5021]207    $env_nbm['msg_info'] = l10n('Mail sent to %s [%s].');
208    $env_nbm['msg_error'] = l10n('Error when sending email to %s [%s].');
[1900]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'];
[1904]224  // Restore current language to stack, necessary because $user change during NBM
225  switch_lang_back();
[1900]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
[1904]255  switch_lang_to($user['language']);
[1900]256
257  if ($is_action_send)
258  {
[25360]259    $env_nbm['mail_template'] = get_mail_template($env_nbm['email_format']);
[2530]260    $env_nbm['mail_template']->set_filename('notification_by_mail', 'notification_by_mail.tpl');
[1900]261  }
262}
263
264/*
265 * Unset user on nbm enviromnent
266 *
267 * Return none
268 */
269function unset_user_on_env_nbm()
270{
271  global $env_nbm;
272
[1904]273  switch_lang_back();
[1900]274  unset($env_nbm['mail_template']);
275}
276
277/*
278 * Inc Counter success
279 *
280 * Return none
281 */
282function inc_mail_sent_success($nbm_user)
283{
284  global $page, $env_nbm;
285
286  $env_nbm['sent_mail_count'] += 1;
[25018]287  $page['infos'][] = sprintf($env_nbm['msg_info'], stripslashes($nbm_user['username']), $nbm_user['mail_address']);
[1900]288}
289
290/*
291 * Inc Counter failed
292 *
293 * Return none
294 */
295function inc_mail_sent_failed($nbm_user)
296{
297  global $page, $env_nbm;
298
299  $env_nbm['error_on_mail_count'] += 1;
[25018]300  $page['errors'][] = sprintf($env_nbm['msg_error'], stripslashes($nbm_user['username']), $nbm_user['mail_address']);
[1900]301}
302
303/*
304 * Display Counter Info
305 *
306 * Return none
307 */
308function display_counter_info()
309{
310  global $page, $env_nbm;
311
312  if ($env_nbm['error_on_mail_count'] != 0)
313  {
[25018]314    $page['errors'][] = l10n_dec(
315      '%d mail was not sent.', '%d mails were not sent.',
316      $env_nbm['error_on_mail_count']
317      );
318     
[1900]319    if ($env_nbm['sent_mail_count'] != 0)
[25018]320    {
321      $page['infos'][] = l10n_dec(
322        '%d mail was sent.', '%d mails were sent.',
323        $env_nbm['sent_mail_count']
324        );
325    }
[1900]326  }
327  else
328  {
329    if ($env_nbm['sent_mail_count'] == 0)
[25018]330    {
331      $page['infos'][] = l10n('No mail to send.');
332    }
[1900]333    else
[25018]334    {
335      $page['infos'][] = l10n_dec(
336        '%d mail was sent.', '%d mails were sent.',
337        $env_nbm['sent_mail_count']
338        );
339    }
[1900]340  }
341}
342
343function assign_vars_nbm_mail_content($nbm_user)
344{
345  global $env_nbm;
346
347  set_make_full_url();
348
[2285]349  $env_nbm['mail_template']->assign
[1900]350  (
351    array
352    (
[4304]353      'USERNAME' => stripslashes($nbm_user['username']),
[1900]354
355      'SEND_AS_NAME' => $env_nbm['send_as_name'],
356
[13106]357      'UNSUBSCRIBE_LINK' => add_url_params(get_gallery_home_url().'/nbm.php', array('unsubscribe' => $nbm_user['check_key'])),
358      'SUBSCRIBE_LINK' => add_url_params(get_gallery_home_url().'/nbm.php', array('subscribe' => $nbm_user['check_key'])),
[1900]359      'CONTACT_EMAIL' => $env_nbm['send_as_mail_address']
360    )
361  );
362
363  unset_make_full_url();
364}
365
366/*
367 * Subscribe or unsubscribe notification by mail
368 *
369 * is_subscribe define if action=subscribe or unsubscribe
370 * check_key list where action will be done
371 *
372 * @return check_key list treated
373 */
374function do_subscribe_unsubscribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array())
375{
376  global $conf, $page, $env_nbm, $conf;
377
[7005]378  set_make_full_url();
379
[1900]380  $check_key_treated = array();
381  $updated_data_count = 0;
382  $error_on_updated_data_count = 0;
383
384  if ($is_subscribe)
385  {
[5021]386    $msg_info = l10n('User %s [%s] was added to the subscription list.');
387    $msg_error = l10n('User %s [%s] was not added to the subscription list.');
[1900]388  }
389  else
390  {
[5021]391    $msg_info = l10n('User %s [%s] was removed from the subscription list.');
392    $msg_error = l10n('User %s [%s] was not removed from the subscription list.');
[1900]393  }
394
395  if (count($check_key_list) != 0)
396  {
397    $updates = array();
398    $enabled_value = boolean_to_string($is_subscribe);
399    $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe);
400
401    // Prepare message after change language
[5021]402    $msg_break_timeout = l10n('Time to send mail is limited. Others mails are skipped.');
[1900]403
404    // Begin nbm users environment
405    begin_users_env_nbm(true);
406
407    foreach ($data_users as $nbm_user)
408    {
409      if (check_sendmail_timeout())
410      {
411        // Stop fill list on 'send', if the quota is override
[25018]412        $page['errors'][] = $msg_break_timeout;
[1900]413        break;
414      }
415
416      // Fill return list
[25018]417      $check_key_treated[] = $nbm_user['check_key'];
[1900]418
419      $do_update = true;
420      if ($nbm_user['mail_address'] != '')
421      {
422        // set env nbm user
423        set_user_on_env_nbm($nbm_user, true);
424
[25360]425        $subject = '['.$conf['gallery_title'].'] '.($is_subscribe ? l10n('Subscribe to notification by mail'): l10n('Unsubscribe from notification by mail'));
[1900]426
427        // Assign current var for nbm mail
428        assign_vars_nbm_mail_content($nbm_user);
429
430        $section_action_by = ($is_subscribe ? 'subscribe_by_' : 'unsubscribe_by_');
431        $section_action_by .= ($is_admin_request ? 'admin' : 'himself');
[3243]432        $env_nbm['mail_template']->assign
433        (
434          array
435          (
436            $section_action_by => true,
437            'GOTO_GALLERY_TITLE' => $conf['gallery_title'],
[6411]438            'GOTO_GALLERY_URL' => get_gallery_home_url(),
[3243]439          )
440        );
[25360]441       
442        $ret = pwg_mail(
443          array(
444            'name' => stripslashes($nbm_user['username']),
445            'email' => $nbm_user['mail_address'],
446            ),
447          array(
448            'from' => $env_nbm['send_as_mail_formated'],
449            'subject' => $subject,
450            'email_format' => $env_nbm['email_format'],
451            'content' => $env_nbm['mail_template']->parse('notification_by_mail', true),
452            'content_format' => $env_nbm['email_format'],
453            )
454          );
[1900]455
[25360]456        if ($ret)
[1900]457        {
458          inc_mail_sent_success($nbm_user);
459        }
460        else
461        {
462          inc_mail_sent_failed($nbm_user);
463          $do_update = false;
464        }
465
466        // unset env nbm user
467        unset_user_on_env_nbm();
468
469      }
470
471      if ($do_update)
472      {
[25018]473        $updates[] = array(
474          'check_key' => $nbm_user['check_key'],
475          'enabled' => $enabled_value
476          );
[1900]477        $updated_data_count += 1;
[25018]478        $page['infos'][] = sprintf($msg_info, stripslashes($nbm_user['username']), $nbm_user['mail_address']);
[1900]479      }
480      else
481      {
482        $error_on_updated_data_count += 1;
[25018]483        $page['errors'][] = sprintf($msg_error, stripslashes($nbm_user['username']), $nbm_user['mail_address']);
[1900]484      }
485
486    }
487
488    // Restore nbm environment
489    end_users_env_nbm();
490
491    display_counter_info();
492
493    mass_updates(
494      USER_MAIL_NOTIFICATION_TABLE,
495      array(
496        'primary' => array('check_key'),
497        'update' => array('enabled')
498      ),
499      $updates
500    );
501
502  }
503
[25018]504  $page['infos'][] = l10n_dec(
505    '%d user was updated.', '%d users were updated.',
506    $updated_data_count
507    );
508 
[1900]509  if ($error_on_updated_data_count != 0)
510  {
[25018]511    $page['errors'][] = l10n_dec(
512      '%d user was not updated.', '%d users were not updated.',
513      $error_on_updated_data_count
514      );
[1900]515  }
516
[7005]517  unset_make_full_url();
518
[1900]519  return $check_key_treated;
520}
521
522/*
523 * Unsubscribe notification by mail
524 *
525 * check_key list where action will be done
526 *
527 * @return check_key list treated
528 */
529function unsubscribe_notification_by_mail($is_admin_request, $check_key_list = array())
530{
531  return do_subscribe_unsubscribe_notification_by_mail($is_admin_request, false, $check_key_list);
532}
533
534/*
535 * Subscribe notification by mail
536 *
537 * check_key list where action will be done
538 *
539 * @return check_key list treated
540 */
541function subscribe_notification_by_mail($is_admin_request, $check_key_list = array())
542{
543  return do_subscribe_unsubscribe_notification_by_mail($is_admin_request, true, $check_key_list);
544}
545
[6411]546?>
Note: See TracBrowser for help on using the repository browser.