ormdocument.class.inc.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // Copyright (C) 2010-2016 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-2016 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.document.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 display a document like an image
  108. * @return string
  109. */
  110. public function GetDisplayURL($sClass, $Id, $sAttCode)
  111. {
  112. return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode";
  113. }
  114. /**
  115. * Returns an URL to download a document like an image (uses HTTP caching)
  116. * @return string
  117. */
  118. public function GetDownloadURL($sClass, $Id, $sAttCode)
  119. {
  120. // Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files)
  121. $sSignature = md5($this->GetData());
  122. return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.document.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode&s=$sSignature&cache=86400";
  123. }
  124. public function IsPreviewAvailable()
  125. {
  126. $bRet = false;
  127. switch($this->GetMimeType())
  128. {
  129. case 'image/png':
  130. case 'image/jpg':
  131. case 'image/jpeg':
  132. case 'image/gif':
  133. $bRet = true;
  134. break;
  135. }
  136. return $bRet;
  137. }
  138. /**
  139. * Downloads a document to the browser, either as 'inline' or 'attachment'
  140. *
  141. * @param WebPage $oPage The web page for the output
  142. * @param string $sClass Class name of the object
  143. * @param mixed $id Identifier of the object
  144. * @param string $sAttCode Name of the attribute containing the document to download
  145. * @param string $sContentDisposition Either 'inline' or 'attachment'
  146. * @param string $sSecretField The attcode of the field containing a "secret" to be provided in order to retrieve the file
  147. * @param string $sSecretValue The value of the secret to be compared with the value of the attribute $sSecretField
  148. * @return none
  149. */
  150. public static function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment', $sSecretField = null, $sSecretValue = null)
  151. {
  152. try
  153. {
  154. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  155. if (!is_object($oObj))
  156. {
  157. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  158. }
  159. if (($sSecretField != null) && ($oObj->Get($sSecretField) != $sSecretValue))
  160. {
  161. usleep(200);
  162. throw new Exception("Invalid secret for class '$sClass' - the object does not exist or you are not allowed to view it");
  163. }
  164. $oDocument = $oObj->Get($sAttCode);
  165. if (is_object($oDocument))
  166. {
  167. $oPage->TrashUnexpectedOutput();
  168. $oPage->SetContentType($oDocument->GetMimeType());
  169. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  170. $oPage->add($oDocument->GetData());
  171. }
  172. }
  173. catch(Exception $e)
  174. {
  175. $oPage->p($e->getMessage());
  176. }
  177. }
  178. }