3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php/* * This file is part of the SensioLabsProfiler SDK package. * * (c) SensioLabs <contact@sensiolabs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */class SprofilerHttpWrapper extends php_user_filter{ public $context; private $sslContext = array( 'SNI_server_name' => 'profiler.sensiolabs.com', ); private $socket; public function stream_open($path, $mode) { if ('wb' !== $mode) { return false; } $url = "ssl://{$this->sslContext['SNI_server_name']}:443"; $context = array('ssl' => $this->sslContext); $this->socket = stream_socket_client($url, $errno, $errmsg, 15, STREAM_CLIENT_CONNECT, stream_context_create($context)); stream_set_timeout($this->socket, 5); if (!$this->socket) { user_error($errmsg); return false; } if (!in_array('sprofiler.chunk', stream_get_filters())) { stream_filter_register('sprofiler.chunk', __CLASS__); } return true; } public function stream_write($data) { if (!$this->params) { $boundary = md5(mt_rand()*mt_rand()).md5(mt_rand()*mt_rand()); $this->params = compact('boundary'); $serverId = ''; $slotId = ''; $authToken = ''; $post = array(); foreach (explode("\n", $data) as $field) { $field = explode(":", $field, 2); switch ($field[0]) { case 'SensioLabsProfiler-Query': $authToken = trim($field[1]); parse_str($authToken, $field); foreach ($field as $k => $v) { if ('agentIds' === $k) { $v = explode(',', $v); $authToken = $v[0].':'.$authToken; foreach ($v as $v) { $post[] = array('agents[]', $v); } } else { if ('profileSlot' === $k) { $slotId = $v; } $post[] = array($k, $v); } } $authToken = base64_encode($authToken); break; case 'SensioLabsProfiler-Auth': $serverId = trim($field[1]); $serverId = "X-{$field[0]}: {$field[1]}\r\n"; break; } } $http = "POST /agent-api/v1/profile-slots/{$slotId} HTTP/1.1\r\n" ."Host: {$this->sslContext['SNI_server_name']}\r\n" ."User-Agent: SensioLabsProfiler-SDK\r\n" ."Authorization: Basic {$authToken}\r\n" ."Content-Type: multipart/form-data; boundary={$boundary}\r\n" ."Transfer-Encoding: chunked\r\n" .$serverId ."\r\n"; if (!$w = fwrite($this->socket, $http)) { return $w; } stream_filter_append($this->socket, 'sprofiler.chunk', STREAM_FILTER_WRITE, $this->params); $http = ''; foreach ($post as $field) { $http .= '--'.$boundary."\r\n" .'Content-Disposition: form-data; name="'.$field[0].'"'."\r\n" ."\r\n" .$field[1]."\r\n"; } $http .= '--'.$boundary."\r\n" ."Content-Disposition: form-data; name=\"payload\"; filename=\"graph.dat\"\r\n" ."Content-Type: application/octet-stream\r\n" ."\r\n"; if (!$w = fwrite($this->socket, $http)) { return $w; } if (in_array('zlib.*', stream_get_filters())) { stream_filter_prepend($this->socket, 'zlib.deflate', STREAM_FILTER_WRITE); } } elseif (!$w = fwrite($this->socket, $data)) { return $w; } return strlen($data); } public function stream_flush() { return fflush($this->socket); } public function stream_close() { fclose($this->socket); $this->socket = $this->params = null; } public function stream_lock() { return false; } public function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $consumed += $bucket->datalen; $bucket->data = dechex($bucket->datalen)."\r\n{$bucket->data}\r\n"; stream_bucket_append($out, $bucket); } if ($closing) { $data = "--{$this->params['boundary']}--\r\n"; $data = dechex(strlen($data))."\r\n{$data}\r\n0\r\n\r\n"; stream_bucket_append($out, stream_bucket_new($this->stream, $data)); } return PSFS_PASS_ON; }}
Output for 5.4.0 - 5.4.32
<?php/* * This file is part of the SensioLabsProfiler SDK package. * * (c) SensioLabs <contact@sensiolabs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */class SprofilerHttpWrapper extends php_user_filter{ public $context; private $sslContext = array( 'SNI_server_name' => 'profiler.sensiolabs.com', ); private $socket; public function stream_open($path, $mode) { if ('wb' !== $mode) { return false; } $url = "ssl://{$this->sslContext['SNI_server_name']}:443"; $context = array('ssl' => $this->sslContext); $this->socket = stream_socket_client($url, $errno, $errmsg, 15, STREAM_CLIENT_CONNECT, stream_context_create($context)); stream_set_timeout($this->socket, 5); if (!$this->socket) { user_error($errmsg); return false; } if (!in_array('sprofiler.chunk', stream_get_filters())) { stream_filter_register('sprofiler.chunk', __CLASS__); } return true; } public function stream_write($data) { if (!$this->params) { $boundary = md5(mt_rand()*mt_rand()).md5(mt_rand()*mt_rand()); $this->params = compact('boundary'); $serverId = ''; $slotId = ''; $authToken = ''; $post = array(); foreach (explode("\n", $data) as $field) { $field = explode(":", $field, 2); switch ($field[0]) { case 'SensioLabsProfiler-Query': $authToken = trim($field[1]); parse_str($authToken, $field); foreach ($field as $k => $v) { if ('agentIds' === $k) { $v = explode(',', $v); $authToken = $v[0].':'.$authToken; foreach ($v as $v) { $post[] = array('agents[]', $v); } } else { if ('profileSlot' === $k) { $slotId = $v; } $post[] = array($k, $v); } } $authToken = base64_encode($authToken); break; case 'SensioLabsProfiler-Auth': $serverId = trim($field[1]); $serverId = "X-{$field[0]}: {$field[1]}\r\n"; break; } } $http = "POST /agent-api/v1/profile-slots/{$slotId} HTTP/1.1\r\n" ."Host: {$this->sslContext['SNI_server_name']}\r\n" ."User-Agent: SensioLabsProfiler-SDK\r\n" ."Authorization: Basic {$authToken}\r\n" ."Content-Type: multipart/form-data; boundary={$boundary}\r\n" ."Transfer-Encoding: chunked\r\n" .$serverId ."\r\n"; if (!$w = fwrite($this->socket, $http)) { return $w; } stream_filter_append($this->socket, 'sprofiler.chunk', STREAM_FILTER_WRITE, $this->params); $http = ''; foreach ($post as $field) { $http .= '--'.$boundary."\r\n" .'Content-Disposition: form-data; name="'.$field[0].'"'."\r\n" ."\r\n" .$field[1]."\r\n"; } $http .= '--'.$boundary."\r\n" ."Content-Disposition: form-data; name=\"payload\"; filename=\"graph.dat\"\r\n" ."Content-Type: application/octet-stream\r\n" ."\r\n"; if (!$w = fwrite($this->socket, $http)) { return $w; } if (in_array('zlib.*', stream_get_filters())) { stream_filter_prepend($this->socket, 'zlib.deflate', STREAM_FILTER_WRITE); } } elseif (!$w = fwrite($this->socket, $data)) { return $w; } return strlen($data); } public function stream_flush() { return fflush($this->socket); } public function stream_close() { fclose($this->socket); $this->socket = $this->params = null; } public function stream_lock() { return false; } public function filter($in, $out, &$consumed, $closing) { while ($bucket = stream_bucket_make_writeable($in)) { $consumed += $bucket->datalen; $bucket->data = dechex($bucket->datalen)."\r\n{$bucket->data}\r\n"; stream_bucket_append($out, $bucket); } if ($closing) { $data = "--{$this->params['boundary']}--\r\n"; $data = dechex(strlen($data))."\r\n{$data}\r\n0\r\n\r\n"; stream_bucket_append($out, stream_bucket_new($this->stream, $data)); } return PSFS_PASS_ON; }}