/modules/mod_showcase/index.mod.php

https://github.com/Xirt/XirtCMS · PHP · 193 lines · 90 code · 56 blank · 47 comment · 15 complexity · 50a6a89ad2187412516175d7ec275b65 MD5 · raw file

  1. <?php
  2. /**
  3. * Shows a collection of images using Slimbox (a Lightbox clone).
  4. *
  5. * @author A.G. Gideonse
  6. * @version 2.0
  7. * @copyright XirtCMS 2010 - 2014
  8. * @package XirtCMS
  9. */
  10. class mod_showcase extends XModule {
  11. /**
  12. * The path for thumbnail caching
  13. * @var String
  14. */
  15. const PATH_CACHE = "cache/mod_showcase/";
  16. /**
  17. * Handles any normal requests
  18. */
  19. function showNormal() {
  20. // Include plugin
  21. XInclude::plugin("slimbox");
  22. // Create cache
  23. $cache = new XDir(self::PATH_CACHE);
  24. $cache->create();
  25. // Show template
  26. $tpl = new XTemplate($this->_location());
  27. $tpl->assign("id", rand());
  28. $tpl->assign("xConf", $this->xConf);
  29. $tpl->assign("images", $this->_getImageList());
  30. $tpl->display("templates/template.tpl");
  31. }
  32. /**
  33. * Returns a list with all thumbnail / original combinations
  34. *
  35. * @return Array with key/value paris of thumbnails/originals
  36. */
  37. private function _getImageList() {
  38. $list = array();
  39. $extensions = explode(",", $this->xConf->ext);
  40. foreach ($this->_getImages() as $file) {
  41. // Check for existing thumbnails
  42. if ($this->xConf->prefix_thumb) {
  43. // Postfix of image / thunbmail
  44. $postfix = substr($file, strlen($this->xConf->prefix_ori));
  45. $thumbnail = $this->xConf->prefix_thumb . $postfix;
  46. // Check for existing thumbnail (same extension)
  47. if (file_exists($this->xConf->folder . $thumbnail)) {
  48. $list[$this->xConf->folder . $thumbnail] = $file;
  49. continue;
  50. }
  51. // Otherwhise look for alternative
  52. $extension = substr($postfix, -3);
  53. $name = substr($postfix, 0, -3);
  54. foreach ($extensions as $ext) {
  55. $thumbnail = $this->xConf->prefix_thumb . $name . $extension;
  56. if (file_exists($this->xConf->folder . $thumbnail)) {
  57. $list[$this->xConf->folder . $thumbnail] = $file;
  58. continue 2;
  59. }
  60. }
  61. }
  62. // Or create a new thumbnail
  63. $list[$this->_createThumbnail($this->xConf->folder . $file)] = $file;
  64. }
  65. return $list;
  66. }
  67. /**
  68. * Creates a thumbnail (if non existent) and returns its path
  69. *
  70. * @param $image String with the path to the image
  71. * @return String The path to the thumbnail
  72. */
  73. private function _createThumbnail($image) {
  74. $item = sprintf("%s.jpg", md5($image));
  75. $cached = self::PATH_CACHE . $item;
  76. if (!file_exists($cached)) {
  77. if (!$size = abs($this->xConf->thumb_size)) {
  78. $size = 100;
  79. }
  80. // Create thumbnail
  81. $img = new XImage();
  82. $img->load($image);
  83. $img->resize($size, $size);
  84. $img->save($cached);
  85. // Chmod thumbnail
  86. $img = new XFile(self::PATH_CACHE, $item);
  87. $img->chmod();
  88. }
  89. return $cached;
  90. }
  91. /**
  92. * Returns all images for this instance
  93. *
  94. * @return Array containing names of the requested images
  95. */
  96. private function _getImages() {
  97. global $xConf;
  98. $list = array();
  99. $extensions = explode(",", $this->xConf->ext);
  100. $path = XConfig::get("SESSION_DIR_BASE") . $this->xConf->folder;
  101. if ($dh = opendir($path)) {
  102. while (false !== ($file = readdir($dh))) {
  103. // Skip system paths
  104. if (in_array($file, array(".", ".."))) {
  105. continue;
  106. }
  107. // Check for required thumbnail prefix
  108. if ($this->xConf->prefix_thumb) {
  109. if (strpos($file, $this->xConf->prefix_thumb) === 0) {
  110. continue;
  111. }
  112. }
  113. // Check for required original prefix
  114. if ($this->xConf->prefix_ori) {
  115. if (strpos($file, $this->xConf->prefix_ori) !== 0) {
  116. continue;
  117. }
  118. }
  119. // Check for required extension
  120. $ext = pathinfo($path . $file, PATHINFO_EXTENSION);
  121. if (!in_array($ext, $extensions)) {
  122. continue;
  123. }
  124. $list[] = $file;
  125. }
  126. @closedir($dh);
  127. }
  128. // Limited lists must be shuffled
  129. if (count($list) > $this->xConf->amount && shuffle($list)) {
  130. return array_slice($list, 0, $this->xConf->amount);
  131. }
  132. // Otherwise return sorted
  133. sort($list);
  134. return $list;
  135. }
  136. }
  137. ?>