dolibarr 19.0.4
mailing-unsubscribe.php
Go to the documentation of this file.
1<?php
30if (!defined('NOLOGIN')) {
31 define('NOLOGIN', '1');
32}
33if (!defined('NOCSRFCHECK')) {
34 define('NOCSRFCHECK', '1');
35}
36if (!defined('NOBROWSERNOTIF')) {
37 define('NOBROWSERNOTIF', '1');
38}
39if (!defined('NOREQUIREMENU')) {
40 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
41}
42if (!defined('NOIPCHECK')) {
43 define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
44}
45if (!defined("NOSESSION")) {
46 define("NOSESSION", '1');
47}
48if (! defined('NOREQUIREHTML')) {
49 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
50}
51if (! defined('NOREQUIREAJAX')) {
52 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
53}
54
55
56// Load Dolibarr environment
57require '../../main.inc.php';
58require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
59
60global $user, $conf, $langs;
61
62$langs->loadLangs(array("main", "mails"));
63
64$mtid = GETPOST('mtid');
65$email = GETPOST('email');
66$tag = GETPOST('tag'); // To retreive the emailing, and recipient
67$unsuscrib = GETPOST('unsuscrib');
68$securitykey = GETPOST('securitykey');
69
70
71/*
72 * Actions
73 */
74
75dol_syslog("public/emailing/mailing-unsubscribe.php : tag=".$tag." securitykey=".$securitykey, LOG_DEBUG);
76
77if ($securitykey != dol_hash(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')."-".$tag."-".$email."-".$mtid, 'md5')) {
78 print 'Bad security key value.';
79 exit;
80}
81
82if (empty($tag) || ($unsuscrib != '1')) {
83 print 'Bad parameters';
84 exit;
85}
86
87
88/*
89 * View
90 */
91
92$head = '';
93$replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
94
95llxHeader($head, $langs->trans("MailUnsubcribe"), '', '', 0, 0, '', '', '', 'onlinepaymentbody', $replacemainarea);
96
97dol_syslog("public/emailing/mailing-unsubscribe.php : Launch unsubscribe requests", LOG_DEBUG);
98
99$sql = "SELECT mc.rowid, mc.email, mc.statut, m.entity";
100$sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
101$sql .= " WHERE mc.fk_mailing = m.rowid AND mc.tag = '".$db->escape($tag)."'";
102
103$resql = $db->query($sql);
104if (!$resql) {
105 dol_print_error($db);
106}
107
108$obj = $db->fetch_object($resql);
109
110if (empty($obj)) {
111 print 'Emailing tag '.$tag.' not found in database. Operation canceled.';
112 llxFooter('', 'private');
113 exit;
114}
115if (empty($obj->email)) {
116 print 'Email for this tag is not valid. Operation canceled.';
117 llxFooter('', 'private');
118 exit;
119}
120
121if ($obj->statut == 3) {
122 print 'Email tag already set to unsubscribe. Operation canceled.';
123 llxFooter('', 'private');
124 exit;
125}
126// TODO Test that mtid and email match also with the one found from $tag
127/*
128if ($obj->email != $email)
129{
130 print 'Email does not match tagnot found. No need to unsubscribe.';
131 exit;
132}
133*/
134
135// Update status of mail in recipient mailing list table
136$statut = '3';
137$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles SET statut=".((int) $statut)." WHERE tag = '".$db->escape($tag)."'";
138
139$resql = $db->query($sql);
140if (!$resql) {
141 dol_print_error($db);
142}
143
144/*
145// Update status communication of thirdparty prospect (old usage)
146$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)";
147
148$resql=$db->query($sql);
149if (! $resql) dol_print_error($db);
150
151// Update status communication of contact prospect (old usage)
152$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)";
153
154$resql=$db->query($sql);
155if (! $resql) dol_print_error($db);
156*/
157
158// Update status communication of email (new usage)
159$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())."')";
160
161$resql = $db->query($sql);
162//if (! $resql) dol_print_error($db); No test on errors, may fail if already unsubscribed
163
164
165print '<table><tr><td style="text_align:center;">';
166print $langs->trans("YourMailUnsubcribeOK", $obj->email)."<br>\n";
167print '</td></tr></table>';
168
169
170llxFooter('', 'public');
171
172$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.