Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TokenWebSource | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| spawnWebsource | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getAccessUrl | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| generateToken | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; under version 2 |
| 8 | * of the License (non-upgradable). |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\websource; |
| 24 | |
| 25 | use common_ext_ExtensionsManager; |
| 26 | |
| 27 | /** |
| 28 | * Grants Access to compiled data via the getFile entry point |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Joel Bout, <joel@taotesting.com> |
| 32 | * @package tao |
| 33 | |
| 34 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 35 | */ |
| 36 | class TokenWebSource extends BaseWebsource |
| 37 | { |
| 38 | public const OPTION_SECRET = 'secret'; |
| 39 | public const OPTION_PATH = 'path'; |
| 40 | public const OPTION_TTL = 'ttl'; |
| 41 | |
| 42 | public static function spawnWebsource($fileSystemId, $path) |
| 43 | { |
| 44 | $provider = self::spawn($fileSystemId, [ |
| 45 | self::OPTION_SECRET => md5(rand() . $path), |
| 46 | self::OPTION_PATH => $path, |
| 47 | self::OPTION_TTL => (int) ini_get('session.gc_maxlifetime') |
| 48 | ]); |
| 49 | return $provider; |
| 50 | } |
| 51 | |
| 52 | public function getAccessUrl($relativePath) |
| 53 | { |
| 54 | $path = []; |
| 55 | foreach (preg_split('/[\/\\\]/', ltrim($relativePath, '/\\')) as $ele) { |
| 56 | $path[] = rawurlencode($ele); |
| 57 | } |
| 58 | $relUrl = implode('/', $path); |
| 59 | $relPath = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $relativePath)); |
| 60 | $token = $this->generateToken($relUrl); |
| 61 | $taoExtension = common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 62 | return $taoExtension->getConstant('BASE_URL') . 'getFile.php/' . $this->getId() . '/' . $token . '/' |
| 63 | . $relUrl . '*/'; |
| 64 | } |
| 65 | // helpers |
| 66 | |
| 67 | /** |
| 68 | * Generate a token for the resource |
| 69 | * Same algorithm is implemented again in getFile.php |
| 70 | * |
| 71 | * @param string $relPath |
| 72 | * @return string |
| 73 | */ |
| 74 | protected function generateToken($relPath) |
| 75 | { |
| 76 | $time = time(); |
| 77 | return $time . '/' . md5($time . $relPath . $this->getOption(self::OPTION_SECRET)); |
| 78 | } |
| 79 | } |