From a444b8adf5491d45f7ee7aff9259c4618308f140 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 13 Feb 2017 17:11:26 +0100 Subject: [PATCH 1/5] Request: support JSON as POST data format refs #2749 --- library/Icinga/Web/Request.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/Icinga/Web/Request.php b/library/Icinga/Web/Request.php index bedcd44d7..88fb729cb 100644 --- a/library/Icinga/Web/Request.php +++ b/library/Icinga/Web/Request.php @@ -3,6 +3,7 @@ namespace Icinga\Web; +use Icinga\Util\Json; use Zend_Controller_Request_Http; use Icinga\Application\Icinga; use Icinga\User; @@ -120,4 +121,11 @@ class Request extends Zend_Controller_Request_Http return $id . '-' . $this->uniqueId; } + + public function getPost($key = null, $default = null) + { + return $key === null && $this->isApiRequest() + ? Json::decode(file_get_contents('php://input'), true) + : parent::getPost($key, $default); + } } From 880a0a254f5b7bfd177049056e5ce8d8feb616c9 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 13 Feb 2017 18:20:22 +0100 Subject: [PATCH 2/5] DateTimePicker: support *nix timestamps refs #2749 --- library/Icinga/Web/Form/Element/DateTimePicker.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/Icinga/Web/Form/Element/DateTimePicker.php b/library/Icinga/Web/Form/Element/DateTimePicker.php index 274c3b676..284a744e3 100644 --- a/library/Icinga/Web/Form/Element/DateTimePicker.php +++ b/library/Icinga/Web/Form/Element/DateTimePicker.php @@ -56,6 +56,11 @@ class DateTimePicker extends FormElement */ public function isValid($value, $context = null) { + if (is_scalar($value) && $value !== '' && ! preg_match('/\D/', $value)) { + $dateTime = new DateTime(); + $value = $dateTime->setTimestamp($value)->format($this->getFormat()); + } + if (! parent::isValid($value, $context)) { return false; } From 235e75d054826d9dcac8db4240e031c167ea23f7 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 14 Feb 2017 11:15:11 +0100 Subject: [PATCH 3/5] Form: handle API requests as expected refs #2749 --- library/Icinga/Web/Form.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 9d0d5d707..c0881d997 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -1143,7 +1143,7 @@ class Form extends Zend_Form } $formData = $this->getRequestData(); - if ($this->getIsApiTarget() || $this->getUidDisabled() || $this->wasSent($formData)) { + if ($this->getIsApiTarget() || $this->getRequest()->isApiRequest() || $this->getUidDisabled() || $this->wasSent($formData)) { if (($frameUpload = (bool) $request->getUrl()->shift('_frameUpload', false))) { $this->getView()->layout()->setLayout('wrapped'); } @@ -1172,7 +1172,7 @@ class Form extends Zend_Form } else { $this->getView()->layout()->redirectUrl = $this->getRedirectUrl()->getAbsoluteUrl(); } - } elseif ($this->getIsApiTarget()) { + } elseif ($this->getIsApiTarget() || $this->getRequest()->isApiRequest()) { $this->getResponse()->json()->setFailData($this->getMessages())->sendResponse(); } } elseif ($this->getValidatePartial()) { @@ -1198,7 +1198,7 @@ class Form extends Zend_Form if (strtolower($this->getRequest()->getMethod()) !== $this->getMethod()) { return false; } - if ($this->getIsApiTarget()) { + if ($this->getIsApiTarget() || $this->getRequest()->isApiRequest()) { return true; } if ($this->getSubmitLabel()) { From ee60a8df99a810078885ae97f27e527220d4dfcc Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 14 Feb 2017 11:16:05 +0100 Subject: [PATCH 4/5] Don't let AutoRefreshForm handle API requests refs #2749 --- library/Icinga/Web/Controller/ActionController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index 0b65cb8dd..f5fded73d 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -447,7 +447,9 @@ class ActionController extends Zend_Controller_Action public function preDispatch() { $form = new AutoRefreshForm(); - $form->handleRequest(); + if (! $this->getRequest()->isApiRequest()) { + $form->handleRequest(); + } $this->_helper->layout()->autoRefreshForm = $form; } From 6f1d8668a023788a678ec7f6f37af8e0f39e8de5 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 22 Jun 2018 11:04:48 +0200 Subject: [PATCH 5/5] Fix line exceeds 120 characters --- library/Icinga/Web/Form.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index c0881d997..8967d2ca6 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -1143,7 +1143,11 @@ class Form extends Zend_Form } $formData = $this->getRequestData(); - if ($this->getIsApiTarget() || $this->getRequest()->isApiRequest() || $this->getUidDisabled() || $this->wasSent($formData)) { + if ($this->getIsApiTarget() + || $this->getRequest()->isApiRequest() + || $this->getUidDisabled() + || $this->wasSent($formData) + ) { if (($frameUpload = (bool) $request->getUrl()->shift('_frameUpload', false))) { $this->getView()->layout()->setLayout('wrapped'); }