mailing-unsubscribe.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (! defined('NOREQUIREHTML')) {
  46. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  47. }
  48. if (! defined('NOREQUIREAJAX')) {
  49. define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  50. }
  51. // Load Dolibarr environment
  52. require '../../main.inc.php';
  53. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  54. global $user, $conf, $langs;
  55. $langs->loadLangs(array("main", "mails"));
  56. $tag = GETPOST('tag'); // To retreive the emailing, and recipient
  57. $unsuscrib = GETPOST('unsuscrib');
  58. $securitykey = GETPOST('securitykey');
  59. /*
  60. * Actions
  61. */
  62. dol_syslog("public/emailing/mailing-read.php : tag=".$tag." securitykey=".$securitykey, LOG_DEBUG);
  63. if ($securitykey != getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')) {
  64. print 'Bad security key value.';
  65. exit;
  66. }
  67. if (empty($tag) || ($unsuscrib != '1')) {
  68. print 'Bad parameters';
  69. exit;
  70. }
  71. /*
  72. * View
  73. */
  74. $head = '';
  75. $replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
  76. llxHeader($head, $langs->trans("MailUnsubcribe"), '', '', 0, 0, '', '', '', 'onlinepaymentbody', $replacemainarea);
  77. dol_syslog("public/emailing/mailing-unsubscribe.php : Launch unsubscribe requests", LOG_DEBUG);
  78. $sql = "SELECT mc.rowid, mc.email, mc.statut, m.entity";
  79. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
  80. $sql .= " WHERE mc.fk_mailing = m.rowid AND mc.tag = '".$db->escape($tag)."'";
  81. $resql = $db->query($sql);
  82. if (!$resql) {
  83. dol_print_error($db);
  84. }
  85. $obj = $db->fetch_object($resql);
  86. if (empty($obj)) {
  87. print 'Email tag not found. Operation canceled.';
  88. llxFooter('', 'private');
  89. exit;
  90. }
  91. if (empty($obj->email)) {
  92. print 'Email for this tag not valid. Operation canceled.';
  93. llxFooter('', 'private');
  94. exit;
  95. }
  96. if ($obj->statut == 3) {
  97. print 'Email tag already set to unsubscribe. Operation canceled.';
  98. llxFooter('', 'private');
  99. exit;
  100. }
  101. // TODO Test that mtid and email match also with the one found from $tag
  102. /*
  103. if ($obj->email != $email)
  104. {
  105. print 'Email does not match tagnot found. No need to unsubscribe.';
  106. exit;
  107. }
  108. */
  109. // Update status of mail in recipient mailing list table
  110. $statut = '3';
  111. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles SET statut=".((int) $statut)." WHERE tag = '".$db->escape($tag)."'";
  112. $resql = $db->query($sql);
  113. if (!$resql) {
  114. dol_print_error($db);
  115. }
  116. /*
  117. // Update status communication of thirdparty prospect (old usage)
  118. $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)";
  119. $resql=$db->query($sql);
  120. if (! $resql) dol_print_error($db);
  121. // Update status communication of contact prospect (old usage)
  122. $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)";
  123. $resql=$db->query($sql);
  124. if (! $resql) dol_print_error($db);
  125. */
  126. // Update status communication of email (new usage)
  127. $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())."')";
  128. $resql = $db->query($sql);
  129. //if (! $resql) dol_print_error($db); No test on errors, may fail if already unsubscribed
  130. print '<table><tr><td style="text_align:center;">';
  131. print $langs->trans("YourMailUnsubcribeOK", $obj->email)."<br>\n";
  132. print '</td></tr></table>';
  133. llxFooter('', 'public');
  134. $db->close();