-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscribers.php
2070 lines (1849 loc) · 87.1 KB
/
subscribers.php
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Module: subscribers class
Version: 1.2
Author: Richard Catto
Creation Date: 2009-07-25
Milestones:
Description:
This class handles all the subscriber related actions.
start must be instantiated before this class is instantiated in order to open the mysql database
*/
class subscribers {
protected $fat;
protected $user;
protected $dbconn;
protected $dbPDO;
protected $globaldb;
protected $BaseURL;
protected $API;
protected $Domain;
protected $ListName;
protected $FromAddress;
protected $SubscriptionConfirmAmount;
protected $SubscriptionConfirmLevel;
// subscriber variables - must be public for forms
public $VALIDATE_EMAIL = true;
public $sock;
public $reply;
public $save_error; // text string of error encountered when saving a subscriber
public $save_result; // text string of result of save
public $create_error;
public $update_error;
public $sid;
public $s_u_id; // profile user id
public $suid;
public $semail;
public $semailuser;
public $semaildomain;
public $spriority;
public $ssubscribedate;
public $sconfirm;
public $sconfirmdate;
public $sunsubscribe;
public $sunsubscribedate = "0000-00-00 00:00:00";
public $sunsubscribereason;
public $sunsubscribebounce;
public $sunsubscribeadmin;
public $sbounces;
public $semailsleft;
public $sfname;
public $slname;
public $sprovince;
public $scountry;
public $sgender;
public $sglobalunsubscribe;
// from regular-expressions.info to find a valid email address
public $find_email_preg = "/\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6})\b/i";
// $dbconn is a mysqli connection object to an open MySQL database
function __construct($fat) {
$this->fat = $fat;
$this->user = $fat->get('user');
$this->dbconn = $fat->get('dbconn');
$this->dbPDO = $fat->get('dbPDO');
$this->BaseURL = $fat->get('BaseURL');
$this->API = $fat->get('API');
$this->Domain = $fat->get('Domain');
$this->ListName = $fat->get('ListName');
$this->FromAddress = $fat->get('FromAddress');
$this->SubscriptionConfirmAmount = $fat->get('SubscriptionConfirmAmount');
$this->SubscriptionConfirmLevel = $fat->get('SubscriptionConfirmLevel');
$gdbhost = $fat->get('gdbhost');
$gdbuser = $fat->get('gdbuser');
$gdbpass = $fat->get('gdbpass');
$gdbname = $fat->get('gdbname');
// Connect to globalfilter database to globally unsubscribe subscribers
$this->globaldb = new mysqli($gdbhost,$gdbuser,$gdbpass,$gdbname);
if ($this->globaldb->connect_error) {
die('Connect Error (' . $this->globaldb->connect_errno . ') ' . $this->globaldb->connect_error);
}
$this->Reset();
}
function __destruct() {
$this->globaldb->close();
}
// temp function - splits subscriber email into user and domain
public function GUFix() {
// $sql1 = "SELECT gu_id, gu_email FROM globalunsubscribe WHERE gu_email_user = \"\" ORDER BY gu_email";
$sql1 = "SELECT s_id, s_email FROM subscribers WHERE s_email_user = \"\" ORDER BY s_email";
$result = $this->dbconn->query($sql1);
$num_rows = $result->num_rows;
// echo "<p>num_rows: $num_rows</p>";
while ($row = $result->fetch_array()) {
$sid = $row['s_id'];
$semail = $row['s_email'];
// echo "<p>gu_id: $guid</p>";
// echo "<p>gu_email: $guemail</p>";
$email_user = $this->GetEmailUser($semail);
// echo "<p>email_user: $email_user</p>";
$email_domain = $this->GetEmailDomain($semail);
// echo "<p>email_domain: $email_domain</p>";
// $sql2 = "update globalunsubscribe set gu_email_user = \"$email_user\", gu_email_domain = \"$email_domain\" where gu_id = \"$guid\"";
$sql2 = "update subscribers set s_email_user = \"$email_user\", s_email_domain = \"$email_domain\" where s_id = \"$sid\"";
$success = $this->dbconn->query($sql2);
set_time_limit(0);
}
$result->close();
}
/*
`gu_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`gu_email` varchar(100) NOT NULL,
`gu_type` enum('BOUNCE','BOUNCE-ADMIN','INVALID','SPAM','SPAM-ADMIN','USER') NOT NULL,
`gu_active` tinyint(1) NOT NULL DEFAULT '1',
*/
public function CheckGU() {
set_time_limit(0);
$invalidchanged = 0;
$fp1 = fopen("checkgu.txt", "a");
$sql1 = "SELECT gu_id, gu_email, gu_type FROM globalunsubscribe WHERE gu_active = \"1\" ORDER BY gu_email";
$result = $this->globaldb->query($sql1);
$num_rows = $result->num_rows;
while ($row = $result->fetch_array()) {
$guid = $row['gu_id'];
$guemail = $row['gu_email'];
$gutype = $row['gu_type'];
$cansendto = $this->FixEmail($guemail);
// change email status to inactive since it can be sent to
if ($cansendto) {
if (($gutype == "BOUNCE") || ($gutype == "BOUNCE-ADMIN") || ($gutype == "INVALID")) {
$sql1 = "update globalsubscribe set gu_email = \"$this-semail\", gu_active = \"0\" WHERE gu_id = \"$guid\"";
$success = $this->globaldb->query($sql1);
if ($success) {
$bdate = date("Y-m-d H:i:s");
fwrite($fp1, $bdate . "$gu_type $guemail -> $this-semail INACTIVE\n" );
$invalidchanged++;
}
}
}
set_time_limit(0);
}
$result->close();
fwrite($fp1, "updated: $invalidchanged\n");
fclose($fp1);
}
// function to recheck that all active non-existent domains are actually still non-existent
public function CheckGDU() {
set_time_limit(0);
$fp1 = fopen("checkgdu.txt", "a");
$bdate = date("Y-m-d H:i:s");
fwrite($fp1, "--------------\n");
fwrite($fp1, $bdate . "\n");
fwrite($fp1, "--------------\n");
// $sql = "SELECT gdu_id, gdu_domain_name, gdu_type FROM globaldomainunsubscribe WHERE (gdu_type = 'NOTEXIST') and (gdu_active = '1') ORDER BY gdu_domain_name";
$sql = "SELECT gdu_id, gdu_domain_name, gdu_type FROM globaldomainunsubscribe WHERE (gdu_type = 'NOTEXIST') and (gdu_active = '1') ORDER BY gdu_id DESC";
$result = $this->globaldb->query($sql);
$num_rows = $result->num_rows;
fwrite($fp1, "Domains to process: $num_rows\n");
$notexistchanged = 0;
while ($row = $result->fetch_array()) {
$gduid = $row['gdu_id'];
$gdudomainname = $row['gdu_domain_name'];
$gdutype = $row['gdu_type'];
// Check that MX records exist for the domain;
$fqdn = $gdudomainname . '.';
$cansendto = checkdnsrr($fqdn);
fwrite($fp1, "$gdudomainname " . ($cansendto ? "YES - " : "NO - "));
// change domain status to inactive since it now exists
if ($cansendto) {
$sql1 = "update globaldomainunsubscribe set gdu_active = \"0\" WHERE gdu_id = \"$gduid\"";
$success = $this->globaldb->query($sql1);
if ($success) {
fwrite($fp1, "MARKED INACTIVE");
$notexistchanged++;
} else {
fwrite($fp1, "FAILED TO MARK INACTIVE");
}
} else {
fwrite($fp1, "NOTEXIST --> NO CHANGE");
}
fwrite($fp1, "\n");
set_time_limit(0);
}
$result->close();
fwrite($fp1, "not exist updated: $notexistchanged\n\n");
fclose($fp1);
}
// UPDATE subscribers SET s_unsubscribe = "1", s_unsubscribedate = NOW( ), s_unsubscribereason = "low priority", s_unsubscribe_admin = "1" WHERE s_priority < "45"
public function PrioritiseSubscribers($priority = 45) {
echo "<p>Prioritising subscribers</p>";
$total = 0;
$sql_update[0] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '\.za$') and (s_priority < \"$priority\")";
$sql_update[1] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '\.za\.net$') and (s_priority < \"$priority\")";
$sql_update[2] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@za\.') and (s_priority < \"$priority\")";
$sql_update[3] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@gmail\.com$') and (s_priority < \"$priority\")";
$sql_update[4] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@telkomsa\.net$') and (s_priority < \"$priority\")";
$sql_update[5] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@iafrica\.com$') and (s_priority < \"$priority\")";
$sql_update[6] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@mweb\.com$') and (s_priority < \"$priority\")";
$sql_update[7] = "update subscribers set s_priority = \"$priority\" where (s_email regexp '@lantic\.net$') and (s_priority < \"$priority\")";
$sql_update[8] = "update subscribers set s_unsubscribe = \"1\", s_unsubscribedate = now(), s_unsubscribereason = \"low priority\", s_unsubscribe_admin = \"1\" WHERE s_priority < \"$priority\"";
foreach ($sql_update as $sql) {
set_time_limit(0);
$success = $this->dbconn->query($sql);
$num_affected = $this->dbconn->affected_rows;
$total += $num_affected;
echo "<p>$sql $num_affected</p>";
}
echo "<p>Total prioritised: $total</p>";
}
public function SyncSubscribers() {
set_time_limit(0);
// $db_server = $this->fat->get('dbservers');
$dbservers = $this->fat->get('dbservers');
// $num_db_servers = count($db_server);
$num_db_servers = count($dbservers);
$total_added = 0;
$this->VALIDATE_EMAIL = false;
// echo "<p>DB servers: $num_db_servers</p>";
$fp1 = fopen("sync.txt", "a");
$bdate = date("Y-m-d H:i:s");
fwrite($fp1, "--------------\n");
fwrite($fp1, $bdate . "\n");
fwrite($fp1, "--------------\n");
// for ($i = 0; $i < $num_db_servers; $i++) {
foreach ($dbservers as $db_server) {
// if (($db_server[$i]['active'] == 1) && ($this->Domain <> $db_server[$i]['domain'])) {
if (($db_server['active'] == 1) && ($this->Domain <> $db_server['domain'])) {
// $domain = $db_server[$i]['domain'];
$domain = $db_server['domain'];
fwrite($fp1, "Domain: $domain\n");
$domain_total = 0;
// connect to database
// $mysqli = new mysqli($db_server[$i]['host'],$db_server[$i]['user'],$db_server[$i]['pass'],$db_server[$i]['name']);
$mysqli = new mysqli($db_server['host'],$db_server['user'],$db_server['pass'],$db_server['name']);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
// $sql = "select s_email, s_priority from subscribers ORDER BY s_priority DESC, s_email ASC";
$sql = "select s_email, s_priority from subscribers WHERE s_priority > '0' ORDER BY s_priority DESC, s_email ASC";
$result = $mysqli->query($sql);
while ($row = $result->fetch_array()) {
set_time_limit(0);
$semail = trim(strtolower($row["s_email"]));
$spriority = $row["s_priority"];
// if ($semail == '') continue; // goes back to the top of the loop - does not add blank email addresses
$subscribed = $this->SimpleSubscribeAndConfirm($semail,$spriority);
if ($subscribed) {
fwrite($fp1, "$this->semail\n");
$total_added++;
$domain_total++;
}
set_time_limit(0);
}
$result->close();
$mysqli->close();
- fwrite($fp1, "Domain total subscribed: $domain_total\n");
}
}
$this->VALIDATE_EMAIL = true;
fwrite($fp1, "--------------\n");
- fwrite($fp1, "Total subscribed: $total_added\n");
fclose($fp1);
return $total_added;
}
// Each subscriber gets assigned a unique random md5 hash ID
// which is used to identify them in lieu of their email address for security purposes.
protected function CreateSUID() {
$suid = md5($this->semail);
$result = $this->dbconn->query("select * from subscribers where s_uniqid = \"$suid\"");
while ($result->num_rows > 0) {
$result->close();
$suid = md5($this->semail . uniqid('',true) . mt_rand());
$result = $this->dbconn->query("select * from subscribers where s_uniqid = \"$suid\"");
}
$result->close();
return $suid;
}
// Additional data to capture when subscribing:
// $_SERVER['HTTP_REFERER'],
// $_SERVER['HTTP_USER_AGENT'],
// $_SERVER['REMOTE_HOST'],
// $_SERVER['REMOTE_ADDR']
// Displays a paginated table of subscribers with related info in columns
// Credit: Tony Marston for his excellent tutorial on pagination
// http://www.tonymarston.net/php-mysql/pagination.html
public function CreateSubscribersHTMLList($ssemail,$pageno,$numrows) {
$html = "";
if ($this->user->uadmin == '1') {
$regularreaders = $this->NumberOfRegularReaders();
$html .= "<p>Regular readers: {$regularreaders}</p>";
// $html .= "<p><a href=\"{$this->BaseURL}check-subscribers\">Check Subscribers</a></p>";
// $html .= "<p><a href=\"{$this->BaseURL}prioritise-subscribers\">Prioritise Subscribers</a></p>";
$html .= "<form name=\"subscribersform\" action=\"{$this->BaseURL}subscribers\" method=\"get\" class=\"form-inline\" role=\"form\">";
$html .= "<fieldset><legend>Search by email address</legend>";
$html .= "<div class=\"form-group\"><div class=\"col-md-3\"><input class=\"form-control\" name=\"e\" type=\"search\" maxlength=\"255\" value=\"$ssemail\"></div></div>";
$html .= "<div class=\"form-group\"><button class=\"btn btn-primary\" type=\"submit\">Email Search</button></div>";
$html .= "</fieldset></form><hr>";
} else {
$html .= "<p>Access denied</p>";
return $html;
}
$sql = "select count(*) from subscribers where s_email like \"%$ssemail%\"";
$result = $this->dbconn->query($sql);
$row = $result->fetch_row();
$totalmatches = $row[0];
$result->close();
if ($totalmatches == 0) {
$html .= "<p>No subscribers match that search.</p>";
return $html;
}
$lastpage = ceil($totalmatches/$numrows);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} elseif ($pageno < 1) {
$pageno = 1;
}
$limit = "limit " . ($pageno - 1) * $numrows . ",$numrows";
$sql = "SELECT s_id, s_email, s_uniqid, s_subscribedate, s_unsubscribe, s_confirm, (select count(*) from smsent where sms_s_id = s_id) as s_messages, (select count(distinct smo_m_id) from smopens where smo_s_uniqid = s_uniqid) as s_opened, s_bounces, s_emailsleft, s_priority, (select concat_ws(' ',m_subject,smo_dateopened) from smopens join messages on (m_id = smo_m_id) where smo_s_uniqid = s_uniqid order by smo_m_id desc limit 1) as s_lastmsg FROM subscribers where s_email like \"%$ssemail%\" order by s_unsubscribe ASC, s_priority DESC, s_email ASC $limit";
$result = $this->dbconn->query($sql);
$html .= "<table class=\"table table-striped table-bordered\"><thead><tr><th>Email</th><th>Subscription Date</th><th>Unsubscribed?</th><th>Confirmed?</th><th>Msgs sent</th><th>Msgs Opened</th><th>Bounces</th><th>Priority</th><th>Emails left</th><th>Last Msg Opened</th></tr></thead><tbody>";
while ($row = $result->fetch_array()) {
$sid = $row["s_id"];
$suid = $row["s_uniqid"];
$semail = $row["s_email"];
$slemail = "<a href=\"{$this->BaseURL}subscribe/{$suid}\">{$semail}</a>";
$ssubscribedate = $row['s_subscribedate'];
$sunsubscribe = ($row["s_unsubscribe"] == "1" ? "Yes" : "<a href=\"{$this->BaseURL}unsubscribe/{$suid}\">unsubscribe</a>");
$sconfirm = ($row["s_confirm"] == "1" ? "Yes" : "<a href=\"{$this->BaseURL}confirm/{$suid}\">confirm</a>");
$smessages = $row["s_messages"]; // TODO make this clickable to allow the administrator to see which messages have been sent to this subscriber
$sopened = $row["s_opened"]; // TODO make this clickable to see which messages were opened (read)
$sbounces = $row["s_bounces"];
$spriority = $row["s_priority"];
$semailsleft = $row['s_emailsleft'];
$slastmsg = (is_null($row["s_lastmsg"]) ? " - " : $row["s_lastmsg"]);
$html .= "<tr><td>$slemail</td><td>$ssubscribedate</td><td>$sunsubscribe</td><td>$sconfirm</td><td>$smessages</td><td>$sopened</td><td>$sbounces</td><td>$spriority</td><td>$semailsleft</td><td>$slastmsg</td></tr>";
}
$html .= "</tbody></table>";
$result->close();
$html .= "<p>Page $pageno of $lastpage - $totalmatches records found</p>";
// add in navigation to browse large results lists.
$html .= "<ul class=\"pager\">";
$qsemail = urlencode($ssemail);
$querystring = ($ssemail == "") ? "" : "{$qsemail}";
if ($pageno == 1) {
$html .= "<li class=\"previous disabled\"><a href=\"#\">First</li>";
$html .= "<li class=\"previous disabled\"><a href=\"#\">Previous</li>";
} else {
$prevpage = $pageno - 1;
$html .= "<li class=\"previous\"><a href=\"{$this->BaseURL}subscribers/1/{$querystring}\">First</a></li>";
$html .= "<li class=\"previous\"><a href=\"{$this->BaseURL}subscribers/{$prevpage}/{$querystring}\">Previous</a></li>";
}
if ($pageno == $lastpage) {
$html .= "<li class=\"next disabled\"><a href=\"#\">Last</li>";
$html .= "<li class=\"next disabled\"><a href=\"#\">Next</li>";
} else {
$nextpage = $pageno + 1;
$html .= "<li class=\"next\"><a href=\"{$this->BaseURL}subscribers/{$lastpage}/{$querystring}\">Last</a></li>";
$html .= "<li class=\"next\"><a href=\"{$this->BaseURL}subscribers/{$nextpage}/{$querystring}\">Next</a></li>";
}
$html .= "</ul>";
return $html;
}
public function CreateSubscriberHTMLform($suid = "") {
$html = "";
if ($suid <> "") { // edit an existing subscriber
if (!$this->Read($suid)) { // subscriber does not exist
$html .= "<p>The subscriber does not exist.</p>";
return $html;
}
$email_disabled = " readonly"; // disable the input for email of existing subscriber
} else {
$email_disabled = " required"; // require email to be input because new subscriber
}
$hh = $this->fat->get('htmlhelper');
$html .= "<form name=\"subscribeform\" action=\"{$this->BaseURL}subscribe\" method=\"post\" class=\"form-horizontal\" role=\"form\">";
$html .= "<input name=\"suid\" type=\"hidden\" value=\"{$suid}\">";
$html .= "<input name=\"spriority\" type=\"hidden\" value=\"{$this->spriority}\">";
$html .= "<fieldset><legend>Subscriber Information</legend>";
$input_text = array(
0 => array("semail","Email","email",$this->semail,$email_disabled,true),
1 => array("spriority","Priority","number",$this->spriority,"",$this->user->uadmin == '1' ? true : false),
2 => array("sfname","First Name","text",$this->sfname,"",true),
3 => array("slname","Last Name","text",$this->slname,"",true)
);
foreach($input_text as $icontrol) {
if ($icontrol[5]) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$icontrol[0]}\">{$icontrol[1]}</label>";
$html .= "<div class=\"col-md-3\">";
$html .= "<input class=\"form-control\" type=\"{$icontrol[2]}\" name=\"{$icontrol[0]}\" value=\"{$icontrol[3]}\"{$icontrol[4]}>";
$html .= "</div></div>";
}
}
$select_text = array(
0 => array("sprovince","Province",$hh->CreateProvinceHTMLDropDown($this->sprovince)),
1 => array("scountry","Country",$hh->CreateCountryHTMLDropDown($this->scountry)),
2 => array("sgender","Gender",$hh->CreateGenderHTMLDropDown($this->sgender))
);
foreach($select_text as $scontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$scontrol[0]}\">{$scontrol[1]}</label>";
$html .= "<div class=\"col-md-3\">";
$html .= "<select class=\"form-control\" name=\"{$scontrol[0]}\">{$scontrol[2]}</select>";
$html .= "</div></div>";
}
if (!$this->user->uloggedin) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"captcha\">Captcha Code</label>";
$html .= "<div class=\"col-md-3\">";
$html .= "<img src=\"/captcha\" title=\"refresh page for new captcha image. does not timeout.\" alt=\"captcha\"/>";
$html .= "<input class=\"form-control\" type=\"text\" name=\"captcha\"/>";
$html .= "</div></div>";
}
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"col-md-offset-1 col-md-3\">";
$html .= "<button class=\"btn btn-primary\" type=\"submit\">Subscribe</button>";
$html .= "</div></div>";
$html .= "</fieldset></form>";
return $html;
}
// BulkSubscribe from a file
public function BulkSubscribe($fname,$spriority = 0) {
$fcontents = file($fname);
$this->VALIDATE_EMAIL = false;
$this->BulkSubscribeForm($fcontents,$spriority,false);
$this->VALIDATE_EMAIL = true;
}
// bulk subscribes emails entered into a textarea, each on a separate line
// uses regular expressions to extract email addresses from free form text
public function BulkSubscribeForm($bemail,$spriority = 0,$prt_debug = true) {
$html = "";
set_time_limit(0);
if (is_array($bemail)) {
$fcontents = $bemail;
} else {
$txt = preg_replace('/\r\n|\r/', "\n", $bemail);
$fcontents = explode("\n",trim($txt));
}
$num = 0;
// Open the file and append to end of file
$fp1 = fopen("emails_added.txt", "a");
$fp2 = fopen("emails_rejected.txt", "a");
// Write the data to the file
$bdate = date("Y-m-d H:i:s");
fwrite($fp1, "--------------\n");
fwrite($fp1, $bdate . "\n");
fwrite($fp2, "--------------\n");
fwrite($fp2, $bdate . "\n");
for($i = 0; $i < sizeof($fcontents); $i++) {
$semail = strtolower(trim($fcontents[$i]));
// Extract all the email addresses in $semail into an array
$EmailAddresses = $this->find_email_addresses($semail);
if (is_array($EmailAddresses)) {
foreach ($EmailAddresses as $email) {
$subscribed = $this->SimpleSubscribeAndConfirm($email,$spriority);
if ($subscribed) {
if ($prt_debug) $html .= "<p>$this->semail</p>";
fwrite($fp1, "$this->semail\n");
$num++;
} else {
if ($prt_debug) $html .= "<p>NS $semail --> $this->semail $this->reply</p>";
}
}
} else { // did not find at least one valid email address
fwrite($fp2, "$semail\n");
}
// script is allowed to run for 10 mins
set_time_limit(0);
}
if ($prt_debug) $html .= "<p>Number subscribed: $num</p>";
fwrite($fp1, "Number subscribed: $num\n");
fclose($fp1);
fclose($fp2);
return $html;
}
// bulk unsubscribes emails entered into a textarea, each on a separate line
public function BulkUnsubscribeForm($bemail,$bounce = "") {
$html = "";
set_time_limit(0);
if ($bounce == "b") {
$gutype = 'BOUNCE-ADMIN';
$html .= "<p>BOUNCE-ADMIN</p>";
$globalunsub = true;
} elseif ($bounce == "s") {
$gutype = 'SPAM-ADMIN';
$html .= "<p>SPAM-ADMIN</p>";
$globalunsub = true;
} elseif ($bounce == "d") {
$gutype = '';
$globalunsub = true;
} else {
$gutype = '';
$globalunsub = false;
}
$reason = "bulk unsubscribed";
$txt = preg_replace('/\r\n|\r/', "\n", $bemail);
$fcontents = explode("\n",trim($txt));
$num = 0;
for($i = 0; $i < sizeof($fcontents); $i++) {
$semail = strtolower(trim($fcontents[$i]));
// Extract all the email addresses in $semail into an array
$EmailAddresses = $this->find_email_addresses($semail);
if (is_array($EmailAddresses)) {
foreach ($EmailAddresses as $email) {
$email_user = $this->GetEmailUser($email);
$email_domain = $this->GetEmailDomain($email);
if ($email <> '') {
if ($globalunsub) {
if ($bounce == "d") {
$success = $this->SaveGlobalDomainUnsubscribe($email_domain,"SPAM");
if ($success) {
$html .= "<p>GDU $email_domain</p>";
$num++;
} else {
$html .= "<p>FFF $email_domain</p>";
$html .= "<p>" . $this->save_error . "</p>";
}
} elseif ($email <> '') {
$success = $this->SaveGlobalUnsubscribe($email,$email_user,$email_domain,$gutype,$reason);
if ($success) {
$html .= "<p>GU $email</p>";
$num++;
} else {
$html .= "<p>FF $email</p>";
$html .= "<p>" . $this->save_error . "</p>";
}
}
} else {
$added = $this->SimpleSubscribeAndConfirm($email);
$this->sunsubscribe = 1;
$this->sunsubscribedate = date("Y-m-d H:i:s");
$this->sunsubscribereason = $reason;
$updated = $this->Update();
if ($added && $updated) {
$html .= "<p>AU ";
$num++;
} else {
$html .= "<p>FU ";
}
$html .= "$email --> $this->semail</p>";
}
}
}
}
// script is allowed to run forever
set_time_limit(0);
}
$html .= "<p>Number unsubscribed: $num</p>";
return $html;
}
// This finds all email addresses
function find_email_addresses($semail) {
if (preg_match_all($this->find_email_preg,$semail,$matches)) {
return $matches[1];
} else {
return "";
}
}
// checks if an email address is subscribed. returns true if subscribed and sets $this->suid and $this->spriority else returns false
public function IsSubscribed($semail) {
$subscribed = false;
// check if email address is subscribed
$sql ="select s_uniqid, s_priority from subscribers where s_email = \"$semail\"";
$result = $this->dbconn->query($sql);
if ($result->num_rows == 1) {
// email address is subscribed. return true
$row = $result->fetch_array();
$this->suid = $row["s_uniqid"];
$this->spriority = $row["s_priority"];
$subscribed = true;
}
$result->close();
return $subscribed;
}
// this is a quick function to subscribe and confirm a single email address
// returns true if address was added else false. $this->suid contains the suid
// updates the subscriber priority to the higher value if suppplied priority is greater
public function SimpleSubscribeAndConfirm($semail,$spriority = 0) {
$semail = strtolower(trim($semail));
if ($semail == '') return false;
$this->Reset(); // reset subscriber variables
$subscribed = $this->IsSubscribed($semail);
$this->semail = $semail;
if ($subscribed) {
if ($spriority > $this->spriority) { // priority is set in IsSubscribed
$cansendto = $this->Read($this->suid);
if ($cansendto) {
$this->spriority = max($spriority,$this->spriority);
$this->Update();
}
}
} else {
$this->spriority = max($spriority,$this->spriority);
return $this->Create();
}
return false;
}
// saves subscriber to mysql. returns true if successful, false if not. $this->save_error contains the text error msg
public function save() {
$this->semail = $this->dbconn->real_escape_string(strtolower(trim($this->semail)));
if ($this->semail == "") {
$this->save_error = "email is blank";
return false;
}
$this->save_error = "";
$this->save_result = "";
$this->suid = $this->dbconn->real_escape_string(trim($this->suid));
$this->spriority = $this->dbconn->real_escape_string($this->spriority);
$this->sfname = $this->dbconn->real_escape_string(trim($this->sfname));
$this->slname = $this->dbconn->real_escape_string(trim($this->slname));
$this->sprovince = $this->dbconn->real_escape_string($this->sprovince);
$this->scountry = $this->dbconn->real_escape_string($this->scountry);
$this->sgender = $this->dbconn->real_escape_string($this->sgender);
// Checks if the email address is already in our database
// $old_suid = $this->suid;
$spriority = $this->spriority;
$subscribed = $this->IsSubscribed($this->semail);
$this->spriority = $spriority;
if ($subscribed) {
$success = $this->UpdateSubscriber();
if ($success) {
$this->save_result = "$this->semail subscriber info updated.";
return true;
} else {
$this->save_result = "$this->semail subscriber info NOT updated due to an error.";
$this->save_error = $this->update_error;
return false;
}
} else {
$success = $this->Subscribe();
if ($success) {
$this->save_result = "$this->semail has been subscribed to " . $this->ListName;
return true;
} else {
$this->save_result = "$this->semail was NOT subscribed to " . $this->ListName . " due to an error.";
$this->save_error = $this->create_error;
return false;
}
}
}
// Add a new subscriber
public function Subscribe() {
$added = $this->Create();
if ($added) {
// send notification email to subscriber and admin
$sname = $this->sfname . " " . $this->slname;
$subject = $this->ListName . " subscription notification";
$mhtml = "<p>{$this->semail} has been subscribed to {$this->ListName}.</p><p>Please confirm your subscription by clicking this link:<br /><a href=\"{$this->BaseURL}confirm/{$this->suid}\">CONFIRM SUBSCRIPTION</a></p>";
$mtext = "{$this->semail} has been subscribed to {$this->ListName}.\nPlease confirm your subscription by clicking this link:\n{$this->BaseURL}confirm/{$this->suid}";
$mailer = $this->fat->get('mailer');
$mailer->OpenSMTP();
$mailer->SendMessage($this->suid,"0","S",$this->FromAddress,$this->semail,$sname,$subject,$mhtml,$mtext,true); // the true at the end means this message will be blind copied to the Admin
$mailer->CloseSMTP();
}
// returns whether the subscription succeeded or not
return $added;
}
public function UpdateSubscriber() {
$this->sunsubscribe = 0;
$this->sunsubscribedate = NULL;
$this->sbounces = 0;
$this->semailsleft = $this->SubscriptionConfirmAmount;
$this->spriority = $this->spriority + 10;
$updated = $this->Update();
if ($updated) {
// send subscriber update notification email to subscriber and admin
$sname = $this->sfname . " " . $this->slname;
$subject = $this->ListName . " subscriber info updated";
$mhtml = "<p>Subscriber info for {$this->semail} on {$this->ListName} has been updated.</p><p><a href=\"{$this->BaseURL}subscribe/{$this->suid}\">UPDATE YOUR PROFILE</a></p>";
$mtext = "Subscriber info for {$this->semail} on {$this->ListName} has been updated.\nUpdate your profile: {$this->BaseURL}subscribe/{$this->suid}";
$mailer = $this->fat->get('mailer');
$mailer->OpenSMTP();
$mailer->SendMessage($this->suid,"0","P",$this->FromAddress,$this->semail,$sname,$subject,$mhtml,$mtext,true); // the true at the end means this message will be blind copied to the Admin
$mailer->CloseSMTP();
}
return $updated;
}
// Bulk subscribes without confirmation, sends email to each email with confirmation link
public function ClaimEmail($bemail,$uid) {
$html = "";
$txt = preg_replace('/\r\n|\r/', "\n", $bemail);
$fcontents = explode("\n",trim($txt));
$num = 0;
for($i = 0; $i < sizeof($fcontents); $i++) {
$email = strtolower(trim($fcontents[$i]));
$EmailAddresses = $this->find_email_addresses($email);
if (is_array($EmailAddresses)) {
foreach ($EmailAddresses as $semail) {
$html .= "<p>$semail ";
$added = $this->SimpleSubscribeAndConfirm($semail);
if ($added) {
$html .= "subscribed and claimed: ADDED</p>";
$num++;
}
if ($this->suid <> '') {
// subscriber exists
if ($this->s_u_id == 0) {
// send a confirmation email
$subject = $this->ListName . " subscription notification";
$mhtml = "<p>{$this->semail} has been claimed by {$uid} on {$this->ListName}.</p><p>Please confirm your claim by clicking this link:<br /><a href=\"{$this->BaseURL}confirm/{$this->suid}/{$uid}\">CONFIRM SUBSCRIPTION</a></p>";
$mtext = "{$this->semail} has been claimed by {$uid} on {$this->ListName}.\nPlease confirm your claim by clicking this link:\n{$this->BaseURL}confirm/{$this->suid}/{$uid}";
$mailer = $this->fat->get('mailer');
$mailer->OpenSMTP();
$mailer->SendMessage($this->suid,"0","K",$this->FromAddress,$this->semail,'',$subject,$mhtml,$mtext,true); // the true at the end means this message will be blind copied to the Admin
$mailer->CloseSMTP();
$num++;
} elseif ($this->s_u_id == $uid) {
// email is already claimed by user
$html .= " is already claimed by you</p>";
} else {
// email is already claimed by someone else
$html .= " is already claimed by someone else</p>";
}
}
}
}
}
$html .= "<p>Number claimed: $num</p>";
return $html;
}
public function UnClaimEmail($suid,$uid) {
$exists = $this->Read($suid);
if ($exists) {
$this->s_u_id = 0;
$this->Update();
return $this->semail;
}
return '';
}
public function ConfirmSubscription($suid,$uid = 0) {
$html = "";
$exists = $this->Read($suid);
if ($exists) {
$html .= "<p>Subscriber exists</p>";
$this->s_u_id = $uid;
$this->sconfirm = 1;
$this->sconfirmdate = date("Y-m-d H:i:s");
$this->sbounces = 0;
$this->spriority = $this->spriority + 10;
$this->semailsleft = $this->SubscriptionConfirmAmount;
$updated = $this->Update();
if ($updated) {
$html .= "<p>Subscriber updated: $this->semail</p>";
// confirmation suceeded - send notification email to subscriber and admin
$sname = "{$this->sfname} {$this->slname}";
$subject = "{$this->ListName} confirmation of subscription notification";
$mhtml = "<p>{$this->semail} has confirmed their subscription to {$this->ListName}.</p>";
$mtext = "{$this->semail} has confirmed their subscription to {$this->ListName}.\n";
$mailer = $this->fat->get('mailer');
$mailer->OpenSMTP();
$mailer->SendMessage($suid,"0","C",$this->FromAddress,$this->semail,$sname,$subject,$mhtml,$mtext,true); // the true at the end means this message will be blind copied to the Admin
$mailer->CloseSMTP();
$html .= "<p>{$this->semail} has confirmed their subscription to {$this->ListName}.</p>";
return $html;
}
} else {
$html .= "<p>Confirmation code is incorrect.</p>";
}
return $html;
}
public function CreateBulkSubscribeHTMLform() {
$html = "";
if ($this->user->uadmin <> '1') {
$html = "<p>Access denied</p>";
return $html;
}
$html .= "<form name=\"bsubscribeform\" action=\"{$this->BaseURL}bulk-subscribe\" method=\"post\" class=\"form-horizontal\" role=\"form\">";
$html .= "<fieldset><legend>Subscribe multiple emails</legend>";
$input_text = array(
0 => array("spriority","Priority","number",100)
);
foreach($input_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$icontrol[0]}\">{$icontrol[1]}</label>";
$html .= "<div class=\"col-md-6\">";
$html .= "<input class=\"form-control\" type=\"{$icontrol[2]}\" size=\"10\" name=\"{$icontrol[0]}\" value=\"{$icontrol[3]}\">";
$html .= "</div></div>";
}
$textarea_text = array(
0 => array("bemail","Emails to subscribe"),
);
foreach($textarea_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$icontrol[0]}\">{$icontrol[1]}</label>";
$html .= "<div class=\"col-md-6\">";
$html .= "<textarea class=\"form-control\" rows=\"20\" cols=\"90\" name=\"{$icontrol[0]}\"></textarea>";
$html .= "</div></div>";
}
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"col-md-offset-1 col-md-6\">";
$html .= "<button class=\"btn btn-primary\" type=\"submit\">Subscribe</button>";
$html .= "</div></div>";
$html .= "</fieldset></form>";
return $html;
}
public function CreateBulkUnsubscribeHTMLform() {
$html = "";
if ($this->user->uadmin <> '1') {
$html = "<p>Access denied</p>";
return $html;
}
$html .= "<form name=\"bunsubscribeform\" action=\"{$this->BaseURL}bulk-unsubscribe\" method=\"post\" class=\"form-horizontal\" role=\"form\">";
$html .= "<fieldset><legend>Unsubscribe multiple emails</legend>";
$radio_text = array(
0 => array("l","Remove from this list only",true),
1 => array("d","Filter entire domains",false),
2 => array("b","These are bounces",false),
3 => array("s","These are spam complainers",false)
);
foreach($radio_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"radio\">";
$html .= "<label class=\"col-md-offset-1\">";
$html .= "<div class=\"|col-md-6\">";
$checked = $icontrol[2] ? "checked" : "";
$html .= "<input type=\"radio\" name=\"bbounce\" value=\"{$icontrol[0]}\" {$checked}>{$icontrol[1]}";
$html .= "</div></label>";
$html .= "</div></div>";
}
$textarea_text = array(
0 => array("bemail","Emails to unsubscribe"),
);
foreach($textarea_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$icontrol[0]}\">{$icontrol[1]}</label>";
$html .= "<div class=\"col-md-6\">";
$html .= "<textarea class=\"form-control\" rows=\"20\" cols=\"90\" name=\"{$icontrol[0]}\"></textarea>";
$html .= "</div></div>";
}
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"col-md-offset-1 col-md-6\">";
$html .= "<button class=\"btn btn-primary\" type=\"submit\">Unsubscribe</button>";
$html .= "</div></div>";
$html .= "</fieldset></form>";
return $html;
}
public function CreateUnsubscribeHTMLform($suid) {
$html = "";
if (!$this->Read($suid)) {
$html .= "<p>The subscriber has already been removed from {$this->ListName}.</p>";
return $html;
}
if ($this->sunsubscribe == "1") {
$html .= "<p>$this->semail has already been unsubscribed from {$this->ListName}.</p>";
return $html;
}
$html .= "<p>Email address to be unsubscribed: <b>$this->semail</b></p>";
$html .= "<form name=\"unsubscribeform\" action=\"{$this->BaseURL}unsubscribe\" method=\"post\" class=\"form-horizontal\" role=\"form\">";
$html .= "<input name=\"suid\" type=\"hidden\" value=\"{$suid}\">";
$html .= "<fieldset><legend>Unsubscribe form</legend>";
$radio_text = array(
0 => array("b","Address does not exist",false),
1 => array("s","Stop sending me SPAM",false),
2 => array("g","Remove me from ALL mailing lists",false),
3 => array("o","Take me off this list only",true)
);
foreach($radio_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"radio\">";
$html .= "<label class=\"col-md-offset-1\">";
$html .= "<div class=\"|col-md-6\">";
$checked = $icontrol[2] ? "checked" : "";
$html .= "<input type=\"radio\" name=\"sglobalunsubscribe\" value=\"{$icontrol[0]}\" {$checked}>{$icontrol[1]}";
$html .= "</div></label>";
$html .= "</div></div>";
}
$textarea_text = array(
0 => array("sunsubscribereason","Reason/comments")
);
foreach($textarea_text as $icontrol) {
$html .= "<div class=\"form-group\">";
$html .= "<label class=\"col-md-1 control-label\" for=\"{$icontrol[0]}\">{$icontrol[1]}</label>";
$html .= "<div class=\"col-md-6\">";
$html .= "<textarea class=\"form-control\" name=\"{$icontrol[0]}\"></textarea>";
$html .= "</div></div>";
}
$html .= "<div class=\"form-group\">";
$html .= "<div class=\"col-md-offset-1 col-md-6\">";
$html .= "<button class=\"btn btn-primary\" type=\"submit\">Unsubscribe</button>";
$html .= "</div></div>";
$html .= "</fieldset></form>";
return $html;
}
// check that the unsubscribe succeeded
public function Unsubscribe() {
$html = "";
if ($this->user->uadmin == "1") {
$admin = true;
} else {
$admin = false;
}
$exists = $this->Read($this->suid);
if (!$exists) {
$html .= "<p>The subscriber has already been removed from {$this->ListName}.</p>";
return $html;
}
if ($this->sunsubscribe == "1") {
$html .= "<p>{$this->semail} has already been unsubscribed from {$this->ListName}.</p>";
return $html;
}
if ($this->sglobalunsubscribe == "b") {
$global = true;
$subject_tag = "BOUNCE ";
$gutype = ($admin ? 'BOUNCE-ADMIN' : 'BOUNCE');
} elseif ($this->sglobalunsubscribe == "s") {
$global = true;
$subject_tag = "SPAM ";
$gutype = ($admin ? 'SPAM-ADMIN' : 'SPAM');
} elseif ($this->sglobalunsubscribe == "g") {
$global = true;
$subject_tag = "";