plugins/gallery/fetcher.php
author Dan Fuhry <dan@enanocms.org>
Sat, 21 Aug 2010 23:51:10 -0400
changeset 44 17cfc3da0dcc
parent 42 7c6e2e97aa08
permissions -rw-r--r--
Put a noscript around that
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     1
<?php
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     2
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     3
/*
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     4
 * Snapr
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     5
 * Version 0.1 beta 1
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     6
 * Copyright (C) 2007 Dan Fuhry
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     7
 *
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    10
 *
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    13
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    14
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    15
##
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    16
## IMAGE FILE FETCHER
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    17
##
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    18
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    19
$plugins->attachHook('base_classes_initted', '
42
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    20
	global $paths;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    21
		$paths->add_page(Array(
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    22
			\'name\'=>\'Image fetcher pagelet\',
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    23
			\'urlname\'=>\'GalleryFetcher\',
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    24
			\'namespace\'=>\'Special\',
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    25
			\'special\'=>0,\'visible\'=>0,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    26
			));
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    27
	');
0
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    28
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    29
function page_Special_GalleryFetcher()
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    30
{
42
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    31
	global $db, $session, $paths, $template, $plugins; // Common objects
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    32
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    33
	// artificial race condition for debug
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    34
	// sleep(5);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    35
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    36
	$type = $paths->getParam(0);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    37
	if ( !in_array($type, array('thumb', 'preview', 'full', 'embed')) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    38
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    39
		die('Hack attempt');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    40
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    41
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    42
	$id = intval($paths->getParam(1));
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    43
	if ( !$id )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    44
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    45
		die('Hack attempt');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    46
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    47
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    48
	// Permissions object
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    49
	$perms = $session->fetch_page_acl($id, 'Gallery');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    50
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    51
	if ( !$perms->get_permissions('gal_full_res') && $type == 'full' )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    52
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    53
		$type = 'preview';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    54
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    55
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    56
	while ( true )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    57
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    58
		$q = $db->sql_query('SELECT img_title, img_filename, img_time_mod, is_folder, processed FROM '.table_prefix.'gallery WHERE img_id=' . $id . ';');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    59
		if ( !$q )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    60
			$db->_die();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    61
		
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    62
		if ( $db->numrows() < 1 )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    63
			die('Image not found');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    64
		
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    65
		$row = $db->fetchrow();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    66
		if ( $row['processed'] == 1 || $type == 'full' )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    67
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    68
		sleep(1);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    69
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    70
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    71
	switch ( $type )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    72
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    73
		case 'thumb':
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    74
			$filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . '-thumb.jpg';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    75
			$mimetype = 'image/jpeg';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    76
			$ext = "jpg";
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    77
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    78
		case 'preview':
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    79
			$filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . '-preview.jpg';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    80
			$mimetype = 'image/jpeg';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    81
			$ext = "jpg";
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    82
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    83
		case 'full':
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    84
			$filename = ENANO_ROOT . '/files/' . $row['img_filename'];
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    85
			$ext = get_file_extension($filename);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    86
			switch($ext)
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    87
			{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    88
				case 'png': $mimetype = 'image/png'; break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    89
				case 'gif': $mimetype = 'image/gif'; break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    90
				case 'bmp': $mimetype = 'image/bmp'; break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    91
				case 'jpg': case 'jpeg': $mimetype = 'image/jpeg'; break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    92
				case 'tif': case 'tiff': $mimetype = 'image/tiff'; break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    93
				default: $mimetype = 'application/octet-stream';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    94
			}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    95
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    96
		case 'embed':
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    97
			if ( !isset($_GET['width']) || !isset($_GET['height']) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    98
			{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
    99
				die('Missing width or height.');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   100
			}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   101
			$width = intval($_GET['width']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   102
			$height = intval($_GET['height']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   103
			if ( empty($width) || empty($height) || $width > 2048 || $height > 2048 )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   104
			{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   105
				die('Bad width or height');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   106
			}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   107
			
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   108
			$ext = get_file_extension($row['img_filename']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   109
			
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   110
			$src_filename  = ENANO_ROOT . '/files/' . $row['img_filename'];
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   111
			$dest_filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . "-embed-$width-$height.$ext";
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   112
			$filename =& $dest_filename;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   113
			
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   114
			if ( !file_exists($dest_filename) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   115
			{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   116
				if ( !scale_image($src_filename, $dest_filename, $width, $height, false) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   117
				{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   118
					die('Image scaling process failed.');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   119
				}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   120
			}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   121
			
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   122
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   123
		default:
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   124
			die('PHP...insane...');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   125
			break;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   126
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   127
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   128
	// Make sure we have permission to read this image
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   129
	if ( !$perms->get_permissions('read') )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   130
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   131
		$filename = ENANO_ROOT . '/plugins/gallery/denied.png';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   132
		$mimetype = 'image/png';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   133
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   134
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   135
	if ( $row['is_folder'] == '1' )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   136
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   137
		$filename = ENANO_ROOT . '/plugins/gallery/folder.png';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   138
		$mimetype = 'image/png';
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   139
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   140
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   141
	if ( !file_exists($filename) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   142
		die('Can\'t retrieve image file ' . $filename);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   143
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   144
	$contents = file_get_contents($filename);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   145
	// expire images 30 days from now
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   146
	$expiry = time() + ( 30 * 86400 );
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   147
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   148
	header('Content-type: '   . $mimetype);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   149
	header('Content-length: ' . strlen($contents));
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   150
	header('Last-Modified: '  . date('r', $row['img_time_mod']));
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   151
	header('Expires: ' . date('r', $expiry));
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   152
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   153
	// check for not-modified condition
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   154
	if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   155
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   156
		$time = @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   157
		if ( ( !empty($time) && intval($row['img_time_mod']) <= $time ) || date('r', $row['img_time_mod']) === $_SERVER['HTTP_IF_MODIFIED_SINCE'] )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   158
		{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   159
			header('HTTP/1.1 304 Not Modified');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   160
			$db->close();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   161
			exit;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   162
		}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   163
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   164
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   165
	if ( isset($_GET['download']) )
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   166
	{
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   167
		// determine an appropriate non-revealing filename
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   168
		$filename = str_replace(' ', '_', $row['img_title']);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   169
		$filename = preg_replace('/([^\w\._-]+)/', '-', $filename);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   170
		$filename = trim($filename, '-');
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   171
		$filename .= ".$ext";
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   172
		header('Content-disposition: attachment; filename=' . $filename);
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   173
	}
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   174
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   175
	echo $contents;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   176
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   177
	gzip_output();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   178
	
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   179
	$db->close();
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   180
	exit;
7c6e2e97aa08 Added AJAX file upload support.
Dan Fuhry <dan@enanocms.org>
parents: 30
diff changeset
   181
	
0
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   182
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   183
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   184
?>