MOON
Server: Apache
System: Linux server1.studioinfinity.com.br 2.6.32-954.3.5.lve1.4.90.el6.x86_64 #1 SMP Tue Feb 21 12:26:30 UTC 2023 x86_64
User: artinside (517)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/artinside/sites.artinside.com.br/paliar/sourcebackup/App/Admin/Services.php
<?php

namespace Source\App\Admin;

use Source\Models\Service;
use Source\Support\Pager;
use Source\Support\Thumb;
use Source\Support\Upload;

/**
 * Class Services
 * @package Source\App\Admin
 */
class Services extends Admin
{

    /**
     * Services constructor.
     */
    public function __construct($router)
    {
        parent::__construct();
        $this->view->addData("router", $router);
    }

    /**
     * @param array|null $data
     */
    public function home(?array $data): void
    {
        //search redirect
        if (!empty($data["s"])) {
            $s = str_search($data["s"]);
            echo json_encode(["redirect" => url("/admin/services/home/{$s}/1")]);
            return;
        }

        $search = null;
        $services = (new Service())->find();

        if (!empty($data["search"]) && str_search($data["search"]) != "all") {
            $search = str_search($data["search"]);
            $services = (new Service())->find("MATCH(title, subtitle) AGAINST(:s)", "s={$search}");
            if (!$services->count()) {
                $this->message->info("Sua pesquisa não retornou resultados")->flash();
                redirect("/admin/services/home");
            }
        }

        $all = ($search ?? "all");
        $pager = new Pager(url("/admin/services/home/{$all}/"));
        $pager->pager($services->count(), 12, (!empty($data["page"]) ? $data["page"] : 1));

        $head = $this->seo->render(
            CONF_SITE_NAME . " | Serviços",
            CONF_SITE_DESC,
            url("/admin"),
            url("/admin/assets/images/image.jpg"),
            false
        );

        echo $this->view->render("widgets/services/home", [
            "app" => "services",
            "head" => $head,
            "services" => $services->limit($pager->limit())->offset($pager->offset())->order("post_at DESC")->fetch(true),
            "paginator" => $pager->render(),
            "search" => $search
        ]);
    }

    /**
     * @param array|null $data
     * @throws \Exception
     */
    public function service(?array $data): void
    {


        //MCE Upload
        if (!empty($data["upload"]) && !empty($_FILES["image"])) {
            $files = $_FILES["image"];
            $upload = new Upload();
            $image = $upload->image($files, "services-" . time());

            if (!$image) {
                $json["message"] = $upload->message()->render();
                echo json_encode($json);
                return;
            }

            $json["mce_image"] = '<img style="width: 100%;" src="' . url("/storage/{$image}") . '" alt="{title}" title="{title}">';
            echo json_encode($json);
            return;
        }

        //create
        if (!empty($data["action"]) && $data["action"] == "create") {
            $content = $data["content"];
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);

            $serviceCreate = new Service();
            $serviceCreate->title = $data["title"];
            $serviceCreate->uri = str_slug($serviceCreate->title);
            $serviceCreate->subtitle = $data["subtitle"];
            $serviceCreate->content = str_replace(["{title}"], [$serviceCreate->title], $content);
            $serviceCreate->post_at = date_fmt_back($data["post_at"]);

            //upload cover
            if (!empty($_FILES["cover"])) {
                $files = $_FILES["cover"];
                $upload = new Upload();
                $image = $upload->image($files, $serviceCreate->title);

                if (!$image) {
                    $json["message"] = $upload->message()->render();
                    echo json_encode($json);
                    return;
                }
                $serviceCreate->cover = $image;
            }



            if (!$serviceCreate->save()) {
                $json["message"] = $serviceCreate->message()->render();
                echo json_encode($json);
                return;
            }

            $this->message->success("Serviço publicado com sucesso...")->flash();
            $json["redirect"] = url("/admin/services/service/{$serviceCreate->id}");

            echo json_encode($json);
            return;
        }

        //update
        if (!empty($data["action"]) && $data["action"] == "update") {
            $content = $data["content"];
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
            $serviceEdit = (new Service())->findById($data["service_id"]);

            if (!$serviceEdit) {
                $this->message->error("Você tentou atualizar um Serviço que não existe ou foi removido")->flash();
                echo json_encode(["redirect" => url("/admin/services/home")]);
                return;
            }


            $serviceEdit->title = $data["title"];
            $serviceEdit->uri = str_slug($serviceEdit->title);
            $serviceEdit->subtitle = $data["subtitle"];
            $serviceEdit->content = str_replace(["{title}"], [$serviceEdit->title], $content);
            $serviceEdit->post_at = date_fmt_back($data["post_at"]);

            //upload cover
            if (!empty($_FILES["cover"])) {
                if ($serviceEdit->cover && file_exists(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}")) {
                    unlink(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}");
                    (new Thumb())->flush($serviceEdit->cover);
                }

                $files = $_FILES["cover"];
                $upload = new Upload();
                $image = $upload->image($files, $serviceEdit->title);

                if (!$image) {
                    $json["message"] = $upload->message()->render();
                    echo json_encode($json);
                    return;
                }
                $serviceEdit->cover = $image;
            }

            if (!$serviceEdit->save()) {
                $json["message"] = $serviceEdit->message()->render();
                echo json_encode($json);
                return;
            }
            $this->message->success("Serviço atualizado com sucesso...")->flash();
            echo json_encode(["redirect" => url("/admin/services/home")]);
            return;
        }

        //delete
        if (!empty($data["action"]) && $data["action"] == "delete") {
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
            $serviceDelete = (new Service())->findById($data["service_id"]);

            if (!$serviceDelete) {
                $this->message->error("Você tentou excluir um Serviço que não existe ou já foi removido")->flash();
                echo json_encode(["reload" => true]);
                return;
            }


            $serviceDelete->destroy();
            $this->message->success("O Serviço foi excluído com sucesso...")->flash();

            echo json_encode(["reload" => true]);
            return;
        }

        $serviceEdit = null;
        if (!empty($data["service_id"])) {
            $serviceId = filter_var($data["service_id"], FILTER_VALIDATE_INT);
            $serviceEdit = (new Service())->findById($serviceId);
        }

        $head = $this->seo->render(
            CONF_SITE_NAME . " | " . ($serviceEdit->title ?? "Novo Artigo"),
            CONF_SITE_DESC,
            url("/admin"),
            url("/admin/assets/images/image.jpg"),
            false
        );

        echo $this->view->render("widgets/services/service", [
            "app" => "services",
            "head" => $head,
            "services" => $serviceEdit,

        ]);
    }

}