1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
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 | // +-----------------------------------------------------------------------+ |
---|
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 | |
---|
32 | if |
---|
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 | */ |
---|
49 | function find_available_check_key() |
---|
50 | { |
---|
51 | while (true) |
---|
52 | { |
---|
53 | $key = generate_key(16); |
---|
54 | $query = ' |
---|
55 | select |
---|
56 | count(*) |
---|
57 | from |
---|
58 | '.USER_MAIL_NOTIFICATION_TABLE.' |
---|
59 | where |
---|
60 | check_key = \''.$key.'\';'; |
---|
61 | |
---|
62 | list($count) = pwg_db_fetch_row(pwg_query($query)); |
---|
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 | */ |
---|
75 | function 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 | */ |
---|
90 | function quote_check_key_list($check_key_list = array()) |
---|
91 | { |
---|
92 | return array_map(create_function('$s', 'return \'\\\'\'.$s.\'\\\'\';'), $check_key_list); |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * Execute all main queries to get list of user |
---|
97 | * |
---|
98 | * Type are the type of list 'subscribe', 'send' |
---|
99 | * |
---|
100 | * return array of users |
---|
101 | */ |
---|
102 | function 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 | { |
---|
113 | $query_and_check_key = ' and |
---|
114 | check_key in ('.implode(",", $quoted_check_key_list).') '; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | $query_and_check_key = ''; |
---|
119 | } |
---|
120 | |
---|
121 | $query = ' |
---|
122 | select |
---|
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 |
---|
129 | from |
---|
130 | '.USER_MAIL_NOTIFICATION_TABLE.' as N, |
---|
131 | '.USERS_TABLE.' as U |
---|
132 | where |
---|
133 | N.user_id = U.'.$conf['user_fields']['id']; |
---|
134 | |
---|
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 .= ' |
---|
152 | order 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 | { |
---|
170 | while ($nbm_user = pwg_db_fetch_assoc($result)) |
---|
171 | { |
---|
172 | $data_users[] = $nbm_user; |
---|
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 | */ |
---|
185 | function 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; |
---|
191 | // Save current language to stack, necessary because $user change during NBM |
---|
192 | switch_lang_to($user['language']); |
---|
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']); |
---|
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()); |
---|
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 |
---|
207 | $env_nbm['msg_info'] = l10n('Mail sent to %s [%s].'); |
---|
208 | $env_nbm['msg_error'] = l10n('Error when sending email to %s [%s].'); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | /* |
---|
213 | * End of use nbm environment |
---|
214 | * Restore environment |
---|
215 | * |
---|
216 | * Return none |
---|
217 | */ |
---|
218 | function 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']; |
---|
224 | // Restore current language to stack, necessary because $user change during NBM |
---|
225 | switch_lang_back(); |
---|
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 | */ |
---|
249 | function 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 | |
---|
255 | switch_lang_to($user['language']); |
---|
256 | |
---|
257 | if ($is_action_send) |
---|
258 | { |
---|
259 | $env_nbm['mail_template'] = get_mail_template($env_nbm['email_format']); |
---|
260 | $env_nbm['mail_template']->set_filename('notification_by_mail', 'notification_by_mail.tpl'); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | /* |
---|
265 | * Unset user on nbm enviromnent |
---|
266 | * |
---|
267 | * Return none |
---|
268 | */ |
---|
269 | function unset_user_on_env_nbm() |
---|
270 | { |
---|
271 | global $env_nbm; |
---|
272 | |
---|
273 | switch_lang_back(); |
---|
274 | unset($env_nbm['mail_template']); |
---|
275 | } |
---|
276 | |
---|
277 | /* |
---|
278 | * Inc Counter success |
---|
279 | * |
---|
280 | * Return none |
---|
281 | */ |
---|
282 | function inc_mail_sent_success($nbm_user) |
---|
283 | { |
---|
284 | global $page, $env_nbm; |
---|
285 | |
---|
286 | $env_nbm['sent_mail_count'] += 1; |
---|
287 | $page['infos'][] = sprintf($env_nbm['msg_info'], stripslashes($nbm_user['username']), $nbm_user['mail_address']); |
---|
288 | } |
---|
289 | |
---|
290 | /* |
---|
291 | * Inc Counter failed |
---|
292 | * |
---|
293 | * Return none |
---|
294 | */ |
---|
295 | function inc_mail_sent_failed($nbm_user) |
---|
296 | { |
---|
297 | global $page, $env_nbm; |
---|
298 | |
---|
299 | $env_nbm['error_on_mail_count'] += 1; |
---|
300 | $page['errors'][] = sprintf($env_nbm['msg_error'], stripslashes($nbm_user['username']), $nbm_user['mail_address']); |
---|
301 | } |
---|
302 | |
---|
303 | /* |
---|
304 | * Display Counter Info |
---|
305 | * |
---|
306 | * Return none |
---|
307 | */ |
---|
308 | function display_counter_info() |
---|
309 | { |
---|
310 | global $page, $env_nbm; |
---|
311 | |
---|
312 | if ($env_nbm['error_on_mail_count'] != 0) |
---|
313 | { |
---|
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 | |
---|
319 | if ($env_nbm['sent_mail_count'] != 0) |
---|
320 | { |
---|
321 | $page['infos'][] = l10n_dec( |
---|
322 | '%d mail was sent.', '%d mails were sent.', |
---|
323 | $env_nbm['sent_mail_count'] |
---|
324 | ); |
---|
325 | } |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | if ($env_nbm['sent_mail_count'] == 0) |
---|
330 | { |
---|
331 | $page['infos'][] = l10n('No mail to send.'); |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | $page['infos'][] = l10n_dec( |
---|
336 | '%d mail was sent.', '%d mails were sent.', |
---|
337 | $env_nbm['sent_mail_count'] |
---|
338 | ); |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | function assign_vars_nbm_mail_content($nbm_user) |
---|
344 | { |
---|
345 | global $env_nbm; |
---|
346 | |
---|
347 | set_make_full_url(); |
---|
348 | |
---|
349 | $env_nbm['mail_template']->assign |
---|
350 | ( |
---|
351 | array |
---|
352 | ( |
---|
353 | 'USERNAME' => stripslashes($nbm_user['username']), |
---|
354 | |
---|
355 | 'SEND_AS_NAME' => $env_nbm['send_as_name'], |
---|
356 | |
---|
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'])), |
---|
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 | */ |
---|
374 | function 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 | |
---|
378 | set_make_full_url(); |
---|
379 | |
---|
380 | $check_key_treated = array(); |
---|
381 | $updated_data_count = 0; |
---|
382 | $error_on_updated_data_count = 0; |
---|
383 | |
---|
384 | if ($is_subscribe) |
---|
385 | { |
---|
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.'); |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
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.'); |
---|
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 |
---|
402 | $msg_break_timeout = l10n('Time to send mail is limited. Others mails are skipped.'); |
---|
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 |
---|
412 | $page['errors'][] = $msg_break_timeout; |
---|
413 | break; |
---|
414 | } |
---|
415 | |
---|
416 | // Fill return list |
---|
417 | $check_key_treated[] = $nbm_user['check_key']; |
---|
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 | |
---|
425 | $subject = '['.$conf['gallery_title'].'] '.($is_subscribe ? l10n('Subscribe to notification by mail'): l10n('Unsubscribe from notification by mail')); |
---|
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'); |
---|
432 | $env_nbm['mail_template']->assign |
---|
433 | ( |
---|
434 | array |
---|
435 | ( |
---|
436 | $section_action_by => true, |
---|
437 | 'GOTO_GALLERY_TITLE' => $conf['gallery_title'], |
---|
438 | 'GOTO_GALLERY_URL' => get_gallery_home_url(), |
---|
439 | ) |
---|
440 | ); |
---|
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 | ); |
---|
455 | |
---|
456 | if ($ret) |
---|
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 | { |
---|
473 | $updates[] = array( |
---|
474 | 'check_key' => $nbm_user['check_key'], |
---|
475 | 'enabled' => $enabled_value |
---|
476 | ); |
---|
477 | $updated_data_count += 1; |
---|
478 | $page['infos'][] = sprintf($msg_info, stripslashes($nbm_user['username']), $nbm_user['mail_address']); |
---|
479 | } |
---|
480 | else |
---|
481 | { |
---|
482 | $error_on_updated_data_count += 1; |
---|
483 | $page['errors'][] = sprintf($msg_error, stripslashes($nbm_user['username']), $nbm_user['mail_address']); |
---|
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 | |
---|
504 | $page['infos'][] = l10n_dec( |
---|
505 | '%d user was updated.', '%d users were updated.', |
---|
506 | $updated_data_count |
---|
507 | ); |
---|
508 | |
---|
509 | if ($error_on_updated_data_count != 0) |
---|
510 | { |
---|
511 | $page['errors'][] = l10n_dec( |
---|
512 | '%d user was not updated.', '%d users were not updated.', |
---|
513 | $error_on_updated_data_count |
---|
514 | ); |
---|
515 | } |
---|
516 | |
---|
517 | unset_make_full_url(); |
---|
518 | |
---|
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 | */ |
---|
529 | function 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 | */ |
---|
541 | function 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 | |
---|
546 | ?> |
---|