public function render(\Curry\Backend\AbstractLegacyBackend $backend, array $params) { $view = $this->view ? $this->view : $this->parentView; if (!$view instanceof \Curry_ModelView_List) { throw new \Exception('Expected view to be of type Curry_ModelView_List, got ' . get_class($view)); } $view = clone $view; // Send response headers to the browser $filename = $this->filename ? $this->filename : $view->getOption('title') . ".csv"; header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename=' . StringHelper::escapeQuotedString($filename)); $fp = fopen('php://output', 'w'); // Print column headers $headers = array(); foreach ($view->getOption('columns') as $name => $opts) { // TODO: should we really ignore display/escape option? $view->addColumn($name, array('display' => null, 'escape' => false)); $headers[] = $opts['label']; } if ($this->includeHeaders) { self::fputcsv($fp, $headers); } // Print rows $page = 0; $maxPerPage = 100; $view->setOptions(array('maxPerPage' => $maxPerPage)); do { $results = $view->getJson(array('p' => ++$page)); foreach ($results['rows'] as $row) { self::fputcsv($fp, $row); } } while (count($results['rows']) == $maxPerPage); fclose($fp); exit; }
/** * Create an archive of the project. */ public function showBundle() { $this->addMainMenu(); $this->addMessage('You can install this bundle using <a href="' . url('', array('module', 'view' => 'InstallScript')) . '">this installation script</a>.', self::MSG_NOTICE, false); $form = new \Curry_Form(array('action' => url('', array("module", "view")), 'method' => 'post', 'elements' => array('project' => array('checkbox', array('label' => 'Project', 'value' => true)), 'www' => array('checkbox', array('label' => 'WWW folder', 'value' => true)), 'vendor' => array('checkbox', array('label' => 'Vendor', 'value' => true)), 'database' => array('checkbox', array('label' => 'Database', 'value' => true)), 'compression' => array('select', array('label' => 'Compression', 'multiOptions' => array(Archive::COMPRESSION_NONE => 'None', Archive::COMPRESSION_GZ => 'Gzip'))), 'save' => array('submit', array('label' => 'Create bundle'))))); if (isPost() && $form->isValid($_POST)) { // create archive @set_time_limit(0); $compression = $form->compression->getValue(); $tar = new Archive('', $compression); // set up file list $options = array(array('pattern' => '*.svn*', 'pattern_subject' => 'path', 'skip' => true), array('pattern' => '*.git*', 'pattern_subject' => 'path', 'skip' => true), array('pattern' => '.DS_Store', 'skip' => true), array('pattern' => 'Thumbs.db', 'skip' => true), array('pattern' => '._*', 'skip' => true)); if ($form->project->isChecked()) { $tar->add($this->app['projectPath'], 'cms/', array_merge($options, array(array('path' => 'data/', 'pattern' => 'data/*/*', 'pattern_subject' => 'path', 'skip' => true)))); } if ($form->www->isChecked()) { $tar->add($this->app['wwwPath'], 'www/', $options); } if ($form->vendor->isChecked()) { $tar->add($this->app['projectPath'] . '/../vendor', 'vendor/', $options); } if ($form->database->isChecked()) { $fiveMBs = 5 * 1024 * 1024; $fp = fopen("php://temp/maxmemory:{$fiveMBs}", 'r+'); if (!\Curry_Backend_DatabaseHelper::dumpDatabase($fp)) { throw new \Exception('Aborting: There was an error when dumping the database.'); } fseek($fp, 0); $tar->addString('db.txt', stream_get_contents($fp)); fclose($fp); } $filename = str_replace(" ", "_", $this->app['name']) . "-bundle-" . date("Ymd") . ".tar" . ($compression ? ".{$compression}" : ''); header("Content-type: " . Archive::getCompressionMimeType($compression)); header("Content-disposition: attachment; filename=" . StringHelper::escapeQuotedString($filename)); // do not use output buffering while (ob_end_clean()) { } $tar->stream(); exit; } $this->addMainContent($form); return parent::render(); }
/** * Return a file to browser and exit. Will set appropriate headers and return the content. * * @param string $file Path to file * @param string $contentType The content-type header to send. * @param string $filename Filename to send to browser, uses the basename of $file if not specified. * @param bool $exit Terminate the script after sending the data. * @param bool $disableOutputBuffering Disable output buffering. */ public static function returnFile($file, $contentType = 'application/octet-stream', $filename = '', $exit = true, $disableOutputBuffering = true) { if (!$filename) { $filename = basename($file); } header('Content-Description: File Transfer'); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename=' . StringHelper::escapeQuotedString($filename)); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-type: ' . $contentType); header('Content-Length: ' . filesize($file)); if ($disableOutputBuffering) { while (@ob_end_flush()) { } } readfile($file); if ($exit) { exit; } }