PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/misc/FileExporter.class.php

http://piwam.googlecode.com/
PHP | 136 lines | 86 code | 8 blank | 42 comment | 2 complexity | f478c910dd27423ea23f1588e4ed4403 MD5 | raw file
Possible License(s): ISC, LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * Provides an easy way to export contents into a file. This class uses
  4. * the ``ob`` buffers to perform.
  5. * Basical works is the following :
  6. *
  7. * 1) Open a new buffer session
  8. * 2) The user can write everything he wants
  9. * 3) We perform the file export
  10. *
  11. * @see http://fr.php.net/manual/fr/book.outcontrol.php
  12. * @author Adrien Mogenet <adrien.mogenet@chilipoker.com>
  13. * @since r15
  14. */
  15. class FileExporter
  16. {
  17. private $_filename = '';
  18. private $_isBufferOpen = false;
  19. private $_mimeType = '';
  20. private $_csvSeparator = ',';
  21. /**
  22. * Default ctor. Can take a filename and the associated MIME type as
  23. * arguments.
  24. *
  25. * @param string $filename
  26. * @param string $mimeType The MIME type
  27. */
  28. public function __construct($filename = 'no-name', $mimeType = 'application/text')
  29. {
  30. $this->setFilename($filename);
  31. $this->setMimeType($mimeType);
  32. $this->startSession();
  33. }
  34. /**
  35. * Set the result filename
  36. *
  37. * @param string $filename
  38. */
  39. public function setFilename($filename)
  40. {
  41. $this->_filename = $filename;
  42. }
  43. /**
  44. * Set the MIME type of the result file.
  45. *
  46. * @param string $mimeType
  47. */
  48. public function setMimeType($mimeType)
  49. {
  50. $this->_mimeType = $mimeType;
  51. }
  52. /**
  53. * Start the new buffering session
  54. */
  55. public function startSession()
  56. {
  57. if ($this->_isBufferOpen == false) {
  58. //ob_end_clean();
  59. ob_start();
  60. $this->_isBufferOpen = true;
  61. }
  62. }
  63. /**
  64. * Performs the export of the file. We disable the error reporting
  65. * during the release in order to avoid useless messages in the result
  66. * file.
  67. */
  68. public function exportContentAsFile()
  69. {
  70. $oldErrorLevel = error_reporting(0);
  71. ini_set('zlib.output_compression', 0);
  72. error_reporting($oldErrorLevel);
  73. define('CFG_SEND_FILENAME', $this->_filename);
  74. define('CFG_DATE_FORMAT', 'D, d M Y H:i:s');
  75. header('Pragma: public');
  76. header('Last-Modified: ' . gmdate(CFG_DATE_FORMAT) . ' GMT');
  77. header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');
  78. header('Content-Tranfer-Encoding: none');
  79. header('Content-type: ' . $this->_mimeType);
  80. header('Content-Disposition: attachment; filename="' . CFG_SEND_FILENAME . '"');
  81. header('Date: ' . gmdate(CFG_DATE_FORMAT, time()) . ' GMT');
  82. header('Expires: ' . gmdate(CFG_DATE_FORMAT, time()+1) . ' GMT');
  83. header('Last-Modified: ' . gmdate(CFG_DATE_FORMAT, time()) . ' GMT');
  84. ob_end_flush();
  85. $this->_isBufferOpen = false;
  86. exit();
  87. }
  88. /**
  89. * Return a secure string to insert in a CSV file
  90. */
  91. public function addCellCSV($cell)
  92. {
  93. return '"'.addcslashes($cell, '"\\').'"';
  94. }
  95. /**
  96. * Set the symbol which will be used as CSV separator
  97. *
  98. * @param string $separator
  99. * @return string old value
  100. */
  101. public function setCSVSeparator($separator)
  102. {
  103. $old = $this->_csvSeparator;
  104. $this->_csvSeparator = $separator;
  105. return $old;
  106. }
  107. /**
  108. * Returns a valid and secure CSV line
  109. *
  110. * @param array $line ; array of strings (cells)
  111. * @return string
  112. */
  113. public function addLineCSV($line)
  114. {
  115. $result = "";
  116. foreach ($line as $cell) {
  117. $result .= $this->addCellCSV($cell);
  118. $result .= $this->_csvSeparator;
  119. }
  120. $result .= PHP_EOL;
  121. return $result;
  122. }
  123. }
  124. ?>