Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add actionable interface for usage in openhandler #670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/DebugBar/DataCollector/Actionable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DebugBar\DataCollector;

/**
* Indicates that a DataCollector is able to run action use the OpenHandler
*/
interface Actionable
{
/**
* Execute an action with a possible payload.
*
* @param string $action
* @param array|null $payload
* @return mixed
*/
function executionAction($action, array $payload = null);
}
18 changes: 18 additions & 0 deletions src/DebugBar/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class DebugBar implements ArrayAccess

protected $stackAlwaysUseSessionStorage = false;

protected $hashKey;

/**
* Adds a data collector
*
Expand Down Expand Up @@ -468,6 +470,22 @@ public function getJavascriptRenderer($baseUrl = null, $basePath = null)
return $this->jsRenderer;
}

public function setHashKey($key)
{
$this->hashKey = $key;
}

public function getHashSignature($data)
{
if ($this->hashKey === null) {
throw new DebugBarException('HashKey must be set before running actions');
}

$data = json_encode($data);

return hash_hmac('sha256', $data, $this->hashKey);
}

// --------------------------------------------
// ArrayAccess implementation

Expand Down
35 changes: 34 additions & 1 deletion src/DebugBar/OpenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace DebugBar;

use DebugBar\DataCollector\Actionable;

/**
* Handler to list and open saved dataset
*/
Expand Down Expand Up @@ -47,7 +49,7 @@ public function handle($request = null, $echo = true, $sendHeader = true)
$op = 'find';
if (isset($request['op'])) {
$op = $request['op'];
if (!in_array($op, array('find', 'get', 'clear'))) {
if (!in_array($op, array('find', 'get', 'clear', 'execute'))) {
throw new DebugBarException("Invalid operation '{$request['op']}'");
}
}
Expand Down Expand Up @@ -114,4 +116,35 @@ protected function clear($request)
$this->debugBar->getStorage()->clear();
return array('success' => true);
}

/**
* Execute an action
* @param $request
* @return mixed
* @throws DebugBarException
*/
protected function execute($request)
{
if (!isset($request['collector']) || !isset($request['action']) || !isset($request['signature'])) {
throw new DebugBarException("Missing 'collector' and/or 'action' parameter in 'execute' operation");
}

// Get the signature and remove if before checking the payload.
$signature = $request['signature'];
unset ($request['signature']);
if (!hash_equals($this->debugBar->getHashSignature($request), $signature)) {
throw new DebugBarException("Signature does not match in 'execute' operation");
}

if (!$this->debugBar->hasCollector($request['collector'])) {
throw new DebugBarException("Collector {$request['collector']} not found in 'execute' operation");
}

$collector = $this->debugBar->getCollector($request['collector']);
if (!$collector instanceof Actionable) {
throw new DebugBarException("Collector {$request['collector']} found in 'execute' operation does not implement the Actionable interface");
}

return $collector->executionAction($request['action'], $request['payload'] ?? null);
}
}
Loading