-
Notifications
You must be signed in to change notification settings - Fork 11
/
CrmAuth.php
411 lines (369 loc) · 19.1 KB
/
CrmAuth.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
// include "CrmAuthenticationHeader.php";
class CrmAuth {
/**
* Gets a CRM Online SOAP header & expiration.
*
* @return CrmAuthenticationHeader An object containing the SOAP header and expiration date/time of the header.
* @param String $username
* Username of a valid CRM user.
* @param String $password
* Password of a valid CRM user.
* @param String $url
* The Url of the CRM Online organization (https://org.crm.dynamics.com).
*/
public function GetHeaderOnline($username, $password, $url) {
$url .= (substr ( $url, - 1 ) == '/' ? '' : '/');
$urnAddress = $this->GetUrnOnline ( $url );
$now = $_SERVER ['REQUEST_TIME'];
$xml = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">";
$xml .= "<s:Header>";
$xml .= "<a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>";
$xml .= "<a:MessageID>urn:uuid:" . $this->getGUID () . "</a:MessageID>";
$xml .= "<a:ReplyTo>";
$xml .= "<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>";
$xml .= "</a:ReplyTo>";
$xml .= "<a:To s:mustUnderstand=\"1\">https://login.microsoftonline.com/RST2.srf</a:To>";
$xml .= "<o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<u:Timestamp u:Id=\"_0\">";
$xml .= "<u:Created>" . gmdate ( 'Y-m-d\TH:i:s.u\Z', $now ) . "</u:Created>";
$xml .= "<u:Expires>" . gmdate ( 'Y-m-d\TH:i:s.u\Z', strtotime ( '+60 minute', $now ) ) . "</u:Expires>";
$xml .= "</u:Timestamp>";
$xml .= "<o:UsernameToken u:Id=\"uuid-" . $this->getGUID () . "-1\">";
$xml .= "<o:Username>" . $username . "</o:Username>";
$xml .= "<o:Password>" . $password . "</o:Password>";
$xml .= "</o:UsernameToken>";
$xml .= "</o:Security>";
$xml .= "</s:Header>";
$xml .= "<s:Body>";
$xml .= "<trust:RequestSecurityToken xmlns:trust=\"http://schemas.xmlsoap.org/ws/2005/02/trust\">";
$xml .= "<wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">";
$xml .= "<a:EndpointReference>";
$xml .= "<a:Address>urn:" . $urnAddress . "</a:Address>";
$xml .= "</a:EndpointReference>";
$xml .= "</wsp:AppliesTo>";
$xml .= "<trust:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</trust:RequestType>";
$xml .= "</trust:RequestSecurityToken>";
$xml .= "</s:Body>";
$xml .= "</s:Envelope>";
$headers = array (
"POST " . "/RST2.srf" . " HTTP/1.1",
"Host: " . "login.microsoftonline.com",
'Connection: Keep-Alive',
"Content-type: application/soap+xml; charset=UTF-8",
"Content-length: " . strlen ( $xml )
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, "https://login.microsoftonline.com/RST2.srf" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml );
$response = curl_exec ( $ch );
curl_close ( $ch );
$responsedom = new DomDocument ();
$responsedom->loadXML ( $response );
$cipherValues = $responsedom->getElementsbyTagName ( "CipherValue" );
$token1 = $cipherValues->item ( 0 )->textContent;
$token2 = $cipherValues->item ( 1 )->textContent;
$keyIdentiferValues = $responsedom->getElementsbyTagName ( "KeyIdentifier" );
$keyIdentifer = $keyIdentiferValues->item ( 0 )->textContent;
$tokenExpiresValues = $responsedom->getElementsbyTagName ( "Expires" );
$tokenExpires = $tokenExpiresValues->item ( 0 )->textContent;
$authHeader = new CrmAuthenticationHeader ();
$authHeader->Expires = $tokenExpires;
$authHeader->Header = $this->CreateSoapHeaderOnline ( $url, $keyIdentifer, $token1, $token2 );
return $authHeader;
}
/**
* Gets a CRM Online SOAP header.
*
* @return String The XML SOAP header to be used in future requests.
* @param String $url
* The Url of the CRM Online organization (https://org.crm.dynamics.com).
* @param String $keyIdentifer
* The KeyIdentifier from the initial request.
* @param String $token1
* The first token from the initial request.
* @param String $token2
* The second token from the initial request.
*/
function CreateSoapHeaderOnline($url, $keyIdentifer, $token1, $token2) {
$xml = "<s:Header>";
$xml .= "<a:Action s:mustUnderstand=\"1\">http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute</a:Action>";
$xml .= "<Security xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<EncryptedData Id=\"Assertion0\" Type=\"http://www.w3.org/2001/04/xmlenc#Element\" xmlns=\"http://www.w3.org/2001/04/xmlenc#\">";
$xml .= "<EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#tripledes-cbc\"/>";
$xml .= "<ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">";
$xml .= "<EncryptedKey>";
$xml .= "<EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\"/>";
$xml .= "<ds:KeyInfo Id=\"keyinfo\">";
$xml .= "<wsse:SecurityTokenReference xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<wsse:KeyIdentifier EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\" ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier\">" . $keyIdentifer . "</wsse:KeyIdentifier>";
$xml .= "</wsse:SecurityTokenReference>";
$xml .= "</ds:KeyInfo>";
$xml .= "<CipherData>";
$xml .= "<CipherValue>" . $token1 . "</CipherValue>";
$xml .= "</CipherData>";
$xml .= "</EncryptedKey>";
$xml .= "</ds:KeyInfo>";
$xml .= "<CipherData>";
$xml .= "<CipherValue>" . $token2 . "</CipherValue>";
$xml .= "</CipherData>";
$xml .= "</EncryptedData>";
$xml .= "</Security>";
$xml .= "<a:MessageID>urn:uuid:" . $this->getGUID () . "</a:MessageID>";
$xml .= "<a:ReplyTo>";
$xml .= "<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>";
$xml .= "</a:ReplyTo>";
$xml .= "<a:To s:mustUnderstand=\"1\">" . $url . "XRMServices/2011/Organization.svc</a:To>";
$xml .= "</s:Header>";
return $xml;
}
/**
* Gets the correct URN Address based on the Online region.
*
* @return String URN Address.
* @param String $url
* The Url of the CRM Online organization (https://org.crm.dynamics.com).
*/
function GetUrnOnline($url) {
if (strpos ( strtoupper ( $url ), "CRM2.DYNAMICS.COM" )) {
return "crmsam:dynamics.com";
}
if (strpos ( strtoupper ( $url ), "CRM4.DYNAMICS.COM" )) {
return "crmemea:dynamics.com";
}
if (strpos ( strtoupper ( $url ), "CRM5.DYNAMICS.COM" )) {
return "crmapac:dynamics.com";
}
if (strpos ( strtoupper ( $url ), "CRM6.DYNAMICS.COM" )) {
return "crmoce:dynamics.com";
}
if (strpos ( strtoupper ( $url ), "CRM7.DYNAMICS.COM" )) {
return "crmjpn:dynamics.com";
}
if (strpos ( strtoupper ( $url ), "CRM9.DYNAMICS.COM" )) {
return "crmgcc:dynamics.com";
}
return "crmna:dynamics.com";
}
/**
* Gets a CRM On Premise SOAP header & expiration.
*
* @return CrmAuthenticationHeader An object containing the SOAP header and expiration date/time of the header.
* @param String $username
* Username of a valid CRM user.
* @param String $password
* Password of a valid CRM user.
* @param String $url
* The Url of the CRM On Premise (IFD) organization (https://org.domain.com).
*/
function GetHeaderOnPremise($username, $password, $url) {
$url .= (substr ( $url, - 1 ) == '/' ? '' : '/');
$adfsUrl = $this->GetADFS ( $url );
$now = $_SERVER ['REQUEST_TIME'];
$urnAddress = $url . "XRMServices/2011/Organization.svc";
$usernamemixed = $adfsUrl . "/13/usernamemixed";
$xml = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\">";
$xml .= "<s:Header>";
$xml .= "<a:Action s:mustUnderstand=\"1\">http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</a:Action>";
$xml .= "<a:MessageID>urn:uuid:" . $this->getGUID () . "</a:MessageID>";
$xml .= "<a:ReplyTo>";
$xml .= "<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>";
$xml .= "</a:ReplyTo>";
$xml .= "<Security s:mustUnderstand=\"1\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<u:Timestamp u:Id=\"" . $this->getGUID () . "\">";
$xml .= "<u:Created>" . gmdate ( 'Y-m-d\TH:i:s.u\Z', $now ) . "</u:Created>";
$xml .= "<u:Expires>" . gmdate ( 'Y-m-d\TH:i:s.u\Z', strtotime ( '+60 minute', $now ) ) . "</u:Expires>";
$xml .= "</u:Timestamp>";
$xml .= "<UsernameToken u:Id=\"" . $this->getGUID () . "\">";
$xml .= "<Username>" . $username . "</Username>";
$xml .= "<Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" . $password . "</Password>";
$xml .= "</UsernameToken>";
$xml .= "</Security>";
$xml .= "<a:To s:mustUnderstand=\"1\">" . $usernamemixed . "</a:To>";
$xml .= "</s:Header>";
$xml .= "<s:Body>";
$xml .= "<trust:RequestSecurityToken xmlns:trust=\"http://docs.oasis-open.org/ws-sx/ws-trust/200512\">";
$xml .= "<wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">";
$xml .= "<a:EndpointReference>";
$xml .= "<a:Address>" . $urnAddress . "</a:Address>";
$xml .= "</a:EndpointReference>";
$xml .= "</wsp:AppliesTo>";
$xml .= "<trust:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</trust:RequestType>";
$xml .= "</trust:RequestSecurityToken>";
$xml .= "</s:Body>";
$xml .= "</s:Envelope>";
$headers = array (
"POST " . parse_url ( $usernamemixed, PHP_URL_PATH ) . " HTTP/1.1",
"Host: " . parse_url ( $adfsUrl, PHP_URL_HOST ),
'Connection: Keep-Alive',
"Content-type: application/soap+xml; charset=UTF-8",
"Content-length: " . strlen ( $xml )
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $usernamemixed );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml );
$response = curl_exec ( $ch );
curl_close ( $ch );
$responsedom = new DomDocument ();
$responsedom->loadXML ( $response );
$cipherValues = $responsedom->getElementsbyTagName ( "CipherValue" );
$token1 = $cipherValues->item ( 0 )->textContent;
$token2 = $cipherValues->item ( 1 )->textContent;
$keyIdentiferValues = $responsedom->getElementsbyTagName ( "KeyIdentifier" );
$keyIdentifer = $keyIdentiferValues->item ( 0 )->textContent;
$x509IssuerNames = $responsedom->getElementsbyTagName ( "X509IssuerName" );
$x509IssuerName = $x509IssuerNames->item ( 0 )->textContent;
$x509SerialNumbers = $responsedom->getElementsbyTagName ( "X509SerialNumber" );
$x509SerialNumber = $x509SerialNumbers->item ( 0 )->textContent;
$binarySecrets = $responsedom->getElementsbyTagName ( "BinarySecret" );
$binarySecret = $binarySecrets->item ( 0 )->textContent;
$created = gmdate ( 'Y-m-d\TH:i:s.u\Z', strtotime ( '-1 minute', $now ) );
$expires = gmdate ( 'Y-m-d\TH:i:s.u\Z', strtotime ( '+5 minute', $now ) );
$timestamp = "<u:Timestamp xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" u:Id=\"_0\"><u:Created>" . $created . "</u:Created><u:Expires>" . $expires . "</u:Expires></u:Timestamp>";
$hashedDataBytes = sha1 ( $timestamp, true );
$digestValue = base64_encode ( $hashedDataBytes );
$signedInfo = "<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"></CanonicalizationMethod><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\"></SignatureMethod><Reference URI=\"#_0\"><Transforms><Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"></Transform></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"></DigestMethod><DigestValue>" . $digestValue . "</DigestValue></Reference></SignedInfo>";
$binarySecretBytes = base64_decode ( $binarySecret );
$hmacHash = hash_hmac ( "sha1", $signedInfo, $binarySecretBytes, true );
$signatureValue = base64_encode ( $hmacHash );
$tokenExpiresValues = $responsedom->getElementsbyTagName ( "Expires" );
$tokenExpires = $tokenExpiresValues->item ( 0 )->textContent;
$authHeader = new CrmAuthenticationHeader ();
$authHeader->Expires = $tokenExpires;
$authHeader->Header = $this->CreateSoapHeaderOnPremise ( $url, $keyIdentifer, $token1, $token2, $x509IssuerName, $x509SerialNumber, $signatureValue, $digestValue, $created, $expires );
return $authHeader;
}
/**
* Gets a CRM On Premise (IFD) SOAP header.
*
* @return String SOAP Header XML.
* @param String $url
* The Url of the CRM On Premise (IFD) organization (https://org.domain.com).
* @param String $keyIdentifer
* The KeyIdentifier from the initial request.
* @param String $token1
* The first token from the initial request.
* @param String $token2
* The second token from the initial request.
* @param String $x509IssuerName
* The certificate issuer.
* @param String $x509SerialNumber
* The certificate serial number.
* @param String $signatureValue
* The hashsed value of the header signature.
* @param String $digestValue
* The hashed value of the header timestamp.
* @param String $created
* The header created date/time.
* @param String $expires
* The header expiration date/tim.
*/
function CreateSoapHeaderOnPremise($url, $keyIdentifer, $token1, $token2, $x509IssuerName, $x509SerialNumber, $signatureValue, $digestValue, $created, $expires) {
$xml = "<s:Header>";
$xml .= "<a:Action s:mustUnderstand=\"1\">http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute</a:Action>";
$xml .= "<a:MessageID>urn:uuid:" . $this->getGUID () . "</a:MessageID>";
$xml .= "<a:ReplyTo>";
$xml .= "<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>";
$xml .= "</a:ReplyTo>";
$xml .= "<a:To s:mustUnderstand=\"1\">" . $url . "XRMServices/2011/Organization.svc</a:To>";
$xml .= "<o:Security xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<u:Timestamp xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" u:Id=\"_0\">";
$xml .= "<u:Created>" . $created . "</u:Created>";
$xml .= "<u:Expires>" . $expires . "</u:Expires>";
$xml .= "</u:Timestamp>";
$xml .= "<xenc:EncryptedData Type=\"http://www.w3.org/2001/04/xmlenc#Element\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\">";
$xml .= "<xenc:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#aes256-cbc\"/>";
$xml .= "<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">";
$xml .= "<e:EncryptedKey xmlns:e=\"http://www.w3.org/2001/04/xmlenc#\">";
$xml .= "<e:EncryptionMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\">";
$xml .= "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>";
$xml .= "</e:EncryptionMethod>";
$xml .= "<KeyInfo>";
$xml .= "<o:SecurityTokenReference xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<X509Data>";
$xml .= "<X509IssuerSerial>";
$xml .= "<X509IssuerName>" . $x509IssuerName . "</X509IssuerName>";
$xml .= "<X509SerialNumber>" . $x509SerialNumber . "</X509SerialNumber>";
$xml .= "</X509IssuerSerial>";
$xml .= "</X509Data>";
$xml .= "</o:SecurityTokenReference>";
$xml .= "</KeyInfo>";
$xml .= "<e:CipherData>";
$xml .= "<e:CipherValue>" . $token1 . "</e:CipherValue>";
$xml .= "</e:CipherData>";
$xml .= "</e:EncryptedKey>";
$xml .= "</KeyInfo>";
$xml .= "<xenc:CipherData>";
$xml .= "<xenc:CipherValue>" . $token2 . "</xenc:CipherValue>";
$xml .= "</xenc:CipherData>";
$xml .= "</xenc:EncryptedData>";
$xml .= "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">";
$xml .= "<SignedInfo>";
$xml .= "<CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>";
$xml .= "<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\"/>";
$xml .= "<Reference URI=\"#_0\">";
$xml .= "<Transforms>";
$xml .= "<Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>";
$xml .= "</Transforms>";
$xml .= "<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>";
$xml .= "<DigestValue>" . $digestValue . "</DigestValue>";
$xml .= "</Reference>";
$xml .= "</SignedInfo>";
$xml .= "<SignatureValue>" . $signatureValue . "</SignatureValue>";
$xml .= "<KeyInfo>";
$xml .= "<o:SecurityTokenReference xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">";
$xml .= "<o:KeyIdentifier ValueType=\"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID\">" . $keyIdentifer . "</o:KeyIdentifier>";
$xml .= "</o:SecurityTokenReference>";
$xml .= "</KeyInfo>";
$xml .= "</Signature>";
$xml .= "</o:Security>";
$xml .= "</s:Header>";
return $xml;
}
/**
* Gets the name of the AD FS server CRM uses for authentication.
*
* @return String The AD FS server url.
* @param String $url
* The Url of the CRM On Premise (IFD) organization (https://org.domain.com).
*/
function GetADFS($url) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url . "XrmServices/2011/Organization.svc?wsdl=wsdl0" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
$response = curl_exec ( $ch );
curl_close ( $ch );
$responsedom = new DomDocument ();
$responsedom->loadXML ( $response );
$identifiers = $responsedom->getElementsbyTagName ( "Identifier" );
$identifier = $identifiers->item ( 0 )->textContent;
return str_replace ( "http://", "https://", $identifier );
}
// http://stackoverflow.com/questions/18206851/com-create-guid-function-got-error-on-server-side-but-works-fine-in-local-usin
function getGUID() {
if (function_exists ( 'com_create_guid' )) {
return com_create_guid ();
} else {
mt_srand ( ( double ) microtime () * 10000 ); // optional for php 4.2.0 and up.
$charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) );
$hyphen = chr ( 45 ); // "-"
$uuid = chr ( 123 ) . // "{"
substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 ) . chr ( 125 ); // "}"
return $uuid;
}
}
}