source: trunk/admin/notification_by_mail.php @ 1094

Last change on this file since 1094 was 1094, checked in by rvelices, 18 years ago

URL rewrite: 3 options in the config file define behaviour (question mark
removal, file name for picture and .php extension removal)

fix: added unsigned for column in install sql - for the sake of uniformization

change: add_url_param is now add_url_params and takes an array as parameter
instead of a string

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 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: 2006-03-23 01:49:04 +0000 (Thu, 23 Mar 2006) $
11// | last modifier : $Author: rvelices $
12// | revision      : $Revision: 1094 $
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}
37
38include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
39include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
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// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49// +-----------------------------------------------------------------------+
50// | functions                                                             |
51// +-----------------------------------------------------------------------+
52/*
53 * Search an available check_key
54 *
55 * It's a copy of function find_available_feed_id
56 *
57 * @return string feed identifier
58 */
59function find_available_check_key()
60{
61  while (true)
62  {
63    $key = generate_key(128);
64    $query = '
65select
66  count(*)
67from
68  '.USER_MAIL_NOTIFICATION_TABLE.'
69where
70  check_key = \''.$key.'\';';
71
72    list($count) = mysql_fetch_row(pwg_query($query));
73    if ($count == 0)
74    {
75      return $key;
76    }
77  }
78}
79
80/*
81 * Updating News users
82 */
83function update_data_user_mail_notification()
84{
85  global $conf, $page;
86
87  // Set null mail_address empty
88  $query = '
89update
90  '.USERS_TABLE.'
91set
92  mail_address = null
93where
94  trim(mail_address) = \'\';';
95  pwg_query($query);
96
97  $query = '
98select
99  u.id user_id, u.username, u.mail_address
100from
101  '.USERS_TABLE.' as u left join '.USER_MAIL_NOTIFICATION_TABLE.' as m on u.id = m.user_id
102where
103  u.mail_address is not null and
104  m.user_id is null
105order by
106  id;';
107
108  $result = pwg_query($query);
109
110  if (mysql_num_rows($result) > 0)
111  {
112    $inserts = array();
113
114    while ($row = mysql_fetch_array($result))
115    {
116      array_push($inserts, array('user_id' => $row['user_id'],
117                                 'check_key' => find_available_check_key(),
118                                 'enabled' => ($conf['default_value_user_mail_notification_enabled'] == true ? 'true' : 'false')));
119      array_push($page['infos'], sprintf(l10n('nbm_User %s [%s] added.'), $row['username'], $row['mail_address']));
120    }
121
122    mass_inserts(USER_MAIL_NOTIFICATION_TABLE, array('user_id', 'check_key', 'enabled'), $inserts);
123  }
124}
125
126/*
127 * Updating News users
128 */
129function send_all_user_mail_notification()
130{
131  global $conf, $conf_mail, $page, $user, $lang_info, $lang;
132  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
133
134  $query = '
135select
136  N.user_id, U.username, U.mail_address, N.last_send
137from
138  '.USER_MAIL_NOTIFICATION_TABLE.' as N,
139  '.USERS_TABLE.' as U
140where
141  N.user_id =  U.id and
142  N.enabled = \'true\' and
143  U.mail_address is not null
144order by
145  user_id;';
146
147  $result = pwg_query($query);
148
149  if (mysql_num_rows($result) > 0)
150  {
151    $error_on_mail_count = 0;
152    $sended_mail_count = 0;
153    $datas = array();
154    // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
155    $sav_mailtousers_user = $user;
156    $sav_mailtousers_lang_info = $lang_info;
157    $sav_mailtousers_lang = $lang;
158    // Save message info and error in the original language
159    $msg_info = l10n('nbm_Mail sended to %s [%s].');
160    $msg_error = l10n('nbm_Error when sending email to %s [%s].');
161    // Last Language
162    $last_mailtousers_language = $user['language'];
163
164    while ($row = mysql_fetch_array($result))
165    {
166      $user = array();
167      $user['id'] = $row['user_id'];
168      $user = array_merge($user, getuserdata($user['id'], true));
169
170      if ($last_mailtousers_language != $user['language'])
171      {
172        $last_mailtousers_language = $user['language'];
173
174        // Re-Init language arrays
175        $lang_info = array();
176        $lang  = array();
177
178        // language files
179        include(get_language_filepath('common.lang.php'));
180        // No test admin because script is checked admin (user selected no)
181        // Translations are in admin file too
182        include(get_language_filepath('admin.lang.php'));
183      }
184
185      $message = '';
186      $news = news($row['last_send'], $dbnow);
187      if (count($news) > 0)
188      {
189        $subject = '['.$conf['gallery_title'].']: '.l10n('nbm_New elements added');
190        $message .= sprintf(l10n('nbm_Hello %s'), $row['username']).",\n\n";
191
192        if (!is_null($row['last_send']))
193          $message .= sprintf(l10n('nbm_New elements were added between %s and %s:'), $row['last_send'], $dbnow);
194        else
195          $message .= sprintf(l10n('nbm_New elements were added on %s:'), $dbnow);
196        $message .= "\n";
197
198        foreach ($news as $line)
199        {
200          $message .= '  o '.$line."\n";
201        }
202
203        $message .= "\n".sprintf(l10n('nbm_Go to %s %s.'), $conf['gallery_title'], $conf['gallery_url'])."\n\n";
204        $message .= "\n".sprintf(l10n('nbm_To unsubscribe send a message to %s.'), $conf_mail['email_webmaster'])."\n\n";
205
206        if (pwg_mail(format_email($row['username'], $row['mail_address']), '', $subject, $message))
207        {
208          $sended_mail_count += 1;
209          array_push($page['infos'], sprintf($msg_info, $row['username'], $row['mail_address']));
210          $data = array('user_id' => $row['user_id'],
211                        'last_send' => $dbnow);
212          array_push($datas, $data);
213        }
214        else
215        {
216          $error_on_mail_count += 1;
217          array_push($page['errors'], sprintf($msg_error, $row['username'], $row['mail_address']));
218        }
219      }
220    }
221
222    // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed)
223    $user = $sav_mailtousers_user;
224    $lang_info = $sav_mailtousers_lang_info;
225    $lang = $sav_mailtousers_lang;
226
227    mass_updates(
228      USER_MAIL_NOTIFICATION_TABLE,
229      array(
230        'primary' => array('user_id'),
231        'update' => array('last_send')
232       ),
233       $datas
234       );
235
236    if ($error_on_mail_count != 0)
237    {
238      array_push($page['errors'], sprintf(l10n('nbm_%d mails were not sended.'), $error_on_mail_count));
239    }
240    else
241    {
242      if ($sended_mail_count == 0)
243        array_push($page['infos'], l10n('nbm_No mail to send.'));
244      else
245        array_push($page['infos'], sprintf(l10n('nbm_%d mails were sended.'), $sended_mail_count));
246    }
247  }
248  else
249  {
250    array_push($page['errors'], l10n('nbm_No user to send notifications by mail.'));
251  }
252}
253
254// +-----------------------------------------------------------------------+
255// | Main                                                                  |
256// +-----------------------------------------------------------------------+
257update_data_user_mail_notification();
258//send_all_user_mail_notification();
259
260if (!isset($_GET['mode']))
261{
262  $page['mode'] = 'send';
263}
264else
265{
266  $page['mode'] = $_GET['mode'];
267}
268
269// +-----------------------------------------------------------------------+
270// | template initialization                                               |
271// +-----------------------------------------------------------------------+
272$template->set_filenames(
273  array(
274    'double_select' => 'admin/double_select.tpl',
275    'notification_by_mail'=>'admin/notification_by_mail.tpl'
276    )
277  );
278
279$base_url = get_root_url().'admin.php';
280
281$template->assign_vars(
282  array(
283    'U_TABSHEET_TITLE' => l10n('nbm_'.$page['mode'].'_mode'),
284    'U_HELP' => add_url_params(get_root_url().'/popuphelp.php', array('page'=>'notification_by_mail') ),
285    'U_PARAM_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'param') ),
286    'U_SUBSCRIBE_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'subscribe') ),
287    'U_SEND_MODE' => add_url_params($base_url.get_query_string_diff(array('mode')), array('mode'=>'send') ),
288    'F_ACTION'=> $base_url.get_query_string_diff(array())
289    ));
290
291switch ($page['mode'])
292{
293  case 'param' :
294  {
295    $template->assign_block_vars(
296      $page['mode'],
297      array(
298        //'HISTORY_YES'=>$history_yes
299        ));
300    break;
301  }
302  case 'subscribe' :
303  {
304    $template->assign_block_vars(
305      $page['mode'],
306      array(
307        //'HISTORY_YES'=>$history_yes
308        ));
309
310    $template->assign_vars(
311      array(
312        'L_CAT_OPTIONS_TRUE' => l10n('nbm_subscribe_col'),
313        'L_CAT_OPTIONS_FALSE' => l10n('nbm_unsubscribe_col')
314        )
315      );
316
317
318/*    $template->assign_block_vars(
319      $blockname,
320      array('SELECTED'=>$selected,
321            'VALUE'=>$category['id'],
322            'OPTION'=>$option
323        ));*/
324    $template->assign_block_vars(
325      'category_option_true',
326      array('SELECTED'=>'',
327            'VALUE'=>'rub',
328            'OPTION'=>'rub [rub@phpwebgallery.net]'
329        ));
330
331    break;
332  }
333  case 'send' :
334  {
335    $template->assign_block_vars(
336      $page['mode'],
337      array(
338        //'HISTORY_YES'=>$history_yes
339        ));
340
341    $template->assign_vars(
342      array(
343        'L_CAT_OPTIONS_TRUE' => l10n('nbm_send_col'),
344        'L_CAT_OPTIONS_FALSE' => l10n('nbm_nosend_col')
345        )
346      );
347
348
349/*    $template->assign_block_vars(
350      $blockname,
351      array('SELECTED'=>$selected,
352            'VALUE'=>$category['id'],
353            'OPTION'=>$option
354        ));*/
355    $template->assign_block_vars(
356      'category_option_true',
357      array('SELECTED'=>' selected="selected"',
358            'VALUE'=>'rub',
359            'OPTION'=>'rub [2006-03-20 23:35:23]'
360        ));
361
362    break;
363  }
364}
365
366// +-----------------------------------------------------------------------+
367// | infos & errors display                                                |
368// +-----------------------------------------------------------------------+
369
370/*echo '<pre>';
371
372if (count($page['errors']) != 0)
373{
374  echo "\n\nErrors:\n";
375  foreach ($page['errors'] as $error)
376  {
377    echo $error."\n";
378  }
379}
380
381if (count($page['infos']) != 0)
382{
383  echo "\n\nInformations:\n";
384  foreach ($page['infos'] as $info)
385  {
386    echo $info."\n";
387  }
388}
389
390echo '</pre>';
391*/
392
393// +-----------------------------------------------------------------------+
394// | Sending html code                                                     |
395// +-----------------------------------------------------------------------+
396$template->assign_var_from_handle('DOUBLE_SELECT', 'double_select');
397$template->assign_var_from_handle('ADMIN_CONTENT', 'notification_by_mail');
398
399?>
Note: See TracBrowser for help on using the repository browser.