diff options
| author | Michael Walker <mike@barrucadu.co.uk> | 2016-08-20 01:15:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-20 01:15:27 +0100 |
| commit | 0678b74b7afed7eade5415d2b2cd4cd619c4bc89 (patch) | |
| tree | c2a831e1fb0a132581d12a5a00477e29e6a4e5d2 /b.php | |
| parent | 42988fe8efd89071608e2239ad1868dfe12362a2 (diff) | |
| parent | 5cf54dbce03632fd7fe76d39247edf4542e18475 (diff) | |
Merge pull request #58 from lainchan/57-priority-banners
Priority Banners (issue #57)
Diffstat (limited to 'b.php')
| -rwxr-xr-x | b.php | 58 |
1 files changed, 42 insertions, 16 deletions
@@ -1,21 +1,47 @@ <?php -//renaming to b.php -$dir = "banners/"; -$files = scandir($dir); -$images = array_diff($files, array('.', '..')); -$name = $images[array_rand($images)]; +// This script assumes there is at least one normal (non-priority) +// banner! -// snags the extension -$img_extension = pathinfo($name, PATHINFO_EXTENSION); +// Get the files in a directory, returns null if the directory does +// not exist. +function getFilesInDirectory($dir) { + if (! is_dir($dir)) { + return null; + } -// send the right headers -header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1 -header('Pragma: no-cache'); // HTTP 1.0 -header('Expires: 0'); // Proxies -header("Content-type: image/" . $img_extension); -header("Content-Disposition: inline; filename=" . $name); + return array_diff(scandir($dir), array('.', '..')); +} -// readfile displays the image, passthru seems to spits stream. -readfile($dir.$name); -exit; +// Serve a random banner and exit. +function serveRandomBanner($dir, $files) { + $name = $files[array_rand($files)]; + + // snags the extension + $ext = pathinfo($name, PATHINFO_EXTENSION); + + // send the right headers + header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1 + header('Pragma: no-cache'); // HTTP 1.0 + header('Expires: 0'); // Proxies + header("Content-type: image/" . ext); + header("Content-Disposition: inline; filename=" . $name); + + // readfile displays the image, passthru seems to spits stream. + readfile($dir.$name); + exit; +} + +// Get all the banners +$bannerDir = "banners/"; +$priorityDir = "banners_priority/"; + +$banners = getFilesInDirectory($bannerDir); +$priority = getFilesInDirectory($priorityDir); + +// If there are priority banners, serve 1/3rd of the time. +if($priority !== null && count($priority) !== 0 && rand(0,2) === 0) { + serveRandomBanner($priorityDir, $priority); +} + +serveRandomBanner($bannerDir, $banners); ?> |
