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

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