Source of file AjaxHelper.php
Size: 4,031 Bytes - Last Modified: 2020-10-24T02:46:31+00:00
/home/travis/build/NextDom/nextdom-core/src/Helpers/AjaxHelper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | <?php /* * This file is part of the NextDom software (https://github.com/NextDom or http://nextdom.github.io). * Copyright (c) 2018 NextDom. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* This file is part of Jeedom. * * Jeedom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jeedom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jeedom. If not, see <http://www.gnu.org/licenses/>. */ namespace NextDom\Helpers; use NextDom\Managers\ConfigManager; /** * Class AjaxHelper * @package NextDom\Helpers */ class AjaxHelper { /** * @var bool Answer state */ private $answerSended = false; /** * Init ajax communication * * @throws \Exception */ public function __construct() { // Prepare ajax response if (!headers_sent()) { header('Content-Type: application/json'); } } /** * Check ajax token validity * * @throws \Exception */ public function checkToken() { if (Utils::init('nextdom_token') != self::getToken()) { self::error(__('Token d\'accès invalide')); } } /** * Get current NextDom token stored in session * * @return string NextDom token * @throws \Exception */ public static function getToken() { if (session_status() == PHP_SESSION_NONE) { @session_start(); @session_write_close(); } if (!isset($_SESSION['nextdom_token'])) { @session_start(); $_SESSION['nextdom_token'] = ConfigManager::genKey(); @session_write_close(); } return $_SESSION['nextdom_token']; } /** * Send error * * @param string $errorData Error description * @param int $errorCode Error code */ public function error($errorData = '', $errorCode = 0) { if (!$this->answerSended) { echo $this->getResponse($errorData, $errorCode); $this->answerSended = true; } } /** * Convert data to JSON response * * @param string $data Data to convert * @param null $errorCode Error code * @return mixed */ public function getResponse($data = '', $errorCode = null) { // @TODO: Tester l'incidence de l'ordre des résultat si result est inséré en dernier // et donc éviter la ligne en double $response = []; if ($errorCode === null) { $response['state'] = 'ok'; $response['result'] = $data; } else { $response['state'] = 'error'; $response['result'] = $data; $response['code'] = $errorCode; } return json_encode($response, JSON_UNESCAPED_UNICODE); } /** * Send answer * * @param string $answer Answer to send */ public function success($answer = '') { if (!$this->answerSended) { echo $this->getResponse($answer); $this->answerSended = true; } } } |