Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/files_texteditor.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2017-08-02 14:11:42 +0300
committerMorris Jobke <hey@morrisjobke.de>2017-08-02 19:00:40 +0300
commit4a1bf25f374cce6b988186cef85817ce8b8caf25 (patch)
tree0367401bdf2966c5eaa014b039273ee5a59dd3de /lib
parent08478d9eb82cd63ea92cb04efab5e746d72d1a3f (diff)
Move classes to PSR-4
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/AppInfo/Application.php56
-rw-r--r--lib/Controller/FileHandlingController.php209
2 files changed, 265 insertions, 0 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
new file mode 100644
index 0000000..1145e7b
--- /dev/null
+++ b/lib/AppInfo/Application.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @author Björn Schießle <schiessle@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace OCA\FilesTextEditor\AppInfo;
+
+use OCA\FilesTextEditor\Controller\FileHandlingController;
+use OCP\AppFramework\App;
+use OCP\AppFramework\IAppContainer;
+
+class Application extends App {
+
+ /**
+ * @param array $urlParams
+ */
+ public function __construct(array $urlParams = array()) {
+ parent::__construct('files_texteditor', $urlParams);
+
+ $container = $this->getContainer();
+ $server = $container->getServer();
+
+ $container->registerService('FileHandlingController', function (IAppContainer $c) use ($server) {
+ $user = $server->getUserSession()->getUser();
+ if ($user) {
+ $uid = $user->getUID();
+ } else {
+ throw new \BadMethodCallException('no user logged in');
+ }
+ return new FileHandlingController(
+ $c->getAppName(),
+ $server->getRequest(),
+ $server->getL10N($c->getAppName()),
+ $server->getLogger(),
+ $server->getUserFolder($uid)
+ );
+ });
+ }
+}
diff --git a/lib/Controller/FileHandlingController.php b/lib/Controller/FileHandlingController.php
new file mode 100644
index 0000000..3b647a9
--- /dev/null
+++ b/lib/Controller/FileHandlingController.php
@@ -0,0 +1,209 @@
+<?php
+/**
+ * @author Björn Schießle <schiessle@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace OCA\FilesTextEditor\Controller;
+
+
+use OC\HintException;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use OCP\Files\ForbiddenException;
+use OCP\IL10N;
+use OCP\ILogger;
+use OCP\IRequest;
+use OCP\Lock\LockedException;
+
+class FileHandlingController extends Controller{
+
+ /** @var IL10N */
+ private $l;
+
+ /** @var ILogger */
+ private $logger;
+
+ /** @var Folder */
+ private $userFolder;
+
+ /**
+ * @NoAdminRequired
+ *
+ * @param string $AppName
+ * @param IRequest $request
+ * @param IL10N $l10n
+ * @param ILogger $logger
+ * @param Folder $userFolder
+ */
+ public function __construct($AppName,
+ IRequest $request,
+ IL10N $l10n,
+ ILogger $logger,
+ Folder $userFolder) {
+ parent::__construct($AppName, $request);
+ $this->l = $l10n;
+ $this->logger = $logger;
+ $this->userFolder = $userFolder;
+ }
+
+ /**
+ * load text file
+ *
+ * @NoAdminRequired
+ *
+ * @param string $dir
+ * @param string $filename
+ * @return DataResponse
+ */
+ public function load($dir, $filename) {
+ try {
+ if (!empty($filename)) {
+ $path = $dir . '/' . $filename;
+
+ /** @var File $file */
+ $file = $this->userFolder->get($path);
+
+ if ($file instanceof Folder) {
+ return new DataResponse(['message' => $this->l->t('You can not open a folder')], Http::STATUS_BAD_REQUEST);
+ }
+
+ // default of 4MB
+ $maxSize = 4194304;
+ if ($file->getSize() > $maxSize) {
+ return new DataResponse(['message' => (string)$this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST);
+ }
+ $fileContents = $file->getContent();
+ if ($fileContents !== false) {
+ $writable = $file->isUpdateable();
+ $mime = $file->getMimeType();
+ $mTime = $file->getMTime();
+ $encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);
+ if ($encoding === '') {
+ // set default encoding if it couldn't be detected
+ $encoding = 'ISO-8859-15';
+ }
+ $fileContents = iconv($encoding, 'UTF-8', $fileContents);
+ return new DataResponse(
+ [
+ 'filecontents' => $fileContents,
+ 'writeable' => $writable,
+ 'mime' => $mime,
+ 'mtime' => $mTime
+ ],
+ Http::STATUS_OK
+ );
+ } else {
+ return new DataResponse(['message' => (string)$this->l->t('Cannot read the file.')], Http::STATUS_BAD_REQUEST);
+ }
+ } else {
+ return new DataResponse(['message' => (string)$this->l->t('Invalid file path supplied.')], Http::STATUS_BAD_REQUEST);
+ }
+
+ } catch (LockedException $e) {
+ $message = (string) $this->l->t('The file is locked.');
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ } catch (ForbiddenException $e) {
+ return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
+ } catch (HintException $e) {
+ $message = (string)$e->getHint();
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ } catch (\Exception $e) {
+ $message = (string)$this->l->t('An internal server error occurred.');
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ }
+ }
+
+ /**
+ * save text file
+ *
+ * @NoAdminRequired
+ *
+ * @param string $path
+ * @param string $filecontents
+ * @param integer $mtime
+ * @return DataResponse
+ */
+ public function save($path, $filecontents, $mtime) {
+ try {
+ if($path !== '' && (is_int($mtime) && $mtime > 0)) {
+
+ /** @var File $file */
+ $file = $this->userFolder->get($path);
+
+ if ($file instanceof Folder) {
+ return new DataResponse(['message' => $this->l->t('You can not write to a folder')], Http::STATUS_BAD_REQUEST);
+ }
+
+ // Get file mtime
+ $filemtime = $file->getMTime();
+ if($mtime !== $filemtime) {
+ // Then the file has changed since opening
+ $this->logger->error('File: ' . $path . ' modified since opening.',
+ ['app' => 'files_texteditor']);
+ return new DataResponse(
+ ['message' => $this->l->t('Cannot save file as it has been modified since opening')],
+ Http::STATUS_BAD_REQUEST);
+ } else {
+ // File same as when opened, save file
+ if($file->isUpdateable()) {
+ $filecontents = iconv(mb_detect_encoding($filecontents), 'UTF-8', $filecontents);
+ try {
+ $file->putContent($filecontents);
+ } catch (LockedException $e) {
+ $message = (string) $this->l->t('The file is locked.');
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ } catch (ForbiddenException $e) {
+ return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
+ }
+ // Clear statcache
+ clearstatcache();
+ // Get new mtime
+ $newmtime = $file->getMTime();
+ $newsize = $file->getSize();
+ return new DataResponse(['mtime' => $newmtime, 'size' => $newsize], Http::STATUS_OK);
+ } else {
+ // Not writeable!
+ $this->logger->error('User does not have permission to write to file: ' . $path,
+ ['app' => 'files_texteditor']);
+ return new DataResponse([ 'message' => $this->l->t('Insufficient permissions')],
+ Http::STATUS_BAD_REQUEST);
+ }
+ }
+ } else if ($path === '') {
+ $this->logger->error('No file path supplied');
+ return new DataResponse(['message' => $this->l->t('File path not supplied')], Http::STATUS_BAD_REQUEST);
+ } else {
+ $this->logger->error('No file mtime supplied', ['app' => 'files_texteditor']);
+ return new DataResponse(['message' => $this->l->t('File mtime not supplied')], Http::STATUS_BAD_REQUEST);
+ }
+
+ } catch (HintException $e) {
+ $message = (string)$e->getHint();
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ } catch (\Exception $e) {
+ $message = (string)$this->l->t('An internal server error occurred.');
+ return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
+ }
+ }
+
+}