File indexing completed on 2024-04-28 05:55:03

0001 <?php /** @noinspection PhpUndefinedFieldInspection */
0002 
0003 use Ocs\Storage\FilesystemAdapter;
0004 
0005 /**
0006  * ocs-fileserver
0007  *
0008  * Copyright 2016 by pling GmbH.
0009  *
0010  * This file is part of ocs-fileserver.
0011  *
0012  * ocs-fileserver is free software: you can redistribute it and/or modify
0013  * it under the terms of the GNU Affero General Public License as published by
0014  * the Free Software Foundation, either version 3 of the License, or
0015  * (at your option) any later version.
0016  *
0017  * ocs-fileserver is distributed in the hope that it will be useful,
0018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020  * GNU Affero General Public License for more details.
0021  *
0022  * You should have received a copy of the GNU Affero General Public License
0023  * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
0024  **/
0025 
0026 class Collections extends BaseController
0027 {
0028 
0029     public function getIndex()
0030     {
0031         $status = 'active';
0032         $clientId = null;
0033         $ownerId = null;
0034         $category = null;
0035         $tags = null; // Comma-separated list
0036         $contentId = null;
0037         $search = null; // 3 or more strings
0038         $ids = null; // Comma-separated list
0039         $favoriteIds = array();
0040         $downloadedTimeperiodBegin = null; // Datetime format
0041         $downloadedTimeperiodEnd = null; // Datetime format
0042         $sort = 'name';
0043         $perpage = $this->appConfig->general['perpage'];
0044         $page = 1;
0045 
0046         if (!empty($this->request->status)) {
0047             $status = $this->request->status;
0048         }
0049         if (!empty($this->request->client_id)) {
0050             $clientId = $this->request->client_id;
0051         }
0052         if (!empty($this->request->owner_id)) {
0053             $ownerId = $this->request->owner_id;
0054         }
0055         if (isset($this->request->category)) {
0056             $category = $this->request->category;
0057         }
0058         if (isset($this->request->tags)) {
0059             $tags = $this->request->tags;
0060         }
0061         if (isset($this->request->content_id)) {
0062             $contentId = $this->request->content_id;
0063         }
0064         if (!empty($this->request->search)) {
0065             $search = $this->request->search;
0066         }
0067         if (!empty($this->request->ids)) {
0068             $ids = $this->request->ids;
0069         }
0070         if (!empty($this->request->client_id)
0071             && !empty($this->request->favoritesby)
0072         ) {
0073             $favoriteIds = $this->_getFavoriteIds(
0074                 $this->request->client_id,
0075                 $this->request->favoritesby
0076             );
0077             if (!$favoriteIds) {
0078                 $this->response->setStatus(404);
0079                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0080             }
0081         }
0082         if (!empty($this->request->downloaded_timeperiod_begin)) {
0083             $downloadedTimeperiodBegin = $this->request->downloaded_timeperiod_begin;
0084         }
0085         if (!empty($this->request->downloaded_timeperiod_end)) {
0086             $downloadedTimeperiodEnd = $this->request->downloaded_timeperiod_end;
0087         }
0088         if (!empty($this->request->sort)) {
0089             $sort = $this->request->sort;
0090         }
0091         if (!empty($this->request->perpage)
0092             && $this->_isValidPerpageNumber($this->request->perpage)
0093         ) {
0094             $perpage = $this->request->perpage;
0095         }
0096         if (!empty($this->request->page)
0097             && $this->_isValidPageNumber($this->request->page)
0098         ) {
0099             $page = $this->request->page;
0100         }
0101 
0102         $collections = $this->models->collections->getCollections(
0103             $status,
0104             $clientId,
0105             $ownerId,
0106             $category,
0107             $tags,
0108             $contentId,
0109             $search,
0110             $ids,
0111             $favoriteIds,
0112             $downloadedTimeperiodBegin,
0113             $downloadedTimeperiodEnd,
0114             $sort,
0115             $perpage,
0116             $page
0117         );
0118 
0119         if (!$collections) {
0120             $this->response->setStatus(404);
0121             throw new Flooer_Exception('Not found', LOG_NOTICE);
0122         }
0123 
0124         $this->_setResponseContent('success', $collections);
0125     }
0126 
0127     public function getCollection()
0128     {
0129         $id = null;
0130 
0131         if (!empty($this->request->id)) {
0132             $id = $this->request->id;
0133         }
0134 
0135         $collection = $this->models->collections->getCollection($id);
0136 
0137         if (!$collection) {
0138             $this->response->setStatus(404);
0139             throw new Flooer_Exception('Not found', LOG_NOTICE);
0140         }
0141 
0142         $this->_setResponseContent(
0143             'success',
0144             array('collection' => $collection)
0145         );
0146     }
0147 
0148     public function postCollection()
0149     {
0150         if (!$this->_isAllowedAccess()) {
0151             $this->response->setStatus(403);
0152             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0153         }
0154 
0155         $id = null; // Auto generated
0156         $active = 1;
0157         $clientId = null;
0158         $ownerId = null;
0159         $name = null; // Auto generated
0160         $files = 0;
0161         $size = 0;
0162         $title = null; // Name as default
0163         $description = null;
0164         $category = null;
0165         $tags = null; // Comma-separated list
0166         $version = null;
0167         $contentId = null;
0168         $contentPage = null;
0169 
0170         if (!empty($this->request->client_id)) {
0171             $clientId = $this->request->client_id;
0172         }
0173         if (!empty($this->request->owner_id)) {
0174             $ownerId = $this->request->owner_id;
0175         }
0176         if (!empty($this->request->title)) {
0177             $title = mb_substr(strip_tags($this->request->title), 0, 200);
0178         }
0179         if (isset($this->request->description)) {
0180             $description = strip_tags($this->request->description);
0181         }
0182         if (isset($this->request->category)) {
0183             $category = mb_substr(strip_tags($this->request->category), 0, 64);
0184         }
0185         if (isset($this->request->tags)) {
0186             $tags = strip_tags($this->request->tags);
0187         }
0188         if (isset($this->request->version)) {
0189             $version = mb_substr(strip_tags($this->request->version), 0, 64);
0190         }
0191         if (isset($this->request->content_id)) {
0192             $contentId = $this->request->content_id;
0193         }
0194         if (!empty($this->request->content_page)) {
0195             $contentPage = $this->request->content_page;
0196         }
0197 
0198         $errors = array();
0199         if (!$clientId) {
0200             $errors['client_id'] = 'Required';
0201         }
0202         if (!$ownerId) {
0203             $errors['owner_id'] = 'Required';
0204         }
0205 
0206         if ($errors) {
0207             $this->response->setStatus(400);
0208             $this->_setResponseContent(
0209                 'error',
0210                 array(
0211                     'message' => 'Validation error',
0212                     'errors' => $errors
0213                 )
0214             );
0215             return;
0216         }
0217 
0218         $id = $this->models->collections->generateId();
0219         $name = $id;
0220         if (!$title) {
0221             $title = $name;
0222         }
0223 
0224         $fileSystemAdapter = new FilesystemAdapter($this->appConfig);
0225         //$fileSystemAdapter = new S3Adapter($this->appConfig);
0226 
0227         // create collection dir
0228         $collectionDir = $this->appConfig->general['filesDir'] . DIRECTORY_SEPARATOR . $name;
0229         if (!$fileSystemAdapter->testAndCreate($collectionDir)) {
0230             $this->response->setStatus(500);
0231             throw new Flooer_Exception('Failed to create collection', LOG_ALERT);
0232         }
0233 
0234         $this->models->collections->$id = array(
0235             'active' => $active,
0236             'client_id' => $clientId,
0237             'owner_id' => $ownerId,
0238             'name' => $name,
0239             'files' => $files,
0240             'size' => $size,
0241             'title' => $title,
0242             'description' => $description,
0243             'category' => $category,
0244             'tags' => $tags,
0245             'version' => $version,
0246             'content_id' => $contentId,
0247             'content_page' => $contentPage
0248         );
0249 
0250         $collection = $this->models->collections->getCollection($id);
0251 
0252         $this->_setResponseContent(
0253             'success',
0254             array('collection' => $collection)
0255         );
0256     }
0257 
0258     public function putCollection()
0259     {
0260         if (!$this->_isAllowedAccess()) {
0261             $this->response->setStatus(403);
0262             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0263         }
0264 
0265         $id = null;
0266         $title = null;
0267         $description = null;
0268         $category = null;
0269         $tags = null; // Comma-separated list
0270         $version = null;
0271         $contentId = null;
0272         $contentPage = null;
0273 
0274         if (!empty($this->request->id)) {
0275             $id = $this->request->id;
0276         }
0277         if (!empty($this->request->title)) {
0278             $title = mb_substr(strip_tags($this->request->title), 0, 200);
0279         }
0280         if (isset($this->request->description)) {
0281             $description = strip_tags($this->request->description);
0282         }
0283         if (isset($this->request->category)) {
0284             $category = mb_substr(strip_tags($this->request->category), 0, 64);
0285         }
0286         if (isset($this->request->tags)) {
0287             $tags = strip_tags($this->request->tags);
0288         }
0289         if (isset($this->request->version)) {
0290             $version = mb_substr(strip_tags($this->request->version), 0, 64);
0291         }
0292         if (isset($this->request->content_id)) {
0293             $contentId = $this->request->content_id;
0294         }
0295         if (!empty($this->request->content_page)) {
0296             $contentPage = $this->request->content_page;
0297         }
0298 
0299         $collection = $this->models->collections->$id;
0300 
0301         if (!$collection) {
0302             $this->response->setStatus(404);
0303             throw new Flooer_Exception('Not found', LOG_NOTICE);
0304         }
0305         else if (!$collection->active || $collection->client_id != $this->request->client_id) {
0306             $this->response->setStatus(403);
0307             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0308         }
0309 
0310         $updata = array();
0311         if ($title !== null) {
0312             $updata['title'] = $title;
0313         }
0314         if ($description !== null) {
0315             $updata['description'] = $description;
0316         }
0317         if ($category !== null) {
0318             $updata['category'] = $category;
0319         }
0320         if ($tags !== null) {
0321             $updata['tags'] = $tags;
0322         }
0323         if ($version !== null) {
0324             $updata['version'] = $version;
0325         }
0326         if ($contentId !== null) {
0327             $updata['content_id'] = $contentId;
0328         }
0329         if ($contentPage !== null) {
0330             $updata['content_page'] = $contentPage;
0331         }
0332 
0333         $this->models->collections->$id = $updata;
0334 
0335         $collection = $this->models->collections->getCollection($id);
0336 
0337         $this->_setResponseContent(
0338             'success',
0339             array('collection' => $collection)
0340         );
0341     }
0342 
0343     public function deleteCollection() {
0344         // Please be care the remove process in Owners::deleteOwner()
0345 
0346         if (!$this->_isAllowedAccess()) {
0347             $this->response->setStatus(403);
0348             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0349         }
0350 
0351         $id = null;
0352 
0353         if (!empty($this->request->id)) {
0354             $id = $this->request->id;
0355         }
0356 
0357         $collection = $this->models->collections->$id;
0358 
0359         if (!$collection) {
0360             $this->response->setStatus(404);
0361             throw new Flooer_Exception('Not found', LOG_NOTICE);
0362         } else if (!$collection->active || $collection->client_id != $this->request->client_id) {
0363             $this->response->setStatus(403);
0364             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0365         }
0366 
0367         $thumbnail = $this->appConfig->general['thumbnailsDir'] . '/collection_' . $id . '.jpg';
0368         if (is_file($thumbnail)) {
0369             unlink($thumbnail);
0370         }
0371 
0372         $fileSystemAdapter = new FilesystemAdapter($this->appConfig);
0373         //$fileSystemAdapter = new \Ocs\Storage\S3Adapter($this->appConfig);
0374 
0375         // move collection to trash dir
0376         $trashDir = $this->appConfig->general['filesDir'] . '/.trash';
0377         if (!$fileSystemAdapter->testAndCreate($trashDir)) {
0378             $this->log->log(__METHOD__ . " - trash dir not found and could not be created: $trashDir");
0379             $this->response->setStatus(500);
0380             throw new Flooer_Exception('Failed to remove the collection', LOG_ALERT);
0381         }
0382         $pathCollection = $this->appConfig->general['filesDir'] . '/' . $collection->name;
0383         if (is_dir($pathCollection) && !rename($pathCollection, $trashDir . '/' . $id . '-' . $collection->name))
0384         {
0385             $this->response->setStatus(500);
0386             throw new Flooer_Exception('Failed to remove the collection', LOG_ALERT);
0387         }
0388 
0389         $this->models->collections->$id = array('active' => 0);
0390         //$this->models->collections_downloaded->deleteByCollectionId($id);
0391         //$this->models->files->deleteByCollectionId($id);
0392         //$this->models->files_downloaded->deleteByCollectionId($id);
0393         $this->models->favorites->deleteByCollectionId($id);
0394         $this->models->media->deleteByCollectionId($id);
0395         $this->models->media_played->deleteByCollectionId($id);
0396 
0397         $this->_setResponseContent('success');
0398     }
0399 
0400     public function headDownload()
0401     {
0402         $this->getDownload(true);
0403     }
0404 
0405     public function getDownload($headeronly = false)
0406     {
0407         // Collection download is disabled
0408         $this->response->setStatus(403);
0409         throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0410 
0411 //        $id = null;
0412 //        $userId = null;
0413 //
0414 //        if (!empty($this->request->id)) {
0415 //            $id = $this->request->id;
0416 //        }
0417 //        if (!empty($this->request->u)) {
0418 //            $userId = $this->request->u;
0419 //        }
0420 //
0421 //        $collection = $this->models->collections->$id;
0422 //
0423 //        if (!$collection) {
0424 //            $this->response->setStatus(404);
0425 //            throw new Flooer_Exception('Not found', LOG_NOTICE);
0426 //        }
0427 //        else if (!$collection->active) {
0428 //            $this->response->setStatus(403);
0429 //            throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0430 //        }
0431 //
0432 //        $archive = '/tmp/archives/' . $collection->name . '.tar.gz';
0433 //        $this->_generateArchive(
0434 //            $this->appConfig->general['filesDir'] . '/' . $collection->name,
0435 //            $archive
0436 //        );
0437 //
0438 //        $profile = $this->models->profiles->getProfileByClientIdAndOwnerId(
0439 //            $collection->client_id,
0440 //            $collection->owner_id
0441 //        );
0442 //
0443 //        $profileName = $collection->owner_id;
0444 //        if ($profile) {
0445 //            $profileName = $profile->name;
0446 //        }
0447 //
0448 //        $collectionTitle = $collection->name;
0449 //        if ($collection->title) {
0450 //            $collectionTitle = $collection->title;
0451 //        }
0452 //
0453 //        $filename = str_replace(' ', '_', $profileName)
0454 //            . '_' . str_replace(' ', '_', $collectionTitle);
0455 //
0456 //        if (!$headeronly && $collection->downloaded_ip != $this->server->REMOTE_ADDR) {
0457 //            $this->models->collections->updateDownloadedStatus($collection->id);
0458 //
0459 //            $downloadedId = $this->models->collections_downloaded->generateId();
0460 //            $this->models->collections_downloaded->$downloadedId = array(
0461 //                'client_id' => $collection->client_id,
0462 //                'owner_id' => $collection->owner_id,
0463 //                'collection_id' => $collection->id,
0464 //                'user_id' => $userId
0465 //            );
0466 //        }
0467 //
0468 //        $this->_sendFile(
0469 //            $archive,
0470 //            $filename . '.tar.gz',
0471 //            'application/x-gzip',
0472 //            filesize($archive),
0473 //            true,
0474 //            $headeronly
0475 //        );
0476     }
0477 
0478 }