Completed
Pull Request — master (#4038)
by Lukas
23:28 queued 11:41
created
apps/files/lib/Controller/ViewController.php 1 patch
Indentation   +203 added lines, -203 removed lines patch added patch discarded remove patch
@@ -49,207 +49,207 @@
 block discarded – undo
49 49
  * @package OCA\Files\Controller
50 50
  */
51 51
 class ViewController extends Controller {
52
-	/** @var string */
53
-	protected $appName;
54
-	/** @var IRequest */
55
-	protected $request;
56
-	/** @var IURLGenerator */
57
-	protected $urlGenerator;
58
-	/** @var IL10N */
59
-	protected $l10n;
60
-	/** @var IConfig */
61
-	protected $config;
62
-	/** @var EventDispatcherInterface */
63
-	protected $eventDispatcher;
64
-	/** @var IUserSession */
65
-	protected $userSession;
66
-	/** @var IAppManager */
67
-	protected $appManager;
68
-	/** @var IRootFolder */
69
-	protected $rootFolder;
70
-
71
-	/**
72
-	 * @param string $appName
73
-	 * @param IRequest $request
74
-	 * @param IURLGenerator $urlGenerator
75
-	 * @param IL10N $l10n
76
-	 * @param IConfig $config
77
-	 * @param EventDispatcherInterface $eventDispatcherInterface
78
-	 * @param IUserSession $userSession
79
-	 * @param IAppManager $appManager
80
-	 * @param IRootFolder $rootFolder
81
-	 */
82
-	public function __construct($appName,
83
-								IRequest $request,
84
-								IURLGenerator $urlGenerator,
85
-								IL10N $l10n,
86
-								IConfig $config,
87
-								EventDispatcherInterface $eventDispatcherInterface,
88
-								IUserSession $userSession,
89
-								IAppManager $appManager,
90
-								IRootFolder $rootFolder
91
-	) {
92
-		parent::__construct($appName, $request);
93
-		$this->appName = $appName;
94
-		$this->request = $request;
95
-		$this->urlGenerator = $urlGenerator;
96
-		$this->l10n = $l10n;
97
-		$this->config = $config;
98
-		$this->eventDispatcher = $eventDispatcherInterface;
99
-		$this->userSession = $userSession;
100
-		$this->appManager = $appManager;
101
-		$this->rootFolder = $rootFolder;
102
-	}
103
-
104
-	/**
105
-	 * @param string $appName
106
-	 * @param string $scriptName
107
-	 * @return string
108
-	 */
109
-	protected function renderScript($appName, $scriptName) {
110
-		$content = '';
111
-		$appPath = \OC_App::getAppPath($appName);
112
-		$scriptPath = $appPath . '/' . $scriptName;
113
-		if (file_exists($scriptPath)) {
114
-			// TODO: sanitize path / script name ?
115
-			ob_start();
116
-			include $scriptPath;
117
-			$content = ob_get_contents();
118
-			@ob_end_clean();
119
-		}
120
-		return $content;
121
-	}
122
-
123
-	/**
124
-	 * FIXME: Replace with non static code
125
-	 *
126
-	 * @return array
127
-	 * @throws \OCP\Files\NotFoundException
128
-	 */
129
-	protected function getStorageInfo() {
130
-		$dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
131
-		return \OC_Helper::getStorageInfo('/', $dirInfo);
132
-	}
133
-
134
-	/**
135
-	 * @NoCSRFRequired
136
-	 * @NoAdminRequired
137
-	 *
138
-	 * @param string $dir
139
-	 * @param string $view
140
-	 * @param string $fileid
141
-	 * @return TemplateResponse|RedirectResponse
142
-	 */
143
-	public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) {
144
-		if ($fileid !== null) {
145
-			try {
146
-				return $this->showFile($fileid);
147
-			} catch (NotFoundException $e) {
148
-				return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
149
-			}
150
-		}
151
-
152
-		$nav = new \OCP\Template('files', 'appnavigation', '');
153
-
154
-		// Load the files we need
155
-		\OCP\Util::addStyle('files', 'merged');
156
-		\OCP\Util::addScript('files', 'merged-index');
157
-
158
-		// mostly for the home storage's free space
159
-		// FIXME: Make non static
160
-		$storageInfo = $this->getStorageInfo();
161
-
162
-		\OCA\Files\App::getNavigationManager()->add(
163
-			[
164
-				'id' => 'favorites',
165
-				'appname' => 'files',
166
-				'script' => 'simplelist.php',
167
-				'order' => 5,
168
-				'name' => $this->l10n->t('Favorites')
169
-			]
170
-		);
171
-
172
-		$navItems = \OCA\Files\App::getNavigationManager()->getAll();
173
-		usort($navItems, function($item1, $item2) {
174
-			return $item1['order'] - $item2['order'];
175
-		});
176
-		$nav->assign('navigationItems', $navItems);
177
-
178
-		$contentItems = [];
179
-
180
-		// render the container content for every navigation item
181
-		foreach ($navItems as $item) {
182
-			$content = '';
183
-			if (isset($item['script'])) {
184
-				$content = $this->renderScript($item['appname'], $item['script']);
185
-			}
186
-			$contentItem = [];
187
-			$contentItem['id'] = $item['id'];
188
-			$contentItem['content'] = $content;
189
-			$contentItems[] = $contentItem;
190
-		}
191
-
192
-		$this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts');
193
-
194
-		$params = [];
195
-		$params['usedSpacePercent'] = (int)$storageInfo['relative'];
196
-		$params['owner'] = $storageInfo['owner'];
197
-		$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'];
198
-		$params['isPublic'] = false;
199
-		$params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
200
-		$user = $this->userSession->getUser()->getUID();
201
-		$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
202
-		$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
203
-		$showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
204
-		$params['showHiddenFiles'] = $showHidden ? 1 : 0;
205
-		$params['fileNotFound'] = $fileNotFound ? 1 : 0;
206
-		$params['appNavigation'] = $nav;
207
-		$params['appContents'] = $contentItems;
208
-
209
-		$response = new TemplateResponse(
210
-			$this->appName,
211
-			'index',
212
-			$params
213
-		);
214
-		$policy = new ContentSecurityPolicy();
215
-		$policy->addAllowedFrameDomain('\'self\'');
216
-		$response->setContentSecurityPolicy($policy);
217
-
218
-		return $response;
219
-	}
220
-
221
-	/**
222
-	 * Redirects to the file list and highlight the given file id
223
-	 *
224
-	 * @param string $fileId file id to show
225
-	 * @return RedirectResponse redirect response or not found response
226
-	 * @throws \OCP\Files\NotFoundException
227
-	 */
228
-	private function showFile($fileId) {
229
-		$uid = $this->userSession->getUser()->getUID();
230
-		$baseFolder = $this->rootFolder->getUserFolder($uid);
231
-		$files = $baseFolder->getById($fileId);
232
-		$params = [];
233
-
234
-		if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
235
-			$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
236
-			$files = $baseFolder->getById($fileId);
237
-			$params['view'] = 'trashbin';
238
-		}
239
-
240
-		if (!empty($files)) {
241
-			$file = current($files);
242
-			if ($file instanceof Folder) {
243
-				// set the full path to enter the folder
244
-				$params['dir'] = $baseFolder->getRelativePath($file->getPath());
245
-			} else {
246
-				// set parent path as dir
247
-				$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
248
-				// and scroll to the entry
249
-				$params['scrollto'] = $file->getName();
250
-			}
251
-			return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
252
-		}
253
-		throw new \OCP\Files\NotFoundException();
254
-	}
52
+    /** @var string */
53
+    protected $appName;
54
+    /** @var IRequest */
55
+    protected $request;
56
+    /** @var IURLGenerator */
57
+    protected $urlGenerator;
58
+    /** @var IL10N */
59
+    protected $l10n;
60
+    /** @var IConfig */
61
+    protected $config;
62
+    /** @var EventDispatcherInterface */
63
+    protected $eventDispatcher;
64
+    /** @var IUserSession */
65
+    protected $userSession;
66
+    /** @var IAppManager */
67
+    protected $appManager;
68
+    /** @var IRootFolder */
69
+    protected $rootFolder;
70
+
71
+    /**
72
+     * @param string $appName
73
+     * @param IRequest $request
74
+     * @param IURLGenerator $urlGenerator
75
+     * @param IL10N $l10n
76
+     * @param IConfig $config
77
+     * @param EventDispatcherInterface $eventDispatcherInterface
78
+     * @param IUserSession $userSession
79
+     * @param IAppManager $appManager
80
+     * @param IRootFolder $rootFolder
81
+     */
82
+    public function __construct($appName,
83
+                                IRequest $request,
84
+                                IURLGenerator $urlGenerator,
85
+                                IL10N $l10n,
86
+                                IConfig $config,
87
+                                EventDispatcherInterface $eventDispatcherInterface,
88
+                                IUserSession $userSession,
89
+                                IAppManager $appManager,
90
+                                IRootFolder $rootFolder
91
+    ) {
92
+        parent::__construct($appName, $request);
93
+        $this->appName = $appName;
94
+        $this->request = $request;
95
+        $this->urlGenerator = $urlGenerator;
96
+        $this->l10n = $l10n;
97
+        $this->config = $config;
98
+        $this->eventDispatcher = $eventDispatcherInterface;
99
+        $this->userSession = $userSession;
100
+        $this->appManager = $appManager;
101
+        $this->rootFolder = $rootFolder;
102
+    }
103
+
104
+    /**
105
+     * @param string $appName
106
+     * @param string $scriptName
107
+     * @return string
108
+     */
109
+    protected function renderScript($appName, $scriptName) {
110
+        $content = '';
111
+        $appPath = \OC_App::getAppPath($appName);
112
+        $scriptPath = $appPath . '/' . $scriptName;
113
+        if (file_exists($scriptPath)) {
114
+            // TODO: sanitize path / script name ?
115
+            ob_start();
116
+            include $scriptPath;
117
+            $content = ob_get_contents();
118
+            @ob_end_clean();
119
+        }
120
+        return $content;
121
+    }
122
+
123
+    /**
124
+     * FIXME: Replace with non static code
125
+     *
126
+     * @return array
127
+     * @throws \OCP\Files\NotFoundException
128
+     */
129
+    protected function getStorageInfo() {
130
+        $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
131
+        return \OC_Helper::getStorageInfo('/', $dirInfo);
132
+    }
133
+
134
+    /**
135
+     * @NoCSRFRequired
136
+     * @NoAdminRequired
137
+     *
138
+     * @param string $dir
139
+     * @param string $view
140
+     * @param string $fileid
141
+     * @return TemplateResponse|RedirectResponse
142
+     */
143
+    public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) {
144
+        if ($fileid !== null) {
145
+            try {
146
+                return $this->showFile($fileid);
147
+            } catch (NotFoundException $e) {
148
+                return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
149
+            }
150
+        }
151
+
152
+        $nav = new \OCP\Template('files', 'appnavigation', '');
153
+
154
+        // Load the files we need
155
+        \OCP\Util::addStyle('files', 'merged');
156
+        \OCP\Util::addScript('files', 'merged-index');
157
+
158
+        // mostly for the home storage's free space
159
+        // FIXME: Make non static
160
+        $storageInfo = $this->getStorageInfo();
161
+
162
+        \OCA\Files\App::getNavigationManager()->add(
163
+            [
164
+                'id' => 'favorites',
165
+                'appname' => 'files',
166
+                'script' => 'simplelist.php',
167
+                'order' => 5,
168
+                'name' => $this->l10n->t('Favorites')
169
+            ]
170
+        );
171
+
172
+        $navItems = \OCA\Files\App::getNavigationManager()->getAll();
173
+        usort($navItems, function($item1, $item2) {
174
+            return $item1['order'] - $item2['order'];
175
+        });
176
+        $nav->assign('navigationItems', $navItems);
177
+
178
+        $contentItems = [];
179
+
180
+        // render the container content for every navigation item
181
+        foreach ($navItems as $item) {
182
+            $content = '';
183
+            if (isset($item['script'])) {
184
+                $content = $this->renderScript($item['appname'], $item['script']);
185
+            }
186
+            $contentItem = [];
187
+            $contentItem['id'] = $item['id'];
188
+            $contentItem['content'] = $content;
189
+            $contentItems[] = $contentItem;
190
+        }
191
+
192
+        $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts');
193
+
194
+        $params = [];
195
+        $params['usedSpacePercent'] = (int)$storageInfo['relative'];
196
+        $params['owner'] = $storageInfo['owner'];
197
+        $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'];
198
+        $params['isPublic'] = false;
199
+        $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
200
+        $user = $this->userSession->getUser()->getUID();
201
+        $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
202
+        $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
203
+        $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
204
+        $params['showHiddenFiles'] = $showHidden ? 1 : 0;
205
+        $params['fileNotFound'] = $fileNotFound ? 1 : 0;
206
+        $params['appNavigation'] = $nav;
207
+        $params['appContents'] = $contentItems;
208
+
209
+        $response = new TemplateResponse(
210
+            $this->appName,
211
+            'index',
212
+            $params
213
+        );
214
+        $policy = new ContentSecurityPolicy();
215
+        $policy->addAllowedFrameDomain('\'self\'');
216
+        $response->setContentSecurityPolicy($policy);
217
+
218
+        return $response;
219
+    }
220
+
221
+    /**
222
+     * Redirects to the file list and highlight the given file id
223
+     *
224
+     * @param string $fileId file id to show
225
+     * @return RedirectResponse redirect response or not found response
226
+     * @throws \OCP\Files\NotFoundException
227
+     */
228
+    private function showFile($fileId) {
229
+        $uid = $this->userSession->getUser()->getUID();
230
+        $baseFolder = $this->rootFolder->getUserFolder($uid);
231
+        $files = $baseFolder->getById($fileId);
232
+        $params = [];
233
+
234
+        if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
235
+            $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
236
+            $files = $baseFolder->getById($fileId);
237
+            $params['view'] = 'trashbin';
238
+        }
239
+
240
+        if (!empty($files)) {
241
+            $file = current($files);
242
+            if ($file instanceof Folder) {
243
+                // set the full path to enter the folder
244
+                $params['dir'] = $baseFolder->getRelativePath($file->getPath());
245
+            } else {
246
+                // set parent path as dir
247
+                $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
248
+                // and scroll to the entry
249
+                $params['scrollto'] = $file->getName();
250
+            }
251
+            return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
252
+        }
253
+        throw new \OCP\Files\NotFoundException();
254
+    }
255 255
 }
Please login to merge, or discard this patch.