ormdocument.class.inc.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * ormDocument
  20. * encapsulate the behavior of a binary data set that will be stored an attribute of class AttributeBlob
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. /**
  26. * ormDocument
  27. * encapsulate the behavior of a binary data set that will be stored an attribute of class AttributeBlob
  28. *
  29. * @package itopORM
  30. */
  31. class ormDocument
  32. {
  33. protected $m_data;
  34. protected $m_sMimeType;
  35. protected $m_sFileName;
  36. /**
  37. * Constructor
  38. */
  39. public function __construct($data = null, $sMimeType = 'text/plain', $sFileName = '')
  40. {
  41. $this->m_data = $data;
  42. $this->m_sMimeType = $sMimeType;
  43. $this->m_sFileName = $sFileName;
  44. }
  45. public function __toString()
  46. {
  47. return MyHelpers::beautifulstr($this->m_data, 100, true);
  48. }
  49. public function IsEmpty()
  50. {
  51. return ($this->m_data == null);
  52. }
  53. public function GetMimeType()
  54. {
  55. return $this->m_sMimeType;
  56. }
  57. public function GetMainMimeType()
  58. {
  59. $iSeparatorPos = strpos($this->m_sMimeType, '/');
  60. if ($iSeparatorPos > 0)
  61. {
  62. return substr($this->m_sMimeType, 0, $iSeparatorPos);
  63. }
  64. return $this->m_sMimeType;
  65. }
  66. public function GetData()
  67. {
  68. return $this->m_data;
  69. }
  70. public function GetFileName()
  71. {
  72. return $this->m_sFileName;
  73. }
  74. public function GetAsHTML()
  75. {
  76. $sResult = '';
  77. if ($this->IsEmpty())
  78. {
  79. // If the filename is not empty, display it, this is used
  80. // by the creation wizard while the file has not yet been uploaded
  81. $sResult = htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8');
  82. }
  83. else
  84. {
  85. $data = $this->GetData();
  86. $sResult = htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8').' [ '.$this->GetMimeType().', size: '.strlen($data).' byte(s) ]<br/>';
  87. }
  88. return $sResult;
  89. }
  90. /**
  91. * Returns an hyperlink to display the document *inline*
  92. * @return string
  93. */
  94. public function GetDisplayLink($sClass, $Id, $sAttCode)
  95. {
  96. return "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" target=\"_blank\" >".htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8')."</a>\n";
  97. }
  98. /**
  99. * Returns an hyperlink to download the document (content-disposition: attachment)
  100. * @return string
  101. */
  102. public function GetDownloadLink($sClass, $Id, $sAttCode)
  103. {
  104. return "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode\">".htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8')."</a>\n";
  105. }
  106. /**
  107. * Returns an URL to download a document like an image (uses HTTP caching)
  108. * @return string
  109. */
  110. public function GetDownloadURL($sClass, $Id, $sAttCode)
  111. {
  112. // Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files)
  113. $sSignature = md5($this->GetData());
  114. return utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode&s=$sSignature&cache=86400";
  115. }
  116. public function IsPreviewAvailable()
  117. {
  118. $bRet = false;
  119. switch($this->GetMimeType())
  120. {
  121. case 'image/png':
  122. case 'image/jpg':
  123. case 'image/jpeg':
  124. case 'image/gif':
  125. $bRet = true;
  126. break;
  127. }
  128. return $bRet;
  129. }
  130. }
  131. ?>