Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 35  | 
               | 
       0.00%  | 
       0 / 5  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| tao_actions_File |         | 
       0.00%  | 
       0 / 35  | 
               | 
       0.00%  | 
       0 / 5  | 
       306 |         | 
       0.00%  | 
       0 / 1  | 
      
| upload |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| uploadFile |         | 
       0.00%  | 
       0 / 4  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| downloadFile |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| getPropertyFileInfo |         | 
       0.00%  | 
       0 / 12  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 | |||
| accessFile |         | 
       0.00%  | 
       0 / 9  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| 1 | <?php | 
| 2 | |
| 3 | /** | 
| 4 | * This program is free software; you can redistribute it and/or | 
| 5 | * modify it under the terms of the GNU General Public License | 
| 6 | * as published by the Free Software Foundation; under version 2 | 
| 7 | * of the License (non-upgradable). | 
| 8 | * | 
| 9 | * This program is distributed in the hope that it will be useful, | 
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 | * GNU General Public License for more details. | 
| 13 | * | 
| 14 | * You should have received a copy of the GNU General Public License | 
| 15 | * along with this program; if not, write to the Free Software | 
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
| 17 | * | 
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg | 
| 19 | * (under the project TAO & TAO2); | 
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung | 
| 21 | * (under the project TAO-TRANSFER); | 
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor | 
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); | 
| 24 | * 2013-2018 (update and modification) Open Assessment Technologies SA; | 
| 25 | */ | 
| 26 | |
| 27 | use oat\oatbox\filesystem\File; | 
| 28 | use oat\tao\model\upload\UploadService; | 
| 29 | use oat\tao\model\websource\WebsourceManager; | 
| 30 | use oat\tao\model\websource\ActionWebSource; | 
| 31 | use oat\generis\model\fileReference\FileReferenceSerializer; | 
| 32 | use oat\generis\model\OntologyAwareTrait; | 
| 33 | |
| 34 | /** | 
| 35 | * | 
| 36 | * Controller use for the file upload components | 
| 37 | * | 
| 38 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} | 
| 39 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php | 
| 40 | * @package tao | 
| 41 | |
| 42 | */ | 
| 43 | class tao_actions_File extends tao_actions_CommonModule | 
| 44 | { | 
| 45 | use OntologyAwareTrait; | 
| 46 | |
| 47 | /** | 
| 48 | * Upload a file using http and copy it from the tmp dir to the target folder | 
| 49 | * @return void | 
| 50 | */ | 
| 51 | public function upload() | 
| 52 | { | 
| 53 | $response = ['uploaded' => false]; | 
| 54 | |
| 55 | foreach ((array)$_FILES as $file) { | 
| 56 | $targetFolder = isset($_POST['folder']) ? $_POST['folder'] : '/'; | 
| 57 | $response = array_merge($response, $this->uploadFile($file, $targetFolder . '/')); | 
| 58 | } | 
| 59 | $this->returnJson($response); | 
| 60 | } | 
| 61 | |
| 62 | /** | 
| 63 | * Get, check and move the file uploaded (described in the posetedFile parameter) | 
| 64 | * | 
| 65 | * @param array $postedFile | 
| 66 | * @param string $folder | 
| 67 | * @return array $data | 
| 68 | * @throws \oat\oatbox\service\ServiceNotFoundException | 
| 69 | * @throws \common_Exception | 
| 70 | */ | 
| 71 | protected function uploadFile($postedFile, $folder) | 
| 72 | { | 
| 73 | $returnValue = []; | 
| 74 | |
| 75 | if (isset($postedFile['tmp_name'], $postedFile['name']) && $postedFile['tmp_name']) { | 
| 76 | $returnValue = $this->getServiceLocator()->get(UploadService::SERVICE_ID)->uploadFile($postedFile, $folder); | 
| 77 | } | 
| 78 | return $returnValue; | 
| 79 | } | 
| 80 | |
| 81 | /** | 
| 82 | * Download a resource file content | 
| 83 | * @param string id Uri of the resource file | 
| 84 | */ | 
| 85 | public function downloadFile() | 
| 86 | { | 
| 87 | if ($this->hasRequestParameter('id')) { | 
| 88 | $fileService = $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID); | 
| 89 | $file = $fileService->unserialize($this->getRequestParameter('id')); | 
| 90 | header("Content-Disposition: attachment; filename=\"{$file->getBasename()}\""); | 
| 91 | tao_helpers_Http::returnStream($file->readPsrStream(), $file->getMimeType()); | 
| 92 | } | 
| 93 | } | 
| 94 | |
| 95 | public function getPropertyFileInfo() | 
| 96 | { | 
| 97 | $data = ['name' => __('(empty)')]; | 
| 98 | |
| 99 | if ($this->hasRequestParameter('uri') && $this->hasRequestParameter('propertyUri')) { | 
| 100 | $uri = tao_helpers_Uri::decode($this->getRequestParameter('uri')); | 
| 101 | $propertyUri = tao_helpers_Uri::decode($this->getRequestParameter('propertyUri')); | 
| 102 | $instance = $this->getResource($uri); | 
| 103 | $fileResource = $instance->getOnePropertyValue($this->getProperty($propertyUri)); | 
| 104 | |
| 105 | if (!is_null($fileResource) && $fileResource instanceof core_kernel_classes_Resource) { | 
| 106 | /** @var FileReferenceSerializer $fileService */ | 
| 107 | $fileService = $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID); | 
| 108 | $file = $fileService->unserialize($fileResource); | 
| 109 | |
| 110 | if ($file instanceof File) { | 
| 111 | $data['name'] = $file->getBasename(); | 
| 112 | } | 
| 113 | } | 
| 114 | } | 
| 115 | |
| 116 | return $this->returnJson($data); | 
| 117 | } | 
| 118 | |
| 119 | /** | 
| 120 | * @throws ResolverException | 
| 121 | * @throws \oat\tao\model\websource\WebsourceNotFound | 
| 122 | * @throws tao_models_classes_FileNotFoundException | 
| 123 | */ | 
| 124 | public function accessFile() | 
| 125 | { | 
| 126 | list($extension, $module, $action, $code, $filePath) = explode('/', tao_helpers_Request::getRelativeUrl(), 5); | 
| 127 | ; | 
| 128 | list($key, $subPath) = explode(' ', base64_decode($code), 2); | 
| 129 | |
| 130 | $source = $this->getServiceLocator()->get(WebsourceManager::class)->getWebsource($key); | 
| 131 | if ($source instanceof ActionWebSource) { | 
| 132 | $path = $subPath . (empty($filePath) ? '' : DIRECTORY_SEPARATOR . $filePath); | 
| 133 | return $this->getPsrResponse() | 
| 134 | ->withBody($source->getFileStream($path)) | 
| 135 | ->withHeader('content-type', $source->getMimetype($path)); | 
| 136 | } else { | 
| 137 | throw new common_exception_NotFound('File not found'); | 
| 138 | } | 
| 139 | } | 
| 140 | } |