PageRenderTime 388ms CodeModel.GetById 102ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/phpthumb/modphpthumb.class.php

https://github.com/francisreboucas/revolution
PHP | 204 lines | 154 code | 16 blank | 34 comment | 12 complexity | 6d0dadf5f75de2c45f969c81d97277b7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package modx
  4. * @subpackage phpthumb
  5. */
  6. require_once MODX_CORE_PATH.'model/phpthumb/phpthumb.class.php';
  7. /**
  8. * Helper class to extend phpThumb and simplify thumbnail generation process
  9. * since phpThumb class is overly convoluted and doesn't do enough.
  10. *
  11. * @package modx
  12. * @subpackage phpthumb
  13. */
  14. class modPhpThumb extends phpThumb {
  15. function __construct(modX &$modx,array $config = array()) {
  16. $this->modx =& $modx;
  17. $this->config = array_merge(array(
  18. ),$config);
  19. parent::__construct();
  20. }
  21. /**
  22. * Setup some site-wide phpthumb options from modx config
  23. */
  24. public function initialize() {
  25. $cachePath = $this->modx->getOption('core_path',null,MODX_CORE_PATH).'cache/phpthumb/';
  26. if (!is_dir($cachePath)) $this->modx->cacheManager->writeTree($cachePath);
  27. $this->setParameter('config_cache_directory',$cachePath);
  28. $this->setCacheDirectory();
  29. $this->setParameter('config_allow_src_above_docroot',(boolean)$this->modx->getOption('phpthumb_allow_src_above_docroot',$this->config,false));
  30. $this->setParameter('config_cache_maxage',(float)$this->modx->getOption('phpthumb_cache_maxage',$this->config,30) * 86400);
  31. $this->setParameter('config_cache_maxsize',(float)$this->modx->getOption('phpthumb_cache_maxsize',$this->config,100) * 1024 * 1024);
  32. $this->setParameter('config_cache_maxfiles',(int)$this->modx->getOption('phpthumb_cache_maxfiles',$this->config,10000));
  33. $this->setParameter('config_error_bgcolor',(string)$this->modx->getOption('phpthumb_error_bgcolor',$this->config,'CCCCFF'));
  34. $this->setParameter('config_error_textcolor',(string)$this->modx->getOption('phpthumb_error_textcolor',$this->config,'FF0000'));
  35. $this->setParameter('config_error_fontsize',(int)$this->modx->getOption('phpthumb_error_fontsize',$this->config,1));
  36. $this->setParameter('config_nohotlink_enabled',(boolean)$this->modx->getOption('phpthumb_nohotlink_enabled',$this->config,true));
  37. $this->setParameter('config_nohotlink_valid_domains',explode(',', $this->modx->getOption('phpthumb_nohotlink_valid_domains',$this->config,$this->modx->getOption('http_host'))));
  38. $this->setParameter('config_nohotlink_erase_image',(boolean)$this->modx->getOption('phpthumb_nohotlink_erase_image',$this->config,true));
  39. $this->setParameter('config_nohotlink_text_message',(string)$this->modx->getOption('phpthumb_nohotlink_text_message',$this->config,'Off-server thumbnailing is not allowed'));
  40. $this->setParameter('config_nooffsitelink_enabled',(boolean)$this->modx->getOption('phpthumb_nooffsitelink_enabled',$this->config,false));
  41. $this->setParameter('config_nooffsitelink_valid_domains',explode(',', $this->modx->getOption('phpthumb_nooffsitelink_valid_domains',$this->config,$this->modx->getOption('http_host'))));
  42. $this->setParameter('config_nooffsitelink_require_refer',(boolean)$this->modx->getOption('phpthumb_nooffsitelink_require_refer',$this->config,false));
  43. $this->setParameter('config_nooffsitelink_erase_image',(boolean)$this->modx->getOption('phpthumb_nooffsitelink_erase_image',$this->config,true));
  44. $this->setParameter('config_nooffsitelink_watermark_src',(string)$this->modx->getOption('phpthumb_nooffsitelink_watermark_src',$this->config,''));
  45. $this->setParameter('config_nooffsitelink_text_message',(string)$this->modx->getOption('phpthumb_nooffsitelink_text_message',$this->config,'Off-server linking is not allowed'));
  46. $this->setParameter('cache_source_enabled',(boolean)$this->modx->getOption('phpthumb_cache_source_enabled',$this->config,false));
  47. $this->setParameter('cache_source_directory',$cachePath.'source/');
  48. $this->setParameter('allow_local_http_src',true);
  49. $this->setParameter('zc',$this->modx->getOption('zc',$_REQUEST,$this->modx->getOption('phpthumb_zoomcrop',$this->config,0)));
  50. $this->setParameter('far',$this->modx->getOption('far',$_REQUEST,$this->modx->getOption('phpthumb_far',$this->config,'C')));
  51. $this->setParameter('cache_directory_depth',4);
  52. $documentRoot = $this->modx->getOption('phpthumb_document_root',$this->config,'');
  53. if (!empty($documentRoot)) {
  54. $this->setParameter('config_document_root',$documentRoot);
  55. }
  56. /* iterate through properties */
  57. foreach ($this->config as $property => $value) {
  58. $this->setParameter($property,$value);
  59. }
  60. return true;
  61. }
  62. /**
  63. * Sets the source image
  64. */
  65. public function set($src) {
  66. $src = rawurldecode($src);
  67. if (empty($src)) return '';
  68. return $this->setSourceFilename($src);
  69. }
  70. /**
  71. * Check to see if cached file already exists
  72. */
  73. public function checkForCachedFile() {
  74. $this->setCacheFilename();
  75. if (file_exists($this->cache_filename) && is_readable($this->cache_filename)) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * Load cached file
  82. */
  83. public function loadCache() {
  84. $this->RedirectToCachedFile();
  85. }
  86. /**
  87. * Cache the generated thumbnail.
  88. */
  89. public function cache() {
  90. phpthumb_functions::EnsureDirectoryExists(dirname($this->cache_filename));
  91. if ((file_exists($this->cache_filename) && is_writable($this->cache_filename)) || is_writable(dirname($this->cache_filename))) {
  92. $this->CleanUpCacheDirectory();
  93. if ($this->RenderToFile($this->cache_filename) && is_readable($this->cache_filename)) {
  94. chmod($this->cache_filename, 0644);
  95. $this->RedirectToCachedFile();
  96. }
  97. }
  98. }
  99. /**
  100. * Generate a thumbnail
  101. */
  102. public function generate() {
  103. if (!$this->GenerateThumbnail()) {
  104. $this->modx->log(modX::LOG_LEVEL_ERROR,'phpThumb was unable to generate a thumbnail for: '.$this->cache_filename);
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. * Output a thumbnail.
  111. */
  112. public function output() {
  113. $output = $this->OutputThumbnail();
  114. if (!$output) {
  115. $this->modx->log(modx::LOG_LEVEL_ERROR,'Error outputting thumbnail:'."\n".$this->debugmessages[(count($this->debugmessages) - 1)]);
  116. }
  117. return $output;
  118. }
  119. /** PHPTHUMB HELPER METHODS **/
  120. public function RedirectToCachedFile() {
  121. $nice_cachefile = str_replace(DIRECTORY_SEPARATOR, '/', $this->cache_filename);
  122. $nice_docroot = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($this->config_document_root, '/\\'));
  123. $parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  124. $nModified = filemtime($this->cache_filename);
  125. if ($this->config_nooffsitelink_enabled && @$_SERVER['HTTP_REFERER'] && !in_array(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) {
  126. $this->DebugMessage('Would have used cached (image/'.$this->thumbnailFormat.') file "'.$this->cache_filename.'" (Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT), but skipping because $_SERVER[HTTP_REFERER] ('.@$_SERVER['HTTP_REFERER'].') is not in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  127. } elseif ($this->phpThumbDebug) {
  128. $this->DebugTimingMessage('skipped using cached image', __FILE__, __LINE__);
  129. $this->DebugMessage('Would have used cached file, but skipping due to phpThumbDebug', __FILE__, __LINE__);
  130. $this->DebugMessage('* Would have sent headers (1): Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT', __FILE__, __LINE__);
  131. $getimagesize = @GetImageSize($this->cache_filename);
  132. if ($getimagesize) {
  133. $this->DebugMessage('* Would have sent headers (2): Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]), __FILE__, __LINE__);
  134. }
  135. if (ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  136. $this->DebugMessage('* Would have sent headers (3): Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])), __FILE__, __LINE__);
  137. } else {
  138. $this->DebugMessage('* Would have sent data: readfile('.$this->cache_filename.')', __FILE__, __LINE__);
  139. }
  140. } else {
  141. /*
  142. if (headers_sent()) {
  143. $this->ErrorImage('Headers already sent ('.basename(__FILE__).' line '.__LINE__.')');
  144. exit;
  145. }*/
  146. $this->SendSaveAsFileHeaderIfNeeded();
  147. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT');
  148. if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && ($nModified == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && @$_SERVER['SERVER_PROTOCOL']) {
  149. header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
  150. exit;
  151. }
  152. $getimagesize = @GetImageSize($this->cache_filename);
  153. if ($getimagesize) {
  154. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]));
  155. } elseif (eregi('\.ico$', $this->cache_filename)) {
  156. header('Content-Type: image/x-icon');
  157. }
  158. if (!$this->config_cache_force_passthru && ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  159. header('Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])));
  160. } else {
  161. @readfile($this->cache_filename);
  162. }
  163. exit;
  164. }
  165. return true;
  166. }
  167. public function SendSaveAsFileHeaderIfNeeded() {
  168. if (headers_sent()) {
  169. return false;
  170. }
  171. $downloadfilename = phpthumb_functions::SanitizeFilename(@$_GET['sia'] ? $_GET['sia'] : (@$_GET['down'] ? $_GET['down'] : 'phpThumb_generated_thumbnail'.(@$_GET['f'] ? $_GET['f'] : 'jpg')));
  172. if (@$downloadfilename) {
  173. $this->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: '.(@$_GET['down'] ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"', __FILE__, __LINE__);
  174. header('Content-Disposition: '.(@$_GET['down'] ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  175. }
  176. return true;
  177. }
  178. }