make handle_static its own extension

This commit is contained in:
Shish 2018-11-11 17:38:32 +00:00
parent 983b4d5d42
commit c9ccb22951
23 changed files with 70 additions and 39 deletions

2
.gitignore vendored
View File

@ -2,10 +2,8 @@ backup
data
images
thumbs
!lib/images
*.phar
*.sqlite
/lib/vendor/
#Composer
composer.phar

View File

@ -3,7 +3,7 @@ imports:
- php
filter:
excluded_paths: [lib/*,ext/*/lib/*,ext/tagger/script.js,ext/chatbox/*]
excluded_paths: [ext/*/lib/*,ext/tagger/script.js,ext/chatbox/*]
tools:
external_code_coverage: true

View File

@ -25,7 +25,7 @@ date_default_timezone_set('UTC');
<html>
<head>
<title>Shimmie Installation</title>
<link rel="shortcut icon" href="lib/static/favicon.ico">
<link rel="shortcut icon" href="ext/handle_static/static/favicon.ico">
<link rel="stylesheet" href="lib/shimmie.css" type="text/css">
<script type="text/javascript" src="vendor/bower-asset/jquery/dist/jquery.min.js"></script>
</head>

View File

@ -322,7 +322,7 @@ class Page {
$this->add_html_header("<script type='text/javascript'>base_href = '$data_href';</script>", 40);
# 404/static handler will map these to themes/foo/bar.ico or lib/static/bar.ico
# static handler will map these to themes/foo/static/bar.ico or ext/handle_static/static/bar.ico
$this->add_html_header("<link rel='icon' type='image/x-icon' href='$data_href/favicon.ico'>", 41);
$this->add_html_header("<link rel='apple-touch-icon' href='$data_href/apple-touch-icon.png'>", 42);
@ -335,7 +335,6 @@ class Page {
/*** Generate CSS cache files ***/
$css_latest = $config_latest;
$css_files = array_merge(
zglob("lib/shimmie.css"),
zglob("ext/{".ENABLED_EXTS."}/style.css"),
zglob("themes/$theme_name/style.css")
);
@ -365,9 +364,8 @@ class Page {
"vendor/bower-asset/jquery-timeago/jquery.timeago.js",
"vendor/bower-asset/tablesorter/jquery.tablesorter.min.js",
"vendor/bower-asset/js-cookie/src/js.cookie.js",
"lib/modernizr-3.3.1.custom.js",
"ext/handle_static/modernizr-3.3.1.custom.js",
],
zglob("lib/shimmie.js"),
zglob("ext/{".ENABLED_EXTS."}/script.js"),
zglob("themes/$theme_name/script.js")
);

View File

@ -37,7 +37,7 @@ _d("SEARCH_ACCEL", false); // boolean use search accelerator
_d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse
_d("VERSION", '2.7-beta'); // string shimmie version
_d("TIMEZONE", null); // string timezone
_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,comment,tag_list,index,tag_edit,alias_editor"); // extensions to always enable
_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor"); // extensions to always enable
_d("EXTRA_EXTS", ""); // string optional extra extensions
_d("BASE_URL", null); // string force a specific base URL (default is auto-detect)
_d("MIN_PHP_VERSION", '7.0');// string minimum supported PHP version

View File

@ -34,7 +34,7 @@ class BrowserSearch extends Extension {
$search_title = $config->get_string('title');
$search_form_url = make_link('post/list/{searchTerms}');
$suggenton_url = make_link('browser_search/')."{searchTerms}";
$icon_b64 = base64_encode(file_get_contents("lib/static/favicon.ico"));
$icon_b64 = base64_encode(file_get_contents("ext/handle_static/static/favicon.ico"));
// Now for the XML
$xml = "

View File

@ -5,7 +5,7 @@
* Link: http://code.shishnet.org/shimmie2/
* License: GPLv2
* Visibility: admin
* Description: If Shimmie can't handle a request, check static files; if that fails, show a 404
* Description: If no other extension puts anything onto the page, show 404
*/
class Handle404 extends Extension {
@ -14,29 +14,12 @@ class Handle404 extends Extension {
// hax.
if($page->mode == "page" && (!isset($page->blocks) || $this->count_main($page->blocks) == 0)) {
$h_pagename = html_escape(implode('/', $event->args));
$f_pagename = preg_replace("/[^a-z_\-\.]+/", "_", $h_pagename);
$theme_name = $config->get_string("theme", "default");
if(file_exists("themes/$theme_name/$f_pagename") || file_exists("lib/static/$f_pagename")) {
$filename = file_exists("themes/$theme_name/$f_pagename") ?
"themes/$theme_name/$f_pagename" : "lib/static/$f_pagename";
$page->add_http_header("Cache-control: public, max-age=600");
$page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT');
$page->set_mode("data");
$page->set_data(file_get_contents($filename));
if(endsWith($filename, ".ico")) $page->set_type("image/x-icon");
if(endsWith($filename, ".png")) $page->set_type("image/png");
if(endsWith($filename, ".txt")) $page->set_type("text/plain");
}
else {
log_debug("handle_404", "Hit 404: $h_pagename");
$page->set_code(404);
$page->set_title("404");
$page->set_heading("404 - No Handler Found");
$page->add_block(new NavBlock());
$page->add_block(new Block("Explanation", "No handler could be found for the page '$h_pagename'"));
}
log_debug("handle_404", "Hit 404: $h_pagename");
$page->set_code(404);
$page->set_title("404");
$page->set_heading("404 - No Handler Found");
$page->add_block(new NavBlock());
$page->add_block(new Block("Explanation", "No handler could be found for the page '$h_pagename'"));
}
}

View File

@ -6,9 +6,6 @@ class Handle404Test extends ShimmiePHPUnitTestCase {
$this->assert_text("No handler could be found for the page 'not/a/page'");
$this->assert_title('404');
$this->assert_response(404);
$this->get_page('favicon.ico');
$this->assert_response(200);
}
}

View File

@ -2,7 +2,7 @@
class IcoHandlerTest extends ShimmiePHPUnitTestCase {
public function testIcoHander() {
$this->log_in_as_user();
$image_id = $this->post_image("lib/static/favicon.ico", "shimmie favicon");
$image_id = $this->post_image("ext/handle_static/static/favicon.ico", "shimmie favicon");
$this->get_page("post/view/$image_id"); // test for no crash
# FIXME: test that the thumb works

View File

@ -0,0 +1,47 @@
<?php
/**
* Name: Static File Handler
* Author: Shish <webmaster@shishnet.org>
* Link: http://code.shishnet.org/shimmie2/
* License: GPLv2
* Visibility: admin
* Description: If Shimmie can't handle a request, check static files ($theme/static/$filename, then ext/handle_static/static/$filename)
*/
class HandleStatic extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $config, $page;
// hax.
if($page->mode == "page" && (!isset($page->blocks) || $this->count_main($page->blocks) == 0)) {
$h_pagename = html_escape(implode('/', $event->args));
$f_pagename = preg_replace("/[^a-z_\-\.]+/", "_", $h_pagename);
$theme_name = $config->get_string("theme", "default");
$theme_file = "themes/$theme_name/static/$f_pagename";
$static_file = "ext/handle_static/static/$f_pagename";
if(file_exists($theme_file) || file_exists($static_file)) {
$filename = file_exists($theme_file) ? $theme_file : $static_file;
$page->add_http_header("Cache-control: public, max-age=600");
$page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT');
$page->set_mode("data");
$page->set_data(file_get_contents($filename));
if(endsWith($filename, ".ico")) $page->set_type("image/x-icon");
if(endsWith($filename, ".png")) $page->set_type("image/png");
if(endsWith($filename, ".txt")) $page->set_type("text/plain");
}
}
}
private function count_main($blocks) {
$n = 0;
foreach($blocks as $block) {
if($block->section == "main" && $block->is_content) $n++; // more hax.
}
return $n;
}
public function get_priority(): int {return 98;} // before 404
}

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

View File

Before

Width:  |  Height:  |  Size: 265 B

After

Width:  |  Height:  |  Size: 265 B

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 43 B

After

Width:  |  Height:  |  Size: 43 B

View File

@ -0,0 +1,8 @@
<?php
class HandleStaticTest extends ShimmiePHPUnitTestCase {
public function testStaticHandler() {
$this->get_page('favicon.ico');
$this->assert_response(200);
}
}

View File

@ -59,7 +59,7 @@ if(!file_exists("vendor/")) {
<html>
<head>
<title>Shimmie Error</title>
<link rel="shortcut icon" href="lib/static/favicon.ico">
<link rel="shortcut icon" href="ext/handle_static/static/favicon.ico">
<link rel="stylesheet" href="lib/shimmie.css" type="text/css">
</head>
<body>