From da663b933f88d578eea25db3df7b326ce3333bea Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Sat, 14 Nov 2015 14:04:51 +0100 Subject: [PATCH] * [MOD] Modified search bahaviour to avoid some "INVALID QUERY" responses because the security token was not set quickly enough by the client. JSON is now used for search response. * [FIX] Fixed integer parsing when an ip address was given as search text. --- ajax/ajax_search.php | 11 ++- inc/SP/Account/AccountSearch.class.php | 2 +- inc/SP/Controller/Controller.class.php | 10 ++- inc/SP/Http/Request.class.php | 110 +++++++++++++------------ inc/themes/material-blue/search.inc | 3 - inc/themes/material-blue/searchbox.inc | 7 +- js/functions.js | 14 +++- js/functions.min.js | 2 +- 8 files changed, 91 insertions(+), 68 deletions(-) diff --git a/ajax/ajax_search.php b/ajax/ajax_search.php index 9ae2d766..04057756 100644 --- a/ajax/ajax_search.php +++ b/ajax/ajax_search.php @@ -27,6 +27,7 @@ use SP\Controller\SearchC; use SP\Core\Init; use SP\Http\Request; use SP\Core\SessionUtil; +use SP\Http\Response; use SP\Util\Util; define('APP_ROOT', '..'); @@ -42,9 +43,15 @@ if (!Init::isLoggedIn()) { $sk = \SP\Http\Request::analyze('sk', false); if (!$sk || !SessionUtil::checkSessionKey($sk)) { - die('
' . _('CONSULTA INVÁLIDA') . '
'); + Response::printJSON(_('CONSULTA INVÁLIDA')); } $Controller = new SearchC(); $Controller->getSearch(); -$Controller->view(); \ No newline at end of file + +$data = array( + 'sk' => SessionUtil::getSessionKey(), + 'html' => $Controller->render() +); + +Response::printJSON($data, 0); \ No newline at end of file diff --git a/inc/SP/Account/AccountSearch.class.php b/inc/SP/Account/AccountSearch.class.php index 73576e4a..694a6881 100644 --- a/inc/SP/Account/AccountSearch.class.php +++ b/inc/SP/Account/AccountSearch.class.php @@ -133,7 +133,7 @@ class AccountSearch */ public function setTxtSearch($txtSearch) { - $this->_txtSearch = $txtSearch; + $this->_txtSearch = (string) $txtSearch; } /** diff --git a/inc/SP/Controller/Controller.class.php b/inc/SP/Controller/Controller.class.php index e1c11585..c2dbddb2 100644 --- a/inc/SP/Controller/Controller.class.php +++ b/inc/SP/Controller/Controller.class.php @@ -104,13 +104,21 @@ abstract class Controller } /** - * Renderizar los datos de la plantilla + * Renderizar los datos de la plantilla y mostrarlos */ public function view() { echo $this->view->render(); } + /** + * Renderizar los datos de la plantilla y devolverlos + */ + public function render() + { + return $this->view->render(); + } + /** * Obtener los datos para la vista de depuración */ diff --git a/inc/SP/Http/Request.class.php b/inc/SP/Http/Request.class.php index 7f28ff84..e7c4d1bb 100644 --- a/inc/SP/Http/Request.class.php +++ b/inc/SP/Http/Request.class.php @@ -36,20 +36,61 @@ use SP\Core\Init; */ class Request { + /** + * Comprobar el método utilizado para enviar un formulario. + * + * @param string $method con el método utilizado. + */ + public static function checkReferer($method) + { + if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method) + || !isset($_SERVER['HTTP_REFERER']) + || !preg_match('#' . Init::$WEBROOT . '/.*$#', $_SERVER['HTTP_REFERER']) + ) { + Init::initError(_('No es posible acceder directamente a este archivo')); + exit(); + } + } + + /** + * Analizar un valor encriptado y devolverlo desencriptado + * + * @param $param + * @return string + */ + public static function analyzeEncrypted($param) + { + $encryptedData = self::analyze($param, '', false, false, false); + + if ($encryptedData === '') { + return ''; + } + + try { + // Desencriptar con la clave RSA + $CryptPKI = new CryptPKI(); + $clearData = $CryptPKI->decryptRSA(base64_decode($encryptedData)); + } catch (\Exception $e) { + return $encryptedData; + } + + return $clearData; + } + /** * Obtener los valores de variables $_GET y $_POST * y devolverlos limpios con el tipo correcto o esperado. * - * @param string $param con el parámetro a consultar - * @param mixed $default valor por defecto a devolver - * @param bool $check comprobar si el parámetro está presente - * @param mixed $force valor devuelto si el parámeto está definido - * @param bool $sanitize escapar/eliminar carácteres especiales + * @param string $param con el parámetro a consultar + * @param mixed $default valor por defecto a devolver + * @param bool $check comprobar si el parámetro está presente + * @param mixed $force valor devuelto si el parámeto está definido + * @param bool $sanitize escapar/eliminar carácteres especiales * @return mixed si está presente el parámeto en la petición devuelve bool. Si lo está, devuelve el valor. */ public static function analyze($param, $default = '', $check = false, $force = false, $sanitize = true) { - switch($_SERVER['REQUEST_METHOD']){ + switch ($_SERVER['REQUEST_METHOD']) { case 'GET': if (!isset($_GET[$param])) { return ($force) ? !$force : $default; @@ -83,15 +124,17 @@ class Request */ private static function parse($value, $default, $sanitize) { - if (is_array($value)){ - foreach($value as &$data){ + if (is_array($value)) { + foreach ($value as &$data) { self::parse($data, $default, $sanitize); } return $value; } - if ((is_numeric($value) && !is_string($default)) || is_numeric($default)) { + if ((is_numeric($value) || is_numeric($default)) + && !is_string($default) + ) { return intval($value); } @@ -101,44 +144,13 @@ class Request } /** - * Comprobar el método utilizado para enviar un formulario. + * Comprobar si se realiza una recarga de la página * - * @param string $method con el método utilizado. + * @return bool */ - public static function checkReferer($method) + public static function checkReload() { - if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method) - || !isset($_SERVER['HTTP_REFERER']) - || !preg_match('#' . Init::$WEBROOT . '/.*$#', $_SERVER['HTTP_REFERER']) - ) { - Init::initError(_('No es posible acceder directamente a este archivo')); - exit(); - } - } - - /** - * Analizar un valor encriptado y devolverlo desencriptado - * - * @param $param - * @return string - */ - public static function analyzeEncrypted($param) - { - $encryptedData = self::analyze($param, '', false, false, false); - - if ($encryptedData === ''){ - return ''; - } - - try { - // Desencriptar con la clave RSA - $CryptPKI = new CryptPKI(); - $clearData = $CryptPKI->decryptRSA(base64_decode($encryptedData)); - } catch (\Exception $e) { - return $encryptedData; - } - - return $clearData; + return (self::getRequestHeaders('Cache-Control') == 'max-age=0'); } /** @@ -174,16 +186,6 @@ class Request return $headers; } - /** - * Comprobar si se realiza una recarga de la página - * - * @return bool - */ - public static function checkReload() - { - return (self::getRequestHeaders('Cache-Control') == 'max-age=0'); - } - /** * Comprobar si existen parámetros pasados por POST para enviarlos por GET */ diff --git a/inc/themes/material-blue/search.inc b/inc/themes/material-blue/search.inc index 80c2d48e..9537c2a3 100644 --- a/inc/themes/material-blue/search.inc +++ b/inc/themes/material-blue/search.inc @@ -229,9 +229,6 @@