/system/cms/modules/files/controllers/files.php

https://github.com/genievn/pyrocms · PHP · 200 lines · 158 code · 28 blank · 14 comment · 19 complexity · 46c3671132bdce7ebc6aad243ab037a1 MD5 · raw file

  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * @package PyroCMS
  4. * @subpackage Files
  5. * @author Phil Sturgeon
  6. *
  7. * Frontend controller for files and stuffs
  8. */
  9. class Files extends Public_Controller
  10. {
  11. private $path = '';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->config->load('files');
  16. $this->_path = FCPATH . $this->config->item('files_folder') . DIRECTORY_SEPARATOR;
  17. }
  18. public function download($id = 0)
  19. {
  20. $this->load->model('file_m');
  21. $this->load->helper('download');
  22. $file = $this->file_m->get($id) OR show_404();
  23. // Read the file's contents
  24. $data = file_get_contents($this->_path . $file->filename);
  25. force_download($file->name . $file->extension , $data);
  26. }
  27. public function thumb($id, $width = 100, $height = 100, $mode = NULL)
  28. {
  29. $this->load->model('file_m');
  30. $file = $this->file_m->get($id) OR show_404();
  31. $cache_dir = $this->config->item('cache_dir') . 'image_files/';
  32. if ( ! is_dir($cache_dir))
  33. {
  34. mkdir($cache_dir, 0777, TRUE);
  35. }
  36. $modes = array('fill', 'fit');
  37. $args = func_num_args();
  38. $args = $args > 3 ? 3 : $args;
  39. $args = $args === 3 && in_array($height, $modes) ? 2 : $args;
  40. switch ($args)
  41. {
  42. case 2:
  43. if (in_array($width, $modes))
  44. {
  45. $mode = $width;
  46. $width = $height; // 100
  47. continue;
  48. }
  49. elseif (in_array($height, $modes))
  50. {
  51. $mode = $height;
  52. $height = empty($width) ? NULL : $width;
  53. }
  54. else
  55. {
  56. $height = NULL;
  57. }
  58. if ( ! empty($width))
  59. {
  60. if (($pos = strpos($width, 'x')) !== FALSE)
  61. {
  62. if ($pos === 0)
  63. {
  64. $height = substr($width, 1);
  65. $width = NULL;
  66. }
  67. else
  68. {
  69. list($width, $height) = explode('x', $width);
  70. }
  71. }
  72. }
  73. case 2:
  74. case 3:
  75. if (in_array($height, $modes))
  76. {
  77. $mode = $height;
  78. $height = empty($width) ? NULL : $width;
  79. }
  80. foreach (array('height' => 'width', 'width' => 'height') as $var1 => $var2)
  81. {
  82. if (${$var1} === 0 OR ${$var1} === '0')
  83. {
  84. ${$var1} = NULL;
  85. }
  86. elseif (empty(${$var1}) OR ${$var1} === 'auto')
  87. {
  88. ${$var1} = (empty(${$var2}) OR ${$var2} === 'auto' OR ! is_null($mode)) ? NULL : 100000;
  89. }
  90. }
  91. break;
  92. }
  93. // Path to image thumbnail
  94. $image_thumb = $cache_dir . ($mode ? $mode : 'normal');
  95. $image_thumb .= '_' . ($width === NULL ? 'a' : ($width > $file->width ? 'b' : $width));
  96. $image_thumb .= '_' . ($height === NULL ? 'a' : ($height > $file->height ? 'b' : $height));
  97. $image_thumb .= '_' . md5($file->filename) . $file->extension;
  98. $expire = 60 * Settings::get('files_cache');
  99. if ($expire)
  100. {
  101. header("Pragma: public");
  102. header("Cache-Control: public");
  103. header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
  104. }
  105. if ( ! file_exists($image_thumb) OR (filemtime($image_thumb) < filemtime($this->_path . $file->filename)))
  106. {
  107. if ($mode === $modes[1])
  108. {
  109. $ratio = $file->width / $file->height;
  110. $crop_width = $width;
  111. $crop_height = $height;
  112. if ($crop_width > $crop_height)
  113. {
  114. $width = $crop_width;
  115. $height = $crop_width / $ratio;
  116. }
  117. else
  118. {
  119. $width = $crop_height * $ratio;
  120. $height = $crop_height;
  121. }
  122. $width = ceil($width);
  123. $height = ceil($height);
  124. }
  125. // LOAD LIBRARY
  126. $this->load->library('image_lib');
  127. // CONFIGURE IMAGE LIBRARY
  128. $config['image_library'] = 'gd2';
  129. $config['source_image'] = $this->_path . $file->filename;
  130. $config['new_image'] = $image_thumb;
  131. $config['maintain_ratio'] = is_null($mode);;
  132. $config['height'] = $height;
  133. $config['width'] = $width;
  134. $this->image_lib->initialize($config);
  135. $this->image_lib->resize();
  136. $this->image_lib->clear();
  137. if ($mode === $modes[1] && ($crop_width !== NULL && $crop_height !== NULL))
  138. {
  139. $x_axis = floor(($width - $crop_width) / 2);
  140. $y_axis = floor(($height - $crop_height) / 2);
  141. // CONFIGURE IMAGE LIBRARY
  142. $config['image_library'] = 'gd2';
  143. $config['source_image'] = $image_thumb;
  144. $config['new_image'] = $image_thumb;
  145. $config['maintain_ratio'] = FALSE;
  146. $config['width'] = $crop_width;
  147. $config['height'] = $crop_height;
  148. $config['x_axis'] = $x_axis;
  149. $config['y_axis'] = $y_axis;
  150. $this->image_lib->initialize($config);
  151. $this->image_lib->crop();
  152. $this->image_lib->clear();
  153. }
  154. }
  155. else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
  156. (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($image_thumb)) &&
  157. $expire )
  158. {
  159. // Send 304 back to browser if file has not beeb changed
  160. header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($image_thumb)).' GMT', true, 304);
  161. exit();
  162. }
  163. header('Content-type: ' . $file->mimetype);
  164. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($image_thumb)) . ' GMT');
  165. readfile($image_thumb);
  166. }
  167. public function large($id, $width = NULL, $height = NULL, $mode = NULL)
  168. {
  169. return $this->thumb($id, $width, $height, $mode);
  170. }
  171. }
  172. /* End of file files.php */