Skip to content

Commit

Permalink
* Rewrote the weekly maintenance task to go through an improved creat…
Browse files Browse the repository at this point in the history
…e_generic_folders function (which now supports re-creating deleted generic files inside non-deleted generic folders), instead of just dealing with the cache folder. That kind of task should probably be airing hourly rather than weekly, but it's a start... (ScheduledTasks.php)

* CSS and JS cache htaccess files are now a bit shorter, because they no longer specify commands for the other (unused) type. (OriginalFiles.php)
  • Loading branch information
Nao committed Mar 23, 2014
1 parent 599afb6 commit d287660
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 53 deletions.
34 changes: 21 additions & 13 deletions core/app/OriginalFiles.php
Expand Up @@ -57,7 +57,10 @@ function create_settings_file()
file_put_contents(ROOT_DIR . $target, $file . "\n?" . '>');
}

function create_generic_folders()
// Go through the folder tree, look for folders that have yet to be created,
// create them as needed, and create index.php and .htaccess files if required.
// $force is used by the weekly maintenance task to re-create files if deleted.
function create_generic_folders($force = false)
{
$folders = array(
'attachments' => true,
Expand All @@ -81,31 +84,36 @@ function create_generic_folders()

foreach ($folders as $key => $folder)
{
if (!file_exists(ROOT_DIR . '/' . $key))
create_generic_folder(ROOT_DIR, $key);
if ($force || !file_exists(ROOT_DIR . '/' . $key))
create_generic_folder(ROOT_DIR, $key, $force);
if (is_array($folder))
foreach ($folder as $sub_folder)
if (!file_exists(ROOT_DIR . '/' . $key . '/' . $sub_folder))
create_generic_folder(ROOT_DIR, $key . '/' . $sub_folder);
if ($force || !file_exists(ROOT_DIR . '/' . $key . '/' . $sub_folder))
create_generic_folder(ROOT_DIR, $key . '/' . $sub_folder, $force);
}
}

function create_generic_folder($root_dir, $folder)
function create_generic_folder($root_dir, $folder, $force = false)
{
// We're gonna let PHP output warnings or errors on mkdir and copy, because it's serious stuff.
$path = str_replace('/', DIRECTORY_SEPARATOR, $root_dir . '/' . $folder);
mkdir($path);
if (!$force || !file_exists($path))
mkdir($path);

// We need to put and index.php file in all folders.
file_put_contents($path . '/index.php', '<' . '?php
if (!$force || !file_exists($path . '/index.php'))
file_put_contents($path . '/index.php', '<' . '?php
// Redirect to the upper level.
header(\'Location: ../\');');

// Copy assets/icons/media to media/icons. After that, you can freely delete the originals.
if ($folder == 'media/icons' && file_exists($root_dir . '/assets/icons/media'))
if (!$force && ($folder == 'media/icons' && file_exists($root_dir . '/assets/icons/media')))
foreach (glob($root_dir . '/assets/icons/media/*.png') as $png_file)
copy($png_file, $root_dir . '/media/icons/' . basename($png_file));

if ($force && file_exists($path . '/.htaccess'))
return;

// We need to be able to access images and various other files from plugins/, but not the archives of plugins themselves.
if ($folder == 'plugins')
$file = '
Expand Down Expand Up @@ -149,15 +157,15 @@ function create_generic_folder($root_dir, $folder)
</Files>
<IfModule mod_mime.c>
AddEncoding x-gzip .gz
AddEncoding x-gzip .cgz
AddEncoding x-gzip .gz' . ($folder == 'gz/js' ? '
AddEncoding x-gzip .jgz
<FilesMatch "\.(js\.gz|jgz)$">
ForceType text/javascript
</FilesMatch>
</FilesMatch>' : '
AddEncoding x-gzip .cgz
<FilesMatch "\.(css\.gz|cgz)$">
ForceType text/css
</FilesMatch>
</FilesMatch>') . '
</IfModule>
<IfModule mod_headers.c>
Expand Down
43 changes: 3 additions & 40 deletions core/app/ScheduledTasks.php
Expand Up @@ -1401,46 +1401,9 @@ function scheduled_weekly_maintenance()
)
);

// Check the cache folders etc. have what they're supposed to have. Just remember that we have to mask things from occasional silly hosts.
if (!file_exists(CACHE_DIR . '/index.php'))
@file_put_contents(CACHE_DIR . '/index.php', '<' . '?ph' . "p\n\n// Redirect to the upper level.\nheader('Location: ../');\n");

if (!file_exists($cachedir . '/.htaccess'))
@file_put_contents($cachedir . '/.htaccess', '<Files *.php>
Deny from all
</Files>
<Files *.zip>
Deny from all
</Files>
<Files *.lock>
Deny from all
</Files>
<Files index.php>
Allow from all
</Files>
<IfModule mod_mime.c>
AddEncoding x-gzip .gz
AddEncoding x-gzip .cgz
AddEncoding x-gzip .jgz
<FilesMatch "\.(js\.gz|jgz)$">
ForceType text/javascript
</FilesMatch>
<FilesMatch "\.(css\.gz|cgz)$">
ForceType text/css
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "max-age=2592000"
Header set Expires "Thu, 21 March 2025 03:42:00 GMT"
Header set Vary "Accept-Encoding"
</IfModule>
FileETag none');
// Check whether the generic folders were removed or something.
loadSource('OriginalFiles');
create_generic_folders(true);

return true;
}
Expand Down

0 comments on commit d287660

Please sign in to comment.