Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 403 when domain is not allowed #6

Closed
baamenabar opened this issue Jun 15, 2016 · 1 comment
Closed

Return 403 when domain is not allowed #6

baamenabar opened this issue Jun 15, 2016 · 1 comment

Comments

@baamenabar
Copy link
Member

Currently only returning 500
Even if I try to exit with error code the framework seems to be catching me first.

public function saveNewFile($sourceFile, $newFileName, $options)
    {
        $newFilePath = TMP_DIR . $newFileName;
        try {
            $tmpFile = $this->saveToTemporaryFile($sourceFile);
        } catch (Exception $e) {
            http_response_code($e->getCode());
            exit($e->getMessage());
        }
        $commandStr = $this->generateCmdString($newFilePath, $tmpFile, $options);

        exec($commandStr, $output, $code);
        if (count($output) === 0) {
            $output = $code;
        } else {
            $output = implode(PHP_EOL, $output);
        }

        if ($code !== 0) {
            throw new \Exception($output . ' Command line: ' . $commandStr);
        }
        $this->filesystem->write($newFileName, stream_get_contents(fopen($newFilePath, 'r')));
        unlink($tmpFile);
        unlink($newFilePath);
    }

and then

public function saveToTemporaryFile($fileUrl)
    {
        //check restricted_domains is enabled
        if ($this->params['restricted_domains'] &&
            is_array($this->params['whitelist_domains']) &&
            !in_array(parse_url($fileUrl, PHP_URL_HOST), $this->params['whitelist_domains'])
        ) {
            throw  new \Exception('Restricted domains enabled, the domain your fetching from is not allowed: ' . parse_url($fileUrl, PHP_URL_HOST), 403);

        }

        if (!$resource = @fopen($fileUrl, "r")) {
            throw  new \Exception('Error occured while trying to read the file Url : ' . $fileUrl, 400);
        }
        $content = "";
        while ($line = fread($resource, 1024)) {
            $content .= $line;
        }
        $tmpFile = TMP_DIR . uniqid("", true);
        file_put_contents($tmpFile, $content);
        return $tmpFile;
    }

but it doesn't work

@sadok-f
Copy link
Member

sadok-f commented Jun 17, 2016

You missing \ in front of Exception,
catch (\Exception $e) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants