File indexing completed on 2024-04-28 05:58:57

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 
0023 // ppload-API: https://github.com/KDE/ocs-fileserver/blob/master/docs/ocs-fileserver-API.md
0024 
0025 class Ppload_Api
0026 {
0027 
0028     protected $_config = array(
0029         'apiUri' => 'https://www.ppload.com/api/',
0030         'clientId' => '',
0031         'secret' => ''
0032     );
0033 
0034     public function __construct(array $config = null)
0035     {
0036         if ($config) {
0037             $this->_config = $config + $this->_config;
0038         }
0039     }
0040 
0041     public function getProfiles(array $params = null)
0042     {
0043         return $this->_request('GET', 'profiles/index', $params);
0044     }
0045 
0046     public function getProfile($id)
0047     {
0048         return $this->_request('GET', 'profiles/profile', array(
0049             'id' => $id
0050         ));
0051     }
0052 
0053     public function postProfile(array $params)
0054     {
0055         return $this->_request('POST', 'profiles/profile', array(
0056             'client_id' => $this->_config['clientId'],
0057             'secret' => $this->_config['secret']
0058         ) + $params);
0059     }
0060 
0061     public function putProfile($id, array $params)
0062     {
0063         return $this->_request('PUT', 'profiles/profile', array(
0064             'id' => $id,
0065             'client_id' => $this->_config['clientId'],
0066             'secret' => $this->_config['secret']
0067         ) + $params);
0068     }
0069 
0070     public function deleteProfile($id)
0071     {
0072         return $this->_request('DELETE', 'profiles/profile', array(
0073             'id' => $id,
0074             'client_id' => $this->_config['clientId'],
0075             'secret' => $this->_config['secret']
0076         ));
0077     }
0078 
0079     public function getCollections(array $params = null)
0080     {
0081         return $this->_request('GET', 'collections/index', $params);
0082     }
0083 
0084     public function getCollection($id)
0085     {
0086         return $this->_request('GET', 'collections/collection', array(
0087             'id' => $id
0088         ));
0089     }
0090 
0091     public function postCollection(array $params)
0092     {
0093         return $this->_request('POST', 'collections/collection', array(
0094             'client_id' => $this->_config['clientId'],
0095             'secret' => $this->_config['secret']
0096         ) + $params);
0097     }
0098 
0099     public function putCollection($id, array $params)
0100     {
0101         return $this->_request('PUT', 'collections/collection', array(
0102             'id' => $id,
0103             'client_id' => $this->_config['clientId'],
0104             'secret' => $this->_config['secret']
0105         ) + $params);
0106     }
0107 
0108     public function deleteCollection($id)
0109     {
0110         return $this->_request('DELETE', 'collections/collection', array(
0111             'id' => $id,
0112             'client_id' => $this->_config['clientId'],
0113             'secret' => $this->_config['secret']
0114         ));
0115     }
0116 
0117     public function getFiles(array $params = null)
0118     {
0119         return $this->_request('GET', 'files/index', $params);
0120     }
0121 
0122     public function getFile($id)
0123     {
0124         return $this->_request('GET', 'files/file', array(
0125             'id' => $id
0126         ));
0127     }
0128 
0129     public function postFile(array $params)
0130     {
0131         return $this->_request('POST', 'files/file', array(
0132             'client_id' => $this->_config['clientId'],
0133             'secret' => $this->_config['secret']
0134         ) + $params);
0135     }
0136 
0137     public function putFile($id, array $params)
0138     {
0139         return $this->_request('PUT', 'files/file', array(
0140             'id' => $id,
0141             'client_id' => $this->_config['clientId'],
0142             'secret' => $this->_config['secret']
0143         ) + $params);
0144     }
0145 
0146     public function deleteFile($id)
0147     {
0148         return $this->_request('DELETE', 'files/file', array(
0149             'id' => $id,
0150             'client_id' => $this->_config['clientId'],
0151             'secret' => $this->_config['secret']
0152         ));
0153     }
0154 
0155     public function getFavorites(array $params = null)
0156     {
0157         return $this->_request('GET', 'favorites/index', $params);
0158     }
0159 
0160     public function getFavorite($id)
0161     {
0162         return $this->_request('GET', 'favorites/favorite', array(
0163             'id' => $id
0164         ));
0165     }
0166 
0167     public function postFavorite(array $params)
0168     {
0169         return $this->_request('POST', 'favorites/favorite', array(
0170             'client_id' => $this->_config['clientId'],
0171             'secret' => $this->_config['secret']
0172         ) + $params);
0173     }
0174 
0175     public function deleteFavorite($id)
0176     {
0177         return $this->_request('DELETE', 'favorites/favorite', array(
0178             'id' => $id,
0179             'client_id' => $this->_config['clientId'],
0180             'secret' => $this->_config['secret']
0181         ));
0182     }
0183 
0184     public function deleteOwner($id)
0185     {
0186         return $this->_request('DELETE', 'owners/owner', array(
0187             'id' => $id,
0188             'client_id' => $this->_config['clientId'],
0189             'secret' => $this->_config['secret']
0190         ));
0191     }
0192 
0193     public function getMediaGenres(array $params = null)
0194     {
0195         return $this->_request('GET', 'media/genres', $params);
0196     }
0197 
0198     public function getMediaOwners(array $params = null)
0199     {
0200         return $this->_request('GET', 'media/owners', $params);
0201     }
0202 
0203     public function getMediaCollections(array $params = null)
0204     {
0205         return $this->_request('GET', 'media/collections', $params);
0206     }
0207 
0208     public function getMediaIndex(array $params = null)
0209     {
0210         return $this->_request('GET', 'media/index', $params);
0211     }
0212 
0213     public function getMedia($id)
0214     {
0215         return $this->_request('GET', 'media/media', array(
0216             'id' => $id
0217         ));
0218     }
0219 
0220     public function postMediaCollectionthumbnail($id, array $params)
0221     {
0222         return $this->_request('POST', 'media/collectionthumbnail', array(
0223             'id' => $id,
0224             'client_id' => $this->_config['clientId'],
0225             'secret' => $this->_config['secret']
0226         ) + $params);
0227     }
0228 
0229     public function postMediaAlbumthumbnail($id, array $params)
0230     {
0231         return $this->_request('POST', 'media/albumthumbnail', array(
0232             'id' => $id,
0233             'client_id' => $this->_config['clientId'],
0234             'secret' => $this->_config['secret']
0235         ) + $params);
0236     }
0237 
0238     protected function _request($method, $uri = '', array $params = null)
0239     {
0240         if (empty($this->_config['apiUri'])) {
0241             return false;
0242         }
0243 
0244         $timeout = 60;
0245         $postFields = array(
0246             'method' => $method,
0247             'format' => 'json'
0248         );
0249         if ($params) {
0250             $postFields = $postFields + $params;
0251         }
0252         if (isset($postFields['file'])) {
0253             $timeout = 600;
0254             if ($postFields['file'][0] != '@') {
0255                 $postFields['file'] = $this->_getCurlValue($postFields['file']);
0256             }
0257         }
0258         else {
0259             $postFields = http_build_query($postFields, '', '&');
0260         }
0261 
0262         $curl = curl_init();
0263         curl_setopt_array($curl, array(
0264             CURLOPT_URL => $this->_config['apiUri'] . ltrim($uri, '/'), //. '?XDEBUG_SESSION_START=files',
0265             CURLOPT_HEADER => false,
0266             CURLOPT_POST => true,
0267             CURLOPT_POSTFIELDS => $postFields,
0268             CURLOPT_RETURNTRANSFER => true,
0269             CURLOPT_TIMEOUT => $timeout
0270         ));
0271         $response = curl_exec($curl);
0272         $last_error = curl_error($curl);
0273         curl_close($curl);
0274 
0275         if ($last_error) {
0276             error_log(__METHOD__ . ' :: ' . $last_error);
0277         }
0278         if ($response) {
0279             return json_decode($response);
0280         }
0281         return false;
0282     }
0283 
0284     /**
0285      * @param string $filename
0286      * @return CURLFile|string
0287      */
0288     private function _getCurlValue($filename)
0289     {
0290         // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
0291         // See: https://wiki.php.net/rfc/curl-file-upload
0292         if (function_exists('curl_file_create')) {
0293             return curl_file_create($filename);
0294         }
0295 
0296         return "@{$filename}";
0297     }
0298 
0299 }