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 02:49:04 +0100 (jeu., 23 mars 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 | /* nbm_global_var */ |
---|
30 | $env_nbm = array |
---|
31 | ( |
---|
32 | 'start_time' => get_moment(), |
---|
33 | 'sendmail_timeout' => (intval(ini_get('max_execution_time')) * $conf['nbm_max_treatment_timeout_percent']), |
---|
34 | 'is_sendmail_timeout' => false |
---|
35 | ); |
---|
36 | |
---|
37 | if |
---|
38 | ( |
---|
39 | (!isset($env_nbm['sendmail_timeout'])) or |
---|
40 | (!is_numeric($env_nbm['sendmail_timeout'])) or |
---|
41 | ($env_nbm['sendmail_timeout'] <= 0) |
---|
42 | ) |
---|
43 | { |
---|
44 | $env_nbm['sendmail_timeout'] = $conf['nbm_treatment_timeout_default']; |
---|
45 | } |
---|
46 | |
---|
47 | /* |
---|
48 | * Search an available check_key |
---|
49 | * |
---|
50 | * It's a copy of function find_available_feed_id |
---|
51 | * |
---|
52 | * @return string nbm identifier |
---|
53 | */ |
---|
54 | function find_available_check_key() |
---|
55 | { |
---|
56 | while (true) |
---|
57 | { |
---|
58 | $key = generate_key(16); |
---|
59 | $query = ' |
---|
60 | select |
---|
61 | count(*) |
---|
62 | from |
---|
63 | '.USER_MAIL_NOTIFICATION_TABLE.' |
---|
64 | where |
---|
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 | * Check sendmail timeout state |
---|
77 | * |
---|
78 | * @return true, if it's timeout |
---|
79 | */ |
---|
80 | function check_sendmail_timeout() |
---|
81 | { |
---|
82 | global $env_nbm; |
---|
83 | |
---|
84 | $env_nbm['is_sendmail_timeout'] = ((get_moment() - $env_nbm['start_time']) > $env_nbm['sendmail_timeout']); |
---|
85 | |
---|
86 | return $env_nbm['is_sendmail_timeout']; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /* |
---|
91 | * Add quote to all elements of check_key_list |
---|
92 | * |
---|
93 | * @return quoted check key list |
---|
94 | */ |
---|
95 | function quote_check_key_list($check_key_list = array()) |
---|
96 | { |
---|
97 | return array_map(create_function('$s', 'return \'\\\'\'.$s.\'\\\'\';'), $check_key_list); |
---|
98 | } |
---|
99 | |
---|
100 | /* |
---|
101 | * Execute all main queries to get list of user |
---|
102 | * |
---|
103 | * Type are the type of list 'subscribe', 'send' |
---|
104 | * |
---|
105 | * return array of users |
---|
106 | */ |
---|
107 | function get_user_notifications($action, $check_key_list = array(), $enabled_filter_value = '') |
---|
108 | { |
---|
109 | global $conf; |
---|
110 | |
---|
111 | $data_users = array(); |
---|
112 | |
---|
113 | if (in_array($action, array('subscribe', 'send'))) |
---|
114 | { |
---|
115 | $quoted_check_key_list = quote_check_key_list($check_key_list); |
---|
116 | if (count($quoted_check_key_list) != 0 ) |
---|
117 | { |
---|
118 | $query_and_check_key = ' and |
---|
119 | check_key in ('.implode(",", $quoted_check_key_list).') '; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | $query_and_check_key = ''; |
---|
124 | } |
---|
125 | |
---|
126 | $query = ' |
---|
127 | select |
---|
128 | N.user_id, |
---|
129 | N.check_key, |
---|
130 | U.'.$conf['user_fields']['username'].' as username, |
---|
131 | U.'.$conf['user_fields']['email'].' as mail_address, |
---|
132 | N.enabled, |
---|
133 | N.last_send |
---|
134 | from |
---|
135 | '.USER_MAIL_NOTIFICATION_TABLE.' as N, |
---|
136 | '.USERS_TABLE.' as U |
---|
137 | where |
---|
138 | N.user_id = U.'.$conf['user_fields']['id']; |
---|
139 | |
---|
140 | if ($action == 'send') |
---|
141 | { |
---|
142 | // No mail empty and all users enabled |
---|
143 | $query .= ' and |
---|
144 | N.enabled = \'true\' and |
---|
145 | U.'.$conf['user_fields']['email'].' is not null'; |
---|
146 | } |
---|
147 | |
---|
148 | $query .= $query_and_check_key; |
---|
149 | |
---|
150 | if (isset($enabled_filter_value) and ($enabled_filter_value != '')) |
---|
151 | { |
---|
152 | $query .= ' and |
---|
153 | N.enabled = \''.boolean_to_string($enabled_filter_value).'\''; |
---|
154 | } |
---|
155 | |
---|
156 | $query .= ' |
---|
157 | order by'; |
---|
158 | |
---|
159 | if ($action == 'send') |
---|
160 | { |
---|
161 | $query .= ' |
---|
162 | last_send, username;'; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | $query .= ' |
---|
167 | username;'; |
---|
168 | } |
---|
169 | |
---|
170 | $query .= ';'; |
---|
171 | |
---|
172 | $result = pwg_query($query); |
---|
173 | if (!empty($result)) |
---|
174 | { |
---|
175 | while ($nbm_user = mysql_fetch_array($result)) |
---|
176 | { |
---|
177 | array_push($data_users, $nbm_user); |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | return $data_users; |
---|
182 | } |
---|
183 | |
---|
184 | /* |
---|
185 | * Begin of use nbm environment |
---|
186 | * Prepare and save current environment and initialize data in order to send mail |
---|
187 | * |
---|
188 | * Return none |
---|
189 | */ |
---|
190 | function begin_users_env_nbm($is_to_send_mail = false) |
---|
191 | { |
---|
192 | global $user, $lang, $lang_info, $conf, $env_nbm; |
---|
193 | |
---|
194 | // Save $user, $lang_info and $lang arrays (include/user.inc.php has been executed) |
---|
195 | $env_nbm['save_user'] = $user; |
---|
196 | $env_nbm['save_lang_info'] = $lang_info; |
---|
197 | $env_nbm['save_lang'] = $lang; |
---|
198 | // Last Language |
---|
199 | $env_nbm['last_language'] = $user['language']; |
---|
200 | |
---|
201 | $env_nbm['is_to_send_mail'] = $is_to_send_mail; |
---|
202 | |
---|
203 | if ($is_to_send_mail) |
---|
204 | { |
---|
205 | // Init mail configuration |
---|
206 | $env_nbm['send_as_name'] = ((isset($conf['nbm_send_mail_as']) and !empty($conf['nbm_send_mail_as'])) ? $conf['nbm_send_mail_as'] : $conf['gallery_title']); |
---|
207 | $env_nbm['send_as_mail_address'] = get_webmaster_mail_address(); |
---|
208 | $env_nbm['send_as_mail_formated'] = format_email($env_nbm['send_as_name'], $env_nbm['send_as_mail_address']); |
---|
209 | // Init mail counter |
---|
210 | $env_nbm['error_on_mail_count'] = 0; |
---|
211 | $env_nbm['sent_mail_count'] = 0; |
---|
212 | // Save sendmail message info and error in the original language |
---|
213 | $env_nbm['msg_info'] = l10n('nbm_msg_mail_sent_to'); |
---|
214 | $env_nbm['msg_error'] = l10n('nbm_msg_error_sending_email_to'); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /* |
---|
219 | * End of use nbm environment |
---|
220 | * Restore environment |
---|
221 | * |
---|
222 | * Return none |
---|
223 | */ |
---|
224 | function end_users_env_nbm() |
---|
225 | { |
---|
226 | global $user, $lang, $lang_info, $env_nbm; |
---|
227 | |
---|
228 | // Restore $user, $lang_info and $lang arrays (include/user.inc.php has been executed) |
---|
229 | $user = $env_nbm['save_user']; |
---|
230 | $lang_info = $env_nbm['save_lang_info']; |
---|
231 | $lang = $env_nbm['save_lang']; |
---|
232 | } |
---|
233 | |
---|
234 | /* |
---|
235 | * Set user_id on nbm enviromnent |
---|
236 | * |
---|
237 | * Return none |
---|
238 | */ |
---|
239 | function set_user_id_on_env_nbm($user_id) |
---|
240 | { |
---|
241 | global $user, $lang, $lang_info, $env_nbm; |
---|
242 | |
---|
243 | $user = array(); |
---|
244 | $user['id'] = $user_id; |
---|
245 | $user = array_merge($user, getuserdata($user['id'], true)); |
---|
246 | |
---|
247 | if ($env_nbm['last_language'] != $user['language']) |
---|
248 | { |
---|
249 | $env_nbm['last_language'] = $user['language']; |
---|
250 | |
---|
251 | // Re-Init language arrays |
---|
252 | $lang_info = array(); |
---|
253 | $lang = array(); |
---|
254 | |
---|
255 | // language files |
---|
256 | include(get_language_filepath('common.lang.php')); |
---|
257 | @include(get_language_filepath('local.lang.php')); |
---|
258 | // No test admin because script is checked admin (user selected no) |
---|
259 | // Translations are in admin file too |
---|
260 | include(get_language_filepath('admin.lang.php')); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | /* |
---|
265 | * Inc Counter success |
---|
266 | * |
---|
267 | * Return none |
---|
268 | */ |
---|
269 | function inc_mail_sent_success($nbm_user) |
---|
270 | { |
---|
271 | global $page, $env_nbm; |
---|
272 | |
---|
273 | $env_nbm['sent_mail_count'] += 1; |
---|
274 | array_push($page['infos'], sprintf($env_nbm['msg_info'], $nbm_user['username'], $nbm_user['mail_address'])); |
---|
275 | } |
---|
276 | |
---|
277 | /* |
---|
278 | * Inc Counter failed |
---|
279 | * |
---|
280 | * Return none |
---|
281 | */ |
---|
282 | function inc_mail_sent_failed($nbm_user) |
---|
283 | { |
---|
284 | global $page, $env_nbm; |
---|
285 | |
---|
286 | $env_nbm['error_on_mail_count'] += 1; |
---|
287 | array_push($page['errors'], sprintf($env_nbm['msg_error'], $nbm_user['username'], $nbm_user['mail_address'])); |
---|
288 | } |
---|
289 | |
---|
290 | /* |
---|
291 | * Display Counter Info |
---|
292 | * |
---|
293 | * Return none |
---|
294 | */ |
---|
295 | function display_counter_info() |
---|
296 | { |
---|
297 | global $page, $env_nbm; |
---|
298 | |
---|
299 | if ($env_nbm['error_on_mail_count'] != 0) |
---|
300 | { |
---|
301 | array_push($page['errors'], sprintf(l10n('nbm_msg_no_mail_to_send'), $env_nbm['error_on_mail_count'])); |
---|
302 | if ($env_nbm['sent_mail_count'] != 0) |
---|
303 | array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count'])); |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | if ($env_nbm['sent_mail_count'] == 0) |
---|
308 | array_push($page['infos'], l10n('nbm_no_mail_to_send')); |
---|
309 | else |
---|
310 | array_push($page['infos'], sprintf(l10n('nbm_msg_n_mails_sent'), $env_nbm['sent_mail_count'])); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | function get_mail_content_subscribe_unsubcribe($nbm_user) |
---|
315 | { |
---|
316 | global $env_nbm; |
---|
317 | |
---|
318 | $content = "\n\n\n"; |
---|
319 | |
---|
320 | set_make_full_url(); |
---|
321 | |
---|
322 | $content .= "___________________________________________________\n\n"; |
---|
323 | $content .= sprintf(l10n('nbm_content_unsubscribe_link'), add_url_params(get_root_url().'nbm.php', array('unsubscribe' => $nbm_user['check_key'])))."\n"; |
---|
324 | $content .= sprintf(l10n('nbm_content_subscribe_link'), add_url_params(get_root_url().'nbm.php', array('subscribe' => $nbm_user['check_key'])))."\n"; |
---|
325 | $content .= sprintf(l10n('nbm_content_subscribe_unsubscribe_contact'), $env_nbm['send_as_mail_address'])."\n"; |
---|
326 | $content .= "___________________________________________________\n\n\n\n"; |
---|
327 | |
---|
328 | unset_make_full_url(); |
---|
329 | |
---|
330 | return $content; |
---|
331 | } |
---|
332 | |
---|
333 | /* |
---|
334 | * Subscribe or unsubscribe notification by mail |
---|
335 | * |
---|
336 | * is_subscribe define if action=subscribe or unsubscribe |
---|
337 | * check_key list where action will be done |
---|
338 | * |
---|
339 | * @return check_key list treated |
---|
340 | */ |
---|
341 | function do_subscribe_unsubcribe_notification_by_mail($is_admin_request, $is_subscribe = false, $check_key_list = array()) |
---|
342 | { |
---|
343 | global $conf, $page, $env_nbm, $conf; |
---|
344 | |
---|
345 | $check_key_treated = array(); |
---|
346 | $updated_data_count = 0; |
---|
347 | $error_on_updated_data_count = 0; |
---|
348 | |
---|
349 | if ($is_subscribe) |
---|
350 | { |
---|
351 | $msg_info = l10n('nbm_user_change_enabled_true'); |
---|
352 | $msg_error = l10n('nbm_user_not_change_enabled_true'); |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | $msg_info = l10n('nbm_user_change_enabled_false'); |
---|
357 | $msg_error = l10n('nbm_user_not_change_enabled_false'); |
---|
358 | } |
---|
359 | |
---|
360 | if (count($check_key_list) != 0) |
---|
361 | { |
---|
362 | $updates = array(); |
---|
363 | $enabled_value = boolean_to_string($is_subscribe); |
---|
364 | $data_users = get_user_notifications('subscribe', $check_key_list, !$is_subscribe); |
---|
365 | |
---|
366 | // Prepare message after change language |
---|
367 | $msg_break_timeout = l10n('nbm_break_timeout_send_mail'); |
---|
368 | |
---|
369 | // Begin nbm users environment |
---|
370 | begin_users_env_nbm(true); |
---|
371 | |
---|
372 | foreach ($data_users as $nbm_user) |
---|
373 | { |
---|
374 | if (check_sendmail_timeout()) |
---|
375 | { |
---|
376 | // Stop fill list on 'send', if the quota is override |
---|
377 | array_push($page['errors'], $msg_break_timeout); |
---|
378 | break; |
---|
379 | } |
---|
380 | |
---|
381 | // Fill return list |
---|
382 | array_push($check_key_treated, $nbm_user['check_key']); |
---|
383 | |
---|
384 | $do_update = true; |
---|
385 | if ($nbm_user['mail_address'] != '') |
---|
386 | { |
---|
387 | // set env nbm user |
---|
388 | set_user_id_on_env_nbm($nbm_user['user_id']); |
---|
389 | |
---|
390 | $message = ''; |
---|
391 | |
---|
392 | $subject = '['.$conf['gallery_title'].']: '.($is_subscribe ? l10n('nbm_object_subcribe'): l10n('nbm_object_unsubcribe')); |
---|
393 | $message .= sprintf(l10n('nbm_content_hello'), $nbm_user['username']).",\n\n"; |
---|
394 | |
---|
395 | if ($is_subscribe) |
---|
396 | { |
---|
397 | $message .= l10n($is_admin_request ? 'nbm_content_subscribe_by_admin' : 'nbm_content_subscribe_by_himself'); |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | $message .= l10n($is_admin_request ? 'nbm_content_unsubscribe_by_admin' : 'nbm_content_unsubscribe_by_himself'); |
---|
402 | } |
---|
403 | |
---|
404 | $message .= "\n\n"; |
---|
405 | $message .= l10n('nbm_content_byebye')."\n ".$env_nbm['send_as_name']."\n\n"; |
---|
406 | |
---|
407 | $message .= get_mail_content_subscribe_unsubcribe($nbm_user); |
---|
408 | |
---|
409 | if (pwg_mail(format_email($nbm_user['username'], $nbm_user['mail_address']), $env_nbm['send_as_mail_formated'], $subject, $message)) |
---|
410 | { |
---|
411 | inc_mail_sent_success($nbm_user); |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | inc_mail_sent_failed($nbm_user); |
---|
416 | $do_update = false; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | if ($do_update) |
---|
421 | { |
---|
422 | array_push |
---|
423 | ( |
---|
424 | $updates, |
---|
425 | array |
---|
426 | ( |
---|
427 | 'check_key' => $nbm_user['check_key'], |
---|
428 | 'enabled' => $enabled_value |
---|
429 | ) |
---|
430 | ); |
---|
431 | $updated_data_count += 1; |
---|
432 | array_push($page['infos'], sprintf($msg_info, $nbm_user['username'], $nbm_user['mail_address'])); |
---|
433 | } |
---|
434 | else |
---|
435 | { |
---|
436 | $error_on_updated_data_count += 1; |
---|
437 | array_push($page['errors'], sprintf($msg_error, $nbm_user['username'], $nbm_user['mail_address'])); |
---|
438 | } |
---|
439 | |
---|
440 | } |
---|
441 | |
---|
442 | // Restore nbm environment |
---|
443 | end_users_env_nbm(); |
---|
444 | |
---|
445 | display_counter_info(); |
---|
446 | |
---|
447 | mass_updates( |
---|
448 | USER_MAIL_NOTIFICATION_TABLE, |
---|
449 | array( |
---|
450 | 'primary' => array('check_key'), |
---|
451 | 'update' => array('enabled') |
---|
452 | ), |
---|
453 | $updates |
---|
454 | ); |
---|
455 | |
---|
456 | } |
---|
457 | |
---|
458 | array_push($page['infos'], sprintf(l10n('nbm_user_change_enabled_updated_data_count'), $updated_data_count)); |
---|
459 | if ($error_on_updated_data_count != 0) |
---|
460 | { |
---|
461 | array_push($page['errors'], sprintf(l10n('nbm_user_change_enabled_error_on_updated_data_count'), $error_on_updated_data_count)); |
---|
462 | } |
---|
463 | |
---|
464 | return $check_key_treated; |
---|
465 | } |
---|
466 | |
---|
467 | /* |
---|
468 | * Unsubscribe notification by mail |
---|
469 | * |
---|
470 | * check_key list where action will be done |
---|
471 | * |
---|
472 | * @return check_key list treated |
---|
473 | */ |
---|
474 | function unsubcribe_notification_by_mail($is_admin_request, $check_key_list = array()) |
---|
475 | { |
---|
476 | return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, false, $check_key_list); |
---|
477 | } |
---|
478 | |
---|
479 | /* |
---|
480 | * Subscribe notification by mail |
---|
481 | * |
---|
482 | * check_key list where action will be done |
---|
483 | * |
---|
484 | * @return check_key list treated |
---|
485 | */ |
---|
486 | function subcribe_notification_by_mail($is_admin_request, $check_key_list = array()) |
---|
487 | { |
---|
488 | return do_subscribe_unsubcribe_notification_by_mail($is_admin_request, true, $check_key_list); |
---|
489 | } |
---|
490 | |
---|
491 | ?> |
---|