/** * Main file of the online gallery. This script is used to browse folders and downloadable files. * Links are created on specified files (ZIP, RAR, ...) using images (PNG, GIF, ...) to display them. * * @version 1.0 * @author Patrick Janser * @copyright Cadwork Informatique Lausanne **/ define('DEBUG_ECHO_ON', true); // Name of the directory containing all the PHP, HTML sources and images. // This value can be changed if you need to change the directory's name. define('SOURCE_DIRECTORY', 'http_site'); // Do not change these 3 constants. define('ROOT_RELATIVE', './'); define('ROOT_ABSOLUTE', dirname(__FILE__) . '/'); $PHP_SELF = basename(__FILE__); // The $PHP_SELF constant is not always available. Create it ourself. // Template manipulation. require_once(ROOT_ABSOLUTE . SOURCE_DIRECTORY . '/template.php'); // Several usefull functions (like getFiles, echoArray, ...). require_once(ROOT_ABSOLUTE . SOURCE_DIRECTORY . '/tools.php'); //------------------------------------------- CONSTANTS -----------------------------------------------// // The directories to ignore. $IGNORED_DIRECTORIES = array(SOURCE_DIRECTORY, 'aspnet_client'); // Title of the page. $TITLE = '
"' . htmlentities($relativePath); $errorContent .= "\" is an invalid path ! Back to home directory...
\n"; $relativePath = ''; } } // Create absolute path. $absolutePath = ROOT_ABSOLUTE . $relativePath; // Check if the number of columns of the main table has been sent by POST (via FORM and option list). if (($nbrColumns = getRequest('cols')) != '') { if (!is_scalar($nbrColumns) || $nbrColumns < $MIN_NBR_COLUMNS || $nbrColumns > $MAX_NBR_COLUMNS) { $errorContent .= "Number of columns is invalid !
\n"; $nbrColumns = $DEFAULT_NBR_COLUMNS; } } else $nbrColumns = $DEFAULT_NBR_COLUMNS; // Check if there is a file to download. $forceDownloadFile = getRequest('file'); //------------------------------- GENERATE BROWSABLE PATH + OPTION LIST -------------------------------// // Generate path browser. $browsablePath .= '' . htmlBrowsablePath($relativePath, $PHP_SELF, "cols=$nbrColumns") . "
\n"; // Generate option list for selection of the number of columns to display. $optionColumns = ''; for ($i = $MIN_NBR_COLUMNS; $i <= $MAX_NBR_COLUMNS; $i++) { if ($i == $nbrColumns) $optionColumns .= str_repeat(' ', 16) . "\n"; else $optionColumns .= str_repeat(' ', 16) . "\n"; } //------------------------------------ GENERATE THE LIST OF DIRECTORIES -------------------------------// // Get the directories. $directories = getDirectories($absolutePath); // Delete the "http_site" directory containing the php script because it musn't be listed. // Only delete it if we are at the root directory. //if ($relativePath == "") { foreach ($IGNORED_DIRECTORIES as $directory) { // Search for the current directory to ignore get its corresponding key. $key = array_search($directory, $directories); // Delete element from array of directories. if (!($key === false)) unset($directories[$key]); } //} // If there is some directories, then create links to them. if (count($directories) > 0) { // Define a variable contained in another. // There are many folderRow items in folder. The append result is folderRows. $t->set_var('folderRows', ''); $t->set_block('folder', 'folderRow', 'folderRows'); // Create links on all the directories. foreach ($directories as $directory) { // Create link href. $folderHref = "$PHP_SELF?path="; if ($relativePath == '') $folderHref .= rawurlencode($directory); else $folderHref .= rawurlencode($relativePath . '/' . $directory); $folderHref .= "&cols=$nbrColumns"; $folderTitle = htmlentities($directory); // Replace spaces by an invisibile dot. This will suppress automatic new line formatting. $folderTitle = str_replace(' ', ' ', $folderTitle); // Set vars and replace values in the folder template. $t->set_var(array('folderHref' => $folderHref, 'folderLinkTitle' => htmlentities($directory), 'folderTitle' => $folderTitle, 'sourceDirectory' => SOURCE_DIRECTORY)); $t->parse('folderRows', 'folderRow', true); // True is to append data into folderRows. } // Parse and add result to page content. $folderSubDirs .= $t->parse('folders', 'folder'); } $folderSubDirs .= "\n"; //-------------------------------- GET THE LIST OF DOWNLOADABLE FILES ---------------------------------// // Get all the downloadable files and create the links. $downloadFiles = getSpecificFiles($absolutePath, $DOWNLOADABLE_EXTENSIONS); //-------------------------- SEND THE FILE TO THE CLIENT IF IT CORRESPONDS ----------------------------// if ($forceDownloadFile != '') { // This loop is to prevent hackers to download PHP files, or other critical files. (Not optimal) foreach ($downloadFiles as $downloadFile) { if (strcasecmp($forceDownloadFile, $downloadFile) == 0) { if ($relativePath == '') $downloadFilePath = $absolutePath . $downloadFile; else $downloadFilePath = $absolutePath . '/' . $downloadFile; if (sendFile($downloadFilePath)) exit(); else exit('Error while sending file !'); } } } //----------------------------- GENERATE THE THUMBNAILS IN A HTML TABLE -------------------------------// if (count($downloadFiles) > 0) { // Define a variable contained in another. // There are many thumbnailRow items in thumbnail. The append result is thumbnailRows. $t->set_var('thumbnailRows', ''); $t->set_block('thumbnail', 'thumbnailRow', 'thumbnailRows'); // Create links on all the downloadable files. foreach ($downloadFiles as $downloadFile) { // Create link to force the download of the file. $downloadHref = "$PHP_SELF?path=" . rawurlencode($relativePath); $downloadHref .= "&file=" . rawurlencode($downloadFile); $downloadHref .= "&cols=$nbrColumns"; // Search for image thumbnails. $downloadFileInfos = pathinfo($downloadFile); foreach ($THUMBNAIL_EXTENSIONS as $thumbnailExtension) { $thumbnailFilenames[] = substr($downloadFile, 0, strlen($downloadFile) - strlen($downloadFileInfos['extension']) - 1) . "." . $thumbnailExtension; } $images = getSpecificFiles($absolutePath, $THUMBNAIL_EXTENSIONS); foreach ($thumbnailFilenames as $thumbnailFilename) { foreach ($images as $image) { if (strcasecmp($thumbnailFilename, $image) == 0) { if ($relativePath == '') $thumbnailSrc[] = urlEncodeWithoutSlashes($relativePath . $thumbnailFilename); else $thumbnailSrc[] = urlEncodeWithoutSlashes($relativePath . '/' . $thumbnailFilename); } } } // If no image found, then use Zip image. if (!isset($thumbnailSrc)) { $thumbnailSrc[] = SOURCE_DIRECTORY . "/images/zip_80x80.gif"; } // Set vars and replace values in the folder template. $t->set_var(array('downloadHref' => $downloadHref, 'downloadLinkTitle' => htmlentities(str_replace('_', ' ', $downloadFile)), 'thumbnailSrc' => $thumbnailSrc[0], 'thumbnailWidth' => $THUMBNAIL_WIDTH, 'thumbnailHeight' => $THUMBNAIL_HEIGHT)); $thumbnails[] = $t->parse('thumbnailRows', 'thumbnailRow', true); // True is to append data into folderRows. // Unset variable for next image research. unset($thumbnailSrc); unset($thumbnailFilenames); } // Parse and add result to page content. $folderContent .= htmlTable($thumbnails, // array of elements to be put in the cells. $nbrColumns, // number of columns. '', // No CSS class name. 'border="0" cellspacing="8" cellpadding="0" align="left"', // TABLE tag properties. 'height="140"', // TR tag properties. 'width="110" align="center" valign="top"', // TD tag properties. ''); // Nothing to be put in the empty cells. } // No download files are available in the current directory. else { $folderContent .= $EMPTY_FOLDER; } //---------------------------- INSERT DYNAMIC DATA FROM PHP INTO HTML TEMPLATES -----------------------// // Set the variables wich we will insert in the template. $t->set_var(array('title' => $TITLE, 'browsablePath' => $browsablePath, 'relativePath' => $relativePath, 'sourceDirectory' => SOURCE_DIRECTORY, 'optionColumns' => $optionColumns, 'errorContent' => $errorContent, 'folderSubDirs' => $folderSubDirs, 'folderContent' => $folderContent, 'footer' => $footer)); // Replace all variables in the main page. $t->pparse('output', 'mainPage'); ?>