Elgg  Version master
Attachment.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Email;
4 
7 
11 class Attachment extends Part {
12 
16  public function __construct($content = '') {
17  parent::__construct($content);
18 
19  $this->disposition = 'attachment';
20  $this->setId(uniqid('attachment'));
21  }
22 
39  public static function factory($options) {
40 
41  if ($options instanceof \ElggFile) {
42  return self::fromElggFile($options);
43  }
44 
45  if (!is_array($options)) {
46  elgg_log(__METHOD__ . ': $options needs to be an array', 'ERROR');
47  return false;
48  }
49 
50  if (!isset($options['content']) && !isset($options['filepath'])) {
51  elgg_log(__METHOD__ . ': $options "content" or "filepath" is required', 'ERROR');
52  return false;
53  }
54 
55  $content = elgg_extract('content', $options);
56  unset($options['content']);
57  if (!isset($content)) {
58  $filepath = elgg_extract('filepath', $options);
59  if (empty($filepath) || !is_file($filepath)) {
60  elgg_log(__METHOD__ . ': $options[filepath] didn\'t result in a valid file', 'ERROR');
61  return false;
62  }
63 
64  $content = file_get_contents($filepath);
65 
66  $options['encoding'] = Mime::ENCODING_BASE64;
67 
68  if (!isset($options['filename'])) {
69  $options['filename'] = basename($filepath);
70  }
71 
72  if (!isset($options['type'])) {
73  $options['type'] = _elgg_services()->mimetype->getMimeType($filepath);
74  }
75  }
76 
77  unset($options['filepath']);
78 
79  $attachment = new self($content);
80 
81  foreach ($options as $key => $value) {
82  $attachment->$key = $value;
83  }
84 
85  return $attachment;
86  }
87 
95  public static function fromElggFile(\ElggFile $file) {
96 
97  if (!$file->exists()) {
98  return false;
99  }
100 
101  $options = [
102  'content' => $file->grabFile(),
103  'type' => $file->getMimeType(),
104  'filename' => basename($file->getFilename()),
105  'encoding' => Mime::ENCODING_BASE64,
106  ];
107 
108  return self::factory($options);
109  }
110 }
const ENCODING_BASE64
Definition: Mime.php:47
__construct($content= '')
Definition: Attachment.php:16
exists()
Returns if the file exists.
Definition: ElggFile.php:331
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
grabFile()
Gets the full contents of this file.
Definition: ElggFile.php:214
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
getFilename()
Return the filename.
Definition: ElggFile.php:96
Email attachment.
Definition: Attachment.php:11
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
getMimeType()
Get the mime type of the file.
Definition: ElggFile.php:121
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$content
Set robots.txt action.
Definition: set_robots.php:6
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
static factory($options)
Create an attachment.
Definition: Attachment.php:39
static fromElggFile(\ElggFile $file)
Create an attachment from an ElggFile.
Definition: Attachment.php:95