OCC Web mit Nextcloud 30.0.2 nutzen

OCC Web mit Nextcloud 30.0.2 nutzen

Wie man diesem Forenbeitrag entnehmen kann, funktioniert OCC Web nicht unter Nextcloud 30. Dort wird zwar ein Lösungsvorschlag angeboten, dieser reicht allein aber nicht aus, um OCC Web unter Nextcloud 30.0.2 zum Laufen zu bringen.

Der Lösungsvorschlag des Nutzers „Gerald1969“ aus dem oben genannten Forenbeitrag erläutert, dass man die Datei apps/occweb/lib/Controller/OccController.php durch folgenden Inhalt ersetzen muss:

<?php

namespace OCA\OCCWeb\Controller;

use Exception;
use OC;
use OC\Console\Application;
use OC\MemoryInfo;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\ILogger;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;

class OccController extends Controller
{
  private $logger;
  private $userId;

  private $application;
  private $symphonyApplication;
  private $output;

  public function __construct(ILogger $logger, $AppName, IRequest $request, $userId)
  {
    parent::__construct($AppName, $request);
    $this->logger = $logger;
    $this->userId = $userId;

    // Obtendo o IAppManager como sexto argumento
    $this->application = new Application(
      OC::$server->getConfig(),
      OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class),
      new FakeRequest(),
      OC::$server->get(LoggerInterface::class),
      OC::$server->query(MemoryInfo::class),
      OC::$server->get(\OCP\App\IAppManager::class) // addition for NC30
    );
    
    $this->application->setAutoExit(false);
    $this->output = new OccOutput(OutputInterface::VERBOSITY_NORMAL, true);
    $this->application->loadCommands(new StringInput(""), $this->output);    

    $reflectionProperty = new \ReflectionProperty(Application::class, 'application');
    $reflectionProperty->setAccessible(true);
    $this->symphonyApplication = $reflectionProperty->getValue($this->application);
  }

  /**
   * @NoCSRFRequired
   */
  public function index()
  {
    return new TemplateResponse('occweb', 'index');
  }

  /**
   * @param $input
   * @return string
   */
  private function run($input)
  {
    try {
      $this->application->run($input, $this->output);
      return $this->output->fetch();
    } catch (Exception $ex) {
      $this->logger->logException($ex);
      return "error: " . $ex->getMessage();
    }
  }

  /**
   * @param string $command
   * @return DataResponse
   */
  public function cmd($command)
  {
    $this->logger->debug($command);
    $input = new StringInput($command);
    $response = $this->run($input);
    $this->logger->debug($response);
    return new DataResponse($response);
  }

  public function list() {
    $defs = $this->symphonyApplication->all();
    $cmds = array();
    foreach ($defs as $d) {
      array_push($cmds, $d->getName());
    }
    return new DataResponse($cmds);
  }
}

Bei mir folgte daraufhin ein Internal Server Error (500) sodass ich noch einmal tiefer in die Logs schauen musste. Ein Kompatibilitätsproblem mit Symfony\Component\Console\Output\ConsoleOutputInterface verhindert, dass die App korrekt läuft.

Ersetzt man nun noch den Inhalt der Datei apps/occweb/lib/Controller/OccOutput.php mit folgendem Code, funktioniert OCC Web wieder – auch unter Nextcloud 30.0.2. Zumindest ist dies bei meiner Installation der Fall.

<?php

namespace OCA\OCCWeb\Controller;

use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;

class OccOutput extends BufferedOutput implements ConsoleOutputInterface
{
    private $consoleSectionOutputs = [];
    private $errorOutput;
    private $stream;

    public function __construct(int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
    {
        parent::__construct($verbosity, $decorated, $formatter);
        // Initialize error output to avoid null references
        $this->errorOutput = new BufferedOutput($verbosity, $decorated, $formatter);
    }

    /**
     * Gets the OutputInterface for errors.
     *
     * @return OutputInterface
     */
    public function getErrorOutput(): OutputInterface
    {
        return $this->errorOutput;
    }

    /**
     * Sets the OutputInterface for errors.
     *
     * @param OutputInterface $error
     */
    public function setErrorOutput(OutputInterface $error): void
    {
        $this->errorOutput = $error;
    }

    /**
     * Creates a new output section.
     */
    public function section(): ConsoleSectionOutput
    {
        if ($this->stream === null) {
            $this->stream = fopen('php://temp', 'w');
        }
        return new ConsoleSectionOutput($this->stream, $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
    }
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.