Skip to content

Commit

Permalink
ensure the config file exists #104
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Sep 20, 2020
1 parent c486430 commit a484bf1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
111 changes: 60 additions & 51 deletions gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,61 +269,14 @@ public function onExtensionAfterSave($context, $table, $isNew, $data = [])
$table->store();
}

$options = json_decode(json_encode($data['params']['gzip']), JSON_OBJECT_AS_ARRAY);

$php_config = [];
$attributes = [];

foreach ($options['instantloading'] as $key => $value) {

switch ($key) {

case 'filters':

$attributes[] = $key . '="' . htmlspecialchars(json_encode(array_filter(preg_split('#\s+#s', $value, -1, PREG_SPLIT_NO_EMPTY), function ($value) {
return $value !== '';
})), ENT_QUOTES) . '"';
break;

case 'trigger':
case 'intensity':
case 'filter-type':
case 'allow-query-string':
case 'allow-external-links':

if (!empty($value)) {

$attributes[] = $key . '="' . $value . '"';
}

break;
}
}
$this->cleanCache();

$php_config['instantloading'] = $attributes;
$php_config['headers'] = $this->updateSecurityHeaders($options);
$options = json_decode(json_encode($data['params']['gzip']), JSON_OBJECT_AS_ARRAY);

$this->cleanCache();
// $this->updateSecurityHeaders($options);
$this->updateManifest($options);
$this->updateServiceWorker($options);


$path = JPATH_SITE . '/cache/z/app/' . $_SERVER['SERVER_NAME'] . '/';

if (!is_dir($path)) {

$old_mask = umask();

umask(022);
mkdir($path, 0755, true);
umask($old_mask);
}

file_put_contents($path . 'config.php', '<?php' . "\n" .
"defined('JPATH_PLATFORM') or die;\n\n" .
"\$php_config = " . var_export($php_config, true) . ';');

$this->updateConfig($options);
}

return true;
Expand All @@ -348,6 +301,11 @@ public function onAfterInitialise()
$this->updateServiceWorker($this->options);
}

if (!is_file(JPATH_SITE . '/cache/z/app/' . $_SERVER['SERVER_NAME'] . '/config.php')) {

$this->updateConfig($this->options);
}

if (is_file($file)) {

$this->worker_id = file_get_contents(JPATH_SITE . '/cache/z/app/' . $_SERVER['SERVER_NAME'] . '/worker_version');
Expand Down Expand Up @@ -1140,6 +1098,57 @@ protected function updateSecurityHeaders($options)
return $headers;
}

protected function updateConfig($options) {

$path = JPATH_SITE . '/cache/z/app/' . $_SERVER['SERVER_NAME'] . '/';

if (!is_dir($path)) {

$old_mask = umask();

umask(022);
mkdir($path, 0755, true);
umask($old_mask);
}


$php_config = [];
$attributes = [];

foreach ($options['instantloading'] as $key => $value) {

switch ($key) {

case 'filters':

$attributes[] = $key . '="' . htmlspecialchars(json_encode(array_filter(preg_split('#\s+#s', $value, -1, PREG_SPLIT_NO_EMPTY), function ($value) {
return $value !== '';
})), ENT_QUOTES) . '"';
break;

case 'trigger':
case 'intensity':
case 'filter-type':
case 'allow-query-string':
case 'allow-external-links':

if (!empty($value)) {

$attributes[] = $key . '="' . $value . '"';
}

break;
}
}

$php_config['instantloading'] = $attributes;
$php_config['headers'] = $this->updateSecurityHeaders($options);

file_put_contents($path . 'config.php', '<?php' . "\n" .
"defined('JPATH_PLATFORM') or die;\n\n" .
"\$php_config = " . var_export($php_config, true) . ';');
}

protected function updateManifest($options)
{

Expand Down Expand Up @@ -1484,7 +1493,7 @@ protected function cleanCache()

foreach (
[
// "config.php",
"config.php",
"headers.php",
"manifest.json",
"manifest_version",
Expand Down
6 changes: 3 additions & 3 deletions helpers/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function preProcessHTML($html, array $options = []) {
if (is_file($path)) {

include $path;
}

if (!empty($php_config['instantloading'])) {
if (!empty($php_config['instantloading'])) {

$html = str_replace('<body', '<body data-instant-'.implode(' data-instant-', $php_config['instantloading']), $html);
$html = str_replace('<body', '<body data-instant-'.implode(' data-instant-', $php_config['instantloading']), $html);
}
}
}

Expand Down

0 comments on commit a484bf1

Please sign in to comment.