-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
892 lines (730 loc) · 36.2 KB
/
plugin.php
File metadata and controls
892 lines (730 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/*
Plugin Name: Newsletter
Plugin URI: http://www.satollo.net/plugins/newsletter
Description: Newsletter is a cool plugin to create your own subscriber list, to send newsletters, to build your business. <strong>Before update give a look to <a href="http://www.satollo.net/plugins/newsletter#update">this page</a> to know what's changed.</strong>
Version: 2.5.2.6
Author: Satollo
Author URI: http://www.satollo.net
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
Copyright 2011 Stefano Lissa (email: stefano@satollo.net, web: http://www.satollo.net)
*/
define('NEWSLETTER_LIST_MAX', 40);
define('NEWSLETTER_PROFILE_MAX', 19);
global $newsletter;
$newsletter = new Newsletter();
class Newsletter {
const VERSION = 250;
var $time_limit;
var $email_limit = 10; // Per run
var $relink_email_id;
var $relink_user_id;
var $mailer;
var $options_main;
var $message;
var $user;
var $error;
function Newsletter() {
global $wpdb;
// Early possible
$max_time = (int) (@ini_get('max_execution_time') * 0.9);
if ($max_time == 0) $max_time = 600;
$this->time_limit = time() + $max_time;
register_activation_hook(__FILE__, array(&$this, 'hook_activate'));
register_deactivation_hook(__FILE__, array(&$this, 'hook_deactivate'));
add_action('init', array(&$this, 'hook_init'));
add_action('admin_init', array(&$this, 'hook_admin_init'));
add_action('mailer_bounce_email', array(&$this, 'mailer_bounce_email'));
add_filter('cron_schedules', array(&$this, 'hook_cron_schedules'), 1000);
add_action('newsletter', array(&$this, 'hook_newsletter'), 1);
// This specific event is created by "Feed by mail" panel on configuration
add_action('template_redirect', array(&$this, 'hook_template_redirect'));
add_action('wp_head', array(&$this, 'hook_wp_head'));
add_shortcode('newsletter', array(&$this, 'shortcode_newsletter'));
add_shortcode('newsletter_lock', array(&$this, 'shortcode_newsletter_lock'));
add_shortcode('newsletter_form', array(&$this, 'shortcode_newsletter_form'));
add_shortcode('newsletter_embed', array(&$this, 'shortcode_newsletter_form'));
add_action('shutdown', array(&$this, 'hook_shutdown'));
if (is_admin ()) {
add_action('admin_menu', array(&$this, 'hook_admin_menu'));
add_action('admin_head', array(&$this, 'hook_admin_head'));
}
$this->options_main = get_option('newsletter_main', array());
}
function hook_admin_head() {
if (strpos($_GET['page'], 'newsletter/') === 0) {
echo '<link type="text/css" rel="stylesheet" href="' .
get_option('siteurl') . '/wp-content/plugins/newsletter/style.css"/>';
}
}
function hook_admin_menu() {
include 'plugin-menu.inc.php';
}
function hook_wp_head() {
include 'plugin-head.inc.php';
}
function hook_newsletter_feed() {
}
function check_transient($name, $time) {
usleep(rand(0, 1000000));
if (get_transient($name) !== false) {
$this->log('Called too quickly');
return false;
}
set_transient($name, 1, $time);
return true;
}
function hook_newsletter() {
global $wpdb;
//$this->log();
if (!$this->check_transient('newsletter', 60)) return;
$max = $this->options_main['scheduler_max'];
if (!is_numeric($max)) $max = 100;
$this->email_limit = max(floor($max / 12), 1);
$this->set_limits();
// Retrieve all email in "sending" status
$emails = $wpdb->get_results("select * from " . $wpdb->prefix . "newsletter_emails where status='sending' order by id desc");
foreach ($emails as $email) {
if (!$this->send($email)) return;
}
}
function send($email, $users = null) {
global $wpdb;
$this->log();
if ($this->limits_exceeded()) return false;
if ($users == null) {
// Fake value representing the WordPress users target
if ($email->list == -1) {
if (!empty($email->query)) $query = "select * from " . $wpdb->prefix . "users " . $email->query . " and id>" . $email->last_id . " order by id limit " . $this->email_limit;
else $query = "select * from " . $wpdb->prefix . "users where id>" . $email->last_id . " order by id limit " . $this->email_limit;
$this->log($query, 3);
$wp_users = $wpdb->get_results($query);
$users = array();
foreach ($wp_users as &$wp_user) {
$user = new stdClass();
$user->email = $wp_user->user_email;
$user->name = $wp_user->user_login;
$user->id = -$wp_user->ID;
$users[] = $user;
}
}
else {
if (!empty($email->query)) $query = "select * from " . $wpdb->prefix . "newsletter " . $email->query;
else {
$query = "select * from " . $wpdb->prefix . "newsletter where status='C'";
if ($email->list != 0) $query .= " and list_" . $email->list . "=1";
if (!empty($email->sex)) $query .= " and sex='" . $email->sex . "'";
}
$query .= " and id>" . $email->last_id . " order by id limit " . $this->email_limit;
$this->log($query, 3);
$users = $wpdb->get_results($query);
}
}
if (empty($users)) {
$this->log('No more users');
$wpdb->query("update " . $wpdb->prefix . "newsletter_emails set status='sent' where id=" . $email->id);
return;
}
foreach ($users as $user) {
$headers = array('List-Unsubscribe' => '<' .
$this->add_qs($this->options_main['url'], 'na=u&ni=' . $user->id . '&nt=' . $user->token) . '>');
$m = $this->execute($email->message, $user);
if (empty($m)) continue;
$m = $this->replace($m, $user);
if ($email->track == 1) $m = $this->relink($m, $email->id, $user->id);
// Add CSS
if ($email->theme[0] == '*') $file = 'newsletter-custom/themes/' . substr($email->theme, 1) . '/style.css';
else $file = 'newsletter/themes/' . $email->theme . '/style.css';
$css = @file_get_contents(ABSPATH . 'wp-content/plugins/' . $file);
$m = '<html><head><style type="text/css">' . $css .
'</style></head><body>' . $m . '</body></html>';
$s = $this->replace($email->subject, $user);
$x = $this->mail($user->email, $s, $m, true, $headers, 2);
$wpdb->query("update " . $wpdb->prefix . "newsletter_emails set sent=sent+1, last_id=" . abs($user->id) . " where id=" . $email->id);
$this->email_limit--;
if ($this->limits_exceeded()) return false;
}
return true;
}
function feed_send($email, $users = null) {
}
function followup_send($users = null, $options=null) {
}
function execute($text, $user=null) {
global $wpdb;
ob_start();
$r = eval('?' . '>' . $text);
if ($r === false) {
$this->error = 'Error while executing a PHP expression in a message body. See log file.';
$this->log('Error on execution of ' . $text, 1);
ob_end_clean();
return false;
}
return ob_get_clean();
}
/**
* Return true if the execution timeout is reached or the maximum number of email
* has been sent.
*/
function limits_exceeded() {
if (time() > $this->time_limit) {
$this->log('Timeout', 2);
return true;
}
if ($this->email_limit < 0) {
$this->log('Email limit reached', 3);
return true;
}
return false;
}
function mail($to, $subject, $message, $html=true, $headers=null, $priority=0) {
$this->log('To: ' . $to, 3);
$this->log('Subject: ' . $subject, 3);
if (empty($subject)) {
$this->log('Subject empty, skipped', 3);
return true;
}
if (!empty($this->options_main['receiver'])) $to = $this->options_main['receiver'];
$h = '';
if (!empty($headers)) {
foreach ($headers as $key => $value)
$h .= $key . ': ' . $value . "\n";
}
$h .= "From: " . $this->options_main['sender_name'] . " <" . $this->options_main['sender_email'] . ">\n";
if (!empty($this->options_main['reply_to'])) {
$h .= "Reply-To: " . $this->options_main['reply_to'] . "\n";
}
if (!empty($this->options_main['return_path'])) {
$h .= "Return-Path: " . $this->options_main['return_path'] . "\n";
}
$h .= "X-MailerPriority: " . $priority . "\n";
$h .= "X-MailerGroup: newsletter\n";
$h .= "MIME-Version: 1.0\n";
if ($html) $h .= "Content-type: text/html; charset=UTF-8\n";
else $h .= "Content-type: text/plain; charset=UTF-8\n";
$this->log('Headers: ' . $h, 3);
$r = wp_mail($to, $subject, $message, $h);
$this->log($r ? 'Mail sent' : 'Mail sending failed', 1);
return $r;
}
function mailer_init() {
}
function relink($text, $email_id, $user_id) {
return $text;
}
function relink_callback($matches) {
}
/**
* Levels are: 1 for errors, 2 for normal activity, 3 for debug.
*/
function log($text='', $level=2) {
if ((int) $this->options_main['logs'] < $level) return;
//$db = debug_backtrace(false);
$time = date('d-m-Y H:i:s ');
switch ($level) {
case 1: $time .= '- ERROR';
break;
case 2: $time .= '- INFO ';
break;
case 3: $time .= '- DEBUG';
break;
}
if (is_array($text) || is_object($text)) $text = print_r($text, true);
file_put_contents(dirname(__FILE__) . '/log.txt', $time . ' - ' . $text . "\n", FILE_APPEND | FILE_TEXT);
}
function hook_activate() {
global $wpdb;
include 'plugin-activate.inc.php';
}
function hook_deactivate() {
wp_clear_scheduled_hook('newsletter');
}
function hook_cron_schedules($schedules) {
$schedules['newsletter'] = array(
'interval' => 300, // seconds
'display' => 'Newsletter'
);
return $schedules;
}
/**
* Generate the profile editing form.
*/
function profile_form($user) {
$options = get_option('newsletter_profile');
$buffer .= '<div class="newsletter newsletter-profile">';
$buffer .= '<form action="' . $this->options_main['url'] . '" method="post"><input type="hidden" name="na" value="ps"/>';
$buffer .= '<input type="hidden" name="ni" value="' . $user->id . '"/>';
$buffer .= '<input type="hidden" name="nt" value="' . $user->token . '"/>';
$buffer .= '<table>';
$buffer .= '<tr><th align="right">' . $options['email'] . '</th><td><input type="text" size="30" name="ne" value="' . htmlspecialchars($user->email) . '"/></td></tr>';
if ($options['name_status'] >= 1) {
$buffer .= '<tr><th align="right">' . $options['name'] . '</th><td><input type="text" size="30" name="nn" value="' . htmlspecialchars($user->name) . '"/></td></tr>';
}
if ($options['surname_status'] >= 1) {
$buffer .= '<tr><th align="right">' . $options['surname'] . '</th><td><input type="text" size="30" name="ns" value="' . htmlspecialchars($user->surname) . '"/></td></tr>';
}
if ($options['sex_status'] >= 1) {
$buffer .= '<tr><th align="right">' . $options['sex'] . '</th><td><select name="nx" class="newsletter-sex">';
// if (!empty($options['sex_none'])) {
// $buffer .= '<option value="n"' . ($user->sex == 'n' ? ' selected' : '') . '>' . $options['sex_none'] . '</option>';
// }
$buffer .= '<option value="f"' . ($user->sex == 'f' ? ' selected' : '') . '>' . $options['sex_female'] . '</option>';
$buffer .= '<option value="m"' . ($user->sex == 'm' ? ' selected' : '') . '>' . $options['sex_male'] . '</option>';
$buffer .= '</select></td></tr>';
}
// Profile
for ($i = 1; $i <= 19; $i++) {
if ($options['profile_' . $i . '_status'] == 0) continue;
$buffer .= '<tr><th align="right">' . $options['profile_' . $i] . '</th><td>';
//if ($options['list_type_' . $i] != 'public') continue;
$field = 'profile_' . $i;
if ($options['profile_' . $i . '_type'] == 'text') {
$buffer .= '<input type="text" size="40" name="np' . $i . '" value="' . htmlspecialchars($user->$field) . '"/>';
}
if ($options['profile_' . $i . '_type'] == 'select') {
$buffer .= '<select name="np' . $i . '">';
$opts = explode(',', $options['profile_' . $i . '_options']);
for ($j = 0; $j < count($opts); $j++) {
$opts[$j] = trim($opts[$j]);
$buffer .= '<option';
if ($opts[$j] == $user->$field) $buffer .= ' selected';
$buffer .= '>' . $opts[$j] . '</option>';
}
$buffer .= '</select>';
}
$buffer .= '</td></tr>';
}
// Lists
$buffer .= '<tr><th> </th><td>';
for ($i = 1; $i <= 9; $i++) {
if ($options['list_' . $i . '_status'] == 0) continue;
$buffer .= '<input type="checkbox" name="nl[]" value="' . $i . '"';
$list = 'list_' . $i;
if ($user->$list == 1) $buffer .= ' checked';
$buffer .= '/> ' . htmlspecialchars($options['list_' . $i]) . '<br />';
}
$buffer .= '</td></tr>';
$buffer .= '<tr><td colspan="2" align="center"><input type="submit" value="' . $options['save'] . '"/></td></tr>';
$buffer .= '</table></form></div>';
return $buffer;
}
function subscription_form() {
$options_profile = get_option('newsletter_profile');
$options = get_option('newsletter');
$buffer = '<div class="newsletter newsletter-subscription"><form method="post" action="' . $this->options_main['url'] . '" onsubmit="return newsletter_check(this)"><input type="hidden" name="na" value="s"/>';
$buffer .= '<table>';
if ($options_profile['name_status'] == 2) {
$buffer .= '<tr><th>' . $options_profile['name'] . '</th><td><input type="text" name="nn" size="30"/></td></tr>';
}
if ($options_profile['surname_status'] == 2) {
$buffer .= '<tr><th>' . $options_profile['surname'] . '</th><td><input type="text" name="ns" size="30"/></td></tr>';
}
$buffer .= '<tr><th>' . $options_profile['email'] . '</th><td align="left"><input type="text" name="ne" size="30"/></td></tr>';
if ($options_profile['sex_status'] == 2) {
$buffer .= '<tr><th>' . $options_profile['sex'] . '</th><td><select name="nx" class="newsletter-sex">';
$buffer .= '<option value="m">' . $options_profile['sex_male'] . '</option>';
$buffer .= '<option value="f">' . $options_profile['sex_female'] . '</option>';
$buffer .= '</select></td></tr>';
}
$lists = '';
for ($i = 1; $i <= 40; $i++) {
if ($options_profile['list_' . $i . '_status'] != 2) continue;
$lists .= '<input type="checkbox" name="nl[]" value="' . $i . '"/> ' . $options_profile['list_' . $i] . '<br />';
}
if (!empty($lists)) $buffer .= '<tr><th> </th><td>' . $lists . '</td></tr>';
// Extra profile fields
for ($i = 1; $i <= 19; $i++) {
if ($options_profile['profile_' . $i . '_status'] != 2) continue;
if ($options_profile['profile_' . $i . '_type'] == 'text') {
$buffer .= '<tr><th>' . $options_profile['profile_' . $i] . '</th><td><input type="text" size="30" name="np' . $i . '"/></td></tr>';
}
if ($options_profile['profile_' . $i . '_type'] == 'select') {
$buffer .= '<tr><th>' . $options_profile['profile_' . $i] . '</th><td><select name="np' . $i . '">';
$opts = explode(',', $options_profile['profile_' . $i . '_options']);
for ($j = 0; $j < count($opts); $j++) {
$buffer .= '<option>' . trim($opts[$j]) . '</option>';
}
$buffer .= '</select></td></tr>';
}
}
$buffer .= '<tr><td colspan="2" style="text-align: center">';
if ($options_profile['privacy_status'] == 1) {
$buffer .= '<input type="checkbox" name="ny"/> ' . $options_profile['privacy'] . '<br />';
}
if (strpos($options_profile['subscribe'], 'http://') !== false) {
$buffer .= '<input type="image" src="' . $options_profile['subscribe'] . '"/></td></tr>';
} else {
$buffer .= '<input type="submit" value="' . $options_profile['subscribe'] . '"/></td></tr>';
}
$buffer .= '</table></form></div>';
return $buffer;
}
function mailer_bounce_email($email) {
}
function hook_template_redirect() {
if (!empty($this->message) && empty($this->options_main['url'])) {
if ($this->options_main['theme'][0] == '*') $file = ABSPATH . 'wp-content/plugins/newsletter-custom/themes-page/' . substr($this->options_main['theme'], 1) . '/theme.php';
else $file = dirname(__FILE__) . '/themes-page/' . $this->options_main['theme'] . '/theme.php';
// Include the labels, language dependend
@include(dirname($file) . '/en_US.php');
if (defined('WPLANG') && WPLANG != '') @include(dirname($file) . '/' . WPLANG . '.php');
ob_start();
@include($file);
$m = ob_get_contents();
ob_end_clean();
echo $this->execute(str_replace('{message}', $this->message, $m));
die();
}
}
function shortcode_newsletter() {
if (!empty($this->message)) return $this->message;
$options = get_option('newsletter');
// If there is an "embedded" form, adjust it and ignore the standard subscription form
if (stripos($options['subscription_text'], '<form') !== false) {
$buffer = str_ireplace('<form', '<form method="post" action="' . $this->options_main['url'] . '" onsubmit="return newsletter_check(this)"', $options['subscription_text']);
return str_ireplace('</form>', '<input type="hidden" name="na" value="s"/></form>', $buffer);
}
if (strpos($buffer, '{subscription_form}') !== false) return str_replace('{subscription_form}', $this->subscription_form(), $options['subscription_text']);
else return $options['subscription_text'] . $this->subscription_form();
return $buffer;
}
function shortcode_newsletter_form($attrs, $content) {
return $this->form($attrs['form']);
}
function form($number=null) {
if ($number == null) return $this->subscription_form();
$options = get_option('newsletter_forms');
$options_profile = get_option('newsletter_profile');
$form = str_replace('{newsletter_url}', $this->options_main['url'], $options['form_' . $number]);
$lists = '';
for ($i = 1; $i <= 9; $i++) {
if ($options_profile['list_' . $i . '_status'] != 2) continue;
$lists .= '<input type="checkbox" name="nl[]" value="' . $i . '"/> ' . $options_profile['list_' . $i] . '<br />';
}
$form = str_replace('{lists}', $lists, $form);
return $form;
}
function hook_init() {
global $cache_stop, $hyper_cache_stop, $wpdb;
$action = $_REQUEST['na'];
if (empty($action) || is_admin()) return;
$hyper_cache_stop = true;
$cache_stop = true;
$this->log($action);
$options = get_option('newsletter', array()); // Subscription options, emails and texts
// Subscription request from a subscription form (in page or widget), can be
// a direct subscription with no confirmation
if ($action == 's') {
$email = $this->normalize_email(stripslashes($_REQUEST['ne']));
if ($email == null) die('Wrong email');
$user = $wpdb->get_row($wpdb->prepare("select * from " . $wpdb->prefix . "newsletter where email=%s", $email));
if ($user == null) {
$this->log("not found");
$user = array('email' => $email);
$user['name'] = $this->normalize_name(stripslashes($_REQUEST['nn']));
$user['surname'] = $this->normalize_name(stripslashes($_REQUEST['ns']));
if (!empty($_REQUEST['nx'])) $user['sex'] = $_REQUEST['nx'][0];
$user['referrer'] = $_REQUEST['nr'];
$options_profile = get_option('newsletter_profile');
// New profiles
for ($i = 1; $i <= 19; $i++) {
if ($options_profile['profile_' . $i . '_status'] == 0) continue;
$user['profile_' . $i] = trim(stripslashes($_REQUEST['np' . $i]));
}
// Lists (field names are nl[] and values the list number so special forms with radio button can work)
if (is_array($_REQUEST['nl'])) {
for ($i = 1; $i <= 9; $i++) {
if ($options_profile['list_' . $i . '_status'] != 2) continue;
if (in_array($i, $_REQUEST['nl'])) $user['list_' . $i] = 1;
}
}
$user['token'] = md5(rand());
$user['ip'] = $_SERVER['REMOTE_ADDR'];
$user['status'] = $options['noconfirmation'] == 1 ? 'C' : 'S';
$wpdb->insert($wpdb->prefix . 'newsletter', $user);
$user = $this->get_user($wpdb->insert_id); // back to an object
// Notification to admin (only for new subscriptions)
if ($user->status == 'C') {
$this->notify_admin($user, 'Newsletter subscription');
}
}
$this->log($user);
$prefix = ($user->status == 'C') ? 'confirmed_' : 'confirmation_';
$message = $options[$prefix . 'message'];
$subject = $options[$prefix . 'subject'];
//setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
$this->mail($user->email, $this->replace($subject, $user), $this->replace($message, $user));
if ($user->status == 'C') {
if (!empty($options['confirmed_url'])) {
header('Location: ' . $options['confirmed_url']);
die();
}
$this->message = $this->replace($options['confirmed_text'], $user);
}
else $this->message = $this->replace($options['subscribed_text'], $user);
return;
}
// Actions below need a user. This code loads the user checking parameter or cookies.
$user = $this->check_user();
if ($user == null) die('No user');
if ($action == 'c') {
setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
if (!empty($options['confirmed_url'])) {
header('Location: ' . $options['confirmed_url']);
die();
}
$this->message = $this->replace($options['confirmed_text'], $user);
return;
}
// Unsubscription request
if ($action == 'u') {
$this->message = $this->replace($options['unsubscription_text'], $user);
return;
}
// Unsubscription confirm
if ($action == 'uc') {
setcookie("newsletter", "", time() - 3600);
$this->message = $this->replace($options['unsubscribed_text'], $user);
return;
}
// Profile saving
if ($action == 'ps') {
$options_profile = get_option('newsletter_profile', array());
// Javascript checked
if (!$this->is_email($_REQUEST['ne'])) die('Wrong email address.');
// General data
$data['email'] = $this->normalize_email(stripslashes($_REQUEST['ne']));
$data['name'] = $this->normalize_name(stripslashes($_REQUEST['nn']));
$data['surname'] = $this->normalize_name(stripslashes($_REQUEST['ns']));
if ($options_profile['sex_status'] >= 1) {
$data['sex'] = $_REQUEST['nx'][0];
// Wrong data injection check
if ($data['sex'] != 'm' && $data['sex'] != 'f') die('Wrong sex field');
}
// Lists
if (is_array($_REQUEST['nl'])) {
for ($i = 1; $i <= 9; $i++) {
if ($options_profile['list_' . $i . '_status'] == 0) continue;
$data['list_' . $i] = in_array($i, $_REQUEST['nl']) ? 1 : 0;
}
}
// Profile
for ($i = 1; $i <= 19; $i++) {
if ($options_profile['profile_' . $i . '_status'] == 0) continue;
$data['profile_' . $i] = stripslashes($_REQUEST['np' . $i]);
}
$wpdb->update($wpdb->prefix . 'newsletter', $data, array('id' => $user->id));
$url = empty($this->options_main['url']) ? get_option('home') : $this->options_main['url'];
header('Location: ' . $url . '/?na=pe&ni=' . $user->id . '&nt=' . $user->token);
die();
}
// Profile editing.
if ($action == 'pe') {
$options_profile = get_option('newsletter_profile');
if (empty($options_profile['profile_text'])) $options_profile['profile_text'] = '{profile_form}';
$this->message = $this->replace($options_profile['profile_text'], $user);
return;
}
if ($action == 'm') {
if ($user->status == 'C') setcookie('newsletter', $user->id . '-' . $user->token, time() + 60 * 60 * 24 * 365, '/');
header('Location: ' . $this->options_main['lock_url']);
die();
}
}
function set_limits() {
global $wpdb;
$wpdb->query("set session wait_timeout=300");
// From default-constants.php
if (function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < 128 )) @ini_set('memory_limit', '128M');
}
function hook_admin_init() {
global $wpdb;
if ($_REQUEST['act'] == 'export' && check_admin_referer()) {
include 'plugin-export.inc.php';
}
}
/**
* Return a user if there are request parameters or cookie with identification data otherwise null.
*/
function check_user() {
global $wpdb;
$id = (int) $_REQUEST['ni'];
$token = $_REQUEST['nt'];
if (empty($id) || empty($token)) list ($id, $token) = @explode('-', $_COOKIE['newsletter'], 2);
return $wpdb->get_row($wpdb->prepare("select * from " . $wpdb->prefix . "newsletter where id=%d and token=%s", $id, $token));
}
function get_user($id) {
global $wpdb;
return $wpdb->get_row($wpdb->prepare("select * from " . $wpdb->prefix . "newsletter where id=%d", $id));
}
/**
* Replace any kind of newsletter placeholder in a text.
*/
function replace($text, $user=null) {
global $wpdb;
if (is_array($user)) $user = $this->get_user($user['id']);
$text = str_replace('{home_url}', get_option('home'), $text);
$text = str_replace('{blog_title}', get_option('blogname'), $text);
$text = str_replace('{date}', date_i18n(get_option('date_format')), $text);
$text = str_replace('{blog_description}', get_option('blogdescription'), $text);
// Date processing
$x = 0;
while (($x = strpos($text, '{date_', $x)) !== false) {
$y = strpos($text, '}', $x);
if ($y === false) continue;
$f = substr($text, $x+6, $y-$x-6);
$text = substr($text, 0, $x) . date($f) . substr($text, $y+1);
}
if ($user != null) {
$text = str_replace('{email}', $user->email, $text);
$text = str_replace('{name}', $user->name, $text);
$text = str_replace('{surname}', $user->surname, $text);
$text = str_replace('{token}', $user->token, $text);
$text = str_replace('%7Btoken%7D', $user->token, $text);
$text = str_replace('{id}', $user->id, $text);
$text = str_replace('%7Bid%7D', $user->id, $text);
$text = str_replace('{ip}', $user->ip, $text);
if (strpos($text, '{profile_form}') !== false) $text = str_replace('{profile_form}', $this->profile_form($user), $text);
for ($i = 1; $i < 20; $i++) {
$p = 'profile_' . $i;
$text = str_replace('{profile_' . $i . '}', $user->$p, $text);
}
$profile = $wpdb->get_results("select name,value from " . $wpdb->prefix . "newsletter_profiles where newsletter_id=" . $user->id);
foreach ($profile as $field) {
$text = str_ireplace('{np_' . $field->name . '}', htmlspecialchars($field->value), $text);
}
$text = preg_replace('/\\{np_.+\}/i', '', $text);
$base = $this->options_main['url'];
if ($base == '') $base = get_option('home');
$id_token = '&ni=' . $user->id . '&nt=' . $user->token;
$text = $this->replace_url($text, 'SUBSCRIPTION_CONFIRM_URL', $this->add_qs(plugins_url('do.php', __FILE__), 'a=c' . $id_token));
$text = $this->replace_url($text, 'UNSUBSCRIPTION_CONFIRM_URL', $this->add_qs(plugins_url('do.php', __FILE__), 'a=uc' . $id_token));
$text = $this->replace_url($text, 'UNSUBSCRIPTION_URL', $this->add_qs($base, 'na=u' . $id_token));
$text = $this->replace_url($text, 'PROFILE_URL', $this->add_qs($base, 'na=pe' . $id_token));
$text = $this->replace_url($text, 'UNLOCK_URL', $this->add_qs($this->options_main['url'], 'na=m' . $id_token));
for ($i = 1; $i <= 9; $i++) {
$text = $this->replace_url($text, 'LIST_' . $i . '_SUBSCRIPTION_URL', $this->add_qs($base, 'na=ls&nl=' . $i . $id_token));
$text = $this->replace_url($text, 'LIST_' . $i . '_UNSUBSCRIPTION_URL', $this->add_qs($base, 'na=lu&nl=' . $i . $id_token));
}
}
return $text;
}
function replace_url($text, $tag, $url) {
$home = get_option('home') . '/';
$tag_lower = strtolower($tag);
$text = str_replace($home . '{' . $tag_lower . '}', $url, $text);
$text = str_replace($home . '%7B' . $tag_lower . '%7D', $url, $text);
$text = str_replace('{' . $tag_lower . '}', $url, $text);
// for compatibility
$text = str_replace($home . $tag, $url, $text);
return $text;
}
function add_qs($url, $qs, $amp=true) {
if (strpos($url, '?') !== false) {
if ($amp) return $url . '&' . $qs;
else return $url . '&' . $qs;
}
else return $url . '?' . $qs;
}
function post_is_old() {
}
function hook_shutdown() {
if ($this->mailer != null) $this->mailer->SmtpClose();
}
function shortcode_newsletter_lock($attrs, $content=null) {
global $hyper_cache_stop, $lite_cache_stop;
$hyper_cache_stop = true;
$lite_cache_stop = true;
$user = $this->check_user();
if ($user != null && $user->status == 'C') {
return do_shortcode($content);
}
$buffer = $this->options_main['lock_message'];
ob_start();
eval('?>' . $buffer . "\n");
$buffer = ob_get_clean();
$buffer = str_ireplace('<form', '<form method="post" action="' . $this->options_main['url'] . '"', $buffer);
$buffer = str_ireplace('</form>', '<input type="hidden" name="na" value="s"/></form>', $buffer);
return do_shortcode($buffer);
}
function normalize_email($email) {
$email = strtolower(trim($email));
if (!is_email($email)) return null;
return $email;
}
function normalize_name($name) {
$name = str_replace(';', ' ', $name);
$name = strip_tags($name);
return $name;
}
function is_email($email, $empty_ok=false) {
$email = strtolower(trim($email));
if ($empty_ok && $email == '') return true;
if (!is_email($email)) return false;
if (strpos($email, 'mailinator.com') !== false) return false;
if (strpos($email, 'guerrillamailblock.com') !== false) return false;
if (strpos($email, 'emailtemporanea.net') !== false) return false;
return true;
}
function m2t($s) {
$s = explode(' ', $s);
$d = explode('-', $s[0]);
$t = explode(':', $s[1]);
return gmmktime((int) $t[0], (int) $t[1], (int) $t[2], (int) $d[1], (int) $d[2], (int) $d[0]);
}
function query($query) {
global $wpdb;
$this->log($query, 3);
return $wpdb->query($query);
}
function notify_admin($user, $subject) {
if ($this->options_main['notify'] != 1) return;
$message = "Subscriber details:\n\n" .
"email: " . $user->email . "\n" .
"first name: " . $user->name . "\n" .
"last name: " . $user->surname . "\n" .
"gender: " . $user->sex . "\n";
$options_profile = get_option('newsletter_profile');
for ($i=0; $i<NEWSLETTER_PROFILE_MAX; $i++) {
if ($options_profile['profile_' . $i] == '') continue;
$field = 'profile_' . $i;
$message .= $options_profile['profile_' . $i] . ': ' . $user->$field . "\n";
}
$message .= "token: " . $user->token . "\n" .
"status: " . $user->status . "\n" .
"\nYours, Newsletter Pro.";
wp_mail(get_option('admin_email'), '[' . get_option('blogname') . '] ' . $subject, $message, "Content-type: text/plain; charset=UTF-8\n");
}
}
require_once(dirname(__FILE__) . '/widget.php');
/**
* Find an image for a post checking the media uploaded for the post and
* choosing the first image found.
*/
function nt_post_image($post_id, $size='thumbnail', $alternative=null) {
$attachments = get_children(array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID'));
if (empty($attachments)) {
return $alternative;
}
foreach ($attachments as $id => $attachment) {
$image = wp_get_attachment_image_src($id, $size);
return $image[0];
}
return null;
}
function nt_option($name, $def = null) {
$options = get_option('newsletter_email');
$option = $options['theme_' . $name];
if (!isset($option)) return $def;
else return $option;
}
// For compatibility
function newsletter_form($number=null) {
global $newsletter;
echo $newsletter->form($number);
}
// For compatibility
function newsletter_embed_form($number=null) {
global $newsletter;
echo $newsletter->form($number);
}