File indexing completed on 2024-04-21 05:54:20

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  * Created: 26.01.2017
0024  */
0025 class Application_Model_PpLoad
0026 {
0027     /**
0028      * @inheritDoc
0029      */
0030     public function __construct()
0031     {
0032     }
0033 
0034     public static function createDownloadUrl($collection_id, $file_name, array $params)
0035     {
0036         $valid_until = time() + 3600; // one hour valid
0037         $hash = self::createDownloadHash($collection_id, $valid_until);
0038         $url = PPLOAD_API_URI . 'files/download';
0039         foreach ($params as $key => $param) {
0040             $url .= '/' . $key . '/' . $param;
0041         }
0042 
0043         return $url . '/s/' . $hash . '/t/' . $valid_until . '/' . $file_name;
0044     }
0045 
0046     /**
0047      * @param int $collection_id
0048      * @param int $valid_until
0049      * @return string
0050      */
0051     public static function createDownloadHash($collection_id, $valid_until)
0052     {
0053         return hash('sha512',
0054             PPLOAD_DOWNLOAD_SECRET . $collection_id . $valid_until); // order isn't important at all... just do the same when verifying
0055     }
0056 
0057     /**
0058      * @param int    $collection_id
0059      * @param string $file_name
0060      * @param array  $payload
0061      * @return string
0062      * @throws Zend_Exception
0063      */
0064     public static function createDownloadUrlJwt($collection_id, $file_name, array $payload)
0065     {
0066         $valid_until = time() + 3600; // one hour valid
0067         $hash = self::createDownloadHash($collection_id, $valid_until);
0068         $payload['s'] = $hash;
0069         $payload['t'] = $valid_until;
0070         try {
0071             $payload['stfp'] = null;
0072             $requestIp = Zend_Controller_Front::getInstance()->getRequest()->getClientIp();
0073             $payload['stip'] = $requestIp;
0074         } catch (Zend_Session_Exception $e) {
0075             Zend_Registry::get('logger')->err(__METHOD__ . '   ' . $e->getMessage());
0076 //            error_log(__METHOD__ . '   ' . $e->getMessage());
0077         }
0078         $jwt = Application_Model_Jwt::encodeFromArray($payload);
0079 
0080         return PPLOAD_API_URI . 'files/download/j/' . $jwt . '/' . $file_name;
0081     }
0082 
0083     /**
0084      * @param int    $projectId
0085      * @param string $url
0086      * @param string $filename
0087      * @param string $fileDescription
0088      * @return bool|mixed
0089      * @throws Zend_Auth_Storage_Exception
0090      * @throws Zend_Exception
0091      */
0092     public function uploadEmptyFileWithLink($projectId, $url, $filename, $fileDescription)
0093     {
0094         $projectId = (int)$projectId;
0095 
0096         $projectData = $this->getProjectData($projectId);
0097 
0098         if (empty($projectData)) {
0099             Zend_Registry::get('logger')->err(__METHOD__ . ' - ppload upload error. no project data found. project_id:'
0100                                               . $projectId);
0101 
0102             return false;
0103         }
0104 
0105         $pploadApi = $this->getPpLoadApi();
0106 
0107         // create empty text file
0108         $fileDummy = '../../data/files/empty';
0109 
0110         $fileRequest = array(
0111             //            'file' => $fileDummy,
0112             'local_file_path' => $fileDummy,
0113             'local_file_name' => $filename,
0114             'owner_id'        => $projectData->member_id,
0115             'tags'            => 'link##' . urlencode($url)
0116         );
0117 
0118         if ($projectData->ppload_collection_id) {
0119             // Append to existing collection
0120             $fileRequest['collection_id'] = $projectData->ppload_collection_id;
0121         }
0122         if (false == empty($fileDescription)) {
0123             $fileRequest['description'] = mb_substr($fileDescription, 0, 140);
0124         }
0125 
0126         //upload to ppload
0127         $fileResponse = $pploadApi->postFile($fileRequest);
0128 
0129         Zend_Registry::get('logger')->debug(__METHOD__ . ' - fileResponse: ' . print_r($fileResponse, true));
0130 
0131         if (empty($fileResponse) OR empty($fileResponse->file) OR $fileResponse->status <> 'success') {
0132             Zend_Registry::get('logger')->err(__METHOD__
0133                                               . ' - ppload upload error. requestData:'
0134                                               . print_r($fileRequest, true) . "\n" . 'response:'
0135                                               . print_r($fileResponse, true)
0136             );
0137 
0138             return false;
0139         }
0140         $log = Zend_Registry::get('logger');
0141         if ($projectData->ppload_collection_id <> $fileResponse->file->collection_id) {
0142             $projectData->ppload_collection_id = $fileResponse->file->collection_id;
0143             if ($this->isAuthmemberProjectCreator($projectData->member_id)) {
0144                 $projectData->changed_at = new Zend_Db_Expr('NOW()');
0145             } else {
0146                 $auth = Zend_Auth::getInstance();
0147                 $authMember = $auth->getStorage()->read();
0148                 $log->info('********** ' . __METHOD__ . ' Project ChangedAt is not set: Auth-Member (' . $authMember->member_id . ') != Project-Owner (' . $projectData->member_id . '): **********' . "\n");
0149             }
0150             $projectData->save();
0151         } else {
0152             if ($this->isAuthmemberProjectCreator($projectData->member_id)) {
0153                 $projectData->changed_at = new Zend_Db_Expr('NOW()');
0154                 $projectData->save();
0155             } else {
0156                 $auth = Zend_Auth::getInstance();
0157                 $authMember = $auth->getStorage()->read();
0158                 $log->info('********** ' . __METHOD__ . ' Project ChangedAt is not set: Auth-Member (' . $authMember->member_id . ') != Project-Owner (' . $projectData->member_id . '): **********' . "\n");
0159             }
0160         }
0161 
0162         return $fileResponse;
0163     }
0164 
0165     /**
0166      * @param int $projectId
0167      *
0168      * @return Zend_Db_Table_Row_Abstract
0169      * @throws Zend_Db_Table_Exception
0170      */
0171     protected function getProjectData($projectId)
0172     {
0173         $projectTable = new Default_Model_DbTable_Project();
0174 
0175         return $projectTable->find($projectId)->current();
0176     }
0177 
0178     /**
0179      * @return Ppload_Api
0180      */
0181     protected function getPpLoadApi()
0182     {
0183         return new Ppload_Api(array(
0184             'apiUri'   => PPLOAD_API_URI,
0185             'clientId' => PPLOAD_CLIENT_ID,
0186             'secret'   => PPLOAD_SECRET
0187         ));
0188     }
0189 
0190     /**
0191      * @param int $creator_id
0192      * @return bool
0193      * @throws Zend_Auth_Storage_Exception
0194      */
0195     public function isAuthmemberProjectCreator($creator_id)
0196     {
0197         $auth = Zend_Auth::getInstance();
0198         $authMember = $auth->getStorage()->read();
0199         if ($authMember->member_id == $creator_id) {
0200             return true;
0201         }
0202 
0203         return false;
0204     }
0205 
0206 }