PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_sm2emailmarketing/sm2emailmarketing.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 1311 lines | 1005 code | 171 blank | 135 comment | 239 complexity | ed843758a76739f9f07e3bd4cdc2dcf1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. // $Id: sm2emailmarketing.php,v 1.34 2008/04/21 08:00:02 sm2tony Exp $
  3. /**
  4. * sm2emailmarketing - Email Marketing Newsletter
  5. *
  6. * Joomla! Newsletter Component that supports multiple list mailing and message queueing
  7. *
  8. * @package Joomla!
  9. * @subpackage com_sm2emailmarketing
  10. * @copyright Copyright (C) 2006 SM2 / All Rights Reserved
  11. * @license commercial
  12. * @author SM2 <info@sm2joomla.com>
  13. */
  14. /**
  15. * Makes sure this is included by a parent file
  16. */
  17. defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
  18. // include required files
  19. require_once($mainframe->getPath('class'));
  20. sm2emailmarketingConfig($option);
  21. global $emErrorHandler, $sm2emailmarketingConfig;
  22. require_once ($mosConfig_absolute_path . '/administrator/components/'.$option.'/includes/errorhandler.admin.php');
  23. $emErrorHandler = new emError;
  24. // include language file, or default to english
  25. if (file_exists ($mosConfig_absolute_path . '/components/'.$option.'/languages/'.$mosConfig_lang.'.php')) {
  26. include_once ($mosConfig_absolute_path . '/components/'.$option.'/languages/'.$mosConfig_lang.'.php');
  27. } else {
  28. include_once ($mosConfig_absolute_path . '/components/'.$option.'/languages/english.php');
  29. } // end if
  30. switch( $task ) {
  31. case 'subscribe': //note that this is also does the first step of the unsubscribe
  32. sm2emailmarketingSubscribe();
  33. break;
  34. case 'unsubscribe':
  35. sm2emailmarketingUnsubscribe();
  36. break;
  37. case 'confirm':
  38. sm2emailmarketingConfirm();
  39. break;
  40. case 'image': //used for stats tracking
  41. sm2emailmarketingImage();
  42. break;
  43. case 'showarchivelist':
  44. default:
  45. sm2emShowArchiveList($option);
  46. break;
  47. case 'showarchivemessage':
  48. sm2emShowMessageArchive($option);
  49. break;
  50. case 'forward':
  51. sm2emForward($option);
  52. break;
  53. case 'sendforward':
  54. sm2emSendForward($option);
  55. break;
  56. }
  57. function sm2emailmarketingSubscribe(){
  58. global $database, $mosConfig_live_site, $option,
  59. $mosConfig_mailfrom, $mosConfig_fromname, $my, $sm2emailmarketingConfig;
  60. $confirmreq = $sm2emailmarketingConfig->get('require_confirmation',0); // def, get, set
  61. $from = $sm2emailmarketingConfig->get('confirm_fromemail',$mosConfig_mailfrom);
  62. $fromname = $sm2emailmarketingConfig->get('confirm_fromname',$mosConfig_fromname);
  63. $message = $sm2emailmarketingConfig->get('confirm_text','[CONFIRM]');
  64. $message_unsubscribe = $sm2emailmarketingConfig->get('unsubscribe_text', _EMKTG_UNSUBSCRIBE_CLICKTOCONFIRM);
  65. // see if we need to do spoofchecking
  66. if ($sm2emailmarketingConfig->get('spoofcheck', 0)) {
  67. josSpoofCheck(true, $option);
  68. }
  69. $name = mosGetParam($_POST, 'name');
  70. $email = mosGetParam($_POST, 'email');
  71. $html = mosGetParam($_POST, 'html');
  72. $status = mosGetParam($_POST, 'status');
  73. $lists = mosGetParam($_POST, 'lists');
  74. //check that they are not a registered user (check for email)
  75. //if so advise that they need to login.
  76. if (empty($my->id)) {
  77. $sql = 'SELECT id FROM #__users WHERE email LIKE '.$database->Quote($email);
  78. $database->setQuery($sql);
  79. $rows=null;
  80. $database->loadObject($rows);
  81. if (is_object($rows)) {
  82. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_ISUSER);
  83. return;
  84. }
  85. //check if email is in the system
  86. $sql = 'SELECT subscriberid AS id, name, email, receive_html AS format, unsubscribe_reason FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  87. } else {
  88. //check if email is in the system
  89. $sql = 'SELECT u.id AS id, u.name, u.email, eu.receive_html AS format, eu.unsubscribe_reason'
  90. .' FROM #__users u INNER JOIN #__emktg_user eu ON (u.id=eu.id) WHERE u.id='.(int)$my->id;
  91. }
  92. $database->setQuery($sql);
  93. $rows=null;
  94. $database->loadObject($rows);
  95. $found = true;
  96. if (!is_object($rows)) {
  97. $found = false;
  98. } else if (empty($rows->id)) {
  99. $found = false;
  100. }
  101. // check first that they are allowed to subscribe
  102. if ($found && $rows->unsubscribe_reason > 1) {
  103. $msgVar = '_EMKTG_RESUBSCRIBE_ERROR_'.(int)$rows->unsubscribe_reason;
  104. if (defined($msgVar)) {
  105. sm2emailmarketingMessage(constant($msgVar));
  106. return;
  107. }
  108. }
  109. if (!$found) { // email is not in the database
  110. if ($status ==0) {// they want to unsubscribe
  111. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_NOTFOUND);
  112. } else { //they are trying to subscribe
  113. if (!$confirmreq || $confirmreq == 0) { //no confirmation needed
  114. if (empty($my->id)) {
  115. $sql = 'INSERT INTO #__emktg_subscriber ( name , email , receive_html , subscribe_date, confirmed)'
  116. .' VALUES('.$database->Quote($name).', '.$database->Quote($email).', '.(int)$html.', NOW(), 1)';
  117. } else {
  118. $sql = 'INSERT INTO #__emktg_user ( id, receive_html , subscribe_date, confirmed)'
  119. .' VALUES('.(int)$my->id.', '.(int)$html.', NOW(), 1)';
  120. }
  121. $database->setQuery($sql);
  122. $database->Query();
  123. if (empty($my->id)) {
  124. $id = $database->insertid(); //subscriberid
  125. if ($id) {
  126. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_EMAILADDED);
  127. $id = -$id;
  128. }
  129. } else {
  130. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_EMAILADDED);
  131. }
  132. //add to the mailing lists they chose
  133. sm2emailmarketingMailingSubscribe($id, $lists);
  134. } else {
  135. if (empty($my->id)) {
  136. $sql = 'INSERT INTO #__emktg_subscriber ( name , email , receive_html , subscribe_date, confirmed)'
  137. .' VALUES('.$database->Quote($name).', '.$database->Quote($email).', '.(int)$html.', NOW(), 0)';
  138. } else {
  139. $sql = 'INSERT INTO #__emktg_user ( id, receive_html , subscribe_date, confirmed)'
  140. .' VALUES('.(int)$my->id.', '.(int)$html.', NOW(), 0)';
  141. }
  142. $database->setQuery($sql);
  143. $database->Query();
  144. if (empty($my->id)) {
  145. $id = $database->insertid();
  146. $id = -$id;
  147. } else {
  148. $row = new mosUser($database);
  149. $row->load($my->id);
  150. $id = $row->id;
  151. $email = $row->email;
  152. }
  153. //add to the mailing lists they chose
  154. sm2emailmarketingMailingSubscribe($id, $lists);
  155. $code = $id.$email;
  156. $code = md5($code);
  157. $mail = mosCreateMail($from, $fromname, _EMKTG_SUBSCRIBE_CONFIRMATION, '');
  158. $confirmUrl = 'index.php?option='.$option.'&task=confirm&email='.$email.'&code='.$code;
  159. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  160. $confirmUrl = sefRelToAbs($confirmUrl);
  161. } else {
  162. $confirmUrl = $mosConfig_live_site.'/'.$confirmUrl;
  163. }
  164. if (!$html || $html == 0) { //send plain text email
  165. $mail->IsHTML(false);
  166. $message = str_replace("\n", '', $message);
  167. $message = str_replace('<br />', "\n", $message);
  168. $message = strip_tags($message, '<a>');
  169. $mail->Body = str_replace('[CONFIRM]', $confirmUrl, $message);
  170. } else {
  171. $mail->IsHTML(true);
  172. $mail->Body = str_replace('[CONFIRM]', '<br /><a href="'.$confirmUrl.'">'._EMKTG_SUBSCRIBE_CLICKTOCONFIRM.'</a>', $message);
  173. }
  174. $mail->AddAddress($email, $name);
  175. if (!$mail->Send()) {
  176. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_ERRORSENDINGMSG);
  177. //exit;
  178. } else {
  179. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_MSGSENDSUCCESS);
  180. $mail->ClearAllRecipients();
  181. }
  182. }
  183. }
  184. } else {// email is already in database
  185. if ($status ==0) {// they want to unsubscribe
  186. //send an email confirming that they do actually want to unsubscribe.
  187. $mail = mosCreateMail($from, $fromname, _EMKTG_UNSUBSCRIBE_CONFIRMATION, '');
  188. $name = $rows->name;
  189. $email = $rows->email;
  190. $format = $rows->format;
  191. $lists = mosGetParam($_POST, 'lists');
  192. if (is_array($lists)) {
  193. $lists = implode('-', $lists);
  194. }
  195. if (empty($my->id)) {
  196. $rows->id = -$rows->id;
  197. }
  198. $code = $rows->id.$rows->email;
  199. $code = md5($code);
  200. $unSubscribeUrl = 'index.php?option='.$option.'&task=unsubscribe&email='.$email.'&code='.$code.'&lists='.$lists;
  201. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  202. $unSubscribeUrl = sefRelToAbs($unSubscribeUrl);
  203. } else {
  204. $unSubscribeUrl = $mosConfig_live_site.'/'.$unSubscribeUrl;
  205. }
  206. if ($format) {
  207. $mail->IsHTML(true);
  208. $mail->Body = '<a href="'.$unSubscribeUrl.'">'.$message_unsubscribe.'</a>';
  209. } else {
  210. $mail->IsHTML(false);
  211. $mail->Body = $unSubscribeUrl.' '.$message_unsubscribe;
  212. }
  213. $mail->AddAddress($email, $name);
  214. if (!$mail->Send()) {
  215. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_ERRORSENDINGMSG);
  216. exit;
  217. } else {
  218. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_MSGSENDSUCCESS);
  219. $mail->ClearAllRecipients();
  220. }
  221. } else { // they want to subscribe
  222. // check to see if we should automatically 'confirm' the user
  223. // if not auto confirm then will need to send them an email
  224. if (!$confirmreq || $confirmreq == 0) { // no confirmation needed
  225. if (empty($my->id)) {
  226. $sql = 'UPDATE #__emktg_subscriber SET name='.$database->Quote($name).', receive_html='.(int)$html.', subscribe_date=NOW(), unsubscribe_date=NULL, unsubscribe_reason=0, confirmed=1 WHERE email LIKE '.$database->Quote($email);
  227. } else {
  228. $sql = 'UPDATE #__emktg_user SET receive_html='.(int)$html.', subscribe_date=NOW(), unsubscribe_date=NULL, unsubscribe_reason=0, confirmed=1 WHERE id='.(int)$my->id;
  229. }
  230. $database->setQuery($sql);
  231. $added = $database->Query();
  232. //add to the mailing lists they chose
  233. if (empty($my->id)) {
  234. $sql = 'SELECT subscriberid AS id FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  235. $database->setQuery($sql);
  236. $id = -$database->loadResult();
  237. } else {
  238. $id = $my->id;
  239. }
  240. sm2emailmarketingMailingSubscribe($id, $lists);
  241. if ($added) {
  242. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_EMAILADDED);
  243. }
  244. } else { //need to send them a confirmation email
  245. //add them to the database with confirm = 0
  246. if (empty($my->id)) {
  247. $sql = 'UPDATE #__emktg_subscriber SET name='.$database->Quote($name).', receive_html='.(int)$html.', subscribe_date=NOW(), unsubscribe_date=NULL, unsubscribe_reason=0, confirmed=0 WHERE email LIKE '.$database->Quote($email);
  248. } else {
  249. $sql = 'UPDATE #__emktg_user SET receive_html='.(int)$html.', subscribe_date=NOW(), unsubscribe_date=NULL, unsubscribe_reason=0, confirmed=0 WHERE id='.(int)$my->id;
  250. }
  251. $database->setQuery($sql);
  252. $added = $database->Query();
  253. //add to the mailing lists they chose
  254. if (empty($my->id)) {
  255. $sql = 'SELECT subscriberid AS id FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  256. $database->setQuery($sql);
  257. $id = -$database->loadResult();
  258. } else {
  259. $id = $my->id;
  260. }
  261. sm2emailmarketingMailingSubscribe($id, $lists);
  262. $code = $id.$rows->email;
  263. $code = md5($code);
  264. $body=$message;
  265. $mail = mosCreateMail($from, $fromname, _EMKTG_SUBSCRIBE_CONFIRMATION, '');
  266. $confirmUrl = $mosConfig_live_site.'/index.php?option='.$option.'&task=confirm&email='.$email.'&code='.$code;
  267. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  268. $confirmUrl = sefRelToAbs($confirmUrl);
  269. }
  270. if(!$html || $html == 0) { //send plain text email
  271. $mail->IsHTML(false);
  272. $body = str_replace("\n", '', $body);
  273. $body = str_replace('<br />', "\n", $body);
  274. $body = strip_tags($body, '<a>');
  275. $mail->Body = str_replace('[CONFIRM]',$confirmUrl, $body);
  276. } else {
  277. $mail->IsHTML(true);
  278. $mail->Body = str_replace('[CONFIRM]','<br /><a href="'.$confirmUrl.'">'._EMKTG_SUBSCRIBE_CLICKTOCONFIRM.'</a>', $body);
  279. }
  280. $mail->AddAddress($rows->email, $rows->name);
  281. if(!$mail->Send()){
  282. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_ERRORSENDINGMSG);
  283. exit;
  284. } else {
  285. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_MSGSENDSUCCESS);
  286. $mail->ClearAllRecipients();
  287. }
  288. }
  289. }
  290. }
  291. } // sm2emailmarketingSubscribe()
  292. function sm2emailmarketingUnsubscribe(){
  293. global $database, $my, $sm2emailmarketingConfig;
  294. $quick_registered_unsubscribe = $sm2emailmarketingConfig->get('quick_registered_unsubscribe', 0);
  295. //grab the values from link
  296. $email = mosGetParam($_GET, 'email');
  297. $code = mosGetParam($_GET, 'code');
  298. //check if email is in the system
  299. if (empty($my->id)) {
  300. $sql = 'SELECT subscriberid AS id, email FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  301. } else {
  302. $sql = 'SELECT u.id AS id, u.email AS format'
  303. .' FROM #__users u INNER JOIN #__emktg_user eu ON (u.id=eu.id) WHERE eu.id='.(int)$my->id;
  304. }
  305. $database->setQuery($sql);
  306. $rows=null;
  307. $database->loadObject($rows);
  308. $found = true;
  309. if (!is_object($rows)) {
  310. $found = false;
  311. } else if (empty($rows->id)) {
  312. $found = false;
  313. }
  314. if (!$found) { //couldn't find the email address. check that they are not a registered subscriber
  315. $sql = 'SELECT id, email FROM #__users WHERE email LIKE '.$database->Quote($email);
  316. $database->setQuery($sql);
  317. $rows=null;
  318. $database->loadObject($rows);
  319. $userFound = true;
  320. if (!is_object($rows)) {
  321. $userFound = false;
  322. } else if (empty($rows->id)) {
  323. $userFound = false;
  324. }
  325. if (!$userFound) {
  326. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_NOTFOUND);
  327. } else { // they are a registered user
  328. if ($quick_registered_unsubscribe) {
  329. $found = true;
  330. } else {
  331. sm2emailmarketingMessage(_EMKTG_SUBSCRIBE_ISUSER);
  332. }
  333. }
  334. } else if (empty($my->id)) {
  335. $rows->id = -$rows->id;
  336. }
  337. if (!$found) return;
  338. $checkcode = $rows->id.$rows->email;
  339. $checkcode = md5($checkcode);
  340. if($checkcode != $code) {
  341. sm2emailmarketingMessage(_EMKTG_CONFIRMATION_SECURITYCHECKFAILED);
  342. } else { //all good let's remove them from the database
  343. $lists = mosGetParam($_GET, 'lists');
  344. if($lists) {
  345. $lists = explode('-', $lists);
  346. } else {
  347. $lists = 0;
  348. }
  349. //remove them from the mailinglists
  350. sm2emailmarketingMailingUnsubscribe($rows->id, $email, $lists);
  351. }
  352. } // sm2emailmarketingUnsubscribe()
  353. function sm2emailmarketingConfirm(){
  354. global $database, $my;
  355. //grab the values from the link
  356. $email = mosGetParam($_GET, 'email');
  357. $code = mosGetParam($_GET, 'code');
  358. //check if email is in the system
  359. if (empty($my->id)) {
  360. $sql = 'SELECT subscriberid AS id, name, email, receive_html AS format FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  361. } else {
  362. $sql = 'SELECT u.id, u.name, u.email, eu.receive_html AS format'
  363. .' FROM #__users u INNER JOIN #__emktg_user eu ON (u.id=eu.id) WHERE eu.id='.(int)$my->id;
  364. }
  365. $database->setQuery($sql);
  366. $rows=null;
  367. $database->loadObject($rows);
  368. $found = true;
  369. if (!is_object($rows)) {
  370. $found = false;
  371. } else if (empty($rows->id)) {
  372. $found = false;
  373. }
  374. if(!$found){ //couldn't find the email address.
  375. sm2emailmarketingMessage(_EMKTG_CONFIRMATION_NOTFOUND);
  376. exit;
  377. } else {
  378. if (empty($my->id)) {
  379. $id = -$rows->id;
  380. } else {
  381. $id = $my->id;
  382. }
  383. $checkcode = $id.$rows->email;
  384. $checkcode = md5($checkcode);
  385. if($checkcode != $code) {
  386. sm2emailmarketingMessage(_EMKTG_CONFIRMATION_SECURITYCHECKFAILED);
  387. } else { //all good, let's confirm them
  388. if (empty($my->id)) {
  389. $sql = 'UPDATE #__emktg_subscriber SET confirmed=1 WHERE subscriberid = '.(int)$rows->id;
  390. } else {
  391. $sql = 'UPDATE #__emktg_user SET confirmed=1 WHERE id = '.(int)$rows->id;
  392. }
  393. $database->setQuery($sql);
  394. $confirmed = $database->Query();
  395. if($confirmed) {
  396. sm2emailmarketingMessage(_EMKTG_CONFIRMATION_CONFIRMSUCCESS, 'success');
  397. } else {
  398. sm2emailmarketingMessage(_EMKTG_CONFIRMATION_CONFIRMERROR);
  399. }
  400. }
  401. }
  402. }
  403. // This gives admins the ability to change the formatting for their errors:
  404. function sm2emailmarketingMessage($message, $type='error') {
  405. if($type=='success') $class = 'success';
  406. else $class = 'error';
  407. echo '<p class="'.$class.'">'.$message.'</p>';
  408. }
  409. function sm2emailmarketingMailingSubscribe($id, $lists=null) {
  410. global $database, $my;
  411. if(empty($lists)) {
  412. return;
  413. } else {
  414. foreach($lists as $list) {
  415. if ($id < 0) {
  416. $sql = 'INSERT IGNORE INTO #__emktg_list_subscriber ( listid, subscriberid ) VALUES ('.(int)$list.', '.(int)$id.')';
  417. } else {
  418. $sql = 'INSERT IGNORE INTO #__emktg_list_user ( listid, id ) VALUES ('.(int)$list.', '.(int)$id.')';
  419. }
  420. $database->setQuery($sql);
  421. $database->Query();
  422. }
  423. }
  424. }
  425. function sm2emailmarketingMailingUnsubscribe($id, $email, $lists) {
  426. global $database, $my;
  427. // if ($id<0) {
  428. // $sql = 'SELECT subscriberid AS id FROM #__emktg_subscriber WHERE email LIKE '.$database->Quote($email);
  429. // } else {
  430. // $sql = 'SELECT u.id'
  431. // .' FROM #__users u INNER JOIN #__emktg_user eu ON (u.id=eu.id) WHERE eu.id='.(int)$my->id;
  432. // }
  433. // $database->setQuery($sql);
  434. // $id = $database->loadResult();
  435. //
  436. // if (empty($my->id)) $id = -$id;
  437. if($lists == 0) { //remove from lists
  438. if ($id < 0) {
  439. $sql = 'DELETE FROM #__emktg_list_subscriber WHERE subscriberid = '.(int)$id;
  440. } else {
  441. $sql = 'DELETE FROM #__emktg_list_user WHERE id = '.(int)$id;
  442. }
  443. $database->setQuery($sql);
  444. $database->Query();
  445. } else {
  446. foreach($lists as $list) {
  447. if ($id < 0) {
  448. $sql = 'DELETE FROM #__emktg_list_subscriber WHERE listid = '.(int)$list.' AND subscriberid = '.(int)$id;
  449. } else {
  450. $sql = 'DELETE FROM #__emktg_list_user WHERE listid = '.(int)$list.' AND id = '.(int)$id;
  451. }
  452. $database->setQuery($sql);
  453. $database->Query();
  454. }
  455. }
  456. if ($id < 0) {
  457. $sql = 'SELECT COUNT(*) FROM #__emktg_list_subscriber WHERE subscriberid = '.(int)$id;
  458. } else {
  459. $sql = 'SELECT COUNT(*) FROM #__emktg_list_user WHERE id = '.(int)$id;
  460. }
  461. $database->setQuery($sql);
  462. $mailinglistcount = $database->LoadResult();
  463. if ($mailinglistcount<1) {
  464. //mark them as unsubscribed if they no longer belong to any mailing lists
  465. if ($id < 0) {
  466. $id = abs($id);
  467. $sql = 'UPDATE #__emktg_subscriber SET unsubscribe_date=NOW(), unsubscribe_reason=1 WHERE subscriberid = '.(int)$id;
  468. $msgSQL = 'select name, email, 1 as unsubscribe_reason from #__emktg_subscriber where subscriberid='.(int)$id;
  469. } else {
  470. $sql = 'UPDATE #__emktg_user SET unsubscribe_date=NOW(), unsubscribe_reason=1 WHERE id = '.(int)$id;
  471. $msgSQL = 'select u.name, u.email, 1 as unsubscribe_reason from #__users u inner join #__emktg_user s on (s.id=u.id) where u.id='.(int)$id;
  472. }
  473. $database->setQuery($sql);
  474. $removed = $database->Query();
  475. if($removed) {
  476. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_SUCCESS, 'success');
  477. sm2emAdminUnsubscribeEmailFromQuery($msgSQL);
  478. } else {
  479. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_ERRORREMOVING);
  480. }
  481. }
  482. else {
  483. sm2emailmarketingMessage(_EMKTG_UNSUBSCRIBE_MAILINGLIST, 'success');
  484. }
  485. } // sm2emailmarketingMailingUnsubscribe()
  486. function sm2emailmarketingImage(){
  487. global $database, $mosConfig_absolute_path;
  488. $id = mosGetParam($_GET, 'id', 0);
  489. if (!empty($id)) {
  490. $database->setQuery('SELECT messageid FROM #__emktg_stats_message WHERE id='.(int)$id);
  491. $messageid = $database->loadResult();
  492. // update the database
  493. $database->setQuery('UPDATE #__emktg_stats_message SET `read`=1 WHERE ID='.(int)$id);
  494. $database->query();
  495. // determine how many times this has been read
  496. // note did this because we cannot be sure they wont read the message more than once
  497. // and probably should not have stored the overall read counter in the overall table
  498. // but instead just doing this sum when lookin at the stats
  499. $database->setQuery('SELECT SUM(`read`) FROM #__emktg_stats_message WHERE messageid='.(int)$messageid);
  500. $read = $database->loadResult();
  501. // update the overall read counter
  502. $database->setQuery('UPDATE #__emktg_stats_overall SET `html_read`='.(int)$read.' WHERE messageid='.(int)$messageid);
  503. $database->query();
  504. }
  505. // send the image
  506. ob_end_clean();
  507. $filename = $mosConfig_absolute_path . '/images/blank.png';
  508. $handle = fopen($filename, 'r');
  509. $contents = fread($handle, filesize($filename));
  510. fclose($handle);
  511. header('Content-type: image/png');
  512. echo $contents;
  513. exit;
  514. }
  515. function sm2emShowArchiveList($option) {
  516. global $database, $my, $Itemid, $mainframe;
  517. // get the parameters for this component to determine how to display this
  518. if (defined('_JLEGACY')) {
  519. $menu =& JSite::getMenu();
  520. $item = $menu->getActive();
  521. if($item)
  522. $params =& $menu->getParams($item->id);
  523. else
  524. $params =& $menu->getParams(null);
  525. } else {
  526. $item = $mainframe->get( 'menu' );
  527. $params = new mosParameters( $item->params );
  528. }
  529. // determine what the user can see
  530. $listMessages = array();
  531. $sentMessages = array();
  532. $messages = array();
  533. $lists = getAvailableLists();
  534. $database->setQuery( 'SELECT DISTINCT m.messageid, m.subject, m.send_date'
  535. .', mp.plugin_data'
  536. .' FROM #__emktg_message m'
  537. .' INNER JOIN #__emktg_message_plugin mp'
  538. .' USING (messageid)'
  539. .' INNER JOIN #__emktg_plugin p'
  540. .' USING (pluginid)'
  541. .' WHERE (m.send_date!=0)'
  542. .' AND (p.filename='.$database->Quote( 'list.plugin.php' ).')'
  543. .' AND (p.enabled=1)'
  544. .' ORDER BY m.send_date DESC' );
  545. $listMessages = $database->loadObjectList();
  546. // get the list of messages for the user
  547. if ($my->id) {
  548. // user is logged in so show the list of messages that the user received
  549. $database->setQuery( 'SELECT DISTINCT m.messageid, m.subject, m.send_date'
  550. .' FROM #__emktg_message m'
  551. .' INNER JOIN #__emktg_stats_message s'
  552. .' ON (m.messageid=s.messageid)'
  553. .' WHERE (s.userid='.(int)$my->id.')'
  554. .' AND (s.send_date!=0) AND (s.status=1)'
  555. .' ORDER BY m.send_date DESC' );
  556. $sentMessages = $database->loadObjectList();
  557. }
  558. // process all of these messages and make sure they are in order
  559. $foundMessages = array();
  560. while (count($listMessages) || count($sentMessages)) {
  561. $type = 0;
  562. if (!count($listMessages)) {
  563. $type = 2;
  564. } else if (!count($sentMessages)) {
  565. $type = 1;
  566. } else if ($listMessages[0]->send_date > $sentMessages[0]->send_date) {
  567. $type = 1;
  568. } else {
  569. $type = 2;
  570. }
  571. if ($type==1) {
  572. // process the list message
  573. $message = array_shift($listMessages);
  574. $selectedLists = unserialize($message->plugin_data);
  575. if (!is_array($selectedLists)) $selectedLists = array();
  576. $matches = array_intersect($selectedLists, $lists);
  577. if (!count($matches)) continue;
  578. } else if ($type==2) {
  579. // process the sent message
  580. $message = array_shift($sentMessages);
  581. }
  582. if (in_array($message->messageid, $foundMessages)) continue;
  583. $foundMessages[] = $message->messageid;
  584. $message->link = 'index.php?option='.$option
  585. .'&task=showarchivemessage'
  586. .'&id='.$message->messageid
  587. .'&Itemid='.$Itemid;
  588. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  589. $message->link = sefRelToAbs($message->link);
  590. }
  591. $message->send_date_display = mosFormatDate($message->send_date, _EMKTG_MESSAGE_SENDDATE_FORMAT);
  592. $messages[] = $message;
  593. }
  594. // load the language elements
  595. $lang = sm2emailmarketingLanguage();
  596. // call the object to display the list
  597. $tmpl = sm2emailmarketingPatTemplate('message.tpl', $option);
  598. $tmpl->addVar('pagetitle', 'pagetitle', $item->name);
  599. if (count($messages)) {
  600. $tmpl->addObject('list', $lang, 'LANG');
  601. $tmpl->addObject('list', $params->toObject(), 'PARAM_');
  602. $tmpl->addObject('rows', $messages);
  603. $tmpl->displayParsedTemplate('list');
  604. } else {
  605. $tmpl->addObject('listempty', $lang, 'LANG');
  606. $tmpl->addObject('listempty', $params->toObject(), 'PARAM_');
  607. $tmpl->displayParsedTemplate('listempty');
  608. }
  609. } // sm2emShowArchiveList()
  610. function sm2emShowMessageArchive($option) {
  611. global $database, $Itemid, $mainframe, $my;
  612. global $mosConfig_fromname, $mosConfig_mailfrom, $mosConfig_absolute_path;
  613. global $sm2emailmarketingConfig, $mosConfig_live_site, $mosConfig_secret;
  614. $adminPath = $mosConfig_absolute_path.'/administrator/components/'.$option;
  615. $id = (int) mosGetParam($_GET, 'id', 0);
  616. if (empty($id)) {
  617. $url = 'index.php?option='.$option.'&task=showarchivelist&Itemid='.$Itemid;
  618. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  619. $url = sefRelToAbs($url);
  620. }
  621. mosRedirect($url);
  622. }
  623. $action = (int) mosGetParam($_GET, 'action', 0);
  624. $confirm = mosGetParam($_GET, 'confirm', null);
  625. // get the parameters for this component to determine how to display this
  626. if (defined('_JLEGACY')) {
  627. $menu =& JSite::getMenu();
  628. $item = $menu->getActive();
  629. if($item)
  630. $params =& $menu->getParams($item->id);
  631. else
  632. $params =& $menu->getParams(null);
  633. } else {
  634. $item = $mainframe->get( 'menu' );
  635. $params = new mosParameters( $item->params );
  636. }
  637. $params->def( 'pageclass_sfx', '' );
  638. $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );
  639. // create an instance of the table class
  640. $row = new sm2emailmarketingMessage();
  641. // Load the row from the db table
  642. $row->load($id);
  643. // determine if we are allowed to continue
  644. $allowed = false;
  645. $confirmURL = '';
  646. if ($confirm!==null) {
  647. // check the ident to see if it is valid
  648. $forwardid = (int) mosGetParam($_GET, 'forwardid', 0);
  649. if (empty($forwardid)) {
  650. $validate = md5($mosConfig_secret.$id);
  651. $confirmURL = '&confirm='.$confirm;
  652. } else {
  653. $validate = md5($id.$mosConfig_secret.$forwardid);
  654. $confirmURL = '&forwardid='.$forwardid.'&confirm='.$confirm;
  655. }
  656. // not used yet so display not auth
  657. if ($validate==$confirm)
  658. $allowed = true;
  659. } else {
  660. // find the lists available to the user
  661. $lists = getAvailableLists();
  662. // get the details for the message
  663. $database->setQuery('SELECT mp.plugin_data'
  664. .' FROM #__emktg_message_plugin mp'
  665. .' INNER JOIN #__emktg_plugin p'
  666. .' USING (pluginid)'
  667. .' WHERE (mp.messageid='.(int)$id.')'
  668. .' AND (p.filename='.$database->Quote( 'list.plugin.php' ).')'
  669. .' AND (p.enabled=1)');
  670. $pluginData = $database->loadResultArray();
  671. foreach ($pluginData as $data) {
  672. $selectedLists = unserialize($data);
  673. if (!is_array($selectedLists)) $selectedLists = array();
  674. $matches = array_intersect($selectedLists, $lists);
  675. if (!count($matches)) continue;
  676. $allowed = true;
  677. }
  678. if (!$allowed && $my->id) {
  679. // user is logged in
  680. // check to see if they received this message
  681. $database->setQuery('SELECT COUNT(*)'
  682. .' FROM #__emktg_stats_message'
  683. .' WHERE (messageid='.(int)$id.')'
  684. .' AND (userid='.(int)$my->id.')');
  685. $count = $database->loadResult();
  686. if ($count) $allowed=true;
  687. }
  688. }
  689. if (!$allowed) {
  690. mosNotAuth();
  691. return;
  692. }
  693. if ($action==0) {
  694. $row->send_date_display = mosFormatDate($row->send_date, _EMKTG_MESSAGE_SENDDATE_FORMAT);
  695. // load the language elements
  696. $lang = sm2emailmarketingLanguage();
  697. // call the object to display the list
  698. $tmpl = sm2emailmarketingPatTemplate('message.tpl', $option);
  699. $tmpl->addVar('pagetitle', 'pagetitle', $item->name);
  700. $tmpl->addVar('archive', 'confirmurl', $confirmURL);
  701. $tmpl->addObject('archive', $lang, 'LANG');
  702. $tmpl->addObject('archive', $row);
  703. $tmpl->addObject('archive', $params->toObject(), 'PARAM_');
  704. $tmpl->displayParsedTemplate('archive');
  705. } else {
  706. //build message
  707. $row->replaceUserInfo('[NAME]', '[EMAIL]', null);
  708. $row->replaceTags(true);
  709. if (empty($row->fromname)) {
  710. $row->fromname = $sm2emailmarketingConfig->get('fromname', $mosConfig_fromname);
  711. }
  712. if (empty($row->fromemail)) {
  713. $row->fromemail = $sm2emailmarketingConfig->get('fromemail', $mosConfig_mailfrom);
  714. }
  715. if (empty($row->bounceemail)) {
  716. $row->bounceemail = $sm2emailmarketingConfig->get('bounceemail', $mosConfig_mailfrom);
  717. }
  718. if ($row->send_date==0) {
  719. $row->send_date = '';
  720. }
  721. // get the plugins to process
  722. $database->setQuery('SELECT * FROM #__emktg_plugin WHERE enabled=1 ORDER BY ordering');
  723. $plugins = $database->loadObjectList();
  724. // process the plugins
  725. foreach ($plugins as $plugin) {
  726. // load the plugin
  727. if (!file_exists($adminPath.'/plugins/'.$plugin->filename)) continue;
  728. require_once($adminPath.'/plugins/'.$plugin->filename);
  729. $class = $plugin->classname;
  730. $obj = new $class();
  731. // process the send of the plugin
  732. if (method_exists($obj, 'processMessage')) {
  733. $obj->processMessage($id, $plugin->pluginid, $option, $row, 0);
  734. }
  735. } // foreach()
  736. // load the language elements
  737. $lang = sm2emailmarketingLanguage();
  738. if (!is_array($row->attachments)) $row->attachments = explode(',', $row->attachments);
  739. $attachments = array();
  740. foreach ($row->attachments as $index=>$file) {
  741. if (!is_file($file)) continue;
  742. if (substr($file, 0, strlen($mosConfig_absolute_path)) != $mosConfig_absolute_path) continue;
  743. $attachments[] = '<a href="'
  744. .str_replace($mosConfig_absolute_path, $mosConfig_live_site, $file)
  745. .'">'.basename($file).'</a>';
  746. }
  747. if (count($attachments)) {
  748. $attachments = implode('<br />', $attachments);
  749. $row->message_htmlDisplay = $row->message_html
  750. .'<br style="clear: both;" /><br /><hr />'
  751. .'<strong>Attachments:</strong><br />'.$attachments;
  752. $row->message_textDisplay = nl2br($row->message_text
  753. .'<hr /><strong>Attachments:</strong><br />'.$attachments);
  754. } else {
  755. $row->message_htmlDisplay = $row->message_html;
  756. $row->message_textDisplay = $row->message_text;
  757. }
  758. sm2emailmarketing_addAbsolutePath($row->message_htmlDisplay);
  759. $out = $row->message_htmlDisplay;
  760. $outTest = strtolower($out);
  761. // see if we can somehow add in the BASE tag
  762. if (strpos($outTest, '</head>')!==false) {
  763. // add the base to the head
  764. $out = preg_replace('/(<\/head>)/i','<base target="_blank" />$1', $out);
  765. } else if (strpos($outTest, '<html')!==false) {
  766. $out = preg_replace('/(<html[^>]*>)/iu', '$1<head><base target="_blank" /></head>', $out);
  767. } else {
  768. $out = '<html><head><base target="_blank" /></head><body>'.$out.'</body></html>';
  769. }
  770. echo $out;
  771. }
  772. } // sm2emShowMessageArchive()
  773. /**
  774. * Function to display the Forward page.
  775. * Validates the confirmation code
  776. */
  777. function sm2emForward($option) {
  778. global $database, $Itemid, $mainframe, $mosConfig_secret, $sm2emailmarketingConfig;
  779. // check to see if forwarding is enabled
  780. if ((int)$sm2emailmarketingConfig->get('forward', 0)==0) {
  781. mosNotAuth();
  782. return;
  783. }
  784. // get the parameters for this component to determine how to display this
  785. if (defined('_JLEGACY')) {
  786. $menu =& JSite::getMenu();
  787. $item = $menu->getActive();
  788. if($item)
  789. $params =& $menu->getParams($item->id);
  790. else {
  791. $params =& $menu->getParams(null);
  792. $item = (object)array('name'=>_EMKTG_FORWARD);
  793. }
  794. } else {
  795. $item = $mainframe->get( 'menu' );
  796. $params = new mosParameters( $item->params );
  797. }
  798. // determine if the details entered are correct
  799. $messageid = (int)mosGetParam($_GET, 'id', 0);
  800. $subscriberid = (int)mosGetParam($_GET, 'subscriberid', 0);
  801. $confirm = mosGetParam($_GET, 'confirm', '');
  802. // validate the confirm
  803. $validation = md5($mosConfig_secret.$messageid.$subscriberid);
  804. $template = ($validation==$confirm) ? 'forward' : 'forwarderror';
  805. $forwardDays = (int)$sm2emailmarketingConfig->get('forward_days', 30);
  806. if ($forwardDays) {
  807. $send_date = date('Y-m-d H:i:s',
  808. time() - ((int)$sm2emailmarketingConfig->get('forward_days', 30) * 24 * 60 * 60)
  809. );
  810. } else {
  811. $send_date = date('Y-m-d H:i:s', mktime(0,0,0,1,1,2000));
  812. }
  813. // lookup the information
  814. if ($subscriberid > 0) {
  815. $database->setQuery('SELECT m.subject, u.name, u.email, sm.html, p.classname, '
  816. .'CASE WHEN m.send_date > '.$database->Quote($send_date).' THEN 1 ELSE 0 END AS forward_date '
  817. .'FROM #__emktg_stats_message sm '
  818. .'INNER JOIN #__emktg_plugin p ON (sm.pluginid=p.pluginid) '
  819. .'INNER JOIN #__emktg_message m ON (sm.messageid=m.messageid) '
  820. .'INNER JOIN #__emktg_user eu ON (sm.userid=eu.id) '
  821. .'INNER JOIN #__users u ON (eu.id=u.id) '
  822. .'WHERE sm.messageid='.$messageid
  823. .' AND sm.userid='.$subscriberid
  824. .' AND sm.send_date!=0'
  825. .' AND m.send_date!=0'
  826. .' AND eu.unsubscribe_date=0'
  827. .' AND eu.confirmed=1'
  828. .' AND u.block=0');
  829. } else {
  830. $database->setQuery('SELECT m.subject, s.name, s.email, sm.html, p.classname, '
  831. .'CASE WHEN m.send_date > '.$database->Quote($send_date).' THEN 1 ELSE 0 END AS forward_date '
  832. .'FROM #__emktg_stats_message sm '
  833. .'INNER JOIN #__emktg_plugin p ON (sm.pluginid=p.pluginid) '
  834. .'INNER JOIN #__emktg_message m ON (sm.messageid=m.messageid) '
  835. .'INNER JOIN #__emktg_subscriber s ON (sm.userid=-s.subscriberid) '
  836. .'WHERE sm.messageid='.$messageid
  837. .' AND sm.userid='.$subscriberid
  838. .' AND sm.send_date!=0'
  839. .' AND m.send_date!=0'
  840. .' AND s.unsubscribe_date=0'
  841. .' AND s.confirmed=1');
  842. }
  843. $row = null;
  844. $contentHtml = 1;
  845. $receiveHtml = false;
  846. if (!$database->loadObject($row)) {
  847. $template = 'forwarderror';
  848. } else {
  849. // determine the format of the message to be sent
  850. if ($subscriberid < 0 && $row->classname=='sm2emailmarketing_plugin_list')
  851. $canBeLink = true;
  852. $receiveHtml = $row->html;
  853. if (!(int)$row->forward_date)
  854. $template = 'forwardexpired';
  855. }
  856. $msg = $sm2emailmarketingConfig->get('forward_msg', _EMKTG_CONFIGURATION_FORWARD_MSG_DEFAULT);
  857. if (strpos($msg, '[ARCHIVE_URL]')==false && strpos($msg, '[CONTENT]')==false) {
  858. $contentHtml = 2;
  859. } else if (strpos($msg, '[ARCHIVE_URL]')) {
  860. $contentHtml = 0;
  861. }
  862. switch ($contentHtml) {
  863. case 0:
  864. $contentHtml = _EMKTG_FORWARD_MESSAGE_CONTENT_LINK;
  865. break;
  866. case 1:
  867. $contentHtml = _EMKTG_FORWARD_MESSAGE_CONTENT_MESSAGE;
  868. break;
  869. case 2:
  870. default:
  871. $contentHtml = _EMKTG_FORWARD_MESSAGE_CONTENT_ATTACHMENT;
  872. break;
  873. }
  874. // load the language elements
  875. $lang = sm2emailmarketingLanguage();
  876. // call the object to display the list
  877. $tmpl = sm2emailmarketingPatTemplate('message.tpl', $option);
  878. $tmpl->addVar('pagetitle', 'pagetitle', $item->name);
  879. $tmpl->addObject($template, $lang, 'LANG');
  880. $tmpl->addVar($template, 'messageid', $messageid);
  881. $tmpl->addVar($template, 'subscriberid', $subscriberid);
  882. $tmpl->addVar($template, 'confirm', md5($messageid.$subscriberid.$mosConfig_secret));
  883. $tmpl->addVar($template, 'receivehtml', ($receiveHtml ? _EMKTG_FORWARD_MESSAGE_FORMAT_HTML : _EMKTG_FORWARD_MESSAGE_FORMAT_TEXT));
  884. $tmpl->addVar($template, 'messagecontent', $contentHtml);
  885. $tmpl->addObject($template, $row);
  886. $tmpl->displayParsedTemplate($template);
  887. } // sm2emForward()
  888. /**
  889. * Function to send the forward email
  890. * validates the confirmation code and the details to be sent
  891. */
  892. function sm2emSendForward($option) {
  893. global $database, $Itemid, $mainframe, $mosConfig_secret, $sm2emailmarketingConfig;
  894. global $mosConfig_absolute_path, $mosConfig_live_site;
  895. // check to see if forwarding is enabled
  896. if ((int)$sm2emailmarketingConfig->get('forward', 0)==0) {
  897. mosNotAuth();
  898. return;
  899. }
  900. // get the parameters for this component to determine how to display this
  901. if (defined('_JLEGACY')) {
  902. $menu =& JSite::getMenu();
  903. $item = $menu->getActive();
  904. if($item)
  905. $params =& $menu->getParams($item->id);
  906. else {
  907. $params =& $menu->getParams(null);
  908. $item = (object)array('name'=>_EMKTG_FORWARD);
  909. }
  910. } else {
  911. $item = $mainframe->get( 'menu' );
  912. $params = new mosParameters( $item->params );
  913. }
  914. // determine if the details entered are correct
  915. $messageid = (int)mosGetParam($_GET, 'id', 0);
  916. $subscriberid = (int)mosGetParam($_GET, 'subscriberid', 0);
  917. $confirm = mosGetParam($_GET, 'confirm', '');
  918. // validate the confirm
  919. $validation = md5($messageid.$subscriberid.$mosConfig_secret);
  920. if ($validation!=$confirm) {
  921. sm2emSendForwardError(_EMKTG_FORWARD_UNKNOWN_ERROR, $option, $menu);
  922. return;
  923. }
  924. // get the post data
  925. $recipientName = trim(mosGetParam($_POST, 'name', ''));
  926. $recipientEmail = trim(mosGetParam($_POST, 'email', ''));
  927. $recipientEmail = explode(',',$recipientEmail);
  928. $recipientEmail = $recipientEmail[0];
  929. $recipientMessage = trim(mosGetParam($_POST, 'note', ''));
  930. // validate the post data
  931. $postErrors = array();
  932. if ($recipientName=='')
  933. $postErrors[] = _EMKTG_ARCHIVE_RECIPIENT_NAME;
  934. if ($recipientEmail=='')
  935. $postErrors[] = _EMKTG_ARCHIVE_RECIPIENT_EMAIL;
  936. if ($recipientMessage=='')
  937. $postErrors[] = _EMKTG_FORWARD_PERSONAL_MESSAGE;
  938. if (count($postErrors)) {
  939. sm2emSendForwardError(_EMKTG_FORWARD_JS_ERROR
  940. .'<ul><li>'.implode('</li><li>', $postErrors).'</li></ul>',
  941. $option, $menu);
  942. return;
  943. }
  944. unset($postErrors);
  945. // validate the message has not already been sent to this subscriber
  946. $database->setQuery('SELECT COUNT(*) FROM #__emktg_stats_message'
  947. .' WHERE messageid='.$messageid
  948. .' AND LOWER(email)='.$database->Quote(strtolower($recipientEmail)));
  949. $count = $database->loadResult();
  950. if ($count) {
  951. sm2emSendForwardError(_EMKTG_FORWARD_DUPLICATE_ERROR, $option, $menu);
  952. return;
  953. }
  954. unset($count);
  955. // get the details of the sender
  956. $use_html = 0;
  957. if ($subscriberid<0) {
  958. $sender = new sm2emailmarketingSubscriber();
  959. $sender->load(-$subscriberid);
  960. $use_html = $sender->receive_html;
  961. } else {
  962. $sender = new mosUser($database);
  963. $sender->load($subscriberid);
  964. $temp = new sm2emailmarketingUser();
  965. $temp->load($subscriberid);
  966. $use_html = $temp->receive_html;
  967. unset($temp);
  968. }
  969. $path = $sm2emailmarketingConfig->get('attachment_path', $mosConfig_absolute_path.'/images/stories/');
  970. // determine what to set the memory limit to - if at all
  971. $currentMemoryLimit = (int)ini_get('memory_limit');
  972. $memoryLimit = (int)$sm2emailmarketingConfig->get('process_memory', 0);
  973. if ($memoryLimit) {
  974. if ($memoryLimit < $currentMemoryLimit) $memoryLimit += $currentMemoryLimit;
  975. ini_set('memory_limit', $memoryLimit.'M');
  976. }
  977. define('_SM2EM_FORWARD', 1);
  978. // get the message
  979. $row = new sm2emailmarketingMessage();
  980. $row->load($messageid);
  981. $row->fromname = $sender->name;
  982. $row->fromemail = $sender->email;
  983. $row->bounceemail = $sender->email;
  984. // get the forward message
  985. $forwardSubject = $sm2emailmarketingConfig->get('forward_subject', _EMKTG_CONFIGURATION_FORWARD_SUBJECT_DEFAULT);
  986. $forwardMessage = $sm2emailmarketingConfig->get('forward_msg', _EMKTG_CONFIGURATION_FORWARD_MSG_DEFAULT);
  987. // process the subject
  988. $forwardSubject = str_replace('[FORWARD_NAME]', $recipientName, $forwardSubject);
  989. $forwardSubject = str_replace('[SUBJECT]', $row->subject, $forwardSubject);
  990. //use Joomla! class to send mail
  991. $mail = mosCreateMail($sender->email, $sender->name, $forwardSubject, '');
  992. // identify that we are sending and what the messageid is
  993. $mail->addCustomHeader('X-Mailer: SM2 Email Marketing');
  994. $mail->addCustomHeader('X-SM2MessageID: '.$row->messageid);
  995. $mail->addCustomHeader('X-SM2Recipient: '.$recipientEmail);
  996. // stop auto responders apparently
  997. $mail->addCustomHeader('Precedence: bulk');
  998. $mail->AddAddress($recipientEmail, $recipientName);
  999. $mail->Subject = $forwardSubject;
  1000. // prepare the search for the message
  1001. $search = array(
  1002. '[FORWARD_NAME]',
  1003. '[FORWARD_EMAIL]',
  1004. '[FROM_NAME]',
  1005. '[FROM_EMAIL]',
  1006. '[FROM]',
  1007. '[NOTE]',
  1008. '[SUBJECT]',
  1009. '[ARCHIVE_URL]',
  1010. '[CONTENT]'
  1011. );
  1012. $replace = array(
  1013. $recipientName,
  1014. $recipientEmail,
  1015. $sender->name,
  1016. $sender->email,
  1017. $sender->name.' ['.$sender->email.']',
  1018. $recipientMessage,
  1019. $row->subject
  1020. );
  1021. // determine what the archive url will be
  1022. $includeArchiveUrl = false;
  1023. if (strpos($forwardMessage, '[ARCHIVE_URL]')) {
  1024. $includeArchiveUrl = true;
  1025. $archiveURL = '';
  1026. $itemid = $row->_getItemId();
  1027. $archiveURL = 'index.php?option=com_sm2emailmarketing'
  1028. .'&task=showarchivemessage'
  1029. .'&id='.$messageid
  1030. .'&forwardid='.$subscriberid
  1031. .'&confirm='.md5($messageid.$mosConfig_secret.$subscriberid)
  1032. .$itemid;
  1033. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  1034. $archiveURL = sefRelToAbs($archiveURL);
  1035. } else {
  1036. $archiveURL = $mosConfig_live_site.'/'.$archiveURL;
  1037. }
  1038. $replace[] = $archiveURL;
  1039. unset($archiveURL);
  1040. } else {
  1041. $replace[] = '';
  1042. }
  1043. // determine what the content will be
  1044. $contentType = $includeArchiveUrl ? 0 : 2;
  1045. $hasContentTag = (bool)strpos($forwardMessage, '[CONTENT]');
  1046. if ($hasContentTag || !$includeArchiveUrl) {
  1047. // need to include the content in some fashion
  1048. //build message
  1049. $row->replaceTags();
  1050. // get the plugins to process
  1051. $database->setQuery('SELECT * FROM #__emktg_plugin WHERE enabled=1 ORDER BY ordering');
  1052. $plugins = $database->loadObjectList();
  1053. // process the plugins
  1054. foreach ($plugins as $plugin) {
  1055. // load the plugin
  1056. if (!file_exists($mosConfig_absolute_path.'/administrator/components/'.$option.'/plugins/'.$plugin->filename)) continue;
  1057. require_once($mosConfig_absolute_path.'/administrator/components/'.$option.'/plugins/'.$plugin->filename);
  1058. $class = $plugin->classname;
  1059. $obj = new $class();
  1060. // process the send of the plugin
  1061. if (method_exists($obj, 'processMessage')) {
  1062. $obj->processMessage($messageid, $plugin->pluginid, $option, $row, 0);
  1063. }
  1064. } // foreach()
  1065. //personalise the message
  1066. $row->replaceUserInfo($sender->name,$sender->email); //message is all set for sending
  1067. sm2emailmarketing_addAbsolutePath($row->message_html);
  1068. foreach ($row->attachments as $attachment) {
  1069. $mail->AddAttachment($attachment, basename($attachment));
  1070. }
  1071. foreach ($row->embeddedImages as $embed) {
  1072. $mail->AddEmbeddedImage($embed->image, $embed->cid, $embed->name, $embed->encoding, $embed->type);
  1073. }
  1074. if (!$hasContentTag) {
  1075. // attach the content
  1076. if ($use_html) { //html message to be sent
  1077. $mail->AddStringAttachment($row->message_html,
  1078. 'message.dat',
  1079. 'quoted-printable',
  1080. 'text/html');
  1081. } else {
  1082. $mail->AddStringAttachment($row->message_html,
  1083. 'message.dat',
  1084. 'quoted-printable',
  1085. 'text/plain');
  1086. }
  1087. $replace[] = '';
  1088. } else {
  1089. if ($use_html) {
  1090. $replace[] = $row->message_html;
  1091. } else {
  1092. $replace[] = $row->message_text;
  1093. }
  1094. }
  1095. } else {
  1096. $replace[] = '';
  1097. }
  1098. // process the message
  1099. $forwardMessage = str_replace($search, $replace, $forwardMessage);
  1100. //get the correct format of message
  1101. $mail->IsHTML((bool)$use_html);
  1102. $mail->Body = $forwardMessage;
  1103. $wordWrap = $sm2emailmarketingConfig->get('word_wrap', 0);
  1104. if ($wordWrap) {
  1105. // word wrap both bodies
  1106. $mail->Body = $mail->WrapText($mail->Body, $wordWrap);
  1107. if (!empty($mail->AltBody))
  1108. $mail->AltBody = $mail->WrapText($mail->AltBody, $wordWrap);
  1109. }
  1110. //send message
  1111. $sendMailOk = $mail->Send();
  1112. if (!$sendMailOk) {
  1113. sm2emSendForwardError(_EMKTG_FORWARD_SEND_ERROR, $option, $menu);
  1114. return;
  1115. }
  1116. // load the language elements
  1117. $lang = sm2emailmarketingLanguage();
  1118. // call the object to display the list
  1119. $tmpl = sm2emailmarketingPatTemplate('message.tpl', $option);
  1120. $tmpl->addVar('pagetitle', 'pagetitle', $item->name);
  1121. $tmpl->addObject('sendforward', $lang, 'LANG');
  1122. $tmpl->addVar('sendforward', 'subject', $forwardSubject);
  1123. $tmpl->addVar('sendforward', 'fromname', $sender->name);
  1124. $tmpl->addVar('sendforward', 'fromemail', $sender->email);
  1125. $tmpl->addVar('sendforward', 'name', $recipientName);
  1126. $tmpl->addVar('sendforward', 'email', $recipientEmail);
  1127. $tmpl->addVar('sendforward', 'note', $recipientMessage);
  1128. $tmpl->displayParsedTemplate('sendforward');
  1129. } // sm2emSendForward()
  1130. function sm2emSendForwardError($errorMsg, $option, $menu) {
  1131. // load the language elements
  1132. $lang = sm2emailmarketingLanguage();
  1133. // call the object to display the list
  1134. $tmpl = sm2emailmarketingPatTemplate('message.tpl', $option);
  1135. $tmpl->addVar('pagetitle', 'pagetitle', $menu->name);
  1136. $tmpl->addObject('sendforwarderror', $lang, 'LANG');
  1137. $tmpl->addVar('sendforwarderror', 'error', $errorMsg);
  1138. $tmpl->displayParsedTemplate('sendforwarderror');
  1139. } // sm2emSendForwardError()
  1140. ?>