tiki-send_newsletters.php 22 KB
Newer Older
lrargerich's avatar
lrargerich committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
rjsmelo's avatar
rjsmelo committed
6
//
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9
// $Id$
changi67's avatar
changi67 committed
10

11
$section = 'newsletters';
rjsmelo's avatar
rjsmelo committed
12
require_once('tiki-setup.php');
13
@ini_set('max_execution_time', 0); //will not work if safe_mode is on
14
$prefs['feature_wiki_protect_email'] = 'n'; //not to alter the email
rjsmelo's avatar
rjsmelo committed
15 16
include_once('lib/newsletters/nllib.php');
$auto_query_args = ['sort_mode', 'offset', 'find', 'nlId', 'cookietab', 'editionId'];
17

18 19 20
$access->check_feature('feature_newsletters');
$access->check_permission('tiki_p_send_newsletters');

rjsmelo's avatar
rjsmelo committed
21
if (! isset($_REQUEST["nlId"])) {
tombombadilom's avatar
tombombadilom committed
22 23
	$_REQUEST["nlId"] = 0;
}
24
$smarty->assign('nlId', $_REQUEST["nlId"]);
rjsmelo's avatar
rjsmelo committed
25 26
$newsletters = $nllib->list_newsletters(0, -1, 'created_desc', '', '', ["tiki_p_admin_newsletters", "tiki_p_send_newsletters"], 'n');
if (! $newsletters['cant']) {
27 28 29
	$smarty->assign('msg', tra("No newsletters available."));
	$smarty->display("error.tpl");
	die;
30
}
31 32 33
if (isset($_REQUEST['cancel'])) {
	unset($_REQUEST['editionId']);
}
34

rjsmelo's avatar
rjsmelo committed
35
if (empty($_REQUEST["sendingUniqId"])) {
36 37 38 39
	$sendingUniqId = $tikilib->genRandomString();
	$smarty->assign('sendingUniqId', $sendingUniqId);
}

rjsmelo's avatar
rjsmelo committed
40
if (! isset($_REQUEST['cookietab']) || isset($_REQUEST['editionId'])) {
tombombadilom's avatar
tombombadilom committed
41 42
	$_REQUEST['cookietab'] = 1;
}
43
$cookietab = $_REQUEST['cookietab'];
44
$smarty->assign('newsletters', $newsletters["data"]);
45
$smarty->assign('absurl', 'y');
46 47
if ($_REQUEST["nlId"]) {
	$nl_info = $nllib->get_newsletter($_REQUEST["nlId"]);
rjsmelo's avatar
rjsmelo committed
48 49 50
	if (! isset($_REQUEST["editionId"])) {
		$_REQUEST["editionId"] = 0;
	}
changi67's avatar
changi67 committed
51
	$smarty->assign('allowTxt', $nl_info['allowTxt']);
52
	$smarty->assign('allowArticleClip', $nl_info['allowArticleClip']);
53 54 55

	if ($prefs['newsletter_external_client'] == 'y') {
		$subscribers = $nllib->get_all_subscribers($_REQUEST["nlId"], "");
rjsmelo's avatar
rjsmelo committed
56
		$email_list = [];
57 58 59 60 61 62
		foreach ($subscribers as $subscriber) {
			$email_list[] = $subscriber['email'];
		}

		$smarty->assign('mailto_link', 'mailto:' . $prefs['sender_email'] . '?bcc=' . urlencode(implode(',', $email_list)));
	}
changi67's avatar
changi67 committed
63
} else {
64
	//No newsletter selected -> Check if the textarea for the first has to be displayed
changi67's avatar
changi67 committed
65
	$smarty->assign('allowTxt', $newsletters['data'][0]['allowTxt']);
66
	$smarty->assign('allowArticleClip', $newsletters['data'][0]['allowTxt']);
lrargerich's avatar
lrargerich committed
67
}
68 69
if ($_REQUEST["editionId"]) {
	$info = $nllib->get_edition($_REQUEST["editionId"]);
rjsmelo's avatar
rjsmelo committed
70
	if (! empty($_REQUEST['resend'])) {
71 72
		$info['editionId'] = 0;
	}
73
} else {
rjsmelo's avatar
rjsmelo committed
74
	$info = [];
75 76 77 78
	$info["data"] = '';
	$info["datatxt"] = '';
	$info["subject"] = '';
	$info["editionId"] = 0;
rjsmelo's avatar
rjsmelo committed
79
	$info["files"] = [];
80 81 82 83 84
	$info['wysiwyg'] = $prefs['wysiwyg_default'];
	$info['is_html'] = ($info['wysiwyg'] === 'y' && $prefs['wysiwyg_htmltowiki'] !== 'y');
}
$smarty->assign_by_ref('info', $info);

hangerman's avatar
hangerman committed
85
// Display to newsletter txtarea or not depending on the preferences
changi67's avatar
changi67 committed
86
$showBoxCheck = "
hangerman's avatar
hangerman committed
87 88
	<script type='text/javascript'>
	<!--
89
	function checkNewsletterTxtArea(nlIndex){
hangerman's avatar
hangerman committed
90
	browser();
91 92 93
	var allowTxt = new Array();
	var allowArticleClip = new Array();
	";
94
for ($i = 0, $tmp_count = count($newsletters['data']); $i < $tmp_count; $i++) {
rjsmelo's avatar
rjsmelo committed
95
	$showBoxCheck .= "allowTxt[$i] = '" . $newsletters['data'][$i]['allowTxt'] . "';
96 97 98 99 100
	allowArticleClip[$i] = '" . $newsletters['data'][$i]['allowArticleClip'] . "';
	";
}
// allowTxt
$showBoxCheck .= "	if (document.getElementById('txtcol1').style.display=='none' && allowTxt[nlIndex] == 'y'){";
changi67's avatar
changi67 committed
101
if (preg_match("/gecko/i", $_SERVER['HTTP_USER_AGENT'])) {
rjsmelo's avatar
rjsmelo committed
102 103
	$showBoxCheck .= "document.getElementById('txtcol1').style.display='table-cell';";
	$showBoxCheck .= "document.getElementById('txtcol2').style.display='table-cell';";
changi67's avatar
changi67 committed
104
} else {
rjsmelo's avatar
rjsmelo committed
105 106
	$showBoxCheck .= "document.getElementById('txtcol1').style.display='inline';	";
	$showBoxCheck .= "document.getElementById('txtcol2').style.display='inline';";
changi67's avatar
changi67 committed
107
};
rjsmelo's avatar
rjsmelo committed
108
$showBoxCheck .= "
109
    	}else if (allowTxt[nlIndex] == 'n') {
hangerman's avatar
hangerman committed
110 111
	document.getElementById('txtcol1').style.display='none';
	document.getElementById('txtcol2').style.display='none';
112 113 114 115
    	}";
// allowArticleClip
$showBoxCheck .= "	if (document.getElementById('clipcol1').style.display=='none' && allowArticleClip[nlIndex] == 'y'){";
if (preg_match("/gecko/i", $_SERVER['HTTP_USER_AGENT'])) {
rjsmelo's avatar
rjsmelo committed
116 117
	$showBoxCheck .= "document.getElementById('clipcol1').style.display='table-cell';";
	$showBoxCheck .= "document.getElementById('clipcol2').style.display='table-cell';";
118
} else {
rjsmelo's avatar
rjsmelo committed
119 120
	$showBoxCheck .= "document.getElementById('clipcol1').style.display='inline';	";
	$showBoxCheck .= "document.getElementById('clipcol2').style.display='inline';";
121
};
rjsmelo's avatar
rjsmelo committed
122
$showBoxCheck .= "
123 124 125 126 127 128
    	}else if (allowArticleClip[nlIndex] == 'n') {
	document.getElementById('clipcol1').style.display='none';
	document.getElementById('clipcol2').style.display='none';
    	}";
// end of function
$showBoxCheck .= "
hangerman's avatar
hangerman committed
129 130 131 132
	}
	-->
	</script>
	";
changi67's avatar
changi67 committed
133
$smarty->assign('showBoxCheck', $showBoxCheck);
134
if (isset($_REQUEST["remove"])) {
135 136
	$access->check_authenticity();
	$nllib->remove_edition($_REQUEST["nlId"], $_REQUEST["remove"]);
lrargerich's avatar
lrargerich committed
137
}
138

139
$editlib = TikiLib::lib('edit');
140
// wysiwyg decision
rjsmelo's avatar
rjsmelo committed
141
include_once('lib/setup/editmode.php');
142 143

// Handles switching editor modes
rjsmelo's avatar
rjsmelo committed
144
if (isset($_REQUEST['mode_normal']) && $_REQUEST['mode_normal'] == 'y') {
145 146 147 148 149 150 151
	if ($_REQUEST['wikiparse'] == 'on') {
		// Parsing page data as first time seeing html page in normal editor
		$smarty->assign('msg', "Parsing html to wiki");
		$info["data"] = $editlib->parseToWiki($_REQUEST["data"]);
	} else {
		$info["data"] = $_REQUEST["data"];
	}
152 153 154 155 156
	$info['wysiwyg'] = 'n';
	$info['is_html'] = false;
	unset($_REQUEST['is_html']);
	$_REQUEST['preview'] = 'y';
	$_REQUEST["data"] = $info["data"];
rjsmelo's avatar
rjsmelo committed
157
} elseif (isset($_REQUEST['mode_wysiwyg']) && $_REQUEST['mode_wysiwyg'] == 'y') {
158 159 160
	// Parsing page data as first time seeing wiki page in wysiwyg editor
	$smarty->assign('msg', "Parsing wiki to html");
	$info["data"] = $editlib->parseToWysiwyg($_REQUEST["data"]);
161 162 163
	$info['wysiwyg'] = 'y';
	$_REQUEST['preview'] = 'y';
	$_REQUEST["data"] = $info["data"];
164 165
}

166
if (isset($_REQUEST['is_html'])) {
rjsmelo's avatar
rjsmelo committed
167
	$info['is_html'] = ! empty($_REQUEST['is_html']);
168 169
	$_REQUEST['is_html'] = 'on';
} else {	// guess html based on wysiwyg mode
rjsmelo's avatar
rjsmelo committed
170
	$info['is_html'] = $info['wysiwyg'] === 'y' && $prefs['wysiwyg_htmltowiki'] !== 'y';
171 172 173
	$_REQUEST['is_html'] = $info['is_html'] ? 'on' : '';
}

174
$parserlib = TikiLib::lib('parser');
rjsmelo's avatar
rjsmelo committed
175
if (isset($_REQUEST["templateId"]) && $_REQUEST["templateId"] > 0 && (! isset($_REQUEST['previousTemplateId']) || $_REQUEST['previousTemplateId'] != $_REQUEST['templateId'])) {
176
	$template_data = TikiLib::lib('template')->get_template($_REQUEST["templateId"]);
177
	$_REQUEST["data"] = $template_data["content"];
rjsmelo's avatar
rjsmelo committed
178
	if (TikiLib::lib('template')->template_is_in_section($_REQUEST['templateId'], 'wiki_html')) {
179
		$_REQUEST['is_html'] = 'on';
rjsmelo's avatar
rjsmelo committed
180
		$_REQUEST['wysiwyg'] = 'y';
181 182
	}
	if (isset($_SESSION['wysiwyg']) && $_SESSION['wysiwyg'] == 'y' || $_REQUEST['wysiwyg'] === 'y') {
rjsmelo's avatar
rjsmelo committed
183
		$_REQUEST['data'] = $parserlib->parse_data($_REQUEST['data'], ['is_html' => $info['is_html'], 'absolute_links' => true, 'suppress_icons' => true]);
184
	}
185
	$_REQUEST["preview"] = 1;
186
	$smarty->assign("templateId", $_REQUEST["templateId"]);
lrargerich's avatar
lrargerich committed
187
}
rjsmelo's avatar
rjsmelo committed
188
$newsletterfiles = [];
189
if (isset($_REQUEST['newsletterfile'])) {
rjsmelo's avatar
rjsmelo committed
190
	$newsletterfiles_post = isset($_REQUEST['newsletterfile']) && is_array($_REQUEST['newsletterfile']) ? $_REQUEST['newsletterfile'] : [];
191
	foreach ($newsletterfiles_post as $k => $id) {
rjsmelo's avatar
rjsmelo committed
192
		$f = [];
193 194 195 196 197
		if ((strlen($id) == 32) && preg_match('/^[0-9a-f]{32}$/', $id)) { // this is a valid md5 hash, so the file was just saved at preview time
			$fpath = $prefs['tmpDir'] . '/newsletterfile-' . $id;
			$f = unserialize(file_get_contents($fpath . '.infos'));
			$f['path'] = $fpath;
			$newsletterfiles[] = $f;
rjsmelo's avatar
rjsmelo committed
198
		} elseif ((int)$_REQUEST['nlId'] > 0) {
199
			foreach ($info['files'] as $f) {
200 201 202 203
				if ($f['id'] == (int)$id) {
					$newsletterfiles[] = $f;
					break;
				}
204 205 206
			}
		}
	}
207 208
} else {
	$newsletterfiles = $info['files'];
209
}
rjsmelo's avatar
rjsmelo committed
210
if (! empty($_FILES) && ! empty($_FILES['newsletterfile'])) {
211
	foreach ($_FILES['newsletterfile']['name'] as $i => $v) {
changi67's avatar
changi67 committed
212
		if ($_FILES['newsletterfile']['error'][$i] == UPLOAD_ERR_OK) {
rjsmelo's avatar
rjsmelo committed
213
			$newsletterfiles[] = [
214 215 216 217 218 219
				'name' => $_FILES['newsletterfile']['name'][$i],
				'type' => $_FILES['newsletterfile']['type'][$i],
				'path' => $_FILES['newsletterfile']['tmp_name'][$i],
				'error' => $_FILES['newsletterfile']['error'][$i],
				'size' => $_FILES['newsletterfile']['size'][$i],
				'savestate' => 'phptmp',
rjsmelo's avatar
rjsmelo committed
220
			];
changi67's avatar
changi67 committed
221
		} else {
222 223
			$error['title'] = tra('A problem occurred during file uploading');
			$error['mes'] = tra('File causing trouble was at rank') . ' ' . ($i + 1);
rjsmelo's avatar
rjsmelo committed
224 225 226 227
			$error['mes'] = tr(
				'The error was %0',
				$tikilib->uploaded_file_error($_FILES['newsletterfile']['error'][$i])
			);
228
			Feedback::error($error);
229
		}
230 231
	}
}
232
$_REQUEST['files'] = $info['files'] = $newsletterfiles;
233
foreach ($info['files'] as $k => $newsletterfile) {
234 235
	if ($newsletterfile['savestate'] == 'phptmp') {
		// move it to temp
changi67's avatar
changi67 committed
236
		$tmpfnamekey = md5(rand() . time() . $newsletterfile['path'] . $newsletterfile['name'] . $newsletterfile['type']);
237
		$tmpfname = $prefs['tmpDir'] . '/newsletterfile-' . $tmpfnamekey;
238
		if (move_uploaded_file($newsletterfile['path'], $tmpfname)) {
changi67's avatar
changi67 committed
239 240 241 242 243
			$info['files'][$k]['savestate'] = 'tikitemp';
			$info['files'][$k]['path'] = $tmpfname;
			$info['files'][$k]['id'] = $tmpfnamekey;
			$info['files'][$k]['filename'] = $tmpfnamekey;
			file_put_contents($tmpfname . '.infos', serialize($info['files'][$k]));
244 245 246 247
		}
	}
}
$smarty->assign('preview', 'n');
248 249
if (isset($_REQUEST["preview"])) {
	$smarty->assign('preview', 'y');
250 251 252 253 254
	if (isset($_REQUEST["subject"])) {
		$info["subject"] = $_REQUEST["subject"];
	} else {
		$info["subject"] = '';
	}
255 256 257 258 259
	if (isset($_REQUEST["data"])) {
		$info["data"] = $_REQUEST["data"];
	} else {
		$info["data"] = '';
	}
rjsmelo's avatar
rjsmelo committed
260 261 262 263 264 265
	if (isset($_REQUEST['wikiparse']) && $_REQUEST['wikiparse'] == 'on') {
		$info['wikiparse'] = 'y';
	} else {
		$info['wikiparse'] = 'n';
	}
	if (! empty($_REQUEST["datatxt"])) {
changi67's avatar
changi67 committed
266
		$info["datatxt"] = $_REQUEST["datatxt"];
hangerman's avatar
hangerman committed
267
		//For the hidden input
changi67's avatar
changi67 committed
268
		$smarty->assign('datatxt', $_REQUEST["datatxt"]);
hangerman's avatar
hangerman committed
269 270 271
	} else {
		$info["datatxt"] = '';
	}
rjsmelo's avatar
rjsmelo committed
272 273
	if (! empty($_REQUEST["usedTpl"])) {
		$smarty->assign('dataparsed', (($info['wikiparse'] == 'y') ? $parserlib->parse_data($info["data"], ['absolute_links' => true, 'suppress_icons' => true]) : $info['data']));
274
		$smarty->assign('subject', $info["subject"]);
changi67's avatar
changi67 committed
275 276 277 278
		$info["dataparsed"] = $smarty->fetch("newsletters/" . $_REQUEST["usedTpl"]);
		if (stristr($info['dataparsed'], "<body") === false) {
			$info['dataparsed'] = "<html><body>" . $info['dataparsed'] . "</body></html>";
		}
279
		$smarty->assign("usedTpl", $_REQUEST["usedTpl"]);
280
	} else {
281 282 283
		$info['dataparsed'] = '<html><body>';
		if ($info['wikiparse'] === 'y') {
			$data = $info['data'];
rjsmelo's avatar
rjsmelo committed
284
			$info['dataparsed'] .= $parserlib->parse_data($data, ['absolute_links' => true, 'suppress_icons' => true,'is_html' => $info['is_html']]);
285 286 287 288 289 290 291
			if (empty($info['data'])) {
				$info['data'] = $data;		// somehow on massive pages this gets reset somewhere inside parse_data
			}
		} else {
			$info['dataparsed'] .= $info['data'];
		}
		$info['dataparsed'] .= '</body></html>';
292
	}
rjsmelo's avatar
rjsmelo committed
293
	if (! empty($_REQUEST['replyto'])) {
294 295
		$smarty->assign('replyto', $_REQUEST['replyto']);
	}
rjsmelo's avatar
rjsmelo committed
296
	if (! empty($_REQUEST['sendfrom'])) {
297 298
		$smarty->assign('sendfrom', $_REQUEST['sendfrom']);
	}
299
	$previewdata = $info['dataparsed'];
300
	$parsed = $info['dataparsed'];
301 302
	if ($nl_info["allowArticleClip"] == 'y' && $nl_info["autoArticleClip"] == 'y') {
		$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
303
		$txtArticleClip = $nllib->generateTxtVersion($articleClip);
304
		$info['datatxt'] = str_replace("~~~articleclip~~~", $txtArticleClip, $info['datatxt']);
305 306
		$previewdata = str_replace("~~~articleclip~~~", $articleClip, $previewdata);
	}
307
	$smarty->assign_by_ref('info', $info);
308
	$smarty->assign('previewdata', $previewdata);
309

310 311 312
	$themelib = TikiLib::lib('theme');
	$news_cssfile = $themelib->get_theme_path($prefs['theme'], '', 'newsletter.css');
	$news_cssfile_option = $themelib->get_theme_path($prefs['theme'], $prefs['theme_option'], 'newsletter.css');
313 314

	TikiLib::lib('header')->add_cssfile($news_cssfile)->add_cssfile($news_cssfile_option);
lrargerich's avatar
lrargerich committed
315
}
316 317
$smarty->assign('presend', 'n');
if (isset($_REQUEST["save"])) {
318
	check_ticket('send-newsletter');
319
	// Now send the newsletter to all the email addresses and save it in sent_newsletters
320
	$info['datatxt'] = $_REQUEST['datatxt'];
321
	$smarty->assign('presend', 'y');
322
	$subscribers = isset($subscribers) ? $subscribers : $nllib->get_all_subscribers($_REQUEST["nlId"], "");
323
	$smarty->assign('nlId', $_REQUEST["nlId"]);
324
	$smarty->assign('datatxt', $_REQUEST["datatxt"]);
325
	$parsed = '';
326 327 328 329 330 331 332
	if (isset($_REQUEST['wikiparse']) && $_REQUEST['wikiparse'] == 'on') {
		$wikiparse = 'y';
	} elseif ($_SESSION['wysiwyg'] == 'y' && $prefs['wysiwyg_wiki_parsed'] == 'y') {
		$wikiparse = 'y';
	} else {
		$wikiparse = 'n';
	}
rjsmelo's avatar
rjsmelo committed
333
	$info['is_html'] = ! empty($_REQUEST['is_html']);
334
	$tikilib = TikiLib::lib('tiki');
rjsmelo's avatar
rjsmelo committed
335 336
	if (! empty($_REQUEST["usedTpl"])) {
		$smarty->assign('dataparsed', (($wikiparse == 'y') ? $parserlib->parse_data($_REQUEST["data"], ['absolute_links' => true, 'suppress_icons' => true]) : $_REQUEST['data']));
337
		$smarty->assign('subject', $_REQUEST["subject"]);
changi67's avatar
changi67 committed
338
		$parsed = $smarty->fetch("newsletters/" . $_REQUEST["usedTpl"]);
339
	} else {
rjsmelo's avatar
rjsmelo committed
340
		$parsed = ($wikiparse == 'y') ? $parserlib->parse_data($_REQUEST["data"], ['is_html' => $info['is_html'], 'absolute_links' => true, 'suppress_icons' => true]) : $_REQUEST['data'];
341
	}
rjsmelo's avatar
rjsmelo committed
342
	if (empty($parsed) && ! empty($_REQUEST['datatxt'])) {
343 344
		$parsed = $_REQUEST['datatxt'];
	}
345
	if (stristr($parsed, "<body") === false) {
346
		$parsed = "<html><body>$parsed</body></html>";
347
	}
348 349 350
	$previewdata = $parsed;
	if ($nl_info["allowArticleClip"] == 'y' && $nl_info["autoArticleClip"] == 'y') {
		$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
351
		$txtArticleClip = $nllib->generateTxtVersion($articleClip, $parsed);
352
		$info['datatxt'] = str_replace("~~~articleclip~~~", $txtArticleClip, $info['datatxt']);
353 354 355
		$previewdata = str_replace("~~~articleclip~~~", $articleClip, $previewdata);
	}
	$smarty->assign('previewdata', $previewdata);
changi67's avatar
changi67 committed
356
	$smarty->assign('dataparsed', $parsed);
357
	$smarty->assign('subject', $_REQUEST["subject"]);
358
	$smarty->assign('data', $_REQUEST["data"]);
359 360
	$cant = count($subscribers);
	$smarty->assign('subscribers', $cant);
361
	$smarty->assign_by_ref('subscribers_list', $subscribers);
362
	$smarty->assign_by_ref('info', $info);
rjsmelo's avatar
rjsmelo committed
363
	if (! empty($_REQUEST['replyto'])) {
364 365
		$smarty->assign('replyto', $_REQUEST['replyto']);
	}
rjsmelo's avatar
rjsmelo committed
366
	if (! empty($_REQUEST['sendfrom'])) {
367 368
		$smarty->assign('sendfrom', $_REQUEST['sendfrom']);
	}
369
}
370
$smarty->assign('emited', 'n');
rjsmelo's avatar
rjsmelo committed
371 372
if (! empty($_REQUEST['datatxt'])) {
	$txt = $_REQUEST['datatxt'];
373
}
rjsmelo's avatar
rjsmelo committed
374
if (empty($txt) && ! empty($_REQUEST["data"])) {
375
	//No txt message is explicitely provided -> Create one with the html Version & remove Wiki tags
376
	$txt = $_REQUEST["data"];
377
	$txt = $nllib->generateTxtVersion($txt, $parsed);
378
	$info["datatxt"] = $txt;
379 380
	$smarty->assign('datatxt', $txt);
	if ($nl_info["allowArticleClip"] == 'y' && $nl_info["autoArticleClip"] == 'y') {
rjsmelo's avatar
rjsmelo committed
381
		if (! isset($txtArticleClip)) {
382
			$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
383
			$txtArticleClip = $nllib->generateTxtVersion($articleClip);
384 385 386
		}
		$info['datatxt'] = str_replace("~~~articleclip~~~", $txtArticleClip, $info['datatxt']);
	}
387
}
rjsmelo's avatar
rjsmelo committed
388
if (! empty($_REQUEST['resendEditionId'])) {
389 390 391 392 393
	if (($info = $nllib->get_edition($_REQUEST['resendEditionId'])) !== false && $info['nlId'] == $_REQUEST['nlId'] && ($_REQUEST['editionId'] = $nllib->replace_edition($info['nlId'], $info['subject'], $info['data'], 0, 0, false, $info['datatxt'], $info['files'], $info['wysiwyg']))) {
		$_REQUEST['data'] = $info['data'];
		$_REQUEST['subject'] = $info['subject'];
		$_REQUEST['datatxt'] = $info['datatxt'];
		$_REQUEST['wysiwyg'] = $info['wysiwyg'];
394
		$_REQUEST['is_html'] = $info['is_html'];
395
		$_REQUEST['dataparsed'] = $info['data'];
396
		$_REQUEST['editionId'] = $nllib->replace_edition($nl_info['nlId'], $info['subject'], $info['data'], 0, 0, false, $info['datatxt'], $info['files'], $info['wysiwyg']);
397 398 399 400 401 402 403 404 405
		$resend = 'y';
	} else {
		$smarty->assign('msg', tra('Incorrect param'));
		$smarty->display('error.tpl');
		die;
	}
} else {
	$resend = 'n';
}
406

rjsmelo's avatar
rjsmelo committed
407
if (isset($_REQUEST["send"]) && ! empty($_REQUEST["sendingUniqId"]) || $resend == 'y') {
408
	check_ticket('send-newsletter');
409 410
	@set_time_limit(0);

411
	if ($resend != 'y') {
rjsmelo's avatar
rjsmelo committed
412 413 414
		if (! is_array($_SESSION["sendingUniqIds"])) {
			$_SESSION["sendingUniqIds"] = [];
		}
415

rjsmelo's avatar
rjsmelo committed
416
		if (isset($_SESSION["sendingUniqIds"][ $_REQUEST["sendingUniqId"] ])) {
417
		// Avoid sending the same newsletter again if the user reload the page
418 419 420 421 422
			print tra('Error: You can\'t send the same newsletter by refreshing this frame content.');
			die;
		} else {
			$_SESSION["sendingUniqIds"][ $_REQUEST["sendingUniqId"] ] = 1;
		}
423
	}
rjsmelo's avatar
rjsmelo committed
424

425
	$_REQUEST['begin'] = true;
426
	$nllib->send($nl_info, $_REQUEST, true, $sent, $errors, $logFileName);
427

428 429
	// use lib function to close the frame with the completion info
	$nllib->closesendframe($sent, $errors, $logFileName);
rjsmelo's avatar
rjsmelo committed
430

431
	exit; // Stop here since we are in an iframe and don't want to use smarty display
tombombadilom's avatar
tombombadilom committed
432
}
433 434

if (isset($_REQUEST['resume'])) {
435
	// for this throttle resume case the editionId, sendfrom and replyto addresses (if used) are added to the tiki-send_newsletter.php URL in the .tpl
436
	$edition_info = $nllib->get_edition($_REQUEST['resume']);
rjsmelo's avatar
rjsmelo committed
437 438 439
	// if they are set the replyto and sendfrom parameter contents are added to edition_info
	if (! empty($_REQUEST['replyto']) &&  $_REQUEST['replyto'] != "undefined") {
		$edition_info['replyto'] = $_REQUEST['replyto'];
440
	}
rjsmelo's avatar
rjsmelo committed
441 442
	if (! empty($_REQUEST['sendfrom']) &&  $_REQUEST['sendfrom'] != "undefined") {
		$edition_info['sendfrom'] = $_REQUEST['sendfrom'];
443
	}
444 445
	$nl_info = $nllib->get_newsletter($edition_info['nlId']);
	$nllib->send($nl_info, $edition_info, true, $sent, $errors, $logFileName);
rjsmelo's avatar
rjsmelo committed
446

447 448
	// use lib function to close the frame with the completion info
	$nllib->closesendframe($sent, $errors, $logFileName);
rjsmelo's avatar
rjsmelo committed
449

450
	exit; // Stop here since we are in an iframe and don't want to use smarty display
451
}
452

453
// Article Clipping
sylvieg's avatar
sylvieg committed
454
$articleClip = '';
sylvieg's avatar
sylvieg committed
455
if (isset($nl_info) && $nl_info["allowArticleClip"] == 'y' && empty($articleClip)) {
456 457 458
	if ($nl_info["autoArticleClip"] == 'y' || isset($_REQUEST["clipArticles"])) {
		$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
		// prevent clearing of keyed in info if any
rjsmelo's avatar
rjsmelo committed
459
		if (! $info["data"] && isset($_REQUEST["data"])) {
460 461
			$info["data"] = $_REQUEST["data"];
		}
rjsmelo's avatar
rjsmelo committed
462
		if (! $info["datatxt"] && isset($_REQUEST["datatxt"])) {
463 464
			$info["datatxt"] = $_REQUEST["datatxt"];
		}
rjsmelo's avatar
rjsmelo committed
465
		if (! $info["subject"] && isset($_REQUEST["subject"])) {
466
			$info["subject"] = $_REQUEST["subject"];
rjsmelo's avatar
rjsmelo committed
467
		}
468 469 470 471 472 473
	} elseif (isset($_REQUEST["articleClip"]) && $_REQUEST["articleClip"]) {
		$articleClip = $_REQUEST["articleClip"];
	}
}
$smarty->assign('articleClip', $articleClip);

474
if (isset($_REQUEST["save_only"])) {
rjsmelo's avatar
rjsmelo committed
475 476 477
	if (! isset($txt) || empty($_REQUEST['datatxt'])) {
		$txt = "";
	}
changi67's avatar
changi67 committed
478
	$smarty->assign('nlId', $_REQUEST['nlId']);
479
	$editionId = $nllib->replace_edition($_REQUEST['nlId'], $_REQUEST['subject'], $_REQUEST['data'], -1, $_REQUEST['editionId'], true, $txt, $info['files'], $_REQUEST['wysiwyg']);
480
	foreach ($info['files'] as $k => $f) {
481
		if ($f['savestate'] == 'tikitemp') {
changi67's avatar
changi67 committed
482 483
			unlink($f['path'] . '.infos');
			$info['files'][$k]['savestate'] = 'tiki';
484 485
		}
	}
hangerman's avatar
hangerman committed
486
	$info = $nllib->get_edition($editionId);
487
	$smarty->assign_by_ref('info', $info);
488
	$cookietab = 2;
489
}
rjsmelo's avatar
rjsmelo committed
490
if (! isset($_REQUEST['ed_sort_mode']) && ! isset($_REQUEST['dr_sort_mode'])) {
491 492 493 494
	$ed_sort_mode = $dr_sort_mode = 'sent_desc';
} else {
	$ed_sort_mode = $_REQUEST['ed_sort_mode'];
	$dr_sort_mode = $_REQUEST['dr_sort_mode'];
changi67's avatar
changi67 committed
495
}
496 497
$smarty->assign_by_ref('ed_sort_mode', $ed_sort_mode);
$smarty->assign_by_ref('dr_sort_mode', $dr_sort_mode);
rjsmelo's avatar
rjsmelo committed
498
if (! isset($_REQUEST['ed_offset']) && ! isset($_REQUEST['dr_offset'])) {
499
	$ed_offset = $dr_offset = 0;
lrargerich's avatar
lrargerich committed
500
} else {
501 502
	$ed_offset = $_REQUEST['ed_offset'];
	$dr_offset = $_REQUEST['dr_offset'];
lrargerich's avatar
lrargerich committed
503
}
504 505 506 507 508
$smarty->assign_by_ref('ed_offset', $ed_offset);
$smarty->assign_by_ref('dr_offset', $dr_offset);
if (isset($_REQUEST['ed_find']) && isset($_REQUEST['dr_find'])) {
	$ed_find = $_REQUEST['ed_find'];
	$dr_find = $_REQUEST['dr_find'];
lrargerich's avatar
lrargerich committed
509
} else {
510
	$ed_find = $dr_find = '';
lrargerich's avatar
lrargerich committed
511
}
512 513
$smarty->assign_by_ref('ed_find', $ed_find);
$smarty->assign_by_ref('dr_find', $dr_find);
514 515
$editions = $nllib->list_editions($_REQUEST["nlId"], $ed_offset, $maxRecords, $ed_sort_mode, $ed_find, false);
$drafts = $nllib->list_editions($_REQUEST["nlId"], $dr_offset, $maxRecords, $dr_sort_mode, $dr_find, true);
516 517 518 519 520 521 522 523
$ed_cant_pages = ceil($editions["cant"] / $maxRecords);
$dr_cant_pages = ceil($drafts["cant"] / $maxRecords);
$smarty->assign_by_ref('ed_cant_pages', $ed_cant_pages);
$smarty->assign('ed_actual_page', 1 + ($ed_offset / $maxRecords));
$smarty->assign_by_ref('dr_cant_pages', $dr_cant_pages);
$smarty->assign('dr_actual_page', 1 + ($dr_offset / $maxRecords));
if ($editions["cant"] > ($ed_offset + $maxRecords)) {
	$smarty->assign('ed_next_offset', $ed_offset + $maxRecords);
lrargerich's avatar
lrargerich committed
524
} else {
525 526 527 528 529 530
	$smarty->assign('ed_next_offset', -1);
}
if ($drafts["cant"] > ($dr_offset + $maxRecords)) {
	$smarty->assign('dr_next_offset', $dr_offset + $maxRecords);
} else {
	$smarty->assign('dr_next_offset', -1);
lrargerich's avatar
lrargerich committed
531 532
}
// If offset is > 0 then prev_offset
533 534 535 536 537 538 539
if ($ed_offset > 0) {
	$smarty->assign('ed_prev_offset', $ed_offset - $maxRecords);
} else {
	$smarty->assign('ed_prev_offset', -1);
}
if ($dr_offset > 0) {
	$smarty->assign('dr_prev_offset', $dr_offset - $maxRecords);
lrargerich's avatar
lrargerich committed
540
} else {
541
	$smarty->assign('dr_prev_offset', -1);
lrargerich's avatar
lrargerich committed
542
}
543 544 545 546 547
$smarty->assign_by_ref('editions', $editions["data"]);
$smarty->assign_by_ref('drafts', $drafts["data"]);
$smarty->assign_by_ref('cant_editions', $editions["cant"]);
$smarty->assign_by_ref('cant_drafts', $drafts["cant"]);
$smarty->assign('url', "tiki-send_newsletters.php");
548 549 550

$templates = TikiLib::lib('template')->list_templates('newsletters', 0, -1, 'name_asc', '');

551 552 553 554 555
$smarty->assign_by_ref('templates', $templates["data"]);
$tpls = $nllib->list_tpls();
if (count($tpls) > 0) {
	$smarty->assign_by_ref('tpls', $tpls);
}
rjsmelo's avatar
rjsmelo committed
556
include_once('tiki-section_options.php');
557

changi67's avatar
changi67 committed
558
ask_ticket('send-newsletter');
lphuberdeau's avatar
lphuberdeau committed
559
$wikilib = TikiLib::lib('wiki');
560 561
$plugins = $wikilib->list_plugins(true, 'editwiki');
$smarty->assign_by_ref('plugins', $plugins);
562 563
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
lrargerich's avatar
lrargerich committed
564
// Display the template
565
$smarty->assign('mid', 'tiki-send_newsletters.tpl');
566
$smarty->display("tiki.tpl");