HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Http/Controllers/Admin/PluginController.php
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Plugin;
use Exception;

class PluginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (!$request->user()->admin->can('read', new \App\Model\Plugin())) {
            return $this->notAuthorized();
        }

        // If admin can view all sending domains
        if (!$request->user()->admin->can("readAll", new \App\Model\Plugin())) {
            $request->merge(array("admin_id" => $request->user()->admin->id));
        }

        // exlude customer seding plugins
        $request->merge(array("no_customer" => true));

        $plugins = \App\Model\Plugin::search($request);

        return view('admin.plugins.index2', [
            'plugins' => $plugins
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function listing(Request $request)
    {
        if (!$request->user()->admin->can('read', new \App\Model\Plugin())) {
            return $this->notAuthorized();
        }

        // If admin can view all sending domains
        if (!$request->user()->admin->can("readAll", new \App\Model\Plugin())) {
            $request->merge(array("admin_id" => $request->user()->admin->id));
        }

        // exlude customer seding plugins
        $request->merge(array("no_customer" => true));

        $plugins = \App\Model\Plugin::search($request)->paginate($request->per_page);

        // Do preliminary check before loading the plugins list
        // For example, in case any plugin cannot be loaded, record the related error
        $settingUrls = [];
        $blacklist = [];
        foreach ($plugins as $plugin) {
            // Check if there is any error is recorded by the autoloader
            $error = $plugin->getPluginInfo('error');
            if (!is_null($error)) {
                $blacklist[$plugin->name] = 'CANNOT LOAD PLUGIN. Message: '.$error;
                continue;
            }

            // Generate setting buttons
            try {
                $composerJson = $plugin->getComposerJson();
                if (array_key_exists('extra', $composerJson) && array_key_exists('setting-route', $composerJson['extra'])) {
                    $url = action($composerJson['extra']['setting-route']);
                    $settingUrls[$plugin->name] = $url;
                } else {
                    throw new Exception('extra/setting-route not found');
                }
            } catch (Exception $ex) {
                $blacklist[$plugin->name] = 'Something went wrong with the plugin: '.$ex->getMessage();
            }
        }

        return view('admin.plugins._list', [
            'plugins' => $plugins,
            'settingUrls' => $settingUrls,
            'blacklist' => $blacklist,
        ]);
    }

    /**
     * Install/Upgrage plugins.
     *
     * @return \Illuminate\Http\Response
     */
    public function install(Request $request)
    {
        // authorize
        if (!$request->user()->admin->can('install', Plugin::class)) {
            return $this->notAuthorized();
        }

        // do install
        if ($request->isMethod('post')) {
            // Upload + register inside a try block so the popup's
            // fetch() gets a JSON body it can read on either path
            // (success OR exception). Without this, a thrown
            // exception lands as an HTML 500 page, the popup's JSON
            // parse fails, and the admin sees a bare "unknown
            // error" toast instead of the actual cause (composer
            // metadata invalid, plugin name conflict, perms, etc).
            //
            // NOT a swallow — per project rules, the catch RE-EMITS
            // the message verbatim into the JSON response with the
            // proper 500 status so the UI surfaces it tường minh.
            try {
                $pluginName = Plugin::upload($request);
                Plugin::register($pluginName);
            } catch (\Throwable $e) {
                return response()->json([
                    'status'  => 'error',
                    'message' => $e->getMessage(),
                ], 500);
            }

            return response()->json([
                'status'  => 'success',
                'message' => trans('refactor/admin_plugins.flash.installed'),
                'url'     => action('Admin\PluginController@index'),
            ]);
        }

        return view('admin.plugins.install');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
        $plugin = Plugin::findByUid($request->uid);

        if (!$request->user()->admin->can('delete', $plugin)) {
            return $this->notAuthorized();
        }

        if ($request->isMethod('post')) {
            $pluginName = $plugin->name;
            $keepData = $request->boolean('keep_data');
            $plugin->deleteAndCleanup($keepData);

            return view('admin.plugins.deleted', [
                'pluginName' => $pluginName,
                'keepData' => $keepData,
            ]);
        }

        return view('admin.plugins.delete', [
            'plugin' => $plugin,
        ]);
    }

    /**
     * Disable sending server.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function disable(Request $request)
    {
        $items = Plugin::whereIn(
            'uid',
            is_array($request->uids) ? $request->uids : explode(',', $request->uids)
        );

        foreach ($items->get() as $item) {
            // authorize
            if ($request->user()->admin->can('disable', $item)) {
                $item->disable();
            }
        }

        if (!$request->ajax()) {
            return redirect()->back()
                ->with('alert-success', trans('messages.plugins.disabled'));
        }

        // Redirect to my lists page
        echo trans('messages.plugins.disabled');
    }

    /**
     * Disable sending server.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function enable(Request $request)
    {
        $plugins = Plugin::whereIn(
            'uid',
            is_array($request->uids) ? $request->uids : explode(',', $request->uids)
        );

        foreach ($plugins->get() as $plugin) {
            // authorize
            if ($request->user()->admin->can('enable', $plugin)) {
                $plugin->activate();
            }
        }

        if (!$request->ajax()) {
            return redirect()->back()
                ->with('alert-success', trans('messages.plugins.enabled'));
        }

        // Redirect to my lists page
        echo trans('messages.plugins.enabled');
    }

    /**
     * Email verification server display options form.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function options(Request $request, $uid = null)
    {
        if ($uid) {
            $plugin = \App\Model\Plugin::findByUid($uid);
        } else {
            $plugin = new \App\Model\Plugin($request->all());
            $options = $plugin->getOptions();
        }

        return view('admin.plugins._options', [
            'server' => $plugin,
            'options' => $options,
        ]);
    }

    public function reindex(Request $request)
    {
        Plugin::resetPluginMasterFile();
        echo "done";
    }
}