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

0001 <?php /** @noinspection PhpUndefinedFieldInspection */
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 Media extends BaseController
0025 {
0026 
0027     public function getGenres()
0028     {
0029         $clientId = null;
0030         $ownerId = null;
0031         $collectionId = null;
0032         $collectionCategory = null;
0033         $collectionTags = null; // Comma-separated list
0034         $collectionContentId = null;
0035         $fileId = null;
0036         $fileTypes = null; // Comma-separated list
0037         $fileCategory = null;
0038         $fileTags = null; // Comma-separated list
0039         $fileOcsCompatibility = 'all';
0040         $fileContentId = null;
0041         $artistId = null;
0042         $albumId = null;
0043         $genre = null;
0044         $search = null; // 3 or more strings
0045         $favoriteIds = array();
0046         $sort = 'name';
0047         $perpage = $this->appConfig->general['perpage'];
0048         $page = 1;
0049 
0050         if (!empty($this->request->client_id)) {
0051             $clientId = $this->request->client_id;
0052         }
0053         if (!empty($this->request->owner_id)) {
0054             $ownerId = $this->request->owner_id;
0055         }
0056         if (!empty($this->request->collection_id)) {
0057             $collectionId = $this->request->collection_id;
0058         }
0059         if (isset($this->request->collection_category)) {
0060             $collectionCategory = $this->request->collection_category;
0061         }
0062         if (isset($this->request->collection_tags)) {
0063             $collectionTags = $this->request->collection_tags;
0064         }
0065         if (isset($this->request->collection_content_id)) {
0066             $collectionContentId = $this->request->collection_content_id;
0067         }
0068         if (!empty($this->request->file_id)) {
0069             $fileId = $this->request->file_id;
0070         }
0071         if (!empty($this->request->file_types)) {
0072             $fileTypes = $this->request->file_types;
0073         }
0074         if (isset($this->request->file_category)) {
0075             $fileCategory = $this->request->file_category;
0076         }
0077         if (isset($this->request->file_tags)) {
0078             $fileTags = $this->request->file_tags;
0079         }
0080         if (!empty($this->request->file_ocs_compatibility)) {
0081             $fileOcsCompatibility = $this->request->file_ocs_compatibility;
0082         }
0083         if (isset($this->request->file_content_id)) {
0084             $fileContentId = $this->request->file_content_id;
0085         }
0086         if (!empty($this->request->artist_id)) {
0087             $artistId = $this->request->artist_id;
0088         }
0089         if (!empty($this->request->album_id)) {
0090             $albumId = $this->request->album_id;
0091         }
0092         if (!empty($this->request->genre)) {
0093             $genre = $this->request->genre;
0094         }
0095         if (!empty($this->request->search)) {
0096             $search = $this->request->search;
0097         }
0098         if (!empty($this->request->client_id)
0099             && !empty($this->request->favoritesby)
0100         ) {
0101             $favoriteIds = $this->_getFavoriteIds(
0102                 $this->request->client_id,
0103                 $this->request->favoritesby
0104             );
0105             if (!$favoriteIds) {
0106                 $this->response->setStatus(404);
0107                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0108             }
0109         }
0110         if (!empty($this->request->sort)) {
0111             $sort = $this->request->sort;
0112         }
0113         if (!empty($this->request->perpage)
0114             && $this->_isValidPerpageNumber($this->request->perpage)
0115         ) {
0116             $perpage = $this->request->perpage;
0117         }
0118         if (!empty($this->request->page)
0119             && $this->_isValidPageNumber($this->request->page)
0120         ) {
0121             $page = $this->request->page;
0122         }
0123 
0124         $genres = $this->models->media->getGenres(
0125             $clientId,
0126             $ownerId,
0127             $collectionId,
0128             $collectionCategory,
0129             $collectionTags,
0130             $collectionContentId,
0131             $fileId,
0132             $fileTypes,
0133             $fileCategory,
0134             $fileTags,
0135             $fileOcsCompatibility,
0136             $fileContentId,
0137             $artistId,
0138             $albumId,
0139             $genre,
0140             $search,
0141             $favoriteIds,
0142             $sort,
0143             $perpage,
0144             $page
0145         );
0146 
0147         if (!$genres) {
0148             $this->response->setStatus(404);
0149             throw new Flooer_Exception('Not found', LOG_NOTICE);
0150         }
0151 
0152         $this->_setResponseContent('success', $genres);
0153     }
0154 
0155     public function getOwners()
0156     {
0157         $clientId = null;
0158         $ownerId = null;
0159         $collectionId = null;
0160         $collectionCategory = null;
0161         $collectionTags = null; // Comma-separated list
0162         $collectionContentId = null;
0163         $fileId = null;
0164         $fileTypes = null; // Comma-separated list
0165         $fileCategory = null;
0166         $fileTags = null; // Comma-separated list
0167         $fileOcsCompatibility = 'all';
0168         $fileContentId = null;
0169         $artistId = null;
0170         $albumId = null;
0171         $genre = null;
0172         $search = null; // 3 or more strings
0173         $favoriteIds = array();
0174         $sort = 'name';
0175         $perpage = $this->appConfig->general['perpage'];
0176         $page = 1;
0177 
0178         if (!empty($this->request->client_id)) {
0179             $clientId = $this->request->client_id;
0180         }
0181         if (!empty($this->request->owner_id)) {
0182             $ownerId = $this->request->owner_id;
0183         }
0184         if (!empty($this->request->collection_id)) {
0185             $collectionId = $this->request->collection_id;
0186         }
0187         if (isset($this->request->collection_category)) {
0188             $collectionCategory = $this->request->collection_category;
0189         }
0190         if (isset($this->request->collection_tags)) {
0191             $collectionTags = $this->request->collection_tags;
0192         }
0193         if (isset($this->request->collection_content_id)) {
0194             $collectionContentId = $this->request->collection_content_id;
0195         }
0196         if (!empty($this->request->file_id)) {
0197             $fileId = $this->request->file_id;
0198         }
0199         if (!empty($this->request->file_types)) {
0200             $fileTypes = $this->request->file_types;
0201         }
0202         if (isset($this->request->file_category)) {
0203             $fileCategory = $this->request->file_category;
0204         }
0205         if (isset($this->request->file_tags)) {
0206             $fileTags = $this->request->file_tags;
0207         }
0208         if (!empty($this->request->file_ocs_compatibility)) {
0209             $fileOcsCompatibility = $this->request->file_ocs_compatibility;
0210         }
0211         if (isset($this->request->file_content_id)) {
0212             $fileContentId = $this->request->file_content_id;
0213         }
0214         if (!empty($this->request->artist_id)) {
0215             $artistId = $this->request->artist_id;
0216         }
0217         if (!empty($this->request->album_id)) {
0218             $albumId = $this->request->album_id;
0219         }
0220         if (!empty($this->request->genre)) {
0221             $genre = $this->request->genre;
0222         }
0223         if (!empty($this->request->search)) {
0224             $search = $this->request->search;
0225         }
0226         if (!empty($this->request->client_id)
0227             && !empty($this->request->favoritesby)
0228         ) {
0229             $favoriteIds = $this->_getFavoriteIds(
0230                 $this->request->client_id,
0231                 $this->request->favoritesby
0232             );
0233             if (!$favoriteIds) {
0234                 $this->response->setStatus(404);
0235                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0236             }
0237         }
0238         if (!empty($this->request->sort)) {
0239             $sort = $this->request->sort;
0240         }
0241         if (!empty($this->request->perpage)
0242             && $this->_isValidPerpageNumber($this->request->perpage)
0243         ) {
0244             $perpage = $this->request->perpage;
0245         }
0246         if (!empty($this->request->page)
0247             && $this->_isValidPageNumber($this->request->page)
0248         ) {
0249             $page = $this->request->page;
0250         }
0251 
0252         $owners = $this->models->media->getOwners(
0253             $clientId,
0254             $ownerId,
0255             $collectionId,
0256             $collectionCategory,
0257             $collectionTags,
0258             $collectionContentId,
0259             $fileId,
0260             $fileTypes,
0261             $fileCategory,
0262             $fileTags,
0263             $fileOcsCompatibility,
0264             $fileContentId,
0265             $artistId,
0266             $albumId,
0267             $genre,
0268             $search,
0269             $favoriteIds,
0270             $sort,
0271             $perpage,
0272             $page
0273         );
0274 
0275         if (!$owners) {
0276             $this->response->setStatus(404);
0277             throw new Flooer_Exception('Not found', LOG_NOTICE);
0278         }
0279 
0280         $this->_setResponseContent('success', $owners);
0281     }
0282 
0283     public function getCollections()
0284     {
0285         $clientId = null;
0286         $ownerId = null;
0287         $collectionId = null;
0288         $collectionCategory = null;
0289         $collectionTags = null; // Comma-separated list
0290         $collectionContentId = null;
0291         $fileId = null;
0292         $fileTypes = null; // Comma-separated list
0293         $fileCategory = null;
0294         $fileTags = null; // Comma-separated list
0295         $fileOcsCompatibility = 'all';
0296         $fileContentId = null;
0297         $artistId = null;
0298         $albumId = null;
0299         $genre = null;
0300         $search = null; // 3 or more strings
0301         $favoriteIds = array();
0302         $sort = 'name';
0303         $perpage = $this->appConfig->general['perpage'];
0304         $page = 1;
0305 
0306         if (!empty($this->request->client_id)) {
0307             $clientId = $this->request->client_id;
0308         }
0309         if (!empty($this->request->owner_id)) {
0310             $ownerId = $this->request->owner_id;
0311         }
0312         if (!empty($this->request->collection_id)) {
0313             $collectionId = $this->request->collection_id;
0314         }
0315         if (isset($this->request->collection_category)) {
0316             $collectionCategory = $this->request->collection_category;
0317         }
0318         if (isset($this->request->collection_tags)) {
0319             $collectionTags = $this->request->collection_tags;
0320         }
0321         if (isset($this->request->collection_content_id)) {
0322             $collectionContentId = $this->request->collection_content_id;
0323         }
0324         if (!empty($this->request->file_id)) {
0325             $fileId = $this->request->file_id;
0326         }
0327         if (!empty($this->request->file_types)) {
0328             $fileTypes = $this->request->file_types;
0329         }
0330         if (isset($this->request->file_category)) {
0331             $fileCategory = $this->request->file_category;
0332         }
0333         if (isset($this->request->file_tags)) {
0334             $fileTags = $this->request->file_tags;
0335         }
0336         if (!empty($this->request->file_ocs_compatibility)) {
0337             $fileOcsCompatibility = $this->request->file_ocs_compatibility;
0338         }
0339         if (isset($this->request->file_content_id)) {
0340             $fileContentId = $this->request->file_content_id;
0341         }
0342         if (!empty($this->request->artist_id)) {
0343             $artistId = $this->request->artist_id;
0344         }
0345         if (!empty($this->request->album_id)) {
0346             $albumId = $this->request->album_id;
0347         }
0348         if (!empty($this->request->genre)) {
0349             $genre = $this->request->genre;
0350         }
0351         if (!empty($this->request->search)) {
0352             $search = $this->request->search;
0353         }
0354         if (!empty($this->request->client_id)
0355             && !empty($this->request->favoritesby)
0356         ) {
0357             $favoriteIds = $this->_getFavoriteIds(
0358                 $this->request->client_id,
0359                 $this->request->favoritesby
0360             );
0361             if (!$favoriteIds) {
0362                 $this->response->setStatus(404);
0363                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0364             }
0365         }
0366         if (!empty($this->request->sort)) {
0367             $sort = $this->request->sort;
0368         }
0369         if (!empty($this->request->perpage)
0370             && $this->_isValidPerpageNumber($this->request->perpage)
0371         ) {
0372             $perpage = $this->request->perpage;
0373         }
0374         if (!empty($this->request->page)
0375             && $this->_isValidPageNumber($this->request->page)
0376         ) {
0377             $page = $this->request->page;
0378         }
0379 
0380         $collections = $this->models->media->getCollections(
0381             $clientId,
0382             $ownerId,
0383             $collectionId,
0384             $collectionCategory,
0385             $collectionTags,
0386             $collectionContentId,
0387             $fileId,
0388             $fileTypes,
0389             $fileCategory,
0390             $fileTags,
0391             $fileOcsCompatibility,
0392             $fileContentId,
0393             $artistId,
0394             $albumId,
0395             $genre,
0396             $search,
0397             $favoriteIds,
0398             $sort,
0399             $perpage,
0400             $page
0401         );
0402 
0403         if (!$collections) {
0404             $this->response->setStatus(404);
0405             throw new Flooer_Exception('Not found', LOG_NOTICE);
0406         }
0407 
0408         $this->_setResponseContent('success', $collections);
0409     }
0410 
0411     public function getIndex()
0412     {
0413         $clientId = null;
0414         $ownerId = null;
0415         $collectionId = null;
0416         $collectionCategory = null;
0417         $collectionTags = null; // Comma-separated list
0418         $collectionContentId = null;
0419         $fileId = null;
0420         $fileTypes = null; // Comma-separated list
0421         $fileCategory = null;
0422         $fileTags = null; // Comma-separated list
0423         $fileOcsCompatibility = 'all';
0424         $fileContentId = null;
0425         $artistId = null;
0426         $albumId = null;
0427         $genre = null;
0428         $search = null; // 3 or more strings
0429         $ids = null; // Comma-separated list
0430         $favoriteIds = array();
0431         $playedTimeperiodBegin = null; // Datetime format
0432         $playedTimeperiodEnd = null; // Datetime format
0433         $sort = 'name';
0434         $perpage = $this->appConfig->general['perpage'];
0435         $page = 1;
0436 
0437         if (!empty($this->request->client_id)) {
0438             $clientId = $this->request->client_id;
0439         }
0440         if (!empty($this->request->owner_id)) {
0441             $ownerId = $this->request->owner_id;
0442         }
0443         if (!empty($this->request->collection_id)) {
0444             $collectionId = $this->request->collection_id;
0445         }
0446         if (isset($this->request->collection_category)) {
0447             $collectionCategory = $this->request->collection_category;
0448         }
0449         if (isset($this->request->collection_tags)) {
0450             $collectionTags = $this->request->collection_tags;
0451         }
0452         if (isset($this->request->collection_content_id)) {
0453             $collectionContentId = $this->request->collection_content_id;
0454         }
0455         if (!empty($this->request->file_id)) {
0456             $fileId = $this->request->file_id;
0457         }
0458         if (!empty($this->request->file_types)) {
0459             $fileTypes = $this->request->file_types;
0460         }
0461         if (isset($this->request->file_category)) {
0462             $fileCategory = $this->request->file_category;
0463         }
0464         if (isset($this->request->file_tags)) {
0465             $fileTags = $this->request->file_tags;
0466         }
0467         if (!empty($this->request->file_ocs_compatibility)) {
0468             $fileOcsCompatibility = $this->request->file_ocs_compatibility;
0469         }
0470         if (isset($this->request->file_content_id)) {
0471             $fileContentId = $this->request->file_content_id;
0472         }
0473         if (!empty($this->request->artist_id)) {
0474             $artistId = $this->request->artist_id;
0475         }
0476         if (!empty($this->request->album_id)) {
0477             $albumId = $this->request->album_id;
0478         }
0479         if (!empty($this->request->genre)) {
0480             $genre = $this->request->genre;
0481         }
0482         if (!empty($this->request->search)) {
0483             $search = $this->request->search;
0484         }
0485         if (!empty($this->request->ids)) {
0486             $ids = $this->request->ids;
0487         }
0488         if (!empty($this->request->client_id)
0489             && !empty($this->request->favoritesby)
0490         ) {
0491             $favoriteIds = $this->_getFavoriteIds(
0492                 $this->request->client_id,
0493                 $this->request->favoritesby
0494             );
0495             if (!$favoriteIds) {
0496                 $this->response->setStatus(404);
0497                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0498             }
0499         }
0500         if (!empty($this->request->played_timeperiod_begin)) {
0501             $playedTimeperiodBegin = $this->request->played_timeperiod_begin;
0502         }
0503         if (!empty($this->request->played_timeperiod_end)) {
0504             $playedTimeperiodEnd = $this->request->played_timeperiod_end;
0505         }
0506         if (!empty($this->request->sort)) {
0507             $sort = $this->request->sort;
0508         }
0509         if (!empty($this->request->perpage)
0510             && $this->_isValidPerpageNumber($this->request->perpage)
0511         ) {
0512             $perpage = $this->request->perpage;
0513         }
0514         if (!empty($this->request->page)
0515             && $this->_isValidPageNumber($this->request->page)
0516         ) {
0517             $page = $this->request->page;
0518         }
0519 
0520         $index = $this->models->media->getIndex(
0521             $clientId,
0522             $ownerId,
0523             $collectionId,
0524             $collectionCategory,
0525             $collectionTags,
0526             $collectionContentId,
0527             $fileId,
0528             $fileTypes,
0529             $fileCategory,
0530             $fileTags,
0531             $fileOcsCompatibility,
0532             $fileContentId,
0533             $artistId,
0534             $albumId,
0535             $genre,
0536             $search,
0537             $ids,
0538             $favoriteIds,
0539             $playedTimeperiodBegin,
0540             $playedTimeperiodEnd,
0541             $sort,
0542             $perpage,
0543             $page
0544         );
0545 
0546         if (!$index) {
0547             $this->response->setStatus(404);
0548             throw new Flooer_Exception('Not found', LOG_NOTICE);
0549         }
0550 
0551         $this->_setResponseContent('success', $index);
0552     }
0553 
0554     public function getMedia()
0555     {
0556         $id = null;
0557 
0558         if (!empty($this->request->id)) {
0559             $id = $this->request->id;
0560         }
0561 
0562         $media = $this->models->media->getMedia($id);
0563 
0564         if (!$media) {
0565             $this->response->setStatus(404);
0566             throw new Flooer_Exception('Not found', LOG_NOTICE);
0567         }
0568 
0569         $this->_setResponseContent(
0570             'success',
0571             array('media' => $media)
0572         );
0573     }
0574 
0575     public function headStream()
0576     {
0577         $this->getStream(true);
0578     }
0579 
0580     public function getStream($headeronly = false)
0581     {
0582         $id = null;
0583         $userId = null;
0584 
0585         if (!empty($this->request->id)) {
0586             $id = $this->request->id;
0587         }
0588         // Disabled for now
0589         //if (!empty($this->request->u)) {
0590         //    $userId = $this->request->u;
0591         //}
0592 
0593         $media = $this->models->media->$id;
0594 
0595         if (!$media) {
0596             $this->response->setStatus(404);
0597             throw new Flooer_Exception('Not found', LOG_NOTICE);
0598         }
0599 
0600         $collection = $this->models->collections->{$media->collection_id};
0601         $file = $this->models->files->{$media->file_id};
0602 
0603         if (!$headeronly && $media->played_ip != $this->server->REMOTE_ADDR) {
0604             $this->models->media->updatePlayedStatus($media->id);
0605 
0606             $playedId = $this->models->media_played->generateId();
0607             $this->models->media_played->$playedId = array(
0608                 'client_id' => $media->client_id,
0609                 'owner_id' => $media->owner_id,
0610                 'collection_id' => $media->collection_id,
0611                 'file_id' => $media->file_id,
0612                 'media_id' => $media->id,
0613                 'user_id' => $userId
0614             );
0615 
0616         }
0617 
0618         $this->_sendFile(
0619             $this->appConfig->general['filesDir'] . '/' . $collection->name . '/' . $file->name,
0620             $file->name,
0621             $file->type,
0622             $file->size,
0623             false,
0624             $headeronly
0625         );
0626     }
0627 
0628     public function headCollectionthumbnail()
0629     {
0630         $this->getCollectionthumbnail(true);
0631     }
0632 
0633     public function getCollectionthumbnail($headeronly = false)
0634     {
0635         $filepath = $this->appConfig->general['thumbnailsDir'] . '/collection_default.jpg';
0636         if (isset($this->request->id)
0637             && is_file($this->appConfig->general['thumbnailsDir'] . '/collection_' . $this->request->id . '.jpg')
0638         ) {
0639             $filepath = $this->appConfig->general['thumbnailsDir'] . '/collection_' . $this->request->id . '.jpg';
0640         }
0641 
0642         $this->_sendFile(
0643             $filepath,
0644             basename($filepath),
0645             'image/jpeg',
0646             filesize($filepath),
0647             false,
0648             $headeronly
0649         );
0650     }
0651 
0652     public function postCollectionthumbnail()
0653     {
0654         if (!$this->_isAllowedAccess()) {
0655             $this->response->setStatus(403);
0656             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0657         }
0658 
0659         $id = null;
0660 
0661         if (!empty($this->request->id)) {
0662             $id = $this->request->id;
0663         }
0664 
0665         $collection = $this->models->collections->$id;
0666 
0667         if (!$collection) {
0668             $this->response->setStatus(404);
0669             throw new Flooer_Exception('Not found', LOG_NOTICE);
0670         }
0671         else if ($collection->client_id != $this->request->client_id) {
0672             $this->response->setStatus(403);
0673             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0674         }
0675 
0676         $errors = array();
0677         if (!isset($_FILES['file'])) {
0678             $errors['file'] = 'Required';
0679         }
0680         if (!empty($_FILES['file']['error'])) { // 0 = UPLOAD_ERR_OK
0681             $errors['file'] = $_FILES['file']['error'];
0682         }
0683         if (!empty($_FILES['file']['tmp_name'])) {
0684             $finfo = new finfo(FILEINFO_MIME_TYPE);
0685             $type = $finfo->file($_FILES['file']['tmp_name']);
0686             if (strpos($type, 'image/jpeg') === false
0687                 && strpos($type, 'image/png') === false
0688             ) {
0689                 $errors['file'] = 'Must upload JPEG or PNG image';
0690             }
0691         }
0692 
0693         if ($errors) {
0694             $this->response->setStatus(400);
0695             $this->_setResponseContent(
0696                 'error',
0697                 array(
0698                     'message' => 'Validation error',
0699                     'errors' => $errors
0700                 )
0701             );
0702             return;
0703         }
0704 
0705         $image = imagecreatefromstring(
0706             file_get_contents($_FILES['file']['tmp_name'])
0707         );
0708 
0709         if ($image !== false) {
0710             imagejpeg($image, $this->appConfig->general['thumbnailsDir'] . '/collection_' . $id . '.jpg', 75);
0711             imagedestroy($image);
0712         }
0713 
0714         $this->_setResponseContent('success');
0715     }
0716 
0717     public function headAlbumthumbnail()
0718     {
0719         $this->getAlbumthumbnail(true);
0720     }
0721 
0722     public function getAlbumthumbnail($headeronly = false)
0723     {
0724         $filepath = $this->appConfig->general['thumbnailsDir'] . '/album_default.jpg';
0725         if (isset($this->request->id)
0726             && is_file($this->appConfig->general['thumbnailsDir'] . '/album_' . $this->request->id . '.jpg')
0727         ) {
0728             $filepath = $this->appConfig->general['thumbnailsDir'] . '/album_' . $this->request->id . '.jpg';
0729         }
0730 
0731         $this->_sendFile(
0732             $filepath,
0733             basename($filepath),
0734             'image/jpeg',
0735             filesize($filepath),
0736             false,
0737             $headeronly
0738         );
0739     }
0740 
0741     public function postAlbumthumbnail()
0742     {
0743         if (!$this->_isAllowedAccess()) {
0744             $this->response->setStatus(403);
0745             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0746         }
0747 
0748         $id = null;
0749 
0750         if (!empty($this->request->id)) {
0751             $id = $this->request->id;
0752         }
0753 
0754         $mediaAlbum = $this->models->media_albums->$id;
0755 
0756         if (!$mediaAlbum) {
0757             $this->response->setStatus(404);
0758             throw new Flooer_Exception('Not found', LOG_NOTICE);
0759         }
0760         else if ($mediaAlbum->client_id != $this->request->client_id) {
0761             $this->response->setStatus(403);
0762             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0763         }
0764 
0765         $errors = array();
0766         if (!isset($_FILES['file'])) {
0767             $errors['file'] = 'Required';
0768         }
0769         if (!empty($_FILES['file']['error'])) { // 0 = UPLOAD_ERR_OK
0770             $errors['file'] = $_FILES['file']['error'];
0771         }
0772         if (!empty($_FILES['file']['tmp_name'])) {
0773             $finfo = new finfo(FILEINFO_MIME_TYPE);
0774             $type = $finfo->file($_FILES['file']['tmp_name']);
0775             if (strpos($type, 'image/jpeg') === false
0776                 && strpos($type, 'image/png') === false
0777             ) {
0778                 $errors['file'] = 'Must upload JPEG or PNG image';
0779             }
0780         }
0781 
0782         if ($errors) {
0783             $this->response->setStatus(400);
0784             $this->_setResponseContent(
0785                 'error',
0786                 array(
0787                     'message' => 'Validation error',
0788                     'errors' => $errors
0789                 )
0790             );
0791             return;
0792         }
0793 
0794         $image = imagecreatefromstring(
0795             file_get_contents($_FILES['file']['tmp_name'])
0796         );
0797 
0798         if ($image !== false) {
0799             imagejpeg($image, $this->appConfig->general['thumbnailsDir'] . '/album_' . $id . '.jpg', 75);
0800             imagedestroy($image);
0801         }
0802 
0803         $this->_setResponseContent('success');
0804     }
0805 
0806 }