private function addMedia($property, $url, $include = true, $element)
{
if ($include) {
$value = file_get_contents($url);
if (!$value) {
throw new VCardMediaException('Nothing returned from URL.');
}
$value = base64_encode($value);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, 'data://application/octet-stream;base64,' . $value);
finfo_close($finfo);
if (preg_match('/^image\\//', $mimetype) !== 1) {
throw new VCardMediaException('Returned data aren\'t an image.');
}
$type = strtoupper(str_replace('image/', '', $mimetype));
$property .= ";ENCODING=b;TYPE=" . $type;
} else {
if (filter_var($url, FILTER_VALIDATE_URL) !== FALSE) {
$propertySuffix = ';VALUE=URL';
$headers = get_headers($url);
$imageTypeMatched = false;
$fileType = null;
foreach ($headers as $header) {
if (preg_match('/Content-Type:\\simage\\/([a-z]+)/i', $header, $m)) {
$fileType = $m[1];
$imageTypeMatched = true;
}
}
if (!$imageTypeMatched) {
throw new VCardMediaException('Returned data isn\'t an image.');
}
$propertySuffix .= ';TYPE=' . strtoupper($fileType);
$property = $property . $propertySuffix;
$value = $url;
} else {
$value = $url;
}
}
$this->setProperty($element, $property, $value);
}