protected function getFilesFromUrl() { $url = $this->request->getParam('URL'); $this->log->debug("Downloading file: %s", $url); if (!self::$lastModified) { self::$lastModified = new binarypool_lastmodified(); } $lastmodified = self::$lastModified->lastModified($this->bucket, $url); if (binarypool_config::getCacheRevalidate($this->bucket) === 0) { $lastmodified['time'] = 0; } $tmpfile = tempnam(sys_get_temp_dir(), 'binary'); if ($tmpfile == '' || $tmpfile === FALSE) { throw new binarypool_exception(104, 500, "Could not create temporary file"); } array_push($this->tmpfiles, $tmpfile); $result = array('code' => 0, 'headers' => array(), 'body' => ''); $retries = 3; if ($lastmodified['revalidate']) { $httpc = new binarypool_httpclient(); while ($retries) { try { $result = $httpc->download($url, $tmpfile, $lastmodified['time']); if ($result['code'] < 500) { break; } } catch (binarypool_httpclient_exception $e) { // ignore - dropped connections etc. - retry $this->log->debug("Failed download attempt from %s: %s", $url, $e); } sleep(1); $retries--; } } else { $result['code'] = 304; } if (304 == $result['code']) { $this->log->debug("File %s has not been modified", $url); return array(); } if ($result['code'] != 200 || !filesize($tmpfile)) { binarypool_views::flagBadUrl($this->bucket, $url); throw new binarypool_exception(121, 400, "File could not be fetched from URL: " . $url); } $url_parsed = parse_url($url); $filename = basename($url_parsed['path']); # Restrict filenames TO ALPHANUMS and reduce sequences of '.' to avoid # traversal issues, unicode issues, command injection etc. $filename = preg_replace(array('#\\.{2,}#', '#[^a-zA-Z0-9\\.]+#'), array('.', '_'), $filename); return array('_' => array('file' => $tmpfile, 'filename' => $filename)); }
/** * Tests the flagBadUrl creates a symlink pointing to /dev/null */ function testFlagBadUrl() { $url = 'http://staticlocal.ch/images/bad.gif'; $urlhash = '55c79a345dd0ec066c1f6089c89e94478eaa2437'; $symlink = sprintf("%sdownloaded/55/55c79a345dd0ec066c1f6089c89e94478eaa2437", self::$BUCKET); binarypool_views::flagBadUrl('test', $url); $this->assertEqual(readlink($symlink), '/dev/null', 'Symlink does not point to /dev/null'); }