Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Commit 01cf4ff

Browse files
author
Joe Axon
committed
Escape field values as per line protocol
1 parent 22ff83e commit 01cf4ff

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/InfluxDB/Point.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public function __toString()
8282
$string = $this->measurement;
8383

8484
if (count($this->tags) > 0) {
85-
$string .= ',' . $this->arrayToString($this->escapeCharacters($this->tags));
85+
$string .= ',' . $this->arrayToString($this->escapeTags($this->tags));
8686
}
8787

88-
$string .= ' ' . $this->arrayToString($this->escapeCharacters($this->fields));
88+
$string .= ' ' . $this->arrayToString($this->escapeFields($this->fields));
8989

9090
if ($this->timestamp) {
9191
$string .= ' '.$this->timestamp;
@@ -143,7 +143,7 @@ public function setFields($fields)
143143
if (is_integer($field)) {
144144
$field = sprintf('%di', $field);
145145
} elseif (is_string($field)) {
146-
$field = sprintf("\"%s\"", $field);
146+
$field = $this->escapeFieldValue($field);
147147
} elseif (is_bool($field)) {
148148
$field = ($field ? "true" : "false");
149149
}
@@ -174,7 +174,7 @@ public function setTimestamp($timestamp)
174174
* @param array $arr
175175
* @return array
176176
*/
177-
private function escapeCharacters(array $arr)
177+
private function escapeTags(array $arr)
178178
{
179179
$returnArr = [];
180180

@@ -185,6 +185,35 @@ private function escapeCharacters(array $arr)
185185
return $returnArr;
186186
}
187187

188+
/**
189+
* Escapes invalid characters in field keys and values
190+
*
191+
* @param array $arr
192+
* @return array
193+
*/
194+
private function escapeFields(array $arr)
195+
{
196+
$returnArr = [];
197+
198+
foreach($arr as $key => $value) {
199+
$returnArr[$this->addSlashes($key)] = $value;
200+
}
201+
202+
return $returnArr;
203+
}
204+
205+
/*
206+
* Returns string double-quoted and double-quotes escaped per Influx write protocol syntax
207+
*
208+
* @param string $value
209+
* @return string
210+
*/
211+
private function escapeFieldValue($value)
212+
{
213+
$escapedValue = str_replace('"', '\"', $value);
214+
return sprintf('"%s"', $escapedValue);
215+
}
216+
188217
/**
189218
* Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax
190219
*
@@ -244,7 +273,5 @@ private function isValidTimeStamp($timestamp)
244273
}
245274

246275
return true;
247-
248-
249276
}
250277
}

tests/unit/PointTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public function testGettersAndSetters()
7474

7575
}
7676

77+
public function testFieldValueStringEscaping()
78+
{
79+
$expected = 'instance,host=server01,region=us-west spaces="string with spaces",doublequote="the \" is escaped"';
80+
$point = $this->getPoint(null);
81+
82+
$point->setFields(['spaces' => 'string with spaces', 'doublequote' => 'the " is escaped']);
83+
84+
$this->assertEquals($expected, (string) $point);
85+
}
7786

7887
/**
7988
* Provide wrong timestamp value for testing.

0 commit comments

Comments
 (0)