mailing-unsubscribe.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2005-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2012 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file public/emailing/mailing-unsubscribe.php
  23. * \ingroup mailing
  24. * \brief Script use to update unsubcribe status of an email
  25. * https://myserver/public/emailing/mailing-unsubscribe.php?unsuscrib=1&securitykey=securitykey&tag=abcdefghijklmn
  26. */
  27. if (!defined('NOLOGIN')) {
  28. define('NOLOGIN', '1');
  29. }
  30. if (!defined('NOCSRFCHECK')) {
  31. define('NOCSRFCHECK', '1');
  32. }
  33. if (!defined('NOBROWSERNOTIF')) {
  34. define('NOBROWSERNOTIF', '1');
  35. }
  36. if (!defined('NOREQUIREMENU')) {
  37. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  38. }
  39. if (!defined('NOIPCHECK')) {
  40. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  41. }
  42. if (!defined("NOSESSION")) {
  43. define("NOSESSION", '1');
  44. }
  45. /**
  46. * Header empty
  47. *
  48. * @return void
  49. */
  50. function llxHeader()
  51. {
  52. }
  53. /**
  54. * Footer empty
  55. *
  56. * @return void
  57. */
  58. function llxFooter()
  59. {
  60. }
  61. require '../../main.inc.php';
  62. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  63. global $user, $conf, $langs;
  64. $langs->loadLangs(array("main", "mails"));
  65. $mtid = GETPOST('mtid');
  66. $email = GETPOST('email');
  67. $tag = GETPOST('tag');
  68. $unsuscrib = GETPOST('unsuscrib');
  69. $securitykey = GETPOST('securitykey');
  70. /*
  71. * Actions
  72. */
  73. dol_syslog("public/emailing/mailing-read.php : tag=".$tag." securitykey=".$securitykey, LOG_DEBUG);
  74. if ($securitykey != $conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) {
  75. print 'Bad security key value.';
  76. exit;
  77. }
  78. if (!empty($tag) && ($unsuscrib == '1')) {
  79. dol_syslog("public/emailing/mailing-unsubscribe.php : Launch unsubscribe requests", LOG_DEBUG);
  80. $sql = "SELECT mc.rowid, mc.email, mc.statut, m.entity";
  81. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
  82. $sql .= " WHERE mc.fk_mailing = m.rowid AND mc.tag='".$db->escape($tag)."'";
  83. $resql = $db->query($sql);
  84. if (!$resql) {
  85. dol_print_error($db);
  86. }
  87. $obj = $db->fetch_object($resql);
  88. if (empty($obj)) {
  89. print 'Email target not valid. Operation canceled.';
  90. exit;
  91. }
  92. if (empty($obj->email)) {
  93. print 'Email target not valid. Operation canceled.';
  94. exit;
  95. }
  96. if ($obj->statut == 3) {
  97. print 'Email target already set to unsubscribe. Operation canceled.';
  98. exit;
  99. }
  100. // TODO Test that mtid and email match also with the one found from $tag
  101. /*
  102. if ($obj->email != $email)
  103. {
  104. print 'Email does not match tagnot found. No need to unsubscribe.';
  105. exit;
  106. }
  107. */
  108. // Update status of mail in recipient mailing list table
  109. $statut = '3';
  110. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles SET statut=".((int) $statut)." WHERE tag = '".$db->escape($tag)."'";
  111. $resql = $db->query($sql);
  112. if (!$resql) {
  113. dol_print_error($db);
  114. }
  115. /*
  116. // Update status communication of thirdparty prospect (old usage)
  117. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=-1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag = '".$db->escape($tag)."' AND source_type='thirdparty' AND source_id is not null)";
  118. $resql=$db->query($sql);
  119. if (! $resql) dol_print_error($db);
  120. // Update status communication of contact prospect (old usage)
  121. $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET no_email=1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag = '".$db->escape($tag)."' AND source_type='contact' AND source_id is not null)";
  122. $resql=$db->query($sql);
  123. if (! $resql) dol_print_error($db);
  124. */
  125. // Update status communication of email (new usage)
  126. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_unsubscribe (date_creat, entity, email, unsubscribegroup, ip) VALUES ('".$db->idate(dol_now())."', ".((int) $obj->entity).", '".$db->escape($obj->email)."', '', '".$db->escape(getUserRemoteIP())."')";
  127. $resql = $db->query($sql);
  128. //if (! $resql) dol_print_error($db); No test on errors, may fail if already unsubscribed
  129. header("Content-type: text/html; charset=".$conf->file->character_set_client);
  130. // Security options
  131. header("X-Content-Type-Options: nosniff"); // With the nosniff option, if the server says the content is text/html, the browser will render it as text/html (note that most browsers now force this option to on)
  132. header("X-Frame-Options: SAMEORIGIN"); // Frames allowed only if on same domain (stop some XSS attacks)
  133. print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  134. print "\n";
  135. print "<html>\n";
  136. print "<head>\n";
  137. print '<meta name="robots" content="noindex,nofollow">'."\n";
  138. print '<meta name="keywords" content="dolibarr,emailing">'."\n";
  139. print '<meta name="description" content="Dolibarr EMailing unsubcribe page">'."\n";
  140. print "<title>".$langs->trans("MailUnsubcribe")."</title>\n";
  141. print '<link rel="stylesheet" type="text/css" href="'.DOL_URL_ROOT.$conf->css.'?lang='.$langs->defaultlang.'">'."\n";
  142. print '<style type="text/css">';
  143. print '.CTableRow1 { margin: 1px; padding: 3px; font: 12px verdana,arial; background: #e6E6eE; color: #000000; -moz-border-radius-topleft:6px; -moz-border-radius-topright:6px; -moz-border-radius-bottomleft:6px; -moz-border-radius-bottomright:6px;}';
  144. print '.CTableRow2 { margin: 1px; padding: 3px; font: 12px verdana,arial; background: #FFFFFF; color: #000000; -moz-border-radius-topleft:6px; -moz-border-radius-topright:6px; -moz-border-radius-bottomleft:6px; -moz-border-radius-bottomright:6px;}';
  145. print '</style>';
  146. print "</head>\n";
  147. print '<body style="margin: 20px;">'."\n";
  148. print '<table><tr><td style="text_align:center;">';
  149. print $langs->trans("YourMailUnsubcribeOK", $obj->email)."<br>\n";
  150. print '</td></tr></table>';
  151. print "</body>\n";
  152. print "</html>\n";
  153. }
  154. $db->close();