File indexing completed on 2025-05-04 05:29:13
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 Default_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 */ 0063 public static function createDownloadUrlJwt($collection_id, $file_name, array $payload) 0064 { 0065 $valid_until = time() + 3600; // one hour valid 0066 $hash = self::createDownloadHash($collection_id, $valid_until); 0067 $payload['s'] = $hash; 0068 $payload['t'] = $valid_until; 0069 try { 0070 $session = new Zend_Session_Namespace(); 0071 $payload['stfp'] = $session->stat_fp; 0072 $payload['stip'] = $session->stat_ipv6 ? $session->stat_ipv6 : $session->stat_ipv4; 0073 } catch (Zend_Session_Exception $e) { 0074 Zend_Registry::get('logger')->err(__METHOD__ . ' ' . $e->getMessage()); 0075 // error_log(__METHOD__ . ' ' . $e->getMessage()); 0076 } 0077 $jwt = Default_Model_Jwt::encodeFromArray($payload); 0078 0079 return PPLOAD_API_URI . 'files/download/j/' . $jwt . '/' . $file_name; 0080 } 0081 0082 /** 0083 * @param int $projectId 0084 * @param string $url 0085 * @param string $filename 0086 * @param string $fileDescription 0087 * @return bool|mixed 0088 * @throws Zend_Auth_Storage_Exception 0089 * @throws Zend_Exception 0090 */ 0091 public function uploadEmptyFileWithLink($projectId, $url, $filename, $fileDescription) 0092 { 0093 $projectId = (int)$projectId; 0094 0095 $projectData = $this->getProjectData($projectId); 0096 0097 if (empty($projectData)) { 0098 Zend_Registry::get('logger')->err(__METHOD__ . ' - ppload upload error. no project data found. project_id:' 0099 . $projectId); 0100 0101 return false; 0102 } 0103 0104 $pploadApi = $this->getPpLoadApi(); 0105 0106 // create empty text file 0107 $fileDummy = '../../data/files/empty'; 0108 0109 $fileRequest = array( 0110 // 'file' => $fileDummy, 0111 'local_file_path' => $fileDummy, 0112 'local_file_name' => $filename, 0113 'owner_id' => $projectData->member_id, 0114 'tags' => 'link##' . urlencode($url) 0115 ); 0116 0117 if ($projectData->ppload_collection_id) { 0118 // Append to existing collection 0119 $fileRequest['collection_id'] = $projectData->ppload_collection_id; 0120 } 0121 if (false == empty($fileDescription)) { 0122 $fileRequest['description'] = mb_substr($fileDescription, 0, 140); 0123 } 0124 0125 //upload to ppload 0126 $fileResponse = $pploadApi->postFile($fileRequest); 0127 0128 Zend_Registry::get('logger')->debug(__METHOD__ . ' - fileResponse: ' . print_r($fileResponse, true)); 0129 0130 if (empty($fileResponse) OR empty($fileResponse->file) OR $fileResponse->status <> 'success') { 0131 Zend_Registry::get('logger')->err(__METHOD__ 0132 . ' - ppload upload error. requestData:' 0133 . print_r($fileRequest, true) . "\n" . 'response:' 0134 . print_r($fileResponse, true) 0135 ); 0136 0137 return false; 0138 } 0139 $log = Zend_Registry::get('logger'); 0140 if ($projectData->ppload_collection_id <> $fileResponse->file->collection_id) { 0141 $projectData->ppload_collection_id = $fileResponse->file->collection_id; 0142 if ($this->isAuthmemberProjectCreator($projectData->member_id)) { 0143 $projectData->changed_at = new Zend_Db_Expr('NOW()'); 0144 } else { 0145 $auth = Zend_Auth::getInstance(); 0146 $authMember = $auth->getStorage()->read(); 0147 $log->info('********** ' . __METHOD__ . ' Project ChangedAt is not set: Auth-Member (' . $authMember->member_id . ') != Project-Owner (' . $projectData->member_id . '): **********' . "\n"); 0148 } 0149 $projectData->save(); 0150 } else { 0151 if ($this->isAuthmemberProjectCreator($projectData->member_id)) { 0152 $projectData->changed_at = new Zend_Db_Expr('NOW()'); 0153 $projectData->save(); 0154 } else { 0155 $auth = Zend_Auth::getInstance(); 0156 $authMember = $auth->getStorage()->read(); 0157 $log->info('********** ' . __METHOD__ . ' Project ChangedAt is not set: Auth-Member (' . $authMember->member_id . ') != Project-Owner (' . $projectData->member_id . '): **********' . "\n"); 0158 } 0159 } 0160 0161 return $fileResponse; 0162 } 0163 0164 /** 0165 * @param int $projectId 0166 * 0167 * @return Zend_Db_Table_Row_Abstract 0168 * @throws Zend_Db_Table_Exception 0169 */ 0170 protected function getProjectData($projectId) 0171 { 0172 $projectTable = new Default_Model_DbTable_Project(); 0173 0174 return $projectTable->find($projectId)->current(); 0175 } 0176 0177 /** 0178 * @return Ppload_Api 0179 */ 0180 protected function getPpLoadApi() 0181 { 0182 return new Ppload_Api(array( 0183 'apiUri' => PPLOAD_API_URI, 0184 'clientId' => PPLOAD_CLIENT_ID, 0185 'secret' => PPLOAD_SECRET 0186 )); 0187 } 0188 0189 /** 0190 * @param int $creator_id 0191 * @return bool 0192 * @throws Zend_Auth_Storage_Exception 0193 */ 0194 public function isAuthmemberProjectCreator($creator_id) 0195 { 0196 $auth = Zend_Auth::getInstance(); 0197 $authMember = $auth->getStorage()->read(); 0198 if ($authMember->member_id == $creator_id) { 0199 return true; 0200 } 0201 0202 return false; 0203 } 0204 0205 }