Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| HttpSource | |
0.00% |
0 / 49 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| getFileInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| download | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
| getBaseName | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getDirectories | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDirectory | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getFileStream | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getRequest | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2015-2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\media\sourceStrategy; |
| 23 | |
| 24 | use common_Exception; |
| 25 | use common_Logger; |
| 26 | use GuzzleHttp\Client; |
| 27 | use helpers_TimeOutHelper; |
| 28 | use oat\tao\model\media\MediaBrowser; |
| 29 | use oat\tao\model\media\mediaSource\DirectorySearchQuery; |
| 30 | |
| 31 | /** |
| 32 | * This media source gives access to files not part of the Tao platform |
| 33 | * |
| 34 | * It is not intended to be used to browse for |
| 35 | */ |
| 36 | class HttpSource implements MediaBrowser |
| 37 | { |
| 38 | /** |
| 39 | * (non-PHPdoc) |
| 40 | * @see \oat\tao\model\media\MediaBrowser::getFileInfo() |
| 41 | */ |
| 42 | public function getFileInfo($link) |
| 43 | { |
| 44 | throw new common_Exception(__FUNCTION__ . ' not implemented'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * (non-PHPdoc) |
| 49 | * @see \oat\tao\model\media\MediaBrowser::download() |
| 50 | */ |
| 51 | public function download($link) |
| 52 | { |
| 53 | $url = str_replace('\/', '/', $link); |
| 54 | $fileName = \tao_helpers_File::createTempDir() . basename($link); |
| 55 | |
| 56 | common_Logger::d('Downloading ' . $url); |
| 57 | helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::NO_TIMEOUT); |
| 58 | $fp = fopen($fileName, 'w+'); |
| 59 | $curlHandler = curl_init(); |
| 60 | curl_setopt($curlHandler, CURLOPT_URL, $url); |
| 61 | curl_setopt($curlHandler, CURLOPT_FILE, $fp); |
| 62 | curl_setopt($curlHandler, CURLOPT_TIMEOUT, 50); |
| 63 | curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, true); |
| 64 | |
| 65 | //if there is an http auth on the local domain, it's mandatory to auth with curl |
| 66 | if (USE_HTTP_AUTH) { |
| 67 | $addAuth = false; |
| 68 | $domains = ['localhost', '127.0.0.1', ROOT_URL]; |
| 69 | |
| 70 | foreach ($domains as $domain) { |
| 71 | if (preg_match("/" . preg_quote($domain, '/') . "/", $url)) { |
| 72 | $addAuth = true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if ($addAuth) { |
| 77 | curl_setopt($curlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 78 | curl_setopt($curlHandler, CURLOPT_USERPWD, USE_HTTP_USER . ":" . USE_HTTP_PASS); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | curl_exec($curlHandler); |
| 83 | $httpCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); |
| 84 | $success = $httpCode == 200; |
| 85 | curl_close($curlHandler); |
| 86 | fclose($fp); |
| 87 | helpers_TimeOutHelper::reset(); |
| 88 | |
| 89 | return $fileName; |
| 90 | } |
| 91 | |
| 92 | public function getBaseName($link) |
| 93 | { |
| 94 | $url = str_replace('\/', '/', $link); |
| 95 | |
| 96 | // by URL Basename |
| 97 | $path = parse_url($url, PHP_URL_PATH); |
| 98 | $realfilename = basename($path); |
| 99 | |
| 100 | $content = @get_headers($url, 1); |
| 101 | |
| 102 | if ($content === false) { |
| 103 | throw new \tao_models_classes_FileNotFoundException($url); |
| 104 | } |
| 105 | |
| 106 | $content = array_change_key_case($content, CASE_LOWER); |
| 107 | // by header |
| 108 | if (isset($content['content-disposition'])) { |
| 109 | $tmp_name = explode('=', $content['content-disposition']); |
| 110 | if ($tmp_name[1]) { |
| 111 | $realfilename = trim($tmp_name[1], '";\''); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $realfilename; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @throws common_Exception |
| 120 | */ |
| 121 | public function getDirectories(DirectorySearchQuery $params): array |
| 122 | { |
| 123 | return $this->getDirectory(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @inheritDoc |
| 128 | */ |
| 129 | public function getDirectory($parentLink = '/', $acceptableMime = [], $depth = 1) |
| 130 | { |
| 131 | throw new common_Exception('Unable to browse the internet'); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | public function getFileStream($link) |
| 136 | { |
| 137 | $url = str_replace('\/', '/', $link); |
| 138 | common_Logger::d('Getting Stream ' . $url); |
| 139 | |
| 140 | $response = $this->getRequest($url); |
| 141 | $stream = $response->getBody(); |
| 142 | return $stream; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param $url |
| 147 | * @return \Psr\Http\Message\ResponseInterface |
| 148 | */ |
| 149 | private function getRequest($url) |
| 150 | { |
| 151 | $client = new Client(); |
| 152 | return $client->get($url); |
| 153 | } |
| 154 | } |