Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/Gelf
File: Publisher.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class Gelf_Publisher
[2] Fix | Delete
{
[3] Fix | Delete
const CHUNK_SIZE_WAN = 1420;
[4] Fix | Delete
[5] Fix | Delete
const CHUNK_SIZE_LAN = 8154;
[6] Fix | Delete
[7] Fix | Delete
const GRAYLOG2_DEFAULT_PORT = 12201;
[8] Fix | Delete
[9] Fix | Delete
const GRAYLOG2_PROTOCOL_VERSION = '1.0';
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* @var string
[13] Fix | Delete
*/
[14] Fix | Delete
protected $hostname;
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* @var int
[18] Fix | Delete
*/
[19] Fix | Delete
protected $port;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* @var int|null
[23] Fix | Delete
*/
[24] Fix | Delete
protected $fallbackPort;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* @var int
[28] Fix | Delete
*/
[29] Fix | Delete
protected $chunkSize;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* @var resource|null
[33] Fix | Delete
*/
[34] Fix | Delete
protected $streamSocketClient = null;
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* @var bool
[38] Fix | Delete
*/
[39] Fix | Delete
private static $brokenSocket = false;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Creates a new publisher that sends errors to a Graylog2 server via UDP
[43] Fix | Delete
*
[44] Fix | Delete
* @throws InvalidArgumentException
[45] Fix | Delete
*
[46] Fix | Delete
* @param string $hostname
[47] Fix | Delete
* @param integer $port
[48] Fix | Delete
* @param integer|null $fallbackPort
[49] Fix | Delete
* @param integer $chunkSize
[50] Fix | Delete
*/
[51] Fix | Delete
public function __construct($hostname, $port = null, $fallbackPort = null, $chunkSize = null)
[52] Fix | Delete
{
[53] Fix | Delete
// Check whether the parameters are set correctly
[54] Fix | Delete
if (!$hostname) {
[55] Fix | Delete
throw new InvalidArgumentException('$hostname must be set');
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
if ($port === null) {
[59] Fix | Delete
$port = self::GRAYLOG2_DEFAULT_PORT;
[60] Fix | Delete
} elseif (!is_numeric($port)) {
[61] Fix | Delete
throw new InvalidArgumentException('$port must be an integer');
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
if ($fallbackPort !== null && !is_numeric($fallbackPort)) {
[65] Fix | Delete
throw new InvalidArgumentException('$fallbackPort must be an integer');
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
if ($chunkSize === null) {
[69] Fix | Delete
$chunkSize = self::CHUNK_SIZE_WAN;
[70] Fix | Delete
} elseif (!is_numeric($chunkSize)) {
[71] Fix | Delete
throw new InvalidArgumentException('$chunkSize must be an integer');
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
$this->hostname = $hostname;
[75] Fix | Delete
$this->port = $port;
[76] Fix | Delete
$this->fallbackPort = $fallbackPort;
[77] Fix | Delete
$this->chunkSize = $chunkSize;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Publishes a Gelf_Message, returns false if an error occurred during write.
[82] Fix | Delete
*
[83] Fix | Delete
* @throws UnexpectedValueException
[84] Fix | Delete
*
[85] Fix | Delete
* @param Gelf_Message $message
[86] Fix | Delete
*
[87] Fix | Delete
* @return boolean
[88] Fix | Delete
*/
[89] Fix | Delete
public function publish(Gelf_Message $message)
[90] Fix | Delete
{
[91] Fix | Delete
if (self::$brokenSocket) {
[92] Fix | Delete
return false;
[93] Fix | Delete
}
[94] Fix | Delete
// Check if required message parameters are set
[95] Fix | Delete
if (!$message->getShortMessage() || !$message->getHost()) {
[96] Fix | Delete
throw new UnexpectedValueException(
[97] Fix | Delete
'Missing required data parameter: "version", "short_message" and "host" are required.'
[98] Fix | Delete
);
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
// Set Graylog protocol version
[102] Fix | Delete
$message->setVersion(self::GRAYLOG2_PROTOCOL_VERSION);
[103] Fix | Delete
[104] Fix | Delete
// Encode the message as json string and compress it using gzip
[105] Fix | Delete
$preparedMessage = $this->getPreparedMessage($message);
[106] Fix | Delete
[107] Fix | Delete
// Infinite-loop break.
[108] Fix | Delete
self::$brokenSocket = true;
[109] Fix | Delete
// Open a connection to GrayLog server.
[110] Fix | Delete
$socket = $this->getSocketConnection();
[111] Fix | Delete
[112] Fix | Delete
if (!$socket) {
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
self::$brokenSocket = false;
[116] Fix | Delete
[117] Fix | Delete
// Several udp writes are required to publish the message
[118] Fix | Delete
if ($this->isMessageSizeGreaterChunkSize($preparedMessage)) {
[119] Fix | Delete
// A unique id which consists of the microtime and a random value
[120] Fix | Delete
$messageId = $this->getMessageId();
[121] Fix | Delete
[122] Fix | Delete
// Split the message into chunks.
[123] Fix | Delete
$messageChunks = $this->getMessageChunks($preparedMessage);
[124] Fix | Delete
$messageChunksCount = count($messageChunks);
[125] Fix | Delete
[126] Fix | Delete
// Send chunks to GrayLog server.
[127] Fix | Delete
foreach (array_values($messageChunks) as $messageChunkIndex => $messageChunk) {
[128] Fix | Delete
$bytesWritten = $this->writeMessageChunkToSocket(
[129] Fix | Delete
$socket,
[130] Fix | Delete
$messageId,
[131] Fix | Delete
$messageChunk,
[132] Fix | Delete
$messageChunkIndex,
[133] Fix | Delete
$messageChunksCount
[134] Fix | Delete
);
[135] Fix | Delete
[136] Fix | Delete
if (false === $bytesWritten) {
[137] Fix | Delete
// Abort due to write error
[138] Fix | Delete
return false;
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
} else {
[142] Fix | Delete
// A single write is enough to get the message published
[143] Fix | Delete
if (false === $this->writeMessageToSocket($socket, $preparedMessage)) {
[144] Fix | Delete
// Abort due to write error
[145] Fix | Delete
return false;
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
// This increases stability a lot if messages are sent in a loop
[150] Fix | Delete
// A value of 20 means 0.02 ms
[151] Fix | Delete
usleep(20);
[152] Fix | Delete
[153] Fix | Delete
// Message successful sent
[154] Fix | Delete
return true;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* @param Gelf_Message $message
[159] Fix | Delete
*
[160] Fix | Delete
* @return string
[161] Fix | Delete
*/
[162] Fix | Delete
protected function getPreparedMessage(Gelf_Message $message)
[163] Fix | Delete
{
[164] Fix | Delete
return gzcompress(json_encode($message->toArray()));
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* @return resource|false
[169] Fix | Delete
*/
[170] Fix | Delete
protected function getSocketConnection()
[171] Fix | Delete
{
[172] Fix | Delete
if (!$this->streamSocketClient) {
[173] Fix | Delete
$hostname = gethostbyname($this->hostname);
[174] Fix | Delete
$this->streamSocketClient = stream_socket_client(sprintf('udp://%s:%d', $hostname, $this->port));
[175] Fix | Delete
if ($this->streamSocketClient === false && $this->fallbackPort) {
[176] Fix | Delete
$this->streamSocketClient = stream_socket_client(sprintf('tcp://%s:%d', $hostname, $this->fallbackPort));
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
return $this->streamSocketClient;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
/**
[184] Fix | Delete
* @param string $preparedMessage
[185] Fix | Delete
*
[186] Fix | Delete
* @return boolean
[187] Fix | Delete
*/
[188] Fix | Delete
protected function isMessageSizeGreaterChunkSize($preparedMessage)
[189] Fix | Delete
{
[190] Fix | Delete
return (strlen($preparedMessage) > $this->chunkSize);
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* @return float
[195] Fix | Delete
*/
[196] Fix | Delete
protected function getMessageId()
[197] Fix | Delete
{
[198] Fix | Delete
return (float)(microtime(true).mt_rand(0, 10000));
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* @param string $preparedMessage
[203] Fix | Delete
*
[204] Fix | Delete
* @return array
[205] Fix | Delete
*/
[206] Fix | Delete
protected function getMessageChunks($preparedMessage)
[207] Fix | Delete
{
[208] Fix | Delete
return str_split($preparedMessage, $this->chunkSize);
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* @param float $messageId
[213] Fix | Delete
* @param string $data
[214] Fix | Delete
* @param integer $sequence
[215] Fix | Delete
* @param integer $sequenceSize
[216] Fix | Delete
*
[217] Fix | Delete
* @throws InvalidArgumentException
[218] Fix | Delete
* @return string
[219] Fix | Delete
*/
[220] Fix | Delete
protected function prependChunkInformation($messageId, $data, $sequence, $sequenceSize)
[221] Fix | Delete
{
[222] Fix | Delete
if (!is_string($data) || $data === '') {
[223] Fix | Delete
throw new InvalidArgumentException('Data must be a string and not be empty.');
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
if (!is_integer($sequence) || !is_integer($sequenceSize)) {
[227] Fix | Delete
throw new InvalidArgumentException('Sequence number and size must be integer.');
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
if ($sequenceSize <= 0) {
[231] Fix | Delete
throw new InvalidArgumentException('Sequence size must be greater than 0.');
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
if ($sequence > $sequenceSize) {
[235] Fix | Delete
throw new InvalidArgumentException('Sequence size must be greater than sequence number.');
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
return pack('CC', 30, 15).substr(md5($messageId, true), 0, 8).pack('CC', $sequence, $sequenceSize).$data;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
/**
[242] Fix | Delete
* @param resource $socket
[243] Fix | Delete
* @param float $messageId
[244] Fix | Delete
* @param string $messageChunk
[245] Fix | Delete
* @param integer $messageChunkIndex
[246] Fix | Delete
* @param integer $messageChunksCount
[247] Fix | Delete
*
[248] Fix | Delete
* @return integer|boolean
[249] Fix | Delete
*/
[250] Fix | Delete
protected function writeMessageChunkToSocket($socket, $messageId, $messageChunk, $messageChunkIndex, $messageChunksCount)
[251] Fix | Delete
{
[252] Fix | Delete
return fwrite(
[253] Fix | Delete
$socket,
[254] Fix | Delete
$this->prependChunkInformation($messageId, $messageChunk, $messageChunkIndex, $messageChunksCount)
[255] Fix | Delete
);
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
/**
[259] Fix | Delete
* @param resource $socket
[260] Fix | Delete
* @param string $preparedMessage
[261] Fix | Delete
*
[262] Fix | Delete
* @return integer|boolean
[263] Fix | Delete
*/
[264] Fix | Delete
protected function writeMessageToSocket($socket, $preparedMessage)
[265] Fix | Delete
{
[266] Fix | Delete
return fwrite($socket, $preparedMessage);
[267] Fix | Delete
}
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function