| 1 | <?php |
| 2 | /** |
| 3 | * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved. |
| 4 | * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 |
| 5 | * @version 2011.7 |
| 6 | * @package kernel |
| 7 | */ |
| 8 | |
| 9 | define( 'TABLE_METADATA', 'ezdfsfile' ); |
| 10 | |
| 11 | function _die( $value ) |
| 12 | { |
| 13 | header( $_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error" ); |
| 14 | die( $value ); |
| 15 | } |
| 16 | |
| 17 | // Storage database connection string |
| 18 | /*if ( defined( 'STORAGE_SOCKET' ) && STORAGE_SOCKET !== false ) |
| 19 | $serverString = STORAGE_SOCKET; |
| 20 | elseif ( defined( 'STORAGE_PORT' ) ) |
| 21 | $serverString = STORAGE_HOST . ':' . STORAGE_PORT; |
| 22 | else |
| 23 | $serverString = STORAGE_HOST;*/ |
| 24 | if ( !defined( 'STORAGE_PORT' ) ) |
| 25 | { |
| 26 | define( 'STORAGE_PORT', '' ); |
| 27 | } |
| 28 | if ( !defined( 'STORAGE_SOCKET' ) ) |
| 29 | { |
| 30 | define( 'STORAGE_SOCKET', '' ); |
| 31 | } |
| 32 | |
| 33 | $maxTries = 3; |
| 34 | $tries = 0; |
| 35 | while ( $tries < $maxTries ) |
| 36 | { |
| 37 | /// @todo test if using socket does work |
| 38 | if ( $db = mysqli_connect( STORAGE_HOST, STORAGE_USER, STORAGE_PASS, STORAGE_DB, STORAGE_PORT, STORAGE_SOCKET ) ) |
| 39 | break; |
| 40 | ++$tries; |
| 41 | } |
| 42 | if ( $tries > $maxTries ) |
| 43 | { |
| 44 | _die( "Unable to connect to database server.\n" ); |
| 45 | } |
| 46 | if ( !$db ) |
| 47 | _die( "Unable to connect to storage server" ); |
| 48 | |
| 49 | if ( !mysqli_set_charset( $db, defined( 'STORAGE_CHARSET' ) ? STORAGE_CHARSET : 'utf8' ) ) |
| 50 | _die( "Failed to set character set.\n" ); |
| 51 | |
| 52 | $filename = ltrim( $_SERVER['REQUEST_URI'], '/'); |
| 53 | if ( ( $queryPos = strpos( $filename, '?' ) ) !== false ) |
| 54 | $filename = substr( $filename, 0, $queryPos ); |
| 55 | |
| 56 | // Fetch file metadata. |
| 57 | $filePathHash = md5( $filename ); |
| 58 | $sql = "SELECT * FROM " . TABLE_METADATA . " WHERE name_hash=('$filePathHash')" ; |
| 59 | if ( !$res = mysqli_query( $db, $sql ) ) |
| 60 | _die( "Failed to retrieve file metadata\n" ); |
| 61 | |
| 62 | if ( !( $metaData = mysqli_fetch_assoc( $res ) ) || |
| 63 | $metaData['mtime'] < 0 ) |
| 64 | { |
| 65 | header( $_SERVER['SERVER_PROTOCOL'] . " 404 Not Found" ); |
| 66 | ?> |
| 67 | <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
| 68 | <HTML><HEAD> |
| 69 | <TITLE>404 Not Found</TITLE> |
| 70 | </HEAD><BODY> |
| 71 | <H1>Not Found</H1> |
| 72 | The requested URL <?php echo htmlspecialchars( $filename ); ?> was not found on this server.<P> |
| 73 | </BODY></HTML> |
| 74 | <?php |
| 75 | mysqli_close( $db ); |
| 76 | exit( 1 ); |
| 77 | } |
| 78 | |
| 79 | mysqli_free_result( $res ); |
| 80 | |
| 81 | // Fetch file data. |
| 82 | $dfsFilePath = MOUNT_POINT_PATH . '/' . $filename; |
| 83 | // Set cache time out to 100 minutes by default |
| 84 | $expiry = defined( 'EXPIRY_TIMEOUT' ) ? EXPIRY_TIMEOUT : 6000; |
| 85 | if ( file_exists( $dfsFilePath ) ) |
| 86 | { |
| 87 | // Output HTTP headers. |
| 88 | $path = $metaData['name']; |
| 89 | $size = $metaData['size']; |
| 90 | $mimeType = $metaData['datatype']; |
| 91 | $mtime = $metaData['mtime']; |
| 92 | $mdate = gmdate( 'D, d M Y H:i:s', $mtime ) . ' GMT'; |
| 93 | |
| 94 | header( "Content-Length: $size" ); |
| 95 | header( "Content-Type: $mimeType" ); |
| 96 | header( "Last-Modified: $mdate" ); |
| 97 | header( "Expires: " . gmdate('D, d M Y H:i:s', time() + $expiry) . ' GMT' ); |
| 98 | header( "Connection: close" ); |
| 99 | header( "X-Powered-By: eZ Publish" ); |
| 100 | header( "Accept-Ranges: none" ); |
| 101 | header( 'Served-by: ' . $_SERVER["SERVER_NAME"] ); |
| 102 | |
| 103 | // Output image data. |
| 104 | $fp = fopen( $dfsFilePath, 'r' ); |
| 105 | fpassthru( $fp ); |
| 106 | fclose( $fp ); |
| 107 | } |
| 108 | ?> |
| 109 | |