-
Notifications
You must be signed in to change notification settings - Fork 9
/
callback_campaign.php
119 lines (97 loc) · 4.34 KB
/
callback_campaign.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
require_once dirname(__DIR__, 2) . '/config/config.inc.php';
if (_PS_VERSION_ < '1.5' || !defined('_PS_ADMIN_DIR_')) {
include_once dirname(__FILE__, 3) . '/init.php';
}
if (Tools::getIsset('data')) {
$data = (object) Tools::getValue('data');
} elseif (Tools::getIsset('mailjet')) {
$mailjet = json_decode(Tools::getValue('mailjet'));
$data = $mailjet->data;
} else {
$data = new stdClass();
}
require_once _PS_MODULE_DIR_ . 'mailjet/mailjet.php';
require_once _PS_MODULE_DIR_ . 'mailjet/classes/MailJetLog.php';
$mj = new Mailjet();
MailJetLog::init();
if (Tools::getIsset('response')) {
$response = (object) Tools::getValue('response');
}
if ($data->next_step_url) {
$expected_response_message = array('html saved successfully', 'campaign added successfully');
if (!empty($response) && in_array($response->message, $expected_response_message)) {
$mj_data = new Mailjet_Api($mj->getAccountSettingsKey('API_KEY'), $mj->getAccountSettingsKey('SECRET_KEY'));
$campaignId = (int) $data->campaign_id;
$html = $mj_data->data('newsletter', $campaignId, 'HTML', 'text/html', null, 'GET', 'LAST')->getResponse();
/* On enregistre la campagne en BDD et on génère un token */
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'mj_campaign WHERE campaign_id = ' . $campaignId;
$res_campaign = Db::getInstance()->GetRow($sql);
if (empty($res_campaign)) {
$token_presta = md5(uniqid('mj', true));
$sql = 'INSERT INTO ' . _DB_PREFIX_ . 'mj_campaign (campaign_id, token_presta, date_add)
VALUES (' . $campaignId . ', \'' . $token_presta . '\', NOW())';
Db::getInstance()->Execute($sql);
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'mj_campaign WHERE campaign_id = ' . $campaignId;
$res_campaign = Db::getInstance()->GetRow($sql);
}
$regexp = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';
preg_match_all("/$regexp/siU", $html, $liens);
$changed_html = false;
foreach ($liens[2] as $key => $l) {
$url = str_replace('.', '\.', str_replace('-', '\-', Configuration::get('PS_SHOP_DOMAIN')));
/* On cherche si on a un lien vers le site et sans le token */
if (preg_match('`' . $url . '`iUs', $l)) {
$changed_html = true;
$link_without_token = $liens[2][$key];
if (preg_match('`tokp`iUs', $l)) {
$link_without_token = preg_replace('`(\?tokp=.+$)`iUs', '', $link_without_token);
}
if (!preg_match('`\?`iUs', $liens[2][$key])) {
$link_with_token = $link_without_token . '?tokp=' . $res_campaign['token_presta'];
} else {
$link_with_token = $link_without_token . '&tokp=' . $res_campaign['token_presta'];
}
$html = str_replace($link_without_token, $link_with_token, $html);
}
}
$res = $mj_data->data('newsletter', $campaignId, 'HTML', 'text/html', $html, 'PUT', 'LAST')->getResponse();
}
$response = [
'code' => 1,
'continue' => true,
'continue_address' => $data->next_step_url,
];
} else {
$response = [
'code' => 0,
'continue' => false,
'exit_url' => 'SOME URL ADDRESS',
];
}
header('Content-Type: application/json');
echo json_encode($response);