ormdocument.class.inc.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if($this->IsEmpty()) return '';
  48. return MyHelpers::beautifulstr($this->m_data, 100, true);
  49. }
  50. public function IsEmpty()
  51. {
  52. return ($this->m_data == null);
  53. }
  54. public function GetMimeType()
  55. {
  56. return $this->m_sMimeType;
  57. }
  58. public function GetMainMimeType()
  59. {
  60. $iSeparatorPos = strpos($this->m_sMimeType, '/');
  61. if ($iSeparatorPos > 0)
  62. {
  63. return substr($this->m_sMimeType, 0, $iSeparatorPos);
  64. }
  65. return $this->m_sMimeType;
  66. }
  67. public function GetData()
  68. {
  69. return $this->m_data;
  70. }
  71. public function GetFileName()
  72. {
  73. return $this->m_sFileName;
  74. }
  75. public function GetAsHTML()
  76. {
  77. $sResult = '';
  78. if ($this->IsEmpty())
  79. {
  80. // If the filename is not empty, display it, this is used
  81. // by the creation wizard while the file has not yet been uploaded
  82. $sResult = htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8');
  83. }
  84. else
  85. {
  86. $data = $this->GetData();
  87. $sResult = htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8').' [ '.$this->GetMimeType().', size: '.strlen($data).' byte(s) ]<br/>';
  88. }
  89. return $sResult;
  90. }
  91. /**
  92. * Returns an hyperlink to display the document *inline*
  93. * @return string
  94. */
  95. public function GetDisplayLink($sClass, $Id, $sAttCode)
  96. {
  97. 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";
  98. }
  99. /**
  100. * Returns an hyperlink to download the document (content-disposition: attachment)
  101. * @return string
  102. */
  103. public function GetDownloadLink($sClass, $Id, $sAttCode)
  104. {
  105. 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";
  106. }
  107. /**
  108. * Returns an URL to display a document like an image
  109. * @return string
  110. */
  111. public function GetDisplayURL($sClass, $Id, $sAttCode)
  112. {
  113. return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode";
  114. }
  115. /**
  116. * Returns an URL to download a document like an image (uses HTTP caching)
  117. * @return string
  118. */
  119. public function GetDownloadURL($sClass, $Id, $sAttCode)
  120. {
  121. // Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files)
  122. $sSignature = md5($this->GetData());
  123. return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.document.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode&s=$sSignature&cache=86400";
  124. }
  125. public function IsPreviewAvailable()
  126. {
  127. $bRet = false;
  128. switch($this->GetMimeType())
  129. {
  130. case 'image/png':
  131. case 'image/jpg':
  132. case 'image/jpeg':
  133. case 'image/gif':
  134. $bRet = true;
  135. break;
  136. }
  137. return $bRet;
  138. }
  139. /**
  140. * Downloads a document to the browser, either as 'inline' or 'attachment'
  141. *
  142. * @param WebPage $oPage The web page for the output
  143. * @param string $sClass Class name of the object
  144. * @param mixed $id Identifier of the object
  145. * @param string $sAttCode Name of the attribute containing the document to download
  146. * @param string $sContentDisposition Either 'inline' or 'attachment'
  147. * @param string $sSecretField The attcode of the field containing a "secret" to be provided in order to retrieve the file
  148. * @param string $sSecretValue The value of the secret to be compared with the value of the attribute $sSecretField
  149. * @return none
  150. */
  151. public static function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment', $sSecretField = null, $sSecretValue = null)
  152. {
  153. try
  154. {
  155. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  156. if (!is_object($oObj))
  157. {
  158. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  159. }
  160. if (($sSecretField != null) && ($oObj->Get($sSecretField) != $sSecretValue))
  161. {
  162. usleep(200);
  163. throw new Exception("Invalid secret for class '$sClass' - the object does not exist or you are not allowed to view it");
  164. }
  165. $oDocument = $oObj->Get($sAttCode);
  166. if (is_object($oDocument))
  167. {
  168. $oPage->TrashUnexpectedOutput();
  169. $oPage->SetContentType($oDocument->GetMimeType());
  170. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  171. $oPage->add($oDocument->GetData());
  172. }
  173. }
  174. catch(Exception $e)
  175. {
  176. $oPage->p($e->getMessage());
  177. }
  178. }
  179. }