/**
  * Do the export.
  *
  * @param DataContainer $dc
  */
 public function onsubmit_callback(DataContainer $dc)
 {
     // Get delimiter
     switch ($dc->getData('delimiter')) {
         case 'semicolon':
             $delimiter = ';';
             break;
         case 'tabulator':
             $delimiter = "\t";
             break;
         case 'linebreak':
             $delimiter = "\n";
             break;
         default:
             $delimiter = ',';
             break;
     }
     // Get enclosure
     switch ($dc->getData('enclosure')) {
         case 'single':
             $enclosure = '\'';
             break;
         default:
             $enclosure = '"';
             break;
     }
     // Get fields
     $fields = $dc->getData('fields');
     // Get field labels
     $labels = array();
     foreach ($fields as $field) {
         switch ($field) {
             default:
                 $fieldConfig = $GLOBALS['TL_DCA']['orm_avisota_recipient']['fields'][$field];
                 if (empty($fieldConfig['label'][0])) {
                     $labels[] = $field;
                 } else {
                     $labels[] = $fieldConfig['label'][0] . ' [' . $field . ']';
                 }
                 break;
         }
     }
     $this->Session->set('AVISOTA_EXPORT', array('delimiter' => $dc->getData('delimiter'), 'enclosure' => $dc->getData('enclosure'), 'fields' => $dc->getData('fields')));
     // search for the list
     $list = \Database::getInstance()->prepare("SELECT * FROM orm_avisota_mailing_list WHERE id=?")->execute($this->Input->get('id'));
     if (!$list->next()) {
         $this->log('The recipient list ID ' . $this->Input->get('id') . ' does not exists!', 'orm_avisota_recipient_export', TL_ERROR);
         $this->redirect('contao/main.php?act=error');
     }
     // create temporary file
     $temporaryPathname = substr(tempnam(TL_ROOT . '/system/tmp', 'recipients_export_') . '.csv', strlen(TL_ROOT) + 1);
     // create new file object
     $temporaryFile = new File($temporaryPathname);
     // open file handle
     $temporaryFile->write('');
     // write the headline
     fputcsv($temporaryFile->handle, $labels, $delimiter, $enclosure);
     // write recipient rows
     $recipient = \Database::getInstance()->prepare("SELECT * FROM orm_avisota_recipient WHERE pid=?")->execute($this->Input->get('id'));
     while ($recipient->next()) {
         $row = array();
         foreach ($fields as $field) {
             switch ($field) {
                 default:
                     $row[] = $recipient->{$field};
             }
         }
         fputcsv($temporaryFile->handle, $row, $delimiter, $enclosure);
     }
     // close file handle
     $temporaryFile->close();
     // create temporary zip file
     $zipFile = $temporaryPathname . '.zip';
     // create a zip writer
     $zip = new ZipWriter($zipFile);
     // add the temporary csv
     $zip->addFile($temporaryPathname, $list->title . '.csv');
     // close the zip
     $zip->close();
     // create new file object
     $zip = new File($zipFile);
     // Open the "save as …" dialogue
     header('Content-Type: ' . $zip->mime);
     header('Content-Transfer-Encoding: binary');
     header('Content-Disposition: attachment; filename="' . $list->title . '.zip"');
     header('Content-Length: ' . $zip->filesize);
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Pragma: public');
     header('Expires: 0');
     // send the zip file
     $resFile = fopen(TL_ROOT . '/' . $zipFile, 'rb');
     fpassthru($resFile);
     fclose($resFile);
     // delete temporary files
     $temporaryFile->delete();
     $zip->delete();
     exit;
 }
Пример #2
0
	/**
	 * Run the live update
	 * @param BackendTemplate
	 */
	protected function runLiveUpdate(BackendTemplate $objTemplate)
	{
		$archive = 'system/tmp/' . $this->Input->get('token');

		// Download the archive
		if (!file_exists(TL_ROOT . '/' . $archive))
		{
			$objRequest = new Request();
			$objRequest->send($GLOBALS['TL_CONFIG']['liveUpdateBase'] . 'request.php?token=' . $this->Input->get('token'));

			if ($objRequest->hasError())
			{
				$objTemplate->updateClass = 'tl_error';
				$objTemplate->updateMessage = $objRequest->response;
				return;
			}

			$objFile = new File($archive);
			$objFile->write($objRequest->response);
			$objFile->close();
		}

		// Create a minimalistic HTML5 document
		echo '<!DOCTYPE html>'
			.'<head>'
			  .'<meta charset="utf-8">'
			  .'<title>Contao Live Update</title>'
			  .'<style>'
			    .'body { background:#f5f5f5 url("../system/themes/default/images/hbg.jpg") left top repeat-x; font-family:Verdana,sans-serif; font-size:11px; color:#444; padding:1em; }'
			    .'div { max-width:680px; margin:0 auto; border:1px solid #bbb; background:#fff; }'
			    .'h1 { font-size:12px; color:#fff; margin:1px; padding:2px 6px 4px; background:#b3b6b3 url("../system/themes/default/images/headline.gif") left top repeat-x; }'
			    .'h2 { font-size:14px; color:#8ab858; margin:18px 15px; padding:6px 42px 8px; background:url("../system/themes/default/images/current.gif") left center no-repeat; }'
			    .'ol { border:1px solid #ccc; max-height:430px; margin:0 15px 18px; overflow:auto; padding:3px 18px 3px 48px; background:#fcfcfc; }'
			    .'li { margin-bottom:3px; }'
			    .'p { margin:0 12px; padding:0; overflow:hidden; }'
			    .'.button { font-family:"Trebuchet MS",Verdana,sans-serif; font-size:12px; display:block; float:right; margin:0 3px 18px; border-radius:3px; background:#808080; text-decoration:none; color:#fff; padding:4px 18px; box-shadow:0 1px 3px #bbb; text-shadow:1px 1px 0 #666; }'
			    .'.button:hover,.button:focus { box-shadow:0 0 6px #8ab858; }'
			    .'.button:active { background:#8ab858; }'
			  .'</style>'
			.'<body>'
			.'<div>';

		$objArchive = new ZipReader($archive);

		// Table of contents
		if ($this->Input->get('toc'))
		{
			$arrFiles = $objArchive->getFileList();
			array_shift($arrFiles);

			echo '<hgroup>'
				  .'<h1>Contao Live Update</h1>'
				  .'<h2>' . $GLOBALS['TL_LANG']['tl_maintenance']['toc'] . '</h2>'
				.'</hgroup>'
				.'<ol>'
				  .'<li>' . implode('</li><li>', $arrFiles) . '</li>'
				.'</ol>'
				.'<p>'
				  .'<a href="' . ampersand(str_replace('toc=1', 'toc=', $this->Environment->base . $this->Environment->request)) . '" class="button">' . $GLOBALS['TL_LANG']['MSC']['continue'] . '</a>'
				  .'<a href="' . $this->Environment->base . 'contao/main.php?do=maintenance" class="button">' . $GLOBALS['TL_LANG']['MSC']['cancelBT'] . '</a>'
				  .'</p>'
				.'</div>';

			exit;
		}

		// Backup
		if ($this->Input->get('bup'))
		{
			echo '<hgroup>'
				  .'<h1>Contao Live Update</h1>'
				  .'<h2>' . $GLOBALS['TL_LANG']['tl_maintenance']['backup'] . '</h2>'
				.'</hgroup>'
				.'<ol>';

			$arrFiles = $objArchive->getFileList();
			$objBackup = new ZipWriter('LU' . date('YmdHi') . '.zip');

			foreach ($arrFiles as $strFile)
			{
				if ($strFile == 'TOC.txt' || $strFile == 'system/runonce.php')
				{
					continue;
				}

				try
				{
					$objBackup->addFile($strFile);
					echo '<li>Backed up ' . $strFile . '</li>';
				}
				catch (Exception $e)
				{
					echo '<li>' . $e->getMessage() . ' (skipped)</li>';
				}
			}

			$objBackup->close();

			echo '</ol>'
				.'<p>'
				  .'<a href="' . ampersand(str_replace('bup=1', 'bup=', $this->Environment->base . $this->Environment->request)) . '" id="continue" class="button">' . $GLOBALS['TL_LANG']['MSC']['continue'] . '</a>'
				  .'<a href="' . $this->Environment->base . 'contao/main.php?do=maintenance" id="back" class="button">' . $GLOBALS['TL_LANG']['MSC']['cancelBT'] . '</a>'
				  .'</p>'
				.'</div>';

			exit;
 		}

 		echo '<hgroup>'
			  .'<h1>Contao Live Update</h1>'
			  .'<h2>' . $GLOBALS['TL_LANG']['tl_maintenance']['update'] . '</h2>'
			.'</hgroup>'
			.'<ol>';

		// Update
		while ($objArchive->next())
		{
			if ($objArchive->file_name == 'TOC.txt')
			{
				continue;
			}

			try
			{
				$objFile = new File($objArchive->file_name);
				$objFile->write($objArchive->unzip());
				$objFile->close();

				echo '<li>Updated ' . $objArchive->file_name . '</li>';
			}
			catch (Exception $e)
			{
				echo '<li><span style="color:#c55">Error updating ' . $objArchive->file_name . ': ' . $e->getMessage() . '</span></li>';
			}
		}

		// Delete the archive
		$this->import('Files');
		$this->Files->delete($archive);

		// Add a log entry
		$this->log('Live update from version ' . VERSION . '.' . BUILD . ' to version ' . $GLOBALS['TL_CONFIG']['latestVersion'] . ' completed', 'LiveUpdate run()', TL_GENERAL);

		echo '</ol>'
			.'<p>'
			  .'<a href="' . $this->Environment->base . 'contao/main.php?do=maintenance&amp;act=runonce" id="continue" class="button">' . $GLOBALS['TL_LANG']['MSC']['continue'] . '</a>'
			.'</p>'
			.'</div>';

		exit;
	}
Пример #3
0
 /**
  * Add templates to the archive
  *
  * @param \ZipWriter $objArchive
  * @param string     $strFolder
  */
 protected function addTemplatesToArchive(\ZipWriter $objArchive, $strFolder)
 {
     // Strip the templates folder name
     $strFolder = preg_replace('@^templates/@', '', $strFolder);
     // Re-add the templates folder name
     if ($strFolder == '') {
         $strFolder = 'templates';
     } else {
         $strFolder = 'templates/' . $strFolder;
     }
     if (\Validator::isInsecurePath($strFolder)) {
         throw new \RuntimeException('Insecure path ' . $strFolder);
     }
     // Return if the folder does not exist
     if (!is_dir(TL_ROOT . '/' . $strFolder)) {
         return;
     }
     $arrAllowed = trimsplit(',', \Config::get('templateFiles'));
     array_push($arrAllowed, 'sql');
     // see #7048
     // Add all template files to the archive
     foreach (scan(TL_ROOT . '/' . $strFolder) as $strFile) {
         if (preg_match('/\\.(' . implode('|', $arrAllowed) . ')$/', $strFile) && strncmp($strFile, 'be_', 3) !== 0 && strncmp($strFile, 'nl_', 3) !== 0) {
             $objArchive->addFile($strFolder . '/' . $strFile);
         }
     }
 }
Пример #4
0
	/**
	 * Add templates to the archive
	 * @param ZipWriter
	 * @param string
	 */
	protected function addTemplatesToArchive(ZipWriter $objArchive, $strFolder)
	{
		// Sanitize the folder name
		$strFolder = str_replace('../', '', $strFolder);
		$strFolder = preg_replace('@^templates/@', '', $strFolder);

		if ($strFolder == '')
		{
			$strFolder = 'templates';
		}
		else
		{
			$strFolder = 'templates/' . $strFolder;
		}

		// Return if the folder does not exist
		if (!is_dir(TL_ROOT .'/'. $strFolder))
		{
			return;
		}

		$arrAllowed = trimsplit(',', $GLOBALS['TL_CONFIG']['templateFiles']);

		// Add all template files to the archive
		foreach (scan(TL_ROOT .'/'. $strFolder) as $strFile)
		{
			if (preg_match('/\.(' . implode('|', $arrAllowed) . ')$/', $strFile) && strncmp($strFile, 'be_', 3) !== 0 && strncmp($strFile, 'nl_', 3) !== 0)
			{
				$objArchive->addFile($strFolder .'/'. $strFile);
			}
		}
	}
Пример #5
0
 /**
  * Starts the download of the specified messages.
  * 
  * @param	string		$pmIDs
  */
 public static function downloadAll($pmIDs)
 {
     // count messages
     $sql = "SELECT\tCOUNT(*) AS count\n\t\t\tFROM\twcf" . WCF_N . "_pm\n\t\t\tWHERE\tpmID IN (" . $pmIDs . ")";
     $row = WCF::getDB()->getFirstRow($sql);
     $count = $row['count'];
     // get recipients
     $recpients = array();
     $sql = "SELECT\t\t*\n\t\t\tFROM\t\twcf" . WCF_N . "_pm_to_user\n\t\t\tWHERE\t\tpmID IN (" . $pmIDs . ")\n\t\t\t\t\tAND isBlindCopy = 0\n\t\t\tORDER BY\trecipient";
     $result = WCF::getDB()->sendQuery($sql);
     while ($row = WCF::getDB()->fetchArray($result)) {
         if (!isset($recpients[$row['pmID']])) {
             $recpients[$row['pmID']] = array();
         }
         $recpients[$row['pmID']][] = new PMRecipient(null, null, $row);
     }
     // get messages
     if ($count > 1) {
         $zip = new ZipWriter();
     }
     $sql = "SELECT\t\trecipient.*,\n\t\t\t\t\tpm.*\n\t\t\tFROM\t\twcf" . WCF_N . "_pm pm\n\t\t\tLEFT JOIN \twcf" . WCF_N . "_pm_to_user recipient\n\t\t\tON \t\t(recipient.pmID = pm.pmID\n\t\t\t\t\tAND recipient.recipientID = " . WCF::getUser()->userID . "\n\t\t\t\t\tAND recipient.isDeleted < 2)\n\t\t\tWHERE\t\tpm.pmID IN (" . $pmIDs . ")\n\t\t\tGROUP BY\tpm.pmID\n\t\t\tORDER BY\tpm.time DESC";
     $result = WCF::getDB()->sendQuery($sql);
     $messageNo = 1;
     while ($row = WCF::getDB()->fetchArray($result)) {
         $pm = new PM(null, $row);
         $pm->setRecipients(isset($recpients[$row['pmID']]) ? $recpients[$row['pmID']] : array());
         // get parsed text
         require_once WCF_DIR . 'lib/data/message/bbcode/MessageParser.class.php';
         $parser = MessageParser::getInstance();
         $parser->setOutputType('text/plain');
         $parsedText = $parser->parse($pm->message, $pm->enableSmilies, $pm->enableHtml, $pm->enableBBCodes, false);
         $data = array('$author' => $pm->username ? $pm->username : WCF::getLanguage()->get('wcf.pm.author.system'), '$date' => DateUtil::formatTime(null, $pm->time), '$recipient' => implode(', ', $pm->getRecipients()), '$subject' => $pm->subject, '$text' => $parsedText);
         if ($count == 1) {
             // send headers
             // file type
             @header('Content-Type: text/plain');
             // file name
             @header('Content-disposition: attachment; filename="' . $pm->pmID . '-' . preg_replace('~[^a-z0-9_ -]+~i', '', $pm->subject) . '.txt"');
             // no cache headers
             @header('Pragma: no-cache');
             @header('Expires: 0');
             // output message
             echo (CHARSET == 'UTF-8' ? "" : '') . WCF::getLanguage()->get('wcf.pm.download.message', $data);
             exit;
         } else {
             $zip->addFile((CHARSET == 'UTF-8' ? "" : '') . WCF::getLanguage()->get('wcf.pm.download.message', $data), $pm->pmID . '-' . preg_replace('~[^a-z0-9_ -]+~i', '', $pm->subject) . '.txt', $pm->time);
         }
         $messageNo++;
     }
     if ($messageNo > 1) {
         // send headers
         // file type
         @header('Content-Type: application/octet-stream');
         // file name
         @header('Content-disposition: attachment; filename="messages-' . ($messageNo - 1) . '.zip"');
         // no cache headers
         @header('Pragma: no-cache');
         @header('Expires: 0');
         // output file
         echo $zip->getFile();
         exit;
     }
 }
 public function generateArchiveOutput(WatchlistItemModel $objItem, \ZipWriter $objZip)
 {
     $objFile = \FilesModel::findById($objItem->uuid);
     if ($objFile === null) {
         return $objZip;
     }
     $objZip->addFile($objFile->path, $objFile->name);
     return $objZip;
 }