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

0001 <?php
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 class Owners extends BaseController
0026 {
0027 
0028     /**
0029      * @throws Flooer_Exception
0030      */
0031     public function deleteOwner() {
0032         if (!$this->_isAllowedAccess()) {
0033             $this->response->setStatus(403);
0034             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0035         }
0036 
0037         if (empty($this->request->client_id) || empty($this->request->id)) {
0038             $this->response->setStatus(404);
0039             throw new Flooer_Exception('Not found', LOG_NOTICE);
0040         }
0041 
0042         $clientId = $this->request->client_id;
0043         $ownerId = $this->request->id;
0044 
0045         // Remove profile
0046         $profile = $this->models->profiles->getProfileByClientIdAndOwnerId($clientId, $ownerId);
0047         if ($profile && $profile->active) {
0048             $this->models->profiles->{$profile->id} = array('active' => 0);
0049         }
0050 
0051         // Remove collections and related data
0052         $collections = $this->models->collections->fetchRowset('WHERE active = :active AND client_id = :client_id AND owner_id = :owner_id',
0053                                                                array(
0054                                                                    ':active'    => 1,
0055                                                                    ':client_id' => $clientId,
0056                                                                    ':owner_id'  => $ownerId
0057                                                                ));
0058         if ($collections) {
0059             $this->log->log("Remove collections (client:$clientId; owner:$ownerId)", LOG_NOTICE);
0060             foreach ($collections as $collection) {
0061                 $thumbnail = $this->appConfig->general['thumbnailsDir'] . '/collection_' . $collection->id . '.jpg';
0062                 if (is_file($thumbnail)) {
0063                     unlink($thumbnail);
0064                 }
0065 
0066                 $fileSystemAdapter = new FilesystemAdapter($this->appConfig);
0067                 //$fileSystemAdapter = new \Ocs\Storage\S3Adapter($this->appConfig);
0068 
0069                 // move collection to trash dir
0070                 $trashDir = $this->appConfig->general['filesDir'] . '/.trash';
0071                 if (!$fileSystemAdapter->testAndCreate($trashDir)) {
0072                     $this->response->setStatus(500);
0073                     throw new Flooer_Exception('Failed to remove the collection', LOG_ALERT);
0074                 }
0075                 $pathCollection = $this->appConfig->general['filesDir'] . '/' . $collection->name;
0076                 if (is_dir($pathCollection) && !rename($pathCollection,$trashDir . '/' . $collection->id . '-' . $collection->name))
0077                 {
0078                     $this->response->setStatus(500);
0079                     throw new Flooer_Exception('Failed to remove the collection', LOG_ALERT);
0080                 }
0081 
0082                 $this->models->collections->{$collection->id} = array('active' => 0);
0083                 //$this->models->collections_downloaded->deleteByCollectionId($collection->id);
0084                 //$this->models->files->deleteByCollectionId($collection->id);
0085                 //$this->models->files_downloaded->deleteByCollectionId($collection->id);
0086                 $this->models->favorites->deleteByCollectionId($collection->id);
0087                 $this->models->media->deleteByCollectionId($collection->id);
0088                 $this->models->media_played->deleteByCollectionId($collection->id);
0089             }
0090         }
0091 
0092         // Remove user or owner from favorites
0093         $favorites = $this->models->favorites->fetchRowset('WHERE client_id = :client_id' . ' AND (user_id = :user_id OR owner_id = :owner_id)',
0094                                                            array(
0095                                                                ':client_id' => $clientId,
0096                                                                ':user_id'   => $ownerId,
0097                                                                ':owner_id'  => $ownerId
0098                                                            ));
0099         if ($favorites) {
0100             foreach ($favorites as $favorite) {
0101                 unset($this->models->favorites->{$favorite->id});
0102             }
0103         }
0104 
0105         $this->_setResponseContent('success');
0106     }
0107 
0108 }