tiki-send_newsletters.php 22.1 KB
Newer Older
lrargerich's avatar
lrargerich committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
changi67's avatar
changi67 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';
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
15
include_once ('lib/newsletters/nllib.php');
16
$auto_query_args = array('sort_mode', 'offset', 'find', 'nlId', 'cookietab', 'editionId');
17

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

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

if ( empty($_REQUEST["sendingUniqId"]) ) {
	$sendingUniqId = $tikilib->genRandomString();
	$smarty->assign('sendingUniqId', $sendingUniqId);
}

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');
mose's avatar
mose committed
46 47 48
if ($_REQUEST["nlId"]) {
	$nl_info = $nllib->get_newsletter($_REQUEST["nlId"]);
	if (!isset($_REQUEST["editionId"])) $_REQUEST["editionId"] = 0;
changi67's avatar
changi67 committed
49
	$smarty->assign('allowTxt', $nl_info['allowTxt']);
50
	$smarty->assign('allowArticleClip', $nl_info['allowArticleClip']);
51 52 53 54 55 56 57 58 59 60

	if ($prefs['newsletter_external_client'] == 'y') {
		$subscribers = $nllib->get_all_subscribers($_REQUEST["nlId"], "");
		$email_list = array();
		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
61
} else {
62
	//No newsletter selected -> Check if the textarea for the first has to be displayed
changi67's avatar
changi67 committed
63
	$smarty->assign('allowTxt', $newsletters['data'][0]['allowTxt']);
64
	$smarty->assign('allowArticleClip', $newsletters['data'][0]['allowTxt']);
lrargerich's avatar
lrargerich committed
65
}
66 67
if ($_REQUEST["editionId"]) {
	$info = $nllib->get_edition($_REQUEST["editionId"]);
68 69 70
	if (!empty($_REQUEST['resend'])) {
		$info['editionId'] = 0;
	}
71 72 73 74 75 76 77 78 79 80 81 82
} else {
	$info = array();
	$info["data"] = '';
	$info["datatxt"] = '';
	$info["subject"] = '';
	$info["editionId"] = 0;
	$info["files"] = array();
	$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
83
// Display to newsletter txtarea or not depending on the preferences
changi67's avatar
changi67 committed
84
$showBoxCheck = "
hangerman's avatar
hangerman committed
85 86
	<script type='text/javascript'>
	<!--
87
	function checkNewsletterTxtArea(nlIndex){
hangerman's avatar
hangerman committed
88
	browser();
89 90 91
	var allowTxt = new Array();
	var allowArticleClip = new Array();
	";
92
for ($i = 0, $tmp_count = count($newsletters['data']); $i < $tmp_count; $i++) {
93 94 95 96 97 98
$showBoxCheck .= "allowTxt[$i] = '" . $newsletters['data'][$i]['allowTxt'] . "';
	allowArticleClip[$i] = '" . $newsletters['data'][$i]['allowArticleClip'] . "';
	";
}
// allowTxt
$showBoxCheck .= "	if (document.getElementById('txtcol1').style.display=='none' && allowTxt[nlIndex] == 'y'){";
changi67's avatar
changi67 committed
99
if (preg_match("/gecko/i", $_SERVER['HTTP_USER_AGENT'])) {
hangerman's avatar
hangerman committed
100 101
	$showBoxCheck.= "document.getElementById('txtcol1').style.display='table-cell';";
	$showBoxCheck.= "document.getElementById('txtcol2').style.display='table-cell';";
changi67's avatar
changi67 committed
102
} else {
hangerman's avatar
hangerman committed
103 104
	$showBoxCheck.= "document.getElementById('txtcol1').style.display='inline';	";
	$showBoxCheck.= "document.getElementById('txtcol2').style.display='inline';";
changi67's avatar
changi67 committed
105 106
};
$showBoxCheck.= "
107
    	}else if (allowTxt[nlIndex] == 'n') {
hangerman's avatar
hangerman committed
108 109
	document.getElementById('txtcol1').style.display='none';
	document.getElementById('txtcol2').style.display='none';
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    	}";
// allowArticleClip
$showBoxCheck .= "	if (document.getElementById('clipcol1').style.display=='none' && allowArticleClip[nlIndex] == 'y'){";
if (preg_match("/gecko/i", $_SERVER['HTTP_USER_AGENT'])) {
	$showBoxCheck.= "document.getElementById('clipcol1').style.display='table-cell';";
	$showBoxCheck.= "document.getElementById('clipcol2').style.display='table-cell';";
} else {
	$showBoxCheck.= "document.getElementById('clipcol1').style.display='inline';	";
	$showBoxCheck.= "document.getElementById('clipcol2').style.display='inline';";
};
$showBoxCheck.= "
    	}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
127 128 129 130
	}
	-->
	</script>
	";
changi67's avatar
changi67 committed
131
$smarty->assign('showBoxCheck', $showBoxCheck);
132
if (isset($_REQUEST["remove"])) {
133 134
	$access->check_authenticity();
	$nllib->remove_edition($_REQUEST["nlId"], $_REQUEST["remove"]);
lrargerich's avatar
lrargerich committed
135
}
136

137
$editlib = TikiLib::lib('edit');
138 139
// wysiwyg decision
include_once ('lib/setup/editmode.php');
140 141 142 143 144 145 146 147 148 149

// Handles switching editor modes
if (isset($_REQUEST['mode_normal']) && $_REQUEST['mode_normal']=='y') {
	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"];
	}
150 151 152 153 154 155
	$info['wysiwyg'] = 'n';
	$info['is_html'] = false;
	unset($_REQUEST['is_html']);
	$_REQUEST['preview'] = 'y';
	$_REQUEST["data"] = $info["data"];

156 157 158 159
} elseif (isset($_REQUEST['mode_wysiwyg']) && $_REQUEST['mode_wysiwyg']=='y') {
	// 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"]);
160 161 162
	$info['wysiwyg'] = 'y';
	$_REQUEST['preview'] = 'y';
	$_REQUEST["data"] = $info["data"];
163 164
}

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

173
if (isset($_REQUEST["templateId"]) && $_REQUEST["templateId"] > 0 && (!isset($_REQUEST['previousTemplateId']) || $_REQUEST['previousTemplateId'] != $_REQUEST['templateId'])) {
174 175
	global $templateslib; require_once 'lib/templates/templateslib.php';
	$template_data = $templateslib->get_template($_REQUEST["templateId"]);
176
	$_REQUEST["data"] = $template_data["content"];
177 178 179 180 181
	if ($templateslib->template_is_in_section($_REQUEST['templateId'], 'wiki_html') ) {
		$_REQUEST['is_html'] = 'on';
		$_REQUEST['wysiwyg'] ='y';
	}
	if (isset($_SESSION['wysiwyg']) && $_SESSION['wysiwyg'] == 'y' || $_REQUEST['wysiwyg'] === 'y') {
182
		$_REQUEST['data'] = $tikilib->parse_data($_REQUEST['data'], array('is_html'=>$info['is_html'], 'absolute_links' => true, 'suppress_icons' => true));
183
	}
184
	$_REQUEST["preview"] = 1;
185
	$smarty->assign("templateId", $_REQUEST["templateId"]);
lrargerich's avatar
lrargerich committed
186
}
changi67's avatar
changi67 committed
187
$newsletterfiles = array();
188 189
if (isset($_REQUEST['newsletterfile'])) {
	$newsletterfiles_post = isset($_REQUEST['newsletterfile']) && is_array($_REQUEST['newsletterfile']) ? $_REQUEST['newsletterfile'] : array();
190
	foreach ($newsletterfiles_post as $k => $id) {
191 192 193 194 195 196 197
		$f = array();
		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;
		} else if ((int)$_REQUEST['nlId'] > 0) {
198
			foreach ($info['files'] as $f) {
199 200 201 202
				if ($f['id'] == (int)$id) {
					$newsletterfiles[] = $f;
					break;
				}
203 204 205
			}
		}
	}
206 207
} else {
	$newsletterfiles = $info['files'];
208 209
}
if (!empty($_FILES) && !empty($_FILES['newsletterfile'])) {
210
	foreach ($_FILES['newsletterfile']['name'] as $i => $v) {
changi67's avatar
changi67 committed
211
		if ($_FILES['newsletterfile']['error'][$i] == UPLOAD_ERR_OK) {
212 213 214 215 216 217 218 219
			$newsletterfiles[] = array(
				'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',
			);
changi67's avatar
changi67 committed
220
		} else {
221
			$smarty->assign('upload_err_msg', tra('A problem occurred during file uploading') . '<br />' . tra('File which was causing trouble was at rank') . '&nbsp;' . ($i + 1) . '<br />' . tra('The error was:') . '&nbsp;<strong>' . $tikilib->uploaded_file_error($_FILES['newsletterfile']['error'][$i]) . '</strong>');
222
		}
223 224
	}
}
sylvieg's avatar
sylvieg committed
225
$_REQUEST['files'] = $info['files'] = $newsletterfiles;
226
foreach ($info['files'] as $k => $newsletterfile) {
227 228
	if ($newsletterfile['savestate'] == 'phptmp') {
		// move it to temp
changi67's avatar
changi67 committed
229
		$tmpfnamekey = md5(rand() . time() . $newsletterfile['path'] . $newsletterfile['name'] . $newsletterfile['type']);
230
		$tmpfname = $prefs['tmpDir'] . '/newsletterfile-' . $tmpfnamekey;
231
		if (move_uploaded_file($newsletterfile['path'], $tmpfname)) {
changi67's avatar
changi67 committed
232 233 234 235 236
			$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]));
237 238 239 240
		}
	}
}
$smarty->assign('preview', 'n');
241 242
if (isset($_REQUEST["preview"])) {
	$smarty->assign('preview', 'y');
243 244 245 246 247
	if (isset($_REQUEST["subject"])) {
		$info["subject"] = $_REQUEST["subject"];
	} else {
		$info["subject"] = '';
	}
mose's avatar
mose committed
248 249 250 251 252
	if (isset($_REQUEST["data"])) {
		$info["data"] = $_REQUEST["data"];
	} else {
		$info["data"] = '';
	}
changi67's avatar
changi67 committed
253 254
	if (isset($_REQUEST['wikiparse']) && $_REQUEST['wikiparse'] == 'on') $info['wikiparse'] = 'y';
	else $info['wikiparse'] = 'n';
255
	if (!empty($_REQUEST["datatxt"])) {
changi67's avatar
changi67 committed
256
		$info["datatxt"] = $_REQUEST["datatxt"];
hangerman's avatar
hangerman committed
257
		//For the hidden input
changi67's avatar
changi67 committed
258
		$smarty->assign('datatxt', $_REQUEST["datatxt"]);
hangerman's avatar
hangerman committed
259 260 261
	} else {
		$info["datatxt"] = '';
	}
262
	if (!empty($_REQUEST["usedTpl"])) {
263
		$smarty->assign('dataparsed', (($info['wikiparse'] == 'y') ? $tikilib->parse_data($info["data"], array('absolute_links' => true, 'suppress_icons' => true)) : $info['data']));
264
		$smarty->assign('subject', $info["subject"]);
changi67's avatar
changi67 committed
265 266 267 268
		$info["dataparsed"] = $smarty->fetch("newsletters/" . $_REQUEST["usedTpl"]);
		if (stristr($info['dataparsed'], "<body") === false) {
			$info['dataparsed'] = "<html><body>" . $info['dataparsed'] . "</body></html>";
		}
269
		$smarty->assign("usedTpl", $_REQUEST["usedTpl"]);
mose's avatar
mose committed
270
	} else {
271 272 273 274 275 276 277 278 279 280 281
		$info['dataparsed'] = '<html><body>';
		if ($info['wikiparse'] === 'y') {
			$data = $info['data'];
			$info['dataparsed'] .= $tikilib->parse_data($data, array('absolute_links' => true, 'suppress_icons' => true,'is_html' => $info['is_html']));
			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>';
mose's avatar
mose committed
282
	}
283 284 285
	if (!empty($_REQUEST['replyto'])) {
		$smarty->assign('replyto', $_REQUEST['replyto']);
	}
286
	$previewdata = $info['dataparsed'];
287
	$parsed = $info['dataparsed'];
288 289
	if ($nl_info["allowArticleClip"] == 'y' && $nl_info["autoArticleClip"] == 'y') {
		$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
290
		$txtArticleClip = $nllib->generateTxtVersion($articleClip);
291
		$info['datatxt'] = str_replace("~~~articleclip~~~", $txtArticleClip, $info['datatxt']);
292 293
		$previewdata = str_replace("~~~articleclip~~~", $articleClip, $previewdata);
	}
294
	$smarty->assign_by_ref('info', $info);
295
	$smarty->assign('previewdata', $previewdata);
296

297 298 299
	$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');
300 301

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

391
if ( isset($_REQUEST["send"]) && ! empty($_REQUEST["sendingUniqId"]) || $resend == 'y' ) {
392
	check_ticket('send-newsletter');
393 394
	@set_time_limit(0);

395 396 397
	if ($resend != 'y') {
		if ( ! is_array($_SESSION["sendingUniqIds"]) )
			$_SESSION["sendingUniqIds"] = array();
398

399
		if ( isset( $_SESSION["sendingUniqIds"][ $_REQUEST["sendingUniqId"] ] ) ) {
400
		// Avoid sending the same newsletter again if the user reload the page
401 402 403 404 405
			print tra('Error: You can\'t send the same newsletter by refreshing this frame content.');
			die;
		} else {
			$_SESSION["sendingUniqIds"][ $_REQUEST["sendingUniqId"] ] = 1;
		}
406
	}
407
	
408
	$_REQUEST['begin'] = true;
409
	$nllib->send($nl_info, $_REQUEST, true, $sent, $errors, $logFileName);
410 411 412 413

	$nb_sent = count($sent);
	$nb_errors = count($errors);

414
	$msg = '<h4>' . sprintf(tra('Newsletter successfully sent to %s users.'), $nb_sent) . '</h4>';
415
	if ( $nb_errors > 0 )
416
		$msg .= "\n" . '<font color="red">' . '(' . sprintf(tra('There was %s errors.'), $nb_errors) . ')' . '</font><br />';
417 418

	// If logfile exists and if it is reachable from the web browser, add a download link
419
	if ( !empty($logFileName) && $logFileName[0] != '/' && $logFileName[0] != '.' )
420 421
		$smarty->assign('downloadLink', $logFileName);

422
	echo str_replace("'", "\\'", $msg);
423 424 425
	echo $smarty->fetch('send_newsletter_footer.tpl');

	$smarty->assign('sent', $nb_sent);
426 427 428
	$smarty->assign('emited', 'y');
	if (count($errors) > 0) {
		$smarty->assign_by_ref('errors', $errors);
429
	}
430 431
	unset($_SESSION["sendingUniqIds"][ $_REQUEST["sendingUniqId"] ]);
	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 435 436 437 438 439

if (isset($_REQUEST['resume'])) {
	$edition_info = $nllib->get_edition($_REQUEST['resume']);
	$nl_info = $nllib->get_newsletter($edition_info['nlId']);
	$nllib->send($nl_info, $edition_info, true, $sent, $errors, $logFileName);
	exit;
}
440

441
// Article Clipping
sylvieg's avatar
sylvieg committed
442
$articleClip = '';
sylvieg's avatar
sylvieg committed
443
if (isset($nl_info) && $nl_info["allowArticleClip"] == 'y' && empty($articleClip)) {
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	if ($nl_info["autoArticleClip"] == 'y' || isset($_REQUEST["clipArticles"])) {
		$articleClip = $nllib->clip_articles($_REQUEST["nlId"]);
		// prevent clearing of keyed in info if any
		if (!$info["data"] && isset($_REQUEST["data"])) {
			$info["data"] = $_REQUEST["data"];
		}
		if (!$info["datatxt"] && isset($_REQUEST["datatxt"])) {
			$info["datatxt"] = $_REQUEST["datatxt"];
		}
		if (!$info["subject"] && isset($_REQUEST["subject"])) {
			$info["subject"] = $_REQUEST["subject"];
		}		
	} elseif (isset($_REQUEST["articleClip"]) && $_REQUEST["articleClip"]) {
		$articleClip = $_REQUEST["articleClip"];
	}
}
$smarty->assign('articleClip', $articleClip);

462
if (isset($_REQUEST["save_only"])) {
changi67's avatar
changi67 committed
463 464
	if (!isset($txt) || empty($_REQUEST['datatxt'])) $txt = "";
	$smarty->assign('nlId', $_REQUEST['nlId']);
465
	$editionId = $nllib->replace_edition($_REQUEST['nlId'], $_REQUEST['subject'], $_REQUEST['data'], -1, $_REQUEST['editionId'], true, $txt, $info['files'], $_REQUEST['wysiwyg']);
466
	foreach ($info['files'] as $k => $f) {
467
		if ($f['savestate'] == 'tikitemp') {
changi67's avatar
changi67 committed
468 469
			unlink($f['path'] . '.infos');
			$info['files'][$k]['savestate'] = 'tiki';
470 471
		}
	}
hangerman's avatar
hangerman committed
472
	$info = $nllib->get_edition($editionId);
473
	$smarty->assign_by_ref('info', $info);
474
	$cookietab = 2;
475
}
476 477 478 479 480
if (!isset($_REQUEST['ed_sort_mode']) && !isset($_REQUEST['dr_sort_mode'])) {
	$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
481
}
482 483 484 485
$smarty->assign_by_ref('ed_sort_mode', $ed_sort_mode);
$smarty->assign_by_ref('dr_sort_mode', $dr_sort_mode);
if (!isset($_REQUEST['ed_offset']) && !isset($_REQUEST['dr_offset'])) {
	$ed_offset = $dr_offset = 0;
lrargerich's avatar
lrargerich committed
486
} else {
487 488
	$ed_offset = $_REQUEST['ed_offset'];
	$dr_offset = $_REQUEST['dr_offset'];
lrargerich's avatar
lrargerich committed
489
}
490 491 492 493 494
$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
495
} else {
496
	$ed_find = $dr_find = '';
lrargerich's avatar
lrargerich committed
497
}
498 499
$smarty->assign_by_ref('ed_find', $ed_find);
$smarty->assign_by_ref('dr_find', $dr_find);
500 501
$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);
502 503 504 505 506 507 508 509
$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
510
} else {
511 512 513 514 515 516
	$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
517 518
}
// If offset is > 0 then prev_offset
519 520 521 522 523 524 525
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
526
} else {
527
	$smarty->assign('dr_prev_offset', -1);
lrargerich's avatar
lrargerich committed
528
}
529 530 531 532 533
$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");
sylvieg's avatar
sylvieg committed
534
if (!empty($tiki_p_use_content_templates) && $tiki_p_use_content_templates == 'y') {
535 536
	global $templateslib; require_once 'lib/templates/templateslib.php';
	$templates = $templateslib->list_templates('newsletters', 0, -1, 'name_asc', '');
lrargerich's avatar
lrargerich committed
537
}
538 539 540 541 542 543
$smarty->assign_by_ref('templates', $templates["data"]);
$tpls = $nllib->list_tpls();
if (count($tpls) > 0) {
	$smarty->assign_by_ref('tpls', $tpls);
}
include_once ('tiki-section_options.php');
544
setcookie('tab', $cookietab);
545
$smarty->assign('cookietab', $_REQUEST['cookietab']);
changi67's avatar
changi67 committed
546
ask_ticket('send-newsletter');
lphuberdeau's avatar
lphuberdeau committed
547
$wikilib = TikiLib::lib('wiki');
548 549
$plugins = $wikilib->list_plugins(true, 'editwiki');
$smarty->assign_by_ref('plugins', $plugins);
550 551
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
lrargerich's avatar
lrargerich committed
552
// Display the template
553
$smarty->assign('mid', 'tiki-send_newsletters.tpl');
554
$smarty->display("tiki.tpl");
555