Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to embed images from external domains #44

Merged
merged 1 commit into from Feb 1, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -440,6 +440,107 @@ public function add_html_image($contents, $name = '', $content_type = 'applicati
public function filesystem_image_exists($filename)
{
if (defined('EMBEDEXTERNALIMAGES') && EMBEDEXTERNALIMAGES) {
// Check for a http(s) address excluding this host
if ((strpos($filename, 'http') === 0) && (strpos($filename, '://'.$_SERVER['SERVER_NAME'].'/') === FALSE)) {
$extCacheDir = $GLOBALS['tmpdir'].'/external_cache';
// Create cache directory
if (!file_exists($extCacheDir))
@mkdir($extCacheDir);
if (file_exists($extCacheDir) && is_writable($extCacheDir)) {
// Remove old files in cache directory
if ($extCacheDirHandle = @opendir($extCacheDir)) {
while (FALSE !== ($cacheFile = @readdir($extCacheDirHandle))) {
if (($cacheFile != '.') && ($cacheFile != '..')) {
$cacheFileMTime = @filemtime($extCacheDir.'/'.$cacheFile);
if (is_numeric($cacheFileMTime) && ($cacheFileMTime > 0) && ((time() - $cacheFileMTime) >= 86400)) // 1 day
@unlink($extCacheDir.'/'.$cacheFile);
}
}
@closedir($extCacheDirHandle);
}
// Generate local filename
//$cacheFile = $extCacheDir.'/'.$this->messageid.'_'.hash('sha256', $filename);
$cacheFile = $extCacheDir.'/'.$this->messageid.'_'.preg_replace(array('~[\.][\.]+~i', '~[^\w\.]~i'), array('', '_'), $filename);
// Download and cache file
if (!file_exists($cacheFile)) {
$cacheFileContent = '';
// Try downloading using cURL
if (function_exists('curl_init')) {
$cURLHandle = curl_init($filename);
if ($cURLHandle !== FALSE) {
//curl_setopt($cURLHandle, CURLOPT_URL, $filename);
curl_setopt($cURLHandle, CURLOPT_HTTPGET, TRUE);
curl_setopt($cURLHandle, CURLOPT_HEADER, 0);
curl_setopt($cURLHandle, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($cURLHandle, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($cURLHandle, CURLOPT_FILE, $cacheFileHandle);
curl_setopt($cURLHandle, CURLOPT_TIMEOUT, 30);
curl_setopt($cURLHandle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($cURLHandle, CURLOPT_MAXREDIRS, 10);
curl_setopt($cURLHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cURLHandle, CURLOPT_FAILONERROR, TRUE);
$cacheFileContent = curl_exec($cURLHandle);
$cURLErrNo = curl_errno($cURLHandle);
$cURLInfo = curl_getinfo($cURLHandle);
curl_close($cURLHandle);
if ($cURLErrNo != 0)
$cacheFileContent = 'CURL_ERROR_'.$cURLErrNo;
if ($cURLInfo['http_code'] >= 400)
$cacheFileContent = 'HTTP_CODE_'.$cURLInfo['http_code'];
}
}
// Try downloading using file_get_contents
if ($cacheFileContent == '') {
$remoteURLContext = stream_context_create(array(
'http' =>
array(
'method' => 'GET',
'timeout' => '30',
'max_redirects' => '10'
)));
$cacheFileContent = file_get_contents($filename, FALSE, $remoteURLContext);
if ($cacheFileContent === FALSE)
$cacheFileContent = 'FGC_ERROR';
}
// Limit size
if (strlen($cacheFileContent) > 1048576) // 1 MB
$cacheFileContent = 'MAX_SIZE';
// Write cache file
//file_put_contents($cacheFile, $cacheFileContent, LOCK_EX);
$cacheFileHandle = @fopen($cacheFile, 'wb');
if ($cacheFileHandle !== FALSE) {
if (flock($cacheFileHandle, LOCK_EX)) {
fwrite($cacheFileHandle, $cacheFileContent);
fflush($cacheFileHandle);
flock($cacheFileHandle, LOCK_UN);
}
fclose($cacheFileHandle);
}
}
if (file_exists($cacheFile) && (@filesize($cacheFile) > 64))
return TRUE;
}
}
}
## find the image referenced and see if it's on the server
$imageroot = getConfig('uploadimageroot');
# cl_output('filesystem_image_exists '.$docroot.' '.$filename);
@@ -469,6 +570,14 @@ public function filesystem_image_exists($filename)
public function get_filesystem_image($filename)
{
if (defined('EMBEDEXTERNALIMAGES') && EMBEDEXTERNALIMAGES) {
$extCacheDir = $GLOBALS['tmpdir'].'/external_cache';
$cacheFile = $extCacheDir.'/'.$this->messageid.'_'.preg_replace(array('~[\.][\.]+~i', '~[^\w\.]~i'), array('', '_'), $filename);
if (file_exists($cacheFile) && (@filesize($cacheFile) > 64))
return base64_encode(file_get_contents($cacheFile));
}
## get the image contents
$localfile = basename(urldecode($filename));
# cl_output('get file system image'.$filename.' '.$localfile);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.