source: trunk/admin/mailtousers.php @ 1062

Last change on this file since 1062 was 1049, checked in by rub, 18 years ago

Step 3: Integration of new admin menu with the associated file (No interface, Only Message)

File size: 9.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-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: 2005-09-21 00:04:57 +0200 (mer, 21 sep 2005) $
11// | last modifier : $Author: plg $
12// | revision      : $Revision: 870 $
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// +-----------------------------------------------------------------------+
30// | include
31// +-----------------------------------------------------------------------+
32
33if (!defined('PHPWG_ROOT_PATH'))
34{
35  die ("Hacking attempt!");
36}
37include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
39
40include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
41include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
42include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
43
44// +-----------------------------------------------------------------------+
45// | functions
46// +-----------------------------------------------------------------------+
47/*
48 * Search an available check_key
49 *
50 * It's a copy of function find_available_feed_id
51 *
52 * @return string feed identifier
53 */
54function find_available_check_key()
55{
56  while (true)
57  {
58    $key = generate_key(128);
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 * Updating News users
77 */
78function update_data_user_mail_notification()
79{
80/*  $query = '
81insert into '.USER_MAIL_NOTIFICATION_TABLE.'
82  (user_id, enabled)
83  (select id, \'false\' from '.USERS_TABLE.'
84  where mail_address is not null and id not in (select user_id from '.USER_MAIL_NOTIFICATION_TABLE.'))
85;';
86  pwg_query($query);*/
87
88  global $conf, $page;
89
90  $query = '
91select
92  id user_id, username, mail_address
93from
94  '.USERS_TABLE.'
95where
96  mail_address is not null and
97  id not in (select user_id from '.USER_MAIL_NOTIFICATION_TABLE.')
98order by
99  id;';
100
101  $result = pwg_query($query);
102
103  if (mysql_num_rows($result) > 0)
104  {
105    $inserts = array();
106
107    while ($row = mysql_fetch_array($result))
108    {
109      array_push($inserts, array('user_id' => $row['user_id'],
110                                 'check_key' => find_available_check_key(),
111                                 'enabled' => ($conf['default_value_user_mail_notification_enabled'] == true ? 'true' : 'false')));
112      array_push($page['infos'], sprintf(l10n('nbm_User %s [%s] added.'), $row['username'], $row['mail_address']));
113    }
114
115    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
116  }
117}
118
119/*
120 * Updating News users
121 */
122function send_all_user_mail_notification()
123{
124  global $conf, $conf_mail, $page, $user, $lang_info, $lang;
125  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
126
127  $query = '
128select
129  N.user_id, U.username, U.mail_address, N.last_send
130from
131  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
132  '.USERS_TABLE.' as U
133where
134  N.user_id =  U.id and
135  N.enabled = \'true\' and
136  U.mail_address is not null
137order by
138  user_id;';
139
140  $result = pwg_query($query);
141
142  if (mysql_num_rows($result) > 0)
143  {
144    $error_on_mail_count = 0;
145    $sended_mail_count = 0;
146    $datas = array();
147    // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
148    $sav_mailtousers_user = $user;
149    $sav_mailtousers_lang_info = $lang_info;
150    $sav_mailtousers_lang = $lang;
151    // Save message info and error in the original language
152    $msg_info = l10n('nbm_Mail sended to %s [%s].');
153    $msg_error = l10n('nbm_Error when sending email to %s [%s].');
154    // Last Language
155    $last_mailtousers_language = $user['language'];
156
157    while ($row = mysql_fetch_array($result))
158    {
159      $user = array();
160      $user['id'] = $row['user_id'];
161      $user = array_merge($user, getuserdata($user['id'], true));
162
163      if ($last_mailtousers_language != $user['language'])
164      {
165        $last_mailtousers_language = $user['language'];
166
167        // Re-Init language arrays
168        $lang_info = array();
169        $lang  = array();
170
171        // language files
172        include(get_language_filepath('common.lang.php'));
173        // No test admin because script is checked admin (user selected no)
174        // Translations are in admin file too
175        include(get_language_filepath('admin.lang.php'));
176      }
177
178      $message = '';
179      $news = news($row['last_send'], $dbnow);
180      if (count($news) > 0)
181      {
182        $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_New elements added');
183        $message .= sprintf(l10n('nbm_Hello %s'), $row['username']).",\n\n";
184
185        if (!is_null($row['last_send']))
186          $message .= sprintf(l10n('nbm_New elements were added between %s and %s:'), $row['last_send'], $dbnow);
187        else
188          $message .= sprintf(l10n('nbm_New elements were added on %s:'), $dbnow);
189        $message .= "\n";
190
191        foreach ($news as $line)
192        {
193          $message .= '  o '.$line."\n";
194        }
195
196        $message .= "\n".sprintf(l10n('nbm_Go to %s %s.'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
197        $message .= "\n".sprintf(l10n('nbm_To unsubscribe send a message to %s.'), $conf_mail['email_webmaster'])."\n\n";
198
199        if (pwg_mail(format_email($row['username'], $row['mail_address']), '', $subject, $message))
200        {
201          $sended_mail_count += 1;
202          array_push($page['infos'], sprintf($msg_info, $row['username'], $row['mail_address']));
203          $data = array('user_id' => $row['user_id'],
204                        'last_send' => $dbnow);
205          array_push($datas, $data);
206        }
207        else
208        {
209          $error_on_mail_count += 1;
210          array_push($page['errors'], sprintf($msg_error, $row['username'], $row['mail_address']));
211        }
212      }
213    }
214
215    // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
216    $user = $sav_mailtousers_user;
217    $lang_info = $sav_mailtousers_lang_info;
218    $lang = $sav_mailtousers_lang;
219
220    mass_updates(
221      USER_MAIL_NOTIFICATION_TABLE,
222      array(
223        'primary' => array('user_id'),
224        'update' => array('last_send')
225       ),
226       $datas
227       );
228
229    if ($error_on_mail_count != 0)
230    {
231      array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sended.'), $error_on_mail_count));
232    }
233    else
234    {
235      if ($sended_mail_count == 0)
236        array_push($page['infos'], l10n('nbm_No mail to send.'));
237      else
238        array_push($page['infos'], sprintf(l10n('nbm_%d mails were sended.'), $sended_mail_count));
239    }
240  }
241  else
242  {
243    array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
244  }
245}
246
247// +-----------------------------------------------------------------------+
248// | Main
249// +-----------------------------------------------------------------------+
250update_data_user_mail_notification();
251send_all_user_mail_notification();
252
253
254// +-----------------------------------------------------------------------+
255// |                        template initialization                        |
256// +-----------------------------------------------------------------------+
257
258$title = l10n('nbm_Send mail to users');
259
260// +-----------------------------------------------------------------------+
261// |                        infos & errors display                         |
262// +-----------------------------------------------------------------------+
263
264/*echo '<pre>';
265
266if (count($page['errors']) != 0)
267{
268  echo "\n\nErrors:\n";
269  foreach ($page['errors'] as $error)
270  {
271    echo $error."\n";
272  }
273}
274
275if (count($page['infos']) != 0)
276{
277  echo "\n\nInformations:\n";
278  foreach ($page['infos'] as $info)
279  {
280    echo $info."\n";
281  }
282}
283
284echo '</pre>';
285*/
286?>
Note: See TracBrowser for help on using the repository browser.