Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FlyTokenWebSource | |
0.00% |
0 / 29 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| setFileSystem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFilePathFromUrl | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| getAccessUrl | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\websource; |
| 23 | |
| 24 | use common_ext_ExtensionsManager; |
| 25 | |
| 26 | /** |
| 27 | * @access public |
| 28 | * @package tao |
| 29 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 30 | */ |
| 31 | class FlyTokenWebSource extends TokenWebSource |
| 32 | { |
| 33 | public const ENTRY_POINT = '/getFileFlysystem.php/'; |
| 34 | |
| 35 | /** |
| 36 | * @param $fileSystem |
| 37 | */ |
| 38 | public function setFileSystem($fileSystem) |
| 39 | { |
| 40 | $this->fileSystem = $fileSystem; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get file path from url. |
| 45 | * @param null $url |
| 46 | * @return string |
| 47 | * @throws \tao_models_classes_FileNotFoundException |
| 48 | */ |
| 49 | public function getFilePathFromUrl($url) |
| 50 | { |
| 51 | $url = parse_url($url)['path']; //remove query part from url. |
| 52 | $rel = substr($url, strpos($url, self::ENTRY_POINT) + strlen(self::ENTRY_POINT)); |
| 53 | $parts = explode('/', $rel, 4); |
| 54 | list($webSourceId, $timestamp, $token, $subPath) = $parts; |
| 55 | |
| 56 | $parts = explode('*/', $subPath, 2); |
| 57 | if (count($parts) < 2) { |
| 58 | throw new \tao_models_classes_FileNotFoundException('`' . $subPath . '` - (wrong number of path parts)'); |
| 59 | } |
| 60 | list($subPath, $file) = $parts; |
| 61 | |
| 62 | $secret = $this->getOption('secret'); |
| 63 | $ttl = $this->getOption('ttl'); |
| 64 | |
| 65 | $correctToken = md5($timestamp . $subPath . $secret); |
| 66 | |
| 67 | if (time() - $timestamp > $ttl) { |
| 68 | throw new \tao_models_classes_FileNotFoundException('`' . $subPath . $file . '` - (file link expired)'); |
| 69 | } |
| 70 | if ($token != $correctToken) { |
| 71 | throw new \tao_models_classes_FileNotFoundException('`' . $subPath . $file . '` - (wrong token)'); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | $path = []; |
| 76 | foreach (explode('/', $subPath . $file) as $ele) { |
| 77 | $path[] = rawurldecode($ele); |
| 78 | } |
| 79 | $filename = implode('/', $path); |
| 80 | |
| 81 | return $filename; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param string $relativePath |
| 86 | * @return string |
| 87 | * @throws \common_exception_Error |
| 88 | * @throws \common_ext_ExtensionException |
| 89 | */ |
| 90 | public function getAccessUrl($relativePath) |
| 91 | { |
| 92 | $path = []; |
| 93 | foreach (preg_split('/[\/\\\]/', ltrim($relativePath, '/\\')) as $ele) { |
| 94 | $path[] = rawurlencode($ele); |
| 95 | } |
| 96 | $relUrl = implode('/', $path); |
| 97 | $token = $this->generateToken($relUrl); |
| 98 | $taoExtension = common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 99 | return $taoExtension->getConstant('BASE_URL') . 'getFileFlysystem.php/' . $this->getId() . '/' . $token |
| 100 | . '/' . $relUrl . '*/'; |
| 101 | } |
| 102 | } |