Version 4.1.19
This commit is contained in:
1 parent
28bd025b35
commit
2bd5025018
2674 files changed
+233518
-77766
No files matched your search
@@ -2,31 +2,110 @@
|
||||
/**
|
||||
* @brief Image Proxy
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 29 Jun 2015
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
/* Init */
|
||||
require_once str_replace( 'applications/core/interface/imageproxy/imageproxy.php', '', str_replace( '\\', '/', __FILE__ ) ) . 'init.php';
|
||||
$url = \IPS\Request::i()->img;
|
||||
|
||||
$image = urldecode( \IPS\Request::i()->img );
|
||||
|
||||
if ( \IPS\Request::i()->key != hash_hmac( "sha256", $image, md5( \IPS\Settings::i()->sql_pass . \IPS\Settings::i()->board_url . \IPS\Settings::i()->sql_database ) ) )
|
||||
/* Check for a valid key */
|
||||
if ( !\IPS\Login::compareHashes( hash_hmac( "sha256", $url, \IPS\Settings::i()->site_secret_key ), (string) \IPS\Request::i()->key ) )
|
||||
{
|
||||
\IPS\Output::i()->sendOutput( NULL, 500 );
|
||||
}
|
||||
|
||||
$output = ( string ) \IPS\Http\Url::external( $image )->request()->get();
|
||||
|
||||
/* Check the cache */
|
||||
try
|
||||
{
|
||||
$cacheEntry = \IPS\Db::i()->select( '*', 'core_image_proxy', array( 'md5_url=?', md5( $url ) ) )->first();
|
||||
|
||||
/* If we have a cache entry, but it is over an hour old and the location is NULL, try to refetch */
|
||||
if( $cacheEntry['location'] === NULL AND $cacheEntry['cache_time'] < time() - 3600 )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_image_proxy', array( 'md5_url=?', $cacheEntry['md5_url'] ) );
|
||||
throw new \UnderflowException;
|
||||
}
|
||||
|
||||
if ( $cacheEntry )
|
||||
{
|
||||
/* Set the cache expiration time */
|
||||
$cacheExpires = new \DateTime; // Use of \DateTime is intentional, do not replace with \IPS\DateTime
|
||||
$cacheExpires->setTimestamp( (int) $cacheEntry['cache_time'] );
|
||||
$cacheExpires->add( new \DateInterval( sprintf( 'P%dD', \IPS\Settings::i()->image_proxy_cache_period ) ) );
|
||||
|
||||
$file = \IPS\File::get( 'core_Imageproxycache', $cacheEntry['location'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Output::i()->sendOutput( NULL, 500 );
|
||||
}
|
||||
}
|
||||
|
||||
/* Not in cache - fetch and store */
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
/* Set the cache expiration time */
|
||||
$cacheExpires = new \DateTime; // Use of \DateTime is intentional, do not replace with \IPS\DateTime
|
||||
$cacheExpires->add( new \DateInterval( sprintf( 'P%dD', \IPS\Settings::i()->image_proxy_cache_period ) ) );
|
||||
|
||||
/* First, let's store a placeholder row that we will later update - this prevents being able to DOS the server if the image is crazy */
|
||||
\IPS\Db::i()->insert( 'core_image_proxy', array(
|
||||
'md5_url' => md5( $url ),
|
||||
'location' => NULL,
|
||||
'cache_time' => time(),
|
||||
) );
|
||||
|
||||
try
|
||||
{
|
||||
$output = \IPS\Http\Url::external( mb_substr( $url, 0, 2 ) === '//' ? "http:{$url}" : $url )->request()->get();
|
||||
/* Check it's a valid image */
|
||||
$image = \IPS\Image::create( (string) $output );
|
||||
unset( $image );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
\IPS\Output::i()->sendOutput( NULL, 500 );
|
||||
}
|
||||
|
||||
/* Work out an appropriate filename */
|
||||
$extension = mb_substr( $url, mb_strrpos( $url, '.' ) + 1 );
|
||||
if ( in_array( $extension, \IPS\Image::$imageExtensions ) )
|
||||
{
|
||||
$filename = mb_substr( $url, mb_strrpos( $url, '/' ) + 1 );
|
||||
if ( mb_strlen( $filename ) > 200 )
|
||||
{
|
||||
$filename = mb_substr( $filename, 0, 150 ) . '.' . $extension;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$filename = md5( uniqid() ) . '.' . $image->type;
|
||||
}
|
||||
|
||||
/* Cache */
|
||||
$file = \IPS\File::create( 'core_Imageproxycache', $filename, (string) $output, 'imageproxy' );
|
||||
\IPS\Db::i()->replace( 'core_image_proxy', array(
|
||||
'md5_url' => md5( $url ),
|
||||
'location' => (string) $file,
|
||||
'cache_time' => time(),
|
||||
) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
\IPS\Image::create( $output );
|
||||
/* Output */
|
||||
\IPS\Output::i()->sendOutput( $file->contents(), 200, \IPS\File::getMimeType( $file->filename ), array(
|
||||
'cache-control' => 'public, max_age=' . max( ( $cacheExpires->getTimestamp() - time() ), 0 ) . ', must-revalidate',
|
||||
'expires' => $cacheExpires->format( 'D, d M Y H:i:s \G\M\T' )
|
||||
) );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( \RuntimeException $e )
|
||||
{
|
||||
\IPS\Output::i()->sendOutput( NULL, 500 );
|
||||
}
|
||||
|
||||
\IPS\Output::i()->sendOutput( $output, 200, 'image' );
|
||||
\IPS\Log::debug( "Failed fetching proxy image", 'imageProxy' );
|
||||
}
|
||||
Reference in new issue
Block a user