PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/data/wpcom-themes/vigilance/images/sidebar/rotate.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 206 lines | 54 code | 20 blank | 132 comment | 11 complexity | d5d4742fcebf966507d96059ea439553 MD5 | raw file
  1. <?php
  2. /*
  3. AUTOMATIC IMAGE ROTATOR
  4. Version 2.2 - December 4, 2003
  5. Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd.
  6. All Rights Reserved.
  7. http://www.hiveware.com/imagerotator.php
  8. http://www.automaticlabs.com/
  9. DISCLAIMER
  10. Automatic, Ltd. makes no representations or warranties about
  11. the suitability of the software, either express or
  12. implied, including but not limited to the implied
  13. warranties of merchantability, fitness for a particular
  14. purpose, or non-infringement. Dan P. Benjamin and Automatic, Ltd.
  15. shall not be liable for any damages suffered by licensee
  16. as a result of using, modifying or distributing this
  17. software or its derivatives.
  18. ABOUT
  19. This PHP script will randomly select an image file from a
  20. folder of images on your webserver. You can then link to it
  21. as you would any standard image file and you'll see a random
  22. image each time you reload.
  23. When you want to add or remove images from the rotation-pool,
  24. just add or remove them from the image rotation folder.
  25. VERSION CHANGES
  26. Version 1.0
  27. - Release version
  28. Version 1.5
  29. - Tweaked a few boring bugs
  30. Version 2.0
  31. - Complete rewrite from the ground-up
  32. - Made it clearer where to make modifications
  33. - Made it easier to specify/change the rotation-folder
  34. - Made it easier to specify/change supported image types
  35. - Wrote better instructions and info (you're them reading now)
  36. - Significant speed improvements
  37. - More error checking
  38. - Cleaner code (albeit more PHP-specific)
  39. - Better/faster random number generation and file-type parsing
  40. - Added a feature where the image to display can be specified
  41. - Added a cool feature where, if an error occurs (such as no
  42. images being found in the specified folder) *and* you're
  43. lucky enough to have the GD libraries compiled into PHP on
  44. your webserver, we generate a replacement "error image" on
  45. the fly.
  46. Version 2.1
  47. - Updated a potential security flaw when value-matching
  48. filenames
  49. Version 2.2
  50. - Updated a few more potential security issues
  51. - Optimized the code a bit.
  52. - Expanded the doc for adding new mime/image types.
  53. Thanks to faithful ALA reader Justin Greer for
  54. lots of good tips and solid code contribution!
  55. INSTRUCTIONS
  56. 1. Modify the $folder setting in the configuration section below.
  57. 2. Add image types if needed (most users can ignore that part).
  58. 3. Upload this file (rotate.php) to your webserver. I recommend
  59. uploading it to the same folder as your images.
  60. 4. Link to the file as you would any normal image file, like this:
  61. <img src="http://example.com/rotate.php">
  62. 5. You can also specify the image to display like this:
  63. <img src="http://example.com/rotate.php?img=gorilla.jpg">
  64. This would specify that an image named "gorilla.jpg" located
  65. in the image-rotation folder should be displayed.
  66. That's it, you're done.
  67. */
  68. /* ------------------------- CONFIGURATION -----------------------
  69. Set $folder to the full path to the location of your images.
  70. For example: $folder = '/user/me/example.com/images/';
  71. If the rotate.php file will be in the same folder as your
  72. images then you should leave it set to $folder = '.';
  73. */
  74. $folder = '.';
  75. /*
  76. Most users can safely ignore this part. If you're a programmer,
  77. keep reading, if not, you're done. Go get some coffee.
  78. If you'd like to enable additional image types other than
  79. gif, jpg, and png, add a duplicate line to the section below
  80. for the new image type.
  81. Add the new file-type, single-quoted, inside brackets.
  82. Add the mime-type to be sent to the browser, also single-quoted,
  83. after the equal sign.
  84. For example:
  85. PDF Files:
  86. $extList['pdf'] = 'application/pdf';
  87. CSS Files:
  88. $extList['css'] = 'text/css';
  89. You can even serve up random HTML files:
  90. $extList['html'] = 'text/html';
  91. $extList['htm'] = 'text/html';
  92. Just be sure your mime-type definition is correct!
  93. */
  94. $extList = array();
  95. $extList['gif'] = 'image/gif';
  96. $extList['jpg'] = 'image/jpeg';
  97. $extList['jpeg'] = 'image/jpeg';
  98. $extList['png'] = 'image/png';
  99. // You don't need to edit anything after this point.
  100. // --------------------- END CONFIGURATION -----------------------
  101. $img = null;
  102. if (substr($folder,-1) != '/') {
  103. $folder = $folder.'/';
  104. }
  105. if (isset($_GET['img'])) {
  106. $imageInfo = pathinfo($_GET['img']);
  107. if (
  108. isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  109. file_exists( $folder.$imageInfo['basename'] )
  110. ) {
  111. $img = $folder.$imageInfo['basename'];
  112. }
  113. } else {
  114. $fileList = array();
  115. $handle = opendir($folder);
  116. while ( false !== ( $file = readdir($handle) ) ) {
  117. $file_info = pathinfo($file);
  118. if (
  119. isset( $extList[ strtolower( $file_info['extension'] ) ] )
  120. ) {
  121. $fileList[] = $file;
  122. }
  123. }
  124. closedir($handle);
  125. if (count($fileList) > 0) {
  126. $imageNumber = time() % count($fileList);
  127. $img = $folder.$fileList[$imageNumber];
  128. }
  129. }
  130. if ($img!=null) {
  131. $imageInfo = pathinfo($img);
  132. $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  133. header ($contentType);
  134. readfile($img);
  135. } else {
  136. if ( function_exists('imagecreate') ) {
  137. header ("Content-type: image/png");
  138. $im = @imagecreate (100, 100)
  139. or die ("Cannot initialize new GD image stream");
  140. $background_color = imagecolorallocate ($im, 255, 255, 255);
  141. $text_color = imagecolorallocate ($im, 0,0,0);
  142. imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
  143. imagepng ($im);
  144. imagedestroy($im);
  145. }
  146. }
  147. ?>