<commit_before><?php
/**
 * Nextcloud - Ownpad
 *
 * This file is licensed under the Affero General Public License
 * version 3 or later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> <<EMAIL>>, 2017
 */

namespace OCA\Ownpad\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\IConfig;

use EtherpadLite\Client;

class DisplayController extends Controller {

    /** @var IURLGenerator */
    private $urlGenerator;

    /** @var IUserSession */
    private $userSession;

    /** @var IConfig */
    private $config;

    /** @var Client */
    private $eplInstance;

    /**
     * @param string $AppName
     * @param IRequest $request
     * @param IURLGenerator $urlGenerator
     */
    public function __construct(
        $AppName,
        IRequest $request,
        IURLGenerator $urlGenerator,
        IUserSession $userSession,
        IConfig $config
    ) {
        parent::__construct($AppName, $request);
        $this->urlGenerator = $urlGenerator;
        $this->userSession = $userSession;
        $this->config = $config;

        if($this->config->getAppValue('ownpad', 'ownpad_etherpad_enable', 'no') !== 'no' AND
           $this->config->getAppValue('ownpad', 'ownpad_etherpad_useapi', 'no') !== 'no')
        {
            $eplHost = $this->config->getAppValue('ownpad', 'ownpad_etherpad_host', '');
            $eplApiKey = $this->config->getAppValue('ownpad', 'ownpad_etherpad_apikey', '');
            $this->eplInstance = new Client($eplApiKey, $eplHost . "/api");
        }
    }

    /**
     * @PublicPage
     * @NoCSRFRequired
     *
     * @return TemplateResponse
     */
    public function showPad($file, $dir) {
        /* Retrieve file content to find pad’s URL */
        $content = \OC\Files\Filesystem::file_get_contents($dir."/".$file);
        preg_match('/URL=(.*)$/', $content, $matches);
        $url = $matches[1];
        $title = $file;

        $eplHost = $this->config->getAppValue('ownpad', 'ownpad_etherpad_host', '');
        $eplHost = rtrim($eplHost, '/');
        $protectedPadRegex = sprintf('/%s\/p\/(g\.\w{16})\\$(.*)$/', preg_quote($eplHost, '/'));<commit_msg>fix: Initialize FS before getting file content for NC21 compat.<commit_after><?php
/**
 * Nextcloud - Ownpad
 *
 * This file is licensed under the Affero General Public License
 * version 3 or later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> <<EMAIL>>, 2017
 */

namespace OCA\Ownpad\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\IConfig;

use EtherpadLite\Client;

class DisplayController extends Controller {

    /** @var IURLGenerator */
    private $urlGenerator;

    /** @var IUserSession */
    private $userSession;

    /** @var IConfig */
    private $config;

    /** @var Client */
    private $eplInstance;

    /**
     * @param string $AppName
     * @param IRequest $request
     * @param IURLGenerator $urlGenerator
     */
    public function __construct(
        $AppName,
        IRequest $request,
        IURLGenerator $urlGenerator,
        IUserSession $userSession,
        IConfig $config
    ) {
        parent::__construct($AppName, $request);
        $this->urlGenerator = $urlGenerator;
        $this->userSession = $userSession;
        $this->config = $config;

        if($this->config->getAppValue('ownpad', 'ownpad_etherpad_enable', 'no') !== 'no' AND
           $this->config->getAppValue('ownpad', 'ownpad_etherpad_useapi', 'no') !== 'no')
        {
            $eplHost = $this->config->getAppValue('ownpad', 'ownpad_etherpad_host', '');
            $eplApiKey = $this->config->getAppValue('ownpad', 'ownpad_etherpad_apikey', '');
            $this->eplInstance = new Client($eplApiKey, $eplHost . "/api");
        }
    }

    /**
     * @PublicPage
     * @NoCSRFRequired
     *
     * @return TemplateResponse
     */
    public function showPad($file, $dir) {
        /*
         * Filesystem is not setup per default on normal route since
         * Nextcloud 21. More information:
         * - https://github.com/nextcloud/server/issues/23210
         * - https://github.com/nextcloud/server/pull/23821
         */
        \OC_Util::setupFS();

        /* Retrieve file content to find pad’s URL */
        $path = \OC\Files\Filesystem::normalizePath($dir."/".$file);
        $content = \OC\Files\Filesystem::file_get_contents($path);
        preg_match('/URL=(.*)$/', $content, $matches);
        $url = $matches[1];
        $title = $file;

        $eplHost = $this->config->getAppValue('ownpad', 'ownpad_etherpad_host', '');
        $eplHost = rtrim($eplHost, '/');
        $protectedPadRegex = sprintf('/%s\/p\/(g\.\w{16})\\$(.*)$/', preg_quote($eplHost, '/'));
{'fraction_non_alphanumeric': 0.15732801595214357, 'fraction_numerical': 0.005982053838484547, 'mean_word_length': 2.737704918032787, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7571902', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1570, 'n_words': 322}