File indexing completed on 2024-04-28 17:09:56

0001 <?php
0002 
0003 /**
0004  * ocs-fileserver
0005  *
0006  * Copyright 2016 by pling GmbH.
0007  *
0008  * This file is part of ocs-fileserver.
0009  *
0010  * ocs-fileserver is free software: you can redistribute it and/or modify
0011  * it under the terms of the GNU Affero General Public License as published by
0012  * the Free Software Foundation, either version 3 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * ocs-fileserver is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Affero General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Affero General Public License
0021  * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 
0024 class Favorites extends BaseController
0025 {
0026 
0027     public function getIndex()
0028     {
0029         $clientId = null;
0030         $userId = null;
0031         $ownerId = null;
0032         $collectionId = null;
0033         $fileId = null;
0034         $ids = null; // Comma-separated list
0035         $perpage = $this->appConfig->general['perpage'];
0036         $page = 1;
0037 
0038         if (!empty($this->request->client_id)) {
0039             $clientId = $this->request->client_id;
0040         }
0041         if (!empty($this->request->user_id)) {
0042             $userId = $this->request->user_id;
0043         }
0044         if (!empty($this->request->owner_id)) {
0045             $ownerId = $this->request->owner_id;
0046         }
0047         if (!empty($this->request->collection_id)) {
0048             $collectionId = $this->request->collection_id;
0049         }
0050         if (!empty($this->request->file_id)) {
0051             $fileId = $this->request->file_id;
0052         }
0053         if (!empty($this->request->ids)) {
0054             $ids = $this->request->ids;
0055         }
0056         if (!empty($this->request->perpage)
0057             && $this->_isValidPerpageNumber($this->request->perpage)
0058         ) {
0059             $perpage = $this->request->perpage;
0060         }
0061         if (!empty($this->request->page)
0062             && $this->_isValidPageNumber($this->request->page)
0063         ) {
0064             $page = $this->request->page;
0065         }
0066 
0067         $favorites = $this->models->favorites->getFavorites(
0068             $clientId,
0069             $userId,
0070             $ownerId,
0071             $collectionId,
0072             $fileId,
0073             $ids,
0074             $perpage,
0075             $page
0076         );
0077 
0078         if (!$favorites) {
0079             $this->response->setStatus(404);
0080             throw new Flooer_Exception('Not found', LOG_NOTICE);
0081         }
0082 
0083         $this->_setResponseContent('success', $favorites);
0084     }
0085 
0086     public function getFavorite()
0087     {
0088         $id = null;
0089 
0090         if (!empty($this->request->id)) {
0091             $id = $this->request->id;
0092         }
0093 
0094         $favorite = $this->models->favorites->$id;
0095 
0096         if (!$favorite) {
0097             $this->response->setStatus(404);
0098             throw new Flooer_Exception('Not found', LOG_NOTICE);
0099         }
0100 
0101         $this->_setResponseContent(
0102             'success',
0103             array('favorite' => $favorite)
0104         );
0105     }
0106 
0107     public function postFavorite()
0108     {
0109         // Get favorite information or add new one
0110 
0111         if (!$this->_isAllowedAccess()) {
0112             $this->response->setStatus(403);
0113             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0114         }
0115 
0116         $id = null; // Auto generated
0117         $clientId = null;
0118         $userId = null;
0119         $ownerId = null; // Auto detect
0120         $collectionId = null; // Auto detect
0121         $fileId = null;
0122 
0123         if (!empty($this->request->client_id)) {
0124             $clientId = $this->request->client_id;
0125         }
0126         if (!empty($this->request->user_id)) {
0127             $userId = $this->request->user_id;
0128         }
0129         if (!empty($this->request->owner_id)) {
0130             $ownerId = $this->request->owner_id;
0131         }
0132         if (!empty($this->request->collection_id)) {
0133             $collectionId = $this->request->collection_id;
0134         }
0135         if (!empty($this->request->file_id)) {
0136             $fileId = $this->request->file_id;
0137         }
0138 
0139         $errors = array();
0140         if (!$clientId) {
0141             $errors['client_id'] = 'Required';
0142         }
0143         if (!$userId) {
0144             $errors['user_id'] = 'Required';
0145         }
0146         if (!$ownerId && !$collectionId && !$fileId) {
0147             $errors['owner_id'] = 'Missing';
0148             $errors['collection_id'] = 'Missing';
0149             $errors['file_id'] = 'Missing';
0150         }
0151 
0152         if ($errors) {
0153             $this->response->setStatus(400);
0154             $this->_setResponseContent(
0155                 'error',
0156                 array(
0157                     'message' => 'Validation error',
0158                     'errors' => $errors
0159                 )
0160             );
0161             return;
0162         }
0163 
0164         $favorite = null;
0165         if ($fileId) {
0166             $favorite = $this->models->favorites->getFavoriteFile(
0167                 $clientId,
0168                 $userId,
0169                 $fileId
0170             );
0171         }
0172         else if ($collectionId) {
0173             $favorite = $this->models->favorites->getFavoriteCollection(
0174                 $clientId,
0175                 $userId,
0176                 $collectionId
0177             );
0178         }
0179         else if ($ownerId) {
0180             $favorite = $this->models->favorites->getFavoriteOwner(
0181                 $clientId,
0182                 $userId,
0183                 $ownerId
0184             );
0185         }
0186 
0187         if (!$favorite) {
0188             $_clientId = null;
0189             if ($fileId) {
0190                 $file = $this->models->files->$fileId;
0191                 if (!$file) {
0192                     $this->response->setStatus(404);
0193                     throw new Flooer_Exception('Not found', LOG_NOTICE);
0194                 }
0195                 else if (!$file->active) {
0196                     $this->response->setStatus(403);
0197                     throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0198                 }
0199                 $_clientId = $file->client_id;
0200                 $ownerId = $file->owner_id;
0201                 $collectionId = $file->collection_id;
0202             }
0203             else if ($collectionId) {
0204                 $collection = $this->models->collections->$collectionId;
0205                 if (!$collection) {
0206                     $this->response->setStatus(404);
0207                     throw new Flooer_Exception('Not found', LOG_NOTICE);
0208                 }
0209                 $_clientId = $collection->client_id;
0210                 $ownerId = $collection->owner_id;
0211             }
0212             else if ($ownerId) {
0213                 $_clientId = $clientId;
0214             }
0215 
0216             if ($_clientId != $clientId) {
0217                 $this->response->setStatus(403);
0218                 throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0219             }
0220 
0221             $id = $this->models->favorites->generateId();
0222             $this->models->favorites->$id = array(
0223                 'client_id' => $clientId,
0224                 'user_id' => $userId,
0225                 'owner_id' => $ownerId,
0226                 'collection_id' => $collectionId,
0227                 'file_id' => $fileId
0228             );
0229 
0230             $favorite = $this->models->favorites->$id;
0231         }
0232 
0233         $this->_setResponseContent(
0234             'success',
0235             array('favorite' => $favorite)
0236         );
0237     }
0238 
0239     public function deleteFavorite()
0240     {
0241         if (!$this->_isAllowedAccess()) {
0242             $this->response->setStatus(403);
0243             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0244         }
0245 
0246         $id = null;
0247 
0248         if (!empty($this->request->id)) {
0249             $id = $this->request->id;
0250         }
0251 
0252         $favorite = $this->models->favorites->$id;
0253 
0254         if (!$favorite) {
0255             $this->response->setStatus(404);
0256             throw new Flooer_Exception('Not found', LOG_NOTICE);
0257         }
0258         else if ($favorite->client_id != $this->request->client_id) {
0259             $this->response->setStatus(403);
0260             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0261         }
0262 
0263         unset($this->models->favorites->$id);
0264 
0265         $this->_setResponseContent('success');
0266     }
0267 
0268 }