|'), array('*' => '.*', '?' => '.?')) . '$/i', $string ); } } /** * Checks if given file is allowed * * @param string $filename * @return boolean */ function isAllowedFile($filename) { if (!defined('ALLOWED_FILES')) return true; $allowedFiles = explode('|', ALLOWED_FILES); foreach ($allowedFiles as $allowedFile) { if (fnmatch($allowedFile, strtolower($filename))) { return true; } } return false; } /** * Sorts files (callback) * * @param string $file1 * @param string $file2 * @return integer */ function sortContent($file1, $file2) { $asc = '-' != substr(SORT_METHOD, -1); switch (strtolower(substr(SORT_METHOD, 0, 4))) { case 'name': return compare($asc ? $file1 : $file2, $asc ? $file2 : $file1); break; case 'date': return compare( filectime(DIRNAME . ($asc ? $file1 : $file2)), filectime(DIRNAME . ($asc ? $file2 : $file1)) ); break; } return 0; } /** * Compare function * * @param mixed $value1 * @param mixed $value2 * @return integer */ function compare($value1, $value2) { if ($value1 < $value2) { return -1; } elseif ($value1 > $value2) { return 1; } return 0; } /** * Normalizes file size * * @param integer $size * @return string */ function normalizeSize($size) { if ($size > 1048576) { return round($size / 1048576, 1) . ' MB'; } if ($size > 1024) { return round($size / 1024, 1) . ' kB'; } return $size . ' B'; } /** * Normalizes date * * @param integer $timestamp * @return string */ function normalizeDate($timestamp) { return date(DATE_FORMAT, $timestamp); } /** * Creates file data object * * @param string $fileName * @param array $content * @return stdClass */ function fileFactory($fileName, $content) { $fullPath = DIRNAME . $fileName; $file = new stdClass(); $file->name = $fileName; preg_match('/\.(\w+)$/', $fileName, $matches); $file->extension = isset($matches[1]) ? $matches[1] : false; $file->size = filesize($fullPath); $file->date = filemtime($fullPath); $file->thumbnail = "?thumb=$fileName"; if (in_array(strtolower($file->extension), array('jpg', 'jpeg', 'jpe', 'png', 'gif', 'bmp')) && $params = @getimagesize($fullPath)) { $file->isDimensional = true; $file->width = $params[0]; $file->height = $params[1]; $userThumbPath = DIRNAME . getThumbName($fileName); if (file_exists($userThumbPath)) { $file->thumbnail = getThumbName($fileName); } elseif (mkdirRecursive(DIRNAME . 'thumbs/')) { $thumbPath = DIRNAME . 'thumbs/' . getThumbName($fileName); if (file_exists($thumbPath)) { $file->thumbnail = 'thumbs/' . getThumbName($fileName); } } } else { $file->isDimensional = false; } $flipped = array_flip($content); $key = $flipped[$fileName]; $file->num = $key + 1; $file->previousFile = $key > 0 ? $content[$key - 1] : end($content); $file->nextFile = $key < count($content) - 1 ? $content[$key + 1] : reset($content); return $file; } /** * Returns thumbnail name for given file name * * @param string $fileName * @return string */ function getThumbName($fileName) { $lastDotPosition = strrpos($fileName, '.'); if (false !== $lastDotPosition) { $fileName = substr($fileName, 0, $lastDotPosition); } return THUMB_PREFIX . $fileName . '.jpg'; } /** * Recursively makes directory, returns TRUE if exists or made * * @param string $path The directory path * @param integer $mode * @return boolean TRUE if exists or made or FALSE on failure */ function mkdirRecursive($path, $mode = 0777) { $parentPath = dirname($path); if (!is_dir($parentPath) && !mkdirRecursive($parentPath, $mode)) { return false; } return is_dir($path) || (@mkdir($path, $mode) && @chmod($path, $mode)); } /** * Generates thumbnail for image * * @param string $filePath * @param string $thumbPath * @return boolean * * @todo Save only jpeg with Imagemagick * @todo Check, won't it resample small images * @todo Add GD functions result checking */ function makeThumb($filePath, $thumbPath) { if (!($imageParams = @getimagesize($filePath))) { return false; } list($imageWidth, $imageHeight) = $imageParams; $dims = explode('x', THUMB_DIMENSIONS); $thumbWidth = empty($dims[0]) ? null : (integer) $dims[0]; $thumbHeight = isset($dims[1]) && !empty($dims[1]) ? (integer) $dims[1] : null; if (!$thumbWidth && !$thumbHeight) { return copy($filePath, $thumbPath); } // Try to use Imagemagick exec("convert $filePath -thumbnail \"{$thumbWidth}x{$thumbHeight}\>\" -quality THUMB_QUALITY $thumbPath", $output, $result); if (0 === $result) { return true; } // There's nothing to do if gd isn't installed if (!extension_loaded('gd')) { return false; } $imageAspect = $imageWidth / $imageHeight; if (!$thumbWidth) { $thumbWidth = (integer) ($thumbHeight * $imageAspect); } elseif (!$thumbHeight) { $thumbHeight = (integer) ($thumbWidth / $imageAspect); } $thumbAspect = $thumbWidth / $thumbHeight; if ($thumbAspect < $imageAspect) { $srcX = (integer) (($imageWidth / $thumbAspect - $imageHeight) / 2); $srcY = 0; $srcW = (integer) ($imageHeight * $thumbAspect); $srcH = $imageHeight; } else { $srcX = 0; $srcW = $imageWidth; $srcH = (integer) ($imageWidth / $thumbAspect); $srcY = (integer) (($imageHeight - $srcH) / 2); } $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); switch ($imageParams[2]) { case IMAGETYPE_GIF: $image = imagecreatefromgif($filePath); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($filePath); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($filePath); break; default: return false; break; } imagecopyresampled($thumb, $image, 0, 0, $srcX, $srcY, $thumbWidth, $thumbHeight, $srcW, $srcH); imagedestroy($image); imagejpeg($thumb, $thumbPath, THUMB_QUALITY); imagedestroy($thumb); } /** * Recursively un-quotes a quoted variable * * @param mixed $var * @return mixed */ function stripslashes_recursive($var) { if (is_array($var)) { $unquoted = array(); foreach ($var as $key => $value) { $unquoted[$key] = stripslashes_recursive($value); } return $unquoted; } elseif (is_scalar($var)) { return stripslashes($var); } else { return $var; } } /** * Makes bounds for list columns (recursive) * * @param integer $total * @param integer $numOfColumns * @param integer $lastBound * @return array */ function makeListBounds($total, $numOfColumns, $lastBound = -1) { if ($numOfColumns > 0) { $perColumn = (integer) ceil($total / $numOfColumns); $result = makeListBounds($total - $perColumn, $numOfColumns - 1, $lastBound + $perColumn); array_unshift($result, $lastBound + $perColumn); return $result; } else { return array(); } } if (!ini_get('date.timezone')) { ini_set('date.timezone', 'UTC'); } umask(0000); error_reporting(0); //error_reporting(E_ALL); // Disable magic quotes if (version_compare('5.3.0', phpversion()) > 0) { set_magic_quotes_runtime(0); } if (get_magic_quotes_gpc()) { $_GET = stripslashes_recursive($_GET); $_REQUEST = stripslashes_recursive($_REQUEST); } // Read content in directory $content = array(); if ($handle = opendir(DIRNAME)) { while (false !== ($file = readdir($handle))) { if (substr($file, 0, 1) != '.' && is_file(DIRNAME . $file) && CURRENT_FILENAME != $file && substr($file, 0, strlen(THUMB_PREFIX)) != THUMB_PREFIX && isAllowedFile($file) ) { $content[] = $file; } } closedir($handle); } usort($content, 'sortContent'); // Route the request if (!empty($_GET)) { if (count($_GET) == 1 && '' == reset($_GET) && !isset($_GET['ext'])) { $fileName = urldecode($_SERVER['QUERY_STRING']); $file = fileFactory($fileName, $content); if (!in_array($fileName, $content) || !$file->isDimensional) { header('Location: ?'); } } elseif (isset($_GET['thumb'])) { if (!in_array($_GET['thumb'], $content)) { header('HTTP/1.1 404 Not Found'); exit(0); } $fullPath = DIRNAME . $_GET['thumb']; $thumbPath = DIRNAME . 'thumbs/' . getThumbName($_GET['thumb']); if (!file_exists($thumbPath)) { makeThumb($fullPath, $thumbPath); } header('Content-type: image/jpeg'); if (file_exists($thumbPath)) { header('Location: thumbs/' . getThumbName($_GET['thumb'])); // header('Expires: ' . date('r', time() + 60 * 60 * 24 * 365)); // 1 year cache // $handle = fopen($thumbPath, 'rb'); // echo fread($handle, filesize($thumbPath)); // fclose($handle); } else { $string = '/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAWgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAAQEBAQEBAQEBAQIBAQECAgIBAQICAgICAgICAgMCAwMDAwIDAwQEBAQEAwUFBQUFBQcHBwcHCAgICAgICAgICAEBAQECAgIFAwMFBwUEBQcICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI/8AAEQgAQABAAwERAAIRAQMRAf/EAIcAAAIBBAMBAAAAAAAAAAAAAAAHBgMECAkBAgoFAQEAAwEBAAAAAAAAAAAAAAAAAQIDBQQQAAECBAQDBQYGAgMAAAAAAAECAxEhBAUAMRIGQQcIYSITFAlRcaEyUhaBkbFCIxXwCnIkJREBAQEAAQQDAQAAAAAAAAAAAAECUREDBBQxEhMy/9oADAMBAAIRAxEAPwD36lU/0GAwh6rOr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OfIH6cAeQP04A8gfpwB5A/TgI5VMLFwuDaUqizWbIUgJ0k6nN1au6lWav4YiPdENRyGAyYp1AhJECkiRBKkkE5gmZSTxM1meWAo1IBSqMCkiCgQpSSlRyIEykngJuGWWATbiC5S7fSuKidzX0nUoKJUP7UKJ0yUoSCiO6mSE8cBIfKj6cAeVH04A8qPpwB5UfTgIbU04F/voKU6SNmaooKgR9w1ZgrTMgkDuCazAZRwD7YjGZMSVaiSCrUJKiUy1cFEST8qcBy+MzGY1GOrScoKOoZSkpXASTPAKFlo+Bt8fKPuW9AjSEAAIu2kQ4AD5UcBNUzgJj4HbgDwO3AHgduAPA7cBD61CU32tT+9Z21PXpME3WrWZ/tAAMVZgZTOAcDKYQEIQgANOnISGnhAfKngJmeAqOJJyzlCQjETEAZRHAZDM4BT0repvbsAJblvsCImRF2yKuBPEzUZ5DATvwT2/DAHgnt+GAPBPb8MAeCe34YCF19MfuVJMdL7liCYAHvM1NweGeZHDgMzgGm2mQyhwzIgT7TmCeOaj2YCqpMYgiXEf5w/XAIy432gs612uvdrrZeLJeLhX07gsF5ulM+1WmqUhSVW9rSseHVd4pX3VApIwFknmhSpJC6q7VJjBPhcud8H9GliJ98szgLxvmG1UtkJF81Ed1bexd1MGZhH/t05Hu9uA6O3y5vArp/vF3USQ2zZrXTgADL/wBGlSRDiSZZTOApG87saQA1tjfFwVLvhrYzSvyffZAjwHATOA+jY3N0Xm8WtNXsa92Rhiobfut9vlTtvSaenYqA20w3YKupUpRceHzoSCInVIJIOwD88B//2Q=='; echo base64_decode($string); } exit(0); break; } } header('Cache-Control: no-store, no-cache, must-revalidate'); header('Expires: ' . date('r')); ?> indexr

<?php echo $file->name; ?>

name; ?>

date); ?>
size); ?>
isDimensional): ?> width; ?> x height; ?>
extension == $selectedExtension)) { $files[] = $file; } if ($file->extension) { if (!isset($extensions[$file->extension])) { $extensions[$file->extension] = 0; } $extensions[$file->extension]++; } } $sorts = array( 'na' => 'return strcmp(strtolower($file1->name), strtolower($file2->name));', 'nd' => 'return -strcmp(strtolower($file1->name), strtolower($file2->name));', 'da' => '$d1 = (integer) $file1->date; $d2 = (integer) $file2->date; return ($d1 == $d2 ? 0 : ($d1 > $d2 ? 1 : -1));', 'dd' => '$d1 = (integer) $file1->date; $d2 = (integer) $file2->date; return -($d1 == $d2 ? 0 : ($d1 > $d2 ? 1 : -1));', 'ea' => 'return strcmp(strtolower($file1->extension), strtolower($file2->extension));', 'ed' => 'return -strcmp(strtolower($file1->extension), strtolower($file2->extension));', ); if (isset($_GET['sort']) && isset($sorts[$_GET['sort']])) { usort($files, create_function('$file1,$file2', $sorts[$_GET['sort']])); } ksort($extensions); ?>
thumbnail): ?> <?php echo $file->name; ?>

name; ?>

date); ?>
size); ?>
isDimensional): ?> width; ?> x height; ?>

files in folder