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 a relative path from one path to another. * * @param string $from * @param string $to * @return string */ public static function getRelativePath($from, $to) { $from = self::getAbsolutePath($from); $to = self::getAbsolutePath($to); $relative = ""; while ($from && $from !== $to && !Curry_String::startsWith($to, $from . '/')) { $relative .= '../'; $from = dirname($from); if ($from == '/') { break; } } if ($from !== $to) { $relative .= substr($to, strlen($from) + 1) . '/'; } return $relative; }
/** * 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; }
/** * Get twig properties for matching search document. * * @param Zend_Search_Lucene_Search_QueryHit $hit * @return array */ public function getHitProperties(Zend_Search_Lucene_Search_QueryHit $hit) { $r = $this->getRequest(); $snippet = Curry_String::toInternalEncoding($hit->body, 'utf-8'); $snippet = self::createSearchSnippet($snippet, $r->get['query'], $this->snippetLength); $relatedObject = null; $model = Curry_String::toInternalEncoding($hit->model, 'utf-8'); $fields = array(); foreach ($hit->getDocument()->getFieldNames() as $fieldName) { $fields[$fieldName] = $hit->{$fieldName}; } return array('Title' => Curry_String::toInternalEncoding($hit->title, 'utf-8'), 'Description' => Curry_String::toInternalEncoding($hit->description, 'utf-8'), 'Url' => Curry_String::toInternalEncoding($hit->url, 'utf-8'), 'Snippet' => $snippet, 'Score' => $hit->score, 'Fields' => $fields, 'RelatedObject' => new Curry_OnDemand(array($this, 'getRelatedObject'), $model, unserialize($hit->model_id))); }
/** * 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; } }
/** * Check if physical path is writable based on FilebrowserAccess. * * @param string $physical * @return bool */ public static function isPhysicalWritable($physical) { $roots = self::getRoots(); foreach ($roots as $root) { if ($root['writable'] && Curry_String::startsWith($physical . '/', $root['realpath'] . '/')) { return true; } } return false; }
/** * Backup database. */ public function showBackup() { $this->showMainMenu(); $tables = array(); $selectedTables = array(); foreach (Curry_Propel::getModels() as $package => $classes) { $selectedTables = array_merge($selectedTables, array_values($classes)); $tables[$package] = array(); foreach ($classes as $table) { $tables[$package][$table] = $table; } } $form = new Curry_Form(array('action' => url('', array("module", "view", "page_id")), 'method' => 'post', 'elements' => array('tables' => array('multiselect', array('label' => 'Tables', 'multiOptions' => $tables, 'value' => $selectedTables, 'size' => 15)), 'name' => array('text', array('label' => 'Name', 'required' => true, 'value' => 'backup_%Y-%m-%d_%H-%M-%S.txt', 'description' => 'Name of the file, strftime() is used to format the string.')), 'type' => array('radio', array('label' => 'Where do you want to save?', 'multiOptions' => array('online' => 'Save online', 'local' => 'Save to local file'), 'value' => 'online'))))); $form->addElement('submit', 'Go'); if (isPost() && ($_POST['tables'] == '*' || $_POST['tables'] == array('*'))) { $_POST['tables'] = $selectedTables; } if (isPost() && $form->isValid($_POST)) { $values = $form->getValues(); if ($values['type'] == 'local') { // dump to temp, stream to client $fp = fopen("php://temp", 'r+'); Curry_Backend_DatabaseHelper::dumpDatabase($fp, $values['tables'], $this); rewind($fp); $name = Curry_String::getRewriteString(Curry_Core::$config->curry->name) . '-db.txt'; Curry_Application::returnData($fp, 'application/octet-stream', $name); } else { if ($values['type'] == 'online') { $filename = Curry_Backend_DatabaseHelper::createBackupName($values['name']); $status = Curry_Backend_DatabaseHelper::dumpDatabase($filename, $values['tables'], $this); $this->addMessage('Backup created ' . $filename, $status ? self::MSG_SUCCESS : self::MSG_ERROR); } } } $this->addMainContent($form); }