silverstripe /
silverstripe-staticpublishqueue
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\StaticPublishQueue; |
||
| 4 | |||
| 5 | use SilverStripe\Control\Director; |
||
| 6 | use SilverStripe\Control\HTTPApplication; |
||
| 7 | use SilverStripe\Control\HTTPRequestBuilder; |
||
| 8 | use SilverStripe\Control\HTTPResponse; |
||
| 9 | use SilverStripe\Control\HTTPResponse_Exception; |
||
| 10 | use SilverStripe\Core\Config\Configurable; |
||
| 11 | use SilverStripe\Core\CoreKernel; |
||
| 12 | use SilverStripe\Core\Environment; |
||
| 13 | use SilverStripe\Core\Injector\Injectable; |
||
| 14 | use SilverStripe\ORM\DataObject; |
||
| 15 | use SilverStripe\ORM\FieldType\DBDatetime; |
||
| 16 | use SilverStripe\ORM\FieldType\DBField; |
||
| 17 | use SilverStripe\StaticPublishQueue\Contract\StaticPublisher; |
||
| 18 | use SilverStripe\View\ArrayData; |
||
| 19 | use SilverStripe\View\Requirements; |
||
| 20 | use SilverStripe\View\Requirements_Backend; |
||
| 21 | use SilverStripe\View\SSViewer; |
||
| 22 | |||
| 23 | abstract class Publisher implements StaticPublisher |
||
| 24 | { |
||
| 25 | use Injectable; |
||
| 26 | use Configurable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | * @config |
||
| 31 | */ |
||
| 32 | private static $static_publisher_themes = []; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * avoid caching any pages with name"SecurityID" - an indication that a |
||
| 36 | * form my be present that requires a fresh SecurityID |
||
| 37 | * @var bool |
||
| 38 | * @config |
||
| 39 | */ |
||
| 40 | private static $lazy_form_recognition = false; |
||
|
0 ignored issues
–
show
|
|||
| 41 | |||
| 42 | /** |
||
| 43 | * @config |
||
| 44 | * |
||
| 45 | * @var bool Use domain based caching (put cache files into a domain subfolder) |
||
| 46 | * This must be true if you are using this with the "subsites" module. |
||
| 47 | * Please note that this form of caching requires all URLs to be provided absolute |
||
| 48 | * (not relative to the webroot) via {@link SiteTree->AbsoluteLink()}. |
||
| 49 | */ |
||
| 50 | private static $domain_based_caching = false; |
||
|
0 ignored issues
–
show
|
|||
| 51 | |||
| 52 | /** |
||
| 53 | * @config |
||
| 54 | * |
||
| 55 | * @var bool Add a timestamp to the statically published output for HTML files |
||
| 56 | */ |
||
| 57 | private static $add_timestamp = false; |
||
|
0 ignored issues
–
show
|
|||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $url |
||
| 61 | * |
||
| 62 | * @return HTTPResponse |
||
| 63 | */ |
||
| 64 | public function generatePageResponse($url) |
||
| 65 | { |
||
| 66 | if (Director::is_relative_url($url)) { |
||
| 67 | $url = Director::absoluteURL($url); |
||
| 68 | } |
||
| 69 | $urlParts = parse_url($url); |
||
| 70 | if (!empty($urlParts['query'])) { |
||
| 71 | parse_str($urlParts['query'], $getVars); |
||
| 72 | } else { |
||
| 73 | $getVars = []; |
||
| 74 | } |
||
| 75 | // back up requirements backend |
||
| 76 | $origRequirements = Requirements::backend(); |
||
| 77 | Requirements::set_backend(Requirements_Backend::create()); |
||
| 78 | |||
| 79 | $origThemes = SSViewer::get_themes(); |
||
| 80 | $staticThemes = self::config()->get('static_publisher_themes'); |
||
| 81 | if ($staticThemes) { |
||
| 82 | SSViewer::set_themes($staticThemes); |
||
| 83 | } else { |
||
| 84 | // get the themes raw from config to prevent the "running from the CMS" problem where no themes are live |
||
| 85 | $rawThemes = SSViewer::config()->uninherited('themes'); |
||
| 86 | SSViewer::set_themes($rawThemes); |
||
| 87 | } |
||
| 88 | try { |
||
| 89 | $ssl = Environment::getEnv('SS_STATIC_FORCE_SSL'); |
||
| 90 | if (is_null($ssl)) { |
||
| 91 | $ssl = $urlParts['scheme'] == 'https' ? true : false; |
||
| 92 | } |
||
| 93 | |||
| 94 | // try to add all the server vars that would be needed to create a static cache |
||
| 95 | $request = HTTPRequestBuilder::createFromVariables( |
||
| 96 | [ |
||
| 97 | '_SERVER' => [ |
||
| 98 | 'REQUEST_URI' => isset($urlParts['path']) ? $urlParts['path'] : '', |
||
| 99 | 'REQUEST_METHOD' => 'GET', |
||
| 100 | 'REMOTE_ADDR' => '127.0.0.1', |
||
| 101 | 'HTTPS' => $ssl ? 'on' : 'off', |
||
| 102 | 'QUERY_STRING' => isset($urlParts['query']) ? $urlParts['query'] : '', |
||
| 103 | 'REQUEST_TIME' => DBDatetime::now()->getTimestamp(), |
||
| 104 | 'REQUEST_TIME_FLOAT' => (float) DBDatetime::now()->getTimestamp(), |
||
| 105 | 'HTTP_HOST' => $urlParts['host'] . (isset($urlParts['port']) ? ':' . $urlParts['port'] : ''), |
||
| 106 | 'HTTP_USER_AGENT' => 'silverstripe/staticpublishqueue', |
||
| 107 | ], |
||
| 108 | '_GET' => $getVars, |
||
| 109 | '_POST' => [], |
||
| 110 | ], |
||
| 111 | '' |
||
| 112 | ); |
||
| 113 | $app = $this->getHTTPApplication(); |
||
| 114 | $response = $app->handle($request); |
||
| 115 | |||
| 116 | if ($this->config()->get('add_timestamp')) { |
||
| 117 | $response->setBody( |
||
| 118 | str_replace( |
||
| 119 | '</html>', |
||
| 120 | '<!-- ' . DBDateTime::now()->Full() . " -->\n</html>", |
||
| 121 | $response->getBody() |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | } catch (HTTPResponse_Exception $e) { |
||
| 126 | $response = $e->getResponse(); |
||
| 127 | } finally { |
||
| 128 | // restore backends |
||
| 129 | SSViewer::set_themes($origThemes); |
||
| 130 | Requirements::set_backend($origRequirements); |
||
| 131 | DataObject::singleton()->flushCache(); |
||
| 132 | } |
||
| 133 | return $response; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return HTTPApplication |
||
| 138 | */ |
||
| 139 | protected function getHTTPApplication() |
||
| 140 | { |
||
| 141 | $kernel = new CoreKernel(BASE_PATH); |
||
| 142 | return new HTTPApplication($kernel); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Generate the templated content for a PHP script that can serve up the |
||
| 147 | * given piece of content with the given age and expiry. |
||
| 148 | * |
||
| 149 | * @param HTTPResponse $response |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | protected function generatePHPCacheFile($response) |
||
| 154 | { |
||
| 155 | $cacheConfig = [ |
||
| 156 | 'responseCode' => $response->getStatusCode(), |
||
| 157 | 'headers' => [], |
||
| 158 | ]; |
||
| 159 | |||
| 160 | foreach ($response->getHeaders() as $header => $value) { |
||
| 161 | if (!in_array($header, ['cache-control'], true)) { |
||
| 162 | $cacheConfig['headers'][] = sprintf('%s: %s', $header, $value); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | return "<?php\n\nreturn " . var_export($cacheConfig, true) . ';'; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $destination |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function generateHTMLCacheRedirection($destination) |
||
| 175 | { |
||
| 176 | return SSViewer::execute_template( |
||
| 177 | 'SilverStripe\\StaticPublishQueue\\HTMLRedirection', |
||
| 178 | ArrayData::create([ |
||
| 179 | 'URL' => DBField::create_field('Varchar', $destination), |
||
| 180 | ]) |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | } |
||
| 184 |