public function render(Curry_Backend $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=' . Curry_String::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) { Curry_Util::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) { Curry_Util::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)), 'base' => array('checkbox', array('label' => 'Curry Core', 'value' => true)), 'database' => array('checkbox', array('label' => 'Database', 'value' => true)), 'compression' => array('select', array('label' => 'Compression', 'multiOptions' => array(Curry_Archive::COMPRESSION_NONE => 'None', Curry_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 Curry_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(Curry_Core::$config->curry->projectPath, 'cms/', array_merge($options, array(array('path' => 'data/', 'pattern' => 'data/*/*', 'pattern_subject' => 'path', 'skip' => true)))); } if ($form->www->isChecked()) { $tar->add(Curry_Core::$config->curry->wwwPath, 'www/', array_merge($options, array(array('path' => 'shared', 'skip' => true), array('path' => 'shared/', 'skip' => true)))); } if ($form->base->isChecked()) { $sharedPath = realpath(Curry_Core::$config->curry->wwwPath . '/shared'); if ($sharedPath) { $tar->add($sharedPath, 'www/shared/', $options); } $tar->add(Curry_Core::$config->curry->basePath . '/include', 'curry/include/', $options); $tar->add(Curry_Core::$config->curry->basePath . '/propel', 'curry/propel/', $options); $tar->add(Curry_Core::$config->curry->basePath . '/vendor', 'curry/vendor/', $options); $tar->add(Curry_Core::$config->curry->basePath . '/.htaccess', 'curry/', $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(" ", "_", Curry_Core::$config->curry->name) . "-bundle-" . date("Ymd") . ".tar" . ($compression ? ".{$compression}" : ''); header("Content-type: " . Curry_Archive::getCompressionMimeType($compression)); header("Content-disposition: attachment; filename=" . Curry_String::escapeQuotedString($filename)); // do not use output buffering while (ob_end_clean()) { } $tar->stream(); exit; } $this->addMainContent($form); }
/** * Generate Excel document. * * @param string|null $filename * @param bool $headers Include first row with column names in output * @param bool $includeHidden Include hidden columns? */ public function returnExcel($filename = null, $headers = true, $includeHidden = false) { // Send response headers to the browser if (!$filename) { $filename = $this->options['title'] . ".csv"; } header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename=' . Curry_String::escapeQuotedString($filename)); $fp = fopen('php://output', 'w'); // Print column headers $hidden = array(); $values = array(); foreach ($this->columns as $opts) { $hide = $opts['hide'] && !$includeHidden; $hidden[] = $hide; if (!$hide) { $values[] = $opts['display']; } } if ($headers) { fputcsv($fp, $values); } // Print rows $q = $this->getCriteria(); $q->setFormatter('PropelOnDemandFormatter'); foreach ($q->find() as $obj) { $values = array(); $rowData = $this->getRow($obj); foreach ($rowData["cell"] as $i => $columnValue) { if (!$hidden[$i]) { $values[] = $columnValue; } } fputcsv($fp, $values); } fclose($fp); exit; }
/** * 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=' . Curry_String::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; } }