Generates an export, download or local file.

@param null|string $path
@return mixed
@throws Exception
421996
protected function generateExport($path = null)
    {
        $temporary = $this->getTemporaryFilePath();

        // Prepare columns/strategies
        $this->columns          = $this->exportInfo->columns;
        $this->columnStrategies = $this->getColumnStrategyInstances();

        // Prepare headers/field keys
        $this->columnHeaders = array_map(
            function (ModelExportColumnDataInterface $column) {
                return $column->header();
            },
            $this->columns
        );

        // Create data array
        $data = [];
        $this->fillDataArray($data);

        $formatter = Formatter::make($data, Formatter::ARR);

        file_put_contents($temporary, $formatter->toXml());

        return $temporary;
    }
736624
private function loadFile(string $path)
    {
        if (!file_exists($path)) {
            throw new Exception('Configuration file "' . $path . '" is not found');
        }

        if (!is_readable($path)) {
            throw new Exception('Configuration file "' . $path . '" is not readable');
        }

        // Include file
        $content = include $path;

        if (!\is_array($content)) {
            throw new Exception("Passed file \"$path\" is not array");
        }

        return $content;
    }
22415
public function getUrl($path)
    {
        if ($this->publish($path)) {
            $obj = $this->getFileObject($path);
            if ($url = $obj->getWebContentLink()) {
                return str_replace('export=download', 'export=media', $url);
            }
            if ($url = $obj->getWebViewLink()) {
                return $url;
            }
        }
        return false;
    }
1061672
public function downloadAction($path)
    {
        try {
            $this->setPath($path);
        } catch (NotFoundException $e) {
            throw $e;
        }

        $dbfile = new WDBFile($this->getpath());
        if (!$dbfile->isFile()) {
            throw new NotFoundException(
                'Can not send raw content of a directory!',
                0, null, TemplateHelper::getRoute($dbfile->getWDBPath())
            );
        }

        $this->wdb->getResponse()
            ->download($path, 'text/plain');
        exit(0);
    }
1224962
public function execute($path = null)
    {
        $content = '';

        /*
         * Let's first take out strings, comments and regular expressions.
         * All of these can contain JS code-like characters, and we should make
         * sure any further magic ignores anything inside of these.
         *
         * Consider this example, where we should not strip any whitespace:
         * var str = "a   test";
         *
         * Comments will be removed altogether, strings and regular expressions
         * will be replaced by placeholder text, which we'll restore later.
         */
        $this->extractStrings('\'"`');
        $this->stripComments();
        $this->extractRegex();

        // loop files
        foreach ($this->data as $source => $js) {
            // take out strings, comments & regex (for which we've registered
            // the regexes just a few lines earlier)
            $js = $this->replace($js);

            $js = $this->propertyNotation($js);
            $js = $this->shortenBools($js);
            $js = $this->stripWhitespace($js);

            // combine js: separating the scripts by a ;
            $content .= $js.";";
        }

        // clean up leftover `;`s from the combination of multiple scripts
        $content = ltrim($content, ';');
        $content = (string) substr($content, 0, -1);

        /*
         * Earlier, we extracted strings & regular expressions and replaced them
         * with placeholder text. This will restore them.
         */
        $content = $this->restoreExtractedData($content);

        return $content;
    }
40818
private function loadFile($path)
    {
        if (file_exists($path)) {
            return $this->loadJsonFile($path);
        }

        $dirs = $this->paths();
        if (empty($dirs)) {
            return null;
        }

        $data = [];
        $dirs = array_reverse($dirs);
        foreach ($dirs as $dir) {
            $file = $dir.DIRECTORY_SEPARATOR.$path;
            if (file_exists($file)) {
                $data = array_replace_recursive($data, $this->loadJsonFile($file));
            }
        }

        if (empty($data)) {
            return null;
        }

        return $data;
    }
278870
public function get(string $path): File {
		return (new File(Paths::appendPath($this->getUrl(), $path)))->addFilters($this->recursiveFilter, true);
	}
790332
public static function fromFile($path)
    {
        return new self($path, filesize($path), \DateTime::createFromFormat('U', filemtime($path)));
    }
169643
public function downloadStream($path)
    {
        $url = $this->getDownloadUrlForFile($path);
        $input = $this->getStreamForUrl($url);

        $output = fopen('php://temp', 'w+');
        if ($output === false) {
            throw new OneDriveClientException('Error when saving the downloaded file');
        }

        while (!$input->eof()) {
            $writeResult = fwrite($output, $input->read(8192));
            if ($writeResult === false) {
                throw new OneDriveClientException('Error when saving the downloaded file');
            }
        }

        $input->close();

        rewind($output);

        return $output;
    }
1363269
public function save($path = null)
    {
        if (!is_null($path)) {
            $this->path = $path;
        }

        if (!is_dir($path = $this->getDestinationDirectory())) {
            File::makeDirectory($path, 0777, true);
        }

        $this->image->save($this->getDestinationFile());

        return $this->filename;
    }
220102
public function downloadFile($path)
	{
		// Download contents of current file
		if (($data = $this->downloadData()) === null) {
			return false;
		}

		// Write contents to specified path
		if (@file_put_contents($path, $data) === false) {
			$this->error = array();
			$this->error['file'] = "Unable to write file '$path'";
			return false;
		}

		return true;
	}
339499
public function get($path)
    {
        // Make sure we have a file
        if (empty($path)) {
            return null;
        }

        // Generate file URL
        $url = $this->urlProvider->getUrlFromPath($path);

        // Validate file URL
        if (! filter_var($url, FILTER_VALIDATE_URL)) {
            return null;
        }

        return $url;
    }
1253132
public function get($path = null) {
		// return full tree
		if (is_null($path)) return $this->_tree;

		if (!isset($this->_nodes[$path])) {
			throw new \Exception(__METHOD__.': path "'.$path.'" does not exist.');
			return;
		}
		return $this->_nodes[$path];
	}
1040090
static public function forge($path){
        switch(mimetype($path)) {
            case 'image/jpeg':
                return new Image(new Adapter\JPEG($path));
                break;
            case 'image/png':
                return new Image(new Adapter\PNG($path));
                break;
            case 'image/gif':
                return new Image(new Adapter\GIF($path));
                break;
            default:
                throw new Exception("'$path' image file is not supported");
        }
	}
119900
static public function download($url, $path)
    {
        # open file to write
        $fp = fopen($path, 'w+');
        # start curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        # set return transfer to false
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        # increase timeout to download big file
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        # write data to local file
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // That's the clue!
        # execute curl
        curl_exec($ch);
        # close curl
        curl_close($ch);
        # close local file
        fclose($fp);

        return filesize($path) > 0;
    }
1286740
public function init($path = null)
    {
        $path = is_null($path) ? $this->environment->path : $path;

        // Check if clone already exists at this path. If so we can safely skip.
        if (file_exists($path)) {
            $wrapper = new GitWrapper();
            $wrapper->setTimeout(3600);

            try {
                $working_copy = new GitWorkingCopy($wrapper, $path);
                $output = $working_copy->remote('-v');
            } catch (GitException $e) {
                throw new \Exception('Path already exists.');
            }

            // if repo exists in the remotes already, this working copy is ok.
            if (strpos(strtolower($output), strtolower($this->app->repo)) !== false) {
                return true;
            } else {
                throw new Exception('Git clone already exists at that path, but it is not for this app.');
            }
        }

        try {
            // Create App folder
            mkdir($path, 0755, TRUE);
            chdir($path);

            // Clone repo
            $wrapper = new GitWrapper();
            $wrapper->setTimeout(3600);
            $wrapper->streamOutput();
            $wrapper->cloneRepository($this->app->repo, $path);

            // Checkout correct version.
            $git = new GitWorkingCopy($wrapper, $this->getSourcePath());
            $git->checkout($this->environment->version);

        } catch (\GitWrapper\GitException $e) {

            // If exception is because there is no git ref, continue.
            if (strpos($e->getMessage(), 'error: pathspec') !== 0) {
                return false;
            }
        }

        chdir($path);
        $wrapper->git('branch');
        $wrapper->git('status');
        $this->loadConfig();

        // Run the build hooks
        if (!empty($this->config['hooks']['build'])) {
            chdir($this->getSourcePath());
            $process = new Process($this->config['hooks']['build']);
            $process->run(function ($type, $buffer) {
                if (Process::ERR === $type) {
                    echo $buffer;
                } else {
                    echo $buffer;
                }
            });
        }

        return $this->writeConfig();
    }
522095
public function saveToFile($path)
    {
        if (!file_exists($path)) {
            if (!@mkdir($path) && !is_dir($path)) {
                throw new \Exception('Cannot create directory ' . $path);
            };
        }
        $file = tempnam($path, 'mautic_api_');

        if (!is_writable($file)) {
            throw new \Exception($file.' is not writable');
        }

        if (!$handle = fopen($file, 'w')) {
            throw new \Exception('Cannot open file '.$file);
        }

        if (fwrite($handle, $this->body) === false) {
            throw new \Exception('Cannot write into file '.$file);
        }

        fclose($handle);

        return array(
            'file' => $file,
        );
    }