PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 205 lines | 155 code | 16 blank | 34 comment | 12 complexity | 716575af18d69c39eddc56f865cdca06 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. $this->setParameter('config_ttf_directory',$this->modx->getOption('core_path',$this->config,MODX_CORE_PATH).'model/phpthumb/fonts/');
  53. $documentRoot = $this->modx->getOption('phpthumb_document_root',$this->config,'');
  54. if (!empty($documentRoot)) {
  55. $this->setParameter('config_document_root',$documentRoot);
  56. }
  57. /* iterate through properties */
  58. foreach ($this->config as $property => $value) {
  59. $this->setParameter($property,$value);
  60. }
  61. return true;
  62. }
  63. /**
  64. * Sets the source image
  65. */
  66. public function set($src) {
  67. $src = rawurldecode($src);
  68. if (empty($src)) return '';
  69. return $this->setSourceFilename($src);
  70. }
  71. /**
  72. * Check to see if cached file already exists
  73. */
  74. public function checkForCachedFile() {
  75. $this->setCacheFilename();
  76. if (file_exists($this->cache_filename) && is_readable($this->cache_filename)) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Load cached file
  83. */
  84. public function loadCache() {
  85. $this->RedirectToCachedFile();
  86. }
  87. /**
  88. * Cache the generated thumbnail.
  89. */
  90. public function cache() {
  91. phpthumb_functions::EnsureDirectoryExists(dirname($this->cache_filename));
  92. if ((file_exists($this->cache_filename) && is_writable($this->cache_filename)) || is_writable(dirname($this->cache_filename))) {
  93. $this->CleanUpCacheDirectory();
  94. if ($this->RenderToFile($this->cache_filename) && is_readable($this->cache_filename)) {
  95. chmod($this->cache_filename, 0644);
  96. $this->RedirectToCachedFile();
  97. }
  98. }
  99. }
  100. /**
  101. * Generate a thumbnail
  102. */
  103. public function generate() {
  104. if (!$this->GenerateThumbnail()) {
  105. $this->modx->log(modX::LOG_LEVEL_ERROR,'phpThumb was unable to generate a thumbnail for: '.$this->cache_filename);
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * Output a thumbnail.
  112. */
  113. public function output() {
  114. $output = $this->OutputThumbnail();
  115. if (!$output) {
  116. $this->modx->log(modx::LOG_LEVEL_ERROR,'Error outputting thumbnail:'."\n".$this->debugmessages[(count($this->debugmessages) - 1)]);
  117. }
  118. return $output;
  119. }
  120. /** PHPTHUMB HELPER METHODS **/
  121. public function RedirectToCachedFile() {
  122. $nice_cachefile = str_replace(DIRECTORY_SEPARATOR, '/', $this->cache_filename);
  123. $nice_docroot = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($this->config_document_root, '/\\'));
  124. $parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  125. $nModified = filemtime($this->cache_filename);
  126. if ($this->config_nooffsitelink_enabled && @$_SERVER['HTTP_REFERER'] && !in_array(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) {
  127. $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__);
  128. } elseif ($this->phpThumbDebug) {
  129. $this->DebugTimingMessage('skipped using cached image', __FILE__, __LINE__);
  130. $this->DebugMessage('Would have used cached file, but skipping due to phpThumbDebug', __FILE__, __LINE__);
  131. $this->DebugMessage('* Would have sent headers (1): Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT', __FILE__, __LINE__);
  132. $getimagesize = @GetImageSize($this->cache_filename);
  133. if ($getimagesize) {
  134. $this->DebugMessage('* Would have sent headers (2): Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]), __FILE__, __LINE__);
  135. }
  136. if (ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  137. $this->DebugMessage('* Would have sent headers (3): Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])), __FILE__, __LINE__);
  138. } else {
  139. $this->DebugMessage('* Would have sent data: readfile('.$this->cache_filename.')', __FILE__, __LINE__);
  140. }
  141. } else {
  142. /*
  143. if (headers_sent()) {
  144. $this->ErrorImage('Headers already sent ('.basename(__FILE__).' line '.__LINE__.')');
  145. exit;
  146. }*/
  147. $this->SendSaveAsFileHeaderIfNeeded();
  148. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $nModified).' GMT');
  149. if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && ($nModified == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && @$_SERVER['SERVER_PROTOCOL']) {
  150. header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
  151. exit;
  152. }
  153. $getimagesize = @GetImageSize($this->cache_filename);
  154. if ($getimagesize) {
  155. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($getimagesize[2]));
  156. } elseif (eregi('\.ico$', $this->cache_filename)) {
  157. header('Content-Type: image/x-icon');
  158. }
  159. if (!$this->config_cache_force_passthru && ereg('^'.preg_quote($nice_docroot).'(.*)$', $nice_cachefile, $matches)) {
  160. header('Location: '.dirname($matches[1]).'/'.urlencode(basename($matches[1])));
  161. } else {
  162. @readfile($this->cache_filename);
  163. }
  164. exit;
  165. }
  166. return true;
  167. }
  168. public function SendSaveAsFileHeaderIfNeeded() {
  169. if (headers_sent()) {
  170. return false;
  171. }
  172. $downloadfilename = phpthumb_functions::SanitizeFilename(@$_GET['sia'] ? $_GET['sia'] : (@$_GET['down'] ? $_GET['down'] : 'phpThumb_generated_thumbnail'.(@$_GET['f'] ? $_GET['f'] : 'jpg')));
  173. if (@$downloadfilename) {
  174. $this->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: '.(@$_GET['down'] ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"', __FILE__, __LINE__);
  175. header('Content-Disposition: '.(@$_GET['down'] ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  176. }
  177. return true;
  178. }
  179. }