Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
[GH-1441] and others.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed May 15, 2018
1 parent 5883b80 commit 271cad5
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions Tideways.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Tideways
*
* Copyright 2014-2016 Tideways GmbH
* Copyright 2014-2018 Tideways GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -351,7 +351,12 @@ public function socketStore(array $trace)
return;
}

$payload = json_encode(array('type' => self::TYPE_TRACE, 'payload' => $trace));
$flags = 0;
if (defined('JSON_PARTIAL_OUTPUT_ON_ERROR')) {
$flags = constant('JSON_PARTIAL_OUTPUT_ON_ERROR');
}

$payload = json_encode(array('type' => self::TYPE_TRACE, 'payload' => $trace), $flags);

$timeout = (int)ini_get('tideways.timeout');

Expand Down Expand Up @@ -504,6 +509,7 @@ class Profiler
const FRAMEWORK_SYMFONY2_COMPONENT = 'symfony2c';
const FRAMEWORK_SYMFONY2_FRAMEWORK = 'symfony2';
const FRAMEWORK_OXID = 'oxid';
const FRAMEWORK_OXID6 = 'oxid6';
const FRAMEWORK_SHOPWARE = 'shopware';
const FRAMEWORK_WORDPRESS = 'wordpress';
const FRAMEWORK_LARAVEL = 'laravel';
Expand Down Expand Up @@ -615,6 +621,11 @@ public static function detectFramework($framework)
self::$defaultOptions['exception_function'] = 'oxShopControl::_handleBaseException';
break;

case self::FRAMEWORK_OXID6:
self::$defaultOptions['transaction_function'] = 'OxidEsales\EshopCommunity\Core\Controller\BaseController::setClassKey';
self::$defaultOptions['exception_function'] = 'OxidEsales\EshopCommunity\Core\ShopControl::logException';
break;

case self::FRAMEWORK_SHOPWARE:
self::$defaultOptions['transaction_function'] = $cli
? 'Symfony\Component\Console\Application::find'
Expand Down Expand Up @@ -894,16 +905,20 @@ private static function decideProfiling($treshold, array $options = array())
{
$vars = array();
$type = null;
$tidewaysReferenceId = null;

if (isset($_SERVER['HTTP_X_TIDEWAYS_PROFILER']) && is_string($_SERVER['HTTP_X_TIDEWAYS_PROFILER'])) {
parse_str($_SERVER['HTTP_X_TIDEWAYS_PROFILER'], $vars);
$type = 'header X-Tideways-Profiler';
$tidewaysReferenceId = isset($_SERVER['HTTP_X_TIDEWAYS_REF']) ? $_SERVER['HTTP_X_TIDEWAYS_REF'] : null;
} else if (isset($_SERVER['TIDEWAYS_SESSION']) && is_string($_SERVER['TIDEWAYS_SESSION'])) {
parse_str($_SERVER['TIDEWAYS_SESSION'], $vars);
$type = 'environment variable TIDEWAYS_SESSION';
$tidewaysReferenceId = isset($_SERVER['TIDEWAYS_REF']) ? $_SERVER['TIDEWAYS_REF'] : null;
} else if (isset($_COOKIE['TIDEWAYS_SESSION']) && is_string($_COOKIE['TIDEWAYS_SESSION'])) {
parse_str($_COOKIE['TIDEWAYS_SESSION'], $vars);
$type = 'cookie TIDEWAYS_SESSION';
$tidewaysReferenceId = isset($_COOKIE['TIDEWAYS_REF']) ? $_COOKIE['TIDEWAYS_REF'] : null;
} else if (isset($_GET['_tideways']) && is_array($_GET['_tideways'])) {
$vars = $_GET['_tideways'];
$type = 'GET parameter';
Expand All @@ -917,7 +932,12 @@ private static function decideProfiling($treshold, array $options = array())
$message = 'method=' . $vars['method'] . '&time=' . $vars['time'] . '&user=' . $vars['user'];
self::log(3, "Found explicit trigger trace parameters in " . $type);

if ($vars['time'] > time() && hash_hmac('sha256', $message, md5(self::$trace['apiKey'])) === $vars['hash']) {
if (hash_hmac('sha256', $message, md5(self::$trace['apiKey'])) === $vars['hash']) {
if ($vars['time'] < time()) {
self::log(1, "trigger trace request with " . $type . " is authenticated, but signature is too old: " . $vars['time'] . " < " . time());
self::$mode = self::MODE_DISABLED;
return;
}

if (self::$logLevel >= 2) {
$location = "unknown";
Expand All @@ -938,10 +958,16 @@ private static function decideProfiling($treshold, array $options = array())
self::$trace['keep'] = true; // always keep

self::enableProfiler($options['triggered']);
self::setCustomVariable('user', $vars['user']);
self::setCustomVariable('tw.uid', $vars['user']);

if ($tidewaysReferenceId) {
self::setCustomVariable('tw.ref', $tidewaysReferenceId);
}
return;
} else {
self::log(1, "Invalid trigger trace request cannot be authenticated.");
self::log(1, "Invalid trigger trace request with " . $type . " cannot be authenticated.");
self::$mode = self::MODE_DISABLED;
return;
}
}

Expand Down Expand Up @@ -1357,7 +1383,12 @@ public static function logException($exception)
$exception = new \RuntimeException($exception);
}

if (self::$error === true || !self::$currentRootSpan || !($exception instanceof \Exception)) {
if (self::$error === true || !self::$currentRootSpan || !is_object($exception)) {
return;
}

// PHP 5 compatible way to check for !($exception instanceof Throwable)
if (!($exception instanceof \Exception) && !in_array('Throwable', class_implements($exception, false))) {
return;
}

Expand Down Expand Up @@ -1401,7 +1432,7 @@ public static function autoStart()
if (self::isStarted() === false) {
switch (php_sapi_name()) {
case 'cli':
if (ini_get("tideways.monitor_cli")) {
if (ini_get("tideways.monitor_cli") || isset($_SERVER['TIDEWAYS_SESSION'])) {
self::start(array('service' => 'cli'));
}
break;
Expand Down

0 comments on commit 271cad5

Please sign in to comment.