163 lines
4.2 KiB
PHP
163 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Controller\ControllerBase,
|
|
Form\CallbackCreate as CreateCallbackForm,
|
|
App\Model\Data\Callback as CallbackModel,
|
|
App\Model\Data\Request as RequestModel,
|
|
App\Model\Data\RequestMeta as RequestMetaModel;
|
|
|
|
class CallbackController extends ControllerBase
|
|
{
|
|
/**
|
|
* @var \Model\Data\User
|
|
*/
|
|
protected $_user;
|
|
|
|
public function initialize()
|
|
{
|
|
$this->_user = $this->_getAuth()->getUser();
|
|
}
|
|
|
|
/**
|
|
* @param $page
|
|
*/
|
|
public function listAction($page = 1)
|
|
{
|
|
$paginator = CallbackModel::getPaginationList($this->_user->getId(), $page, 10);
|
|
|
|
if ($paginator->getPaginate()->current > $paginator->getPaginate()->total_pages) {
|
|
$paginator->setCurrentPage(1);
|
|
}
|
|
|
|
$this->view->page = $paginator->getPaginate();
|
|
$this->view->pagination_url = '/callback/list/';
|
|
}
|
|
|
|
/**
|
|
* Create a new test session.
|
|
*/
|
|
public function newAction()
|
|
{
|
|
$form = new CreateCallbackForm();
|
|
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->getPost();
|
|
|
|
if ($form->isValid($data)) {
|
|
$callback = new CallbackModel();
|
|
$callback->User = $this->_user;
|
|
|
|
$callback->setName($this->request->getPost());
|
|
|
|
$result = $callback->save();
|
|
if ($result) {
|
|
$callback->refresh();
|
|
|
|
return $this->response->redirect(array(
|
|
'for' => 'cb-created',
|
|
'id' => $callback->getPublicId()));
|
|
} else {
|
|
foreach($callback->getMessages() as $msg) {
|
|
$this->flash->error($msg);
|
|
}
|
|
}
|
|
} else {
|
|
$msg = '<ul>';
|
|
|
|
foreach($form->getMessages() as $message) {
|
|
$msg .= '<li><strong>' . $message->getField() . '</strong>: '. $message->getMessage() . '</li>';
|
|
}
|
|
|
|
$msg .= '</ul>';
|
|
$this->flash->message('error', $msg);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function createdAction($id)
|
|
{
|
|
$row = CallbackModel::get($id);
|
|
if (!$row) {
|
|
|
|
}
|
|
$this->view->id = $id;
|
|
}
|
|
|
|
/**
|
|
* Monitor a test session.
|
|
*
|
|
* @param $id
|
|
* @param $page
|
|
*/
|
|
public function showAction($id = null, $page = 1)
|
|
{
|
|
$callback = CallbackModel::findFirst(array(
|
|
'conditions' => 'public_id = ?0 AND userid = ?1',
|
|
'bind' => array($id, $this->_user->getId())
|
|
));
|
|
|
|
if (!$callback) {
|
|
$this->_forward404();
|
|
return;
|
|
}
|
|
|
|
$paginator = $callback->getRequestPaginator($page, 30);
|
|
|
|
$this->view->item = $callback;
|
|
$this->view->page = $paginator->getPaginate();
|
|
$this->view->pagination_url = '/callback/show/' . $id . '/';
|
|
}
|
|
|
|
/**
|
|
* This is the action that the API to be
|
|
* tested should make it's callback to. So we can catch it.
|
|
*
|
|
* @Acl(resource="api")
|
|
* @param int $id The test session id so
|
|
* we know what test it belongs to.
|
|
* @return string
|
|
*/
|
|
public function endpointAction($id)
|
|
{
|
|
$this->view->disable();
|
|
|
|
$allowed_methods = array('GET', 'POST');
|
|
if ($this->request->isMethod($allowed_methods)) {
|
|
|
|
$callback = CallbackModel::get($id);
|
|
|
|
$request = new RequestModel();
|
|
|
|
$request->setHeaders($this->request->getHeaders());
|
|
$request->setBody($this->request->getRawBody());
|
|
|
|
$dt = new \DateTime();
|
|
|
|
$callback->setLastRequest($dt->format('Y-m-d H:i:s'));
|
|
|
|
$meta = new RequestMetaModel();
|
|
$meta->Callback = $callback;
|
|
$meta->RequestObject = $request;
|
|
|
|
$meta->setSourceIp($this->request->getClientAddress());
|
|
$meta->setMethod($this->request->isPost() ? 'POST' : 'GET');
|
|
$meta->setUri($this->request->getServer('REQUEST_URI'));
|
|
|
|
$result = $meta->save();
|
|
if ($result == false) {
|
|
var_dump($meta->getMessages());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|