Source of file ContentDeliveryService.php
Size: 3,325 Bytes - Last Modified: 2021-12-23T10:28:12+00:00
/var/www/docs.ssmods.com/process/src/code/content/ContentDeliveryService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 | <?php /** * Provides an interface to content delivery networks * * @author marcus@symbiote.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ class ContentDeliveryService { const CDN_THEME_PREFIX = 'cdntheme'; /** * @var ContentService */ public $contentService; private $cleanupFiles = array(); public static $dependencies = array( 'contentService' => '%$ContentService' ); public function removeLocalFile($filepath) { $this->cleanupFiles[] = $filepath; } public function cleanup() { foreach ($this->cleanupFiles as $path) { if (file_exists($path)) { @unlink($path); } } } /** * Get the first available CDN for a given theme * * @param string $theme * @return ThemeCdn */ public function getCdnForTheme($theme) { $cdn = ThemeCdn::get()->filter('Theme', $theme); return $cdn->first(); } /** * Store the contents of a folder on a CDN. * * If processReferences is set, relative URL references are attempted to be * detected and stored remotely as well, with the file to be stored rewritten * to refer to the CDN value. This really is only useful for CSS * * @param string $folder * @param boolean $processReferences */ public function storeThemeFile($toCdn, $file, $forceUpdate = false, $processReferences = false) { $mtime = @filemtime($file); $relativeName = self::CDN_THEME_PREFIX . '/' . $mtime . '/' . trim(str_replace(Director::baseFolder(), '', $file), '/'); if (!$forceUpdate) { // see if the file already exists, if not we do NOT do an update $reader = $this->contentService->findReaderFor($toCdn, $relativeName); if ($reader && $reader->exists()) { return $reader->getURL(); } } $clear = false; if ($processReferences) { $clear = true; $file = $this->processFileReferences($toCdn, $file, $forceUpdate); } // otherwise, lets get a content writer $writer = $this->contentService->getWriter($toCdn); try { $writer->write($file, $relativeName); } catch (Exception $e) { SS_Log::log($e, SS_Log::WARN); } if ($clear && strpos($file, '.cdn') > 0) { @unlink($file); } $id = $writer->getContentId(); return $writer->getReader()->getURL(); } protected function processFileReferences($toCdn, $file, $forceUpdate = false) { $content = file_get_contents($file); $processed = array(); if (preg_match_all('/url\((.*?)\)/', $content, $matches)) { foreach ($matches[1] as $segment) { $segment = trim($segment, '\'"'); if (strpos($segment, '#') !== false) { $segment = substr($segment, 0, strpos($segment, '#')); } if (isset($processed[$segment])) { continue; } if (strpos($segment, '//') !== false || $segment[0] == '/') { continue; } $realPath = realpath(dirname($file) .'/' . $segment); if (!strlen($realPath) || !file_exists($realPath)) { continue; } $replacement = $this->storeThemeFile($toCdn, $realPath, $forceUpdate); $content = str_replace($segment, $replacement, $content); $processed[$segment] = $replacement; } } if (count($processed)) { // we need to upload a temp version of the file $file = $file . '.cdn'; file_put_contents($file, $content); } return $file; } } |