/include/class.attachment.php

https://gitlab.com/billyprice1/osTicket · PHP · 213 lines · 153 code · 35 blank · 25 comment · 23 complexity · 245b6d9011cb0fd96b4d866b1a9f02dc MD5 · raw file

  1. <?php
  2. /*********************************************************************
  3. class.attachment.php
  4. Attachment Handler - mainly used for lookup...doesn't save!
  5. Peter Rotich <peter@osticket.com>
  6. Copyright (c) 2006-2013 osTicket
  7. http://www.osticket.com
  8. Released under the GNU General Public License WITHOUT ANY WARRANTY.
  9. See LICENSE.TXT for details.
  10. vim: expandtab sw=4 ts=4 sts=4:
  11. **********************************************************************/
  12. require_once(INCLUDE_DIR.'class.ticket.php');
  13. require_once(INCLUDE_DIR.'class.file.php');
  14. class Attachment extends VerySimpleModel {
  15. static $meta = array(
  16. 'table' => ATTACHMENT_TABLE,
  17. 'pk' => array('id'),
  18. 'select_related' => array('file'),
  19. 'joins' => array(
  20. 'draft' => array(
  21. 'constraint' => array(
  22. 'type' => "'D'",
  23. 'object_id' => 'Draft.id',
  24. ),
  25. ),
  26. 'file' => array(
  27. 'constraint' => array(
  28. 'file_id' => 'AttachmentFile.id',
  29. ),
  30. ),
  31. 'thread_entry' => array(
  32. 'constraint' => array(
  33. 'type' => "'H'",
  34. 'object_id' => 'ThreadEntry.id',
  35. ),
  36. ),
  37. ),
  38. );
  39. var $object;
  40. function getId() {
  41. return $this->id;
  42. }
  43. function getFileId() {
  44. return $this->file_id;
  45. }
  46. function getFile() {
  47. return $this->file;
  48. }
  49. function getFilename() {
  50. return $this->name ?: $this->file->name;
  51. }
  52. function getHashtable() {
  53. return $this->ht;
  54. }
  55. function getInfo() {
  56. return $this->getHashtable();
  57. }
  58. function getObject() {
  59. if (!isset($this->object))
  60. $this->object = ObjectModel::lookup(
  61. $this->ht['object_id'], $this->ht['type']);
  62. return $this->object;
  63. }
  64. static function lookupByFileHash($hash, $objectId=0) {
  65. $file = static::objects()
  66. ->filter(array('file__key' => $hash));
  67. if ($objectId)
  68. $file->filter(array('object_id' => $objectId));
  69. return $file->first();
  70. }
  71. static function lookup($var, $objectId=0) {
  72. return (is_string($var))
  73. ? static::lookupByFileHash($var, $objectId)
  74. : parent::lookup($var);
  75. }
  76. }
  77. class GenericAttachments
  78. extends InstrumentedList {
  79. var $lang;
  80. function getId() { return $this->key['object_id']; }
  81. function getType() { return $this->key['type']; }
  82. /**
  83. * Drop attachments whose file_id values are not in the included list,
  84. * additionally, add new files whose IDs are in the list provided.
  85. */
  86. function keepOnlyFileIds($ids, $inline=false, $lang=false) {
  87. if (!$ids) $ids = array();
  88. $new = array_fill_keys($ids, 1);
  89. foreach ($this as $A) {
  90. if (!isset($new[$A->file_id]) && $A->lang == $lang && $A->inline == $inline)
  91. // Not in the $ids list, delete
  92. $this->remove($A);
  93. unset($new[$A->file_id]);
  94. }
  95. // Everything remaining in $new is truly new
  96. $this->upload(array_keys($new), $inline, $lang);
  97. }
  98. function upload($files, $inline=false, $lang=false) {
  99. $i=array();
  100. if (!is_array($files))
  101. $files = array($files);
  102. foreach ($files as $file) {
  103. if (is_numeric($file))
  104. $fileId = $file;
  105. elseif (is_array($file) && isset($file['id']))
  106. $fileId = $file['id'];
  107. elseif (isset($file['tmp_name']) && ($F = AttachmentFile::upload($file)))
  108. $fileId = $F->getId();
  109. elseif ($F = AttachmentFile::create($file))
  110. $fileId = $F->getId();
  111. else
  112. continue;
  113. $_inline = isset($file['inline']) ? $file['inline'] : $inline;
  114. $att = $this->add(new Attachment(array(
  115. 'file_id' => $fileId,
  116. 'inline' => $_inline ? 1 : 0,
  117. )));
  118. // Record varying file names in the attachment record
  119. if (is_array($file) && isset($file['name'])) {
  120. $filename = $file['name'];
  121. }
  122. if ($filename) {
  123. // This should be a noop since the ORM caches on PK
  124. $file = $F ?: AttachmentFile::lookup($fileId);
  125. // XXX: This is not Unicode safe
  126. if ($file && 0 !== strcasecmp($file->name, $filename))
  127. $att->name = $filename;
  128. }
  129. if ($lang)
  130. $att->lang = $lang;
  131. // File may already be associated with the draft (in the
  132. // event it was deleted and re-added)
  133. $att->save();
  134. $i[] = $fileId;
  135. }
  136. return $i;
  137. }
  138. function save($file, $inline=true) {
  139. $ids = $this->upload($file, $inline);
  140. return $ids[0];
  141. }
  142. function getInlines($lang=false) { return $this->_getList(false, true, $lang); }
  143. function getSeparates($lang=false) { return $this->_getList(true, false, $lang); }
  144. function getAll($lang=false) { return $this->_getList(true, true, $lang); }
  145. function count($lang=false) { return count($this->getSeparates($lang)); }
  146. function _getList($separates=false, $inlines=false, $lang=false) {
  147. $base = $this;
  148. if ($separates && !$inlines)
  149. $base = $base->filter(array('inline' => 0));
  150. elseif (!$separates && $inlines)
  151. $base = $base->filter(array('inline' => 1));
  152. if ($lang)
  153. $base = $base->filter(array('lang' => $lang));
  154. return $base;
  155. }
  156. function delete($file_id) {
  157. return $this->objects()->filter(array('file_id'=>$file_id))->delete();
  158. }
  159. function deleteAll($inline_only=false){
  160. if ($inline_only)
  161. return $this->objects()->filter(array('inline' => 1))->delete();
  162. return parent::expunge();
  163. }
  164. function deleteInlines() {
  165. return $this->deleteAll(true);
  166. }
  167. static function forIdAndType($id, $type) {
  168. return new static(array(
  169. 'Attachment',
  170. array('object_id' => $id, 'type' => $type)
  171. ));
  172. }
  173. }
  174. ?>