Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit c56c3f9

Browse files
committed
Bug fixes and supporting WordPress native HTTP client
1 parent 2ded0e2 commit c56c3f9

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

ApplicationInsights/Channel/Contracts/Utils.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ public static function returnISOStringForTime($time = null)
6767
return gmdate('c', $time) . 'Z';
6868
}
6969
}
70+
71+
/**
72+
* Returns a Guid on all flavors of PHP. Copied from the PHP manual: http://php.net/manual/en/function.com-create-guid.php
73+
* @return mixed
74+
*/
75+
public static function returnGuid()
76+
{
77+
if (function_exists('com_create_guid') === true)
78+
{
79+
return trim(com_create_guid(), '{}');
80+
}
81+
82+
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
83+
84+
}
7085
}

ApplicationInsights/Channel/Telemetry_Channel.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,38 @@ public function send()
148148

149149
$serializedTelemetryItem = $this->getSerializedQueue();
150150

151-
$client = new \GuzzleHttp\Client();
152-
153-
$response = $client->post($this->_endpointUrl, [
154-
'headers' => ['Accept' => 'application/json',
155-
'Content-Type' => 'application/json; charset=utf-8'],
156-
'body' => utf8_encode($serializedTelemetryItem),
157-
'verify' => false /* If you want to verify, you can, but you will need to provide proper CA bundle. See http://guzzle.readthedocs.org/en/latest/clients.html#verify-option */
158-
,'proxy' => '127.0.0.1:8888' /* For Fiddler debugging */
159-
]);
151+
$headersArray = ['Accept' => 'application/json',
152+
'Content-Type' => 'application/json; charset=utf-8'];
153+
154+
if (array_key_exists('HTTP_USER_AGENT', $_SERVER) == true)
155+
{
156+
$headersArray['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
157+
}
158+
159+
$body = utf8_encode($serializedTelemetryItem);
160+
161+
if (class_exists('\GuzzleHttp\Client') == true)
162+
{
163+
// Standard case if properly pulled in composer dependencies
164+
165+
$client = new \GuzzleHttp\Client();
166+
167+
$client->post($this->_endpointUrl, [
168+
'headers' => $headersArray,
169+
'body' => $body,
170+
'verify' => false /* If you want to verify, you can, but you will need to provide proper CA bundle. See http://guzzle.readthedocs.org/en/latest/clients.html#verify-option */
171+
//,'proxy' => '127.0.0.1:8888' /* For Fiddler debugging */
172+
]);
173+
}
174+
else if (function_exists('wp_remote_post'))
175+
{
176+
// Used in WordPress
177+
wp_remote_post($this->_endpointUrl, [
178+
'method' => 'POST',
179+
'blocking' => true,
180+
'headers' => $headersArray,
181+
'body' => $body
182+
]);
183+
}
160184
}
161185
}

ApplicationInsights/Current_User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function __construct()
2727

2828
if ($this->id == NULL)
2929
{
30-
$this->id = trim(com_create_guid(), '{}');
30+
$this->id = \ApplicationInsights\Channel\Contracts\Utils::returnGuid();
3131
$_COOKIE['ai_user'] = $this->id;
3232
}
3333
}

ApplicationInsights/Tests/Current_Session_Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Current_Session_Test extends \PHPUnit_Framework_TestCase
1212

1313
protected function setUp()
1414
{
15-
$this->sessionId = trim(com_create_guid(), '{}');
15+
$this->sessionId = \ApplicationInsights\Channel\Contracts\Utils::returnGuid();
1616
$this->sessionCreatedTime = time();
1717
$this->sessionLastRenewedTime = time() - 10000;
1818
Utils::setSessionCookie($this->sessionId, $this->sessionCreatedTime, $this->sessionLastRenewedTime);

ApplicationInsights/Tests/Current_User_Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Current_User_Test extends \PHPUnit_Framework_TestCase
1010

1111
protected function setUp()
1212
{
13-
$this->userId = trim(com_create_guid(), '{}');
13+
$this->userId = \ApplicationInsights\Channel\Contracts\Utils::returnGuid();
1414
Utils::setUserCookie($this->userId);
1515
}
1616

ApplicationInsights/Tests/Utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static function throwNestedException($depth = 0)
138138
*/
139139
public static function setUserCookie($userId = NULL)
140140
{
141-
$_COOKIE['ai_user'] = $userId == NULL ? trim(com_create_guid(), '{}') : $userId;
141+
$_COOKIE['ai_user'] = $userId == NULL ? \ApplicationInsights\Channel\Contracts\Utils::returnGuid() : $userId;
142142
}
143143

144144
/**
@@ -154,7 +154,7 @@ public static function clearUserCookie()
154154
*/
155155
public static function setSessionCookie($sessionId = NULL, $sessionCreatedDate = NULL, $lastRenewedDate = NULL)
156156
{
157-
$sessionId = $sessionId == NULL ? trim(com_create_guid(), '{}') : $sessionId;
157+
$sessionId = $sessionId == NULL ? \ApplicationInsights\Channel\Contracts\Utils::returnGuid() : $sessionId;
158158

159159
$sessionCreatedDate == NULL ? $sessionCreatedDate = time() : $sessionCreatedDate;
160160
$lastRenewedDate == NULL ? $lastRenewedDate = time() : $lastRenewedDate;

0 commit comments

Comments
 (0)