Source of file PlanAjax.php

Size: 10,892 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Ajax/PlanAjax.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
<?php
/* 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\Ajax;

use NextDom\Enums\AjaxParams;
use NextDom\Enums\NextDomFolder;
use NextDom\Enums\NextDomObj;
use NextDom\Enums\UserRight;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\AuthentificationHelper;
use NextDom\Helpers\NextDomHelper;
use NextDom\Helpers\Utils;
use NextDom\Managers\PlanHeaderManager;
use NextDom\Managers\PlanManager;
use NextDom\Model\DataClass\UploadedImage;
use NextDom\Model\Entity\Plan;
use NextDom\Model\Entity\PlanHeader;

/**
 * Class PlanAjax
 * @package NextDom\Ajax
 */
class PlanAjax extends BaseAjax
{
    protected $NEEDED_RIGHTS = UserRight::USER;
    protected $MUST_BE_CONNECTED = true;
    protected $CHECK_AJAX_TOKEN = true;

    public function save()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $plans = json_decode(Utils::init('plans'), true);
        foreach ($plans as $plan_ajax) {
            @$plan = PlanManager::byId($plan_ajax['id']);
            if (!is_object($plan)) {
                $plan = new Plan();
            }
            Utils::a2o($plan, NextDomHelper::fromHumanReadable($plan_ajax));
            $plan->save();
        }
        $this->ajax->success();
    }

    public function execute()
    {
        $plan = PlanManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($plan)) {
            throw new CoreException(__('Aucun plan correspondant'));
        }
        $this->ajax->success($plan->execute());
    }

    public function planHeader()
    {
        $return = [];
        /**
         * @var Plan $plan
         */
        foreach (PlanManager::byPlanHeaderId(Utils::init('planHeader_id')) as $plan) {
            $result = $plan->getHtml(Utils::init(AjaxParams::VERSION));
            if (is_array($result)) {
                $return[] = $result;
            }
        }
        $this->ajax->success($return);
    }

    public function create()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        if (Utils::init('plan', '') === '') {
            throw new CoreException(__('L\'identifiant du plan doit être fourni'));
        }
        $plan = new Plan();
        Utils::a2o($plan, json_decode(Utils::init('plan'), true));
        $plan->save();
        $this->ajax->success($plan->getHtml(Utils::init(AjaxParams::VERSION)));
    }

    public function copy()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $plan = PlanManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($plan)) {
            throw new CoreException(__('Aucun plan correspondant'));
        }
        $this->ajax->success($plan->copy()->getHtml(Utils::init(AjaxParams::VERSION, 'dplan')));
    }

    public function get()
    {
        $plan = PlanManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($plan)) {
            throw new CoreException(__('Aucun plan correspondant'));
        }
        $this->ajax->success(NextDomHelper::toHumanReadable(Utils::o2a($plan)));
    }

    public function remove()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $plan = PlanManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($plan)) {
            throw new CoreException(__('Aucun plan correspondant'));
        }
        $this->ajax->success($plan->remove());
    }

    public function removePlanHeader()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $planHeader = PlanHeaderManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($planHeader)) {
            throw new CoreException(__('Objet inconnu. Vérifiez l\'ID'));
        }
        $planHeader->remove();
        $this->ajax->success();
    }

    public function allHeader()
    {
        $planHeaders = PlanHeaderManager::all();
        $result = [];
        foreach ($planHeaders as $planHeader) {
            $info_planHeader = Utils::o2a($planHeader);
            unset($info_planHeader['image']);
            $result[] = $info_planHeader;
        }
        $this->ajax->success($result);
    }

    public function getPlanHeader()
    {
        $planHeader = PlanHeaderManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($planHeader)) {
            throw new CoreException(__('Plan header inconnu. Vérifiez l\'ID ') . Utils::init(AjaxParams::ID));
        }
        if (trim($planHeader->getConfiguration('accessCode', '')) != '' && $planHeader->getConfiguration('accessCode', '') != sha512(Utils::init('code'))) {
            throw new CoreException(__('Code d\'acces invalide'), -32005);
        }
        $result = Utils::o2a($planHeader);
        $result['image'] = $planHeader->displayImage();
        $this->ajax->success($result);
    }

    public function savePlanHeader()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $planHeader_ajax = json_decode(Utils::init('planHeader'), true);
        $planHeader = null;
        if (isset($planHeader_ajax['id'])) {
            $planHeader = PlanHeaderManager::byId($planHeader_ajax['id']);
        }
        if (!is_object($planHeader)) {
            $planHeader = new PlanHeader();
        }
        Utils::a2o($planHeader, $planHeader_ajax);
        $planHeader->save();
        $this->ajax->success(Utils::o2a($planHeader));
    }

    public function copyPlanHeader()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $planHeader = PlanHeaderManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($planHeader)) {
            throw new CoreException(__('Plan header inconnu. Vérifiez l\'ID ') . Utils::init(AjaxParams::ID));
        }
        $this->ajax->success(Utils::o2a($planHeader->copy(Utils::init('name'))));
    }

    public function removeImageHeader()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $planHeader = PlanHeaderManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($planHeader)) {
            throw new CoreException(__('Plan header inconnu. Vérifiez l\'ID ') . Utils::init(AjaxParams::ID));
        }
        $planHeader->setImage('sha512', '');
        $planHeader->save();
        @unlink(NEXTDOM_DATA . '/' . $planHeader->getImgLink());
        $this->ajax->success();
    }

    /**
     * Get data of uploaded file
     *
     * @return UploadedImage
     * @throws CoreException
     */
    private function getUploadedImageData()
    {
        $uploadedImageData = new UploadedImage();
        if (!isset($_FILES['file'])) {
            throw new CoreException(__('Aucun fichier trouvé. Vérifiez le paramètre PHP (post size limit)'));
        }
        $extension = strtolower(strrchr($_FILES['file']['name'], '.'));
        $uploadedImageData->setType(substr($extension, 1));
        $this->checkSizeAndExtension($extension);
        $uploadedImageData->setSize(getimagesize($_FILES['file']['tmp_name']));
        $fileContent = file_get_contents($_FILES['file']['tmp_name']);
        $uploadedImageData->setHash(Utils::sha512($fileContent));
        $uploadedImageData->setPath($_FILES['file']['tmp_name']);
        return $uploadedImageData;
    }

    /**
     * Check file path and move file
     *
     * @param $uploadFile
     * @param $targetPath
     *
     * @throws CoreException
     */
    private function checkAndMoveUploadImage($uploadFile, $targetPath)
    {
        // Check $targetPath don't go up
        if (preg_match('/.*(\.\.\/)|(\/\.\.).*/', $targetPath) !== 0) {
            throw new CoreException(__('Le répertoire de destination n\'est pas valide'));
        }
        if (!move_uploaded_file($uploadFile, $targetPath)) {
            throw new CoreException(__('Impossible de sauvegarder l\'image'));
        }
    }

    /**
     * Upload background picture on plan
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function uploadImage()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $planHeader = PlanHeaderManager::byId(Utils::init(AjaxParams::ID));
        if (!is_dir(NextDomFolder::PLAN_IMAGE)) {
            mkdir(NextDomFolder::PLAN_IMAGE, 0755, true);
        }
        if (!is_object($planHeader)) {
            throw new CoreException(__('Objet inconnu. Vérifiez l\'ID'));
        }
        $uploadedImageData = $this->getUploadedImageData();
        PlanHeaderManager::cleanPlanImageFolder($planHeader->getId());
        $planHeader->setImage('type', $uploadedImageData->getType());
        $planHeader->setImage('size', $uploadedImageData->getSize());
        $planHeader->setImage('sha512', $uploadedImageData->getHash());
        $destFilename = NextDomObj::PLAN_HEADER . $planHeader->getId() . '-' . $uploadedImageData->getHash() . '.' . $uploadedImageData->getType();
        $this->checkAndMoveUploadImage($uploadedImageData->getPath(), NextDomFolder::PLAN_IMAGE . $destFilename);
        $planHeader->setConfiguration('desktopSizeX', $uploadedImageData->getSizeX());
        $planHeader->setConfiguration('desktopSizeY', $uploadedImageData->getSizeY());
        $planHeader->save();
        $this->ajax->success();
    }

    /**
     * Upload image for static picture on plan
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function uploadImagePlan()
    {
        AuthentificationHelper::isConnectedAsAdminOrFail();
        $plan = PlanManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($plan)) {
            throw new CoreException(__('Objet inconnu. Vérifiez l\'ID'));
        }
        $uploadedImageData = $this->getUploadedImageData();
        $destPath = NextDomFolder::PLAN_IMAGE . 'plan_' . $plan->getId();
        FileSystemHelper::rrmdir($destPath);
        mkdir($destPath, 0775, true);
        $destFilename = $uploadedImageData->getHash() . '.' . $uploadedImageData->getType();
        $this->checkAndMoveUploadImage($uploadedImageData->getPath(), $destPath . '/' . $destFilename);
        $plan->setDisplay('width', $uploadedImageData->getSizeX());
        $plan->setDisplay('height', $uploadedImageData->getSizeY());
        $plan->setDisplay('path', 'data/plan/plan_' . $plan->getId() . '/' . $destFilename);
        $plan->save();
        $this->ajax->success();
    }


}