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

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 Profiles extends BaseController
0025 {
0026 
0027     public function getIndex()
0028     {
0029         $status = 'active';
0030         $clientId = null;
0031         $ownerId = null;
0032         $search = null; // 3 or more strings
0033         $ids = null; // Comma-separated list
0034         $favoriteIds = array();
0035         $sort = 'name';
0036         $perpage = $this->appConfig->general['perpage'];
0037         $page = 1;
0038 
0039         if (!empty($this->request->status)) {
0040             $status = $this->request->status;
0041         }
0042         if (!empty($this->request->client_id)) {
0043             $clientId = $this->request->client_id;
0044         }
0045         if (!empty($this->request->owner_id)) {
0046             $ownerId = $this->request->owner_id;
0047         }
0048         if (!empty($this->request->search)) {
0049             $search = $this->request->search;
0050         }
0051         if (!empty($this->request->ids)) {
0052             $ids = $this->request->ids;
0053         }
0054         if (!empty($this->request->client_id)
0055             && !empty($this->request->favoritesby)
0056         ) {
0057             $favoriteIds = $this->_getFavoriteIds(
0058                 $this->request->client_id,
0059                 $this->request->favoritesby
0060             );
0061             if (!$favoriteIds) {
0062                 $this->response->setStatus(404);
0063                 throw new Flooer_Exception('Not found', LOG_NOTICE);
0064             }
0065         }
0066         if (!empty($this->request->sort)) {
0067             $sort = $this->request->sort;
0068         }
0069         if (!empty($this->request->perpage)
0070             && $this->_isValidPerpageNumber($this->request->perpage)
0071         ) {
0072             $perpage = $this->request->perpage;
0073         }
0074         if (!empty($this->request->page)
0075             && $this->_isValidPageNumber($this->request->page)
0076         ) {
0077             $page = $this->request->page;
0078         }
0079 
0080         $profiles = $this->models->profiles->getProfiles(
0081             $status,
0082             $clientId,
0083             $ownerId,
0084             $search,
0085             $ids,
0086             $favoriteIds,
0087             $sort,
0088             $perpage,
0089             $page
0090         );
0091 
0092         if (!$profiles) {
0093             $this->response->setStatus(404);
0094             throw new Flooer_Exception('Not found', LOG_NOTICE);
0095         }
0096 
0097         $this->_setResponseContent('success', $profiles);
0098     }
0099 
0100     public function getProfile()
0101     {
0102         $id = null;
0103 
0104         if (!empty($this->request->id)) {
0105             $id = $this->request->id;
0106         }
0107 
0108         $profile = $this->models->profiles->getProfile($id);
0109 
0110         if (!$profile) {
0111             $this->response->setStatus(404);
0112             throw new Flooer_Exception('Not found', LOG_NOTICE);
0113         }
0114 
0115         $this->_setResponseContent(
0116             'success',
0117             array('profile' => $profile)
0118         );
0119     }
0120 
0121     public function postProfile()
0122     {
0123         // Update profile or add new one
0124 
0125         if (!$this->_isAllowedAccess()) {
0126             $this->response->setStatus(403);
0127             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0128         }
0129 
0130         $id = null; // Auto generated
0131         $active = 1;
0132         $clientId = null;
0133         $ownerId = null;
0134         $name = null;
0135         $email = null;
0136         $homepage = null;
0137         $image = null;
0138         $description = null;
0139 
0140         if (!empty($this->request->client_id)) {
0141             $clientId = $this->request->client_id;
0142         }
0143         if (!empty($this->request->owner_id)) {
0144             $ownerId = $this->request->owner_id;
0145         }
0146         if (!empty($this->request->name)) {
0147             $name = mb_substr(strip_tags($this->request->name), 0, 255);
0148         }
0149         if (!empty($this->request->email)) {
0150             $email = $this->request->email;
0151         }
0152         if (!empty($this->request->homepage)) {
0153             $homepage = $this->request->homepage;
0154         }
0155         if (!empty($this->request->image)) {
0156             $image = $this->request->image;
0157         }
0158         if (isset($this->request->description)) {
0159             $description = strip_tags($this->request->description);
0160         }
0161 
0162         $errors = array();
0163         if (!$clientId) {
0164             $errors['client_id'] = 'Required';
0165         }
0166         if (!$ownerId) {
0167             $errors['owner_id'] = 'Required';
0168         }
0169         if (!$name) {
0170             $errors['name'] = 'Required';
0171         }
0172         if ($email && !$this->_isValidEmail($email)) {
0173             $errors['email'] = 'Invalid';
0174         }
0175         if ($homepage && !$this->_isValidUri($homepage)) {
0176             $errors['homepage'] = 'Invalid';
0177         }
0178         if ($image && !$this->_isValidUri($image)) {
0179             $errors['image'] = 'Invalid';
0180         }
0181 
0182         if ($errors) {
0183             $this->response->setStatus(400);
0184             $this->_setResponseContent(
0185                 'error',
0186                 array(
0187                     'message' => 'Validation error',
0188                     'errors' => $errors
0189                 )
0190             );
0191             return;
0192         }
0193 
0194         $profile = $this->models->profiles->getProfileByClientIdAndOwnerId($clientId, $ownerId);
0195 
0196         if ($profile) {
0197             if ($profile->active) {
0198                 $id = $profile->id;
0199             }
0200             else {
0201                 $this->response->setStatus(403);
0202                 throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0203             }
0204         }
0205         else {
0206             $id = $this->models->profiles->generateId();
0207         }
0208 
0209         $this->models->profiles->$id = array(
0210             'active' => $active,
0211             'client_id' => $clientId,
0212             'owner_id' => $ownerId,
0213             'name' => $name,
0214             'email' => $email,
0215             'homepage' => $homepage,
0216             'image' => $image,
0217             'description' => $description
0218         );
0219 
0220         $profile = $this->models->profiles->getProfile($id);
0221 
0222         $this->_setResponseContent(
0223             'success',
0224             array('profile' => $profile)
0225         );
0226     }
0227 
0228     public function putProfile()
0229     {
0230         if (!$this->_isAllowedAccess()) {
0231             $this->response->setStatus(403);
0232             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0233         }
0234 
0235         $id = null;
0236         $name = null;
0237         $email = null;
0238         $homepage = null;
0239         $image = null;
0240         $description = null;
0241 
0242         if (!empty($this->request->id)) {
0243             $id = $this->request->id;
0244         }
0245         if (!empty($this->request->name)) {
0246             $name = mb_substr(strip_tags($this->request->name), 0, 255);
0247         }
0248         if (!empty($this->request->email)) {
0249             $email = $this->request->email;
0250         }
0251         if (!empty($this->request->homepage)) {
0252             $homepage = $this->request->homepage;
0253         }
0254         if (!empty($this->request->image)) {
0255             $image = $this->request->image;
0256         }
0257         if (isset($this->request->description)) {
0258             $description = strip_tags($this->request->description);
0259         }
0260 
0261         $profile = $this->models->profiles->$id;
0262 
0263         if (!$profile) {
0264             $this->response->setStatus(404);
0265             throw new Flooer_Exception('Not found', LOG_NOTICE);
0266         }
0267         else if (!$profile->active || $profile->client_id != $this->request->client_id) {
0268             $this->response->setStatus(403);
0269             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0270         }
0271 
0272         $updata = array();
0273         if ($name !== null) {
0274             $updata['name'] = $name;
0275         }
0276         if ($email !== null && $this->_isValidEmail($email)) {
0277             $updata['email'] = $email;
0278         }
0279         if ($homepage !== null && $this->_isValidUri($homepage)) {
0280             $updata['homepage'] = $homepage;
0281         }
0282         if ($image !== null && $this->_isValidUri($image)) {
0283             $updata['image'] = $image;
0284         }
0285         if ($description !== null) {
0286             $updata['description'] = $description;
0287         }
0288 
0289         $this->models->profiles->$id = $updata;
0290 
0291         $profile = $this->models->profiles->getProfile($id);
0292 
0293         $this->_setResponseContent(
0294             'success',
0295             array('profile' => $profile)
0296         );
0297     }
0298 
0299     public function deleteProfile()
0300     {
0301         // Please be care the remove process in Owners::deleteOwner()
0302 
0303         if (!$this->_isAllowedAccess()) {
0304             $this->response->setStatus(403);
0305             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0306         }
0307 
0308         $id = null;
0309 
0310         if (!empty($this->request->id)) {
0311             $id = $this->request->id;
0312         }
0313 
0314         $profile = $this->models->profiles->$id;
0315 
0316         if (!$profile) {
0317             $this->response->setStatus(404);
0318             throw new Flooer_Exception('Not found', LOG_NOTICE);
0319         }
0320         else if (!$profile->active || $profile->client_id != $this->request->client_id) {
0321             $this->response->setStatus(403);
0322             throw new Flooer_Exception('Forbidden', LOG_NOTICE);
0323         }
0324 
0325         $this->models->profiles->$id = array('active' => 0);
0326 
0327         $this->_setResponseContent('success');
0328     }
0329 
0330 }