Elgg  Version 3.0
Attachment.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Email;
4 
8 
12 class Attachment extends Part {
13 
25  public function __construct($content = '') {
26  parent::__construct($content);
27 
28  $this->disposition = 'attachment';
29  $this->setId(uniqid('attachment'));
30  }
31 
48  public static function factory($options) {
49 
50  if ($options instanceof \ElggFile) {
51  return self::fromElggFile($options);
52  }
53 
54  if (!is_array($options)) {
55  elgg_log(__METHOD__ . ': $options needs to be an array', 'ERROR');
56  return false;
57  }
58 
59  if (!isset($options['content']) && !isset($options['filepath'])) {
60  elgg_log(__METHOD__ . ': $options "content" or "filepath" is required', 'ERROR');
61  return false;
62  }
63 
64  $content = elgg_extract('content', $options);
65  unset($options['content']);
66  if (!isset($content)) {
67  $filepath = elgg_extract('filepath', $options);
68  if (empty($filepath) || !is_file($filepath)) {
69  elgg_log(__METHOD__ . ': $options[filepath] didn\'t result in a valid file', 'ERROR');
70  return false;
71  }
72 
73  $content = file_get_contents($filepath);
74 
75  $options['encoding'] = Mime::ENCODING_BASE64;
76 
77  if (!isset($options['filename'])) {
78  $options['filename'] = basename($filepath);
79  }
80 
81  if (!isset($options['type'])) {
82  $options['type'] = (new MimeTypeDetector())->tryStrategies($filepath);
83  }
84  }
85 
86  unset($options['filepath']);
87 
88  $attachment = new self($content);
89 
90  foreach ($options as $key => $value) {
91  $attachment->$key = $value;
92  }
93 
94  return $attachment;
95  }
96 
104  public static function fromElggFile(\ElggFile $file) {
105 
106  if (!$file->exists()) {
107  return false;
108  }
109 
110  $options = [
111  'content' => $file->grabFile(),
112  'type' => $file->getMimeType(),
113  'filename' => basename($file->getFilename()),
114  'encoding' => Mime::ENCODING_BASE64,
115  ];
116 
117  return self::factory($options);
118  }
119 }
__construct($content= '')
create a new Mime Part.
Definition: Attachment.php:25
if(!array_key_exists($filename, $text_files)) $file
exists()
Returns if the file exists.
Definition: ElggFile.php:349
$options
Elgg admin footer.
Definition: footer.php:6
grabFile()
Gets the full contents of this file.
Definition: ElggFile.php:246
getFilename()
Return the filename.
Definition: ElggFile.php:67
Email attachment.
Definition: Attachment.php:12
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
getMimeType()
Get the mime type of the file.
Definition: ElggFile.php:103
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
$value
Definition: debugging.php:7
Detect the MIME type of a file.
$content
Set robots.txt action.
Definition: set_robots.php:6
static factory($options)
Create an attachment.
Definition: Attachment.php:48
static fromElggFile(\ElggFile $file)
Create an attachment from an ElggFile.
Definition: Attachment.php:104