ECID.php 5.81 KB
Newer Older
Ubuntu's avatar
Ubuntu committed
1
2
3
4
5
<?php

namespace Adobe;

/**
Pedro Monjo's avatar
Pedro Monjo committed
6
 * ECID class
Ubuntu's avatar
Ubuntu committed
7
8
9
10
11
12
 */
class ECID {
	
	/**
	 * Supported version of the ECID service
	 */
13
	const VERSION = "5.2.0";
Ubuntu's avatar
Ubuntu committed
14
15
16
17
18
19
20
	
	/**
	 * MID
	 */
	private $mid;
	
	/**
21
	 * MID refresh period
Ubuntu's avatar
Ubuntu committed
22
23
24
25
	 */
	private $mid_refresh;
	
	/**
26
	 * UUID
Ubuntu's avatar
Ubuntu committed
27
	 */
28
	private $uuid;
Ubuntu's avatar
Ubuntu committed
29
30
31
32
	
	/**
	 * Demdex BLOB
	 */
33
	private $blob;
Ubuntu's avatar
Ubuntu committed
34
35
36
37
38
	
	/**
	 * DCS region
	 */
	private $dcs_region;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

	/**
	 * TTL
	 */
	private $ottl;

	/**
	 * TID
	 */
	private $tid;

	/**
	 * Subdomain
	 */
	private $subdomain;
Ubuntu's avatar
Ubuntu committed
54
55
	
	/**
56
	 * Link to configuration
Ubuntu's avatar
Ubuntu committed
57
	 */
58
	private $config;
Ubuntu's avatar
Ubuntu committed
59
60
	
	/**
61
	 * SDID
Ubuntu's avatar
Ubuntu committed
62
	 */
63
64
65
66
67
68
	private $sdid;
	
	/**
	 * Response string from dpm.demdex.net
	 */
	private $demdex_response;
Ubuntu's avatar
Ubuntu committed
69
	
70
71
72
73
74
75
76
77
78
	/**
	 * User agent of the browser
	 */
	private $user_agent;
	
	/**
	 * IP address of the client
	 */
	private $ip;
79

Ubuntu's avatar
Ubuntu committed
80
81
82
	/**
	 * Constructor
	 */
83
	public function __construct ($config,$user_agent,$ip_client) {
84
85
86
		$this->config = $config;
		$this->mid = null;
		$this->sdid = null;
87
88
		$this->user_agent = $user_agent;
		$this->ip = $ip_client;
Ubuntu's avatar
Ubuntu committed
89
90
91
	}
	
	/**
92
	 * Generate the URL to call the ECID service
Ubuntu's avatar
Ubuntu committed
93
	 */
Pedro Monjo's avatar
Pedro Monjo committed
94
	private function getURL($curl) {
95
96
97
98
99
		$url = 'http';
		if ($this->config->getMarketingCloudConfig(Config::MARKETINGCLOUD_SSL) === true) {
			$url .= 's';
		}
		$url .= '://dpm.demdex.net/id';
100
101
102
103
		$url .= '?d_visid_ver=' . curl_escape($curl,ECID::VERSION);
		$url .= '&d_fieldgroup=MC&d_rtbd=json&d_ver=2&d_verify=1&d_nsid=0';
		$url .= '&d_orgid=' . curl_escape($curl,$this->config->getMarketingCloudConfig(Config::MARKETINGCLOUD_ORG));
		$url .= '&ts=' . curl_escape($curl,time());
104
		if ($this->mid) {
105
			$url .= '&d_mid=' . $this->mid;
106
		}
107
		return $url;
Ubuntu's avatar
Ubuntu committed
108
109
110
	}
	
	/**
111
	 * Configure curl for the call to the ECID service
Ubuntu's avatar
Ubuntu committed
112
	 */
113
	private function curlSetopt($curl,$url) {
114
115
116
117
118
119
		$curlopts = array(
			CURLOPT_URL 			=> $url,
			CURLOPT_RETURNTRANSFER 	=> 1,
			CURLOPT_AUTOREFERER		=> TRUE,
			CURLOPT_FOLLOWLOCATION	=> TRUE,
			CURLOPT_COOKIEFILE 		=> "",
120
121
122
123
124
			CURLOPT_USERAGENT 		=> $this->user_agent,
			CURLOPT_PROTOCOLS		=> CURLPROTO_HTTP|CURLPROTO_HTTPS,
			CURLOPT_HTTPHEADER		=> array(
				'X-Forwarded-For: ' . $this->ip,
			)
125
126
		);
		curl_setopt_array($curl, $curlopts);
127
128
129
130
131
132
133
134
135
136
	}
	
	/**
	 * Parse the cookies from the HTTP response.
	 */
	private function parseCookies($curl) {
		$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
		foreach ($cookies as $c) {
			$m = preg_match('/\.demdex\.net.*\s(\d+)\sdemdex\s(\d+)/', $c, $matches);
			if ($m == 1) {
137
138
				$this->uuid = $matches[2];
				// $this->demdex_expiration = $matches[1];
139
				break;
Ubuntu's avatar
Ubuntu committed
140
141
			}
		}
142
143
144
145
146
147
148
149
150
	}
	
	/**
	 * Request a new ECID and associated parameters
	 */
	private function requestECID() {
		// Initialise curl
		$curl = curl_init();
		// Create the URL
Pedro Monjo's avatar
Pedro Monjo committed
151
		$url = $this->getURL($curl);
152
153
154
		// Prepare HTTP request
		$this->curlSetopt($curl,$url);
		// Make the HTTP request
155
		$this->demdex_response = curl_exec($curl);
156
157
158
		if ($this->demdex_response === FALSE) {
			throw new \Exception("Adobe\\ECID\\requestECID: " . curl_error($curl) . ": " . curl_errno($curl));
		}
159
		// Parse the result
160
		$this->fromJSON($this->demdex_response);
161
162
		// Parse cookies
		$this->parseCookies($curl);
Ubuntu's avatar
Ubuntu committed
163
		// Free resources
Pedro Monjo's avatar
Pedro Monjo committed
164
		curl_close($curl);		
Ubuntu's avatar
Ubuntu committed
165
	}
166
	
167
168
169
170
	/**
	 * Load the ECID, either from a previous session or by making a call to dpm.demdex.net.
	 */
	public function load() {
171
172
173
174
		$this->requestECID();
	}
	
	/**
175
	 * Load from a JSON object
176
	 */
177
	public function fromJSON($json) {
178
179
180
181
182
183
184
185
186
		$jd = json_decode($json,TRUE);
		if ($jd === NULL || isset($jd['errors'])) {
			// For now, just ignore JSON errors
			return;
		}
		$this->mid = isset($jd['d_mid']) ? $jd['d_mid'] : null;
		$this->blob = isset($jd['d_blob']) ? $jd['d_blob'] : null;
		$this->dcs_region = isset($jd['dcs_region']) ? $jd['dcs_region'] : null;
		$this->uuid = isset($jd['d_uuid']) ? $jd['d_uuid'] : null;
187
188
189
190
		$this->ottl = isset($jd['d_ottl']) ? $jd['d_ottl'] : null;
		$this->tid = isset($jd['tid']) ? $jd['tid'] : null;
		$this->subdomain = isset($jd['subdomain']) ? $jd['subdomain'] : null;
		// Other parameters to process: id_sync_ttl
191
192
	}
	
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	/**
	 * Load from AMCV cookie
	 */
	public function loadFromAMCVCookie($cookie) {
		$cookie = urldecode($cookie);
		$ca = explode('|',$cookie);
		$i = 0;
		while ($i < count($ca)) {
			if ($ca[$i] == "MCMID") {
				$this->mid = $ca[$i+1];
				$i+=2;
			}
			else if (substr($ca[$i],0,7) == "MCAAMLH") {
				$this->dcs_region = $ca[$i+1];
				$i+=2;
			}
			else if (substr($ca[$i],0,6) == "MCAAMB") {
				$this->blob = $ca[$i+1];
				$i+=2;
			}
			else {
				$i++;
			}
		}
	}
	
219
220
	/**
	 * Get the ECID value (internally known as MID)
221
	 */
222
	public function getECID() {
223
224
225
226
		return $this->mid;
	}
	
	/**
227
	 * Get the UUID (the demdex cookie value)
228
	 */
229
230
	public function getUUID() {
		return $this->uuid;
231
	}
232
233
234
235
236
	
	/**
	 * Get the AAM blob
	 */
	public function getBlob() {
237
		return $this->blob;
238
239
240
241
242
243
244
245
	}
	
	/**
	 * Get the DCS region
	 */
	public function getDCSRegion() {
		return $this->dcs_region;
	}
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

	/**
	 * Get TTL
	 */
	public function getTTL() {
		return $this->d_ottl;
	}

	/**
	 * Get TID
	 */
	public function getTID() {
		return $this->tid;
	}

	/**
	 * Get subdomain
	 */
	public function getSubdomain() {
		return $this->subdomain;
	}
267
268
269
270
271
272
273
	
	/**
	 * Get the response from dpm.demdex.net. Only valid when the ECID is a new one.
	 */
	public function getDemdexResponse() {
		return $this->demdex_response;
	}
274
275
276
277
278
279
280
281
282
283
284
285

	/**
	 * Get the SDID
	 */
	public function getSDID() {
		if (!isset($this->sdid)) {
			$sha = sha1(uniqid() . md5(rand())); // This should be 40 chars long
			$this->sdid = substr($sha,0,16) . '-' . substr($sha,16,16);
			$this->sdid = strtoupper($this->sdid);
		}
		return $this->sdid;
	}
286
287
288
289
290
291
292
293
	
	/**
	 * Convert the object into a JSON object, to be stored elsewhere
	 */
	public function toJSON() {
		$e = array(
			'd_mid'			=> $this->mid,
			'd_blob'		=> $this->blob,
294
295
296
297
298
			'dcs_region'	=> $this->dcs_region,
			'd_uuid'		=> $this->uuid,
			'd_ottl'		=> $this->ottl,
			'tid'			=> $this->tid,
			'subdomain'		=> $this->subdomain
299
300
301
		);
		return json_encode($e);
	}
Ubuntu's avatar
Ubuntu committed
302
}