-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsletter.php
347 lines (346 loc) · 15.8 KB
/
newsletter.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
<?php
session_start();
if(!isset($_POST['unox']) || $_POST['unox']!=$_SESSION['unox']) {sleep(2);exit;} // appel depuis uno.php
?>
<?php
include('../../config.php');
include('lang/lang.php');
$busy = (isset($_POST['ubusy'])?preg_replace("/[^A-Za-z0-9-_]/",'',$_POST['ubusy']):'index');
// ********************* actions *************************************************************************
if(isset($_POST['action'])) {
switch ($_POST['action']) {
// ********************************************************************************************
case 'plugin': ?>
<link rel="stylesheet" type="text/css" media="screen" href="uno/plugins/newsletter/newsletter.css" />
<div class="blocForm">
<div id="newsletterC" class="bouton fr" onClick="f_config_newsletter();" title="<?php echo T_("Configure the plugin");?>"><?php echo T_("Config");?></div>
<div id="newsletterL" class="bouton fr" onClick="f_list_newsletter();" title="<?php echo T_("Edit your mailing list");?>"><?php echo T_("Mailing List");?></div>
<div id="newsletterW" class="bouton fr current" onClick="f_write_newsletter();" title="<?php echo T_("Write a newsletter");?>"><?php echo T_("Write");?></div>
<h2><?php echo T_("Newsletter");?></h2>
<div id="newsletterWrite">
<div id="newsletterResult"></div>
<p>
<?php echo T_("This plugin allows you to send a newsletter to an email list.")." ";?>
<?php echo T_("It works with the mail() function of your server or in SMTP with your GMAIL account or with another SMTP Provider account.");?>
</p>
<p>
<?php echo T_("The shortcode");?> <code>[[newsletter]]</code> <?php echo T_("add a field to enter his email address and receive the newsletter.")." ";?>
<?php echo T_("The newsletter contains in footer an automatic unsubscribe link.");?>
</p>
<p><?php echo T_("This plugin adds the excellent PHPMailer module which will then be used by other plugins (contact, users...).");?></p>
<div class="blocForm">
<div class="input" id="newsletterP">
<p><?php echo T_("Subject");?></p>
<input name="newsletterSu" id="newsletterSu" type="text" value="" />
<p><?php echo T_("Content");?></p>
<textarea name="newsletterCont" id="newsletterCont"></textarea>
</div>
</div>
<div class="blocBouton">
<div id="newsletterGroup" class="newsletterGroup fl"></div>
<div id="newsletterSaveCont" class="bouton fr <?php if(!file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) echo 'danger'; ?>" onClick="f_save_newsletter();" title="<?php echo T_("Saves the contents");?>"><?php echo T_("Save");?></div>
<div class="bouton fr" onClick="f_send_newsletter(0,'<?php echo T_("Sending");?>','<?php echo T_("finished.");?>');" title="<?php echo T_("Send only to Admin");?>"><?php echo T_("Send to test");?></div>
<div class="bouton fr" onClick="f_send_newsletter(1,'<?php echo T_("Sending");?>','<?php echo T_("finished.");?>');" title="<?php echo T_("Send the newsletter");?>"><?php echo T_("Send");?></div>
</div>
</div>
<div id="newsletterList" style="display:none;">
<div>
<p><?php echo T_("New recipient");?></p>
<table>
<tr><td>
<input name="newsletterAdd" id="newsletterAdd" size="50" type="text" />
<br />
<input name="newsletterLGadd" id="newsletterLGadd" size="10" type="text" placeholder="<?php echo T_("Create group");?>" />
<div id="newsletterLgroup" class="newsletterGroup fl" style="width:320px;"></div>
</td><td><div style="height:4px;"> </div>
<div class="bouton" style="margin-left:25px;" onClick="f_add_newsletter();" title="<?php echo T_("Add a new recipient");?>"><?php echo T_("Add");?></div>
</td></tr>
</table>
<div id="newsletterML">
<table>
<thead>
<tr>
<th><?php echo T_("Email");?></th>
<th><?php echo T_("Group");?></th>
<th colspan="2"><?php echo T_("Action");?></th>
</tr>
</thead>
<tbody id="newsletterTlist"></tbody>
</table>
</div>
<?php
if(file_exists('../../data/_sdata-'.$sdata.'/users.json') && file_exists('../users/users.php')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/users.json');
$a = json_decode($q,true);
echo '<p>'.T_("The User group contains the members of the Users plugin :").' '.count($a['user']).' '.T_("members.").'</p>';
} ?>
</div>
</div>
<div id="newsletterConfig" style="display:none;">
<table class="hForm">
<tr>
<td colspan=2 style="text-align:left;padding-left:30px;font-weight:700;"><?php echo T_("Sending method");?> :</td>
</tr>
<tr>
<td><label><?php echo T_("Method");?></label></td>
<td>
<select name="newsletterMet" id="newsletterMet" onChange="f_trsmtp_newsletter(this)">
<option value=""><?php echo T_("Default (PHP)");?></option>
<option value="gmail"><?php echo T_("Gmail (SMTP)");?></option>
<option value="smtp"><?php echo T_("Other SMTP");?></option>
</select>
</td>
<td><em><?php echo T_("Newsletter is sent by using this method.");?></em></td>
</tr>
<tr class="trsmtp">
<td><label><?php echo T_("Username");?></label></td>
<td><input type="text" class="input" name="newsletterGmA" id="newsletterGmA" style="width:150px;" /></td>
<td><em><?php echo T_("Email address for Gmail, username for other SMTP.");?></em></td>
</tr>
<tr class="trsmtp">
<td><label><?php echo T_("Password");?></label></td>
<td><input type="password" class="input" name="newsletterGmP" id="newsletterGmP" style="width:150px;" /></td>
<td><em><?php echo T_("Account password for this username. (Registered encrypted)");?></em></td>
</tr>
<tr class="trsmt2">
<td><label><?php echo T_("Host");?></label></td>
<td><input type="password" class="input" name="newsletterGmH" id="newsletterGmH" style="width:150px;" /></td>
<td><em><?php echo T_("URL of your SMTP Provider");?> (smtp.mailgun.org, smtp.mandrillapp.com...)</em></td>
</tr>
</table>
<div id="newsletterSaveConf" class="bouton fr <?php if(!file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) echo 'danger'; ?>" onClick="f_saveConf_newsletter();" title="<?php echo T_("Save settings");?>"><?php echo T_("Save");?></div>
</div>
<div class="clear"></div>
</div>
<?php break;
// ********************************************************************************************
case 'save':
$b = 0;
if(file_put_contents('../../data/newsletter.txt', $_POST['cont'])) $b=1;
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json');
$a = json_decode($q,true);
}
else $a = array();
if(!isset($a['met'])) $a['met'] = '';
if(!isset($a['gma'])) $a['gma'] = '';
if(!isset($a['gmp'])) $a['gmp'] = '';
if(!isset($a['gmh'])) $a['gmh'] = '';
if(empty($a['iv'])) $a['iv'] = base64_encode(openssl_random_pseudo_bytes(16));
if(empty($a['group'])) $a['group'] = array('ext','man');
$a['su'] = strip_tags($_POST['su']);
$out = json_encode($a);
if(file_put_contents('../../data/_sdata-'.$sdata.'/newsletter.json', $out) && $b) echo T_('newsletter saved');
else echo '!'.T_('Impossible backup');
break;
// ********************************************************************************************
case 'saveConf':
$b = 0;
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json');
$a = json_decode($q,true);
}
else $a = array();
$a['met'] = $_POST['met'];
$a['gma'] = $_POST['gma'];
$a['gmh'] = $_POST['gmh'];
if(empty($a['iv'])) $a['iv'] = base64_encode(openssl_random_pseudo_bytes(16));
if(empty($a['group'])) $a['group'] = array('ext','man');
if($a['gma']) $a['gmp'] = base64_encode(openssl_encrypt(strip_tags($_POST['gmp']), 'AES-256-CBC', substr($Ukey,0,32), OPENSSL_RAW_DATA, base64_decode($a['iv'])));
$out = json_encode($a);
if(file_put_contents('../../data/_sdata-'.$sdata.'/newsletter.json', $out)) echo T_('config saved');
else echo '!'.T_('Error');
break;
// ********************************************************************************************
case 'load':
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json'); // liste des emails + options + sujet
$a = json_decode($q,true);
if(isset($a['list'][0]) && is_string($a['list'][0])) { // *** PATCH - ADD GROUP IF NOT EXISTS
$c = array();
foreach($a['list'] as $k=>$v) {
$c[$v] = array('man','ext');
}
$a['list'] = $c;
file_put_contents('../../data/_sdata-'.$sdata.'/newsletter.json', json_encode($a));
} // ***
if(file_exists('../../data/'.$busy.'/site.json')) {
$q = file_get_contents('../../data/'.$busy.'/site.json'); $b = json_decode($q,true); $a['tit'] = $b['tit']; $a['url'] = $b['url']; $a['nom'] = $b['nom'];
}
else exit;
if(file_exists('../../data/_sdata-'.$sdata.'/ssite.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/ssite.json'); $b = json_decode($q,true); $a['mel'] = $b['mel'];
}
else exit;
if(isset($a['gmp']) && $a['gmp']) {
$a['gmp'] = openssl_decrypt(base64_decode($a['gmp']), 'AES-256-CBC', substr($Ukey,0,32), OPENSSL_RAW_DATA, base64_decode($a['iv']));
$a['gmp'] = rtrim($a['gmp'], "\0");
}
$c = array('ext'=>T_("External"), 'man'=>T_("Manual"));
if(isset($a['group'])) {
foreach($a['group'] as $k=>$v) {
if(isset($c[$v])) $a['group'][$k] = '|'.$c[$v];
}
if(file_exists('../../data/_sdata-'.$sdata.'/users.json') && file_exists('../users/users.php')) $a['group'][] = '|User';
}
if(isset($a['list'])) {
foreach($a['list'] as $k=>$v) {
foreach($v as $k1=>$v1) if(isset($c[$v1])) $a['list'][$k][$k1] = $c[$v1];
}
}
if(!empty($_POST['send']) && file_exists('../../data/_sdata-'.$sdata.'/users.json') && file_exists('../users/users.php')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/users.json');
$c = json_decode($q,true);
if(isset($c['user'])) {
foreach($c['user'] as $k=>$v) {
if(!empty($v['e']) && (!isset($a['unsub']) || !in_array($v['e'],$a['unsub']))) {
if(!isset($a['list'][$v['e']])) $a['list'][$v['e']] = array('User');
else $a['list'][$v['e']][] = 'User';
}
}
}
}
$out = json_encode($a);
echo $out;
}
else echo '';
exit;
break;
// ********************************************************************************************
case 'loadContent':
if(file_exists('../../data/newsletter.txt')) {
$q = file_get_contents('../../data/newsletter.txt'); // contenu
echo stripslashes($q);
}
else echo '';
exit;
break;
// ********************************************************************************************
case 'add':
$mel = strip_tags($_POST['add']);
$ngr = strip_tags($_POST['ng']);
$group = ((isset($_POST['group'])&&$j=json_decode(strip_tags($_POST['group'])))?$j:array());
if($mel) {
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json');
$a = json_decode($q,true);
}
if(!in_array('man',$group)) $group[] = 'man';
if($ngr && !in_array($ngr,$group)) $group[] = $ngr;
$a['list'][$mel] = $group; // Add/Update mail to list
foreach($group as $r) {
if($r && !in_array($r,$a['group']) && $r!='User') $a['group'][] = $r;
}
// Check group list
$b = array();
foreach($a['list'] as $gr) { // "list":{"[email protected]":["man","blue"],
foreach($gr as $v) if(!in_array($v,$b)) $b[] = $v;
}
foreach($a['group'] as $k=>$v) { // "group":["ext","man","blue,...]
if($v!='user' && $v!='ext' && $v!='man' && !in_array($v,$b)) unset($a['group'][$k]);
}
// CLEAN UNSUB
if(file_exists('../../data/_sdata-'.$sdata.'/users.json') && file_exists('../users/users.php') && isset($a['unsub'])) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/users.json');
$c = json_decode($q,true);
$b = ',';
foreach($c['user'] as $k=>$v) $b .= $v['e'].',';
foreach($a['unsub'] as $k=>$v) {
if(strpos($b, ','.$v.',')===false) unset($a['unsub'][$k]);
}
}
$out = json_encode($a);
if(file_put_contents('../../data/_sdata-'.$sdata.'/newsletter.json', $out)) echo T_('email added');
else echo '!'.T_('impossible add');
}
else echo '!'.T_('Error');
break;
// ********************************************************************************************
case 'del':
$l = strip_tags($_POST['del']);
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json') && $l) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json');
$a = json_decode($q,true);
if(isset($a['list'][$l])) unset($a['list'][$l]);
else {
echo '!'.T_('Error');
break;
}
// Check group exists
$b = array();
foreach($a['list'] as $k=>$v) {
foreach($v as $r) if(!in_array($r,$b)) $b[] = $r;
}
foreach($a['group'] as $k=>$v) {
if($v!='user' && $v!='ext' && $v!='man' && !in_array($v,$b)) unset($a['group'][$k]);
}
$out = json_encode($a);
if(file_put_contents('../../data/_sdata-'.$sdata.'/newsletter.json', $out)) echo T_('email deleted');
else echo '!'.T_('undeletable');
}
else echo '!'.T_('No data');
break;
// ********************************************************************************************
case 'send':
$dest = strip_tags($_POST['dest']);
$url = strip_tags($_POST['url']);
$tit = strip_tags($_POST['tit']);
$nom = strip_tags($_POST['nom']);
$sujet = strip_tags(stripslashes($_POST['su']));
$cont = stripslashes($_POST['cont']);
$mel = strip_tags($_POST['mel']);
$met = strip_tags($_POST['met']);
$gma = strip_tags($_POST['gma']);
$gmp = strip_tags($_POST['gmp']);
$gmh = strip_tags($_POST['gmh']);
if(file_exists('../../data/_sdata-'.$sdata.'/newsletter.json')) {
$q = file_get_contents('../../data/_sdata-'.$sdata.'/newsletter.json');
$a = json_decode($q,true);
}
else {
echo '!'.T_('Error');
break;
}
include '../../template/mailTemplate.php';
$r = base64_encode(openssl_encrypt(strip_tags($_POST['dest']), 'AES-256-CBC', substr($Ukey,0,32), OPENSSL_RAW_DATA, base64_decode($a['iv'])));
$ul = $url."/uno/plugins/newsletter/newsletterSubscribe.php?c=".urlencode($r)."&m=".urlencode($dest)."&a=del&b=".urlencode($url.'/'.$nom.'.html');
$supp = "<a href='".$ul."'>".T_("Unsubscribe")."</a>";
$bottom = str_replace('[[unsubscribe]]',$supp, $bottom); // template
$msgT = strip_tags($cont).' -------- '.T_("Unsubscribe").' : '.$ul;
$msgH = $top . $cont . $bottom;
$fm = preg_replace("/[^a-zA-Z ]+/", "", $tit);
// PHPMailer
require 'PHPMailer/PHPMailerAutoload.php';
$phm = new PHPMailer();
$phm->CharSet = "UTF-8";
$phm->setFrom($mel, $fm);
$phm->addReplyTo($mel, $fm);
$phm->AddAddress($dest);
$phm->isHTML(true);
$phm->Subject = $sujet;
$phm->Body = $msgH;
$phm->AltBody = $msgT;
if(empty($met)) { // PHP mail()
if($phm->Send()) echo '<div style="color:green;">'.$dest.' : OK</div>';
else echo '<div style="color:red;">'.$dest.' : '.T_("Failure").' : '.$phm->ErrorInfo.'</div>';
}
else { // SMTP
$phm->IsSMTP();
$phm->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$phm->SMTPAuth = true; // authentication enabled
$phm->SMTPSecure = 'tls';
$phm->Port = 587;
$phm->Host = ($met=='gmail'?'smtp.gmail.com':$gmh); // 'smtp.gmail.com'...
$phm->Username = $gma;
$phm->Password = utf8_encode($gmp);
if($phm->Send()) echo '<div style="color:green;">'.$dest.' : OK</div> --- ';
else echo '<div style="color:red;">'.$dest.' : '.T_("Failure").' : '.$phm->ErrorInfo.'</div> --- ';
sleep(1.1);
}
break;
// ********************************************************************************************
}
clearstatcache();
exit;
}
?>